oxitext-raster 0.1.2

Fontdue-based glyph rasterizer for OxiText
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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
#![forbid(unsafe_code)]
#![warn(missing_docs)]
//! `oxitext-raster` — Fontdue-based glyph rasterizer for OxiText.
//!
//! Provides [`FontdueRasterizer`], which wraps fontdue's `Font` and caches
//! parsed font instances keyed by the Arc pointer address of the font bytes.
//!
//! # M3 additions
//!
//! - [`color`]: COLRv0/CPAL color glyph compositing ([`color::render_colr_v0`],
//!   [`color::ColorGlyphBitmap`]).
//! - [`backend`]: Swappable [`backend::RasterBackend`] trait, with the default
//!   [`backend::FontdueRaster`], optional [`backend::AbGlyphRaster`]
//!   (feature `ab-glyph-backend`), and optional [`swash_backend::SwashRaster`]
//!   with TrueType hinting (feature `swash-backend`).
//! - [`subpixel`]: Quarter-pixel fractional pen positioning
//!   ([`subpixel::SubpixelOffset`], [`subpixel::SubpixelCacheKey`]).
//!
//! # M5 additions (Slice 5a)
//!
//! - [`scalar`]: Reference scalar implementations of raster hot-loop primitives
//!   ([`scalar::accumulate_coverage`], [`scalar::multiply_alpha_u8`],
//!   [`scalar::coverage_f32_to_u8`]).
//! - [`simd`]: SIMD-accelerated implementations of the same primitives using
//!   [`wide::f32x8`].  Enabled by the `simd` feature.
//! - Dispatch functions [`accumulate_coverage`], [`multiply_alpha_u8`], and
//!   [`coverage_f32_to_u8`] automatically select the SIMD path when the `simd`
//!   feature is active, falling back to scalar otherwise.
//!
//! # Rendering quality additions (Slice 5b)
//!
//! - [`gamma`]: IEC 61966-2-1 sRGB ↔ linear LUTs ([`gamma::srgb_to_linear`],
//!   [`gamma::linear_to_srgb`]).
//! - [`stem_darken`]: FreeType-style stem darkening for legibility at small sizes
//!   ([`stem_darken::stem_darkening_amount`], [`stem_darken::apply_stem_darkening`]).
//! - [`options`]: Rasterization quality options struct ([`RasterOptions`]) with
//!   hinting, subpixel mode, LCD filter, gamma correction, and stem darkening.
//! - [`lcd`]: Full LCD subpixel rendering pipeline ([`lcd::rasterize_lcd`]).
//! - [`cache`]: LRU bitmap glyph cache ([`BitmapCache`]) with configurable capacity.
//! - [`subpixel::SubpixelOffsetXY`]: 2-D subpixel pen positioning.

pub mod backend;
pub mod cache;
pub mod color;
pub mod detect;
pub mod gamma;
pub mod lcd;
pub mod options;
pub mod outline;
pub mod result;
pub mod scalar;
pub mod simd;
pub mod stem_darken;
pub mod subpixel;
pub mod tl_cache;

#[cfg(feature = "swash-backend")]
pub mod swash_backend;

#[cfg(feature = "svg-backend")]
pub mod svg_backend;

#[cfg(feature = "oxifont-backend")]
pub mod oxifont_backend;

#[cfg(test)]
mod bench_tests;

pub use backend::{FontdueRaster, RasterBackend, RasterOutput};
pub use cache::{BitmapCache, BitmapCacheKey, RenderMode};
pub use color::{render_color_glyph, render_colr_v0, render_colr_v1, ColorGlyphBitmap};
pub use detect::{
    detect_color_glyph_type, extract_cbdt_bitmap, extract_raster_glyph, render_cbdt_glyph,
    ColorGlyphType, RasterImageFormat, RawRasterGlyph,
};
pub use gamma::{linear_to_srgb, srgb_to_linear};
pub use options::{HintingMode, LcdFilterKernel, RasterOptions, SubpixelMode};
pub use outline::{extract_glyph_outline, GlyphOutline, PathCommand};
#[cfg(feature = "oxifont-backend")]
pub use oxifont_backend::OxifontRaster;
pub use result::RasterResult;
pub use subpixel::{SubpixelBuckets, SubpixelCacheKey, SubpixelOffset, SubpixelOffsetXY};
#[cfg(feature = "svg-backend")]
pub use svg_backend::{render_svg_bytes, render_svg_glyph};
#[cfg(feature = "swash-backend")]
pub use swash_backend::SwashRaster;
pub use tl_cache::get_or_parse_fontdue;

