fret-canvas 0.1.0

Canvas and node-graph substrate for interactive Fret tooling.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use fret_core::{SceneOp, TextBlobId, TextConstraints, TextMetrics, TextStyle, UiServices};
use std::cmp::Reverse;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::hash::{Hash, Hasher};
use std::sync::Arc;

use crate::cache::CacheStats;

fn hash_text(text: &str) -> u64 {
    let mut hasher = Fnv1a64::default();
    hasher.write(text.as_bytes());
    hasher.finish()
}

/// A prepared text blob with metrics and a stable cache key.
#[derive(Debug, Clone, Copy)]
pub struct PreparedText {
    pub blob: TextBlobId,
    pub metrics: TextMetrics,
    pub key: u64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct TextCacheKey {
    text_hash: u64,
    text_len: u32,
    text: Arc<str>,
    font: fret_core::FontId,
    size_bits: u32,
    weight: u16,
    slant: u8,
    line_height_bits: Option<u32>,
    letter_spacing_em_bits: Option<u32>,
    scale_factor_bits: u32,
    max_width_bits: Option<u32>,
    wrap: fret_core::TextWrap,
    overflow: fret_core::TextOverflow,
}

impl Hash for TextCacheKey {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.text_hash.hash(state);
        self.text_len.hash(state);
        self.font.hash(state);
        self.size_bits.hash(state);
        self.weight.hash(state);
        self.slant.hash(state);
        self.line_height_bits.hash(state);
        self.letter_spacing_em_bits.hash(state);
        self.scale_factor_bits.hash(state);
        self.max_width_bits.hash(state);
        self.wrap.hash(state);
        self.overflow.hash(state);
    }
}

impl TextCacheKey {
    #[cfg(test)]
    fn new(text: &str, style: &TextStyle, constraints: TextConstraints) -> Self {
        Self::new_arc(Arc::<str>::from(text), style, constraints)
    }

    fn new_arc(text: Arc<str>, style: &TextStyle, constraints: TextConstraints) -> Self {
        Self {
            text_hash: hash_text(text.as_ref()),
            text_len: text.len() as u32,
            text,
            font: style.font.clone(),
            size_bits: style.size.0.to_bits(),
            weight: style.weight.0,
            slant: style.slant as u8,
            line_height_bits: style.line_height.map(|v| v.0.to_bits()),
            letter_spacing_em_bits: style.letter_spacing_em.map(f32::to_bits),
            scale_factor_bits: constraints.scale_factor.to_bits(),
            max_width_bits: constraints.max_width.map(|v| v.0.to_bits()),
            wrap: constraints.wrap,
            overflow: constraints.overflow,
        }
    }

    fn stable_key(&self) -> u64 {
        let mut hasher = Fnv1a64::default();
        self.hash(&mut hasher);
        hasher.finish()
    }
}

#[derive(Debug, Clone)]
struct TextCacheEntry {
    prepared: PreparedText,
    last_used_frame: u64,
}

/// A small keyed cache for prepared text blobs.
///
/// The cache owns the `TextBlobId`s and must be cleared (or dropped) with access to `UiServices`
/// so resources can be released deterministically.
#[derive(Debug, Default)]
pub struct TextCache {
    frame: u64,
    entries: HashMap<TextCacheKey, TextCacheEntry>,
    blob_by_id: HashMap<TextBlobId, TextCacheKey>,
    stats: CacheStats,
}

impl TextCache {
    /// Increments and returns the internal frame counter used for optional pruning.
    ///
    /// Callers may ignore this entirely; the cache remains correct without pruning.
    pub fn begin_frame(&mut self) -> u64 {
        self.frame = self.frame.wrapping_add(1);
        self.frame
    }

