ruviz 0.3.6

High-performance 2D plotting library for Rust
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
use crate::{
    core::{PlottingError, Result},
    render::{
        Color,
        text_anchor::{TextAnchorKind, anchor_to_top_left},
    },
};
use tiny_skia::{IntSize, Pixmap};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TypstBackendKind {
    Raster,
    Svg,
}

#[derive(Debug)]
pub struct TypstRasterOutput {
    pub pixmap: Pixmap,
    pub width: f32,
    pub height: f32,
}

#[derive(Debug, Clone)]
pub struct TypstSvgOutput {
    pub svg: String,
    pub width: f32,
    pub height: f32,
}

pub fn literal_text_snippet(text: &str) -> String {
    let escaped = text
        .replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t");
    format!("#text(\"{}\")", escaped)
}

/// Text anchor semantics used when positioning rendered Typst output.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypstTextAnchor {
    /// Horizontal text anchored at the top-left corner.
    TopLeft,
    /// Horizontal text anchored at the top-center point.
    TopCenter,
    /// Text anchored at geometric center (used for rotated/centered content).
    Center,
}

/// Convert anchor coordinates into top-left draw coordinates.
pub fn anchored_top_left(
    anchor_x: f32,
    anchor_y: f32,
    rendered_width: f32,
    rendered_height: f32,
    anchor: TypstTextAnchor,
) -> (f32, f32) {
    let shared_anchor = match anchor {
        TypstTextAnchor::TopLeft => TextAnchorKind::TopLeft,
        TypstTextAnchor::TopCenter => TextAnchorKind::TopCenter,
        TypstTextAnchor::Center => TextAnchorKind::Center,
    };
    anchor_to_top_left(
        anchor_x,
        anchor_y,
        rendered_width,
        rendered_height,
        shared_anchor,
    )
}

#[cfg(not(feature = "typst-math"))]
pub fn render_raster(
    _snippet: &str,
    _size_pt: f32,
    _color: Color,
    _rotation_deg: f32,
    operation: &str,
) -> Result<TypstRasterOutput> {
    Err(PlottingError::FeatureNotEnabled {
        feature: "typst-math".to_string(),
        operation: operation.to_string(),
    })
}

#[cfg(not(feature = "typst-math"))]
pub fn render_svg(
    _snippet: &str,
    _size_pt: f32,
    _color: Color,
    _rotation_deg: f32,
    operation: &str,
) -> Result<TypstSvgOutput> {
    Err(PlottingError::FeatureNotEnabled {
        feature: "typst-math".to_string(),
        operation: operation.to_string(),
    })
}

#[cfg(not(feature = "typst-math"))]
pub fn measure_text(
    _snippet: &str,
    _size_pt: f32,
    _color: Color,
    _rotation_deg: f32,
    _backend: TypstBackendKind,
    operation: &str,
) -> Result<(f32, f32)> {
    Err(PlottingError::FeatureNotEnabled {
        feature: "typst-math".to_string(),
        operation: operation.to_string(),
    })
}

#[cfg(feature = "typst-math")]
mod imp {
    use super::{TypstBackendKind, TypstRasterOutput, TypstSvgOutput};
    use crate::{
        core::{PlottingError, Result},
        render::Color,
    };
    use std::{
        collections::HashMap,
        path::PathBuf,
        sync::{Mutex, MutexGuard, OnceLock},
    };
    use tiny_skia::{IntSize, Pixmap};
    use typst::{
        Library, World, compile,
        diag::FileError,
        foundations::{Bytes, Datetime},
        layout::{Page, PagedDocument},
        syntax::{FileId, Source, VirtualPath},
        text::{Font, FontBook},
        utils::LazyHash,
    };
    use typst_kit::fonts::{FontSearcher, FontSlot};

    const MAX_CACHE_ENTRIES: usize = 256;
    const MAX_CACHE_BYTES: usize = 64 * 1024 * 1024;
    const MAX_RASTER_DIMENSION: u32 = 8_192;
    const MAX_RASTER_BYTES: usize = 128 * 1024 * 1024;

    #[derive(Debug, Clone, PartialEq, Eq, Hash)]
    struct CacheKey {
        snippet: String,
        size_bits: u32,
        color: (u8, u8, u8, u8),
        rotation_bits: u32,
        backend: TypstBackendKind,
    }

    #[derive(Debug, Clone)]
    enum CachedValue {
        Raster {
            pixels: Vec<u8>,
            pixel_width: u32,
            pixel_height: u32,
            logical_width: f32,
            logical_height: f32,
        },
        Svg {
            svg: String,
            width: f32,
            height: f32,
        },
    }

