Skip to main content

concinnity_core/defaults/
font.rs

1// The font the injected HUD chips and the loading label draw with: the face
2// bundled in the binary, rasterised at start and appended to the world's font
3// table. Appending leaves every handle the build assigned where it was.
4
5use crate::bake;
6use crate::ecs::{FontHandle, PipelineContext};
7use crate::resource::{FontTable, ResourceEntry};
8use crate::result::CnResult;
9
10/// Pixel size the injected HUD face is rasterised at. Chips draw it minified,
11/// so the atlas is supersampled from here rather than authored larger.
12pub const HUD_FONT_SIZE_PX: u32 = 20;
13
14/// The face baked for this world, so a second caller shares it rather than
15/// paying for the atlas again.
16#[derive(Debug, Clone, Copy)]
17pub struct HudFont(pub FontHandle);
18
19/// The font the engine's own HUD text draws with, baked into `ctx`'s font table
20/// on the first call and returned as it stands afterwards.
21///
22/// A host that draws HUD text of its own before the world starts (the editor's
23/// panels) reaches it here, so one atlas serves both.
24pub fn hud_font(ctx: &mut PipelineContext) -> Result<FontHandle, CnResult> {
25    if let Some(HudFont(handle)) = ctx.resource::<HudFont>().copied() {
26        return Ok(handle);
27    }
28    let payload = bake::font::compile(
29        bake::font::BUILTIN_FONT_BYTES,
30        HUD_FONT_SIZE_PX,
31        bake::font::BUILTIN_FONT_FILE,
32    )
33    .map_err(|_| CnResult::InvalidArgument)?;
34    // The table is created when the world carries none: one assembled in code
35    // rather than loaded from a blob.
36    if ctx.resource::<FontTable>().is_none() {
37        ctx.insert_resource(FontTable::default());
38    }
39    let table = ctx
40        .resource_mut::<FontTable>()
41        .ok_or(CnResult::InvalidState)?;
42    let handle = FontHandle(table.append(ResourceEntry::baked(payload)));
43    ctx.insert_resource(HudFont(handle));
44    Ok(handle)
45}