Skip to content

font

FreeBodyEngine.core.files.loaders.font #

DEFAULT_FONT_ATLAS_SIZE = 48 module-attribute #

DEFAULT_FONT_RANGE_PX = 4.0 module-attribute #

DEFAULT_FONT_WEIGHT = 400 module-attribute #

FONT_FILE = 'FONT_FILE' module-attribute #

FONT_MANIFEST_KEY = '_ENGINE_font_manifest.json' module-attribute #

FONT_WEIGHT_ALIASES = {'normal': 'regular', 'book': 'regular', 'heavy': 'black', 'demibold': 'semibold'} module-attribute #

FONT_WEIGHT_NAMES = {100: 'Thin', 200: 'ExtraLight', 300: 'Light', 400: 'Regular', 500: 'Medium', 600: 'SemiBold', 700: 'Bold', 800: 'ExtraBold', 900: 'Black'} module-attribute #

load_font(file) #

Loads a pre-baked .fbfont sidecar (as written by font/atlasgen.py / the freebody create font CLI command) plus its atlas image.

normalize_font_weight(weight) #

Turns a font_weight style value (a name like "bold", or a CSS- style number 100-900) into (numeric_weight, CanonicalName). Unknown names warn and fall back to Regular; numbers snap to the nearest of the 9 standard weights.

resolve_font(font_path, size=DEFAULT_FONT_ATLAS_SIZE, range_px=DEFAULT_FONT_RANGE_PX, weight=DEFAULT_FONT_WEIGHT) #

Turns a plain path to a raw font file (a font style value like "test.ttf") into a ready-to-render Font.

weight (a name like "bold"/"semibold", or a CSS-style number 100-900 - see FONT_WEIGHT_NAMES) is resolved to a sibling file next to font_path following the "{family}-{Weight}.ttf" naming convention (see _weight_variant_path) before anything else below runs - the manifest lookup and cache both key off the actual resolved path, so a bold variant is fully a separate font/atlas from its regular weight, not a runtime style applied to one shared atlas (there's no italic/ bold synthesis - this only ever selects a real, separately-authored font file).

That lookup is deliberately manifest-only, never a live filesystem probe: DevFileSystem.get_file() on a path that turns out not to exist - which a guessed weight-variant filename very often will - creates an empty stub file there when ASSET_WRITES_PERMITTED is set (true in dev mode), then FreeType fails on that empty "font" with an opaque FT_Exception instead of a clean "not found". A guessed weight variant that isn't in the manifest just isn't resolvable yet; if no matching sibling file exists at all, warns once and falls back to font_path at its base weight.

Looks the (possibly weight-resolved) path up in the manifest build_fonts() writes at build time - the normal path for any font that's been through a freebody build/dev build since it was added to the project - and loads the pre-built .fbfont it points at. If the base font_path (not a weight variant) isn't in the manifest either (the project hasn't been (re)built yet, or this is an engine:// font, which build_fonts() doesn't cover), falls back to generating its atlas on the spot so nothing breaks; that result is only cached in memory for this run, not written back to the manifest - run the build to persist it.