1
//! Font parsing, metrics extraction, and subsetting.
2
//!
3
//! This module provides the core font infrastructure for text layout and PDF generation:
4
//! - `loading`: System font cache construction and font reload errors
5
//! - `mock`: Mock font implementation for testing without real font files
6
//! - `parsed`: Full font parsing via allsorts (outlines, metrics, shaping tables, subsetting)
7

            
8
#![cfg(feature = "font_loading")]
9

            
10
use azul_css::{AzString, U8Vec};
11
use rust_fontconfig::{FcFontCache, OwnedFontSource};
12

            
13
pub mod loading {
14
    #![cfg(feature = "std")]
15
    #![cfg(feature = "font_loading")]
16
    #![cfg_attr(not(feature = "std"), no_std)]
17

            
18
    use std::io::Error as IoError;
19

            
20
    use azul_css::{AzString, StringVec, U8Vec};
21
    use rust_fontconfig::FcFontCache;
22

            
23
    #[cfg(not(miri))]
24
3432
    pub fn build_font_cache() -> FcFontCache {
25
3432
        FcFontCache::build()
26
3432
    }
27

            
28
    #[cfg(miri)]
29
    pub fn build_font_cache() -> FcFontCache {
30
        FcFontCache::default()
31
    }
32

            
33
    #[derive(Debug)]
34
    pub enum FontReloadError {
35
        Io(IoError, AzString),
36
        FontNotFound(AzString),
37
        FontLoadingNotActive(AzString),
38
    }
39

            
40
    impl Clone for FontReloadError {
41
        fn clone(&self) -> Self {
42
            use self::FontReloadError::*;
43
            match self {
44
                Io(err, path) => Io(IoError::new(err.kind(), "Io Error"), path.clone()),
45
                FontNotFound(id) => FontNotFound(id.clone()),
46
                FontLoadingNotActive(id) => FontLoadingNotActive(id.clone()),
47
            }
48
        }
49
    }
50

            
51
    azul_core::impl_display!(FontReloadError, {
52
        Io(err, path_buf) => format!("Could not load \"{}\" - IO error: {}", path_buf.as_str(), err),
53
        FontNotFound(id) => format!("Could not locate system font: \"{:?}\" found", id),
54
        FontLoadingNotActive(id) => format!("Could not load system font: \"{:?}\": crate was not compiled with --features=\"font_loading\"", id)
55
    });
56
}
57
pub mod mock {
58
    //! Mock font implementation for testing text layout.
59
    //!
60
    //! Provides a `MockFont` that simulates font behavior without requiring
61
    //! actual font files, useful for unit testing text layout functionality.
62

            
63
    use std::collections::BTreeMap;
64

            
65
    use crate::text3::cache::LayoutFontMetrics;
66

            
67
    /// A mock font implementation for testing text layout without real fonts.
68
    ///
69
    /// This allows testing text shaping, layout, and rendering code paths
70
    /// without needing to load actual TrueType/OpenType font files.
71
    #[derive(Debug, Clone)]
72
    pub struct MockFont {
73
        /// Font metrics (ascent, descent, etc.).
74
        pub font_metrics: LayoutFontMetrics,
75
        /// Width of the space character in font units.
76
        pub space_width: Option<usize>,
77
        /// Horizontal advance widths keyed by glyph ID.
78
        pub glyph_advances: BTreeMap<u16, u16>,
79
        /// Glyph bounding box sizes (width, height) keyed by glyph ID.
80
        pub glyph_sizes: BTreeMap<u16, (i32, i32)>,
81
        /// Unicode codepoint to glyph ID mapping.
82
        pub glyph_indices: BTreeMap<u32, u16>,
83
    }
84

            
85
    impl MockFont {
86
        /// Creates a new `MockFont` with the given font metrics.
87
        pub fn new(font_metrics: LayoutFontMetrics) -> Self {
88
            MockFont {
89
                font_metrics,
90
                space_width: Some(10),
91
                glyph_advances: BTreeMap::new(),
92
                glyph_sizes: BTreeMap::new(),
93
                glyph_indices: BTreeMap::new(),
94
            }
95
        }
96

            
97
        /// Sets the space character width.
98
        pub fn with_space_width(mut self, width: usize) -> Self {
99
            self.space_width = Some(width);
100
            self
101
        }
102

            
103
        /// Adds a horizontal advance value for a glyph.
104
        pub fn with_glyph_advance(mut self, glyph_index: u16, advance: u16) -> Self {
105
            self.glyph_advances.insert(glyph_index, advance);
106
            self
107
        }
108

            
109
        /// Adds a bounding box size for a glyph.
110
        pub fn with_glyph_size(mut self, glyph_index: u16, size: (i32, i32)) -> Self {
111
            self.glyph_sizes.insert(glyph_index, size);
112
            self
113
        }
114

            
115
        /// Adds a Unicode codepoint to glyph ID mapping.
116
        pub fn with_glyph_index(mut self, unicode: u32, index: u16) -> Self {
117
            self.glyph_indices.insert(unicode, index);
118
            self
119
        }
120
    }
121
}
122

            
123
pub mod parsed {
124
    use core::fmt;
125
    use std::{collections::BTreeMap, sync::Arc};
126

            
127
    use allsorts::{
128
        binary::read::ReadScope,
129
        font_data::FontData,
130
        layout::{GDEFTable, LayoutCache, LayoutCacheData, GPOS, GSUB},
131
        outline::{OutlineBuilder, OutlineSink},
132
        pathfinder_geometry::{line_segment::LineSegment2F, vector::Vector2F},
133
        subset::{subset as allsorts_subset, whole_font, CmapTarget, SubsetProfile},
134
        tables::{
135
            cmap::owned::CmapSubtable as OwnedCmapSubtable,
136
            glyf::{
137
                Glyph, GlyfVisitorContext, LocaGlyf, Point,
138
                VariableGlyfContext, VariableGlyfContextStore,
139
            },
140
            kern::owned::KernTable,
141
            FontTableProvider, HheaTable, MaxpTable,
142
        },
143
        tag,
144
    };
145
    use azul_core::resources::{
146
        GlyphOutline, GlyphOutlineOperation, OutlineCubicTo, OutlineLineTo, OutlineMoveTo,
147
        OutlineQuadTo, OwnedGlyphBoundingBox,
148
    };
149
    use azul_css::props::basic::FontMetrics as CssFontMetrics;
150

            
151
    // Mock font module for testing
152
    pub use crate::font::mock::MockFont;
153
    use crate::text3::cache::LayoutFontMetrics;
154

            
155
    /// Cached GSUB table for glyph substitution operations.
156
    pub type GsubCache = Arc<LayoutCacheData<GSUB>>;
157
    /// Cached GPOS table for glyph positioning operations.
158
    pub type GposCache = Arc<LayoutCacheData<GPOS>>;
159

            
160
    /// Monotonic-clock nanos since process start. Used to timestamp
161
    /// `ParsedFont.last_used` for LRU eviction. Cheap (single
162
    /// `Instant::now`); resolution is plenty fine for "did this
163
    /// face get touched in the last N seconds" decisions. Exposed
164
    /// `pub(crate)` so `FontManager::evict_unused` reads from the
165
    /// same clock as `last_used` writes.
166
152636
    pub(crate) fn monotonic_now_nanos() -> u64 {
167
        // Safe: `Instant::elapsed` against the same launch instant is
168
        // monotonic and never overflows in any realistic process
169
        // lifetime (>500 years).
170
        use std::sync::OnceLock;
171
        use std::time::Instant;
172
        static LAUNCH: OnceLock<Instant> = OnceLock::new();
173
152636
        let start = LAUNCH.get_or_init(Instant::now);
174
152636
        start.elapsed().as_nanos() as u64
175
152636
    }
176

            
177
    /// Glyph-outline decoder state. See the
178
    /// [`ParsedFont::loca_glyf`] field docs for the full description.
179
    #[derive(Clone)]
180
    pub(crate) enum LocaGlyfState {
181
        /// Ready to decode immediately, or known to have no outline
182
        /// data. `None` covers both CFF fonts and fonts where the
183
        /// loca+glyf parse failed.
184
        ///
185
        /// This variant *cannot* be evicted by
186
        /// [`crate::text3::cache::FontManager::evict_unused`]: there
187
        /// are no source bytes retained to re-decode from. The eager
188
        /// `from_bytes` path (tests, `with_source_bytes` PDF callers)
189
        /// produces this variant.
190
        Loaded(Option<Arc<std::sync::Mutex<LocaGlyf>>>),
191
        /// Font bytes retained for lazy `LocaGlyf` construction.
192
        ///
193
        /// `loaded` is `Mutex<Option<…>>` (not `OnceLock`) so an
194
        /// idle eviction can clear it back to `None`; the next
195
        /// `get_or_decode_glyph` will re-parse from `bytes`. Two-step
196
        /// double-check pattern in `resolve_loca_glyf` keeps the
197
        /// expensive `LocaGlyf::load` outside the critical section.
198
        Deferred {
199
            bytes: Arc<rust_fontconfig::FontBytes>,
200
            font_index: usize,
201
            loaded: Arc<std::sync::Mutex<Option<Arc<std::sync::Mutex<LocaGlyf>>>>>,
202
        },
203
    }
204

            
205
    /// Adapter that collects allsorts outline commands into our `GlyphOutline` format.
206
    ///
207
    /// Implements `OutlineSink` so it can be passed to `GlyfVisitorContext::visit()`.
208
    /// This handles composite glyph resolution, transforms, and variable font
209
    /// deltas automatically via allsorts internals.
210
    struct GlyphOutlineCollector {
211
        contours: Vec<GlyphOutline>,
212
        current_contour: Vec<GlyphOutlineOperation>,
213
    }
214

            
215
    impl GlyphOutlineCollector {
216
43428
        fn new() -> Self {
217
43428
            Self {
218
43428
                contours: Vec::new(),
219
43428
                current_contour: Vec::new(),
220
43428
            }
221
43428
        }
222

            
223
43428
        fn into_outlines(mut self) -> Vec<GlyphOutline> {
224
43428
            if !self.current_contour.is_empty() {
225
                self.contours.push(GlyphOutline {
226
                    operations: std::mem::take(&mut self.current_contour).into(),
227
                });
228
43428
            }
229
43428
            self.contours
230
43428
        }
231
    }
232

            
233
    impl OutlineSink for GlyphOutlineCollector {
234
61204
        fn move_to(&mut self, to: Vector2F) {
235
61204
            if !self.current_contour.is_empty() {
236
                self.contours.push(GlyphOutline {
237
                    operations: std::mem::take(&mut self.current_contour).into(),
238
                });
239
61204
            }
240
61204
            self.current_contour.push(GlyphOutlineOperation::MoveTo(OutlineMoveTo {
241
61204
                x: to.x() as i16,
242
61204
                y: to.y() as i16,
243
61204
            }));
244
61204
        }
245

            
246
255640
        fn line_to(&mut self, to: Vector2F) {
247
255640
            self.current_contour.push(GlyphOutlineOperation::LineTo(OutlineLineTo {
248
255640
                x: to.x() as i16,
249
255640
                y: to.y() as i16,
250
255640
            }));
251
255640
        }
252

            
253
807268
        fn quadratic_curve_to(&mut self, ctrl: Vector2F, to: Vector2F) {
254
807268
            self.current_contour.push(GlyphOutlineOperation::QuadraticCurveTo(
255
807268
                OutlineQuadTo {
256
807268
                    ctrl_1_x: ctrl.x() as i16,
257
807268
                    ctrl_1_y: ctrl.y() as i16,
258
807268
                    end_x: to.x() as i16,
259
807268
                    end_y: to.y() as i16,
260
807268
                },
261
807268
            ));
262
807268
        }
263

            
264
        fn cubic_curve_to(&mut self, ctrl: LineSegment2F, to: Vector2F) {
265
            self.current_contour.push(GlyphOutlineOperation::CubicCurveTo(
266
                OutlineCubicTo {
267
                    ctrl_1_x: ctrl.from_x() as i16,
268
                    ctrl_1_y: ctrl.from_y() as i16,
269
                    ctrl_2_x: ctrl.to_x() as i16,
270
                    ctrl_2_y: ctrl.to_y() as i16,
271
                    end_x: to.x() as i16,
272
                    end_y: to.y() as i16,
273
                },
274
            ));
275
        }
276

            
277
61204
        fn close(&mut self) {
278
61204
            self.current_contour.push(GlyphOutlineOperation::ClosePath);
279
61204
            self.contours.push(GlyphOutline {
280
61204
                operations: std::mem::take(&mut self.current_contour).into(),
281
61204
            });
282
61204
        }
283
    }
284

            
285
    /// Parsed font data with all required tables for text layout and PDF generation.
286
    ///
287
    /// This struct holds the parsed representation of a TrueType/OpenType font,
288
    /// including glyph outlines, metrics, and shaping tables. It's used for:
289
    /// - Text layout (via GSUB/GPOS tables)
290
    /// - Glyph rendering (via glyf/CFF outlines)
291
    /// - PDF font embedding (via font metrics and subsetting)
292
    pub struct ParsedFont {
293
        /// Hash of the font bytes for caching and equality checks.
294
        pub hash: u64,
295
        /// Layout-specific font metrics (ascent, descent, line gap).
296
        pub font_metrics: LayoutFontMetrics,
297
        /// PDF-specific detailed font metrics from HEAD, HHEA, OS/2 tables.
298
        pub pdf_font_metrics: PdfFontMetrics,
299
        /// Total number of glyphs in the font (from maxp table).
300
        pub num_glyphs: u16,
301
        /// Horizontal header table (hhea) containing global horizontal metrics.
302
        pub hhea_table: HheaTable,
303
        /// Offset+length into original_bytes for hmtx table (lazy: no copy).
304
        pub hmtx_range: (usize, usize),
305
        /// Offset+length into original_bytes for vmtx table (lazy: no copy).
306
        pub vmtx_range: (usize, usize),
307
        /// Vertical header table (vhea), same format as hhea. None if font has no vertical metrics.
308
        pub vhea_table: Option<HheaTable>,
309
        /// Maximum profile table (maxp) containing glyph count and memory hints.
310
        pub maxp_table: MaxpTable,
311
        /// Raw GSUB table bytes, kept as a `Vec<u8>` (tens to low-hundreds
312
        /// of KiB) so the parsed `GsubCache` can be built on first shape
313
        /// call instead of up-front. Access via [`ParsedFont::gsub`] —
314
        /// that getter populates `gsub_cache_lazy` via `OnceLock` and
315
        /// returns a borrow.
316
        pub(crate) gsub_bytes: Option<Vec<u8>>,
317
        /// Lazy GSUB cache: populated on first [`ParsedFont::gsub`] call.
318
        /// `None` means "font has no GSUB table" *after* init attempt;
319
        /// the `OnceLock` wrapper distinguishes "not yet initialised"
320
        /// from "initialised to None".
321
        pub(crate) gsub_cache_lazy: std::sync::OnceLock<Option<GsubCache>>,
322
        /// Raw GPOS table bytes. Same lazy-parse arrangement as
323
        /// `gsub_bytes` — see [`ParsedFont::gpos`].
324
        pub(crate) gpos_bytes: Option<Vec<u8>>,
325
        /// Lazy GPOS cache, populated on first [`ParsedFont::gpos`] call.
326
        pub(crate) gpos_cache_lazy: std::sync::OnceLock<Option<GposCache>>,
327
        /// Glyph definition table (GDEF) for glyph classification.
328
        pub opt_gdef_table: Option<Arc<GDEFTable>>,
329
        /// Legacy kerning table (kern) for fonts without GPOS.
330
        pub opt_kern_table: Option<Arc<KernTable>>,
331
        /// Monotonic-clock nanos at the most recent
332
        /// [`ParsedFont::get_or_decode_glyph`] / `gsub()` / `gpos()`
333
        /// call. `0` means "never touched". Used by
334
        /// [`crate::text3::cache::FontManager::evict_unused`] to
335
        /// decide which `LocaGlyfState::Deferred` faces to release.
336
        pub(crate) last_used: Arc<std::sync::atomic::AtomicU64>,
337
        /// `true` if this font is a variable font (carries a `gvar`
338
        /// table). Cached at parse time so [`decode_glyph_inner`]
339
        /// can short-circuit the variable-context construction for
340
        /// the common non-variable case. Variable-glyph delta
341
        /// application requires the source bytes to be retained,
342
        /// so it only fires on the `LocaGlyfState::Deferred` path.
343
        pub(crate) is_variable_font: bool,
344
        /// Lazy outline cache. Populated on first
345
        /// [`ParsedFont::get_or_decode_glyph`] call per `gid`; entries
346
        /// are wrapped in `Arc` so callers can hold them without
347
        /// keeping the lock. The space glyph (and `.notdef` when
348
        /// present) are pre-inserted by `from_bytes_internal` so the
349
        /// shaper's cmap-miss path has something to render without
350
        /// racing with a decode.
351
        ///
352
        /// Tests that previously walked the public `glyph_records_decoded`
353
        /// `BTreeMap` field now call
354
        /// [`ParsedFont::prime_glyph_cache`] (decodes every glyph into
355
        /// this cache) followed by
356
        /// [`ParsedFont::for_each_decoded_glyph`] /
357
        /// [`ParsedFont::glyph_cache_snapshot`] to walk the result.
358
        // [az-web-lift] queue RwLock spins in lock_contended in single-threaded lifted wasm
359
        // (only the pure-Rust queue RwLock is lifted; Mutex is Leaf-stubbed). Reuse
360
        // rust_fontconfig::StLock (no-atomic single-threaded bypass). One of the 3 RwLocks total.
361
        pub(crate) glyph_cache: Arc<rust_fontconfig::StLock<BTreeMap<u16, Arc<OwnedGlyph>>>>,
362
        /// Glyph outline decoder state.
363
        ///
364
        /// - `Loaded(Some(arc))`: `LocaGlyf` is already loaded (owning
365
        ///   its own `Box<[u8]>` copy of the loca+glyf tables) and
366
        ///   ready to decode glyphs. Produced by the eager `from_bytes`
367
        ///   constructor path (tests).
368
        /// - `Loaded(None)`: the font has no usable loca+glyf (CFF, or
369
        ///   a parse failure). Glyph outlines won't decode; the hmtx
370
        ///   advance fallback fills in the blanks.
371
        /// - `Deferred`: we retain an `Arc<[u8]>` to the full font file
372
        ///   and the `font_index`; the first `get_or_decode_glyph` call
373
        ///   parses a fresh `FontData` / `TableProvider` from those
374
        ///   bytes and loads `LocaGlyf`, storing the result in the
375
        ///   `OnceLock`. Fonts that get resolved into a chain but are
376
        ///   never actually rasterized pay zero decode cost — this is
377
        ///   the big win for pages like `excel.html` where 20+ fallback
378
        ///   faces load but only a handful are touched.
379
        pub(crate) loca_glyf: LocaGlyfState,
380
        /// Cached width of the space character in font units.
381
        pub space_width: Option<usize>,
382
        /// Character-to-glyph mapping (cmap subtable).
383
        pub cmap_subtable: Option<OwnedCmapSubtable>,
384
        /// Mock font data for testing (replaces real font behavior).
385
        pub mock: Option<Box<MockFont>>,
386
        /// Reverse mapping: glyph_id -> cluster text (handles ligatures like "fi").
387
        pub reverse_glyph_cache: std::collections::BTreeMap<u16, String>,
388
        /// Original font bytes — only retained for callers that need to
389
        /// reconstruct or subset the font (PDF export). Layout / shaping /
390
        /// raster never read this, so `ParsedFont::from_bytes` leaves it
391
        /// as `None` by default and callers opt in via
392
        /// [`ParsedFont::with_source_bytes`]. Shared across faces of the
393
        /// same `.ttc` via the `Arc<FontBytes>` that
394
        /// [`rust_fontconfig::FcFontCache::get_font_bytes`] returns —
395
        /// for disk fonts the backing is an mmap so untouched pages
396
        /// don't count toward RSS.
397
        pub original_bytes: Option<std::sync::Arc<rust_fontconfig::FontBytes>>,
398
        /// Font index within collection (0 for single-font files).
399
        pub original_index: usize,
400
        /// GID to CID mapping for CFF fonts (required for PDF embedding).
401
        pub index_to_cid: BTreeMap<u16, u16>,
402
        /// Font type (TrueType outlines or OpenType CFF).
403
        pub font_type: FontType,
404
        /// PostScript font name from the NAME table.
405
        pub font_name: Option<String>,
406
        /// TrueType bytecode hinting instance (mutable interpreter state).
407
        /// Wrapped in Mutex because hinting mutates internal state.
408
        /// None for CFF fonts or fonts without hinting data.
409
        pub hint_instance: Option<std::sync::Mutex<allsorts::hinting::HintInstance>>,
410
    }
411

            
412
    impl Clone for ParsedFont {
413
        fn clone(&self) -> Self {
414
            ParsedFont {
415
                hash: self.hash,
416
                font_metrics: self.font_metrics.clone(),
417
                pdf_font_metrics: self.pdf_font_metrics,
418
                num_glyphs: self.num_glyphs,
419
                hhea_table: self.hhea_table.clone(),
420
                hmtx_range: self.hmtx_range,
421
                vmtx_range: self.vmtx_range,
422
                vhea_table: self.vhea_table.clone(),
423
                maxp_table: self.maxp_table.clone(),
424
                // OnceLock<T: Clone>: Clone preserves the init state, so
425
                // a clone of a parsed cache skips re-parse on first
426
                // access. The raw bytes we keep around for lazy init
427
                // are cloned too.
428
                gsub_bytes: self.gsub_bytes.clone(),
429
                gsub_cache_lazy: self.gsub_cache_lazy.clone(),
430
                gpos_bytes: self.gpos_bytes.clone(),
431
                gpos_cache_lazy: self.gpos_cache_lazy.clone(),
432
                opt_gdef_table: self.opt_gdef_table.clone(),
433
                opt_kern_table: self.opt_kern_table.clone(),
434
                // Share the lazy cache and loca_glyf across clones: cheap
435
                // Arc bump, amortises glyph decode across clones of the
436
                // same face.
437
                last_used: Arc::clone(&self.last_used),
438
                is_variable_font: self.is_variable_font,
439
                glyph_cache: Arc::clone(&self.glyph_cache),
440
                // `LocaGlyfState` is `Clone` — for `Loaded` this is an
441
                // `Arc::clone`; for `Deferred` it's an `Arc::clone` of
442
                // the bytes + the `OnceLock`, so a clone of a face
443
                // that's already decoded glyphs carries the decode.
444
                loca_glyf: self.loca_glyf.clone(),
445
                space_width: self.space_width,
446
                cmap_subtable: self.cmap_subtable.clone(),
447
                mock: self.mock.clone(),
448
                reverse_glyph_cache: self.reverse_glyph_cache.clone(),
449
                // Arc clone — O(1), just bumps refcount; no byte copy.
450
                original_bytes: self.original_bytes.clone(),
451
                original_index: self.original_index,
452
                index_to_cid: self.index_to_cid.clone(),
453
                font_type: self.font_type.clone(),
454
                font_name: self.font_name.clone(),
455
                // HintInstance has mutable interpreter state and is not Clone.
456
                // Clones are used for PDF/serialization where hinting isn't needed.
457
                hint_instance: None,
458
            }
459
        }
460
    }
461

            
462
    /// Distinguishes TrueType fonts from OpenType CFF fonts.
463
    ///
464
    /// This affects how glyph outlines are extracted and how the font
465
    /// is embedded in PDF documents.
466
    #[derive(Debug, Clone, PartialEq)]
467
    pub enum FontType {
468
        /// TrueType font with quadratic Bézier outlines in glyf table.
469
        TrueType,
470
        /// OpenType font with cubic Bézier outlines in CFF table.
471
        /// Contains the serialized CFF data for PDF embedding.
472
        OpenTypeCFF(Vec<u8>),
473
    }
474

            
475
    /// PDF-specific font metrics from HEAD, HHEA, and OS/2 tables.
476
    ///
477
    /// These metrics are used for PDF font descriptors and accurate
478
    /// text positioning in generated PDF documents.
479
    #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
480
    #[repr(C)]
481
    pub struct PdfFontMetrics {
482
        // -- HEAD table fields --
483
        /// Font units per em-square (typically 1000 or 2048).
484
        pub units_per_em: u16,
485
        /// Font flags (italic, bold, fixed-pitch, etc.).
486
        pub font_flags: u16,
487
        /// Minimum x-coordinate across all glyphs.
488
        pub x_min: i16,
489
        /// Minimum y-coordinate across all glyphs.
490
        pub y_min: i16,
491
        /// Maximum x-coordinate across all glyphs.
492
        pub x_max: i16,
493
        /// Maximum y-coordinate across all glyphs.
494
        pub y_max: i16,
495

            
496
        // -- HHEA table fields --
497
        /// Typographic ascender (distance above baseline).
498
        pub ascender: i16,
499
        /// Typographic descender (distance below baseline, usually negative).
500
        pub descender: i16,
501
        /// Recommended line gap between lines of text.
502
        pub line_gap: i16,
503
        /// Maximum horizontal advance width across all glyphs.
504
        pub advance_width_max: u16,
505
        /// Caret slope rise for italic angle calculation.
506
        pub caret_slope_rise: i16,
507
        /// Caret slope run for italic angle calculation.
508
        pub caret_slope_run: i16,
509

            
510
        // -- OS/2 table fields (0 if table not present) --
511
        /// Average width of lowercase letters.
512
        pub x_avg_char_width: i16,
513
        /// Visual weight class (100-900, 400=normal, 700=bold).
514
        pub us_weight_class: u16,
515
        /// Visual width class (1-9, 5=normal).
516
        pub us_width_class: u16,
517
        /// Thickness of strikeout stroke in font units.
518
        pub y_strikeout_size: i16,
519
        /// Vertical position of strikeout stroke.
520
        pub y_strikeout_position: i16,
521
    }
522

            
523
    impl Default for PdfFontMetrics {
524
        fn default() -> Self {
525
            PdfFontMetrics::zero()
526
        }
527
    }
528

            
529
    impl PdfFontMetrics {
530
        /// Returns zeroed metrics with `units_per_em` set to 1000 (standard PostScript default)
531
        /// to avoid division-by-zero in scaling calculations.
532
12056
        pub const fn zero() -> Self {
533
12056
            PdfFontMetrics {
534
12056
                units_per_em: 1000,
535
12056
                font_flags: 0,
536
12056
                x_min: 0,
537
12056
                y_min: 0,
538
12056
                x_max: 0,
539
12056
                y_max: 0,
540
12056
                ascender: 0,
541
12056
                descender: 0,
542
12056
                line_gap: 0,
543
12056
                advance_width_max: 0,
544
12056
                caret_slope_rise: 0,
545
12056
                caret_slope_run: 0,
546
12056
                x_avg_char_width: 0,
547
12056
                us_weight_class: 0,
548
12056
                us_width_class: 0,
549
12056
                y_strikeout_size: 0,
550
12056
                y_strikeout_position: 0,
551
12056
            }
552
12056
        }
553
    }
554

            
555
    /// Result of font subsetting operation.
556
    ///
557
    /// Contains the subsetted font bytes and a mapping from original
558
    /// glyph IDs to new glyph IDs in the subset.
559
    #[derive(Debug, Clone)]
560
    pub struct SubsetFont {
561
        /// The subsetted font file bytes (smaller than original).
562
        pub bytes: Vec<u8>,
563
        /// Mapping: original glyph ID -> (new subset glyph ID, source character).
564
        pub glyph_mapping: BTreeMap<u16, (u16, char)>,
565
    }
566

            
567
    impl SubsetFont {
568
        /// Return the changed text so that when rendering with the subset font (instead of the
569
        /// original) the renderer will end up at the same glyph IDs as if we used the original text
570
        /// on the original font
571
        pub fn subset_text(&self, text: &str) -> String {
572
            text.chars()
573
                .filter_map(|c| {
574
                    self.glyph_mapping.values().find_map(|(ngid, ch)| {
575
                        if *ch == c {
576
                            char::from_u32(*ngid as u32)
577
                        } else {
578
                            None
579
                        }
580
                    })
581
                })
582
                .collect()
583
        }
584
    }
585

            
586
    /// Hash-based equality: two fonts are considered equal if their content hash matches.
587
    /// This is a performance optimization — hash collisions are possible but vanishingly
588
    /// unlikely (~1/2^64).
589
    impl PartialEq for ParsedFont {
590
        fn eq(&self, other: &Self) -> bool {
591
            self.hash == other.hash
592
        }
593
    }
594

            
595
    impl Eq for ParsedFont {}
596

            
597
    const FONT_B64_START: &str = "data:font/ttf;base64,";
598

            
599
    impl serde::Serialize for ParsedFont {
600
        fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
601
            use base64::Engine;
602
            let s = format!(
603
                "{FONT_B64_START}{}",
604
                base64::prelude::BASE64_STANDARD.encode(&self.to_bytes(None).unwrap_or_default())
605
            );
606
            s.serialize(serializer)
607
        }
608
    }
609

            
610
    impl<'de> serde::Deserialize<'de> for ParsedFont {
611
        fn deserialize<D: serde::Deserializer<'de>>(
612
            deserializer: D,
613
        ) -> Result<ParsedFont, D::Error> {
614
            use base64::Engine;
615
            let s = String::deserialize(deserializer)?;
616
            let b64 = if s.starts_with(FONT_B64_START) {
617
                let b = &s[FONT_B64_START.len()..];
618
                base64::prelude::BASE64_STANDARD.decode(&b).ok()
619
            } else {
620
                None
621
            };
622

            
623
            let mut warnings = Vec::new();
624
            ParsedFont::from_bytes(&b64.unwrap_or_default(), 0, &mut warnings).ok_or_else(|| {
625
                serde::de::Error::custom(format!("Font deserialization error: {warnings:?}"))
626
            })
627
        }
628
    }
629

            
630
    impl fmt::Debug for ParsedFont {
631
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
632
            f.debug_struct("ParsedFont")
633
                .field("hash", &self.hash)
634
                .field("font_metrics", &self.font_metrics)
635
                .field("num_glyphs", &self.num_glyphs)
636
                .field("hhea_table", &self.hhea_table)
637
                .field(
638
                    "hmtx_range",
639
                    &format_args!("<{} bytes>", self.hmtx_range.1),
640
                )
641
                .field("maxp_table", &self.maxp_table)
642
                .field(
643
                    "glyph_cache",
644
                    &format_args!(
645
                        "{} entries (lazy)",
646
                        self.glyph_cache.read().map(|m| m.len()).unwrap_or(0),
647
                    ),
648
                )
649
                .field("space_width", &self.space_width)
650
                .field("cmap_subtable", &self.cmap_subtable)
651
                .finish()
652
        }
