1
//! Glyph path and cell cache for CPU rendering.
2
//!
3
//! Two-level cache:
4
//! 1. **Path cache**: `PathStorage` objects keyed by (font, glyph, ppem).
5
//!    Avoids redundant path construction from font outlines.
6
//! 2. **Cell cache**: Rasterizer cells keyed by (font, glyph, ppem, scale, sub-pixel).
7
//!    Avoids the expensive path→cells conversion on every frame.
8
//!    Cells are computed at position (0,0) and offset at render time.
9

            
10
use std::collections::HashMap;
11

            
12
use agg_rust::path_storage::PathStorage;
13
use agg_rust::rasterizer_cells_aa::CellAa;
14

            
15
use crate::font::parsed::{build_glyph_path, OwnedGlyph, ParsedFont};
16

            
17
/// Cache key for a glyph path.
18
/// ppem = 0 means unhinted (font-unit path), ppem > 0 means hinted at that size.
19
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20
pub struct GlyphPathKey {
21
    pub font_hash: u64,
22
    pub glyph_id: u16,
23
    pub ppem: u16,
24
}
25

            
26
/// Cache key for pre-rasterized glyph cells.
27
/// Includes sub-pixel x/y fractional position quantized to 1/4 pixel.
28
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
29
pub struct GlyphCellKey {
30
    pub font_hash: u64,
31
    pub glyph_id: u16,
32
    pub ppem: u16,
33
    /// Scale factor encoded as fixed-point (scale * 65536) for unhinted glyphs.
34
    /// 0 for hinted glyphs (already in pixel coords).
35
    pub scale_fixed: u32,
36
    /// Sub-pixel x position quantized to 1/4 pixel (0..3).
37
    pub subpx_x: u8,
38
    /// Sub-pixel y position quantized to 1/4 pixel (0..3).
39
    pub subpx_y: u8,
40
}
41

            
42
/// Result of a cache lookup: the path plus whether it's hinted (pixel coords) or not.
43
pub struct CachedGlyph<'a> {
44
    pub path: &'a PathStorage,
45
    pub is_hinted: bool,
46
}
47

            
48
/// Pre-rasterized glyph cells at a canonical position.
49
/// Contains the rasterizer's cell output for a glyph at sub-pixel position (subpx_x, subpx_y).
50
/// To render at actual position (x, y), add integer pixel offset to each cell.
51
pub struct CachedCells {
52
    pub cells: Vec<CellAa>,
53
}
54

            
55
/// Maximum number of glyph path entries before eviction.
56
/// ~8K glyphs covers most Latin + CJK pages without unbounded growth.
57
const MAX_PATH_ENTRIES: usize = 8192;
58
/// Maximum number of cell cache entries before eviction.
59
/// Cell entries are larger than paths, so a lower limit is appropriate.
60
const MAX_CELL_ENTRIES: usize = 16384;
61

            
62
/// Cache of built glyph paths and pre-rasterized cells.
63
pub struct GlyphCache {
64
    paths: HashMap<GlyphPathKey, Option<(PathStorage, bool)>>,
65
    cells: HashMap<GlyphCellKey, Option<CachedCells>>,
66
}
67

            
68
/// Quantize a fractional pixel position to 1/4 pixel (0..3).
69
#[inline]
70
630
fn quantize_subpx(frac: f32) -> u8 {
71
630
    let f = frac - frac.floor();
72
630
    (f * 4.0).min(3.0) as u8
73
630
}
74

            
75
impl GlyphCache {
76
    #[must_use]
77
875
    pub fn new() -> Self {
78
875
        Self {
79
875
            paths: HashMap::new(),
80
875
            cells: HashMap::new(),
81
875
        }
82
875
    }
83

            
84
    /// Entry count of the glyph-path cache (for leak probes).
85
    pub fn paths_len(&self) -> usize { self.paths.len() }
86

            
87
    /// Entry count of the pre-rasterized cell cache (for leak probes).
88
    pub fn cells_len(&self) -> usize { self.cells.len() }
89

            
90
    /// Get a cached path, or build it on cache miss.
91
    /// Returns `None` if the glyph has no outline (e.g. space character).
92
6265
    pub fn get_or_build(
93
6265
        &mut self,
94
6265
        font_hash: u64,
95
6265
        glyph_id: u16,
96
6265
        glyph_data: &OwnedGlyph,
97
6265
        parsed_font: &ParsedFont,
98
6265
        ppem: u16,
99
6265
    ) -> Option<CachedGlyph<'_>> {
100
6265
        if self.paths.len() >= MAX_PATH_ENTRIES {
101
            self.paths.clear();
102
6265
        }
103
6265
        let key = GlyphPathKey { font_hash, glyph_id, ppem };
104
6265
        let entry = self
105
6265
            .paths
106
6265
            .entry(key)
107
6265
            .or_insert_with(|| {
108
                // Try hinted path first if ppem > 0
109
2450
                if ppem > 0 {
110
2450
                    if let Some(path) = build_hinted_path(glyph_data, parsed_font, ppem) {
111
2345
                        return Some((path, true));
112
105
                    }
113
                }
114
                // Fall back to unhinted path
115
105
                build_glyph_path(glyph_data).map(|p| (p, false))
116
2450
            });
117
6265
        entry.as_ref().map(|(path, is_hinted)| CachedGlyph {
118
5950
            path,
119
5950
            is_hinted: *is_hinted,
120
5950
        })
121
6265
    }
