forked from joejulian/gio
f77bf9a42c
This commit adds back support for loading font collections, which we
lost when switching to the harfbuzz-based shaper last January. In
addition, this commit takes advantage of our new font loading library's
metadata facilities to automatically construct text.FontFaces for all
fonts within a collection. This is significantly more ergonomic for
users, and can be used to load single fonts with automatic metadata
detection as well.
I've exposed a opentype.Face.Font() method that can be used to get the
font metadata for a given face as well, though you have to type assert to
see it:
var myFace text.Face
if asOpentype, ok := myFace.(opentype.Face); ok {
myFont := asOpentype.Font()
}
The one problem with this approach is that the font variant field always
be automatically populated. Mono font detection is supported, but
other variants like SmallCaps are more complicated and may need to be
expressed differently in the future (smallcaps is a feature that any font
file can have, not necessarily a separate font file). See this [0] upstream
issue for details.
Additionally, in order to avoid import cycles, I've moved the declarations
of font attributes to package font. You can fix your code automatically to
refer to the new definitions by running the following:
gofmt -w -r 'text.FontFace -> font.FontFace' .
gofmt -w -r 'text.Variant -> font.Variant' .
gofmt -w -r 'text.Style -> font.Style' .
gofmt -w -r 'text.Typeface -> font.Typeface' .
gofmt -w -r 'text.Font -> font.Font' .
gofmt -w -r 'text.Regular -> font.Regular' .
gofmt -w -r 'text.Italic -> font.Italic' .
gofmt -w -r 'text.Thin -> font.Thin' .
gofmt -w -r 'text.ExtraLight -> font.ExtraLight' .
gofmt -w -r 'text.Light -> font.Light' .
gofmt -w -r 'text.Normal -> font.Normal' .
gofmt -w -r 'text.Medium -> font.Medium' .
gofmt -w -r 'text.SemiBold -> font.SemiBold' .
gofmt -w -r 'text.Bold -> font.Bold' .
gofmt -w -r 'text.ExtraBold -> font.ExtraBold' .
gofmt -w -r 'text.Black -> font.Black' .
gofmt -w -r 'text.Hairline -> font.Thin' .
gofmt -w -r 'text.UltraLight -> font.ExtraLight' .
gofmt -w -r 'text.DemiBold -> font.SemiBold' .
gofmt -w -r 'text.UltraBold -> font.ExtraBold' .
gofmt -w -r 'text.Heavy -> font.Black' .
gofmt -w -r 'text.ExtraBlack -> font.Black+50' .
gofmt -w -r 'text.UltraBlack -> font.ExtraBlack' .
Make sure each affected file imports gioui.org/font.
[0] https://github.com/go-text/typesetting/issues/57
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
package widget
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"testing"
|
|
|
|
"gioui.org/font"
|
|
"gioui.org/font/gofont"
|
|
"gioui.org/io/key"
|
|
"gioui.org/layout"
|
|
"gioui.org/op"
|
|
"gioui.org/text"
|
|
"gioui.org/unit"
|
|
)
|
|
|
|
func TestSelectableZeroValue(t *testing.T) {
|
|
var s Selectable
|
|
if s.Text() != "" {
|
|
t.Errorf("expected zero value to have no text, got %q", s.Text())
|
|
}
|
|
if start, end := s.Selection(); start != 0 || end != 0 {
|
|
t.Errorf("expected start=0, end=0, got start=%d, end=%d", start, end)
|
|
}
|
|
if selected := s.SelectedText(); selected != "" {
|
|
t.Errorf("expected selected text to be \"\", got %q", selected)
|
|
}
|
|
s.SetCaret(5, 5)
|
|
if start, end := s.Selection(); start != 0 || end != 0 {
|
|
t.Errorf("expected start=0, end=0, got start=%d, end=%d", start, end)
|
|
}
|
|
}
|
|
|
|
// Verify that an existing selection is dismissed when you press arrow keys.
|
|
func TestSelectableMove(t *testing.T) {
|
|
gtx := layout.Context{
|
|
Ops: new(op.Ops),
|
|
Locale: english,
|
|
}
|
|
cache := text.NewShaper(gofont.Collection())
|
|
fnt := font.Font{}
|
|
fontSize := unit.Sp(10)
|
|
|
|
str := `0123456789`
|
|
|
|
// Layout once to populate e.lines and get focus.
|
|
gtx.Queue = newQueue(key.FocusEvent{Focus: true})
|
|
s := new(Selectable)
|
|
|
|
s.SetText(str)
|
|
s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{})
|
|
|
|
testKey := func(keyName string) {
|
|
// Select 345
|
|
s.SetCaret(3, 6)
|
|
if start, end := s.Selection(); start != 3 || end != 6 {
|
|
t.Errorf("expected start=%d, end=%d, got start=%d, end=%d", 3, 6, start, end)
|
|
}
|
|
if expected, got := "345", s.SelectedText(); expected != got {
|
|
t.Errorf("KeyName %s, expected %q, got %q", keyName, expected, got)
|
|
}
|
|
|
|
// Press the key
|
|
gtx.Queue = newQueue(key.Event{State: key.Press, Name: keyName})
|
|
s.SetText(str)
|
|
s.Layout(gtx, cache, fnt, fontSize, op.CallOp{}, op.CallOp{})
|
|
|
|
if expected, got := "", s.SelectedText(); expected != got {
|
|
t.Errorf("KeyName %s, expected %q, got %q", keyName, expected, got)
|
|
}
|
|
}
|
|
|
|
testKey(key.NameLeftArrow)
|
|
testKey(key.NameRightArrow)
|
|
testKey(key.NameUpArrow)
|
|
testKey(key.NameDownArrow)
|
|
}
|
|
|
|
func TestSelectableConfigurations(t *testing.T) {
|
|
gtx := layout.Context{
|
|
Ops: new(op.Ops),
|
|
Constraints: layout.Exact(image.Pt(300, 300)),
|
|
Locale: english,
|
|
}
|
|
cache := text.NewShaper(gofont.Collection())
|
|
fontSize := unit.Sp(10)
|
|
font := font.Font{}
|
|
sentence := "\n\n\n\n\n\n\n\n\n\n\n\nthe quick brown fox jumps over the lazy dog"
|
|
|
|
for _, alignment := range []text.Alignment{text.Start, text.Middle, text.End} {
|
|
for _, zeroMin := range []bool{true, false} {
|
|
t.Run(fmt.Sprintf("Alignment: %v ZeroMinConstraint: %v", alignment, zeroMin), func(t *testing.T) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}()
|
|
if zeroMin {
|
|
gtx.Constraints.Min = image.Point{}
|
|
} else {
|
|
gtx.Constraints.Min = gtx.Constraints.Max
|
|
}
|
|
s := new(Selectable)
|
|
s.Alignment = alignment
|
|
s.SetText(sentence)
|
|
interactiveDims := s.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
|
|
staticDims := Label{Alignment: alignment}.Layout(gtx, cache, font, fontSize, sentence, op.CallOp{})
|
|
|
|
if interactiveDims != staticDims {
|
|
t.Errorf("expected consistent dimensions, static returned %#+v, interactive returned %#+v", staticDims, interactiveDims)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|