653
    }
654

            
655
    /// Warning or error message generated during font parsing.
656
    #[derive(Debug, Clone, PartialEq, Eq)]
657
    pub struct FontParseWarning {
658
        /// Severity level of this warning.
659
        pub severity: FontParseWarningSeverity,
660
        /// Human-readable description of the issue.
661
        pub message: String,
662
    }
663

            
664
    /// Severity level for font parsing warnings.
665
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
666
    pub enum FontParseWarningSeverity {
667
        /// Informational message (not an error).
668
        Info,
669
        /// Warning that may affect font rendering.
670
        Warning,
671
        /// Error that prevents proper font usage.
672
        Error,
673
    }
674

            
675
    impl FontParseWarning {
676
        /// Creates an info-level message.
677
        pub fn info(message: String) -> Self {
678
            Self {
679
                severity: FontParseWarningSeverity::Info,
680
                message,
681
            }
682
        }
683

            
684
        /// Creates a warning-level message.
685
        pub fn warning(message: String) -> Self {
686
            Self {
687
                severity: FontParseWarningSeverity::Warning,
688
                message,
689
            }
690
        }
691

            
692
        /// Creates an error-level message.
693
        pub fn error(message: String) -> Self {
694
            Self {
695
                severity: FontParseWarningSeverity::Error,
696
                message,
697
            }
698
        }
699
    }
700

            
701
    // WEB-LIFT FIX (2026-06-02): a `FontTableProvider` that scans the sfnt table directory
702
    // by hand from the raw font bytes. allsorts' `OffsetTableFontProvider` produces garbage
703
    // on the remill/web backend: (1) `ReadArray::read_item`'s nested-tuple `TableRecord` read
704
    // returns `table_tag = 0` for EVERY record (proven: tags[7]=0x0000 while the bytes there
705
    // are 0x68656164 'head'); (2) even a hand-rolled scan added to the *allsorts crate* sees a
706
    // bad `self.scope.data()` (the ReadScope fat-pointer mis-lifts through provider
707
    // construction, or allsorts-crate code lifts differently). This provider lives in
708
    // azul-layout — whose identical byte reads PROVABLY work (the `from_provider` probe read
709
    // num_tables=15 from these same `font_bytes`) — and reads the slice directly. KEEP.
710
    #[inline]
711
12056
    fn manual_be16(d: &[u8], o: usize) -> u32 {
712
12056
        ((d[o] as u32) << 8) | (d[o + 1] as u32)
713
12056
    }
714
    #[inline]
715
5437036
    fn manual_be32(d: &[u8], o: usize) -> u32 {
716
5437036
        ((d[o] as u32) << 24)
717
5437036
            | ((d[o + 1] as u32) << 16)
718
5437036
            | ((d[o + 2] as u32) << 8)
719
5437036
            | (d[o + 3] as u32)
720
5437036
    }
721

            
722
    struct ManualTableProvider<'a> {
723
        data: &'a [u8],
724
        dir: usize, // byte offset of the first table record (offset-table base + 12)
725
        num: usize, // number of table records
726
    }
727

            
728
    impl<'a> ManualTableProvider<'a> {
729
12056
        fn new(data: &'a [u8], font_index: usize) -> Option<Self> {
730
12056
            if data.len() < 12 {
731
                return None;
732
12056
            }
733
12056
            let base = if manual_be32(data, 0) == 0x7474_6366 {
734
                // 'ttcf' (TrueType Collection): the font_index'th offset-table offset.
735
                let num_fonts = manual_be32(data, 8) as usize;
736
                if font_index >= num_fonts || 12 + font_index * 4 + 4 > data.len() {
737
                    return None;
738
                }
739
                manual_be32(data, 12 + font_index * 4) as usize
740
            } else {
741
12056
                0 // single font: offset table at the start
742
            };
743
12056
            if base + 12 > data.len() {
744
                return None;
745
12056
            }
746
12056
            Some(ManualTableProvider {
747
12056
                data,
748
12056
                dir: base + 12,
749
12056
                num: manual_be16(data, base + 4) as usize,
750
12056
            })
751
12056
        }
752
    }
753

            
754
    impl allsorts::tables::FontTableProvider for ManualTableProvider<'_> {
755
397804
        fn table_data(
756
397804
            &self,
757
397804
            tag: u32,
758
397804
        ) -> Result<Option<std::borrow::Cow<'_, [u8]>>, allsorts::error::ParseError> {
759
397804
            let mut i = 0;
760
5098368
            while i < self.num {
761
4942036
                let r = self.dir + i * 16;
762
4942036
                if r + 16 > self.data.len() {
763
                    break;
764
4942036
                }
765
4942036
                if manual_be32(self.data, r) == tag {
766
241472
                    let off = manual_be32(self.data, r + 8) as usize;
767
241472
                    let len = manual_be32(self.data, r + 12) as usize;
768
241472
                    return Ok(off
769
241472
                        .checked_add(len)
770
241472
                        .filter(|&e| e <= self.data.len())
771
241472
                        .map(|e| std::borrow::Cow::Borrowed(&self.data[off..e])));
772
4700564
                }
773
4700564
                i += 1;
774
            }
775
156332
            Ok(None)
776
397804
        }
777

            
778
132396
        fn has_table(&self, tag: u32) -> bool {
779
132396
            self.table_data(tag).ok().flatten().is_some()
780
132396
        }
781

            
782
        fn table_tags(&self) -> Option<Vec<u32>> {
783
            // DIAG (REVERT): sentinel 0xFADE as tags[0] proves THIS provider ran; then
784
            // self.num pushes let me see if the usize field survived; then the real reads
785
            // show if self.data (slice field) survived the struct move through generics.
786
            let mut tags = Vec::with_capacity(self.num + 1);
787
            tags.push(0x0000_FADE);
788
            let mut i = 0;
789
            while i < self.num {
790
                let r = self.dir + i * 16;
791
                if r + 4 > self.data.len() {
792
                    break;
793
                }
794
                tags.push(manual_be32(self.data, r));
795
                i += 1;
796
            }
797
            Some(tags)
798
        }
799
    }
800

            
801
    impl allsorts::tables::SfntVersion for ManualTableProvider<'_> {
802
        fn sfnt_version(&self) -> u32 {
803
            let base = self.dir.saturating_sub(12);
804
            if base + 4 <= self.data.len() {
805
                manual_be32(self.data, base)
806
            } else {
807
                0
808
            }
809
        }
810
    }
811

            
812
    impl ParsedFont {
813
        /// Parse a font from bytes using allsorts
814
        ///
815
        /// # Arguments
816
        /// * `font_bytes` - The font file data
817
        /// * `font_index` - Index of the font in a font collection (0 for single fonts)
818
        /// * `warnings` - Optional vector to collect parsing warnings
819
        ///
820
        /// # Returns
821
        /// `Some(ParsedFont)` if parsing succeeds, `None` otherwise
822
        ///
823
        /// Note: Outlines are decoded lazily by `get_or_decode_glyph`;
824
        /// `LocaGlyf::load` runs eagerly here. Use `from_bytes_shared`
825
        /// for the lazy-LocaGlyf production path.
826
44
        pub fn from_bytes(
827
44
            font_bytes: &[u8],
828
44
            font_index: usize,
829
44
            warnings: &mut Vec<FontParseWarning>,
830
44
        ) -> Option<Self> {
831
            // `from_bytes` keeps the eager-LocaGlyf behaviour for the
832
            // small number of callers (mainly tests) that don't have
833
            // an `Arc<[u8]>` to keep alive for the lazy path.
834
44
            let mut font = Self::from_bytes_internal(font_bytes, font_index, warnings, false)?;
835
            // Retain an owned copy of the source bytes so the face can later be
836
            // subset/embedded (PDF export, save->parse roundtrips). Callers pass a
837
            // borrowed slice that may not outlive us, so we own it here. Mirrors
838
            // `from_bytes_shared`, which retains the caller's `Arc<FontBytes>`.
839
44
            if font.original_bytes.is_none() {
840
44
                font.original_bytes = Some(std::sync::Arc::new(
841
44
                    rust_fontconfig::FontBytes::Owned(std::sync::Arc::from(font_bytes.to_vec())),
842
44
                ));
843
44
            }
844
44
            Some(font)
845
44
        }
846

            
847
        /// Shared implementation of `from_bytes` / `from_bytes_shared`.
848
        ///
849
        /// `defer_loca_glyf = true` skips the `LocaGlyf::load` call
850
        /// here so the caller (`from_bytes_shared`) can install a
851
        /// `LocaGlyfState::Deferred` slot that re-parses on first
852
        /// glyph decode. Saves the load-then-drop cycle the previous
853
        /// arrangement paid (`from_bytes_shared` used to call
854
        /// `from_bytes` and immediately replace the loaded LocaGlyf
855
        /// with a Deferred slot, throwing away ~hundreds of KiB of
856
        /// loca+glyf bytes per face for fonts in the chain that get
857
        /// loaded but never rasterized).
858
12056
        fn from_bytes_internal(
859
12056
            font_bytes: &[u8],
860
12056
            font_index: usize,
861
12056
            warnings: &mut Vec<FontParseWarning>,
862
12056
            defer_loca_glyf: bool,
863
12056
        ) -> Option<Self> {
864
            use allsorts::{binary::read::ReadScope, font_data::FontData};
865

            
866
12056
            let scope = ReadScope::new(font_bytes);
867
12056
            let font_file = match scope.read::<FontData<'_>>() {
868
12056
                Ok(ff) => ff,
869
                Err(e) => {
870
                    warnings.push(FontParseWarning::error(format!(
871
                        "Failed to read font data: {}",
872
                        e
873
                    )));
874
                    return None;
875
                }
876
            };
877
            // FIX (2026-06-02): route OpenType fonts through the CONCRETE provider
878
            // (`OffsetTableFontProvider`) instead of `FontData::table_provider`'s
879
            // `Box<dyn FontTableProvider>`. On the lifted/web backend the trait-object
880
            // VTABLE dispatch (allsorts font_data.rs:45 `self.provider.table_data(tag)`)
881
            // mis-lifts: the vtable's fn-pointers are untranslated native addresses, so the
882
            // indirect-call dispatcher routes the dyn call to the WRONG `table_data` impl,
883
            // which returns a `Cow::Owned` garbage buffer → `HeadTable::read` errors → font
884
            // parse returns None → text measures height 0. A concrete provider makes every
885
            // `table_data` a DIRECT (monomorphized) call, which lifts correctly. Woff/Woff2
886
            // keep the dyn path (they're not used on the web backend's embedded TTF).
887
            fn provider_err(font_index: usize, e: impl core::fmt::Display) -> FontParseWarning {
888
                FontParseWarning::error(format!(
889
                    "Failed to get table provider for font index {}: {}",
890
                    font_index, e
891
                ))
892
            }
893
12056
            match font_file {
894
12056
                FontData::OpenType(otf) => {
895
                    // Prefer the hand-rolled provider (reads font_bytes directly) over
896
                    // allsorts' OffsetTableFontProvider, whose lifted table reads are garbage
897
                    // on the web backend. Fall back to allsorts only if the manual layout
898
                    // parse can't recognise the sfnt (e.g. an unusual TTC).
899
12056
                    if let Some(mp) = ManualTableProvider::new(font_bytes, font_index) {
900
12056
                        Self::from_provider(mp, font_bytes, font_index, warnings, defer_loca_glyf)
901
                    } else {
902
                        match otf.table_provider(font_index) {
903
                            Ok(p) => Self::from_provider(
904
                                p,
905
                                font_bytes,
906
                                font_index,
907
                                warnings,
908
                                defer_loca_glyf,
909
                            ),
910
                            Err(e) => {
911
                                warnings.push(provider_err(font_index, e));
912
                                None
913
                            }
914
                        }
915
                    }
916
                }
917
                other => match other.table_provider(font_index) {
918
                    Ok(p) => {
919
                        Self::from_provider(p, font_bytes, font_index, warnings, defer_loca_glyf)
920
                    }
921
                    Err(e) => {
922
                        warnings.push(provider_err(font_index, e));
923
                        None
924
                    }
925
                },
926
            }
927
12056
        }
