Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
shaped_text.hpp
Go to the documentation of this file.
1// shaped_text.hpp
2//
3// `ShapedText` is the canonical artifact produced by `TextRunPlanner::shape`.
4// Measurement and paint paths are intended to consume this same artifact:
5// measurement uses the per-run advances and line-break opportunities, while
6// paint can walk the same runs, clusters, and eventual glyph buffers. Keeping
7// the text artifact shared prevents layout and rendering from inventing
8// incompatible segmentation rules.
9//
10// `TextRunPlanner` fills this structure with resolved font traces, bidi/script
11// runs, UAX #29-lite grapheme clusters, heuristic line-break opportunities, and
12// UTF-8 / UTF-16 / cluster index maps. Glyph buffers remain optional until the
13// painting path asks SkShaper for per-glyph output.
14
15#pragma once
16
19
20#include <cstdint>
21#include <string>
22#include <string_view>
23#include <vector>
24
25namespace pulp::canvas {
26
27// ── Metrics ──────────────────────────────────────────────────────────────
28
33struct RunMetrics {
34 float ascent = 0.0f;
35 float descent = 0.0f;
36 float leading = 0.0f;
37
38 float line_height() const noexcept { return ascent + descent + leading; }
39};
40
41// ── ShapedRun ────────────────────────────────────────────────────────────
42
48struct ShapedRun {
51 std::uint8_t bidi_level = 0;
52 std::uint32_t script_tag = 0;
53 std::string locale;
54
56 std::vector<std::uint16_t> glyph_ids;
57 std::vector<float> advances;
58 std::vector<float> offsets_x;
59 std::vector<float> offsets_y;
64 std::vector<std::uint32_t> cluster_indices;
65
67 std::size_t logical_start = 0;
68 std::size_t logical_end = 0;
69
72 float advance_total = 0.0f;
73};
74
75// ── Clusters + line breaks ───────────────────────────────────────────────
76
81 std::uint32_t utf8_start = 0;
82 std::uint32_t utf8_end = 0;
83 std::uint32_t run_index = 0;
84 std::uint32_t glyph_start = 0;
85 std::uint32_t glyph_count = 0;
86};
87
93 std::uint32_t utf8_offset = 0;
94 enum class Kind : std::uint8_t {
98};
99
100// ── Unicode index map ────────────────────────────────────────────────────
101
110 std::vector<std::uint32_t> scalar_offsets;
111
115 std::vector<std::uint32_t> utf16_offsets;
116
119 std::vector<std::uint32_t> byte_to_cluster;
120
121 std::size_t scalar_count() const noexcept {
122 return scalar_offsets.empty() ? 0 : scalar_offsets.size() - 1;
123 }
124};
125
126// ── ShapedText ───────────────────────────────────────────────────────────
127
133 std::string text;
135
136 std::vector<ShapedRun> runs;
137 std::vector<ClusterEntry> clusters;
138 std::vector<LineBreakOpportunity> line_breaks;
140
144 float total_width = 0.0f;
145
150
151 bool empty() const noexcept { return runs.empty(); }
152};
153
154} // namespace pulp::canvas
Definition affine_transform.hpp:24
One grapheme cluster in the input.
Definition shaped_text.hpp:80
std::uint32_t utf8_start
UTF-8 byte offset (start, inclusive).
Definition shaped_text.hpp:81
std::uint32_t glyph_count
Glyph count for this cluster; 0 until glyph buffers are populated.
Definition shaped_text.hpp:85
std::uint32_t run_index
Index into ShapedText::runs.
Definition shaped_text.hpp:83
std::uint32_t glyph_start
First glyph in that run; 0 until glyph buffers are populated.
Definition shaped_text.hpp:84
std::uint32_t utf8_end
UTF-8 byte offset (end, exclusive).
Definition shaped_text.hpp:82
The typed, hashable contract that replaces (family_string, size) throughout the font subsystem.
Definition font_options.hpp:190
Line-break opportunity used by the line layout step.
Definition shaped_text.hpp:92
std::uint32_t utf8_offset
Definition shaped_text.hpp:93
Kind
Definition shaped_text.hpp:94
@ Hard
Wrap required (U+000A, U+2028, U+2029).
Definition shaped_text.hpp:96
@ Soft
Wrap allowed (whitespace / discretionary).
Definition shaped_text.hpp:95
enum pulp::canvas::LineBreakOpportunity::Kind kind
Definition font_resolver.hpp:216
Per-line / per-run typographic metrics.
Definition shaped_text.hpp:33
float ascent
Definition shaped_text.hpp:34
float line_height() const noexcept
Definition shaped_text.hpp:38
float descent
Definition shaped_text.hpp:35
float leading
Definition shaped_text.hpp:36
One run inside a ShapedText — a maximal slice of the input text that shares (bidi level × script × la...
Definition shaped_text.hpp:48
std::string locale
BCP-47; "" inherits from FontOptions.
Definition shaped_text.hpp:53
std::vector< std::uint32_t > cluster_indices
cluster_indices[i] is the UTF-8 byte index of the cluster the i-th glyph belongs to.
Definition shaped_text.hpp:64
std::vector< float > offsets_x
Definition shaped_text.hpp:58
std::vector< float > advances
Definition shaped_text.hpp:57
RunMetrics metrics
Definition shaped_text.hpp:50
std::size_t logical_end
Definition shaped_text.hpp:68
ResolvedFont font
Resolved typeface + trace.
Definition shaped_text.hpp:49
std::vector< float > offsets_y
Definition shaped_text.hpp:59
std::size_t logical_start
Logical (input-order) UTF-8 byte range this run covers.
Definition shaped_text.hpp:67
std::vector< std::uint16_t > glyph_ids
Glyph data, parallel arrays of length glyph_ids.size().
Definition shaped_text.hpp:56
std::uint8_t bidi_level
0 = LTR; odd = RTL.
Definition shaped_text.hpp:51
float advance_total
Sum of advances plus letter/word spacing.
Definition shaped_text.hpp:72
std::uint32_t script_tag
ISO 15924 four-char as packed u32; 0 = Common.
Definition shaped_text.hpp:52
The canonical output of TextRunPlanner::shape(text, FontOptions).
Definition shaped_text.hpp:132
std::string text
Owned UTF-8 copy of the input.
Definition shaped_text.hpp:133
float total_width
Sum of per-run advance_total.
Definition shaped_text.hpp:144
std::vector< ClusterEntry > clusters
Definition shaped_text.hpp:137
RunMetrics overall_metrics
Worst-case ascent / descent / leading across all runs on a single notional line.
Definition shaped_text.hpp:149
std::vector< ShapedRun > runs
Definition shaped_text.hpp:136
std::vector< LineBreakOpportunity > line_breaks
Definition shaped_text.hpp:138
UnicodeIndexMap index_map
Definition shaped_text.hpp:139
bool empty() const noexcept
Definition shaped_text.hpp:151
FontOptions options
Full FontOptions blob the shape was produced for.
Definition shaped_text.hpp:134
JS counts UTF-16, ICU APIs vary between UTF-16 and scalar offsets, HarfBuzz reports cluster indices,...
Definition shaped_text.hpp:106
std::vector< std::uint32_t > byte_to_cluster
utf8_byte → cluster index.
Definition shaped_text.hpp:119
std::vector< std::uint32_t > scalar_offsets
scalar_offsets[i] = UTF-8 byte index of the i-th Unicode scalar value (codepoint).
Definition shaped_text.hpp:110
std::vector< std::uint32_t > utf16_offsets
utf16_offsets[i] = UTF-16 code-unit index of the i-th Unicode scalar.
Definition shaped_text.hpp:115
std::size_t scalar_count() const noexcept
Definition shaped_text.hpp:121