mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 10:25:37 +00:00
font: add a central font registry
Package font holds a singleton text.Shaper for general use. Subpackages call Register to add fonts to the registry. The intention is that programs can add a typeface by importing a package: import _ "gioui.org/font/gofont" Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
// SPDX-License-Identifier: Unlicense OR MIT
|
||||||
|
|
||||||
|
// Package font implements a central font registry.
|
||||||
|
package font
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"gioui.org/text"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
mu sync.Mutex
|
||||||
|
initialized bool
|
||||||
|
shaper = new(text.Shaper)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Default returns a singleton *text.Shaper that contains
|
||||||
|
// the registered fonts.
|
||||||
|
func Default() *text.Shaper {
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
initialized = true
|
||||||
|
return shaper
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register a face. Register panics if Default has been
|
||||||
|
// called.
|
||||||
|
func Register(font text.Font, face text.Face) {
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
if initialized {
|
||||||
|
panic("Register must be called before Default")
|
||||||
|
}
|
||||||
|
shaper.Register(font, face)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user