From e14bbee252d8ef19a0fa1a77c35b78b0ba3841bd Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Wed, 16 Mar 2022 16:01:40 -0400 Subject: [PATCH] font/gofont: [API] use new opentype impl for Collection() This commit switches gofont.Collection from returning a collection of fonts using the old text shaper to using the new harfbuzz-based shaper. The underlying type of gofont.Collection() has changed, which may break users who dug into the font data. References: https://todo.sr.ht/~eliasnaur/gio/146 Signed-off-by: Chris Waldon --- font/gofont/gofont.go | 36 ++---------------------------------- widget/editor_test.go | 18 +++++++++--------- 2 files changed, 11 insertions(+), 43 deletions(-) diff --git a/font/gofont/gofont.go b/font/gofont/gofont.go index 104a83d8..a06efeb8 100644 --- a/font/gofont/gofont.go +++ b/font/gofont/gofont.go @@ -29,10 +29,8 @@ import ( ) var ( - once sync.Once - collection []text.FontFace - onceHB sync.Once - collectionHB []text.FontFace + once sync.Once + collection []text.FontFace ) func Collection() []text.FontFace { @@ -56,36 +54,6 @@ func Collection() []text.FontFace { return collection } -func CollectionHB() []text.FontFace { - onceHB.Do(func() { - registerHB(text.Font{}, goregular.TTF) - registerHB(text.Font{Style: text.Italic}, goitalic.TTF) - registerHB(text.Font{Weight: text.Bold}, gobold.TTF) - registerHB(text.Font{Style: text.Italic, Weight: text.Bold}, gobolditalic.TTF) - registerHB(text.Font{Weight: text.Medium}, gomedium.TTF) - registerHB(text.Font{Weight: text.Medium, Style: text.Italic}, gomediumitalic.TTF) - registerHB(text.Font{Variant: "Mono"}, gomono.TTF) - registerHB(text.Font{Variant: "Mono", Weight: text.Bold}, gomonobold.TTF) - registerHB(text.Font{Variant: "Mono", Weight: text.Bold, Style: text.Italic}, gomonobolditalic.TTF) - registerHB(text.Font{Variant: "Mono", Style: text.Italic}, gomonoitalic.TTF) - registerHB(text.Font{Variant: "Smallcaps"}, gosmallcaps.TTF) - registerHB(text.Font{Variant: "Smallcaps", Style: text.Italic}, gosmallcapsitalic.TTF) - // Ensure that any outside appends will not reuse the backing store. - n := len(collectionHB) - collectionHB = collectionHB[:n:n] - }) - return collectionHB -} - -func registerHB(fnt text.Font, ttf []byte) { - face, err := opentype.Parse(ttf) - if err != nil { - panic(fmt.Errorf("failed to parse font: %v", err)) - } - fnt.Typeface = "Go" - collectionHB = append(collectionHB, text.FontFace{Font: fnt, Face: face}) -} - func register(fnt text.Font, ttf []byte) { face, err := opentype.Parse(ttf) if err != nil { diff --git a/widget/editor_test.go b/widget/editor_test.go index 9f49c8b0..4f23ff2c 100644 --- a/widget/editor_test.go +++ b/widget/editor_test.go @@ -42,7 +42,7 @@ func TestEditorConfigurations(t *testing.T) { Constraints: layout.Exact(image.Pt(100, 100)), Locale: english, } - cache := text.NewCache(gofont.CollectionHB()) + cache := text.NewCache(gofont.Collection()) fontSize := unit.Px(10) font := text.Font{} sentence := "\n\n\n\n\n\n\n\n\n\n\n\nthe quick brown fox jumps over the lazy dog" @@ -82,7 +82,7 @@ func TestEditor(t *testing.T) { Constraints: layout.Exact(image.Pt(100, 100)), Locale: english, } - cache := text.NewCache(gofont.CollectionHB()) + cache := text.NewCache(gofont.Collection()) fontSize := unit.Px(10) font := text.Font{} @@ -378,7 +378,7 @@ func TestEditorDimensions(t *testing.T) { Queue: tq, Locale: english, } - cache := text.NewCache(gofont.CollectionHB()) + cache := text.NewCache(gofont.Collection()) fontSize := unit.Px(10) font := text.Font{} dims := e.Layout(gtx, cache, font, fontSize, nil) @@ -425,7 +425,7 @@ func TestEditorCaretConsistency(t *testing.T) { Constraints: layout.Exact(image.Pt(100, 100)), Locale: english, } - cache := text.NewCache(gofont.CollectionHB()) + cache := text.NewCache(gofont.Collection()) fontSize := unit.Px(10) font := text.Font{} for _, a := range []text.Alignment{text.Start, text.Middle, text.End} { @@ -518,7 +518,7 @@ func TestEditorMoveWord(t *testing.T) { Constraints: layout.Exact(image.Pt(100, 100)), Locale: english, } - cache := text.NewCache(gofont.CollectionHB()) + cache := text.NewCache(gofont.Collection()) fontSize := unit.Px(10) font := text.Font{} e.SetText(t) @@ -623,7 +623,7 @@ func TestEditorInsert(t *testing.T) { Constraints: layout.Exact(image.Pt(100, 100)), Locale: english, } - cache := text.NewCache(gofont.CollectionHB()) + cache := text.NewCache(gofont.Collection()) fontSize := unit.Px(10) font := text.Font{} e.SetText(t) @@ -713,7 +713,7 @@ func TestEditorDeleteWord(t *testing.T) { Constraints: layout.Exact(image.Pt(100, 100)), Locale: english, } - cache := text.NewCache(gofont.CollectionHB()) + cache := text.NewCache(gofont.Collection()) fontSize := unit.Px(10) font := text.Font{} e.SetText(t) @@ -767,7 +767,7 @@ g 2 4 6 8 g Ops: new(op.Ops), Locale: english, } - cache := text.NewCache(gofont.CollectionHB()) + cache := text.NewCache(gofont.Collection()) font := text.Font{} fontSize := unit.Px(10) @@ -861,7 +861,7 @@ func TestSelectMove(t *testing.T) { Ops: new(op.Ops), Locale: english, } - cache := text.NewCache(gofont.CollectionHB()) + cache := text.NewCache(gofont.Collection()) font := text.Font{} fontSize := unit.Px(10)