From 40822ef26be1a8160e468f13c167d1127c7335af Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 13 Oct 2019 18:12:07 +0200 Subject: [PATCH] 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 --- font/font.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 font/font.go diff --git a/font/font.go b/font/font.go new file mode 100644 index 00000000..1a5dd118 --- /dev/null +++ b/font/font.go @@ -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) +}