928

            
929
        /// Build a `ParsedFont` from a concrete [`FontTableProvider`]. Split out of
930
        /// `from_bytes_internal` (2026-06-02) so OpenType fonts use the concrete
931
        /// `OffsetTableFontProvider` (direct `table_data` calls that lift correctly on
932
        /// the web backend) rather than `FontData::table_provider`'s `Box<dyn>`, whose
933
        /// trait-object vtable dispatch mis-lifts (wrong impl → Owned garbage → parse fail).
934
12056
        fn from_provider<P: allsorts::tables::FontTableProvider>(
935
12056
            provider: P,
936
12056
            font_bytes: &[u8],
937
12056
            font_index: usize,
938
12056
            warnings: &mut Vec<FontParseWarning>,
939
12056
            defer_loca_glyf: bool,
940
12056
        ) -> Option<Self> {
941
            use std::{
942
                collections::hash_map::DefaultHasher,
943
                hash::{Hash, Hasher},
944
            };
945

            
946
            use allsorts::{
947
                binary::read::ReadScope,
948
                tables::{
949
                    cmap::{owned::CmapSubtable as OwnedCmapSubtable, CmapSubtable},
950
                    FontTableProvider, HeadTable, HheaTable, MaxpTable,
951
                },
952
                tag,
953
            };
954

            
955
            // Extract font name from NAME table early (before provider is moved).
956
            // WEB-LIFT FIX (2026-06-02): NameTable::string_for_id decodes the NAME strings via
957
            // `encoding_rs` (Mac Roman / UTF-16 charset state machines), whose jump-tables
958
            // are NOT devirt'd by the remill lift → MISSING_BLOCK trap (proven: trap in
959
            // encoding_rs::Decoder::decode_to_utf8). font_name is OPTIONAL metadata (NOT used
960
            // for layout/metrics/shaping — those are binary head/hhea/maxp/cmap/glyf), so skip
961
            // the NAME-string decode on the web backend to avoid encoding_rs entirely.
962
            #[cfg(feature = "web_lift")]
963
            let font_name: Option<String> = None;
964
            #[cfg(not(feature = "web_lift"))]
965
12056
            let font_name = provider.table_data(tag::NAME).ok().and_then(|name_data| {
966
12056
                ReadScope::new(&name_data?)
967
12056
                    .read::<allsorts::tables::NameTable>()
968
12056
                    .ok()
969
12056
                    .and_then(|name_table| {
970
12056
                        name_table.string_for_id(allsorts::tables::NameTable::POSTSCRIPT_NAME)
971
12056
                    })
972
12056
            });
973

            
974
            // DIAG (2026-06-02, REVERT): pinpoint the web font-parse-fails root — does HEAD
975
            // fail because table_data can't find/return the table (directory mis-lift) or
976
            // because HeadTable::read errors (table-read mis-lift)? Surfaced via warnings.
977
12056
            let head_table = match provider.table_data(tag::HEAD) {
978
12056
                Ok(Some(head_cow)) => {
979
                    // DIAG: is the HEAD table data CORRECT (magicNumber 0x5F0F3CF5 @ off 12 →
980
                    // HeadTable::read mis-lifts) or WRONG bytes (directory offset mis-lift)?
981
12056
                    let bb = head_cow.as_ref();
982
12056
                    let magic = if bb.len() >= 16 {
983
12056
                        ((bb[12] as u32) << 24) | ((bb[13] as u32) << 16)
984
12056
                            | ((bb[14] as u32) << 8) | (bb[15] as u32)
985
                    } else { 0 };
986
12056
                    match ReadScope::new(&head_cow).read::<HeadTable>() {
987
12056
                        Ok(h) => h,
988
                        Err(_) => {
989
                            // DIAG: surface the sliced offset (how wrong) as hex — "HO" + 8 hex
990
                            // of (head_cow.ptr - font_bytes.ptr). garbage→offset-read mis-lift;
991
                            // 00000000→base; plausible-but-wrong→record mapping. "RF"=bytes-OK.
992
                            let m = if magic == 0x5F0F3CF5 {
993
                                "RF000000".to_string()
994
                            } else {
995
                                let off = (head_cow.as_ref().as_ptr() as usize)
996
                                    .wrapping_sub(font_bytes.as_ptr() as usize);
997
                                let mut msg = String::new();
998
                                // B=Borrowed(slice of font_bytes, ptr-arith/base mis-lift) vs
999
                                // O=Owned(decompressed/copied Vec — wrong path for plain TTF).
                                msg.push(if matches!(head_cow, std::borrow::Cow::Borrowed(_)) { 'B' } else { 'O' });
                                msg.push_str("HO");
                                let mut sh: i32 = 28;
                                while sh >= 0 {
                                    let d = ((off >> sh) & 0xf) as u8;
                                    msg.push((if d < 10 { b'0' + d } else { b'a' + d - 10 }) as char);
                                    sh -= 4;
                                }
                                msg
                            };
                            warnings.push(FontParseWarning::error(m));
                            return None;
                        }
                    }
                }
                Ok(None) => {
                    // DIAG (REVERT): bytes+len+read_item-count+dir all proved OK (N0fr0fc0fg1)
                    // yet find_table_record(HEAD)=None though 'head' is rec[7] on disk. So
                    // either read_item's table_tag FIELD is garbage, or tag::HEAD mis-lifts, or
                    // the u32 == mis-lifts. t7 = tags[7] (should be 0x68656164 'head' low16
                    // =6164); H = tag::HEAD low16 (should be 6164); f = ANY tag==HEAD via an
                    // indexed compare loop (NOT .iter().any). "T<4h t7>H<4h HEAD>f<0|1>".
                    //   T6164 H6164 f1 → values+compare OK (won't reach here — HEAD found)
                    //   T6164 H6164 f0 → the u32 == comparison mis-lifts
                    //   T!=6164        → read_item table_tag FIELD garbage (tuple read mis-lift)
                    //   H!=6164        → tag::HEAD const mis-lifts
                    fn hx(m: &mut String, val: u32, nibbles: i32) {
                        let mut sh = (nibbles - 1) * 4;
                        while sh >= 0 {
                            let d = ((val >> sh) & 0xf) as u8;
                            m.push((if d < 10 { b'0' + d } else { b'a' + d - 10 }) as char);
                            sh -= 4;
                        }
                    }
                    // DECISIVE: tags[8] (head, file off 124) reads 0 but tags[2] (off 28) is OK.
                    // Read the SAME offsets from from_provider's LOCAL font_bytes param (proven
                    // correct at off 4/12). If local@124 = 0x6865 'he' but provider tags[8]=0 ⇒
                    // STORED-SLICE issue (provider self.data fat-ptr mis-lifts) → read locally.
                    // If local@124 = 0 ⇒ the font CONST is only PARTIALLY MIRRORED into the wasm
                    // (deep data-mirror gap) → table data is simply absent. local@93596 (=0x16f9c,
                    // head TABLE data start) further maps the mirror: 'he'/nonzero vs 0.
                    let loc124 = if font_bytes.len() >= 126 {
                        ((font_bytes[124] as u32) << 8) | (font_bytes[125] as u32)
                    } else {
                        0xEEEE
                    };
                    let loc_head = if font_bytes.len() >= 93598 {
                        ((font_bytes[93596] as u32) << 8) | (font_bytes[93597] as u32)
                    } else {
                        0xEEEE
                    };
                    let mut m = String::from("L"); // local font_bytes[124..126] (head dir record):
                    hx(&mut m, loc124 & 0xffff, 4); // 6865 'he' = mirrored; 0000 = not
                    m.push('H'); // local font_bytes[93596..] (head TABLE data, deep):
                    hx(&mut m, loc_head & 0xffff, 4);
                    warnings.push(FontParseWarning::error(m));
                    return None;
                }
                Err(_) => {
                    warnings.push(FontParseWarning::error("HEAD_DATAERR".to_string()));
                    return None;
                }
            };
12056
            let maxp_table = provider
12056
                .table_data(tag::MAXP)
12056
                .ok()
12056
                .and_then(|maxp_data| ReadScope::new(&maxp_data?).read::<MaxpTable>().ok())
12056
                .unwrap_or(MaxpTable {
12056
                    num_glyphs: 0,
12056
                    version1_sub_table: None,
12056
                });
12056
            let num_glyphs = maxp_table.num_glyphs as usize;
            // Compute byte offset+length into font_bytes for hmtx/vmtx
            // instead of copying the table data. The provider returns a
            // borrowed slice for OpenType fonts, so we can derive the
            // offset via pointer arithmetic.
12056
            let hmtx_range = provider
12056
                .table_data(tag::HMTX)
12056
                .ok()
12056
                .and_then(|cow_opt| {
12056
                    let cow = cow_opt?;
12056
                    match cow {
12056
                        std::borrow::Cow::Borrowed(slice) => {
12056
                            let base = font_bytes.as_ptr() as usize;
12056
                            let ptr = slice.as_ptr() as usize;
12056
                            let offset = ptr.checked_sub(base)?;
12056
                            if offset + slice.len() <= font_bytes.len() {
12056
                                Some((offset, slice.len()))
                            } else {
                                None
                            }
                        }
                        std::borrow::Cow::Owned(_) => None,
                    }
12056
                })
12056
                .unwrap_or((0, 0));
12056
            let vmtx_range = provider
12056
                .table_data(tag::VMTX)
12056
                .ok()
12056
                .and_then(|s| {
12056
                    let slice = s?;
220
                    let base = font_bytes.as_ptr() as usize;
220
                    let ptr = slice.as_ptr() as usize;
220
                    let offset = ptr.checked_sub(base)?;
220
                    if offset + slice.len() <= font_bytes.len() {
220
                        Some((offset, slice.len()))
                    } else {
                        None
                    }
12056
                })
12056
                .unwrap_or((0, 0));
            // Parse vhea table (same format as hhea, used for vertical metrics)
12056
            let vhea_table = provider
12056
                .table_data(tag::VHEA)
12056
                .ok()
12056
                .and_then(|vhea_data| ReadScope::new(&vhea_data?).read::<HheaTable>().ok());
            // hhea is required per the OpenType spec; return None if missing
12056
            let hhea_table = provider
12056
                .table_data(tag::HHEA)
12056
                .ok()
12056
                .and_then(|hhea_data| ReadScope::new(&hhea_data?).read::<HheaTable>().ok())?;
            // Build layout-specific font metrics
12056
            let font_metrics = LayoutFontMetrics {
12056
                units_per_em: if head_table.units_per_em == 0 {
                    1000
                } else {
12056
                    head_table.units_per_em
                },
12056
                ascent: hhea_table.ascender as f32,
12056
                descent: hhea_table.descender as f32,
12056
                line_gap: hhea_table.line_gap as f32,
12056
                x_height: None, // will be populated from OS/2 table via from_font_metrics if available
12056
                cap_height: None,
            };
            // Build PDF-specific font metrics
12056
            let pdf_font_metrics =
12056
                Self::parse_pdf_font_metrics(font_bytes, font_index, &head_table, &hhea_table);
            // Use allsorts LocaGlyf for on-demand outline extraction. We
            // *load* LocaGlyf eagerly (it owns ~tens of KiB of loca +
            // ~hundreds of KiB of glyf bytes) but we *don't* decode any
            // glyph outlines up front — that's the big RSS win. Glyphs
            // are decoded by `ParsedFont::get_or_decode_glyph` on first
            // access from the CPU/GPU rasterizer.
            //
            // When `defer_loca_glyf` is set (production lazy path via
            // `from_bytes_shared`), we skip `LocaGlyf::load` here too —
            // the caller will overwrite the slot with
            // `LocaGlyfState::Deferred` carrying the source bytes
            // `Arc<[u8]>`, and the load happens on the first
            // `get_or_decode_glyph` call. This avoids parsing
            // ~hundreds of KiB per face for fonts that get resolved
            // into a chain but never actually rasterized (typical
            // for fallback fonts in CSS chains).
12056
            let has_glyf = provider.has_table(tag::GLYF) && provider.has_table(tag::LOCA);
            // Cache `has_gvar` before `provider` gets moved into
            // `allsorts::font::Font::new(provider)` further down —
            // it's the cheapest way to detect a variable font and
            // avoids the borrow-after-move that a later
            // `provider.has_table(tag::GVAR)` would incur.
12056
            let has_gvar = provider.has_table(tag::GVAR);
12056
            let loca_glyf_opt: Option<Arc<std::sync::Mutex<LocaGlyf>>> = if has_glyf
11836
                && !defer_loca_glyf
            {
44
                match LocaGlyf::load(&provider) {
44
                    Ok(lg) => Some(Arc::new(std::sync::Mutex::new(lg))),
                    Err(e) => {
                        warnings.push(FontParseWarning::warning(format!(
                            "Failed to load LocaGlyf: {} — falling back to hmtx-only", e
                        )));
                        None
                    }
                }
            } else {
12012
                None
            };
            // Lazy `glyph_cache` starts empty; the space-glyph stub
            // below pre-inserts gid 0 / space so the shaper's
            // cmap-miss fallback has something to render without
            // racing with a decode.
12056
            let mut font_data_impl = allsorts::font::Font::new(provider).ok()?;
            // Create TrueType hinting instance from font tables.
            // [az-web-lift] Skip on the web build. The lifted layout never grid-fits glyphs to a
            // pixel raster (it measures + ships a display list to JS), so hinting is never used.
            // Building it (HintInstance::new) runs the allsorts bytecode Interpreter
            // (Interpreter::new + ::dispatch — a large un-devirt'd opcode jump table the remill
            // lift can't resolve, plus ~700 op_* fns of closure bloat). This is INDEPENDENT of the
            // lift's jump-table devirt: even with a perfect lift, web has no use for hinting, and
            // hinted advances are lower-quality output than the plain scaled advance. Native keeps
            // real hinting unchanged.
            #[cfg(feature = "web_lift")]
            let hint_instance: Option<std::sync::Mutex<allsorts::hinting::HintInstance>> = None;
            #[cfg(not(feature = "web_lift"))]
12056
            let hint_instance = allsorts::hinting::HintInstance::new(
12056
                &font_data_impl.font_table_provider
12056
            ).ok().flatten().map(|h| std::sync::Mutex::new(h));
            // Stash raw GSUB/GPOS bytes for lazy parse. Typical fonts
            // have ~tens of KiB of GSUB + a few-to-tens of KiB of GPOS —
            // dwarfed by glyph outlines — so we keep the bytes around
            // and only spend `LayoutTable::read` + `new_layout_cache`
            // cycles when the shaper actually needs them (via
            // `ParsedFont::gsub` / `::gpos`). For an ASCII run where no
            // substitution / kerning is required, we skip both entirely.
12056
            let gsub_bytes = font_data_impl
12056
                .font_table_provider
12056
                .table_data(tag::GSUB)
12056
                .ok()
12056
                .flatten()
12056
                .map(|c| c.into_owned());
12056
            let gpos_bytes = font_data_impl
12056
                .font_table_provider
12056
                .table_data(tag::GPOS)
12056
                .ok()
12056
                .flatten()
12056
                .map(|c| c.into_owned());
12056
            let opt_gdef_table = font_data_impl.gdef_table().ok().and_then(|o| o);
12056
            let num_glyphs = font_data_impl.num_glyphs();
12056
            let opt_kern_table = font_data_impl
12056
                .kern_table()
12056
                .ok()
12056
                .and_then(|s| Some(s?.to_owned()));
12056
            let cmap_data = font_data_impl.cmap_subtable_data();
12056
            let cmap_subtable = ReadScope::new(cmap_data);
12056
            let cmap_subtable = cmap_subtable
12056
                .read::<CmapSubtable<'_>>()
12056
                .ok()
12056
                .and_then(|s| s.to_owned());
            // Font identity hash — used by `PartialEq` for ParsedFont.
            //
            // Previously we did `font_bytes.hash(&mut hasher)` over
            // the full mmap. That touched every page of the file
            // (a 40 MiB `.ttc` walked byte-for-byte) so the "lazy
            // mmap" ended up *fully resident* the moment we built
            // a `ParsedFont`. Cold RSS jumped ~40 MiB from this
            // single line.
            //
            // The hash doesn't need to be cryptographic — it just
            // has to disambiguate two `ParsedFont`s. `(len, first
            // 4 KiB, last 4 KiB, font_index)` is plenty unique and
            // only faults in the two header / trailer pages, which
            // shaping is going to need anyway.
12056
            let mut hasher = DefaultHasher::new();
12056
            (font_bytes.len() as u64).hash(&mut hasher);
12056
            let head_len = font_bytes.len().min(4096);
12056
            font_bytes[..head_len].hash(&mut hasher);
12056
            let tail_start = font_bytes.len().saturating_sub(4096);
12056
            font_bytes[tail_start..].hash(&mut hasher);
12056
            font_index.hash(&mut hasher);
12056
            let hash = hasher.finish();
12056
            let mut font = ParsedFont {
12056
                hash,
12056
                font_metrics,
12056
                pdf_font_metrics,
12056
                num_glyphs,
12056
                hhea_table,
12056
                hmtx_range,
12056
                vmtx_range,
12056
                vhea_table,
12056
                maxp_table,
12056
                gsub_bytes,
12056
                gsub_cache_lazy: std::sync::OnceLock::new(),
12056
                gpos_bytes,
12056
                gpos_cache_lazy: std::sync::OnceLock::new(),
12056
                opt_gdef_table,
12056
                opt_kern_table,
12056
                cmap_subtable,
12056
                last_used: Arc::new(std::sync::atomic::AtomicU64::new(0)),
12056
                is_variable_font: has_gvar,
12056
                glyph_cache: Arc::new(rust_fontconfig::StLock::new(BTreeMap::new())),
12056
                // Eager path: `from_bytes` loaded LocaGlyf immediately
12056
                // (or set None if the font has no loca+glyf). Lazy
12056
                // callers use `from_bytes_shared` which replaces this
12056
                // with `LocaGlyfState::Deferred` before returning.
12056
                loca_glyf: LocaGlyfState::Loaded(loca_glyf_opt),
12056
                space_width: None,
12056
                mock: None,
12056
                reverse_glyph_cache: BTreeMap::new(),
12056
                // Don't retain the source bytes by default — layout and
12056
                // raster don't need them. PDF subsetting / `to_bytes`
12056
                // callers opt in via `with_source_bytes`.
12056
                original_bytes: None,
12056
                original_index: font_index,
12056
                index_to_cid: BTreeMap::new(), // Will be filled for CFF fonts
12056
                font_type: FontType::TrueType, // Default, will be updated if CFF
12056
                font_name,
12056
                hint_instance,
12056
            };
            // Calculate space width
12056
            let space_width = font.get_space_width_internal();
            // Pre-decode the space glyph straight into the lazy
            // `glyph_cache`. Space typically has no outline, so the
            // decoder's outline visitor returns nothing useful and
            // we'd spin re-decoding it every shape — short-circuit
            // here with a hand-rolled record carrying the hmtx
            // advance.
12056
            let _ = (|| {
12056
                let space_gid = font.lookup_glyph_index(' ' as u32)?;
12056
                if let Ok(cache) = font.glyph_cache.read() {
12056
                    if cache.contains_key(&space_gid) {
                        return None;
12056
                    }
                }
12056
                let space_width_val = space_width?;
12056
                let space_record = OwnedGlyph {
12056
                    bounding_box: OwnedGlyphBoundingBox {
12056
                        max_x: 0,
12056
                        max_y: 0,
12056
                        min_x: 0,
12056
                        min_y: 0,
12056
                    },
12056
                    horz_advance: space_width_val as u16,
12056
                    outline: Vec::new(),
12056
                    phantom_points: None,
12056
                    raw_points: None,
12056
                    raw_on_curve: None,
12056
                    raw_contour_ends: None,
12056
                    instructions: None,
12056
                };
12056
                if let Ok(mut cache) = font.glyph_cache.write() {
12056
                    cache.insert(space_gid, Arc::new(space_record));
12056
                }
12056
                Some(())
            })();
12056
            font.space_width = space_width;
12056
            Some(font)
12056
        }
        /// Attach the source font bytes to this `ParsedFont`, enabling
        /// [`ParsedFont::to_bytes`] and [`ParsedFont::subset`] (both of
        /// which the layout / shaping path never calls).
        ///
        /// Takes an `Arc<FontBytes>` so the same file's bytes can be
        /// shared across every face of a `.ttc` at zero extra cost —
        /// pair with [`rust_fontconfig::FcFontCache::get_font_bytes`].
        /// For ad-hoc PDF callers that have raw heap bytes, wrap them
        /// via `Arc::new(FontBytes::Owned(Arc::from(vec)))`.
        pub fn with_source_bytes(mut self, bytes: std::sync::Arc<rust_fontconfig::FontBytes>) -> Self {
            self.original_bytes = Some(bytes);
            self
        }
        /// Lazy-friendly constructor — identical to
        /// [`ParsedFont::from_bytes`] except that `LocaGlyf` is
        /// **not** loaded during the call. Instead, the supplied
        /// `Arc<[u8]>` is retained and `LocaGlyf::load` runs the first
        /// time [`get_or_decode_glyph`] needs glyph outlines for this
        /// face.
        ///
        /// Fonts that get resolved into a CSS fallback chain but are
        /// never actually rasterized (common on desktop — e.g. every
        /// face of HelveticaNeue.ttc loads, but only one or two are
        /// shaped) then pay zero loca/glyf cost.
        ///
        /// Production callers (the reftest harness, `LayoutWindow`,
        /// `cpurender`) should prefer this constructor. Tests that
        /// inspect `glyph_records_decoded` directly and don't want
        /// a lazy path keep using `from_bytes`.