122

            
123
    /// Get cached rasterizer cells for a glyph, or build them from the path.
124
    ///
125
    /// - `glyph_x`, `glyph_y`: final pixel position (used for sub-pixel quantization)
126
    /// - `scale`: font-unit→pixel scale (0.0 for hinted glyphs)
127
    /// - `is_hinted`: whether the path is in pixel coords (hinted) or font units
128
    ///
129
    /// Returns the cached cells and the integer pixel offset to apply.
130
6265
    pub fn get_or_build_cells(
131
6265
        &mut self,
132
6265
        font_hash: u64,
133
6265
        glyph_id: u16,
134
6265
        ppem: u16,
135
6265
        glyph_x: f32,
136
6265
        glyph_y: f32,
137
6265
        scale: f32,
138
6265
        is_hinted: bool,
139
6265
    ) -> Option<(&[CellAa], i32, i32)> {
140
6265
        if self.cells.len() >= MAX_CELL_ENTRIES {
141
            self.cells.clear();
142
6265
        }
143
6265
        let subpx_x = if is_hinted { 0 } else { quantize_subpx(glyph_x) };
144
6265
        let subpx_y = if is_hinted { 0 } else { quantize_subpx(glyph_y) };
145
6265
        debug_assert!(scale >= 0.0 && scale < 65536.0, "scale out of range for fixed-point: {}", scale);
146
6265
        let scale_fixed = if is_hinted { 0 } else { (scale * 65536.0) as u32 };
147

            
148
6265
        let cell_key = GlyphCellKey {
149
6265
            font_hash, glyph_id, ppem, scale_fixed, subpx_x, subpx_y,
150
6265
        };
151

            
152
        // Integer pixel offset — the cells are at sub-pixel origin, offset by int part
153
6265
        let int_x = if is_hinted { glyph_x.round() as i32 } else { glyph_x.floor() as i32 };
154
6265
        let int_y = if is_hinted { glyph_y.round() as i32 } else { glyph_y.floor() as i32 };
155

            
156
6265
        if !self.cells.contains_key(&cell_key) {
157
            // Build cells from cached path
158
2450
            let path_key = GlyphPathKey { font_hash, glyph_id, ppem };
159
2450
            let path_entry = self.paths.get(&path_key);
160
2450
            let cached_cells = path_entry.and_then(|entry| {
161
2450
                let (path, _) = entry.as_ref()?;
162
2345
                let frac_x = (subpx_x as f64) * 0.25;
163
2345
                let frac_y = (subpx_y as f64) * 0.25;
164

            
165
                use agg_rust::trans_affine::TransAffine;
166
                use agg_rust::basics::FillingRule;
167
                use agg_rust::rasterizer_scanline_aa::RasterizerScanlineAa;
168

            
169
2345
                let mut ras = RasterizerScanlineAa::new();
170
2345
                ras.filling_rule(FillingRule::NonZero);
171

            
172
2345
                let transform = if is_hinted {
173
2345
                    TransAffine::new_translation(frac_x, frac_y)
174
                } else {
175
                    let mut t = TransAffine::new_scaling_uniform(scale as f64);
176
                    t.multiply(&TransAffine::new_translation(frac_x, frac_y));
177
                    t
178
                };
179

            
180
2345
                let verts = path.vertices();
181
2345
                ras.add_path_vertices_transformed(verts, &transform);
182
2345
                let cells = ras.outline_cells();
183
2345
                if cells.is_empty() { None } else { Some(CachedCells { cells }) }
184
2450
            });
185
2450
            self.cells.insert(cell_key, cached_cells);
186
3815
        }
187

            
188
6265
        let entry = self.cells.get(&cell_key)?;
189
6265
        entry.as_ref().map(|cc| (cc.cells.as_slice(), int_x, int_y))
190
6265
    }