    #[derive(Debug, Default)]
    struct CacheState {
        entries: HashMap<CacheKey, CachedValue>,
        total_bytes: usize,
    }

    #[derive(Debug)]
    struct FontContext {
        book: LazyHash<FontBook>,
        fonts: Vec<FontSlot>,
        sans_family: String,
    }

    #[derive(Debug)]
    struct TypstWorld {
        library: &'static LazyHash<Library>,
        book: &'static LazyHash<FontBook>,
        fonts: &'static [FontSlot],
        main: FileId,
        source: Source,
    }

    impl World for TypstWorld {
        fn library(&self) -> &LazyHash<Library> {
            self.library
        }

        fn book(&self) -> &LazyHash<FontBook> {
            self.book
        }

        fn main(&self) -> FileId {
            self.main
        }

        fn source(&self, id: FileId) -> typst::diag::FileResult<Source> {
            if id == self.main {
                Ok(self.source.clone())
            } else {
                Err(FileError::NotFound(PathBuf::from("<memory>")))
            }
        }

        fn file(&self, _id: FileId) -> typst::diag::FileResult<Bytes> {
            Err(FileError::NotFound(PathBuf::from("<memory>")))
        }

        fn font(&self, index: usize) -> Option<Font> {
            self.fonts.get(index).and_then(FontSlot::get)
        }

        fn today(&self, _offset: Option<i64>) -> Option<Datetime> {
            None
        }
    }

