rocketsplash-rt 0.3.0

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: 2.0.0</VERS>
// <WCTX>Foundation crates 0.3.0 (RELEASE_PLAN D2 typed errors, F2 cycle-break)</WCTX>
// <CLOG>2.0.0: structural checks now live in GlyphData::validate (typed FormatError); this keeps only the allocation-tracked conversion; direct sibling import. 1.0.0: extracted from fnc_load_font.</CLOG>

use std::mem::size_of;

use rocketsplash_formats::{AllocationTracker, GlyphData};

use super::cls_font::RuntimeGlyph;
use crate::Error;

pub(super) fn convert_glyph(
    tracker: &mut AllocationTracker,
    glyph: GlyphData,
) -> Result<RuntimeGlyph, Error> {
    // Structure (dims, char count, opacity length) was validated by
    // read_font_atlas; this pass only budgets the runtime allocation.
    glyph.validate().map_err(Error::from)?;

    let chars: Vec<char> = glyph.chars.chars().collect();
    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)?;

    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: 2.0.0</VERS>