12012
        pub fn from_bytes_shared(
12012
            bytes: std::sync::Arc<rust_fontconfig::FontBytes>,
12012
            font_index: usize,
12012
            warnings: &mut Vec<FontParseWarning>,
12012
        ) -> Option<Self> {
            // Skip the eager LocaGlyf::load via `defer_loca_glyf=true`
            // — saves the load-then-drop cycle the prior arrangement
            // paid (when this called `from_bytes`, allocated
            // ~hundreds of KiB of loca+glyf bytes, then immediately
            // replaced the slot with `Deferred` and dropped them).
            // `bytes.as_ref()` derefs FontBytes → &[u8] (mmap or owned
            // — same code path).
12012
            let mut font = Self::from_bytes_internal(bytes.as_ref(), font_index, warnings, true)?;
12012
            font.original_bytes = Some(bytes.clone());
12012
            font.loca_glyf = LocaGlyfState::Deferred {
12012
                bytes,
12012
                font_index,
12012
                loaded: Arc::new(std::sync::Mutex::new(None)),
12012
            };
12012
            Some(font)
12012
        }
        /// Resolve the current face's `LocaGlyf`, loading it lazily
        /// on first call when `loca_glyf` is `Deferred`. Returns
        /// `None` when the font has no usable loca+glyf (CFF fonts
        /// or parse failures).
43428
        fn resolve_loca_glyf(&self) -> Option<Arc<std::sync::Mutex<LocaGlyf>>> {
43428
            match &self.loca_glyf {
                LocaGlyfState::Loaded(inner) => inner.clone(),
43428
                LocaGlyfState::Deferred { bytes, font_index, loaded } => {
                    // Fast path: cached LocaGlyf is present.
43428
                    if let Ok(guard) = loaded.lock() {
43428
                        if let Some(arc) = guard.as_ref() {
39028
                            return Some(Arc::clone(arc));
4400
                        }
                    }
4400
                    let _p = crate::probe::Probe::span("resolve_loca_glyf");
                    // Slow path: parse provider + load LocaGlyf without
                    // holding the slot's lock (allsorts can take a
                    // millisecond or two on a fresh load). Re-check
                    // after acquiring the write lock so a parallel
                    // decoder doesn't double-load.
                    use allsorts::{
                        binary::read::ReadScope,
                        font_data::FontData,
                        tables::FontTableProvider,
                    };
4400
                    let scope = ReadScope::new(bytes.as_slice());
4400
                    let font_data = scope.read::<FontData<'_>>().ok()?;
4400
                    let provider = font_data.table_provider(*font_index).ok()?;
                    // Gate on table presence to match the `from_bytes`
                    // has_glyf check; avoids a spurious warning on
                    // CFF fonts that sneak into the Deferred path.
4400
                    if !provider.has_table(tag::GLYF) || !provider.has_table(tag::LOCA) {
                        return None;
4400
                    }
4400
                    let new_arc = LocaGlyf::load(&provider)
4400
                        .ok()
4400
                        .map(|lg| Arc::new(std::sync::Mutex::new(lg)))?;
4400
                    if let Ok(mut guard) = loaded.lock() {
4400
                        if let Some(existing) = guard.as_ref() {
                            return Some(Arc::clone(existing));
4400
                        }
4400
                        *guard = Some(Arc::clone(&new_arc));
                    }
4400
                    Some(new_arc)
                }
            }
43428
        }
        /// Source bytes for PDF subsetting / table extraction.
        ///
        /// Looks in two places:
        /// - `original_bytes` (set by [`ParsedFont::with_source_bytes`]
        ///   for legacy PDF-first construction).
        /// - `LocaGlyfState::Deferred.bytes` (set by
        ///   [`ParsedFont::from_bytes_shared`] — the production lazy
        ///   path, which already retains an `Arc<[u8]>` for the lazy
        ///   loca/glyf loader).
        ///
        /// Returns `None` only for `ParsedFont`s built via the eager
        /// `from_bytes` path without an explicit `with_source_bytes`
        /// call — i.e. unit tests that load a font and don't touch
        /// PDF.
