text,widget/material: make font collections explicit

Before this change, package font implemented a global font registry,
with the usual problems of package global state.

This change deletes the global registry and introduces the text.Collection
type for representing a list of fonts and their faces. Collection exports
Lookup that finds the closest match and its face.

The existing FontRegistry is renamed to Cache to reflect its new limited
functionality: a cache of shapes and measurements on top of a Collection.

Then, material.NewTheme is changed to take a Collection and initialize
a Cache.

Updates gio#19 because multiple windows require a separate (writable) Cache per
window, while (read-only) Collections may be shared.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-06-07 16:20:44 +02:00
parent c2c31a4d00
commit b07d34354e
4 changed files with 110 additions and 113 deletions
-36
View File
@@ -1,36 +0,0 @@
// 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.FontRegistry)
)
// Default returns a singleton *text.Shaper that contains
// the registered fonts.
func Default() *text.FontRegistry {
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)
}