text: replace Family with Shaper, add Font, Face

There is now a single shaping implementation, Shaper, for all fonts, replacing
Family that only covered a single typeface.

A typeface is identified by a name, where the empty string denotes the
default typeface.

Font is introduced to specify a particular font from the typeface, style,
weight and size.

Face is changed to an interface for a particular layout and shaping method.
The text/shape package is renamed to text/opentype and contains a Face
implementation based on golang.org/x/image/font/sfnt.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-10-08 21:29:04 +02:00
parent ea404bc8fc
commit bef7c39e4c
8 changed files with 233 additions and 198 deletions
+4 -16
View File
@@ -12,17 +12,13 @@ import (
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/unit"
"golang.org/x/image/math/fixed"
)
// Label is a widget for laying out and drawing text.
type Label struct {
// Face defines the style of the text.
Face Face
// Size is the text size. If zero, a default size is used.
Size unit.Value
Font Font
// Material is a macro recording the material to draw the
// text. Use a ColorOp for colored text.
@@ -93,10 +89,9 @@ func (l *lineIterator) Next() (String, f32.Point, bool) {
return String{}, f32.Point{}, false
}
func (l Label) Layout(gtx *layout.Context, family Family) {
func (l Label) Layout(gtx *layout.Context, s *Shaper) {
cs := gtx.Constraints
tsize := textSize(gtx, l.Size)
textLayout := family.Layout(l.Face, tsize, l.Text, LayoutOptions{MaxWidth: cs.Width.Max})
textLayout := s.Layout(gtx, l.Font, l.Text, LayoutOptions{MaxWidth: cs.Width.Max})
lines := textLayout.Lines
if max := l.MaxLines; max > 0 && len(lines) > max {
lines = lines[:max]
@@ -123,7 +118,7 @@ func (l Label) Layout(gtx *layout.Context, family Family) {
var stack op.StackOp
stack.Push(gtx.Ops)
op.TransformOp{}.Offset(off).Add(gtx.Ops)
family.Shape(l.Face, tsize, str).Add(gtx.Ops)
s.Shape(gtx, l.Font, str).Add(gtx.Ops)
// Set a default color in case the material is empty.
paint.ColorOp{Color: color.RGBA{A: 0xff}}.Add(gtx.Ops)
l.Material.Add(gtx.Ops)
@@ -153,10 +148,3 @@ func textPadding(lines []Line) (padTop int, padBottom int) {
}
return
}
func textSize(c unit.Converter, s unit.Value) float32 {
if s.V == 0 {
s = unit.Sp(12)
}
return float32(c.Px(s))
}