132
        pub fn source_bytes_for_subset(&self) -> Option<std::sync::Arc<rust_fontconfig::FontBytes>> {
132
            if let Some(bytes) = &self.original_bytes {
132
                return Some(std::sync::Arc::clone(bytes));
            }
            if let LocaGlyfState::Deferred { bytes, .. } = &self.loca_glyf {
                return Some(std::sync::Arc::clone(bytes));
            }
            None
132
        }
        /// Read the monotonic-clock nanos timestamp of the most
        /// recent [`get_or_decode_glyph`] call on this face, or `0`
        /// if it's never been touched.
        pub fn last_used_nanos(&self) -> u64 {
            self.last_used.load(std::sync::atomic::Ordering::Relaxed)
        }
        /// Drop the cached `LocaGlyf` for this face if it's
        /// `Deferred`-with-bytes-retained — so the next
        /// [`get_or_decode_glyph`] re-parses from `bytes`. No-op for
        /// `Loaded` faces (no source bytes to fall back to).
        ///
        /// Used by [`crate::text3::cache::FontManager::evict_unused`]
        /// and exposed publicly so embedders can free memory under
        /// pressure on fonts they no longer need to render.
        pub fn evict_loca_glyf(&self) -> bool {
            match &self.loca_glyf {
                LocaGlyfState::Deferred { loaded, .. } => {
                    if let Ok(mut guard) = loaded.lock() {
                        if guard.is_some() {
                            *guard = None;
                            return true;
                        }
                    }
                    false
                }
                LocaGlyfState::Loaded(_) => false,
            }
        }
        /// Fetch the parsed GSUB cache if this font has one, parsing
        /// it from the retained `gsub_bytes` on first access.
        ///
        /// Moved out of the eager `from_bytes` path because most text
        /// runs never trigger GSUB — plain ASCII without ligatures is
        /// handled entirely by the cmap + hmtx fast path. Building
        /// `LayoutCacheData<GSUB>` up front reserved ~0.5–2 MiB per
        /// face just to throw it away on pages that don't shape
        /// complex scripts.
20064
        pub fn gsub(&self) -> Option<&GsubCache> {
20064
            self.gsub_cache_lazy
20064
                .get_or_init(|| {
                    use allsorts::{
                        binary::read::ReadScope,
                        layout::{new_layout_cache, LayoutTable, GSUB},
                    };
4488
                    let bytes = self.gsub_bytes.as_ref()?;
4488
                    ReadScope::new(bytes)
4488
                        .read::<LayoutTable<GSUB>>()
4488
                        .ok()
4488
                        .map(new_layout_cache)
4488
                })
20064
                .as_ref()
20064
        }
        /// Fetch the parsed GPOS cache if this font has one, parsing
        /// it from the retained `gpos_bytes` on first access. See
        /// [`ParsedFont::gsub`] for the motivation.
20064
        pub fn gpos(&self) -> Option<&GposCache> {
20064
            self.gpos_cache_lazy
20064
                .get_or_init(|| {
                    use allsorts::{
                        binary::read::ReadScope,
                        layout::{new_layout_cache, LayoutTable, GPOS},
                    };
4488
                    let bytes = self.gpos_bytes.as_ref()?;
4488
                    ReadScope::new(bytes)
4488
                        .read::<LayoutTable<GPOS>>()
4488
                        .ok()
4488
                        .map(new_layout_cache)
4488
                })
20064
                .as_ref()
20064
        }
        /// Fetch an `OwnedGlyph` for `gid`, decoding it on first access.
        ///
        /// Cached in the `Arc<RwLock<…>>` `glyph_cache` so subsequent
        /// calls (including across clones of this `ParsedFont`) hit the
        /// cache. Returns `None` when `gid >= num_glyphs` or the font
        /// has no loca+glyf and no hmtx entry for the glyph. For CFF
        /// fonts the returned record has an empty outline and an advance
        /// pulled from hmtx — matching the pre-lazy behaviour.
        ///
        /// Called on the rasterizer hot path; performance budget is a
        /// few µs per unique glyph (first hit) and an Arc bump + BTreeMap
        /// lookup (cache hits). The write lock is held only across the
        /// decode, not across the caller's use of the returned Arc.
152636
        pub fn get_or_decode_glyph(&self, gid: u16) -> Option<std::sync::Arc<OwnedGlyph>> {
            use std::sync::Arc;
152636
            if usize::from(gid) >= self.num_glyphs.min(u16::MAX) as usize {
                return None;
152636
            }
            // Bump the LRU timestamp so `FontManager::evict_unused`
            // can tell this face is still in use. Cheap atomic store
            // (Relaxed — eviction reads the same atomic and tolerates
            // a slightly stale value, which only causes "evict, then
            // re-load on next access" — never an incorrect render).
152636
            self.last_used
152636
                .store(monotonic_now_nanos(), std::sync::atomic::Ordering::Relaxed);
            // Fast path: cache hit.
152636
            if let Ok(cache) = self.glyph_cache.read() {
152636
                if let Some(existing) = cache.get(&gid) {
109208
                    return Some(Arc::clone(existing));
43428
                }
            }
            // Miss: decode. We drop the read lock before taking the
            // write lock to avoid deadlock, and we re-check on the way
            // in because another thread may have decoded the same glyph
            // in between.
43428
            let record = self.decode_glyph_inner(gid);
43428
            let arc = Arc::new(record);
43428
            if let Ok(mut cache) = self.glyph_cache.write() {
43428
                cache
43428
                    .entry(gid)
43428
                    .or_insert_with(|| Arc::clone(&arc));
                // If another thread beat us to the insert, return theirs
                // so all callers observe the same Arc.
43428
                if let Some(winner) = cache.get(&gid) {
43428
                    return Some(Arc::clone(winner));
                }
            }
            Some(arc)
152636
        }
        /// Eagerly decode every glyph into the lazy `glyph_cache`,
        /// restoring the pre-lazy "every glyph is materialised at
        /// construction time" behaviour. Used by tests that iterate
        /// or compare against reference tooling, and by embedders
        /// that want a walkable view without driving every shape
        /// through `get_or_decode_glyph`.
        ///
        /// After `prime_glyph_cache`, callers can use
        /// [`ParsedFont::for_each_decoded_glyph`] or
        /// [`ParsedFont::glyph_cache_snapshot`] to observe the
        /// populated cache.
        pub fn prime_glyph_cache(&mut self) {
            let n = self.num_glyphs.min(u16::MAX) as usize;
            for glyph_index in 0..n {
                let gid = glyph_index as u16;
                let _ = self.get_or_decode_glyph(gid);
            }
        }
        /// Walk every entry currently in the lazy `glyph_cache`,
        /// invoking `f(gid, &OwnedGlyph)` for each. Holds a read
        /// lock for the duration; do not call back into the font
        /// from `f`. The cache is populated on demand by
        /// [`ParsedFont::get_or_decode_glyph`] (and bulk-prefilled
        /// by [`ParsedFont::prime_glyph_cache`]).
        pub fn for_each_decoded_glyph<F: FnMut(u16, &OwnedGlyph)>(&self, mut f: F) {
            if let Ok(cache) = self.glyph_cache.read() {
                for (gid, glyph) in cache.iter() {
                    f(*gid, glyph.as_ref());
                }
            }
        }
        /// Snapshot of the currently-decoded glyphs as a
        /// `BTreeMap<u16, Arc<OwnedGlyph>>`. Cheap (clones the
        /// Arcs, not the records). Used by callers that want to
        /// hand the map off across an API boundary; for in-place
        /// iteration prefer [`ParsedFont::for_each_decoded_glyph`].
        pub fn glyph_cache_snapshot(&self) -> BTreeMap<u16, Arc<OwnedGlyph>> {
            self.glyph_cache
                .read()
                .map(|c| c.clone())
                .unwrap_or_default()
        }
        /// Core decode routine: produces one `OwnedGlyph` for `gid` by
        /// locking `loca_glyf` and running allsorts' outline visitor +
        /// raw-simple-glyph extraction. Factored out so both
        /// [`get_or_decode_glyph`] and [`prime_glyph_cache`] share it.
        ///
        /// Always returns an `OwnedGlyph` — if anything in the decode
        /// chain fails, falls back to an empty-outline record with the
        /// `hmtx` advance. This mirrors the pre-lazy behaviour where
        /// every gid ended up in `glyph_records_decoded`.
195272
        fn hmtx_bytes(&self) -> &[u8] {
195272
            let (off, len) = self.hmtx_range;
195272
            if len == 0 { return &[]; }
195272
            self.original_bytes.as_ref()
195272
                .map(|b| &b.as_ref()[off..off+len])
195272
                .unwrap_or(&[])
195272
        }
        fn vmtx_bytes(&self) -> &[u8] {
            let (off, len) = self.vmtx_range;
            if len == 0 { return &[]; }
            self.original_bytes.as_ref()
                .map(|b| &b.as_ref()[off..off+len])
                .unwrap_or(&[])
        }
43428
        fn decode_glyph_inner(&self, gid: u16) -> OwnedGlyph {
43428
            let _p = crate::probe::Probe::span("decode_glyph");
            // [az-web-lift] use get_horizontal_advance (reads hmtx directly on the web build)
            // instead of allsorts::glyph_info::advance, whose lifted ReadArray parse has an
            // un-devirt'd jump table → MISSING_BLOCK → OOB during measure.
43428
            let horz_advance = self.get_horizontal_advance(gid);
43428
            let mut record = OwnedGlyph {
43428
                horz_advance,
43428
                bounding_box: OwnedGlyphBoundingBox {
43428
                    min_x: 0,
43428
                    min_y: 0,
43428
                    max_x: horz_advance as i16,
43428
                    max_y: 0,
43428
                },
43428
                outline: Vec::new(),
43428
                phantom_points: None,
43428
                raw_points: None,
43428
                raw_on_curve: None,
43428
                raw_contour_ends: None,
43428
                instructions: None,
43428
            };
            // Resolve the `LocaGlyf` for this face. For `Loaded` that's
            // a cheap `Arc::clone`; for `Deferred` this is where the
            // actual `LocaGlyf::load` happens on first access, paid once
            // per face that ever decodes a glyph.
43428
            let Some(loca_glyf_arc) = self.resolve_loca_glyf() else {
                // No usable loca+glyf → CFF / OpenType-PostScript font
                // (Noto Sans/Serif CJK and most .otf). Decode the glyph
                // from the `CFF ` table instead; the TrueType-only glyf
                // path below can't see these, which left every CFF glyph
                // blank on the cpurender/headless path (CJK rendered as
                // empty space with the hmtx advance still reserved).
                self.decode_cff_glyph_into(gid, &mut record);
                return record;
            };
43428
            let Ok(mut loca_glyf) = loca_glyf_arc.lock() else {
                return record;
            };
            // Visit the outline. If this is a variable font (gvar
            // table present) AND we still have source bytes (only
            // the `LocaGlyfState::Deferred` path retains them), we
            // re-derive a `VariableGlyfContext` here so default-
            // instance vs designed-instance differences land in
            // the decoded outline. The chained `if let` pattern
            // keeps `provider` and `store` in scope for the
            // visit, which the borrow checker requires (the
            // store's `Cow::Borrowed(&[u8])` tables tie its
            // lifetime to the provider).
            //
            // Eager-`from_bytes` faces (no retained bytes) and
            // non-variable fonts skip the var-context machinery
            // and decode the default instance — same behaviour as
            // before R4.
            // [az-web-lift] The lifted web layout NEVER rasterizes (it measures + positions, then
            // ships a display list to JS) — so glyph OUTLINES + TrueType hinting raw-points are
            // never needed in wasm. Decoding them (allsorts GlyfVisitorContext::visit +
            // GlyphOutlineCollector::into_outlines, whose GlyphOutlineOperation match is a 5-arm
            // jump table the remill lift doesn't devirtualize → MISSING_BLOCK → OOB) crashes the
            // measure pass. Skip BOTH decode passes on the web build; the record keeps its hmtx
            // advance/metrics (set above) which is all text measurement needs.
43428
            if !cfg!(feature = "web_lift") {
43428
            let mut outline_done = false;
43428
            if self.is_variable_font {
                if let LocaGlyfState::Deferred { bytes, .. } = &self.loca_glyf {
                    let scope = allsorts::binary::read::ReadScope::new(bytes);
                    if let Ok(font_data) =
                        scope.read::<allsorts::font_data::FontData<'_>>()
                    {
                        if let Ok(provider) = font_data.table_provider(self.original_index) {
                            if let Ok(store) = VariableGlyfContextStore::read(&provider) {
                                if let Ok(var_ctx) = VariableGlyfContext::new(&store) {
                                    let mut visitor = GlyfVisitorContext::new(
                                        &mut *loca_glyf,
                                        Some(var_ctx),
                                    );
                                    let mut collector = GlyphOutlineCollector::new();
                                    if visitor.visit(gid, None, &mut collector).is_ok() {
                                        record.outline = collector.into_outlines();
                                        let (min_x, min_y, max_x, max_y) =
                                            compute_outline_bbox(&record.outline);
                                        record.bounding_box = OwnedGlyphBoundingBox {
                                            min_x,
                                            min_y,
                                            max_x,
                                            max_y,
                                        };
                                        outline_done = true;
                                    }
                                }
                            }
                        }
                    }
                }
43428
            }
43428
            if !outline_done {
43428
                let mut visitor =
43428
                    GlyfVisitorContext::new(&mut *loca_glyf, None);
43428
                let mut collector = GlyphOutlineCollector::new();
43428
                if visitor.visit(gid, None, &mut collector).is_ok() {
43428
                    record.outline = collector.into_outlines();
43428
                    let (min_x, min_y, max_x, max_y) =
43428
                        compute_outline_bbox(&record.outline);
43428
                    record.bounding_box = OwnedGlyphBoundingBox {
43428
                        min_x,
43428
                        min_y,
43428
                        max_x,
43428
                        max_y,
43428
                    };
43428
                }
            }
            // Second pass: pull raw SimpleGlyph data for TrueType
            // bytecode hinting. LocaGlyf caches the `Arc<Glyph>`
            // internally so this lookup is cheap after the first call.
43428
            if let Ok(glyph_arc) = loca_glyf.glyph(gid) {
43428
                if let allsorts::tables::glyf::Glyph::Simple(sg) = glyph_arc.as_ref() {
43208
                    record.raw_points = Some(
1369148
                        sg.coordinates.iter().map(|(_, pt)| (pt.0, pt.1)).collect(),
                    );
43208
                    record.raw_on_curve = Some(
1369148
                        sg.coordinates.iter().map(|(f, _)| f.is_on_curve()).collect(),
                    );
43208
                    record.raw_contour_ends = Some(sg.end_pts_of_contours.clone());
43208
                    record.instructions = Some(sg.instructions.to_vec());
220
                }
            }
            } // [az-web-lift] end skip glyph outline/hinting decode on web
43428
            record
43428
        }
        /// Decode a single glyph outline from the `CFF ` (OpenType
        /// PostScript) table into `record`. Used for fonts with no `glyf`
        /// table — `decode_glyph_inner`'s TrueType path returns an empty
        /// outline for them, so without this every CFF glyph rasterised as
        /// blank on the CPU renderer. Notably this hit ALL CJK text: the
        /// installed Noto Sans/Serif CJK fonts are CID-keyed CFF. allsorts'
        /// `CFFOutlines` feeds the same `GlyphOutlineCollector` the glyf
        /// path uses and resolves CID-keyed local subrs internally.
        fn decode_cff_glyph_into(&self, gid: u16, record: &mut OwnedGlyph) {
            use allsorts::cff::{outline::CFFOutlines, CFF};
            let Some(ref original) = self.original_bytes else {
                return;
            };
            let bytes: &[u8] = original.as_slice();
            let Ok(font_data) = ReadScope::new(bytes).read::<FontData<'_>>() else {
                return;
            };
            let Ok(provider) = font_data.table_provider(self.original_index) else {
                return;
            };
            let Ok(Some(cff_data)) = provider.table_data(tag::CFF) else {
                return;
            };
            let Ok(cff) = ReadScope::new(&cff_data).read::<CFF<'_>>() else {
                return;
            };
            let mut outlines = CFFOutlines { table: &cff };
            let mut collector = GlyphOutlineCollector::new();
            if outlines.visit(gid, None, &mut collector).is_ok() {
                record.outline = collector.into_outlines();
                let (min_x, min_y, max_x, max_y) = compute_outline_bbox(&record.outline);
                record.bounding_box = OwnedGlyphBoundingBox {
                    min_x,
                    min_y,
                    max_x,
                    max_y,
                };
            }
        }
        /// Parse PDF-specific font metrics from HEAD, HHEA, and OS/2 tables
