natiny.font.offset_at

Lua
natiny.font.offset_at(font, text, x) -> offset

Returns the byte offset in text nearest to horizontal position x.

Parameters

Name Type Description
font number Font handle from natiny.font.create
text string UTF-8 text of one line
x number Distance from the start of the line, in drawing units

Returns

Type Description
number Byte offset from 0 through #text

Example

Lua
-- where a click puts the caret
local caret = natiny.font.offset_at(font, line, mouse_x - text_x)

local before = line:sub(1, caret)
local after  = line:sub(caret + 1)
natiny.render.text(font, text_x, y, before .. "|" .. after)