natiny_font_offset_at
C
uint32_t natiny_font_offset_at(NatinyFont font, const char* text, float x);
Returns the byte offset in text nearest to horizontal position x.
Parameters
| Name |
Type |
Description |
font |
NatinyFont |
Font handle from natiny_font_create |
text |
const char* |
Null-terminated UTF-8 text of one line |
x |
float |
Distance from the start of the line, in drawing units |
Returns
| Type |
Description |
uint32_t |
Byte offset from 0 through strlen(text); 0 for an invalid handle or NULL text |
Example
C
uint32_t caret = natiny_font_offset_at(font, line, mouse_x - text_x);
/* the text up to the caret */
fwrite(line, 1, caret, stdout);
Notes
- The offset is a byte count into
text and always lands on a UTF-8
boundary, never inside a character.
- Past the middle of a glyph the answer is the offset after it, which is what
makes a click land where the eye put it.
- One line at a time:
\n ends the search and its byte offset is returned.
- The position accounts for kerning, letter spacing, and tab stops in the
same way as
natiny_font_measure_text.