From 938179d29360dc155cc882fd07a05198fef41bac Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Wed, 16 Mar 2022 16:01:35 -0400 Subject: [PATCH] font/gofont: add font collection using the new shaper This commit adds a font collection that uses the new text shaper so that constructing material.Themes atop it is equally simple to using the old shaper. You can use material.NewTheme(gofont.CollectionHB()) with this commit applied. References: https://todo.sr.ht/~eliasnaur/gio/146 Signed-off-by: Chris Waldon --- font/gofont/gofont.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/font/gofont/gofont.go b/font/gofont/gofont.go index a06efeb8..7c7555d0 100644 --- a/font/gofont/gofont.go +++ b/font/gofont/gofont.go @@ -29,8 +29,10 @@ import ( ) var ( - once sync.Once - collection []text.FontFace + once sync.Once + collection []text.FontFace + onceHB sync.Once + collectionHB []text.FontFace ) func Collection() []text.FontFace { @@ -54,6 +56,36 @@ 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.ParseHarfbuzz(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 {