Files
gio-patched/text/text.go
T
Elias Naur 4c220f4554 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>
2020-02-03 23:32:55 +01:00

94 lines
1.8 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package text
import (
"io"
"gioui.org/op"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
// A Line contains the measurements of a line of text.
type Line struct {
Layout []Glyph
// Len is the length in UTF8 bytes of the line.
Len int
// Width is the width of the line.
Width fixed.Int26_6
// Ascent is the height above the baseline.
Ascent fixed.Int26_6
// Descent is the height below the baseline, including
// the line gap.
Descent fixed.Int26_6
// Bounds is the visible bounds of the line.
Bounds fixed.Rectangle26_6
}
type Glyph struct {
Rune rune
Advance fixed.Int26_6
}
// Style is the font style.
type Style int
// Weight is a font weight, in CSS units.
type Weight int
// Font specify a particular typeface variant, style and weight.
type Font struct {
Typeface Typeface
Variant Variant
Style Style
// Weight is the text weight. If zero, Normal is used instead.
Weight Weight
}
// Face implements text layout and shaping for a particular font.
type Face interface {
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
}
// Typeface identifies a particular typeface design. The empty
// string denotes the default typeface.
type Typeface string
// Variant denotes a typeface variant such as "Mono" or "Smallcaps".
type Variant string
type Alignment uint8
const (
Start Alignment = iota
End
Middle
)
const (
Regular Style = iota
Italic
)
const (
Normal Weight = 400
Medium Weight = 500
Bold Weight = 600
)
func (a Alignment) String() string {
switch a {
case Start:
return "Start"
case End:
return "End"
case Middle:
return "Middle"
default:
panic("unreachable")
}
}