ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
ZenitUI::TextEngine Class Reference

Per-context font store, HarfBuzz shaper, FreeType glyph atlas, and layout engine. More...

#include <TextEngine.hpp>

Public Types

using SyntheticDrawCallback = std::function<void(std::string_view)>
 Optional test hook invoked per visible line when drawing in synthetic fallback mode.

Public Member Functions

 TextEngine (const TextEngine &)=delete
TextEngine & operator= (const TextEngine &)=delete
 TextEngine (TextEngine &&) noexcept
TextEngine & operator= (TextEngine &&) noexcept
FontHandle loadFont (std::string_view name, std::string_view path)
 Load a TTF/OTF font file from disk under logical name.
FontHandle loadFontFromMemory (std::string_view name, const void *data, size_t size)
 Load a TTF/OTF font from an in-memory buffer under logical name (copies bytes).
void unloadFont (std::string_view name)
 Unload a font by logical name and invalidate layout caches.
FontHandle getFont (std::string_view name) const
 Look up a loaded font by logical name; returns {0} if not found.
FontHandle resolveFont (const FontStack &stack, FontHandle fallback={}) const
 Resolve the first loaded font matching stack; falls back to the default font if set.
void setDefaultFont (FontHandle h)
 Set the fallback font used when a node specifies no font or an unknown font name.
FontHandle defaultFont () const
 Return the current default font handle ({0} if none).
FontMetrics fontMetrics (FontHandle font, float fontSize) const
 Query scaled vertical metrics for font at logical fontSize.
void setDpiScale (float scale)
 Update the active logical-to-physical DPI scale used when rasterizing glyphs.
float dpiScale () const
void setTargetDpiScale (float scale)
void setFallbackCharWidth (float width)
 Set the fixed character advance used when no TTF/OTF font is loaded (headless fallback).
float fallbackCharWidth () const
void setLayoutCacheCapacity (size_t maxEntries)
 Configure the maximum number of entries kept in the layout LRU cache.
void clearAtlas (IRenderer &renderer)
 Destroy all GPU atlas pages on renderer and clear the glyph cache.
void clearLayoutCache ()
 Clear the cached TextLayout entries without touching GPU atlas textures.
TextAtlasStats stats () const
 Return current atlas and layout-cache diagnostics.
void setSyntheticDrawCallback (SyntheticDrawCallback cb)
TextLayout layout (std::string_view text, const TextMeasureSpec &spec, const TextLayoutOptions &options={}) const
 Shape and lay out text according to spec and options (uncached).
std::shared_ptr< const TextLayout > layoutCached (std::string_view text, const TextMeasureSpec &spec, const TextLayoutOptions &options={})
 Cached variant of layout() backed by the per-engine LRU cache.
Vec2 measureText (std::string_view text, const TextMeasureSpec &spec) const
 Measure a single line (no wrapping, splits on \n) and return its {width, height}.
void draw (IRenderer &renderer, const TextLayout &layout, Rect bounds, Color color, const TextMeasureSpec &spec, Align align=Align::Start, std::string_view fallbackText={}, const Rect *cullRect=nullptr)
 Rasterize any missing glyphs into the atlas and emit batched drawGeometry calls.

Static Public Member Functions

static TextDirection detectDirection (std::string_view text)
 Detect the strong base direction of text via UAX #9 (RTL or LTR).
static size_t hitTestLine (std::string_view fullText, const TextLine &line, float localX, bool clampSoftBreak=false)
 Map a line-local X coordinate to the closest UTF-8 cluster byte offset.
static size_t hitTestPoint (std::string_view fullText, const TextLayout &layout, Vec2 localPos, Align textAlign=Align::Start, float boxWidth=0.0f, bool clampSoftBreak=true)
 Map a layout-local (x, y) point to the closest UTF-8 cluster byte offset.
static size_t lineIndexForByte (const TextLayout &layout, size_t byteIndex)
 Return the line index containing byteIndex.
static float caretXOnLine (const TextLine &line, size_t byteIndex)
 Return the line-local X coordinate of byteIndex on line.
static Rect caretRect (const TextLayout &layout, size_t byteIndex, float caretWidth=2.0f, Align textAlign=Align::Start, float boxWidth=0.0f)
 Compute the caret rectangle in layout-local coordinates.
static std::vector< Rect > selectionRects (const TextLayout &layout, size_t selStart, size_t selEnd, Align textAlign=Align::Start, float boxWidth=0.0f)
 Compute selection highlight rectangles in layout-local coordinates for [selStart, selEnd).

Detailed Description

Per-context font store, HarfBuzz shaper, FreeType glyph atlas, and layout engine.

Member Function Documentation

◆ draw()

void ZenitUI::TextEngine::draw ( IRenderer & renderer,
const TextLayout & layout,
Rect bounds,
Color color,
const TextMeasureSpec & spec,
Align align = Align::Start,
std::string_view fallbackText = {},
const Rect * cullRect = nullptr )

Rasterize any missing glyphs into the atlas and emit batched drawGeometry calls.

Parameters
rendererDestination renderer.
layoutPreviously computed TextLayout.
boundsLogical bounding box for alignment (x, y, width, height).
colorText color (including opacity).
specFont size used to select physical glyph pixel size.
alignHorizontal alignment within bounds.width.
fallbackTextSource UTF-8 text used only in synthetic headless fallback mode (when no TTF/OTF font is loaded in TextEngine).
cullRectOptional layout-local clip rectangle; lines completely outside its vertical range are skipped before rasterization and submission.

◆ hitTestLine()

size_t ZenitUI::TextEngine::hitTestLine ( std::string_view fullText,
const TextLine & line,
float localX,
bool clampSoftBreak = false )
static

Map a line-local X coordinate to the closest UTF-8 cluster byte offset.

Parameters
fullTextOriginal UTF-8 text backing line.
lineLaid-out visual line to hit-test.
localXHorizontal offset from the start of line in logical pixels.
clampSoftBreakWhen true and !line.hardBreak, avoids returning line.byteEnd (which belongs to the next wrapped line).

The documentation for this class was generated from the following file: