Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
font_options.hpp
Go to the documentation of this file.
1// font_options.hpp
2//
3// `FontOptions` is the typed, hashable blob that replaces today's
4// `(family_string, size, weight, slant)` tuple wherever the SDK needs to
5// describe what font to use. Every cache in the font subsystem must key on
6// the FULL blob, never on a subset — this single rule eliminates an entire
7// class of "the cache returned the wrong thing because the key was too
8// narrow" bugs (lifecycle invalidation, axis-instance cohabitation,
9// per-feature cache splits, scope isolation).
10//
11// Deliberately NOT included in FontOptions:
12// * Paint position (x, y) — that's an argument to ShapedText::paint_at.
13// * TextAnchor — that's an argument to ShapedText::paint_at.
14// Folding either into FontOptions would force every cache miss to re-shape
15// on an anchor or position change. The `paint_at` API takes them separately so
16// the same `ShapedText` artifact can paint at any anchor without re-shaping.
17//
18// This header is intentionally Skia-free so non-GPU translation units
19// (e.g. `core/view/` widgets) can construct FontOptions without dragging
20// the Skia headers in. The resolver layer (font_resolver.hpp) is the seam
21// where Skia types are introduced.
22
23#pragma once
24
25#include <cstdint>
26#include <functional>
27#include <string>
28#include <utility>
29#include <vector>
30
31namespace pulp::canvas {
32
33// ── Enums ────────────────────────────────────────────────────────────────
34
37enum class FontSlant : std::uint8_t {
41};
42
45enum class BaseDirection : std::uint8_t {
49};
50
64enum class FallbackMode : std::uint8_t {
68};
69
72enum class HintingMode : std::uint8_t {
78};
79
84enum class AntiAliasMode : std::uint8_t {
89};
90
95enum class ColorFontMode : std::uint8_t {
101};
102
103// ── Tag types ────────────────────────────────────────────────────────────
104
107using FontFeatureTag = std::uint32_t;
108
109constexpr FontFeatureTag make_font_feature_tag(char a, char b, char c, char d) {
110 return (static_cast<std::uint32_t>(static_cast<unsigned char>(a)) << 24)
111 | (static_cast<std::uint32_t>(static_cast<unsigned char>(b)) << 16)
112 | (static_cast<std::uint32_t>(static_cast<unsigned char>(c)) << 8)
113 | (static_cast<std::uint32_t>(static_cast<unsigned char>(d)));
114}
115
121 std::int32_t value = 0;
122
123 constexpr bool operator==(const FontFeature& other) const noexcept {
124 return tag == other.tag && value == other.value;
125 }
126};
127
131using VariationAxisTag = std::uint32_t;
132
133constexpr VariationAxisTag make_variation_axis_tag(char a, char b, char c, char d) {
134 return make_font_feature_tag(a, b, c, d);
135}
136
139 float value = 0.0f;
140
141 bool operator==(const VariationAxis& other) const noexcept {
142 return tag == other.tag && value == other.value;
143 }
144};
145
146// ── Synthesis policy ─────────────────────────────────────────────────────
147
154 bool weight = false;
155 bool slant = false;
156 bool width = false;
157
158 bool operator==(const FontSynthesisPolicy&) const = default;
159};
160
161// ── Scope ────────────────────────────────────────────────────────────────
162
169 enum class Kind : std::uint8_t { Global, Plugin, View };
170
172 std::uint64_t id = 0;
173
174 static constexpr FontScopeId global() noexcept { return {Kind::Global, 0}; }
175 static constexpr FontScopeId plugin(std::uint64_t plugin_id) noexcept {
176 return {Kind::Plugin, plugin_id};
177 }
178 static constexpr FontScopeId view(std::uint64_t view_id) noexcept {
179 return {Kind::View, view_id};
180 }
181
182 bool operator==(const FontScopeId&) const = default;
183};
184
185// ── FontOptions blob ─────────────────────────────────────────────────────
186
191 // ── Family + style ──────────────────────────────────────────────────
196 std::vector<std::string> family_stack;
197
200 float weight = 400.0f;
201
204 float width = 100.0f;
205
209 float oblique_angle = 0.0f;
210
214 float size = 14.0f;
215
216 // ── OpenType features ───────────────────────────────────────────────
217 std::vector<FontFeature> features;
218
219 // ── Variable-font axes ──────────────────────────────────────────────
223 std::vector<VariationAxis> variation_axes;
224
225 // ── Locale + direction ──────────────────────────────────────────────
228 std::string locale;
230
231
232 // ── Spacing ─────────────────────────────────────────────────────────
234 float letter_spacing = 0.0f;
236 float word_spacing = 0.0f;
237
238 // ── Render policy ───────────────────────────────────────────────────
242
243 // ── Synthesis policy ────────────────────────────────────────────────
245
246 // ── Resolver behavior ───────────────────────────────────────────────
249
256 std::uint64_t registry_generation = 0;
257
258 bool operator==(const FontOptions&) const = default;
259
263 std::size_t hash() const noexcept;
264};
265
266} // namespace pulp::canvas
267
268namespace std {
269template <>
270struct hash<pulp::canvas::FontOptions> {
271 std::size_t operator()(const pulp::canvas::FontOptions& opts) const noexcept {
272 return opts.hash();
273 }
274};
275template <>
276struct hash<pulp::canvas::FontScopeId> {
277 std::size_t operator()(const pulp::canvas::FontScopeId& id) const noexcept {
278 return (std::hash<std::uint64_t>{}(id.id) << 2)
279 ^ std::hash<std::uint8_t>{}(static_cast<std::uint8_t>(id.kind));
280 }
281};
282} // namespace std
Definition affine_transform.hpp:24
FontSlant
CSS-style slant.
Definition font_options.hpp:37
@ Oblique
Definition font_options.hpp:40
@ Italic
Definition font_options.hpp:39
@ Normal
Definition font_options.hpp:38
constexpr VariationAxisTag make_variation_axis_tag(char a, char b, char c, char d)
Definition font_options.hpp:133
std::uint32_t FontFeatureTag
OpenType four-byte tag (e.g.
Definition font_options.hpp:107
@ Default
Definition emoji_segmenter.hpp:42
BaseDirection
Logical text direction.
Definition font_options.hpp:45
@ Auto
Definition font_options.hpp:48
ColorFontMode
Color-font policy.
Definition font_options.hpp:95
@ Auto
Definition font_options.hpp:96
@ ForceMonochrome
Definition font_options.hpp:100
@ Bitmap
Definition font_options.hpp:97
@ SVG
Definition font_options.hpp:99
@ COLR
Definition font_options.hpp:98
constexpr FontFeatureTag make_font_feature_tag(char a, char b, char c, char d)
Definition font_options.hpp:109
@ Auto
Run UBA rule P2 (first strong character).
Definition bidi.hpp:43
@ LTR
Force paragraph level 0.
Definition bidi.hpp:44
@ RTL
Force paragraph level 1.
Definition bidi.hpp:45
HintingMode
CSS font-smoothing analogue.
Definition font_options.hpp:72
@ None
Definition font_options.hpp:73
@ PlatformDefault
Definition font_options.hpp:77
@ Slight
Definition font_options.hpp:74
@ Full
Definition font_options.hpp:76
AntiAliasMode
Default follows the existing inside_non_opaque_layer() heuristic for opacity-layer text edging.
Definition font_options.hpp:84
@ NoAA
Definition font_options.hpp:88
@ Default
Definition font_options.hpp:85
@ Grayscale
Definition font_options.hpp:87
@ LCD
Definition font_options.hpp:86
FallbackMode
Three fallback policies for the resolver cascade.
Definition font_options.hpp:64
@ Native
Definition font_options.hpp:66
@ Deterministic
Definition font_options.hpp:65
@ Hybrid
Definition font_options.hpp:67
std::uint32_t VariationAxisTag
Variation-axis tag + value.
Definition font_options.hpp:131
Definition analyzer_provider.hpp:12
STL namespace.
OpenType feature record: tag + value.
Definition font_options.hpp:119
constexpr bool operator==(const FontFeature &other) const noexcept
Definition font_options.hpp:123
std::int32_t value
Definition font_options.hpp:121
FontFeatureTag tag
Definition font_options.hpp:120
The typed, hashable contract that replaces (family_string, size) throughout the font subsystem.
Definition font_options.hpp:190
std::vector< std::string > family_stack
Full CSS-style cascade.
Definition font_options.hpp:196
BaseDirection direction
Definition font_options.hpp:229
std::vector< FontFeature > features
Definition font_options.hpp:217
HintingMode hinting_mode
Definition font_options.hpp:239
FontSlant slant
Definition font_options.hpp:206
std::size_t hash() const noexcept
Hash combining all fields.
FallbackMode fallback_mode
Definition font_options.hpp:247
float width
CSS font-stretch scalar 50..200, also used as wdth axis value when present.
Definition font_options.hpp:204
float letter_spacing
Extra letter spacing in px (post-shaping, between clusters).
Definition font_options.hpp:234
float oblique_angle
Slant angle in degrees.
Definition font_options.hpp:209
FontSynthesisPolicy font_synthesis
Definition font_options.hpp:244
FontScopeId scope
Definition font_options.hpp:248
float weight
CSS scalar 100..900 (or variable-axis value when the resolved face has a wght axis).
Definition font_options.hpp:200
std::vector< VariationAxis > variation_axes
Each entry: (axis tag, value).
Definition font_options.hpp:223
std::uint64_t registry_generation
Set by the resolver before returning a ResolvedFont.
Definition font_options.hpp:256
std::string locale
BCP-47 language tag ("", "en", "ja-JP", "zh-Hans").
Definition font_options.hpp:228
float size
Pixel size at the canvas's current device scale.
Definition font_options.hpp:214
float word_spacing
Extra word spacing in px (applied at whitespace cluster boundaries).
Definition font_options.hpp:236
AntiAliasMode aa_mode
Definition font_options.hpp:240
ColorFontMode color_font_mode
Definition font_options.hpp:241
bool operator==(const FontOptions &) const =default
Opaque identifier for a FontScope (defined in font_scope.hpp).
Definition font_options.hpp:168
Kind
Definition font_options.hpp:169
@ Plugin
Definition font_options.hpp:169
@ View
Definition font_options.hpp:169
@ Global
Definition font_options.hpp:169
static constexpr FontScopeId global() noexcept
Definition font_options.hpp:174
static constexpr FontScopeId view(std::uint64_t view_id) noexcept
Definition font_options.hpp:178
Kind kind
Definition font_options.hpp:171
bool operator==(const FontScopeId &) const =default
static constexpr FontScopeId plugin(std::uint64_t plugin_id) noexcept
Definition font_options.hpp:175
Explicit policy for faux-bold / faux-italic / faux-width.
Definition font_options.hpp:153
bool slant
Definition font_options.hpp:155
bool operator==(const FontSynthesisPolicy &) const =default
bool weight
Definition font_options.hpp:154
bool width
Definition font_options.hpp:156
Definition font_options.hpp:137
float value
Definition font_options.hpp:139
bool operator==(const VariationAxis &other) const noexcept
Definition font_options.hpp:141
VariationAxisTag tag
Definition font_options.hpp:138
std::size_t operator()(const pulp::canvas::FontOptions &opts) const noexcept
Definition font_options.hpp:271
std::size_t operator()(const pulp::canvas::FontScopeId &id) const noexcept
Definition font_options.hpp:277