    pub fn stats(&self) -> CacheStats {
        self.stats
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn reset_stats(&mut self) {
        self.stats = CacheStats::default();
    }

    /// Releases all cached blobs.
    pub fn clear(&mut self, services: &mut dyn UiServices) {
        self.stats.clear_calls = self.stats.clear_calls.saturating_add(1);
        for t in self.entries.values() {
            services.text().release(t.prepared.blob);
            self.stats.release_clear = self.stats.release_clear.saturating_add(1);
        }
        self.entries.clear();
        self.blob_by_id.clear();
    }

    /// Touch an existing prepared text blob so it is not pruned.
    pub fn touch_blob(&mut self, blob: TextBlobId) -> bool {
        let Some(key) = self.blob_by_id.get(&blob).cloned() else {
            return false;
        };
        let Some(entry) = self.entries.get_mut(&key) else {
            self.blob_by_id.remove(&blob);
            return false;
        };
        entry.last_used_frame = self.frame;
        true
    }

    /// Touch any text blobs referenced by `SceneOp::Text` so they are not pruned.
    pub fn touch_blobs_in_scene_ops(&mut self, ops: &[SceneOp]) -> u32 {
        let mut touched: u32 = 0;
        for op in ops {
            let SceneOp::Text { text, .. } = *op else {
                continue;
            };
            if self.touch_blob(text) {
                touched = touched.saturating_add(1);
            }
        }
        touched
    }

    /// Returns a cached prepared text blob for `(text, style, constraints)` if present.
    ///
    /// This updates the entry's `last_used_frame` for pruning purposes.
    pub fn get_arc(
        &mut self,
        text: Arc<str>,
        style: &TextStyle,
        constraints: TextConstraints,
    ) -> Option<PreparedText> {
        self.stats.get_calls = self.stats.get_calls.saturating_add(1);
        let key = TextCacheKey::new_arc(text, style, constraints);
        let entry = match self.entries.get_mut(&key) {
            Some(entry) => entry,
            None => {
                self.stats.get_misses = self.stats.get_misses.saturating_add(1);
                return None;
            }
        };
        entry.last_used_frame = self.frame;
        self.stats.get_hits = self.stats.get_hits.saturating_add(1);
        Some(entry.prepared)
    }

    /// Convenience wrapper around [`Self::get_arc`] for `&str`.
    pub fn get(
        &mut self,
        text: &str,
        style: &TextStyle,
        constraints: TextConstraints,
    ) -> Option<PreparedText> {
        self.get_arc(Arc::<str>::from(text), style, constraints)
    }

    /// Prepares text and caches it by a stable key derived from `(text, style, constraints)`.
    ///
    /// Note: callers that apply additional view-zoom scaling should incorporate that into
    /// `constraints.scale_factor` (e.g. `dpi * zoom`).
    pub fn prepare_arc(
        &mut self,
        services: &mut dyn UiServices,
        text: Arc<str>,
        style: &TextStyle,
        constraints: TextConstraints,
    ) -> PreparedText {
        self.stats.prepare_calls = self.stats.prepare_calls.saturating_add(1);
        let key = TextCacheKey::new_arc(text, style, constraints);
        match self.entries.entry(key) {
            Entry::Occupied(mut e) => {
                e.get_mut().last_used_frame = self.frame;
                self.stats.prepare_hits = self.stats.prepare_hits.saturating_add(1);
                e.get().prepared
            }
            Entry::Vacant(e) => {
                let (blob, metrics) =
                    services
                        .text()
                        .prepare_str(e.key().text.as_ref(), style, constraints);
                let prepared = PreparedText {
                    blob,
                    metrics,
                    key: e.key().stable_key(),
                };
                self.blob_by_id.insert(blob, e.key().clone());
                e.insert(TextCacheEntry {
                    prepared,
                    last_used_frame: self.frame,
                });
                self.stats.prepare_misses = self.stats.prepare_misses.saturating_add(1);
                prepared
            }
        }
    }

    /// Prepares text and caches it by a stable key derived from `(text, style, constraints)`.
    ///
    /// Note: callers that apply additional view-zoom scaling should incorporate that into
    /// `constraints.scale_factor` (e.g. `dpi * zoom`).
    pub fn prepare(
        &mut self,
        services: &mut dyn UiServices,
        text: &str,
        style: &TextStyle,
        constraints: TextConstraints,
    ) -> PreparedText {
        self.prepare_arc(services, Arc::<str>::from(text), style, constraints)
    }

    /// Drops old cache entries and releases their blobs.
    ///
    /// This is intentionally simple and conservative: it is an optional hygiene helper for long-lived
    /// canvases (plots/editors) to avoid unbounded growth.
    pub fn prune(
        &mut self,
        services: &mut dyn UiServices,
        max_age_frames: u64,
        max_entries: usize,
    ) {
        self.stats.prune_calls = self.stats.prune_calls.saturating_add(1);
        let now = self.frame;

        let mut removed_blobs: Vec<TextBlobId> = Vec::new();
        self.entries.retain(|_, entry| {
            let keep = now.saturating_sub(entry.last_used_frame) <= max_age_frames;
            if !keep {
                removed_blobs.push(entry.prepared.blob);
                services.text().release(entry.prepared.blob);
                self.stats.release_prune_age = self.stats.release_prune_age.saturating_add(1);
            }
            keep
        });
        for blob in removed_blobs {
            self.blob_by_id.remove(&blob);
        }

        if self.entries.len() <= max_entries {
            return;
        }

        let mut keys: Vec<(Reverse<u64>, TextCacheKey)> = self
            .entries
            .iter()
            .map(|(k, v)| (Reverse(v.last_used_frame), k.clone()))
            .collect();
        keys.sort_by(|a, b| a.0.cmp(&b.0));

        for (_, key) in keys.into_iter().skip(max_entries) {
            if let Some(entry) = self.entries.remove(&key) {
                services.text().release(entry.prepared.blob);
                self.stats.release_prune_budget = self.stats.release_prune_budget.saturating_add(1);
                self.blob_by_id.remove(&entry.prepared.blob);
            }
        }
    }
}

#[derive(Debug, Default)]
struct Fnv1a64(u64);

impl Hasher for Fnv1a64 {
    fn write(&mut self, bytes: &[u8]) {
        if self.0 == 0 {
            self.0 = 0xcbf29ce484222325;
        }
        let mut state = self.0;
        for b in bytes {
            state ^= u64::from(*b);
            state = state.wrapping_mul(0x100000001b3);
        }
        self.0 = state;
    }

