forked from joejulian/gio
b7d126e24c
This commit restructures the entire text shaping stack to enable lines of shaped text to
have non-homogeneous properties like which font face they belong to and which direction
a segment of text is going.
The text package now provides a concrete type text.Shaper which can be used to convert
strings into sequences of renderable text.Glyphs. At a high level, the API is used
like this:
// Prepare some fonts.
var collection []text.FontFace
// Make a shaper with those fonts loaded.
shaper := text.NewShaper(collection)
// Shape a string.
shaper.LayoutString(text.Parameters{
PxPerEm: fixed.I(12),
}, 0, 100, system.Locale{}, "Hello")
// Iterate the glyphs from that string.
for glyph, ok := shaper.NextGlyph(); ok; glyph, ok = shaper.NextGlyph() {
// Convert the glyph data into a path. In real uses, convert batches of glyphs
// rather than single glyphs to reduce the number of individual paths and offsets
// required to display your text.
shape := shaper.Shape([]text.Glyph{glyph})
// Offset the glyph to the position it declares within its fields. This will
// automatically handle correct bidirectional text glyph positioning.
offset := op.Offset(image.Pt(glyph.X.Floor(), int(glyph.Y))).Push(gtx.Ops)
// Create a clip area from the shape of the glyph.
area := clip.Outline{Path: shape}.Push(gtx.Ops)
// Paint whatever the current color is within the glyph's shape.
paint.PaintOp{}.Add(gtx.Ops)
area.Pop()
offset.Pop()
}
This API will transparently handle both font fallback (choosing appropriate fonts
from those loaded when the primary font doesn't contain a required glyph) and
bidirectional text (mixed left-to-right and right-to-left text). Glyphs are
iterated in order of the input runes, not their visual order, but proper use
of the provided offsets will ensure that text always displays correctly.
Thanks to Elias Naur for suggesting this glyph iterator strategy. It let us cut
through a lot of accumulated complexity from trying to match our old text APIs,
meaning that this change actually is a net negative change in lines of code.
This commit consumes the upstream github.com/go-text/typesetting/shaping API
now that my prior work is merged there, removing the need for the font/opentype/internal
package entirely.
As part of my efforts, I fuzzed both the low-level text shaping stack and the
editor widget extensively. I've committed regression tests found that way into
the appropriate testdata files to ensure the fuzzer re-checks them.
Fixes: https://todo.sr.ht/~eliasnaur/gio/425
Fixes: https://todo.sr.ht/~eliasnaur/gio/211
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
100 lines
2.2 KiB
Go
100 lines
2.2 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image/color"
|
|
|
|
"gioui.org/layout"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/text"
|
|
"gioui.org/unit"
|
|
"gioui.org/widget"
|
|
)
|
|
|
|
type LabelStyle struct {
|
|
// Face defines the text style.
|
|
Font text.Font
|
|
// Color is the text color.
|
|
Color color.NRGBA
|
|
// Alignment specify the text alignment.
|
|
Alignment text.Alignment
|
|
// MaxLines limits the number of lines. Zero means no limit.
|
|
MaxLines int
|
|
Text string
|
|
TextSize unit.Sp
|
|
|
|
shaper *text.Shaper
|
|
}
|
|
|
|
func H1(th *Theme, txt string) LabelStyle {
|
|
label := Label(th, th.TextSize*96.0/16.0, txt)
|
|
label.Font.Weight = text.Light
|
|
return label
|
|
}
|
|
|
|
func H2(th *Theme, txt string) LabelStyle {
|
|
label := Label(th, th.TextSize*60.0/16.0, txt)
|
|
label.Font.Weight = text.Light
|
|
return label
|
|
}
|
|
|
|
func H3(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize*48.0/16.0, txt)
|
|
}
|
|
|
|
func H4(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize*34.0/16.0, txt)
|
|
}
|
|
|
|
func H5(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize*24.0/16.0, txt)
|
|
}
|
|
|
|
func H6(th *Theme, txt string) LabelStyle {
|
|
label := Label(th, th.TextSize*20.0/16.0, txt)
|
|
label.Font.Weight = text.Medium
|
|
return label
|
|
}
|
|
|
|
func Subtitle1(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize*16.0/16.0, txt)
|
|
}
|
|
|
|
func Subtitle2(th *Theme, txt string) LabelStyle {
|
|
label := Label(th, th.TextSize*14.0/16.0, txt)
|
|
label.Font.Weight = text.Medium
|
|
return label
|
|
}
|
|
|
|
func Body1(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize, txt)
|
|
}
|
|
|
|
func Body2(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize*14.0/16.0, txt)
|
|
}
|
|
|
|
func Caption(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize*12.0/16.0, txt)
|
|
}
|
|
|
|
func Overline(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize*10.0/16.0, txt)
|
|
}
|
|
|
|
func Label(th *Theme, size unit.Sp, txt string) LabelStyle {
|
|
return LabelStyle{
|
|
Text: txt,
|
|
Color: th.Palette.Fg,
|
|
TextSize: size,
|
|
shaper: th.Shaper,
|
|
}
|
|
}
|
|
|
|
func (l LabelStyle) Layout(gtx layout.Context) layout.Dimensions {
|
|
paint.ColorOp{Color: l.Color}.Add(gtx.Ops)
|
|
tl := widget.Label{Alignment: l.Alignment, MaxLines: l.MaxLines}
|
|
return tl.Layout(gtx, l.shaper, l.Font, l.TextSize, l.Text)
|
|
}
|