natiny.font.measure_wrapped
Lua
natiny.font.measure_wrapped(font, text [, max_width]) -> width, height, lines
Lays text out and reports the size of the block it makes, without drawing it.
Parameters
| Name |
Type |
Default |
Description |
font |
number |
|
Font handle from natiny.font.create |
text |
string |
|
UTF-8 text to lay out |
max_width |
number |
0 |
Wrap width in drawing units; values at or below 0 do not wrap |
Returns
| Type |
Description |
number |
Width of the widest line in drawing units |
number |
Height of the block in drawing units |
number |
Number of lines in the block |
Example
Lua
local w, h, lines = natiny.font.measure_wrapped(font, message, 300)
-- a panel that fits its text
natiny.render.set_color(0.1, 0.1, 0.12, 1)
natiny.render.rectangle(20, 20, w + 16, h + 16)
natiny.render.set_color(1, 1, 1, 1)
natiny.render.text_box(font, 28, 28, 300, h, message)
Notes
- Breaks the text exactly as
natiny.render.text_box does, so a box sized
from this holds what that draws into it.
- Lines break at spaces and tabs, at
\n, and between Han, kana and Hangul
characters, which are written without spaces. A word longer than
max_width is cut where it stops fitting rather than allowed to overflow.
- Trailing spaces are neither measured nor drawn, and a wrapped line never
begins with one.
- The height is
lines times natiny.font.get_line_height, so it follows
natiny.font.set_line_spacing.
- An empty string is one empty line: the width is
0, the height is one
line, and lines is 1.