rocketsplash-rt 0.2.2

Runtime library for loading and rendering Rocketsplash assets (.rst, .rsf)
Documentation
// <FILE>crates/rocketsplash-rt/src/font/fnc_convert_glyph.rs</FILE>
// <DESC>Convert serialized glyph data into runtime glyphs.</DESC>
// <VERS>VERSION: 1.0.0</VERS>
// <WCTX>Public release refactor audit</WCTX>
// <CLOG>Extract glyph conversion from fnc_load_font</CLOG>

use std::mem::size_of;

use rocketsplash_formats::{AllocationTracker, GlyphData};

use crate::font::RuntimeGlyph;
use crate::Error;

pub(super) fn convert_glyph(
    tracker: &mut AllocationTracker,
    glyph: GlyphData,
) -> Result<RuntimeGlyph, Error> {
    let cell_count = (glyph.width as usize)
        .checked_mul(glyph.height as usize)
        .ok_or_else(|| Error::InvalidFormat {
            message: format!("Glyph dimensions {}x{} overflow", glyph.width, glyph.height),
        })?;

    let chars: Vec<char> = glyph.chars.chars().collect();
    if chars.len() != cell_count {
        return Err(Error::InvalidFormat {
            message: format!(
                "Glyph char count {} does not match {}x{}",
                chars.len(),
                glyph.width,
                glyph.height
            ),
        });
    }
    if let Some(ref opacity) = glyph.opacity {
        if opacity.len() != cell_count {
            return Err(Error::InvalidFormat {
                message: format!(
                    "Glyph opacity length {} does not match {}x{}",
                    opacity.len(),
                    glyph.width,
                    glyph.height
                ),
            });
        }
    }

    let char_bytes = chars.len() * size_of::<char>();
    let opacity_bytes = glyph.opacity.as_ref().map(|o| o.len()).unwrap_or(0);
    tracker
        .reserve(char_bytes + opacity_bytes)
        .map_err(|message| Error::InvalidFormat { message })?;

    Ok(RuntimeGlyph {
        width: glyph.width as usize,
        height: glyph.height as usize,
        chars,
        opacity: glyph.opacity,
    })
}

// <FILE>crates/rocketsplash-rt/src/font/fnc_convert_glyph.rs</FILE>
// <VERS>END OF VERSION: 1.0.0</VERS>