TableTower/ui/font_catalog.gd
RobinFiredrake f731c6ffbf Theme fonts: 13 bundled families and text/title pickers in the theme editor
Standard (Inter, Source Sans 3, Lora, Rokkitt), easy to read (Atkinson
Hyperlegible, Lexend, Andika, OpenDyslexic) and themed title faces (Cinzel,
IM Fell English, Uncial Antiqua, Orbitron, Special Elite). All OFL except
Special Elite (Apache 2.0); licence files sit beside the fonts. FontCatalog
builds faces, synthesising missing bold/italic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 08:16:42 -06:00

107 lines
5 KiB
GDScript

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"},
"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},
}
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]