191

            
192
    /// Evict all cached paths and cells.
193
    pub fn clear(&mut self) {
194
        self.paths.clear();
195
        self.cells.clear();
196
    }
197

            
198
    /// Evict caches if they exceed size limits.
199
    /// Called automatically by get_or_build / get_or_build_cells, but can
200
    /// also be called manually between frames to enforce bounds.
201
    pub fn evict_if_needed(&mut self) {
202
        if self.paths.len() >= MAX_PATH_ENTRIES {
203
            self.paths.clear();
204
        }
205
        if self.cells.len() >= MAX_CELL_ENTRIES {
206
            self.cells.clear();
207
        }
208
    }
209

            
210
    /// Returns `true` if the path cache is empty.
211
    pub fn is_empty(&self) -> bool {
212
        self.paths.is_empty()
213
    }
214

            
215
    /// Number of cached path entries.
216
    pub fn len(&self) -> usize {
217
        self.paths.len()
218
    }
219

            
220
    /// Number of cached cell entries.
221
    pub fn cell_cache_len(&self) -> usize {
222
        self.cells.len()
223
    }
224
}
225

            
226
/// Build a hinted glyph path using TrueType bytecode hinting.
227
///
228
/// The returned path is in pixel coordinates (1 unit = 1 pixel at the given ppem).
229
/// Returns `None` if the glyph has no raw hinting data or hinting fails.
230
2450
fn build_hinted_path(
231
2450
    glyph: &OwnedGlyph,
232
2450
    parsed_font: &ParsedFont,
233
2450
    ppem: u16,
234
2450
) -> Option<PathStorage> {
235
2450
    let raw_points = glyph.raw_points.as_ref()?;
236
2345
    let raw_on_curve = glyph.raw_on_curve.as_ref()?;
237
2345
    let raw_contour_ends = glyph.raw_contour_ends.as_ref()?;
238
2345
    let instructions = glyph.instructions.as_ref()?;
239

            
240
2345
    if raw_points.is_empty() || raw_contour_ends.is_empty() {
241
        return None;
242
2345
    }
243

            
244
2345
    let hint_mutex = parsed_font.hint_instance.as_ref()?;
245
2345
    let mut hint = hint_mutex.lock().ok()?;
246

            
247
2345
    let upem = parsed_font.font_metrics.units_per_em;
248
2345
    if upem == 0 {
249
        return None;
250
2345
    }
251

            
252
    // Set up hinting for this ppem (scales CVT, runs prep)
253
2345
    if hint.set_ppem(ppem, ppem as f64).is_err() {
254
        return None;
255
2345
    }
256

            
257
    // Scale raw points from font units to F26Dot6
258
2345
    let scale = allsorts::hinting::f26dot6::compute_scale(ppem, upem);
259
    use allsorts::hinting::f26dot6::F26Dot6;
260

            
261
2345
    let points_f26dot6: Vec<(i32, i32)> = raw_points
262
2345
        .iter()
263
48930
        .map(|&(x, y)| {
264
48930
            let sx = F26Dot6::from_funits(x as i32, scale);
265
48930
            let sy = F26Dot6::from_funits(y as i32, scale);
266
48930
            (sx.to_bits(), sy.to_bits())
267
48930
        })
268
2345
        .collect();
269

            
270
    // Scale advance width to F26Dot6 for phantom points
271
2345
    let adv_f26dot6 = F26Dot6::from_funits(glyph.horz_advance as i32, scale).to_bits();
272

            
273
    // Run hinting with unscaled orus for precise IUP interpolation
274
2345
    let hinted = match hint.hint_glyph_with_orus(
275
2345
        &points_f26dot6,
276
2345
        Some(raw_points.as_slice()),
277
2345
        raw_on_curve,
278
2345
        raw_contour_ends,
279
2345
        instructions,
280
2345
        adv_f26dot6,
281
2345
    ) {
282
2345
        Ok(h) => h,
283
        Err(_) => return None,
284
    };
285

            
286
    // Build path from hinted points using TrueType quadratic contour conventions
287
2345
    build_path_from_contours(&hinted, raw_on_curve, raw_contour_ends)
288
2450
}
289

            
290
/// Build an agg PathStorage from TrueType contour data (points in F26Dot6).
291
///
292
/// Matches allsorts' `visit_simple_glyph_outline` algorithm exactly:
293
/// - On-curve points are endpoints of line/curve segments
294
/// - Off-curve points are quadratic Bézier control points
295
/// - Two consecutive off-curve points have an implicit on-curve midpoint
296
/// - Y is negated for screen coordinates (font Y-up → screen Y-down)
297
/// - The origin point is NOT revisited in the loop; close() handles the final segment
298
2345
pub fn build_path_from_contours(
299
2345
    points: &[(i32, i32)],
300
2345
    on_curve: &[bool],
301
2345
    contour_ends: &[u16],
302
2345
) -> Option<PathStorage> {
303
    use agg_rust::basics::PATH_FLAGS_NONE;
304

            
305
2345
    let mut path = PathStorage::new();
306
2345
    let mut has_ops = false;
307
2345
    let mut contour_start = 0usize;
308

            
309
5845
    for &end_idx in contour_ends {
310
3500
        let end = end_idx as usize;
311
3500
        if end >= points.len() || contour_start > end {
312
            contour_start = end + 1;
313
            continue;
314
3500
        }
315

            
316
3500
        let pts = &points[contour_start..=end];
317
3500
        let flags = &on_curve[contour_start..=end];
318
3500
        let n = pts.len();
319
3500
        if n < 2 {
320
            contour_start = end + 1;
321
            continue;
322
3500
        }
323

            
324
        // Helper: get point as (f64, f64) with Y negated
325
59535
        let px = |i: usize| -> (f64, f64) {
326
59535
            (f26_to_px(pts[i].0) as f64, -f26_to_px(pts[i].1) as f64)
327
59535
        };
328
10605
        let mid = |a: (f64, f64), b: (f64, f64)| -> (f64, f64) {
329
10605
            ((a.0 + b.0) * 0.5, (a.1 + b.1) * 0.5)
330
10605
        };
331

            
332
        // Determine origin and processing range (matching allsorts' calculate_origin)
333
3500
        let (origin, start, until) = if flags[0] {
334
3500
            (px(0), 1usize, n)
335
        } else if flags[n - 1] {
336
            (px(n - 1), 0usize, n - 1)
337
        } else {
338
            (mid(px(0), px(n - 1)), 0usize, n)
339
        };
340

            
341
3500
        path.move_to(origin.0, origin.1);
342
3500
        has_ops = true;
343

            
344
3500
        let mut i = start;
345
39725
        while i < until {
346
36225
            if flags[i] {
347
15015
                // On-curve: line segment
348
15015
                let to = px(i);
349
15015
                path.line_to(to.0, to.1);
350
15015
                i += 1;
351
15015
            } else {
352
                // Off-curve control point
353
21210
                let ctrl = px(i);
354
21210
                let next = i + 1;
355
21210
                if next < until {
356
19810
                    if flags[next] {
357
9205
                        // Next is on-curve: quad to it, consume both
358
9205
                        let to = px(next);
359
9205
                        path.curve3(ctrl.0, ctrl.1, to.0, to.1);
360
9205
                        i = next + 1;
361
10605
                    } else {
362
10605
                        // Next is also off-curve: quad to implicit midpoint
363
10605
                        let m = mid(ctrl, px(next));
364
10605
                        path.curve3(ctrl.0, ctrl.1, m.0, m.1);
365
10605
                        i = next;
366
10605
                    }
367
1400
                } else {
368
1400
                    // End of range: curve back to origin
369
1400
                    path.curve3(ctrl.0, ctrl.1, origin.0, origin.1);
370
1400
                    i = next;
371
1400
                }
372
            }
373
        }
374
3500
        path.close_polygon(PATH_FLAGS_NONE);
375

            
376
3500
        contour_start = end + 1;
377
    }
378

            
379
2345
    if !has_ops {
380
        return None;
381
2345
    }
382
2345
    Some(path)
383
2345
}
384

            
385
/// Convert F26Dot6 value to pixel coordinate (f32).
386
#[inline]
387
119070
fn f26_to_px(v: i32) -> f32 {
388
119070
    v as f32 / 64.0
389
119070
}