mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 10:25:37 +00:00
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 <mail@eliasnaur.com>
This commit is contained in:
@@ -179,7 +179,7 @@ func layoutText(ppem fixed.Int26_6, str string, f *opentype, opts text.LayoutOpt
|
|||||||
}
|
}
|
||||||
for prev.idx < len(str) {
|
for prev.idx < len(str) {
|
||||||
c, s := utf8.DecodeRuneInString(str[prev.idx:])
|
c, s := utf8.DecodeRuneInString(str[prev.idx:])
|
||||||
nl := text.IsNewline(c)
|
nl := c == '\n'
|
||||||
if opts.SingleLine && nl {
|
if opts.SingleLine && nl {
|
||||||
nl = false
|
nl = false
|
||||||
c = ' '
|
c = ' '
|
||||||
|
|||||||
+1
-1
@@ -334,7 +334,7 @@ func (e *Editor) layoutText() {
|
|||||||
// up all available space.
|
// up all available space.
|
||||||
if len(s) > 0 {
|
if len(s) > 0 {
|
||||||
r, _ := utf8.DecodeLastRuneInString(s)
|
r, _ := utf8.DecodeLastRuneInString(s)
|
||||||
if !IsNewline(r) {
|
if r != '\n' {
|
||||||
dims.Size.X = e.maxWidth
|
dims.Size.X = e.maxWidth
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-4
@@ -1,5 +1,6 @@
|
|||||||
// SPDX-License-Identifier: Unlicense OR MIT
|
// SPDX-License-Identifier: Unlicense OR MIT
|
||||||
|
|
||||||
|
// Package text implements text widgets.
|
||||||
package text
|
package text
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -15,12 +16,20 @@ import (
|
|||||||
"golang.org/x/image/math/fixed"
|
"golang.org/x/image/math/fixed"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Label is a widget for laying out and drawing text.
|
||||||
type Label struct {
|
type Label struct {
|
||||||
Face Face
|
// Face define the font and style of the text.
|
||||||
Material ui.MacroOp
|
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
|
Alignment Alignment
|
||||||
Text string
|
// Text is the string to draw.
|
||||||
MaxLines int
|
Text string
|
||||||
|
// MaxLines specify the maximum number of lines the text
|
||||||
|
// may fill.
|
||||||
|
MaxLines int
|
||||||
|
|
||||||
it lineIterator
|
it lineIterator
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-10
@@ -11,6 +11,7 @@ import (
|
|||||||
"golang.org/x/image/math/fixed"
|
"golang.org/x/image/math/fixed"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A Line contains the measurements of a line of text.
|
||||||
type Line struct {
|
type Line struct {
|
||||||
Text String
|
Text String
|
||||||
// Width is the width of the line.
|
// Width is the width of the line.
|
||||||
@@ -25,22 +26,31 @@ type Line struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type String struct {
|
type String struct {
|
||||||
String string
|
String string
|
||||||
|
// Advances contain the advance of each rune in String.
|
||||||
Advances []fixed.Int26_6
|
Advances []fixed.Int26_6
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A Layout contains the measurements of a body of text as
|
||||||
|
// a list of Lines.
|
||||||
type Layout struct {
|
type Layout struct {
|
||||||
Lines []Line
|
Lines []Line
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LayoutOptions specify the constraints of a text layout.
|
||||||
type LayoutOptions struct {
|
type LayoutOptions struct {
|
||||||
MaxWidth int
|
// MaxWidth set the maximum width of the layout.
|
||||||
|
MaxWidth int
|
||||||
|
// SingleLine specify that line breaks are ignored.
|
||||||
SingleLine bool
|
SingleLine bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Face interface {
|
type Face interface {
|
||||||
Layout(str string, opts LayoutOptions) *Layout
|
// Layout returns the text layout for a string given a set of
|
||||||
Path(str String) ui.MacroOp
|
// 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
|
type Alignment uint8
|
||||||
@@ -48,7 +58,7 @@ type Alignment uint8
|
|||||||
const (
|
const (
|
||||||
Start Alignment = iota
|
Start Alignment = iota
|
||||||
End
|
End
|
||||||
Center
|
Middle
|
||||||
)
|
)
|
||||||
|
|
||||||
func linesDimens(lines []Line) layout.Dimens {
|
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 {
|
func align(align Alignment, width fixed.Int26_6, maxWidth int) fixed.Int26_6 {
|
||||||
mw := fixed.I(maxWidth)
|
mw := fixed.I(maxWidth)
|
||||||
switch align {
|
switch align {
|
||||||
case Center:
|
case Middle:
|
||||||
return fixed.I(((mw - width) / 2).Floor())
|
return fixed.I(((mw - width) / 2).Floor())
|
||||||
case End:
|
case End:
|
||||||
return fixed.I((mw - width).Floor())
|
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))
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user