Utilities

Helper classes used across the library for fonts, logging, and encoding.

DocraftFontRegistry

Singleton registry for in-memory and file-based fonts.

class DocraftFontRegistry

Singleton registry for in-memory and file-based fonts.

Stores raw font data so backends can register fonts without re-reading files.

Public Functions

bool register_font(const std::string &name, const unsigned char *data, size_t size)

Registers a font from memory.

Parameters:
  • name – Font family or variant name.

  • data – Raw font data.

  • size – Size of the data in bytes.

Returns:

true if the font was registered.

bool register_font(const std::string &name, const std::string &file_path)

Registers a font by loading it from a file path.

Parameters:
  • name – Font family or variant name.

  • file_path – Path to the font file.

Returns:

true on success, false on failure.

const DocraftFontData *find_font(const std::string &name) const

Returns font data for a registered name, or nullptr if missing.

Parameters:

name – Font family or variant name.

Returns:

Pointer to font data, or nullptr if not found.

std::vector<std::string> registered_font_names() const

Returns the list of registered font names, including aliases (see register_font_alias()) &#8212; DocraftFontResolver builds its family/style index from this list, so an alias must appear here to be resolvable by family+style.

Returns:

Vector of font names.

void register_font_alias(const std::string &alias, const std::string &target_name)

Registers an alias for a font name, e.g.

mapping a craft-language family name like “OpenSans-Bold” to whatever internal name the backend actually loaded the font under (which the backend chooses, not the caller &#8212; see IDocraftFontBackend::register_ttf_font_from_file()).

Parameters:
  • alias – Name callers will request (e.g. via font_name attributes).

  • target_name – Backend-internal name to resolve alias to.

std::string resolve_font_alias(const std::string &name) const

Resolves a font name through the alias table.

Parameters:

name – Requested name.

Returns:

The aliased target name if name is a registered alias, otherwise name itself unchanged.

Public Static Functions

static DocraftFontRegistry &instance()

Returns the singleton instance.

Returns:

Reference to the registry singleton.

DocraftFontResolver

Resolves a font family + style request into the best available font name.

class DocraftFontResolver

Resolves a font family + style into an available font name.

The resolver builds a per-family index of available variants (regular/bold/italic/bold-italic) based on a list of built-in fonts and fonts registered in DocraftFontRegistry. It returns the closest available variant when an exact match is missing.

Public Functions

DocraftFontResolver() = default

Creates a font resolver with an empty index.

void rebuild_index(const std::vector<std::string> &builtin_fonts, const std::vector<std::string> &registered_fonts)

Rebuilds the internal index from the provided font name lists.

Parameters:
  • builtin_fonts – Built-in font names.

  • registered_fonts – Fonts registered at runtime.

std::string resolve(const std::string &requested, TextStyle style) const

Resolves the best matching font for a given request.

Parameters:
  • requested – Requested font family name.

  • style – Requested text style.

Returns:

Resolved font name (may be empty).

Public Static Functions

static std::vector<std::string> builtin_font_names()

Returns libharu’s 14 built-in base font names (Courier/Helvetica/Times variants, Symbol, ZapfDingbats) &#8212; these resolve by name with no registration needed, unlike a custom TTF registered via DocraftFontRegistry.

DocraftLogger

Simple console logging utility with configurable levels.

class DocraftLogger

Simple logging utility for console output.

Public Types

enum class LogLevel

Values:

enumerator kDebug
enumerator kInfo
enumerator kWarning
enumerator kError

Public Static Functions

static void debug(const std::string &message)

Logs a debug-level message.

Parameters:

message – Message to log.

static void info(const std::string &message)

Logs an info-level message.

Parameters:

message – Message to log.

static void warning(const std::string &message)

Logs a warning-level message.

Parameters:

message – Message to log.

static void error(const std::string &message)

Logs an error-level message.

Parameters:

message – Message to log.

static void set_level_enabled(LogLevel level, bool enabled = true)

Enables/disables a specific log level.

Parameters:
  • level – Log level to update.

  • enabled – Whether the level should be active.

static bool is_level_enabled(LogLevel level)

Returns current activation state of a log level.

Parameters:

level – Level to query.

Returns:

true if active.

static void reset_levels()

Resets levels to defaults: warning/error enabled, info/debug disabled.

static void enable_debug(bool enabled = true)
static void enable_info(bool enabled = true)
static void enable_warning(bool enabled = true)
static void enable_error(bool enabled = true)

DocraftParserUtilis

Static helpers for template expression detection and JSON data extraction.

class DocraftParserUtilis

Public Static Functions

static std::string extract_data_attribute(const std::string &data_request, const nlohmann::json &item)

Extracts a value from a JSON object based on a data request string.

The data request can be a simple key or a dot-separated path for nested values.

Parameters:
  • data_request – The data request string (e.g., “${data(name)” or “${data(“age”)”).

  • item – The JSON object to extract data from.

Returns:

The extracted value as a string, or an empty string if not found.

static std::string extract_data_attribute(const std::vector<unsigned char> &data_request, const nlohmann::json &item)
static bool is_data_request(const std::string &data_request)
static bool is_template_variable(const std::string &variable)
static bool is_data_request(const std::vector<unsigned char> &data)

Base64 Decoding

std::vector<unsigned char> docraft::utils::decode_base64(std::string_view input)

Decodes a base64 string into raw bytes.

Parameters:

input – Base64 string (no data URI prefix).

Throws:

docraft::exception::InvalidInputException – if the input is not valid base64.

Returns:

Decoded bytes.