mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
go.*,text,widget{,/material}: enable configurable line wrapping within words
This commit enables consumers of the text shaper to select a policy for how line breaking candidates will be chosen. The new default policy can break lines within "words" (UAX#14 segments) when words do not fit by themselves on a line. This ensures that text does not horizontally overflow its bounding box unless the available width is insufficient to display a single UAX#29 grapheme cluster. Fixes: https://todo.sr.ht/~eliasnaur/gio/467 Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
+13
-1
@@ -401,11 +401,23 @@ func (s *shaperImpl) shapeText(faces []font.Face, ppem fixed.Int26_6, lc system.
|
||||
return s.outScratchBuf
|
||||
}
|
||||
|
||||
func wrapPolicyToGoText(p WrapPolicy) shaping.LineBreakPolicy {
|
||||
switch p {
|
||||
case WrapGraphemes:
|
||||
return shaping.Always
|
||||
case WrapWords:
|
||||
return shaping.Never
|
||||
default:
|
||||
return shaping.WhenNecessary
|
||||
}
|
||||
}
|
||||
|
||||
// shapeAndWrapText invokes the text shaper and returns wrapped lines in the shaper's native format.
|
||||
func (s *shaperImpl) shapeAndWrapText(faces []font.Face, params Parameters, txt []rune) (_ []shaping.Line, truncated int) {
|
||||
wc := shaping.WrapConfig{
|
||||
TruncateAfterLines: params.MaxLines,
|
||||
TextContinues: params.forceTruncate,
|
||||
BreakPolicy: wrapPolicyToGoText(params.WrapPolicy),
|
||||
}
|
||||
if wc.TruncateAfterLines > 0 {
|
||||
if len(params.Truncator) == 0 {
|
||||
@@ -416,7 +428,7 @@ func (s *shaperImpl) shapeAndWrapText(faces []font.Face, params Parameters, txt
|
||||
wc.Truncator = s.shapeText(faces, params.PxPerEm, params.Locale, []rune(params.Truncator))[0]
|
||||
}
|
||||
// Wrap outputs into lines.
|
||||
return s.wrapper.WrapParagraph(wc, params.MaxWidth, txt, s.shapeText(faces, params.PxPerEm, params.Locale, txt)...)
|
||||
return s.wrapper.WrapParagraph(wc, params.MaxWidth, txt, shaping.NewSliceIterator(s.shapeText(faces, params.PxPerEm, params.Locale, txt)))
|
||||
}
|
||||
|
||||
// replaceControlCharacters replaces problematic unicode
|
||||
|
||||
Reference in New Issue
Block a user