text: simplify text layout and shaping API

First, replace LayoutOptions with an explicit maximum width parameter.  The
single-field option struct doesn't carry its weight, and I don't think we'll
see more global layout options in the future. Rather, I expect options to cover
spans of text or be part of a Font.

Second, replace the unit.Converter with an scaled text size. It's simpler and
allow the Editor and similar widgets to easily detect whether their cached
layouts are stale. Package text no longer depends on package unit, which is
now dealt with at the widget-level only.

Finally, remove the Size field from Font. It was a design mistake: a Font is
assumed to cover all sizes, as evidenced by the FontRegistry disregarding
Size when looking up fonts.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-02-03 23:19:28 +01:00
parent dcacca3442
commit 4c220f4554
12 changed files with 68 additions and 85 deletions
+5 -3
View File
@@ -12,6 +12,7 @@ import (
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"golang.org/x/image/math/fixed"
)
@@ -83,9 +84,10 @@ func (l *lineIterator) Next() (int, int, []text.Glyph, f32.Point, bool) {
return 0, 0, nil, f32.Point{}, false
}
func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, txt string) {
func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, size unit.Value, txt string) {
cs := gtx.Constraints
lines := s.LayoutString(gtx, font, txt, text.LayoutOptions{MaxWidth: cs.Width.Max})
textSize := fixed.I(gtx.Px(size))
lines := s.LayoutString(font, textSize, cs.Width.Max, txt)
if max := l.MaxLines; max > 0 && len(lines) > max {
lines = lines[:max]
}
@@ -109,7 +111,7 @@ func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, txt st
stack.Push(gtx.Ops)
op.TransformOp{}.Offset(off).Add(gtx.Ops)
str := txt[start:end]
s.ShapeString(gtx, font, str, layout).Add(gtx.Ops)
s.ShapeString(font, textSize, str, layout).Add(gtx.Ops)
paint.PaintOp{Rect: lclip}.Add(gtx.Ops)
stack.Pop()
}