Files
gio/font/gofont/gofont.go
T
Chris Waldon 01276238df font/opentype: [API] replace old font type with harfbuzz
This commit replaces the previous opentype.Font with
an implementation that uses the new text shaper. In
order to keep the implementation simple, support for
opentype font collections was dropped. It should be
possible to re-add this support after some changes
to the text shaper's line wrapping algorithm.

To expand on the above, doing proper font fallback with
harfbuzz will require splitting the input text on font
glyph support boundaries, changing the input from a
simple shaping.Input to []shaping.Input with each input
matched against the font that supports its language.
The line wrapping then needs to be able to properly
consume that slice. Since the line wrapping algorithm is
really complex, I'm hoping to defer that modification
until this simple version is accepted.

References: https://todo.sr.ht/~eliasnaur/gio/146
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-03-18 08:04:14 +01:00

97 lines
3.5 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
// Package gofont exports the Go fonts as a text.Collection.
//
// See https://blog.golang.org/go-fonts for a description of the
// fonts, and the golang.org/x/image/font/gofont packages for the
// font data.
package gofont
import (
"fmt"
"sync"
"golang.org/x/image/font/gofont/gobold"
"golang.org/x/image/font/gofont/gobolditalic"
"golang.org/x/image/font/gofont/goitalic"
"golang.org/x/image/font/gofont/gomedium"
"golang.org/x/image/font/gofont/gomediumitalic"
"golang.org/x/image/font/gofont/gomono"
"golang.org/x/image/font/gofont/gomonobold"
"golang.org/x/image/font/gofont/gomonobolditalic"
"golang.org/x/image/font/gofont/gomonoitalic"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/gofont/gosmallcaps"
"golang.org/x/image/font/gofont/gosmallcapsitalic"
"gioui.org/font/opentype"
"gioui.org/text"
)
var (
once sync.Once
collection []text.FontFace
onceHB sync.Once
collectionHB []text.FontFace
)
func Collection() []text.FontFace {
once.Do(func() {
register(text.Font{}, goregular.TTF)
register(text.Font{Style: text.Italic}, goitalic.TTF)
register(text.Font{Weight: text.Bold}, gobold.TTF)
register(text.Font{Style: text.Italic, Weight: text.Bold}, gobolditalic.TTF)
register(text.Font{Weight: text.Medium}, gomedium.TTF)
register(text.Font{Weight: text.Medium, Style: text.Italic}, gomediumitalic.TTF)
register(text.Font{Variant: "Mono"}, gomono.TTF)
register(text.Font{Variant: "Mono", Weight: text.Bold}, gomonobold.TTF)
register(text.Font{Variant: "Mono", Weight: text.Bold, Style: text.Italic}, gomonobolditalic.TTF)
register(text.Font{Variant: "Mono", Style: text.Italic}, gomonoitalic.TTF)
register(text.Font{Variant: "Smallcaps"}, gosmallcaps.TTF)
register(text.Font{Variant: "Smallcaps", Style: text.Italic}, gosmallcapsitalic.TTF)
// Ensure that any outside appends will not reuse the backing store.
n := len(collection)
collection = collection[:n:n]
})
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 {
panic(fmt.Errorf("failed to parse font: %v", err))
}
fnt.Typeface = "Go"
collection = append(collection, text.FontFace{Font: fnt, Face: face})
}