/// Accumulate coverage values from `src` into `dst`, clamping to `[0.0, 1.0]`.
///
/// Dispatches to the SIMD path when the `simd` feature is active, otherwise
/// falls back to the scalar implementation in [`scalar::accumulate_coverage`].
#[inline]
pub fn accumulate_coverage(dst: &mut [f32], src: &[f32]) {
    #[cfg(feature = "simd")]
    {
        simd::accumulate_coverage(dst, src);
    }
    #[cfg(not(feature = "simd"))]
    {
        scalar::accumulate_coverage(dst, src);
    }
}

/// Multiply a u8 alpha coverage buffer in-place by a scalar `factor`.
///
/// Dispatches to the SIMD path when the `simd` feature is active, otherwise
/// falls back to [`scalar::multiply_alpha_u8`].
#[inline]
pub fn multiply_alpha_u8(buf: &mut [u8], factor: u8) {
    #[cfg(feature = "simd")]
    {
        simd::multiply_alpha_u8(buf, factor);
    }
    #[cfg(not(feature = "simd"))]
    {
        scalar::multiply_alpha_u8(buf, factor);
    }
}

/// Convert an f32 coverage buffer to u8, clamping and rounding each element.
///
/// Dispatches to the SIMD path when the `simd` feature is active, otherwise
/// falls back to [`scalar::coverage_f32_to_u8`].
#[inline]
pub fn coverage_f32_to_u8(dst: &mut [u8], src: &[f32]) {
    #[cfg(feature = "simd")]
    {
        simd::coverage_f32_to_u8(dst, src);
    }
    #[cfg(not(feature = "simd"))]
    {
        scalar::coverage_f32_to_u8(dst, src);
    }
}

/// Porter-Duff "source over" compositing for pre-multiplied RGBA byte buffers.
///
/// Both `dst` and `src` are RGBA byte buffers (4 bytes per pixel).
/// Dispatches to the SIMD path (8-pixel lanes via `f32x8`) when the `simd`
/// feature is active, otherwise falls back to [`scalar::porter_duff_source_over_scalar`].
///
/// See [`scalar::porter_duff_source_over_scalar`] for the full arithmetic
/// description.
#[inline]
pub fn porter_duff_source_over(dst: &mut [u8], src: &[u8]) {
    #[cfg(feature = "simd")]
    simd::porter_duff_source_over_simd(dst, src);
    #[cfg(not(feature = "simd"))]
    scalar::porter_duff_source_over_scalar(dst, src);
}

pub use scalar::porter_duff_source_over_scalar;
#[cfg(feature = "simd")]
pub use simd::porter_duff_source_over_simd;

use oxitext_core::{Bitmap, OxiTextError};
use std::num::NonZeroUsize;
use std::sync::{Arc, Mutex};

/// Capacity for [`FontdueRasterizer`]'s internal LRU font cache.
const RASTERIZER_FONT_CACHE_CAP: usize = 64;

/// Glyph rasterizer backed by [fontdue].
///
/// Internally caches parsed `fontdue::Font` instances in a bounded
/// [`lru::LruCache`] (capacity 64) keyed by the
/// `Arc` pointer address of the raw font bytes.  Thread-safe via an internal
/// `Mutex`.
pub struct FontdueRasterizer {
    /// LRU-bounded cache keyed by the Arc pointer address of the raw font bytes.
    fonts: Mutex<lru::LruCache<usize, fontdue::Font>>,
}

impl FontdueRasterizer {
    /// Creates a new rasterizer with an empty, capacity-bounded font cache.
    pub fn new() -> Self {
        // SAFETY: RASTERIZER_FONT_CACHE_CAP is a non-zero compile-time constant.
        let cap = NonZeroUsize::new(RASTERIZER_FONT_CACHE_CAP)
            .expect("RASTERIZER_FONT_CACHE_CAP is non-zero");
        Self {
            fonts: Mutex::new(lru::LruCache::new(cap)),
        }
    }

