-
I want to implement text wrapping for my ui library and it requires measuring the dimensions of a string of text so I can compare it to the line width. Is there an example with this library to do so? It would need to take into consideration emojis and kerning. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Unfortunately there is no high-level API that will take an arbitrary Unicode string and return dimensions. You will need to divide up the text by script and perform font matching to determine which font will be used for each run of characters. For a given run of characters in a font and script you would do the following:
https://github.com/yeslogic/allsorts-tools/blob/6ed62c379913fa2c819d52a90c2f4b5efda103ca/src/shape.rs covers steps 1 to 4. Step 5 is something like glyph_positions returns dimensions in font units. You will need to scale this to your desired font size using the units per em in the let font_size = 24.0;
let head = font.head_table()?.ok_or(ParseError::MissingValue)?;
let scale = font_size / f32::from(head.units_per_em); |
Beta Was this translation helpful? Give feedback.
Unfortunately there is no high-level API that will take an arbitrary Unicode string and return dimensions. You will need to divide up the text by script and perform font matching to determine which font will be used for each run of characters. For a given run of characters in a font and script you would do the following:
https://github.com/yeslogic/allsorts-tools/blob/6ed62c379913fa2c819d52a90c2f4b5efda103ca/src/shape.rs covers steps 1 to 4. Step 5 is something like
positions.iter().map(|pos| pos.hori_advance).sum()
.glyph_positions returns dimensions in font…