cranpose-render-wgpu 0.25.0

GPU renderer for Cranpose scenes through wgpu
Documentation
use super::*;

fn placement(glyph_id: u32, x: i32, font_hash: u64, color: Color) -> AtlasGlyph {
    AtlasGlyph {
        key: GlyphMaskKey {
            font_hash,
            glyph_id,
            size_bits: 14.0f32.to_bits(),
            stroke_bits: 0,
            embolden_bits: 0,
            skew_bits: 0,
        },
        x,
        y: -3,
        width: 7,
        height: 9,
        color,
    }
}

fn fields(glyph: AtlasGlyph) -> (GlyphMaskKey, i32, i32, usize, usize, Color) {
    (
        glyph.key,
        glyph.x,
        glyph.y,
        glyph.width,
        glyph.height,
        glyph.color,
    )
}

const INK: Color = Color(0.1, 0.2, 0.3, 1.0);
const LINK: Color = Color(0.0, 0.4, 1.0, 1.0);

#[test]
fn a_run_gives_back_the_placements_it_was_made_from() {
    // Two segments of one text: a word in ink, then one in the link color
    // and another face, then ink again.
    let placements = [
        placement(40, 0, 7, INK),
        placement(41, 8, 7, INK),
        placement(42, 16, 9, LINK),
        placement(43, 24, 7, INK),
    ];
    let mut scratch = RunGlyphScratch::default();
    let run = RunGlyphs::of(placements.iter().copied(), &mut scratch).expect("compact");
    assert_eq!(run.iter().len(), placements.len());
    for (glyph, placement) in run.iter().zip(placements) {
        assert_eq!(fields(glyph), fields(placement));
    }
    assert_eq!(run.faces.len(), 3, "a face per change of segment");
}

#[test]
fn runs_in_the_same_faces_share_them() {
    let mut scratch = RunGlyphScratch::default();
    let first =
        RunGlyphs::of([placement(40, 0, 7, INK)].into_iter(), &mut scratch).expect("compact");
    let second =
        RunGlyphs::of([placement(41, 0, 7, INK)].into_iter(), &mut scratch).expect("compact");
    let other =
        RunGlyphs::of([placement(41, 0, 7, LINK)].into_iter(), &mut scratch).expect("compact");
    assert!(Rc::ptr_eq(&first.faces, &second.faces));
    assert!(!Rc::ptr_eq(&second.faces, &other.faces));
}

#[test]
fn a_glyph_past_the_compact_form_has_no_run() {
    let mut scratch = RunGlyphScratch::default();
    let huge = AtlasGlyph {
        width: 70_000,
        ..placement(40, 0, 7, INK)
    };
    assert!(RunGlyphs::of([huge].into_iter(), &mut scratch).is_none());
    let far_id = placement(70_000, 0, 7, INK);
    assert!(RunGlyphs::of([far_id].into_iter(), &mut scratch).is_none());
}

#[test]
fn a_compact_glyph_takes_sixteen_bytes() {
    assert_eq!(size_of::<RunGlyph>(), 16);
    assert!(size_of::<AtlasGlyph>() >= 48);
}

#[test]
fn a_run_built_in_a_given_back_slice_holds_its_own_glyphs() {
    let mut scratch = RunGlyphScratch::default();
    let old = [placement(40, 0, 7, INK), placement(41, 8, 7, INK)];
    let new = [placement(50, 1, 7, INK), placement(51, 9, 7, INK)];
    RunGlyphs::of(old.iter().copied(), &mut scratch)
        .expect("compact")
        .give_back(&mut scratch);
    let run = RunGlyphs::of(new.iter().copied(), &mut scratch).expect("compact");
    assert_eq!(run.iter().len(), new.len());
    for (glyph, placement) in run.iter().zip(new) {
        assert_eq!(fields(glyph), fields(placement));
    }
}

#[test]
fn a_run_a_reader_still_holds_keeps_its_glyphs_when_given_back() {
    let mut scratch = RunGlyphScratch::default();
    let old = [placement(40, 0, 7, INK), placement(41, 8, 7, INK)];
    let new = [placement(50, 1, 7, INK), placement(51, 9, 7, INK)];
    let held = RunGlyphs::of(old.iter().copied(), &mut scratch).expect("compact");
    held.clone().give_back(&mut scratch);
    let run = RunGlyphs::of(new.iter().copied(), &mut scratch).expect("compact");
    for (glyph, placement) in held.iter().zip(old) {
        assert_eq!(fields(glyph), fields(placement));
    }
    for (glyph, placement) in run.iter().zip(new) {
        assert_eq!(fields(glyph), fields(placement));
    }
}