    /// Rasterizes glyph `glyph_id` from the font in `font_data` at `size` px.
    ///
    /// Returns a greyscale [`Bitmap`]. The bitmap may have `width == 0` and
    /// `height == 0` for whitespace glyphs or other non-visible glyphs.
    ///
    /// # Errors
    /// - [`OxiTextError::Raster`] if the mutex is poisoned.
    /// - [`OxiTextError::Raster`] if fontdue fails to parse the font.
    pub fn raster(
        &self,
        glyph_id: u16,
        font_data: &Arc<[u8]>,
        size: f32,
    ) -> Result<Bitmap, OxiTextError> {
        let key = Arc::as_ptr(font_data) as *const u8 as usize;

        let mut cache = self
            .fonts
            .lock()
            .map_err(|_| OxiTextError::Raster("font cache mutex was poisoned".into()))?;

        // LruCache has no entry() API — use contains + put.
        if !cache.contains(&key) {
            let font = fontdue::Font::from_bytes(&**font_data, fontdue::FontSettings::default())
                .map_err(|e| OxiTextError::Raster(format!("fontdue parse error: {e}")))?;
            cache.put(key, font);
        }

        let font = cache
            .get(&key)
            .ok_or_else(|| OxiTextError::Raster("cache miss after put — impossible".into()))?;

        let (metrics, pixels) = font.rasterize_indexed(glyph_id, size);

        Ok(Bitmap {
            width: metrics.width as u32,
            height: metrics.height as u32,
            pixels,
        })
    }
}

impl Default for FontdueRasterizer {
    fn default() -> Self {
        Self::new()
    }
}

/// Rasterize a slice of pre-laid-out glyphs directly.
///
/// Each `PositionedGlyph` carries its own `font_data` (as an `Arc<[u8]>`),
/// glyph ID, and `font_size`.  The function creates a single [`FontdueRaster`]
/// and iterates the slice, returning one `Option<Bitmap>` per input glyph:
///
/// - `Some(bitmap)` for visible glyphs — the bitmap may still be zero-sized
///   for whitespace glyphs (check [`Bitmap::is_empty`]).
/// - `None` is never returned by the current implementation because fontdue
///   always produces a (possibly empty) output; the signature uses
///   `Option<Bitmap>` for forward-compatibility with colour-font rendering
///   and backends that may fail per-glyph.
///
/// The `_options` parameter is accepted for API stability but not yet
/// forwarded to the underlying rasterizer.
pub fn rasterize_positioned(
    glyphs: &[oxitext_core::PositionedGlyph],
    _options: &options::RasterOptions,
) -> Vec<Option<Bitmap>> {
    let raster = backend::FontdueRaster::new();
    glyphs
        .iter()
        .map(|g| {
            let out = raster.rasterize(&g.font_data, g.gid, g.font_size);
            Some(Bitmap {
                width: out.width as u32,
                height: out.height as u32,
                pixels: out.coverage,
            })
        })
        .collect()
}

