Donner SVG Editor & Engine
SVG-native editor and embeddable SVG2 + CSS3 engine in C++20, with GPU (WebGPU) and compact CPU renderers, built for correctness, security, and performance.
Loading...
Searching...
No Matches
TextEngineHelpers.h File Reference

Internal helpers for TextEngine layout. Exposed in a header for unit testing. More...

#include <optional>
#include <string_view>
#include <vector>
#include "donner/base/Length.h"
#include "donner/base/SmallVector.h"
#include "donner/svg/components/text/ComputedTextComponent.h"
#include "donner/svg/core/DominantBaseline.h"
#include "donner/svg/core/TextAnchor.h"
#include "donner/svg/text/TextBackend.h"
#include "donner/svg/text/TextLayoutParams.h"
#include "donner/svg/text/TextTypes.h"
Include dependency graph for TextEngineHelpers.h:

Classes

struct  donner::svg::text_engine_detail::ChunkRange
 Byte range within a span representing a shaping chunk. More...
struct  donner::svg::text_engine_detail::ByteIndexMappings
 Index mapping result for a span's text. More...
struct  donner::svg::text_engine_detail::ChunkBoundary
 Text chunk boundary for per-chunk text-anchor adjustment. More...
struct  donner::svg::text_engine_detail::RunPenExtent
 Per-run pen start/end position for textLength calculation. More...

Namespaces

namespace  donner
 Top-level Donner namespace, which is split into different sub-namespaces such as donner::svg and donner::css.
namespace  donner::svg
 Donner SVG library, which can load, manipulate and render SVG files.

Functions

double donner::svg::text_engine_detail::computeBaselineShift (DominantBaseline baseline, const FontVMetrics &vm, float scale)
 Compute the baseline-alignment offset for a dominant-baseline / alignment-baseline keyword, in pixels. The offset is added to the baseline Y position, so positive values move glyphs down (+Y in SVG coordinates); e.g. hanging returns +0.8 * ascent.
std::vector< ChunkRangedonner::svg::text_engine_detail::findChunkRanges (std::string_view spanText, const SmallVector< std::optional< Lengthd >, 1 > &xList, const SmallVector< std::optional< Lengthd >, 1 > &yList)
 Pre-scan span text to find chunk byte boundaries. A new chunk starts when a non-first codepoint has an absolute x or y position.
ByteIndexMappings donner::svg::text_engine_detail::buildByteIndexMappings (std::string_view spanText)
 Build byte→charIdx and byte→rawCpIdx mappings for a span.
void donner::svg::text_engine_detail::applyTextLength (std::vector< TextRun > &runs, const components::ComputedTextComponent &text, const std::vector< RunPenExtent > &runExtents, const TextLayoutParams &params, bool vertical, double currentPenX, double currentPenY)
 Apply per-span and global textLength adjustments to positioned runs.
void donner::svg::text_engine_detail::applyTextAnchor (std::vector< TextRun > &runs, std::vector< ChunkBoundary > &chunkBoundaries, const components::ComputedTextComponent &text, bool vertical)
 Fix up chunk text-anchors and apply per-chunk text-anchor adjustment.
bool donner::svg::text_engine_detail::applyInlineSizeWrap (std::vector< TextRun > &runs, const components::ComputedTextComponent &text, const TextLayoutParams &params, double measurePx, double lineHeightPx)
 Break horizontally-laid-out runs into stacked lines for SVG2 inline-size auto-flow, rewriting glyph positions in place. Called after the flat single-line layout and before text-anchor adjustment; when it wraps it applies text-anchor per line itself.
double donner::svg::text_engine_detail::computeSpanBaselineShiftPx (const TextBackend &backend, const components::ComputedTextComponent::TextSpan &span, FontHandle spanFont, float spanScale, const TextLayoutParams &params)
 Compute per-span baseline-shift in pixels, including OS/2 sub/super metrics and ancestor baseline-shift accumulation.

Detailed Description

Internal helpers for TextEngine layout. Exposed in a header for unit testing.

Function Documentation

◆ applyInlineSizeWrap()

bool donner::svg::text_engine_detail::applyInlineSizeWrap ( std::vector< TextRun > & runs,
const components::ComputedTextComponent & text,
const TextLayoutParams & params,
double measurePx,
double lineHeightPx )

Break horizontally-laid-out runs into stacked lines for SVG2 inline-size auto-flow, rewriting glyph positions in place. Called after the flat single-line layout and before text-anchor adjustment; when it wraps it applies text-anchor per line itself.

Wrap rules (slice 1, horizontal writing modes only):

  • Soft-wrap opportunities are whitespace (U+0020/tab/newline) between words; there is no mid-word breaking, hyphenation, or CJK per-character breaking.
  • Greedy line filling: a word is placed on the current line when it fits within measurePx, otherwise it starts a new line. The first word of a line is always placed even when it overflows the measure.
  • Whitespace at a soft-wrap break hangs on the previous line and is not counted toward the next line's width; intra-line spacing is preserved from the original layout.
  • Each line is independently anchored by text-anchor (from params) about the block origin (the first glyph's position) and stacked downward by lineHeightPx.
Parameters
runsRuns produced by the flat horizontal layout; glyph positions are rewritten.
textComputed text tree, parallel to runs (span i ↔ run i), used for span text.
paramsLayout params; textAnchor selects per-line anchoring.
measurePxThe inline-size measure in pixels (must be > 0).
lineHeightPxVertical advance between line baselines in pixels.
Returns
true if wrapping was applied.

◆ computeBaselineShift()

double donner::svg::text_engine_detail::computeBaselineShift ( DominantBaseline baseline,
const FontVMetrics & vm,
float scale )

Compute the baseline-alignment offset for a dominant-baseline / alignment-baseline keyword, in pixels. The offset is added to the baseline Y position, so positive values move glyphs down (+Y in SVG coordinates); e.g. hanging returns +0.8 * ascent.

Parameters
baselineEffective baseline keyword for the span.
vmFont vertical metrics in design units (FontVMetrics::xHeight may be 0).
scaleDesign units → pixels scale (em scale, fontSize / unitsPerEm).