font/opentype: [API] support font collection loading

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>
This commit is contained in:
Chris Waldon
2023-04-07 13:24:51 -04:00
committed by Elias Naur
parent cda73efa6a
commit f77bf9a42c
25 changed files with 366 additions and 241 deletions
+2 -1
View File
@@ -13,6 +13,7 @@ import (
"unicode/utf8"
"gioui.org/f32"
"gioui.org/font"
"gioui.org/gesture"
"gioui.org/io/clipboard"
"gioui.org/io/event"
@@ -508,7 +509,7 @@ func (e *Editor) initBuffer() {
// Layout lays out the editor using the provided textMaterial as the paint material
// for the text glyphs+caret and the selectMaterial as the paint material for the
// selection rectangle.
func (e *Editor) Layout(gtx layout.Context, lt *text.Shaper, font text.Font, size unit.Sp, textMaterial, selectMaterial op.CallOp) layout.Dimensions {
func (e *Editor) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, size unit.Sp, textMaterial, selectMaterial op.CallOp) layout.Dimensions {
e.initBuffer()
e.text.Update(gtx, lt, font, size, e.processEvents)
+20 -19
View File
@@ -17,6 +17,7 @@ import (
nsareg "eliasnaur.com/font/noto/sans/arabic/regular"
"eliasnaur.com/font/roboto/robotoregular"
"gioui.org/f32"
"gioui.org/font"
"gioui.org/font/gofont"
"gioui.org/font/opentype"
"gioui.org/io/event"
@@ -109,7 +110,7 @@ func TestEditorReadOnly(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
e := new(Editor)
e.ReadOnly = true
e.SetText("The quick brown fox jumps over the lazy dog. We just need a few lines of text in the editor so that it can adequately test a few different modes of selection. The quick brown fox jumps over the lazy dog. We just need a few lines of text in the editor so that it can adequately test a few different modes of selection.")
@@ -188,7 +189,7 @@ func TestEditorConfigurations(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
sentence := "\n\n\n\n\n\n\n\n\n\n\n\nthe quick brown fox jumps over the lazy dog"
runes := len([]rune(sentence))
@@ -242,7 +243,7 @@ func TestEditor(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
// Regression test for bad in-cluster rune offset math.
e.SetText("æbc")
@@ -336,9 +337,9 @@ var arabic = system.Locale{
Direction: system.RTL,
}
var arabicCollection = func() []text.FontFace {
var arabicCollection = func() []font.FontFace {
parsed, _ := opentype.Parse(nsareg.TTF)
return []text.FontFace{{Font: text.Font{}, Face: parsed}}
return []font.FontFace{{Font: font.Font{}, Face: parsed}}
}()
func TestEditorRTL(t *testing.T) {
@@ -350,7 +351,7 @@ func TestEditorRTL(t *testing.T) {
}
cache := text.NewShaper(arabicCollection)
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
e.SetCaret(0, 0) // shouldn't panic
assertCaret(t, e, 0, 0, 0)
@@ -417,16 +418,16 @@ func TestEditorLigature(t *testing.T) {
if err != nil {
t.Skipf("failed parsing test font: %v", err)
}
cache := text.NewShaper([]text.FontFace{
cache := text.NewShaper([]font.FontFace{
{
Font: text.Font{
Font: font.Font{
Typeface: "Roboto",
},
Face: face,
},
})
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
/*
In this font, the following rune sequences form ligatures:
@@ -540,7 +541,7 @@ func TestEditorDimensions(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
dims := e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
if dims.Size.X == 0 {
t.Errorf("EditEvent was not reflected in Editor width")
@@ -587,7 +588,7 @@ func TestEditorCaretConsistency(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
for _, a := range []text.Alignment{text.Start, text.Middle, text.End} {
e := &Editor{}
e.Alignment = a
@@ -679,7 +680,7 @@ func TestEditorMoveWord(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
e.SetText(t)
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
return e
@@ -784,7 +785,7 @@ func TestEditorInsert(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
e.SetText(t)
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
return e
@@ -874,7 +875,7 @@ func TestEditorDeleteWord(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
e.SetText(t)
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
return e
@@ -927,7 +928,7 @@ g 2 4 6 8 g
Locale: english,
}
cache := text.NewShaper(gofont.Collection())
font := text.Font{}
font := font.Font{}
fontSize := unit.Sp(10)
var tim time.Duration
@@ -1025,7 +1026,7 @@ func TestSelectMove(t *testing.T) {
Locale: english,
}
cache := text.NewShaper(gofont.Collection())
font := text.Font{}
font := font.Font{}
fontSize := unit.Sp(10)
// Layout once to populate e.lines and get focus.
@@ -1114,7 +1115,7 @@ func TestEditor_MaxLen(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
if got, want := e.Text(), "12345678"; got != want {
@@ -1145,7 +1146,7 @@ func TestEditor_Filter(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
if got, want := e.Text(), "12345678"; got != want {
@@ -1169,7 +1170,7 @@ func TestEditor_Submit(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
if got, want := e.Text(), "ab1"; got != want {
+12 -11
View File
@@ -6,6 +6,7 @@ import (
"testing"
nsareg "eliasnaur.com/font/noto/sans/arabic/regular"
"gioui.org/font"
"gioui.org/font/opentype"
"gioui.org/text"
"golang.org/x/image/font/gofont/goregular"
@@ -19,13 +20,13 @@ func makePosTestText(fontSize, lineWidth int, alignOpposite bool) (source string
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
shaper := text.NewShaper([]text.FontFace{
shaper := text.NewShaper([]font.FontFace{
{
Font: text.Font{Typeface: "LTR"},
Font: font.Font{Typeface: "LTR"},
Face: ltrFace,
},
{
Font: text.Font{Typeface: "RTL"},
Font: font.Font{Typeface: "RTL"},
Face: rtlFace,
},
})
@@ -33,7 +34,7 @@ func makePosTestText(fontSize, lineWidth int, alignOpposite bool) (source string
// changing scripts within the RTL).
bidiSource := "The quick سماء שלום لا fox تمط שלום غير the lazy dog."
ltrParams := text.Parameters{
Font: text.Font{Typeface: "LTR"},
Font: font.Font{Typeface: "LTR"},
PxPerEm: fixed.I(fontSize),
MaxWidth: lineWidth,
MinWidth: lineWidth,
@@ -41,7 +42,7 @@ func makePosTestText(fontSize, lineWidth int, alignOpposite bool) (source string
}
rtlParams := text.Parameters{
Alignment: text.End,
Font: text.Font{Typeface: "RTL"},
Font: font.Font{Typeface: "RTL"},
PxPerEm: fixed.I(fontSize),
MaxWidth: lineWidth,
MinWidth: lineWidth,
@@ -68,12 +69,12 @@ func makeAccountingTestText(str string, fontSize, lineWidth int) (txt []text.Gly
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
shaper := text.NewShaper([]text.FontFace{{
Font: text.Font{Typeface: "LTR"},
shaper := text.NewShaper([]font.FontFace{{
Font: font.Font{Typeface: "LTR"},
Face: ltrFace,
},
{
Font: text.Font{Typeface: "RTL"},
Font: font.Font{Typeface: "RTL"},
Face: rtlFace,
},
})
@@ -94,12 +95,12 @@ func getGlyphs(fontSize, minWidth, lineWidth int, align text.Alignment, str stri
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
shaper := text.NewShaper([]text.FontFace{{
Font: text.Font{Typeface: "LTR"},
shaper := text.NewShaper([]font.FontFace{{
Font: font.Font{Typeface: "LTR"},
Face: ltrFace,
},
{
Font: text.Font{Typeface: "RTL"},
Font: font.Font{Typeface: "RTL"},
Face: rtlFace,
},
})
+2 -1
View File
@@ -6,6 +6,7 @@ import (
"image"
"gioui.org/f32"
"gioui.org/font"
"gioui.org/io/semantic"
"gioui.org/layout"
"gioui.org/op"
@@ -30,7 +31,7 @@ type Label struct {
}
// Layout the label with the given shaper, font, size, text, and material.
func (l Label) Layout(gtx layout.Context, lt *text.Shaper, font text.Font, size unit.Sp, txt string, textMaterial op.CallOp) layout.Dimensions {
func (l Label) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, size unit.Sp, txt string, textMaterial op.CallOp) layout.Dimensions {
cs := gtx.Constraints
textSize := fixed.I(gtx.Sp(size))
lt.LayoutString(text.Parameters{
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"image/color"
"math"
"gioui.org/font"
"gioui.org/internal/f32color"
"gioui.org/io/semantic"
"gioui.org/layout"
@@ -22,7 +23,7 @@ type ButtonStyle struct {
Text string
// Color is the text color.
Color color.NRGBA
Font text.Font
Font font.Font
TextSize unit.Sp
Background color.NRGBA
CornerRadius unit.Dp
+2 -1
View File
@@ -6,6 +6,7 @@ import (
"image"
"image/color"
"gioui.org/font"
"gioui.org/internal/f32color"
"gioui.org/layout"
"gioui.org/op"
@@ -19,7 +20,7 @@ import (
type checkable struct {
Label string
Color color.NRGBA
Font text.Font
Font font.Font
TextSize unit.Sp
IconColor color.NRGBA
Size unit.Dp
+2 -1
View File
@@ -5,6 +5,7 @@ package material
import (
"image/color"
"gioui.org/font"
"gioui.org/internal/f32color"
"gioui.org/layout"
"gioui.org/op"
@@ -15,7 +16,7 @@ import (
)
type EditorStyle struct {
Font text.Font
Font font.Font
TextSize unit.Sp
// Color is the text color.
Color color.NRGBA
+6 -5
View File
@@ -5,6 +5,7 @@ package material
import (
"image/color"
"gioui.org/font"
"gioui.org/internal/f32color"
"gioui.org/layout"
"gioui.org/op"
@@ -19,7 +20,7 @@ import (
// the label will be non-interactive.
type LabelStyle struct {
// Face defines the text style.
Font text.Font
Font font.Font
// Color is the text color.
Color color.NRGBA
// SelectionColor is the color of the background for selected text.
@@ -47,13 +48,13 @@ type LabelStyle struct {
func H1(th *Theme, txt string) LabelStyle {
label := Label(th, th.TextSize*96.0/16.0, txt)
label.Font.Weight = text.Light
label.Font.Weight = font.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
label.Font.Weight = font.Light
return label
}
@@ -71,7 +72,7 @@ func H5(th *Theme, txt string) LabelStyle {
func H6(th *Theme, txt string) LabelStyle {
label := Label(th, th.TextSize*20.0/16.0, txt)
label.Font.Weight = text.Medium
label.Font.Weight = font.Medium
return label
}
@@ -81,7 +82,7 @@ func Subtitle1(th *Theme, txt string) LabelStyle {
func Subtitle2(th *Theme, txt string) LabelStyle {
label := Label(th, th.TextSize*14.0/16.0, txt)
label.Font.Weight = text.Medium
label.Font.Weight = font.Medium
return label
}
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"golang.org/x/exp/shiny/materialdesign/icons"
"gioui.org/font"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
@@ -46,7 +47,7 @@ type Theme struct {
FingerSize unit.Dp
}
func NewTheme(fontCollection []text.FontFace) *Theme {
func NewTheme(fontCollection []font.FontFace) *Theme {
t := &Theme{
Shaper: text.NewShaper(fontCollection),
}
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"math"
"strings"
"gioui.org/font"
"gioui.org/gesture"
"gioui.org/io/clipboard"
"gioui.org/io/event"
@@ -176,7 +177,7 @@ func (l *Selectable) Truncated() bool {
// Layout clips to the dimensions of the selectable, updates the shaped text, configures input handling, and paints
// the text and selection rectangles. The provided textMaterial and selectionMaterial ops are used to set the
// paint material for the text and selection rectangles, respectively.
func (l *Selectable) Layout(gtx layout.Context, lt *text.Shaper, font text.Font, size unit.Sp, textMaterial, selectionMaterial op.CallOp) layout.Dimensions {
func (l *Selectable) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, size unit.Sp, textMaterial, selectionMaterial op.CallOp) layout.Dimensions {
l.initialize()
l.text.Alignment = l.Alignment
l.text.MaxLines = l.MaxLines
+5 -4
View File
@@ -5,6 +5,7 @@ import (
"image"
"testing"
"gioui.org/font"
"gioui.org/font/gofont"
"gioui.org/io/key"
"gioui.org/layout"
@@ -37,7 +38,7 @@ func TestSelectableMove(t *testing.T) {
Locale: english,
}
cache := text.NewShaper(gofont.Collection())
font := text.Font{}
fnt := font.Font{}
fontSize := unit.Sp(10)
str := `0123456789`
@@ -47,7 +48,7 @@ func TestSelectableMove(t *testing.T) {
s := new(Selectable)
s.SetText(str)
s.Layout(gtx, cache, text.Font{}, fontSize, op.CallOp{}, op.CallOp{})
s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{})
testKey := func(keyName string) {
// Select 345
@@ -62,7 +63,7 @@ func TestSelectableMove(t *testing.T) {
// Press the key
gtx.Queue = newQueue(key.Event{State: key.Press, Name: keyName})
s.SetText(str)
s.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
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)
@@ -83,7 +84,7 @@ func TestSelectableConfigurations(t *testing.T) {
}
cache := text.NewShaper(gofont.Collection())
fontSize := unit.Sp(10)
font := text.Font{}
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} {
+2 -1
View File
@@ -10,6 +10,7 @@ import (
"unicode/utf8"
"gioui.org/f32"
"gioui.org/font"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
@@ -222,7 +223,7 @@ func (e *textView) calculateViewSize(gtx layout.Context) image.Point {
// Update the text, reshaping it as necessary. If not nil, eventHandling will be invoked after reshaping the text to
// allow parent widgets to adapt to any changes in text content or positioning. If eventHandling modifies the contents
// of the textView, it is guaranteed to be reshaped (and ready for painting) before Update returns.
func (e *textView) Update(gtx layout.Context, lt *text.Shaper, font text.Font, size unit.Sp, eventHandling func(gtx layout.Context)) {
func (e *textView) Update(gtx layout.Context, lt *text.Shaper, font font.Font, size unit.Sp, eventHandling func(gtx layout.Context)) {
if e.params.Locale != gtx.Locale {
e.params.Locale = gtx.Locale
e.invalidate()
+9 -8
View File
@@ -10,6 +10,7 @@ import (
"time"
colEmoji "eliasnaur.com/font/noto/emoji/color"
"gioui.org/font"
"gioui.org/font/gofont"
"gioui.org/font/opentype"
"gioui.org/gpu/headless"
@@ -34,11 +35,11 @@ var (
}()
sizes = []int{10, 100, 1000}
locales = []system.Locale{arabic, english}
benchFonts = func() []text.FontFace {
benchFonts = func() []font.FontFace {
collection := gofont.Collection()
collection = append(collection, arabicCollection...)
collection = append(collection, text.FontFace{
Font: text.Font{
collection = append(collection, font.FontFace{
Font: font.Font{
Typeface: "Noto Color Emoji",
},
Face: emojiFace,
@@ -91,7 +92,7 @@ func BenchmarkLabelStatic(b *testing.B) {
defer win.Release()
}
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
runes := []rune(txt)[:runeCount]
runesStr := string(runes)
l := Label{}
@@ -123,7 +124,7 @@ func BenchmarkLabelDynamic(b *testing.B) {
defer win.Release()
}
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
runes := []rune(txt)[:runeCount]
l := Label{}
b.ResetTimer()
@@ -158,7 +159,7 @@ func BenchmarkEditorStatic(b *testing.B) {
defer win.Release()
}
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
runes := []rune(txt)[:runeCount]
runesStr := string(runes)
e := Editor{}
@@ -191,7 +192,7 @@ func BenchmarkEditorDynamic(b *testing.B) {
defer win.Release()
}
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
runes := []rune(txt)[:runeCount]
e := Editor{}
e.SetText(string(runes))
@@ -225,7 +226,7 @@ func FuzzEditorEditing(f *testing.F) {
}
cache := text.NewShaper(benchFonts)
fontSize := unit.Sp(10)
font := text.Font{}
font := font.Font{}
e := Editor{}
f.Fuzz(func(t *testing.T, txt string, replaceFrom, replaceTo int16) {
e.SetText(txt)