Donner 0.5.0
Embeddable browser-grade SVG2 engine
Loading...
Searching...
No Matches
donner::svg::FontManager Class Reference

Manages font loading, caching, and lookup for text rendering. More...

#include "donner/svg/resources/FontManager.h"

Classes

struct  FontFaceComponent
struct  LoadedFontComponent

Public Member Functions

 FontManager (Registry &registry)
 FontManager (const FontManager &)=delete
FontManager & operator= (const FontManager &)=delete
 FontManager (FontManager &&)=delete
FontManager & operator= (FontManager &&)=delete
void addFontFace (const css::FontFace &face)
 Register a @font-face declaration.
void setGenericFamilyMapping (std::string_view genericName, std::string_view realFamily)
 Register a mapping from a CSS generic family name (serif, sans-serif, monospace, cursive, fantasy) to a real font family name registered via addFontFace().
FontHandle findFont (std::string_view family)
 Find or load a font matching the given family name.
FontHandle findFont (std::string_view family, int weight)
 Find or load a font matching the given family name and weight.
FontHandle findFont (std::string_view family, int weight, int style, int stretch)
 Find or load a font matching the given family name, weight, style, and stretch.
FontHandle loadFontData (std::span< const uint8_t > data)
 Load a font from raw TTF/OTF/WOFF data.
std::span< const uint8_t > fontData (FontHandle handle) const
 Get the raw font data bytes for a handle.
size_t numFaces () const
 Get the number of registered @font-face rules.
std::string_view faceFamilyName (size_t index) const
 Get the family name of a registered @font-face rule by index.
FontHandle fallbackFont ()
 Get the handle for the embedded fallback font (Public Sans).

Detailed Description

Manages font loading, caching, and lookup for text rendering.

FontManager is the shared font infrastructure used by both the Skia and TinySkia backends. It handles:

  • Loading raw TTF/OTF font data.
  • Loading WOFF 1.0 fonts via the existing WoffParser, reconstructing the sfnt byte stream, then storing the reconstructed sfnt bytes.
  • Resolving @font-face source cascades.
  • Caching resolved family/style lookups to avoid repeated face scans.
  • Falling back to the embedded Public Sans font when no match is found.
  • Storing backend caches on the same font entity as the loaded bytes.

FontManager uses entt entities to store font data, with one entity per registered @font-face rule or directly-loaded font. Text backends can cache parsed backend objects directly on the same entity.

Member Function Documentation

◆ addFontFace()

void donner::svg::FontManager::addFontFace ( const css::FontFace & face)

Register a @font-face declaration.

Sources are resolved lazily on first findFont() call for the corresponding family name.

Parameters
faceThe parsed @font-face rule.

◆ faceFamilyName()

std::string_view donner::svg::FontManager::faceFamilyName ( size_t index) const

Get the family name of a registered @font-face rule by index.

Parameters
indexIndex into the registered faces (0 to numFaces()-1).
Returns
The family name, or empty string_view if index is out of range.

◆ findFont() [1/3]

FontHandle donner::svg::FontManager::findFont ( std::string_view family)

Find or load a font matching the given family name.

Resolution order:

  1. If the family is a CSS generic name with a registered mapping, resolve to the real name.
  2. Return an already-loaded entity if the best matching face has already been resolved.
  3. Walk registered @font-face rules whose font-family matches, trying each source.
  4. Fall back to the embedded Public Sans font.
Parameters
familyFont family name to look up.
Returns
A valid FontHandle, or an invalid handle if even the fallback fails.

◆ findFont() [2/3]

FontHandle donner::svg::FontManager::findFont ( std::string_view family,
int weight )

Find or load a font matching the given family name and weight.

Parameters
familyFont family name to look up.
weightCSS font-weight value (100-900, 400=normal, 700=bold).
Returns
A valid FontHandle, or falls back to findFont(family) if no weight match.

◆ findFont() [3/3]

FontHandle donner::svg::FontManager::findFont ( std::string_view family,
int weight,
int style,
int stretch )

Find or load a font matching the given family name, weight, style, and stretch.

Parameters
familyFont family name to look up.
weightCSS font-weight value (100-900, 400=normal, 700=bold).
styleCSS font-style value (0=normal, 1=italic, 2=oblique).
stretchCSS font-stretch value (1-9, 5=normal, matching FontStretch enum).
Returns
A valid FontHandle, or falls back to findFont(family, weight) if no match.

◆ fontData()

std::span< const uint8_t > donner::svg::FontManager::fontData ( FontHandle handle) const

Get the raw font data bytes for a handle.

This is useful for backends (like Skia) that need to create their own font objects from the raw data.

Parameters
handleA valid FontHandle.
Returns
Span of the raw font data, or empty span if the handle is invalid.

◆ loadFontData()

FontHandle donner::svg::FontManager::loadFontData ( std::span< const uint8_t > data)

Load a font from raw TTF/OTF/WOFF data.

The data is copied internally. The font is not associated with any family name; callers should use findFont() for name-based lookup.

Parameters
dataRaw font file bytes (TTF, OTF, or WOFF 1.0).
Returns
A valid FontHandle on success, or an invalid handle on failure.

◆ setGenericFamilyMapping()

void donner::svg::FontManager::setGenericFamilyMapping ( std::string_view genericName,
std::string_view realFamily )

Register a mapping from a CSS generic family name (serif, sans-serif, monospace, cursive, fantasy) to a real font family name registered via addFontFace().

This allows findFont("sans-serif") to resolve to the specified family.

Parameters
genericNameThe CSS generic family name (case-insensitive).
realFamilyThe real family name to resolve to.

The documentation for this class was generated from the following files:
  • donner/svg/resources/FontManager.h
  • donner/svg/resources/FontManager.cc