From b57625296376ef8d3c7b324ab9a3aa7d1af8c0cc Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Tue, 17 Dec 2024 10:00:32 -0500 Subject: [PATCH] text: allow disabling space trimming This commit adds a shaping parameter that disables the trimming of trailing whitespace from lines. Text editors and similar use-cases want trailing whitspace glyphs to be selectable, which means they must occupy space. Signed-off-by: Chris Waldon --- text/gotext.go | 9 +++++---- text/shaper.go | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/text/gotext.go b/text/gotext.go index c7ee3480..60670407 100644 --- a/text/gotext.go +++ b/text/gotext.go @@ -490,10 +490,11 @@ func wrapPolicyToGoText(p WrapPolicy) shaping.LineBreakPolicy { // shapeAndWrapText invokes the text shaper and returns wrapped lines in the shaper's native format. func (s *shaperImpl) shapeAndWrapText(params Parameters, txt []rune) (_ []shaping.Line, truncated int) { wc := shaping.WrapConfig{ - Direction: mapDirection(params.Locale.Direction), - TruncateAfterLines: params.MaxLines, - TextContinues: params.forceTruncate, - BreakPolicy: wrapPolicyToGoText(params.WrapPolicy), + Direction: mapDirection(params.Locale.Direction), + TruncateAfterLines: params.MaxLines, + TextContinues: params.forceTruncate, + BreakPolicy: wrapPolicyToGoText(params.WrapPolicy), + DisableTrailingWhitespaceTrim: params.DisableSpaceTrim, } families := s.defaultFaces if params.Font.Typeface != "" { diff --git a/text/shaper.go b/text/shaper.go index ecd89878..db679268 100644 --- a/text/shaper.go +++ b/text/shaper.go @@ -76,6 +76,11 @@ type Parameters struct { // text with a MaxLines. It is unexported because this behavior only makes sense for the // shaper to control when it iterates paragraphs of text. forceTruncate bool + + // DisableSpaceTrim prevents the width of the final whitespace glyph on a line from being zeroed. + // This is desirable for text editors (so that the whitespace can be selected), but is undesirable + // for ordinary display text. + DisableSpaceTrim bool } type FontFace = giofont.FontFace