Skip to main content

yog_book/
font.rs

1//! Custom TTF/OTF font support for book rendering.
2//! Uses `fontdue` to rasterize glyphs into a packed atlas texture (RGBA8).
3
4use std::collections::HashMap;
5use serde::{Deserialize, Serialize};
6
7/// Reference to a registered font — serializable, stored in `BookPage::CustomText`.
8/// The actual TTF bytes live in `BookFontRegistry` (registered separately).
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct BookFont {
11    /// Font registry ID (e.g. `"hexcasting:display_font"`).
12    pub font_id: String,
13    /// Render size in pixels (e.g. 14.0).
14    pub size_px: f32,
15}
16
17/// Global font data registry — maps font_id → raw TTF/OTF bytes.
18/// Populated independently of book JSON, before the first render.
19#[derive(Debug, Default)]
20pub struct BookFontRegistry {
21    pub fonts: HashMap<String, Vec<u8>>,
22}
23
24impl BookFontRegistry {
25    pub fn register(&mut self, id: impl Into<String>, ttf: Vec<u8>) {
26        self.fonts.insert(id.into(), ttf);
27    }
28    pub fn get(&self, id: &str) -> Option<&[u8]> {
29        self.fonts.get(id).map(Vec::as_slice)
30    }
31}
32
33/// UV coordinates + metrics for one glyph in the atlas.
34#[derive(Debug, Clone)]
35pub struct GlyphInfo {
36    pub u0:      f32,
37    pub v0:      f32,
38    pub u1:      f32,
39    pub v1:      f32,
40    pub width:   u32,
41    pub height:  u32,
42    pub xoff:    f32,
43    pub yoff:    f32,
44    pub advance: f32,
45}
46
47/// Built glyph atlas: RGBA8 pixel buffer + glyph map.
48/// Upload `pixels` to GPU via `ctx.create_texture_rgba()` to get a GL handle.
49pub struct FontAtlas {
50    pub pixels:     Vec<u8>,
51    pub atlas_size: u32,
52    pub glyphs:     HashMap<char, GlyphInfo>,
53    pub line_height: f32,
54}
55
56/// ASCII + common Latin Extended-A charset for the atlas.
57const ATLAS_CHARS: &str =
58    " !\"#$%&'()*+,-./0123456789:;<=>?@\
59     ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`\
60     abcdefghijklmnopqrstuvwxyz{|}~\
61     ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞß\
62     àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ";
63
64impl FontAtlas {
65    /// Build a glyph atlas from raw TTF/OTF bytes at the given pixel size.
66    /// Returns `None` if the `fonts` feature is not enabled or on parse error.
67    pub fn build(font_data: &[u8], size_px: f32) -> Option<FontAtlas> {
68        Self::build_impl(font_data, size_px)
69    }
70
71    #[cfg(feature = "fonts")]
72    fn build_impl(font_data: &[u8], size_px: f32) -> Option<FontAtlas> {
73        // Requires `fontdue = "0.8"` in Cargo.toml + feature "fonts" enabled.
74        let font = ::fontdue::Font::from_bytes(
75            font_data,
76            ::fontdue::FontSettings { scale: size_px, ..Default::default() },
77        ).ok()?;
78
79        const ATLAS_W: u32 = 512;
80        const ATLAS_H: u32 = 512;
81        let mut pixels = vec![0u8; (ATLAS_W * ATLAS_H * 4) as usize];
82        let mut glyphs = HashMap::new();
83
84        let row_h = size_px.ceil() as u32 + 2;
85        let mut cx: u32 = 1;
86        let mut cy: u32 = 1;
87
88        for ch in ATLAS_CHARS.chars() {
89            let (metrics, bitmap) = font.rasterize(ch, size_px);
90            let gw = metrics.width as u32;
91            let gh = metrics.height as u32;
92
93            if gw == 0 || gh == 0 {
94                glyphs.insert(ch, GlyphInfo {
95                    u0: 0.0, v0: 0.0, u1: 0.0, v1: 0.0,
96                    width: 0, height: 0,
97                    xoff: metrics.xmin as f32, yoff: metrics.ymin as f32,
98                    advance: metrics.advance_width,
99                });
100                continue;
101            }
102            if cx + gw + 1 >= ATLAS_W { cx = 1; cy += row_h; }
103            if cy + gh + 1 >= ATLAS_H { break; }
104
105            for row in 0..gh {
106                for col in 0..gw {
107                    let alpha = bitmap[(row * gw + col) as usize];
108                    let idx = ((cy + row) * ATLAS_W + (cx + col)) as usize * 4;
109                    pixels[idx]     = 255;
110                    pixels[idx + 1] = 255;
111                    pixels[idx + 2] = 255;
112                    pixels[idx + 3] = alpha;
113                }
114            }
115
116            glyphs.insert(ch, GlyphInfo {
117                u0:      cx as f32 / ATLAS_W as f32,
118                v0:      cy as f32 / ATLAS_H as f32,
119                u1:      (cx + gw) as f32 / ATLAS_W as f32,
120                v1:      (cy + gh) as f32 / ATLAS_H as f32,
121                width: gw, height: gh,
122                xoff: metrics.xmin as f32, yoff: metrics.ymin as f32,
123                advance: metrics.advance_width,
124            });
125            cx += gw + 1;
126        }
127
128        Some(FontAtlas { pixels, atlas_size: ATLAS_W, glyphs, line_height: size_px * 1.2 })
129    }
130
131    #[cfg(not(feature = "fonts"))]
132    fn build_impl(_font_data: &[u8], _size_px: f32) -> Option<FontAtlas> { None }
133
134    /// Measure the width of a text string in pixels.
135    pub fn measure_width(&self, text: &str) -> f32 {
136        text.chars().map(|c| self.glyphs.get(&c).map(|g| g.advance).unwrap_or(0.0)).sum()
137    }
138}