    fn library() -> &'static LazyHash<Library> {
        static LIB: OnceLock<LazyHash<Library>> = OnceLock::new();
        LIB.get_or_init(|| LazyHash::new(Library::default()))
    }

    fn fonts() -> &'static FontContext {
        static FONTS: OnceLock<FontContext> = OnceLock::new();
        FONTS.get_or_init(|| {
            let mut searcher = FontSearcher::new();
            let found = searcher.search();
            let sans_family = select_sans_family(&found.book);
            FontContext {
                book: LazyHash::new(found.book),
                fonts: found.fonts,
                sans_family,
            }
        })
    }

    fn select_sans_family(book: &FontBook) -> String {
        const PREFERRED: &[&str] = &[
            "noto sans",
            "dejavu sans",
            "liberation sans",
            "arial",
            "helvetica",
            "new computer modern sans",
            "latin modern sans",
        ];

        for candidate in PREFERRED {
            if book.contains_family(candidate) {
                return (*candidate).to_string();
            }
        }

        let mut first_family: Option<String> = None;
        for (family, _) in book.families() {
            if first_family.is_none() {
                first_family = Some(family.to_string());
            }
            if family.to_ascii_lowercase().contains("sans") {
                return family.to_string();
            }
        }

        first_family.unwrap_or_else(|| "New Computer Modern Sans".to_string())
    }

    fn escape_typst_string(value: &str) -> String {
        value.replace('\\', "\\\\").replace('"', "\\\"")
    }

    fn cache() -> &'static Mutex<CacheState> {
        static CACHE: OnceLock<Mutex<CacheState>> = OnceLock::new();
        CACHE.get_or_init(|| Mutex::new(CacheState::default()))
    }

    fn lock_cache_resource<'a, T>(
        mutex: &'a Mutex<T>,
        resource_name: &str,
    ) -> Result<MutexGuard<'a, T>> {
        mutex.lock().map_err(|_| {
            PlottingError::TypstError(format!(
                "Typst rendering aborted because {resource_name} lock is poisoned"
            ))
        })
    }

    fn lock_cache() -> Result<MutexGuard<'static, CacheState>> {
        lock_cache_resource(cache(), "Typst cache")
    }

    fn make_key(
        snippet: &str,
        size_pt: f32,
        color: Color,
        rotation_deg: f32,
        backend: TypstBackendKind,
    ) -> CacheKey {
        CacheKey {
            snippet: snippet.to_string(),
            size_bits: size_pt.to_bits(),
            color: (color.r, color.g, color.b, color.a),
            rotation_bits: rotation_deg.to_bits(),
            backend,
        }
    }

    fn cached_value_bytes(value: &CachedValue) -> usize {
        match value {
            CachedValue::Raster { pixels, .. } => pixels.len(),
            CachedValue::Svg { svg, .. } => svg.len(),
        }
    }

    fn maybe_evict(cache: &mut CacheState, incoming_bytes: usize) {
        // Evict in arbitrary HashMap iteration order; for this small local render
        // cache that trade-off is acceptable. A single near-limit item can drain
        // the cache to make room for itself.
        while cache.entries.len() >= MAX_CACHE_ENTRIES
            || (!cache.entries.is_empty()
                && cache.total_bytes.saturating_add(incoming_bytes) > MAX_CACHE_BYTES)
        {
            if let Some(first_key) = cache.entries.keys().next().cloned() {
                if let Some(removed) = cache.entries.remove(&first_key) {
                    cache.total_bytes = cache
                        .total_bytes
                        .saturating_sub(cached_value_bytes(&removed));
                }
            } else {
                break;
            }
        }
    }

    fn remove_cached_value(cache: &mut CacheState, key: &CacheKey) {
        if let Some(previous) = cache.entries.remove(key) {
            cache.total_bytes = cache
                .total_bytes
                .saturating_sub(cached_value_bytes(&previous));
        }
    }

    fn insert_cached_value(cache: &mut CacheState, key: CacheKey, value: CachedValue) {
        let incoming_bytes = cached_value_bytes(&value);
        if incoming_bytes > MAX_CACHE_BYTES {
            remove_cached_value(cache, &key);
            return;
        }

        remove_cached_value(cache, &key);

        maybe_evict(cache, incoming_bytes);
        cache.total_bytes = cache.total_bytes.saturating_add(incoming_bytes);
        cache.entries.insert(key, value);
    }

    fn validate_raster_size(width: u32, height: u32, operation: &str) -> Result<()> {
        if width > MAX_RASTER_DIMENSION || height > MAX_RASTER_DIMENSION {
            return Err(PlottingError::PerformanceLimit {
                limit_type: format!("{operation} raster dimension"),
                actual: width.max(height) as usize,
                maximum: MAX_RASTER_DIMENSION as usize,
            });
        }

        let bytes = (width as usize)
            .checked_mul(height as usize)
            .and_then(|pixels| pixels.checked_mul(4))
            .ok_or_else(|| PlottingError::PerformanceLimit {
                limit_type: format!("{operation} raster bytes"),
                actual: usize::MAX,
                maximum: MAX_RASTER_BYTES,
            })?;

        if bytes > MAX_RASTER_BYTES {
            return Err(PlottingError::PerformanceLimit {
                limit_type: format!("{operation} raster bytes"),
                actual: bytes,
                maximum: MAX_RASTER_BYTES,
            });
        }

        Ok(())
    }

    fn snippet_excerpt(snippet: &str) -> String {
        let compact = snippet.trim().replace('\n', " ");
        let mut chars = compact.chars();
        let excerpt: String = chars.by_ref().take(80).collect();
        if chars.next().is_some() {
            format!("{}...", excerpt)
        } else {
            excerpt
        }
    }

    fn build_document_source(
        snippet: &str,
        size_pt: f32,
        color: Color,
        rotation_deg: f32,
        font_family: &str,
    ) -> String {
        let size_pt = size_pt.max(1.0);
        let font_family = escape_typst_string(font_family);
        if rotation_deg.abs() > f32::EPSILON {
            format!(
                "#set page(width: auto, height: auto, margin: 0pt, fill: none)\n#set text(font: \"{font_family}\", size: {size_pt}pt, fill: rgb({r}, {g}, {b}, {a}), top-edge: \"ascender\", bottom-edge: \"descender\")\n#rotate({rotation_deg}deg, reflow: true)[{snippet}]",
                r = color.r,
                g = color.g,
                b = color.b,
                a = color.a,
            )
        } else {
            format!(
                "#set page(width: auto, height: auto, margin: 0pt, fill: none)\n#set text(font: \"{font_family}\", size: {size_pt}pt, fill: rgb({r}, {g}, {b}, {a}), top-edge: \"ascender\", bottom-edge: \"descender\")\n{snippet}",
                r = color.r,
                g = color.g,
                b = color.b,
                a = color.a,
            )
        }
    }

    fn compile_single_page(
        snippet: &str,
        size_pt: f32,
        color: Color,
        rotation_deg: f32,
        operation: &str,
    ) -> Result<Page> {
        let font_ctx = fonts();
        let source_text =
            build_document_source(snippet, size_pt, color, rotation_deg, &font_ctx.sans_family);
        let main = FileId::new_fake(VirtualPath::new("/main.typ"));
        let source = Source::new(main, source_text);
        let world = TypstWorld {
            library: library(),
            book: &font_ctx.book,
            fonts: &font_ctx.fonts,
            main,
            source,
        };

        let warned = compile::<PagedDocument>(&world);
        let document = warned.output.map_err(|errors| {
            let details = errors
                .iter()
                .map(|diag| diag.message.as_str())
                .collect::<Vec<_>>()
                .join("; ");
            PlottingError::TypstError(format!(
                "{} failed: {}; snippet=`{}`",
                operation,
                details,
                snippet_excerpt(snippet)
            ))
        })?;

        document.pages.first().cloned().ok_or_else(|| {
            PlottingError::TypstError(format!(
                "{} failed: Typst produced no pages; snippet=`{}`",
                operation,
                snippet_excerpt(snippet)
            ))
        })
    }

    pub fn render_raster(
        snippet: &str,
        size_pt: f32,
        color: Color,
        rotation_deg: f32,
        operation: &str,
    ) -> Result<TypstRasterOutput> {
        if snippet.trim().is_empty() {
            let pixmap = Pixmap::new(1, 1).ok_or_else(|| {
                PlottingError::RenderError("Failed to allocate pixmap".to_string())
            })?;
            return Ok(TypstRasterOutput {
                pixmap,
                width: 0.0,
                height: 0.0,
            });
        }

        let key = make_key(
            snippet,
            size_pt,
            color,
            rotation_deg,
            TypstBackendKind::Raster,
        );

        {
            let cache = lock_cache()?;
            if let Some(CachedValue::Raster {
                pixels,
                pixel_width,
                pixel_height,
                logical_width,
                logical_height,
            }) = cache.entries.get(&key)
            {
                let size = IntSize::from_wh(*pixel_width, *pixel_height).ok_or_else(|| {
                    PlottingError::RenderError("Invalid cached typst raster size".to_string())
                })?;
                let pixmap = Pixmap::from_vec(pixels.clone(), size).ok_or_else(|| {
                    PlottingError::RenderError(
                        "Failed to create pixmap from cached Typst raster".to_string(),
                    )
                })?;
                return Ok(TypstRasterOutput {
                    pixmap,
                    width: *logical_width,
                    height: *logical_height,
                });
            }
        }

        let page = compile_single_page(snippet, size_pt, color, rotation_deg, operation)?;
        let size = page.frame.size();
        let logical_width = size.x.to_pt() as f32;
        let logical_height = size.y.to_pt() as f32;
        let expected_width = logical_width.ceil().max(1.0) as u32;
        let expected_height = logical_height.ceil().max(1.0) as u32;
        validate_raster_size(expected_width, expected_height, operation)?;

        let rendered_pixmap = typst_render::render(&page, 1.0);
        let pixel_width = rendered_pixmap.width();
        let pixel_height = rendered_pixmap.height();
        validate_raster_size(pixel_width, pixel_height, operation)?;
        let size = IntSize::from_wh(pixel_width, pixel_height).ok_or_else(|| {
            PlottingError::RenderError("Typst raster output has invalid dimensions".to_string())
        })?;
        let pixmap = Pixmap::from_vec(rendered_pixmap.data().to_vec(), size).ok_or_else(|| {
            PlottingError::RenderError("Failed to convert Typst raster output".to_string())
        })?;
        let pixels = pixmap.data().to_vec();
        let pixel_bytes = pixels.len();
        let mut cache = lock_cache()?;
        if pixel_bytes > MAX_CACHE_BYTES {
            remove_cached_value(&mut cache, &key);
        } else {
            insert_cached_value(
                &mut cache,
                key,
                CachedValue::Raster {
                    pixels,
                    pixel_width,
                    pixel_height,
                    logical_width,
                    logical_height,
                },
            );
        }

        Ok(TypstRasterOutput {
            pixmap,
            width: logical_width,
            height: logical_height,
        })
    }

    pub fn render_svg(
        snippet: &str,
        size_pt: f32,
        color: Color,
        rotation_deg: f32,
        operation: &str,
    ) -> Result<TypstSvgOutput> {
        if snippet.trim().is_empty() {
            return Ok(TypstSvgOutput {
                svg: String::new(),
                width: 0.0,
                height: 0.0,
            });
        }

        let key = make_key(snippet, size_pt, color, rotation_deg, TypstBackendKind::Svg);
        {
            let cache = lock_cache()?;
            if let Some(CachedValue::Svg { svg, width, height }) = cache.entries.get(&key) {
                return Ok(TypstSvgOutput {
                    svg: svg.clone(),
                    width: *width,
                    height: *height,
                });
            }
        }

        let page = compile_single_page(snippet, size_pt, color, rotation_deg, operation)?;
        let raw_svg = typst_svg::svg(&page);
        let size = page.frame.size();
        let width = size.x.to_pt() as f32;
        let height = size.y.to_pt() as f32;

        let mut cache = lock_cache()?;
        if raw_svg.len() > MAX_CACHE_BYTES {
            remove_cached_value(&mut cache, &key);
        } else {
            insert_cached_value(
                &mut cache,
                key,
                CachedValue::Svg {
                    svg: raw_svg.clone(),
                    width,
                    height,
                },
            );
        }

        Ok(TypstSvgOutput {
            svg: raw_svg,
            width,
            height,
        })
    }

    pub fn measure_text(
        snippet: &str,
        size_pt: f32,
        color: Color,
        rotation_deg: f32,
        backend: TypstBackendKind,
        operation: &str,
    ) -> Result<(f32, f32)> {
        match backend {
            TypstBackendKind::Raster => {
                let rendered = render_raster(snippet, size_pt, color, rotation_deg, operation)?;
                Ok((rendered.width, rendered.height))
            }
            TypstBackendKind::Svg => {
                let rendered = render_svg(snippet, size_pt, color, rotation_deg, operation)?;
                Ok((rendered.width, rendered.height))
            }
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn poisoned_typst_cache_lock_returns_error() {
            let mutex = Mutex::new(0_u8);
            let _ = std::panic::catch_unwind(|| {
                let _guard = mutex.lock().unwrap();
                panic!("poison typst cache lock");
            });

            let err = lock_cache_resource(&mutex, "test cache").unwrap_err();
            assert!(matches!(err, PlottingError::TypstError(_)));
            assert!(err.to_string().contains("test cache lock is poisoned"));
        }

        #[test]
        fn cache_insert_tracks_bytes_without_rescanning() {
            let mut cache = CacheState::default();
            let key = make_key(
                "#text(\"a\")",
                12.0,
                Color::BLACK,
                0.0,
                TypstBackendKind::Svg,
            );
            let svg = "<svg>abc</svg>".to_string();
            let bytes = svg.len();

            insert_cached_value(
                &mut cache,
                key.clone(),
                CachedValue::Svg {
                    svg,
                    width: 12.0,
                    height: 8.0,
                },
            );

            assert_eq!(cache.total_bytes, bytes);
            assert!(cache.entries.contains_key(&key));
        }

        #[test]
        fn cache_insert_evicts_stale_entry_when_replacement_is_too_large() {
            let mut cache = CacheState::default();
            let key = make_key(
                "#text(\"grow\")",
                12.0,
                Color::BLACK,
                0.0,
                TypstBackendKind::Svg,
            );
            let small_svg = "<svg>small</svg>".to_string();
            let small_bytes = small_svg.len();

            insert_cached_value(
                &mut cache,
                key.clone(),
                CachedValue::Svg {
                    svg: small_svg,
                    width: 12.0,
                    height: 8.0,
                },
            );

            assert_eq!(cache.total_bytes, small_bytes);
            assert!(cache.entries.contains_key(&key));

            insert_cached_value(
                &mut cache,
                key.clone(),
                CachedValue::Svg {
                    svg: "x".repeat(MAX_CACHE_BYTES + 1),
                    width: 12.0,
                    height: 8.0,
                },
            );

            assert_eq!(cache.total_bytes, 0);
            assert!(!cache.entries.contains_key(&key));
        }

        #[test]
        fn oversized_render_path_evicts_stale_entry_even_without_recaching() {
            let mut cache = CacheState::default();
            let key = make_key(
                "#text(\"grow\")",
                12.0,
                Color::BLACK,
                0.0,
                TypstBackendKind::Svg,
            );

            insert_cached_value(
                &mut cache,
                key.clone(),
                CachedValue::Svg {
                    svg: "<svg>small</svg>".to_string(),
                    width: 12.0,
                    height: 8.0,
                },
            );

            let oversized_svg = "x".repeat(MAX_CACHE_BYTES + 1);
            if oversized_svg.len() > MAX_CACHE_BYTES {
                remove_cached_value(&mut cache, &key);
            } else {
                insert_cached_value(
                    &mut cache,
                    key.clone(),
                    CachedValue::Svg {
                        svg: oversized_svg,
                        width: 12.0,
                        height: 8.0,
                    },
                );
            }

            assert_eq!(cache.total_bytes, 0);
            assert!(!cache.entries.contains_key(&key));
        }
    }
}

#[cfg(feature = "typst-math")]
pub use imp::{measure_text, render_raster, render_svg};