From 2f9ac5aebbe911de327f9043a7ad1452607f3843 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 11 Aug 2019 19:25:50 +0200 Subject: [PATCH] ui/text: rename Center Alignment to Middle and drop IsNewline Middle matches the similar layout.Middle constant, and IsNewline is too simple to export. Add documentation while we're here. Signed-off-by: Elias Naur --- ui/measure/measure.go | 2 +- ui/text/editor.go | 2 +- ui/text/label.go | 17 +++++++++++++---- ui/text/measure.go | 39 +++++++++++++++++++++++++++++---------- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/ui/measure/measure.go b/ui/measure/measure.go index 5821f27f..112700b7 100644 --- a/ui/measure/measure.go +++ b/ui/measure/measure.go @@ -179,7 +179,7 @@ func layoutText(ppem fixed.Int26_6, str string, f *opentype, opts text.LayoutOpt } for prev.idx < len(str) { c, s := utf8.DecodeRuneInString(str[prev.idx:]) - nl := text.IsNewline(c) + nl := c == '\n' if opts.SingleLine && nl { nl = false c = ' ' diff --git a/ui/text/editor.go b/ui/text/editor.go index c9c505e5..d68981b1 100644 --- a/ui/text/editor.go +++ b/ui/text/editor.go @@ -334,7 +334,7 @@ func (e *Editor) layoutText() { // up all available space. if len(s) > 0 { r, _ := utf8.DecodeLastRuneInString(s) - if !IsNewline(r) { + if r != '\n' { dims.Size.X = e.maxWidth break } diff --git a/ui/text/label.go b/ui/text/label.go index e32071d7..fcb0dbf2 100644 --- a/ui/text/label.go +++ b/ui/text/label.go @@ -1,5 +1,6 @@ // SPDX-License-Identifier: Unlicense OR MIT +// Package text implements text widgets. package text import ( @@ -15,12 +16,20 @@ import ( "golang.org/x/image/math/fixed" ) +// Label is a widget for laying out and drawing text. type Label struct { - Face Face - Material ui.MacroOp + // Face define the font and style of the text. + Face Face + // Material is a macro recording the material to draw the + // text. Use a ColorOp for colored text. + Material ui.MacroOp + // Alignment specify the text alignment. Alignment Alignment - Text string - MaxLines int + // Text is the string to draw. + Text string + // MaxLines specify the maximum number of lines the text + // may fill. + MaxLines int it lineIterator } diff --git a/ui/text/measure.go b/ui/text/measure.go index 5a644191..7418ba56 100644 --- a/ui/text/measure.go +++ b/ui/text/measure.go @@ -11,6 +11,7 @@ import ( "golang.org/x/image/math/fixed" ) +// A Line contains the measurements of a line of text. type Line struct { Text String // Width is the width of the line. @@ -25,22 +26,31 @@ type Line struct { } type String struct { - String string + String string + // Advances contain the advance of each rune in String. Advances []fixed.Int26_6 } +// A Layout contains the measurements of a body of text as +// a list of Lines. type Layout struct { Lines []Line } +// LayoutOptions specify the constraints of a text layout. type LayoutOptions struct { - MaxWidth int + // MaxWidth set the maximum width of the layout. + MaxWidth int + // SingleLine specify that line breaks are ignored. SingleLine bool } type Face interface { - Layout(str string, opts LayoutOptions) *Layout - Path(str String) ui.MacroOp + // Layout returns the text layout for a string given a set of + // options. + Layout(s string, opts LayoutOptions) *Layout + // Path returns the ClipOp outline of a text recorded in a macro. + Path(s String) ui.MacroOp } type Alignment uint8 @@ -48,7 +58,7 @@ type Alignment uint8 const ( Start Alignment = iota End - Center + Middle ) func linesDimens(lines []Line) layout.Dimens { @@ -77,14 +87,10 @@ func linesDimens(lines []Line) layout.Dimens { } } -func IsNewline(r rune) bool { - return r == '\n' -} - func align(align Alignment, width fixed.Int26_6, maxWidth int) fixed.Int26_6 { mw := fixed.I(maxWidth) switch align { - case Center: + case Middle: return fixed.I(((mw - width) / 2).Floor()) case End: return fixed.I((mw - width).Floor()) @@ -94,3 +100,16 @@ func align(align Alignment, width fixed.Int26_6, maxWidth int) fixed.Int26_6 { panic(fmt.Errorf("unknown alignment %v", align)) } } + +func (a Alignment) String() string { + switch a { + case Start: + return "Start" + case End: + return "End" + case Middle: + return "Middle" + default: + panic("unreachable") + } +}