mln-rs-engine 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use std::sync::Arc;

use mln_style::bitmap::AlphaImage;
use mln_style::size::Size;

use super::{GlyphCache, GlyphCacheRange, MIN_SWEEP};
use crate::text::glyph::{Glyph, GlyphFont, GlyphID, GlyphMetrics, GlyphRange};
use crate::text::glyph_pbf::parse_glyph_pbf;

fn glyph(id: char, value: u8) -> Glyph {
    let mut bitmap = AlphaImage::new(Size::new(4, 4));
    bitmap.fill(value);
    Glyph {
        id: GlyphID::from_char(id).unwrap(),
        metrics: GlyphMetrics { width: 4, height: 4, ..Default::default() },
        bitmap,
    }
}

/// The glyph of `range` with `id`.
fn get(range: &GlyphCacheRange, id: char) -> &Arc<Glyph> {
    &range[&GlyphID::from_char(id).unwrap()]
}

/// Equal glyphs are one glyph, in one range or several, and ranges of equal
/// glyphs are one range.
#[test]
fn equal_glyphs_and_ranges_intern_once() {
    let cache = GlyphCache::new();
    let ab = cache.intern_range(vec![glyph('a', 1), glyph('b', 1)]);
    assert!(Arc::ptr_eq(&ab, &cache.intern_range(vec![glyph('a', 1), glyph('b', 1)])));
    let ac = cache.intern_range(vec![glyph('a', 1), glyph('c', 1)]);
    assert!(!Arc::ptr_eq(&ab, &ac));
    assert!(Arc::ptr_eq(get(&ab, 'a'), get(&ac, 'a')));
}

/// Glyphs that differ in their id, metrics or bitmap stay apart, and so do
/// ranges of different glyphs.
#[test]
fn glyphs_that_differ_stay_apart() {
    let cache = GlyphCache::new();
    let a = cache.intern_range(vec![glyph('a', 1)]);
    let shifted = Glyph { metrics: GlyphMetrics { left: 1.0, ..glyph('a', 1).metrics }, ..glyph('a', 1) };
    for other in [glyph('b', 1), shifted, glyph('a', 2)] {
        let id = other.id;
        let other = cache.intern_range(vec![other]);
        assert!(!Arc::ptr_eq(&a, &other), "{other:?}");
        assert!(!a.values().any(|glyph| Arc::ptr_eq(glyph, &other[&id])), "{other:?}");
    }
}

#[test]
fn glyphs_and_ranges_go_with_their_last_owner() {
    let cache = GlyphCache::new();
    let range = cache.intern_range(vec![glyph('a', 1)]);
    let kept = Arc::clone(get(&range, 'a'));
    let weak = Arc::downgrade(&range);
    drop(range);
    cache.intern_range(vec![glyph('b', 1)]);
    assert_eq!(0, weak.strong_count());
    assert_eq!(1, cache.live_glyphs());
    assert!(Arc::ptr_eq(&kept, get(&cache.intern_range(vec![glyph('a', 1)]), 'a')));
}

/// The table drops the entries of glyphs gone as it grows, so a cache that
/// glyphs pass through stays as large as the glyphs alive.
#[test]
fn the_entries_of_glyphs_gone_are_swept() {
    let cache = GlyphCache::new();
    let kept = cache.intern_range(vec![glyph('a', 0)]);
    for value in 1..=255 {
        cache.intern_range(('b'..='z').map(|id| glyph(id, value)).collect());
    }
    let entries = cache.inner.lock().glyphs.buckets.values().map(Vec::len).sum::<usize>();
    assert!(entries < MIN_SWEEP, "{entries} entries");
    assert!(Arc::ptr_eq(&kept, &cache.intern_range(vec![glyph('a', 0)])), "the live range stays");
}

/// Two threads that parse the same glyph PBF get the same glyphs.
#[test]
fn a_range_parsed_on_two_threads_interns_the_same_glyphs() {
    let cache = GlyphCache::new();
    let data = crate::tests::testdata("test/fixtures/resources/glyphs.pbf");
    let intern = || {
        let parsed = parse_glyph_pbf(GlyphRange::new(0, 255, GlyphFont::FontPBF), &data).unwrap();
        cache.intern_range(parsed)
    };

    let (first, second) = std::thread::scope(|scope| {
        let a = scope.spawn(intern);
        let b = scope.spawn(intern);
        (a.join().unwrap(), b.join().unwrap())
    });

    assert!(!first.is_empty());
    assert!(Arc::ptr_eq(&first, &second));
}