UI module

index_at_x

procedureextensionexperimentalsince 0.31.0Source

The byte offset in string_to_measure of the grapheme boundary nearest to horizontal position x.

Example↗
ui := headless();
defer free_ui(*ui);
index := index_at_x(*ui.text, "abcdef", .{ size = 10 }, 13);  // x = 13 falls in the third character
assert(index == 2);
index_at_x :: (
    text: *Text_System,
    string_to_measure: string,
    style: Text_Style,
    x: float
) -> int

The byte offset in string_to_measure of the grapheme boundary nearest to horizontal position x.

Parameters

text: *Text_System
the text system holding the fonts.
string_to_measure: string
the UTF-8 text.
style: Text_Style
the font and size the text is drawn with.
x: float
the position in points from the start of the text. Zero or less gives 0.

Returns

int
Example↗
ui := headless();
defer free_ui(*ui);
index := index_at_x(*ui.text, "abcdef", .{ size = 10 }, 13);  // x = 13 falls in the third character
assert(index == 2);

See also