From 682d2810d32adf37f875a41ed1cd7d64fe0cce7e Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sat, 9 Nov 2019 20:00:09 +0100 Subject: [PATCH] text: remove SingleLine from LayoutOptions Low level text layout should not deal with filtering newlines. Updates gio#61 Signed-off-by: Elias Naur --- font/opentype/opentype.go | 8 +------- text/text.go | 4 +--- widget/editor.go | 7 ++++--- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/font/opentype/opentype.go b/font/opentype/opentype.go index b9057571..6d2f222d 100644 --- a/font/opentype/opentype.go +++ b/font/opentype/opentype.go @@ -81,12 +81,6 @@ func layoutText(buf *sfnt.Buffer, ppem fixed.Int26_6, str string, f *opentype, o } for prev.idx < len(str) { c, s := utf8.DecodeRuneInString(str[prev.idx:]) - nl := c == '\n' - if opts.SingleLine && nl { - nl = false - c = ' ' - s = 1 - } a, ok := f.GlyphAdvance(buf, ppem, c) if !ok { prev.idx += s @@ -99,7 +93,7 @@ func layoutText(buf *sfnt.Buffer, ppem fixed.Int26_6, str string, f *opentype, o x: prev.x + prev.adv, valid: true, } - if nl { + if c == '\n' { // The newline is zero width; use the previous // character for line measurements. prev.advs = append(prev.advs, 0) diff --git a/text/text.go b/text/text.go index b2b4841f..9c968ac2 100644 --- a/text/text.go +++ b/text/text.go @@ -36,10 +36,8 @@ type Layout struct { // LayoutOptions specify the constraints of a text layout. type LayoutOptions struct { - // MaxWidth set the maximum width of the layout. + // MaxWidth is the available width of the layout. MaxWidth int - // SingleLine specify that line breaks are ignored. - SingleLine bool } // Style is the font style. diff --git a/widget/editor.go b/widget/editor.go index 21809c9e..6c2a64d8 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -5,6 +5,7 @@ package widget import ( "image" "math" + "strings" "time" "unicode/utf8" @@ -399,7 +400,7 @@ func (e *Editor) moveCoord(c unit.Converter, pos image.Point) { func (e *Editor) layoutText(c unit.Converter, s *text.Shaper, font text.Font) ([]text.Line, layout.Dimensions) { txt := e.rr.String() - opts := text.LayoutOptions{SingleLine: e.SingleLine, MaxWidth: e.maxWidth} + opts := text.LayoutOptions{MaxWidth: e.maxWidth} textLayout := s.Layout(c, font, txt, opts) lines := textLayout.Lines dims := linesDimens(lines) @@ -463,8 +464,8 @@ func (e *Editor) deleteRuneForward() { } func (e *Editor) append(s string) { - if e.SingleLine && s == "\n" { - return + if e.SingleLine { + s = strings.ReplaceAll(s, "\n", "") } e.prepend(s) e.rr.caret += len(s)