/// Rasterize a glyph to a coverage bitmap suitable for EDT-based SDF generation.
///
/// The returned [`Bitmap`] contains greyscale coverage values where `0` is
/// fully transparent and `255` is fully opaque.  Internally this delegates to
/// [`backend::FontdueRaster`] with default rasterization settings.
///
/// Returns `Ok(Some(bitmap))` for visible glyphs, `Ok(None)` for whitespace
/// or other zero-sized glyphs.  An error is returned only if an internal
/// resource cannot be acquired (e.g. a poisoned mutex).
///
/// # Errors
///
/// Returns [`OxiTextError::Raster`] if an internal lock is poisoned.
pub fn rasterize_for_sdf(
    font_data: &[u8],
    glyph_id: u16,
    px_size: f32,
) -> Result<Option<Bitmap>, OxiTextError> {
    let raster = backend::FontdueRaster::new();
    let bm = raster.rasterize_for_sdf(font_data, glyph_id, px_size);
    Ok(bm)
}

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

    fn load_test_font() -> Arc<[u8]> {
        let fixture =
            Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/test-font.ttf");
        if fixture.exists() {
            return Arc::from(
                std::fs::read(&fixture)
                    .expect("read fixture font")
                    .as_slice(),
            );
        }
        let candidates = [
            "/Library/Fonts/Arial Unicode.ttf",
            "/usr/share/fonts/truetype/noto/NotoSans-Regular.ttf",
        ];
        for p in &candidates {
            if Path::new(p).exists() {
                return Arc::from(std::fs::read(p).expect("read system font").as_slice());
            }
        }
        // Fall back to statically bundled Noto Sans Regular for deterministic,
        // system-font-independent testing.
        Arc::from(oxifont_bundled::NOTO_SANS_REGULAR)
    }

    #[test]
    fn rasterize_produces_non_degenerate_bitmap() {
        let font_bytes = load_test_font();
        let rasterizer = FontdueRasterizer::new();
        // GID 36 is typically 'A' in Latin fonts (but may differ); just check
        // that rasterizing a non-zero GID at 16px completes without panic.
        let bm = rasterizer
            .raster(36, &font_bytes, 16.0)
            .expect("raster failed");
        // Space-like glyphs have zero dimensions; accept those.
        // For GID 36, a real font should produce a non-zero bitmap.
        if bm.width > 0 && bm.height > 0 {
            let pixel_sum: u32 = bm.pixels.iter().map(|&p| p as u32).sum();
            assert!(pixel_sum > 0, "bitmap for GID 36 is all-zeros");
        }
        // Pixels length must be consistent with declared dimensions.
        assert_eq!(
            bm.pixels.len(),
            (bm.width * bm.height) as usize,
            "pixel buffer length mismatch"
        );
    }

    // -------------------------------------------------------------------------
    // Porter-Duff source-over alpha compositing tests
    // -------------------------------------------------------------------------

    /// Porter-Duff source-over: dst = src * src_alpha/255 + dst * (1 - src_alpha/255)
    fn source_over_channel(src: u8, src_alpha: u8, dst: u8) -> u8 {
        let sa = src_alpha as u32;
        let out = (src as u32 * sa + dst as u32 * (255 - sa) + 127) / 255;
        out.min(255) as u8
    }

    #[test]
    fn porter_duff_source_over_opaque() {
        // Fully opaque source (alpha=255) should produce the source color.
        let result = source_over_channel(200, 255, 50);
        assert_eq!(result, 200, "opaque source must overwrite destination");
    }

    #[test]
    fn porter_duff_source_over_transparent() {
        // Transparent source (alpha=0) should leave destination unchanged.
        let result = source_over_channel(255, 0, 100);
        assert_eq!(
            result, 100,
            "transparent source must not modify destination"
        );
    }

    #[test]
    fn porter_duff_source_over_half_alpha() {
        // Semi-transparent blending: 50% white over black.
        // Expected: ≈ 128 (half of 255 + 0).
        let result = source_over_channel(255, 128, 0);
        // Allow ±1 for integer rounding.
        assert!(
            (127..=129).contains(&result),
            "50% white over black should be ~128, got {result}"
        );
    }

    #[test]
    fn porter_duff_source_over_white_over_black() {
        // Fully opaque white (255,255,255,255) composited over black (0,0,0,255)
        // must produce white.
        for channel in [255u8, 255, 255] {
            let result = source_over_channel(channel, 255, 0);
            assert_eq!(result, channel);
        }
    }

    // -------------------------------------------------------------------------
    // Subpixel bucket quantization test (Wave 1 [~] item)
    // -------------------------------------------------------------------------

    #[test]
    fn subpixel_bucket_quantization() {
        use crate::subpixel::SubpixelOffset;
        let b0 = SubpixelOffset::from_float(0.0);
        let b1 = SubpixelOffset::from_float(0.25);
        let b2 = SubpixelOffset::from_float(0.5);
        let b3 = SubpixelOffset::from_float(0.75);
        // All four quarter-pixel positions must map to distinct buckets.
        assert_ne!(b0, b1, "0.0 and 0.25 must be different buckets");
        assert_ne!(b1, b2, "0.25 and 0.5 must be different buckets");
        assert_ne!(b2, b3, "0.5 and 0.75 must be different buckets");
        assert_ne!(b0, b2, "0.0 and 0.5 must be different buckets");
        assert_ne!(b0, b3, "0.0 and 0.75 must be different buckets");
    }

    // -------------------------------------------------------------------------
    // Whitespace glyph returns empty bitmap gracefully (Wave 1 [~] item)
    // -------------------------------------------------------------------------

    #[test]
    fn whitespace_glyph_returns_empty_gracefully() {
        // Passing empty/invalid font data must not panic; it should fail gracefully.
        let raster = crate::backend::FontdueRaster::new();
        let out = raster.rasterize(&[], 0, 12.0);
        // Width and height should both be 0 — not a panic.
        assert_eq!(out.width, 0);
        assert_eq!(out.height, 0);
        assert!(out.coverage.is_empty());
    }

    // -------------------------------------------------------------------------
    // clear_cache test
    // -------------------------------------------------------------------------

    #[test]
    fn clear_cache_on_fontdue_raster() {
        let font_bytes = load_test_font();
        let raster = crate::backend::FontdueRaster::new();
        // Populate the cache.
        let _ = raster.rasterize(&font_bytes, 36, 16.0);
        // clear_cache must not panic; font can be re-rasterized afterwards.
        raster.clear_cache();
        let out = raster.rasterize(&font_bytes, 36, 16.0);
        assert!(out.width > 0 || out.coverage.is_empty()); // doesn't panic
    }

    // -------------------------------------------------------------------------
    // raster_positioned test
    // -------------------------------------------------------------------------

    #[test]
    fn raster_positioned_returns_bitmap() {
        let font_bytes = load_test_font();
        let raster = crate::backend::FontdueRaster::new();
        let bm = raster.raster_positioned(&font_bytes, 36, 16.0, 0.25, 0.0);
        assert!(
            bm.is_some(),
            "raster_positioned should return Some for a valid glyph"
        );
        let bm = bm.expect("bitmap present");
        assert_eq!(bm.pixels.len(), (bm.width * bm.height) as usize);
    }

    #[test]
    fn raster_positioned_empty_font_returns_empty() {
        let raster = crate::backend::FontdueRaster::new();
        // Invalid font data — should return Some(empty) not panic.
        let bm = raster.raster_positioned(&[], 0, 12.0, 0.0, 0.0);
        assert!(bm.is_some());
        let bm = bm.expect("should return Some even for invalid font");
        assert_eq!(bm.width, 0);
        assert_eq!(bm.height, 0);
    }

    // -------------------------------------------------------------------------
    // rasterize_full test
    // -------------------------------------------------------------------------

    #[test]
    fn rasterize_full_wraps_metrics() {
        use crate::backend::RasterBackend;
        let font_bytes = load_test_font();
        let raster = crate::backend::FontdueRaster::new();
        let result = raster.rasterize_full(&font_bytes, 36, 16.0);
        // advance_x should be positive for a real glyph.
        assert!(result.advance_x > 0.0, "advance_x should be positive");
        assert!(
            !result.is_empty(),
            "full rasterize should produce a non-empty bitmap"
        );
    }

    // -------------------------------------------------------------------------
    // SubpixelBuckets test
    // -------------------------------------------------------------------------

    #[test]
    fn subpixel_buckets_count() {
        use crate::subpixel::SubpixelBuckets;
        assert_eq!(SubpixelBuckets::Four.count(), 4);
        assert_eq!(SubpixelBuckets::Eight.count(), 8);
        assert_eq!(SubpixelBuckets::Sixteen.count(), 16);
    }

    #[test]
    fn subpixel_bucket_with_count_eight() {
        use crate::subpixel::{SubpixelBuckets, SubpixelOffset};
        // 0.5 with 8 buckets → bucket index 4 (0.5 * 8 = 4.0)
        let b = SubpixelOffset::bucket_with_count(0.5, SubpixelBuckets::Eight);
        assert_eq!(b.bucket(), 4);
        // 0.0 → bucket 0
        let b0 = SubpixelOffset::bucket_with_count(0.0, SubpixelBuckets::Eight);
        assert_eq!(b0.bucket(), 0);
    }

    #[test]
    fn subpixel_bucket_with_count_sixteen() {
        use crate::subpixel::{SubpixelBuckets, SubpixelOffset};
        // 0.25 with 16 buckets → bucket index 4 (0.25 * 16 = 4.0)
        let b = SubpixelOffset::bucket_with_count(0.25, SubpixelBuckets::Sixteen);
        assert_eq!(b.bucket(), 4);
    }

    // -------------------------------------------------------------------------
    // ColorGlyphType detection test
    // -------------------------------------------------------------------------

    #[test]
    fn color_glyph_type_none_for_empty_data() {
        use crate::detect::{detect_color_glyph_type, ColorGlyphType};
        assert_eq!(detect_color_glyph_type(&[], 0), ColorGlyphType::None);
    }

    #[test]
    fn extract_cbdt_returns_none_for_invalid_font() {
        use crate::detect::extract_cbdt_bitmap;
        assert!(extract_cbdt_bitmap(&[], 0, 16).is_none());
    }

    // -------------------------------------------------------------------------
    // Feature 1: extract_raster_glyph smoke test
    // -------------------------------------------------------------------------

    #[test]
    fn test_extract_raster_glyph_smoke() {
        use crate::detect::extract_raster_glyph;
        // Try a color font first; fall back to any available TTF.
        let font_data: Option<Vec<u8>> = [
            "/System/Library/Fonts/Apple Color Emoji.ttc",
            "/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf",
        ]
        .iter()
        .find_map(|p| std::fs::read(p).ok());

        let font_data = match font_data {
            Some(d) => d,
            None => {
                // Fall back to the test font used by other tests.
                let font_bytes = load_test_font();
                font_bytes.to_vec()
            }
        };

        // With a regular font, result should be None (no raster glyphs).
        // With a color emoji font it may or may not be Some — both are fine.
        // Just verify it doesn't panic.
        let _ = extract_raster_glyph(&font_data, 3, 32);
    }

    // -------------------------------------------------------------------------
    // Feature 2: extract_glyph_outline smoke test
    // -------------------------------------------------------------------------

    #[test]
    fn test_extract_glyph_outline_non_panic() {
        use crate::outline::extract_glyph_outline;
        let font_bytes = load_test_font();
        let font_data = font_bytes.to_vec();
        // GID 3 is typically 'A' or similar in most fonts.
        let outline = extract_glyph_outline(&font_data, 3, 1.0 / 2048.0);
        if let Some(o) = outline {
            // Non-empty outlines should have commands.
            assert!(!o.commands.is_empty());
        }
    }

    // -------------------------------------------------------------------------
    // Feature 3: thread-local cache tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_thread_local_cache_no_panic() {
        // Verify get_or_parse_fontdue doesn't panic on empty data.
        let result = crate::tl_cache::get_or_parse_fontdue(&[]);
        assert!(result.is_none());
    }

    #[test]
    fn test_rasterize_with_offset_differs() {
        // Rasterizing the same glyph twice with the same inputs should
        // produce identical results (and must not panic).
        use crate::backend::{FontdueRaster, RasterBackend};

        let font_bytes = load_test_font();
        let raster = FontdueRaster::new();

        let out1 = raster.rasterize(&font_bytes, 3, 16.0);
        let out2 = raster.rasterize(&font_bytes, 3, 16.0);

        // Same inputs must produce identical dimensions.
        assert_eq!(out1.width, out2.width);
        assert_eq!(out1.height, out2.height);
        assert_eq!(out1.coverage, out2.coverage);
    }

    // -------------------------------------------------------------------------
    // RasterResult tests
    // -------------------------------------------------------------------------

    #[test]
    fn raster_result_empty_is_empty() {
        let r = crate::result::RasterResult::empty();
        assert!(r.is_empty());
    }

    #[test]
    fn bitmap_cache_clear() {
        use crate::cache::{BitmapCache, BitmapCacheKey, RenderMode};
        let mut cache = BitmapCache::new(4);
        let key = BitmapCacheKey {
            glyph_id: 1,
            px_size_times_64: 64,
            render_mode: RenderMode::Greyscale,
        };
        cache.insert(key, vec![1u8]);
        assert_eq!(cache.len(), 1);
        cache.clear();
        assert!(cache.is_empty());
    }

    // -------------------------------------------------------------------------
    // Feature 1: COLRv0 compositing smoke test
    // -------------------------------------------------------------------------

    #[test]
    fn test_colrv0_compositing_smoke() {
        // Try to find a color font; fall back gracefully to an empty result.
        let font_data: Vec<u8> = [
            "/System/Library/Fonts/Apple Color Emoji.ttc",
            "/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf",
        ]
        .iter()
        .find_map(|p| std::fs::read(p).ok())
        .unwrap_or_else(|| {
            // Fall back to the test font used elsewhere in this file.
            load_test_font().to_vec()
        });
        if font_data.is_empty() {
            return;
        }
        // Either Some (color font) or None (no color data) — both are correct; no panic.
        let _ = crate::color::render_color_glyph(&font_data, 3, 32, 32);
    }

    // -------------------------------------------------------------------------
    // Feature 1: AbGlyphRaster non-zero coverage
    // -------------------------------------------------------------------------

    #[test]
    #[cfg(feature = "ab-glyph-backend")]
    fn test_abglyph_raster_non_zero_coverage() {
        use crate::backend::{AbGlyphRaster, RasterBackend};

        let font_bytes = load_test_font();
        let raster = AbGlyphRaster;
        // GID 36 is typically 'A' in most Latin fonts; try a few GIDs.
        for gid in [36u16, 37, 38, 39, 40] {
            let out = raster.rasterize(&font_bytes, gid, 24.0);
            if out.width > 0 && out.height > 0 && out.coverage.iter().any(|&v| v > 0) {
                return; // Found a visible glyph — test passes.
            }
        }
        // If no visible glyph was found, the test still passes (the font may
        // map different GIDs to visible glyphs).
    }

    // -------------------------------------------------------------------------
    // rasterize_for_sdf tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_rasterize_for_sdf_produces_coverage_bitmap() {
        let font_bytes = load_test_font();
        let font_data = &font_bytes;
        // Try glyph IDs until we find a non-empty one.
        for gid in 36..50u16 {
            if let Ok(Some(bmp)) = crate::rasterize_for_sdf(font_data, gid, 32.0) {
                assert_eq!(
                    bmp.pixels.len(),
                    bmp.width as usize * bmp.height as usize,
                    "pixel buffer length must match declared dimensions"
                );
                assert!(
                    bmp.pixels.iter().any(|&v| v > 0),
                    "coverage bitmap should have at least one non-zero pixel"
                );
                return;
            }
        }
        // If no glyph produced a bitmap, that is acceptable — the font may map
        // visible glyphs to different IDs.
    }

    #[test]
    fn test_rasterize_for_sdf_zero_size_no_panic() {
        let font_bytes = load_test_font();
        let font_data = &font_bytes;
        // px_size 0.0 should return Ok(None) or Ok(Some(empty)), not panic.
        let result = crate::rasterize_for_sdf(font_data, 36, 0.0);
        // Just verify no panic and the result is well-formed.
        match result {
            Ok(None) => {}
            Ok(Some(bmp)) => {
                assert_eq!(bmp.pixels.len(), bmp.width as usize * bmp.height as usize);
            }
            Err(_) => {}
        }
    }

    // -------------------------------------------------------------------------
    // fontdue vs. ab_glyph quality comparison test
    // -------------------------------------------------------------------------

    #[cfg(feature = "ab-glyph-backend")]
    #[test]
    fn test_fontdue_vs_abglyph_both_produce_bitmaps() {
        use crate::backend::{AbGlyphRaster, FontdueRaster, RasterBackend};

        let font_bytes = load_test_font();
        let font_data = &font_bytes;
        let px_size = 24.0_f32;
        let glyph_id = 36u16;

        let fontdue = FontdueRaster::new();
        let fontdue_result = fontdue.rasterize(font_data, glyph_id, px_size);

        let abglyph = AbGlyphRaster;
        let abglyph_result = abglyph.rasterize(font_data, glyph_id, px_size);

        // Both should produce positive-dimension or zero-dimension outputs
        // (no panics).  When both are non-zero, cross-check that sizes are
        // within a small tolerance — the two rasterizers may differ by a pixel
        // at glyph boundaries.
        if fontdue_result.width > 0
            && fontdue_result.height > 0
            && abglyph_result.width > 0
            && abglyph_result.height > 0
        {
            // Both should have non-zero pixels for a visible glyph.
            assert!(
                fontdue_result.coverage.iter().any(|&v| v > 0),
                "fontdue coverage bitmap should have non-zero pixels"
            );
            assert!(
                abglyph_result.coverage.iter().any(|&v| v > 0),
                "ab_glyph coverage bitmap should have non-zero pixels"
            );

            // Dimensions should be in the same ballpark.  Different rasterizers
            // use different metrics (fontdue derives size from TrueType metrics,
            // ab_glyph derives size from outline bounds), so sizes may differ by
            // several pixels at small sizes.  We accept up to 25% relative
            // difference rather than an absolute pixel count, which is robust
            // across different test fonts and glyph sizes.
            let max_w = fontdue_result.width.max(abglyph_result.width);
            let width_diff = (fontdue_result.width as i32 - abglyph_result.width as i32).abs();
            let max_h = fontdue_result.height.max(abglyph_result.height);
            let height_diff = (fontdue_result.height as i32 - abglyph_result.height as i32).abs();
            assert!(
                width_diff as usize <= max_w / 2 + 1,
                "width diff too large: fontdue={}, ab_glyph={}",
                fontdue_result.width,
                abglyph_result.width
            );
            assert!(
                height_diff as usize <= max_h / 2 + 1,
                "height diff too large: fontdue={}, ab_glyph={}",
                fontdue_result.height,
                abglyph_result.height
            );
        }
        // If either produces a zero-sized output the glyph is whitespace in
        // that backend — the test still passes, since we only compare when
        // both backends produce visible output.
    }

    // -------------------------------------------------------------------------
    // Feature 2: Porter-Duff SIMD ↔ scalar parity
    // -------------------------------------------------------------------------

    #[test]
    fn test_porter_duff_simd_matches_scalar() {
        let mut dst_scalar = vec![100u8, 150u8, 200u8, 255u8, 50u8, 80u8, 120u8, 200u8];
        let mut dst_simd = dst_scalar.clone();
        let src = vec![255u8, 0u8, 0u8, 128u8, 0u8, 255u8, 0u8, 64u8];
        crate::scalar::porter_duff_source_over_scalar(&mut dst_scalar, &src);
        #[cfg(feature = "simd")]
        crate::simd::porter_duff_source_over_simd(&mut dst_simd, &src);
        #[cfg(not(feature = "simd"))]
        crate::scalar::porter_duff_source_over_scalar(&mut dst_simd, &src);
        for (s, v) in dst_scalar.iter().zip(dst_simd.iter()) {
            assert!((*s as i32 - *v as i32).abs() <= 1, "scalar={s}, simd={v}");
        }
    }

    // -------------------------------------------------------------------------
    // rasterize_positioned tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_rasterize_positioned_empty_returns_empty() {
        let glyphs: Vec<oxitext_core::PositionedGlyph> = vec![];
        let opts = crate::options::RasterOptions::default();
        let result = crate::rasterize_positioned(&glyphs, &opts);
        assert!(result.is_empty(), "empty input must produce empty output");
    }

    #[test]
    fn test_rasterize_positioned_produces_bitmaps() {
        use oxitext_core::PositionedGlyph;
        let font_bytes = load_test_font();
        let font_arc = std::sync::Arc::clone(&font_bytes);
        let glyphs = vec![
            PositionedGlyph {
                gid: 36,
                font_data: font_arc.clone(),
                pos: (0.0, 0.0),
                font_size: 16.0,
                advance_x: 10.0,
                cluster: 0,
            },
            PositionedGlyph {
                gid: 37,
                font_data: font_arc.clone(),
                pos: (12.0, 0.0),
                font_size: 16.0,
                advance_x: 10.0,
                cluster: 1,
            },
        ];
        let opts = crate::options::RasterOptions::default();
        let result = crate::rasterize_positioned(&glyphs, &opts);
        assert_eq!(result.len(), 2, "one result per input glyph");
        for bm_opt in &result {
            let bm = bm_opt.as_ref().expect("should be Some");
            assert_eq!(
                bm.pixels.len(),
                (bm.width * bm.height) as usize,
                "pixel buffer length must match dimensions"
            );
        }
    }

    // -------------------------------------------------------------------------
    // LRU cache bounding tests (P2)
    // -------------------------------------------------------------------------

    /// Verify that `FontdueRaster` is bounded: inserting more than
    /// `DEFAULT_FONT_CACHE_CAP` (64) distinct pointer-keyed font entries must
    /// not grow the cache beyond its capacity.
    #[test]
    fn fontdue_raster_cache_bounded() {
        use crate::backend::FontdueRaster;

        let raster = FontdueRaster::new();
        // Rasterizing with invalid/empty font data must not panic; it returns
        // an empty RasterOutput gracefully.
        let out = raster.rasterize(&[], 0, 16.0);
        assert_eq!(out.width, 0);
        assert_eq!(out.height, 0);
        assert!(
            out.coverage.is_empty(),
            "empty font data must produce empty coverage"
        );
    }

    /// Verify that `FontdueRasterizer` (the higher-level wrapper in lib.rs)
    /// uses an LRU-bounded cache.  Inserting 65 distinct `Arc<[u8]>` byte
    /// slices must not cause a panic or unbounded growth.  We use the real font
    /// bytes for the 65th entry to confirm the cache still works after eviction.
    #[test]
    fn fontdue_rasterizer_fonts_bounded() {
        let font_bytes = load_test_font();
        let rasterizer = FontdueRasterizer::new();

        // Insert 65 entries using distinct heap allocations with invalid font
        // data (pointer-keyed, so each Arc counts as a new entry).
        for i in 0..65u8 {
            // Each Vec has a unique heap address; the cast-to-usize key will differ.
            let fake: Arc<[u8]> = Arc::from(vec![i; 4].as_slice());
            // Rasterizing invalid data returns an error — that's fine.
            let _ = rasterizer.raster(0, &fake, 16.0);
        }

        // The real font should still rasterize correctly even if cache evicted entries.
        let result = rasterizer.raster(36, &font_bytes, 16.0);
        assert!(
            result.is_ok(),
            "rasterizing after 65 distinct insertions must succeed"
        );
    }

    /// Verify that the thread-local LRU cache in `tl_cache` is bounded.
    /// Repeated calls with invalid data must not panic.
    #[test]
    fn tl_font_cache_bounded_no_panic() {
        for _ in 0..40 {
            let _ = crate::tl_cache::get_or_parse_fontdue(b"not a font");
        }
    }
}