12056
        fn parse_pdf_font_metrics(
12056
            font_bytes: &[u8],
12056
            font_index: usize,
12056
            head_table: &allsorts::tables::HeadTable,
12056
            hhea_table: &allsorts::tables::HheaTable,
12056
        ) -> PdfFontMetrics {
            use allsorts::{
                binary::read::ReadScope,
                font_data::FontData,
                tables::{os2::Os2, FontTableProvider},
                tag,
            };
12056
            let scope = ReadScope::new(font_bytes);
12056
            let font_file = scope.read::<FontData<'_>>().ok();
12056
            let provider = font_file
12056
                .as_ref()
12056
                .and_then(|ff| ff.table_provider(font_index).ok());
12056
            let os2_table = provider
12056
                .as_ref()
12056
                .and_then(|p| p.table_data(tag::OS_2).ok())
12056
                .and_then(|os2_data| {
12056
                    let data = os2_data?;
12056
                    let scope = ReadScope::new(&data);
12056
                    scope.read_dep::<Os2>(data.len()).ok()
12056
                });
            // Base metrics from HEAD and HHEA (always present)
12056
            let base = PdfFontMetrics {
12056
                units_per_em: head_table.units_per_em,
12056
                font_flags: head_table.flags,
12056
                x_min: head_table.x_min,
12056
                y_min: head_table.y_min,
12056
                x_max: head_table.x_max,
12056
                y_max: head_table.y_max,
12056
                ascender: hhea_table.ascender,
12056
                descender: hhea_table.descender,
12056
                line_gap: hhea_table.line_gap,
12056
                advance_width_max: hhea_table.advance_width_max,
12056
                caret_slope_rise: hhea_table.caret_slope_rise,
12056
                caret_slope_run: hhea_table.caret_slope_run,
12056
                ..PdfFontMetrics::zero()
12056
            };
            // Add OS/2 metrics if available
12056
            os2_table
12056
                .map(|os2| PdfFontMetrics {
12056
                    x_avg_char_width: os2.x_avg_char_width,
12056
                    us_weight_class: os2.us_weight_class,
12056
                    us_width_class: os2.us_width_class,
12056
                    y_strikeout_size: os2.y_strikeout_size,
12056
                    y_strikeout_position: os2.y_strikeout_position,
                    ..base
12056
                })
12056
                .unwrap_or(base)
12056
        }
        /// Returns the width of the space character in font units.
        ///
        /// This is used internally for text layout calculations.
        /// Returns `None` if the font has no space glyph or its width cannot be determined.
12056
        fn get_space_width_internal(&self) -> Option<usize> {
12056
            if let Some(mock) = self.mock.as_ref() {
                return mock.space_width;
12056
            }
12056
            let glyph_index = self.lookup_glyph_index(' ' as u32)?;
            // [az-web-lift] use get_horizontal_advance (direct hmtx on web) instead of
            // allsorts::glyph_info::advance (un-devirt'd jump table → OOB).
12056
            Some(self.get_horizontal_advance(glyph_index) as usize)
12056
        }
        /// Look up the glyph index for a Unicode codepoint
184932
        pub fn lookup_glyph_index(&self, codepoint: u32) -> Option<u16> {
184932
            let cmap = self.cmap_subtable.as_ref()?;
184932
            cmap.map_glyph(codepoint).ok().flatten()
184932
        }
        /// Get the horizontal advance width for a glyph in font units.
        ///
        /// Pulled straight from the `hmtx` table — no glyph-outline
        /// decode. Called once per shaped glyph per layout pass, so
        /// avoiding the lazy decode here is a meaningful win over
        /// routing through `get_or_decode_glyph`.
195272
        pub fn get_horizontal_advance(&self, glyph_index: u16) -> u16 {
195272
            if let Some(mock) = self.mock.as_ref() {
                return mock.glyph_advances.get(&glyph_index).copied().unwrap_or(0);
195272
            }
            // [az-web-lift] Read the hmtx advance DIRECTLY (a plain longHorMetric table lookup)
            // instead of allsorts::glyph_info::advance, whose lifted binary `ReadArray` parse has
            // an un-devirt'd jump table → MISSING_BLOCK → OOB during text measure. Identical result
            // for non-variable fonts (the web fallback font is non-variable); native keeps the
            // allsorts path (variable-font deltas etc.).
            #[cfg(feature = "web_lift")]
            {
                let hmtx = self.hmtx_bytes();
                let num = usize::from(self.hhea_table.num_h_metrics);
                if num == 0 {
                    return 0;
                }
                let idx = (glyph_index as usize).min(num - 1);
                let off = idx * 4;
                return if off + 2 <= hmtx.len() {
                    ((hmtx[off] as u16) << 8) | (hmtx[off + 1] as u16)
                } else {
                    0
                };
            }
            #[cfg(not(feature = "web_lift"))]
            {
195272
                allsorts::glyph_info::advance(
195272
                    &self.maxp_table,
195272
                    &self.hhea_table,
195272
                    self.hmtx_bytes(),
195272
                    glyph_index,
                )
195272
                .unwrap_or_default()
            }
195272
        }
        /// Get the hinted advance width in pixels for a glyph at the given ppem.
        ///
        /// For glyphs with outlines, runs TrueType bytecode hinting to get the
        /// grid-fitted advance from phantom points. For glyphs without outlines
        /// (e.g. space), rounds the scaled advance to the pixel grid, matching
        /// FreeType's behavior.
        ///
        /// Returns `None` if hinting is not available or fails.
139788
        pub fn get_hinted_advance_px(&self, glyph_index: u16, ppem: u16) -> Option<f32> {
            // [az-web-lift] No pixel grid-fitting on the web (measure-only): return None so the
            // caller falls back to the plain scaled advance. Hard-cfg (not a runtime `if cfg!`)
            // so the whole hinting body — get_or_decode_glyph's outline path AND set_ppem →
            // allsorts Interpreter::dispatch (opcode jump table → OOB) — is removed from the lift
            // closure entirely. SEPARATE concern from the transpiler's jump-table devirt: web has
            // no use for hinted advances regardless of lift quality. Native is unchanged.
            #[cfg(feature = "web_lift")]
            {
                let _ = (glyph_index, ppem);
                None
            }
            #[cfg(not(feature = "web_lift"))]
            {
139788
            let glyph = self.get_or_decode_glyph(glyph_index)?;
139788
            let upem = self.font_metrics.units_per_em;
139788
            if upem == 0 || ppem == 0 {
                return None;
139788
            }
            // Check if we even have a hint instance
139788
            let _hint_mutex = self.hint_instance.as_ref()?;
            use allsorts::hinting::f26dot6::{compute_scale, F26Dot6};
139788
            let scale = compute_scale(ppem, upem);
139788
            let adv_f26dot6 = F26Dot6::from_funits(glyph.horz_advance as i32, scale);
            // For glyphs with outline data, run bytecode hinting
123112
            if let (Some(raw_points), Some(raw_on_curve), Some(raw_contour_ends)) = (
139788
                glyph.raw_points.as_ref(),
139788
                glyph.raw_on_curve.as_ref(),
139788
                glyph.raw_contour_ends.as_ref(),
            ) {
123112
                let instructions = glyph.instructions.as_deref().unwrap_or(&[]);
123112
                let mut hint = _hint_mutex.lock().ok()?;
123112
                hint.set_ppem(ppem, ppem as f64).ok()?;
123112
                let points_f26dot6: Vec<(i32, i32)> = raw_points
123112
                    .iter()
3866060
                    .map(|&(x, y)| {
3866060
                        let sx = F26Dot6::from_funits(x as i32, scale);
3866060
                        let sy = F26Dot6::from_funits(y as i32, scale);
3866060
                        (sx.to_bits(), sy.to_bits())
3866060
                    })
123112
                    .collect();
                // Use the scaled advance rounded to pixel grid, NOT the hinted
                // phantom point.  Some glyph programs apply ClearType-specific
                // SHPIX adjustments to the advance phantom point that are wrong
                // for non-ClearType rendering.  The rounded scaled advance matches
                // FreeType's DEFAULT mode advance output.
123112
                let rounded = (adv_f26dot6.to_bits() + 32) & !63;
123112
                Some(rounded as f32 / 64.0)
            } else {
                // No outline (e.g. space): use scaled advance, rounded to grid
                // (matching FreeType's phantom point pre-rounding)
16676
                let rounded = (adv_f26dot6.to_bits() + 32) & !63;
16676
                Some(rounded as f32 / 64.0)
            }
            } // [az-web-lift] end #[cfg(not(web_lift))] hinting body
139788
        }
        /// Get the number of glyphs in this font
20064
        pub fn num_glyphs(&self) -> u16 {
20064
            self.num_glyphs
20064
        }
        /// Check if this font has a glyph for the given codepoint
        pub fn has_glyph(&self, codepoint: u32) -> bool {
            self.lookup_glyph_index(codepoint).is_some()
        }
        /// Get vertical metrics for a glyph (for vertical text layout).
        ///
        /// Uses vhea+vmtx tables (same binary format as hhea+hmtx).
        /// Returns None if font has no vertical metrics tables.
