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
+2 -10
View File
@@ -6,7 +6,6 @@ import (
"io"
"gioui.org/op"
"gioui.org/unit"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
@@ -32,23 +31,16 @@ type Glyph struct {
Advance fixed.Int26_6
}
// LayoutOptions specify the constraints of a text layout.
type LayoutOptions struct {
// MaxWidth is the available width of the layout.
MaxWidth int
}
// Style is the font style.
type Style int
// Weight is a font weight, in CSS units.
type Weight int
// Font specify a particular typeface, style and size.
// Font specify a particular typeface variant, style and weight.
type Font struct {
Typeface Typeface
Variant Variant
Size unit.Value
Style Style
// Weight is the text weight. If zero, Normal is used instead.
Weight Weight
@@ -56,7 +48,7 @@ type Font struct {
// Face implements text layout and shaping for a particular font.
type Face interface {
Layout(ppem fixed.Int26_6, txt io.Reader, opts LayoutOptions) ([]Line, error)
Layout(ppem fixed.Int26_6, maxWidth int, txt io.Reader) ([]Line, error)
Shape(ppem fixed.Int26_6, str []Glyph) op.CallOp
Metrics(ppem fixed.Int26_6) font.Metrics
}