cranpose-render-wgpu 0.28.0

GPU renderer for Cranpose scenes through wgpu
Documentation
//! The glyphs of a text run the renderer keeps between frames.

use std::rc::Rc;

use cranpose_render_common::{glyph_mask::GlyphMaskKey, text_atlas::AtlasGlyph};
use cranpose_ui_graphics::Color;

use crate::rc_slice_pool::RcSlicePool;

// The most face lists of recent runs kept for the next runs to share.
const RECENT_FACES: usize = 8;

/// What the glyphs of one segment of a run share: face, size and synthesis
/// (an atlas key but for its glyph id) and color.
#[derive(Clone, Copy, PartialEq)]
struct RunFace {
    key: GlyphMaskKey,
    color: Color,
}

impl RunFace {
    fn of(glyph: &AtlasGlyph) -> Self {
        Self {
            key: GlyphMaskKey {
                glyph_id: 0,
                ..glyph.key
            },
            color: glyph.color,
        }
    }
}

/// A glyph of a run: where it sits in the run's raster, its size, its
/// glyph id and the index of its face.
#[derive(Clone, Copy)]
struct RunGlyph {
    x: i32,
    y: i32,
    width: u16,
    height: u16,
    glyph_id: u16,
    face: u16,
}

impl RunGlyph {
    fn of(glyph: &AtlasGlyph, face: usize) -> Option<Self> {
        Some(Self {
            x: glyph.x,
            y: glyph.y,
            width: u16::try_from(glyph.width).ok()?,
            height: u16::try_from(glyph.height).ok()?,
            glyph_id: u16::try_from(glyph.key.glyph_id).ok()?,
            face: u16::try_from(face).ok()?,
        })
    }
}

/// Where a glyph of a run draws from the run's raster origin, and in what
/// color: what a quad drawn of it needs, without the atlas key it is looked
/// up by.
#[derive(Clone, Copy)]
pub(crate) struct RunGlyphBox {
    pub(crate) x: i32,
    pub(crate) y: i32,
    pub(crate) width: u16,
    pub(crate) height: u16,
    pub(crate) color: Color,
}

/// A run's drawing glyphs, 16 bytes each where a placement takes 72: what
/// a segment's glyphs share is held once, as their face.
#[derive(Clone)]
pub(crate) struct RunGlyphs {
    faces: Rc<[RunFace]>,
    glyphs: Rc<[RunGlyph]>,
}

// Buffers a renderer reuses while it builds runs, the faces of recent runs,
// which the next run shares when its own are the same, and the glyph slices
// of runs the cache let go of.
#[derive(Default)]
pub(crate) struct RunGlyphScratch {
    faces: Vec<RunFace>,
    glyphs: Vec<RunGlyph>,
    recent_faces: Vec<Rc<[RunFace]>>,
    spare_glyphs: RcSlicePool<RunGlyph>,
}

impl RunGlyphScratch {
    // The recent face list equal to the faces just collected, or a new one
    // that joins the recent lists.
    fn shared_faces(&mut self) -> Rc<[RunFace]> {
        if let Some(faces) = self
            .recent_faces
            .iter()
            .find(|faces| ***faces == *self.faces)
        {
            return Rc::clone(faces);
        }
        let faces: Rc<[RunFace]> = Rc::from(self.faces.as_slice());
        if self.recent_faces.len() == RECENT_FACES {
            self.recent_faces.remove(0);
        }
        self.recent_faces.push(Rc::clone(&faces));
        faces
    }

    // Drops the glyph slices kept for later runs, and their room.
    pub(crate) fn clear_spares(&mut self) {
        self.spare_glyphs.clear();
    }
}

impl RunGlyphs {
    /// The run of `placements`, sharing the last run's faces when they are
    /// the same, as consecutive texts in one style's are. `None` when a
    /// glyph has no compact form: a glyph id or a mask side past 65535, or
    /// as many faces.
    pub(crate) fn of(
        placements: impl Iterator<Item = AtlasGlyph>,
        scratch: &mut RunGlyphScratch,
    ) -> Option<Self> {
        scratch.faces.clear();
        scratch.glyphs.clear();
        for placement in placements {
            let face = RunFace::of(&placement);
            if scratch.faces.last() != Some(&face) {
                scratch.faces.push(face);
            }
            let glyph = RunGlyph::of(&placement, scratch.faces.len() - 1)?;
            scratch.glyphs.push(glyph);
        }
        Some(Self {
            faces: scratch.shared_faces(),
            glyphs: scratch.spare_glyphs.filled(&scratch.glyphs),
        })
    }

    // Gives this run's glyph slice to `scratch` for a later run of its
    // length, when nothing else holds it.
    pub(crate) fn give_back(self, scratch: &mut RunGlyphScratch) {
        scratch.spare_glyphs.give(self.glyphs);
    }

    /// Each glyph's box and color.
    pub(crate) fn boxes(&self) -> impl ExactSizeIterator<Item = RunGlyphBox> + '_ {
        self.glyphs.iter().map(|glyph| RunGlyphBox {
            x: glyph.x,
            y: glyph.y,
            width: glyph.width,
            height: glyph.height,
            color: self.faces[usize::from(glyph.face)].color,
        })
    }

    /// Each glyph as the placement it was made from.
    pub(crate) fn iter(&self) -> impl ExactSizeIterator<Item = AtlasGlyph> + '_ {
        self.glyphs.iter().map(|glyph| {
            let face = self.faces[usize::from(glyph.face)];
            AtlasGlyph {
                key: GlyphMaskKey {
                    glyph_id: u32::from(glyph.glyph_id),
                    ..face.key
                },
                x: glyph.x,
                y: glyph.y,
                width: usize::from(glyph.width),
                height: usize::from(glyph.height),
                color: face.color,
            }
        })
    }
}

#[cfg(test)]
#[path = "tests/glyph_run_tests.rs"]
mod tests;