139788
        pub fn get_vertical_metrics(
139788
            &self,
139788
            glyph_id: u16,
139788
        ) -> Option<crate::text3::cache::VerticalMetrics> {
139788
            let vhea = self.vhea_table.as_ref()?;
            if self.vmtx_range.1 == 0 {
                return None;
            }
            let vert_advance = allsorts::glyph_info::advance(
                &self.maxp_table, vhea, self.vmtx_bytes(), glyph_id,
            ).ok()? as f32;
            let units_per_em = self.font_metrics.units_per_em as f32;
            let scale = if units_per_em > 0.0 { 1.0 / units_per_em } else { 0.001 };
            // Vertical bearing: approximate from glyph bbox if available
            let (bearing_x, bearing_y) = self.get_or_decode_glyph(glyph_id)
                .map(|g| {
                    let bbox = &g.bounding_box;
                    // tsb (top side bearing): origin_y - max_y
                    // lsb for vertical: center the glyph horizontally
                    let width = (bbox.max_x - bbox.min_x) as f32;
                    (-(width / 2.0) * scale, (vert_advance * scale) - (bbox.max_y as f32 * scale))
                })
                .unwrap_or((0.0, 0.0));
            Some(crate::text3::cache::VerticalMetrics {
                advance: vert_advance * scale,
                bearing_x,
                bearing_y,
                origin_y: self.font_metrics.ascent * scale,
            })
139788
        }
        /// Get layout-specific font metrics
        pub fn get_font_metrics(&self) -> crate::text3::cache::LayoutFontMetrics {
            // Ensure descent is positive (OpenType may have negative descent)
            let descent = if self.font_metrics.descent > 0.0 {
                self.font_metrics.descent
            } else {
                -self.font_metrics.descent
            };
            crate::text3::cache::LayoutFontMetrics {
                ascent: self.font_metrics.ascent,
                descent,
                line_gap: self.font_metrics.line_gap,
                units_per_em: self.font_metrics.units_per_em,
                x_height: self.font_metrics.x_height,
                cap_height: self.font_metrics.cap_height,
            }
        }
        /// Convert the ParsedFont back to bytes using allsorts::whole_font
        /// This reconstructs the entire font from the parsed data
        ///
        /// Source bytes come from either the explicit
        /// [`ParsedFont::with_source_bytes`] handle (PDF-first
        /// construction) *or* the `LocaGlyfState::Deferred` slot
        /// installed by [`ParsedFont::from_bytes_shared`]. The
        /// production lazy path retains bytes for the lazy LocaGlyf
        /// loader, so PDF subsetting Just Works without an extra
        /// `with_source_bytes` call.
        ///
        /// # Arguments
        /// * `tags` - Optional list of specific table tags to include (None = all tables)
        pub fn to_bytes(&self, tags: Option<&[u32]>) -> Result<Vec<u8>, String> {
            let source = self.source_bytes_for_subset().ok_or_else(|| {
                "ParsedFont::to_bytes requires source bytes; construct via \
                 ParsedFont::from_bytes_shared (production lazy path) or \
                 attach via ParsedFont::with_source_bytes"
                    .to_string()
            })?;
            let scope = ReadScope::new(source.as_slice());
            let font_file = scope.read::<FontData<'_>>().map_err(|e| e.to_string())?;
            let provider = font_file
                .table_provider(self.original_index)
                .map_err(|e| e.to_string())?;
            let tags_to_use = tags.unwrap_or(&[
                tag::CMAP,
                tag::HEAD,
                tag::HHEA,
                tag::HMTX,
                tag::MAXP,
                tag::NAME,
                tag::OS_2,
                tag::POST,
                tag::GLYF,
                tag::LOCA,
            ]);
            whole_font(&provider, tags_to_use).map_err(|e| e.to_string())
        }
        /// Create a subset font containing only the specified glyph IDs
        /// Returns the subset font bytes and a mapping from old to new glyph IDs
        ///
        /// # Arguments
        /// * `glyph_ids` - The glyph IDs to include in the subset (glyph 0/.notdef is always
        ///   included)
        /// * `cmap_target` - Target cmap format (Unicode for web, MacRoman for compatibility)
        ///
        /// # Returns
        /// A tuple of (subset_font_bytes, glyph_mapping) where glyph_mapping maps
        /// original_glyph_id -> (new_glyph_id, original_char)
        pub fn subset(
            &self,
            glyph_ids: &[(u16, char)],
            cmap_target: CmapTarget,
        ) -> Result<(Vec<u8>, BTreeMap<u16, (u16, char)>), String> {
            let source = self.source_bytes_for_subset().ok_or_else(|| {
                "ParsedFont::subset requires source bytes; construct via \
                 ParsedFont::from_bytes_shared (production lazy path) or \
                 attach via ParsedFont::with_source_bytes"
                    .to_string()
            })?;
            let scope = ReadScope::new(source.as_slice());
            let font_file = scope.read::<FontData<'_>>().map_err(|e| e.to_string())?;
            let provider = font_file
                .table_provider(self.original_index)
                .map_err(|e| e.to_string())?;
            // Build glyph mapping: original_id -> (new_id, char)
            let glyph_mapping: BTreeMap<u16, (u16, char)> = glyph_ids
                .iter()
                .enumerate()
                .map(|(new_id, &(original_id, ch))| (original_id, (new_id as u16, ch)))
                .collect();
            // Extract just the glyph IDs for subsetting
            let ids: Vec<u16> = glyph_ids.iter().map(|(id, _)| *id).collect();
            // Use PDF profile for embedding fonts in PDFs
            let font_bytes = allsorts_subset(&provider, &ids, &SubsetProfile::Pdf, cmap_target)
                .map_err(|e| format!("Subset error: {:?}", e))?;
            Ok((font_bytes, glyph_mapping))
        }
        /// Get the width of a glyph in font units (internal, unscaled)
        pub fn get_glyph_width_internal(&self, glyph_index: u16) -> Option<usize> {
            allsorts::glyph_info::advance(
                &self.maxp_table,
                &self.hhea_table,
                self.hmtx_bytes(),
                glyph_index,
            )
            .ok()
            .map(|s| s as usize)
        }
        /// Get the width of the space character (unscaled font units)
        #[inline]
        pub const fn get_space_width(&self) -> Option<usize> {
            self.space_width
        }
        /// Add glyph-to-text mapping to reverse cache
        /// This should be called during text shaping when we know both the source text and
        /// resulting glyphs
        pub fn cache_glyph_mapping(&mut self, glyph_id: u16, cluster_text: &str) {
            self.reverse_glyph_cache
                .insert(glyph_id, cluster_text.to_string());
        }
        /// Get the cluster text that produced a specific glyph ID
        /// Returns the original text that was shaped into this glyph (handles ligatures correctly)
        pub fn get_glyph_cluster_text(&self, glyph_id: u16) -> Option<&str> {
            self.reverse_glyph_cache.get(&glyph_id).map(|s| s.as_str())
        }
        /// Get the first character from the cluster text for a glyph ID
        /// This is useful for PDF ToUnicode CMap generation which requires single character
        /// mappings
        pub fn get_glyph_primary_char(&self, glyph_id: u16) -> Option<char> {
            self.reverse_glyph_cache
                .get(&glyph_id)
                .and_then(|text| text.chars().next())
        }
        /// Clear the reverse glyph cache (useful for memory management)
        pub fn clear_glyph_cache(&mut self) {
            self.reverse_glyph_cache.clear();
        }
        /// Get the bounding box size of a glyph (unscaled units) - for PDF
        /// Returns (width, height) in font units
        pub fn get_glyph_bbox_size(&self, glyph_index: u16) -> Option<(i32, i32)> {
            let g = self.get_or_decode_glyph(glyph_index)?;
            let glyph_width = g.horz_advance as i32;
            let glyph_height = g.bounding_box.max_y as i32 - g.bounding_box.min_y as i32;
            Some((glyph_width, glyph_height))
        }
    }
    /// Compute the bounding box from collected glyph outlines.
43428
    fn compute_outline_bbox(outlines: &[GlyphOutline]) -> (i16, i16, i16, i16) {
43428
        let mut min_x = i16::MAX;
43428
        let mut min_y = i16::MAX;
43428
        let mut max_x = i16::MIN;
43428
        let mut max_y = i16::MIN;
43428
        let mut has_points = false;
104632
        for outline in outlines {
1185316
            for op in outline.operations.as_slice() {
1185316
                let points: &[(i16, i16)] = match op {
61204
                    GlyphOutlineOperation::MoveTo(m) => &[(m.x, m.y)],
255640
                    GlyphOutlineOperation::LineTo(l) => &[(l.x, l.y)],
807268
                    GlyphOutlineOperation::QuadraticCurveTo(q) => {
                        // Check both control and end point for bbox
807268
                        min_x = min_x.min(q.ctrl_1_x).min(q.end_x);
807268
                        min_y = min_y.min(q.ctrl_1_y).min(q.end_y);
807268
                        max_x = max_x.max(q.ctrl_1_x).max(q.end_x);
807268
                        max_y = max_y.max(q.ctrl_1_y).max(q.end_y);
807268
                        has_points = true;
807268
                        continue;
                    }
                    GlyphOutlineOperation::CubicCurveTo(c) => {
                        min_x = min_x.min(c.ctrl_1_x).min(c.ctrl_2_x).min(c.end_x);
                        min_y = min_y.min(c.ctrl_1_y).min(c.ctrl_2_y).min(c.end_y);
                        max_x = max_x.max(c.ctrl_1_x).max(c.ctrl_2_x).max(c.end_x);
                        max_y = max_y.max(c.ctrl_1_y).max(c.ctrl_2_y).max(c.end_y);
                        has_points = true;
                        continue;
                    }
61204
                    GlyphOutlineOperation::ClosePath => continue,
                };
633688
                for &(x, y) in points {
316844
                    min_x = min_x.min(x);
316844
                    min_y = min_y.min(y);
316844
                    max_x = max_x.max(x);
316844
                    max_y = max_y.max(y);
316844
                    has_points = true;
316844
                }
            }
        }
43428
        if has_points {
43428
            (min_x, min_y, max_x, max_y)
        } else {
            (0, 0, 0, 0)
        }
43428
    }
    #[derive(Debug, Clone)]
    pub struct OwnedGlyph {
        pub bounding_box: OwnedGlyphBoundingBox,
        pub horz_advance: u16,
        pub outline: Vec<GlyphOutline>,
        pub phantom_points: Option<[Point; 4]>,
        /// Raw TrueType points in font units (for hinting). None for composite/CFF glyphs.
        pub raw_points: Option<Vec<(i16, i16)>>,
        /// On-curve flags for each raw point.
        pub raw_on_curve: Option<Vec<bool>>,
        /// Contour end-point indices (TrueType).
        pub raw_contour_ends: Option<Vec<u16>>,
        /// Per-glyph TrueType hinting instructions.
        pub instructions: Option<Vec<u8>>,
    }
    // --- ParsedFontTrait Implementation for ParsedFont ---
    impl crate::text3::cache::ShallowClone for ParsedFont {
        fn shallow_clone(&self) -> Self {
            self.clone() // ParsedFont::clone uses Arc internally, so it's shallow
        }
    }
    impl crate::text3::cache::ParsedFontTrait for ParsedFont {
        fn shape_text(
            &self,
            text: &str,
            script: crate::font_traits::Script,
            language: crate::font_traits::Language,
            direction: crate::font_traits::BidiDirection,
            style: &crate::font_traits::StyleProperties,
        ) -> Result<Vec<crate::font_traits::Glyph>, crate::font_traits::LayoutError> {
            // Call the existing shape_text_for_parsed_font method (defined in default.rs)
            crate::text3::default::shape_text_for_parsed_font(
                self, text, script, language, direction, style,
            )
        }
        fn get_hash(&self) -> u64 {
            self.hash
        }
        fn get_glyph_size(
            &self,
            glyph_id: u16,
            font_size_px: f32,
        ) -> Option<azul_core::geom::LogicalSize> {
            self.get_or_decode_glyph(glyph_id).map(|record| {
                let units_per_em = self.font_metrics.units_per_em as f32;
                let scale_factor = if units_per_em > 0.0 {
                    font_size_px / units_per_em
                } else {
                    0.01
                };
                let bbox = &record.bounding_box;
                azul_core::geom::LogicalSize {
                    width: (bbox.max_x - bbox.min_x) as f32 * scale_factor,
                    height: (bbox.max_y - bbox.min_y) as f32 * scale_factor,
                }
            })
        }
        fn get_hyphen_glyph_and_advance(&self, font_size: f32) -> Option<(u16, f32)> {
            let glyph_id = self.lookup_glyph_index('-' as u32)?;
            let advance_units = self.get_horizontal_advance(glyph_id);
            let scale_factor = if self.font_metrics.units_per_em > 0 {
                font_size / (self.font_metrics.units_per_em as f32)
            } else {
                return None;
            };
            let scaled_advance = advance_units as f32 * scale_factor;
            Some((glyph_id, scaled_advance))
        }
        fn get_kashida_glyph_and_advance(&self, font_size: f32) -> Option<(u16, f32)> {
            let glyph_id = self.lookup_glyph_index('\u{0640}' as u32)?;
            let advance_units = self.get_horizontal_advance(glyph_id);
            let scale_factor = if self.font_metrics.units_per_em > 0 {
                font_size / (self.font_metrics.units_per_em as f32)
            } else {
                return None;
            };
            let scaled_advance = advance_units as f32 * scale_factor;
            Some((glyph_id, scaled_advance))
        }
        fn has_glyph(&self, codepoint: u32) -> bool {
            self.lookup_glyph_index(codepoint).is_some()
        }
        fn get_vertical_metrics(
            &self,
            glyph_id: u16,
        ) -> Option<crate::text3::cache::VerticalMetrics> {
            self.get_vertical_metrics(glyph_id)
        }
        fn get_font_metrics(&self) -> crate::text3::cache::LayoutFontMetrics {
            self.font_metrics.clone()
        }
        fn num_glyphs(&self) -> u16 {
            self.num_glyphs
        }
        fn get_space_width(&self) -> Option<usize> {
            self.space_width
        }
    }
    /// Build an agg-rust PathStorage from an OwnedGlyph outline (in font units, Y-up → Y-down).
    ///
    /// Returns `None` if the glyph has no outline operations (e.g. space).
    /// The caller is responsible for applying scale and translation transforms.
    #[cfg(feature = "cpurender")]
264
    pub fn build_glyph_path(glyph: &OwnedGlyph) -> Option<agg_rust::path_storage::PathStorage> {
        use agg_rust::{basics::PATH_FLAGS_NONE, path_storage::PathStorage};
264
        let mut path = PathStorage::new();
264
        let mut has_ops = false;
396
        for outline in &glyph.outline {
2244
            for op in outline.operations.as_slice() {
2244
                has_ops = true;
2244
                match op {
132
                    GlyphOutlineOperation::MoveTo(OutlineMoveTo { x, y }) => {
132
                        path.move_to(*x as f64, -(*y as f64));
132
                    }
616
                    GlyphOutlineOperation::LineTo(OutlineLineTo { x, y }) => {
616
                        path.line_to(*x as f64, -(*y as f64));
616
                    }
                    GlyphOutlineOperation::QuadraticCurveTo(OutlineQuadTo {
1364
                        ctrl_1_x, ctrl_1_y, end_x, end_y,
1364
                    }) => {
1364
                        path.curve3(
1364
                            *ctrl_1_x as f64, -(*ctrl_1_y as f64),
1364
                            *end_x as f64, -(*end_y as f64),
1364
                        );
1364
                    }
                    GlyphOutlineOperation::CubicCurveTo(OutlineCubicTo {
                        ctrl_1_x, ctrl_1_y, ctrl_2_x, ctrl_2_y, end_x, end_y,
                    }) => {
                        path.curve4(
                            *ctrl_1_x as f64, -(*ctrl_1_y as f64),
                            *ctrl_2_x as f64, -(*ctrl_2_y as f64),
                            *end_x as f64, -(*end_y as f64),
                        );
                    }
132
                    GlyphOutlineOperation::ClosePath => {
132
                        path.close_polygon(PATH_FLAGS_NONE);
132
                    }
                }
            }
        }
264
        if !has_ops {
220
            return None;
44
        }
44
        Some(path)
264
    }
}