TableTower/ui/font_catalog.gd

147 lines
8.2 KiB
GDScript3
Raw Normal View History

class_name FontCatalog
extends RefCounted
## The font families bundled in assets/fonts (all SIL Open Font License, except
## Special Elite: Apache 2.0; licence files sit beside the fonts). A theme names
## one family for body text (font_body) and one for titles (font_display).
##
## Family keys: name, group (shown in the picker), roles ("body" and/or
## "display"), regular, bold, italic (file names; bold/italic optional),
## variable (the files have a wght axis), fake_bold (false: never embolden,
## for decorative faces).
##
## Cards sit on the zoomable board. Static faces use MSDF there; variable faces
## have overlapping outlines that MSDF draws badly, so they are oversampled.
const DIR := "res://assets/fonts/"
const DEFAULT_BODY := "atkinson"
const DEFAULT_DISPLAY := "rokkitt"
const GROUPS := ["Standard", "Easy to read", "Themed"]
const FAMILIES := {
"atkinson": {"name": "Atkinson Hyperlegible", "group": "Easy to read", "roles": ["body", "display"],
"regular": "AtkinsonHyperlegible-Regular.ttf", "bold": "AtkinsonHyperlegible-Bold.ttf",
"italic": "AtkinsonHyperlegible-Italic.ttf"},
"lexend": {"name": "Lexend", "group": "Easy to read", "roles": ["body", "display"],
"regular": "Lexend-Variable.ttf", "variable": true},
"andika": {"name": "Andika", "group": "Easy to read", "roles": ["body", "display"],
"regular": "Andika-Regular.ttf", "bold": "Andika-Bold.ttf", "italic": "Andika-Italic.ttf"},
"opendyslexic": {"name": "OpenDyslexic", "group": "Easy to read", "roles": ["body", "display"],
"regular": "OpenDyslexic-Regular.otf", "bold": "OpenDyslexic-Bold.otf",
"italic": "OpenDyslexic-Italic.otf"},
"rokkitt": {"name": "Rokkitt", "group": "Standard", "roles": ["body", "display"],
"regular": "Rokkitt-Variable.ttf", "variable": true},
"inter": {"name": "Inter", "group": "Standard", "roles": ["body", "display"],
"regular": "Inter-Variable.ttf", "italic": "Inter-Italic-Variable.ttf", "variable": true},
"sourcesans": {"name": "Source Sans 3", "group": "Standard", "roles": ["body", "display"],
"regular": "SourceSans3-Variable.ttf", "italic": "SourceSans3-Italic-Variable.ttf", "variable": true},
"lora": {"name": "Lora", "group": "Standard", "roles": ["body", "display"],
"regular": "Lora-Variable.ttf", "italic": "Lora-Italic-Variable.ttf", "variable": true},
"cinzel": {"name": "Cinzel (classical)", "group": "Themed", "roles": ["display"],
"regular": "Cinzel-Variable.ttf", "variable": true},
"imfell": {"name": "IM Fell English (old book)", "group": "Themed", "roles": ["body", "display"],
"regular": "IMFellEnglish-Regular.ttf", "italic": "IMFellEnglish-Italic.ttf", "fake_bold": false},
"uncial": {"name": "Uncial Antiqua (Celtic)", "group": "Themed", "roles": ["display"],
"regular": "UncialAntiqua-Regular.ttf", "fake_bold": false},
"orbitron": {"name": "Orbitron (sci-fi)", "group": "Themed", "roles": ["display"],
"regular": "Orbitron-Variable.ttf", "variable": true},
"specialelite": {"name": "Special Elite (typewriter)", "group": "Themed", "roles": ["display"],
"regular": "SpecialElite-Regular.ttf", "fake_bold": false},
# Added for the 2026-11 theme sets (all OFL).
"nunito": {"name": "Nunito", "group": "Standard", "roles": ["body", "display"],
"regular": "Nunito-Variable.ttf", "italic": "Nunito-Italic-Variable.ttf", "variable": true},
"quicksand": {"name": "Quicksand", "group": "Standard", "roles": ["body", "display"],
"regular": "Quicksand-Variable.ttf", "variable": true},
"asap": {"name": "Asap", "group": "Standard", "roles": ["body", "display"],
"regular": "Asap-Variable.ttf", "italic": "Asap-Italic-Variable.ttf", "variable": true},
"fraunces": {"name": "Fraunces", "group": "Standard", "roles": ["body", "display"],
"regular": "Fraunces-Variable.ttf", "variable": true},
"zillaslab": {"name": "Zilla Slab", "group": "Standard", "roles": ["body", "display"],
"regular": "ZillaSlab-Regular.ttf", "bold": "ZillaSlab-Bold.ttf", "italic": "ZillaSlab-Italic.ttf"},
"alegreya": {"name": "Alegreya", "group": "Standard", "roles": ["body", "display"],
"regular": "Alegreya-Variable.ttf", "italic": "Alegreya-Italic-Variable.ttf", "variable": true},
"righteous": {"name": "Righteous (retro)", "group": "Themed", "roles": ["display"],
"regular": "Righteous-Regular.ttf", "fake_bold": false},
"audiowide": {"name": "Audiowide (80s)", "group": "Themed", "roles": ["display"],
"regular": "Audiowide-Regular.ttf", "fake_bold": false},
"unifraktur": {"name": "UnifrakturMaguntia (blackletter)", "group": "Themed", "roles": ["display"],
"regular": "UnifrakturMaguntia-Book.ttf", "fake_bold": false},
"vt323": {"name": "VT323 (terminal)", "group": "Themed", "roles": ["body", "display"],
"regular": "VT323-Regular.ttf", "fake_bold": false},
"arimo": {"name": "Arimo", "group": "Standard", "roles": ["body", "display"],
"regular": "Arimo-Variable.ttf", "italic": "Arimo-Italic-Variable.ttf", "variable": true},
"fredoka": {"name": "Fredoka (rounded)", "group": "Standard", "roles": ["body", "display"],
"regular": "Fredoka-Variable.ttf", "variable": true},
"comicneue": {"name": "Comic Neue", "group": "Easy to read", "roles": ["body", "display"],
"regular": "ComicNeue-Regular.ttf", "bold": "ComicNeue-Bold.ttf", "italic": "ComicNeue-Italic.ttf"},
"courierprime": {"name": "Courier Prime (typewriter)", "group": "Themed", "roles": ["body", "display"],
"regular": "CourierPrime-Regular.ttf", "bold": "CourierPrime-Bold.ttf", "italic": "CourierPrime-Italic.ttf"},
"chakrapetch": {"name": "Chakra Petch (tech)", "group": "Themed", "roles": ["body", "display"],
"regular": "ChakraPetch-Regular.ttf", "bold": "ChakraPetch-Bold.ttf", "italic": "ChakraPetch-Italic.ttf"},
"sharetechmono": {"name": "Share Tech Mono (readout)", "group": "Themed", "roles": ["body", "display"],
"regular": "ShareTechMono-Regular.ttf"},
"imfellsc": {"name": "IM Fell English SC (old book capitals)", "group": "Themed", "roles": ["display"],
"regular": "IMFellEnglishSC-Regular.ttf", "fake_bold": false},
"bangers": {"name": "Bangers (comic)", "group": "Themed", "roles": ["display"],
"regular": "Bangers-Regular.ttf", "fake_bold": false},
"silkscreen": {"name": "Silkscreen (pixel)", "group": "Themed", "roles": ["display"],
"regular": "Silkscreen-Regular.ttf", "bold": "Silkscreen-Bold.ttf"},
}
static var _files: Dictionary = {}
## [[id, label], ...] for a role, grouped Standard / Easy to read / Themed.
static func options(role: String) -> Array:
var out := []
for g: String in GROUPS:
for id: String in FAMILIES:
var f: Dictionary = FAMILIES[id]
if f.group == g and role in f.roles:
out.append([id, "%s — %s" % [f.name, g.to_lower()]])
return out
## A known family id for a role (unknown ids fall back to the default).
static func resolve(id: String, role: String) -> String:
if FAMILIES.has(id) and role in (FAMILIES[id].roles as Array):
return id
return DEFAULT_BODY if role == "body" else DEFAULT_DISPLAY
## One face of a family. Missing bold or italic files are synthesised.
static func face(id: String, weight: int, italic: bool, zoomable: bool) -> Font:
var fam: Dictionary = FAMILIES.get(id, FAMILIES[DEFAULT_BODY])
var v := FontVariation.new()
var need_bold := weight >= 600
if fam.get("variable", false):
var file: String = fam.italic if italic and fam.has("italic") else fam.regular
v.base_font = _file(file, false, 2.0 if zoomable else 0.0)
v.variation_opentype = {TextServerManager.get_primary_interface().name_to_tag("wght"): weight}
need_bold = false
else:
var file: String = fam.regular
if need_bold and fam.has("bold"):
file = fam.bold
need_bold = false
elif italic and fam.has("italic"):
file = fam.italic
v.base_font = _file(file, zoomable, 0.0)
if need_bold and fam.get("fake_bold", true):
v.variation_embolden = 0.6
if italic and not fam.has("italic"):
v.variation_transform = Transform2D(Vector2(1, 0), Vector2(0.2, 1), Vector2.ZERO)
return v
static func _file(file: String, msdf: bool, oversampling: float) -> FontFile:
var key := "%s|%s|%s" % [file, msdf, oversampling]
if not _files.has(key):
var f: FontFile = load(DIR + file)
if msdf or oversampling > 0.0:
f = f.duplicate() as FontFile
f.multichannel_signed_distance_field = msdf
if oversampling > 0.0:
f.oversampling = oversampling
_files[key] = f
return _files[key]