Donner SVG main
0.8.0 · 382bd0190.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
TextEngineHelpers.h File Reference

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

#include <cstddef>
#include <cstdint>
#include <optional>
#include <span>
#include <string_view>
#include <utility>
#include <vector>
#include "donner/base/Length.h"
#include "donner/base/MathUtils.h"
#include "donner/base/SmallVector.h"
#include "donner/base/Transform.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::InlineDisplacement
 An authored inline displacement consumed by one positioned glyph group. More...
struct  donner::svg::text_engine_detail::RunPenExtent
 Per-run pen start/end position for textLength calculation. More...
struct  donner::svg::text_engine_detail::TextLengthTraversalStats
 Optional operation counts for deterministic textLength traversal complexity tests. 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.

Typedefs

using donner::svg::text_engine_detail::AnchorExclusions = std::vector<std::vector<bool>>
 Per-run masks indexed by original source-byte cluster, retaining CSS hanging decisions through SVG textLength adjustments and glyph quarantine. Empty runs have no excluded clusters.

Functions

template<typename Bitmap>
Transform2d donner::svg::text_engine_detail::computeLocalFromBitmapGlyph (const TextGlyph &glyph, const Bitmap &bitmap)
 Map positive-size bitmap texels through their bearing and stretch, then rotate at glyph origin.
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< ChunkRange > donner::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 typographic character has an absolute x or y position.
ByteIndexMappings donner::svg::text_engine_detail::buildByteIndexMappings (std::string_view spanText)
 Build the byte-to-codepoint map; shaping determines group applicability. DOM indexing is separate.
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, TextLengthTraversalStats *traversalStats=nullptr)
 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, const AnchorExclusions &exclusions={}, double firstLineIndent=0.0)
 Fix up chunk anchors and apply the existing per-line anchor profile. firstLineIndent contributes its signed leading margin only to the first flat chunk.
bool donner::svg::text_engine_detail::applyInlineSizeWrap (std::vector< TextRun > &runs, const components::ComputedTextComponent &text, const TextLayoutParams &params, double measurePx, double lineHeightPx, const TextBackend *backend=nullptr, std::span< const ChunkBoundary > chunks={}, std::span< const InlineDisplacement > displacements={}, AnchorExclusions *exclusions=nullptr)
 Apply whitespace-dependent line layout along the inline axis, before text-anchor adjustment. Each line/chunk uses its first addressable character's anchor; lines advance along the block axis.
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,
const TextBackend * backend = nullptr,
std::span< const ChunkBoundary > chunks = {},
std::span< const InlineDisplacement > displacements = {},
AnchorExclusions * exclusions = nullptr )

Apply whitespace-dependent line layout along the inline axis, before text-anchor adjustment. Each line/chunk uses its first addressable character's anchor; lines advance along the block axis.

Soft wrapping uses spaces and tabs according to each span's white-space policy. Preserved segment breaks force new lines even without inline-size. Overflow-wrap enables bounded emergency typographic-unit breaks; there is no hyphenation or CJK per-character policy. Collapsible trailing spaces are removed; pre-wrap spaces may hang, while break-spaces gives each preserved space a break opportunity. Text-on-path runs are excluded: their controls are shaped as blanks without creating line boxes.

Parameters
runsRuns produced by the single-line layout; glyph positions are rewritten.
textComputed text tree, parallel to runs (span i corresponds to run i).
paramsLayout params, including writing mode and the first-line start margin.
measurePxThe inline-size measure in pixels, or zero to apply only forced breaks/tabs.
lineHeightPxAdvance between line baselines along the block axis, in pixels.
backendFont metrics for the minimum tab advance (0.5ch).
chunksAuthored text chunk boundaries, retained across line breaks.
displacementsPer-group authored inline shifts, applied after CSS line layout.
exclusionsReceives source-cluster hanging decisions for final single-line anchoring.
Returns
true if multiple lines were laid out. Tab-only changes return false.

◆ 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).

◆ computeLocalFromBitmapGlyph()

template<typename Bitmap>
Transform2d donner::svg::text_engine_detail::computeLocalFromBitmapGlyph ( const TextGlyph & glyph,
const Bitmap & bitmap )

Map positive-size bitmap texels through their bearing and stretch, then rotate at glyph origin.

Parameters
glyphPlaced glyph in text-element local coordinates.
bitmapDecoded bitmap or metadata with positive width and height.
Returns
Transform from bitmap texels to text-element local coordinates.