1use std::collections::HashMap;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct BookFont {
11 pub font_id: String,
13 pub size_px: f32,
15}
16
17#[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#[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
47pub 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#[cfg(feature = "fonts")]
58const ATLAS_CHARS: &str =
59 " !\"#$%&'()*+,-./0123456789:;<=>?@\
60 ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`\
61 abcdefghijklmnopqrstuvwxyz{|}~\
62 ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞß\
63 àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ";
64
65impl FontAtlas {
66 pub fn build(font_data: &[u8], size_px: f32) -> Option<FontAtlas> {
69 Self::build_impl(font_data, size_px)
70 }
71
72 #[cfg(feature = "fonts")]
73 fn build_impl(font_data: &[u8], size_px: f32) -> Option<FontAtlas> {
74 let font = ::fontdue::Font::from_bytes(
76 font_data,
77 ::fontdue::FontSettings { scale: size_px, ..Default::default() },
78 ).ok()?;
79
80 const ATLAS_W: u32 = 512;
81 const ATLAS_H: u32 = 512;
82 let mut pixels = vec![0u8; (ATLAS_W * ATLAS_H * 4) as usize];
83 let mut glyphs = HashMap::new();
84
85 let row_h = size_px.ceil() as u32 + 2;
86 let mut cx: u32 = 1;
87 let mut cy: u32 = 1;
88
89 for ch in ATLAS_CHARS.chars() {
90 let (metrics, bitmap) = font.rasterize(ch, size_px);
91 let gw = metrics.width as u32;
92 let gh = metrics.height as u32;
93
94 if gw == 0 || gh == 0 {
95 glyphs.insert(ch, GlyphInfo {
96 u0: 0.0, v0: 0.0, u1: 0.0, v1: 0.0,
97 width: 0, height: 0,
98 xoff: metrics.xmin as f32, yoff: metrics.ymin as f32,
99 advance: metrics.advance_width,
100 });
101 continue;
102 }
103 if cx + gw + 1 >= ATLAS_W { cx = 1; cy += row_h; }
104 if cy + gh + 1 >= ATLAS_H { break; }
105
106 for row in 0..gh {
107 for col in 0..gw {
108 let alpha = bitmap[(row * gw + col) as usize];
109 let idx = ((cy + row) * ATLAS_W + (cx + col)) as usize * 4;
110 pixels[idx] = 255;
111 pixels[idx + 1] = 255;
112 pixels[idx + 2] = 255;
113 pixels[idx + 3] = alpha;
114 }
115 }
116
117 glyphs.insert(ch, GlyphInfo {
118 u0: cx as f32 / ATLAS_W as f32,
119 v0: cy as f32 / ATLAS_H as f32,
120 u1: (cx + gw) as f32 / ATLAS_W as f32,
121 v1: (cy + gh) as f32 / ATLAS_H as f32,
122 width: gw, height: gh,
123 xoff: metrics.xmin as f32, yoff: metrics.ymin as f32,
124 advance: metrics.advance_width,
125 });
126 cx += gw + 1;
127 }
128
129 Some(FontAtlas { pixels, atlas_size: ATLAS_W, glyphs, line_height: size_px * 1.2 })
130 }
131
132 #[cfg(not(feature = "fonts"))]
133 fn build_impl(_font_data: &[u8], _size_px: f32) -> Option<FontAtlas> { None }
134
135 pub fn measure_width(&self, text: &str) -> f32 {
137 text.chars().map(|c| self.glyphs.get(&c).map(|g| g.advance).unwrap_or(0.0)).sum()
138 }
139}