    fn finish(&self) -> u64 {
        if self.0 == 0 {
            0xcbf29ce484222325
        } else {
            self.0
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fret_core::{
        PathCommand, PathConstraints, PathId, PathMetrics, PathService, PathStyle, Px, Size, SvgId,
        SvgService, TextBlobId, TextConstraints, TextInput, TextLineHeightPolicy, TextMetrics,
        TextOverflow, TextService, TextWrap,
    };

    #[test]
    fn key_includes_line_height() {
        let style_a = TextStyle {
            line_height: None,
            ..TextStyle::default()
        };
        let style_b = TextStyle {
            line_height: Some(Px(22.0)),
            line_height_policy: TextLineHeightPolicy::FixedFromStyle,
            ..TextStyle::default()
        };

        let k_a = TextCacheKey::new("hello", &style_a, TextConstraints::default()).stable_key();
        let k_b = TextCacheKey::new("hello", &style_b, TextConstraints::default()).stable_key();
        assert_ne!(k_a, k_b);
    }

    #[test]
    fn key_includes_letter_spacing() {
        let style_a = TextStyle {
            letter_spacing_em: None,
            ..TextStyle::default()
        };
        let style_b = TextStyle {
            letter_spacing_em: Some(0.05),
            ..TextStyle::default()
        };

        let k_a = TextCacheKey::new("hello", &style_a, TextConstraints::default()).stable_key();
        let k_b = TextCacheKey::new("hello", &style_b, TextConstraints::default()).stable_key();
        assert_ne!(k_a, k_b);
    }

    #[test]
    fn key_includes_constraints() {
        let a = TextConstraints {
            scale_factor: 1.0,
            wrap: TextWrap::Word,
            overflow: TextOverflow::Clip,
            ..Default::default()
        };

        let mut b = a;
        b.scale_factor = 2.0;

        let k_a = TextCacheKey::new("hello", &TextStyle::default(), a).stable_key();
        let k_b = TextCacheKey::new("hello", &TextStyle::default(), b).stable_key();
        assert_ne!(k_a, k_b);
    }

    #[test]
    fn key_is_collision_safe_on_equal_hash() {
        // This is intentionally not trying to manufacture a real FNV collision (impractical here).
        // The invariant we care about: even if a hash collides, `TextCacheKey` equality compares
        // the full text, so correctness does not depend on the stable key alone.
        let a = TextCacheKey::new("hello", &TextStyle::default(), TextConstraints::default());
        let b = TextCacheKey::new("hello!", &TextStyle::default(), TextConstraints::default());
        assert_ne!(a, b);
    }

    #[derive(Default)]
    struct FakeServices {
        text_prepare_calls: u64,
    }

    impl TextService for FakeServices {
        fn prepare(
            &mut self,
            _input: &TextInput,
            _constraints: TextConstraints,
        ) -> (TextBlobId, TextMetrics) {
            self.text_prepare_calls += 1;
            (
                TextBlobId::default(),
                TextMetrics {
                    size: Size::default(),
                    baseline: Px(0.0),
                },
            )
        }

        fn release(&mut self, _blob: TextBlobId) {}
    }

    impl PathService for FakeServices {
        fn prepare(
            &mut self,
            _commands: &[PathCommand],
            _style: PathStyle,
            _constraints: PathConstraints,
        ) -> (PathId, PathMetrics) {
            (PathId::default(), PathMetrics::default())
        }

        fn release(&mut self, _path: PathId) {}
    }

    impl SvgService for FakeServices {
        fn register_svg(&mut self, _bytes: &[u8]) -> SvgId {
            SvgId::default()
        }

        fn unregister_svg(&mut self, _svg: SvgId) -> bool {
            true
        }
    }

    impl fret_core::MaterialService for FakeServices {
        fn register_material(
            &mut self,
            _desc: fret_core::MaterialDescriptor,
        ) -> Result<fret_core::MaterialId, fret_core::MaterialRegistrationError> {
            Err(fret_core::MaterialRegistrationError::Unsupported)
        }

        fn unregister_material(&mut self, _id: fret_core::MaterialId) -> bool {
            true
        }
    }

    #[test]
    fn stats_count_prepare_hits_and_misses() {
        let mut cache = TextCache::default();
        let mut services = FakeServices::default();
        cache.begin_frame();

        let style = TextStyle::default();
        let constraints = TextConstraints::default();

        let _ = cache.prepare(&mut services, "hello", &style, constraints);
        let _ = cache.prepare(&mut services, "hello", &style, constraints);

        assert_eq!(services.text_prepare_calls, 1);
        assert_eq!(cache.stats().prepare_misses, 1);
        assert_eq!(cache.stats().prepare_hits, 1);
    }

    #[derive(Default)]
    struct TouchServices {
        text_prepare_calls: u64,
        text_release_calls: u64,
    }

    impl TextService for TouchServices {
        fn prepare(
            &mut self,
            _input: &TextInput,
            _constraints: TextConstraints,
        ) -> (TextBlobId, TextMetrics) {
            self.text_prepare_calls += 1;
            (
                TextBlobId::default(),
                TextMetrics {
                    size: Size::default(),
                    baseline: Px(0.0),
                },
            )
        }

        fn release(&mut self, _blob: TextBlobId) {
            self.text_release_calls += 1;
        }
    }

    impl PathService for TouchServices {
        fn prepare(
            &mut self,
            _commands: &[PathCommand],
            _style: PathStyle,
            _constraints: PathConstraints,
        ) -> (PathId, PathMetrics) {
            (PathId::default(), PathMetrics::default())
        }

        fn release(&mut self, _path: PathId) {}
    }

    impl SvgService for TouchServices {
        fn register_svg(&mut self, _bytes: &[u8]) -> SvgId {
            SvgId::default()
        }

        fn unregister_svg(&mut self, _svg: SvgId) -> bool {
            true
        }
    }

    impl fret_core::MaterialService for TouchServices {
        fn register_material(
            &mut self,
            _desc: fret_core::MaterialDescriptor,
        ) -> Result<fret_core::MaterialId, fret_core::MaterialRegistrationError> {
            Err(fret_core::MaterialRegistrationError::Unsupported)
        }

        fn unregister_material(&mut self, _id: fret_core::MaterialId) -> bool {
            true
        }
    }

    #[test]
    fn touch_blob_prevents_prune_age_release() {
        let mut cache = TextCache::default();
        let mut services = TouchServices::default();

        cache.begin_frame(); // frame 1
        let style = TextStyle::default();
        let constraints = TextConstraints::default();
        let prepared = cache.prepare(&mut services, "hello", &style, constraints);
        assert_eq!(services.text_prepare_calls, 1);

        cache.begin_frame(); // frame 2
        assert!(cache.touch_blob(prepared.blob));
        cache.prune(&mut services, 0, 10);
        assert_eq!(services.text_release_calls, 0);
    }

    #[test]
    fn touch_blobs_in_scene_ops_prevents_prune_age_release() {
        let mut cache = TextCache::default();
        let mut services = TouchServices::default();

        cache.begin_frame(); // frame 1
        let style = TextStyle::default();
        let constraints = TextConstraints::default();
        let prepared = cache.prepare(&mut services, "hello", &style, constraints);

        let ops = [fret_core::SceneOp::Text {
            order: fret_core::DrawOrder(0),
            origin: fret_core::Point::new(Px(0.0), Px(0.0)),
            text: prepared.blob,
            paint: fret_core::Color::TRANSPARENT.into(),
            outline: None,
            shadow: None,
        }];

        cache.begin_frame(); // frame 2
        let touched = cache.touch_blobs_in_scene_ops(&ops);
        assert_eq!(touched, 1);
        cache.prune(&mut services, 0, 10);
        assert_eq!(services.text_release_calls, 0);
    }
}