Module i_slint_core::textlayout
source · [−]Expand description
module for basic text layout
The basic algorithm for breaking text into multiple lines:
-
First we determine the boundaries for text shaping. As shaping happens based on a single font and we know that different fonts cater different writing systems, we split up the text into chunks that maximize our chances of finding a font that covers all glyphs in the chunk. This way for example arabic text can be covered by a font that has excellent arabic coverage while latin text is rendered using a different font. Shaping boundaries are always also grapheme boundaries.
-
Then we shape the text at shaping boundaries, to determine the metrics of glyphs and glyph clusters (grapheme boundaries with the shapable)
-
Allocate graphemes into new text lines until all graphemes are consumed:
-
Loop over all graphemes: Compute the width of the grapheme Determine if the grapheme is produced by a white space character If grapheme is not at break opportunity: Add grapheme to fragment // Fall back to breaking anywhere if we can’t find any other break point If the current fragment ends before the first break opportunity and width of current line + fragment <= available width: Add fragment to current line Clear fragment If width of current line <= available width AND width of current line + fragment > available width: Emit current line Current line starts with fragment Clear fragment Else: Continue Else if break opportunity at grapheme boundary is optional OR if current is space and next is optional: If width of current line + fragment <= available width: Add fragment to current line Clear fragment Else: Emit current line Current line starts with fragment Clear fragment Add grapheme to fragment
Else if break opportunity at grapheme boundary is mandatory: Add fragment to current line Emit current line Clear fragment Add grapheme to fragment