mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
43c47f0883
This commit updates the text package to be able to load system fonts. As a consequence,
application authors may choose to provide no fonts manually, and it's
also possible that the system provides none (WASM, for instance, currently provides no
system fonts). As such, the text stack needed some minor tweaks to handle this case by
displaying blank spaces where text should be rather than crashing when no faces are
available.
Internally, we are dropping the old method of choosing faces and instead relying solely
on the new font matching logic in go-text. I chose to do this because maintaining two
different sets of logic with a hierarchical relationship proved to be really complex,
and also the go-text logic seems to produce higher-quality choices.
The breaking API change from this commit is the new way of constructing a text shaper
using text.ShaperOptions. Providing no options will result in a shaper that uses solely
system fonts. The various options can be used to disable system font loading and to
provide an already-parsed collection of fonts as per Gio's old API.
The material.NewTheme function now accepts no arguments instead of a font collection.
Users wanting to provide a collection can simply provide a new shaper configured how
they would like:
theme := material.NewTheme()
theme.Shaper = text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Regular()))
This commit touches many packages to fix up their construction of text shapers, mostly in
test code. The changes to the tests in package widget deserve special note:
Changing our font resolution logic caused the tofu characters within the
test strings to use a different font's tofu. This isn't a problem, but shifted
the layout of the shaped text a little bit. I've updated the numbers to expect
the new glyph positions.
Fixes: https://todo.sr.ht/~eliasnaur/gio/309
Fixes: https://todo.sr.ht/~eliasnaur/gio/184
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package material_test
|
|
|
|
import (
|
|
"image"
|
|
"testing"
|
|
"time"
|
|
|
|
"gioui.org/io/system"
|
|
"gioui.org/layout"
|
|
"gioui.org/op"
|
|
"gioui.org/unit"
|
|
"gioui.org/widget"
|
|
"gioui.org/widget/material"
|
|
)
|
|
|
|
func TestListAnchorStrategies(t *testing.T) {
|
|
var ops op.Ops
|
|
gtx := layout.NewContext(&ops, system.FrameEvent{
|
|
Metric: unit.Metric{
|
|
PxPerDp: 1,
|
|
PxPerSp: 1,
|
|
},
|
|
Now: time.Now(),
|
|
Size: image.Point{
|
|
X: 500,
|
|
Y: 500,
|
|
},
|
|
})
|
|
gtx.Constraints.Min = image.Point{}
|
|
|
|
var spaceConstraints layout.Constraints
|
|
space := func(gtx layout.Context, index int) layout.Dimensions {
|
|
spaceConstraints = gtx.Constraints
|
|
if spaceConstraints.Min.X < 0 || spaceConstraints.Min.Y < 0 ||
|
|
spaceConstraints.Max.X < 0 || spaceConstraints.Max.Y < 0 {
|
|
t.Errorf("invalid constraints at index %d: %#+v", index, spaceConstraints)
|
|
}
|
|
return layout.Dimensions{Size: image.Point{
|
|
X: gtx.Constraints.Max.X,
|
|
Y: gtx.Dp(20),
|
|
}}
|
|
}
|
|
|
|
var list widget.List
|
|
list.Axis = layout.Vertical
|
|
elements := 100
|
|
th := material.NewTheme()
|
|
materialList := material.List(th, &list)
|
|
indicatorWidth := gtx.Dp(materialList.Width())
|
|
|
|
materialList.AnchorStrategy = material.Occupy
|
|
occupyDims := materialList.Layout(gtx, elements, space)
|
|
occupyConstraints := spaceConstraints
|
|
|
|
materialList.AnchorStrategy = material.Overlay
|
|
overlayDims := materialList.Layout(gtx, elements, space)
|
|
overlayConstraints := spaceConstraints
|
|
|
|
// Both anchor strategies should use all space available if their elements do.
|
|
if occupyDims != overlayDims {
|
|
t.Errorf("expected occupy dims (%v) to be equal to overlay dims (%v)", occupyDims, overlayDims)
|
|
}
|
|
// The overlay strategy should not reserve any space for the scroll indicator,
|
|
// so the constraints that it presents to its elements should be larger than
|
|
// those presented by the occupy strategy.
|
|
if overlayConstraints.Max.X != occupyConstraints.Max.X+indicatorWidth {
|
|
t.Errorf("overlay max width (%d) != occupy max width (%d) + indicator width (%d)",
|
|
overlayConstraints.Max.X, occupyConstraints.Max.X, indicatorWidth)
|
|
}
|
|
}
|