spitfire_fontdue/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use bytemuck::Pod;
use etagere::{euclid::default::Rect, size2, AtlasAllocator};
use fontdue::{
    layout::{GlyphPosition, GlyphRasterConfig, Layout},
    Font,
};
use spitfire_core::VertexStream;
use std::{
    collections::{hash_map::Entry, HashMap},
    marker::PhantomData,
};

pub trait TextVertex<UD: Copy> {
    fn apply(&mut self, position: [f32; 2], tex_coord: [f32; 3], user_data: UD);
}

#[derive(Debug, Default, Clone, Copy)]
pub struct TextRendererGlyph {
    pub page: usize,
    pub rectangle: Rect<u32>,
}

pub struct TextRendererUnpacked<UD: Copy> {
    pub glyphs: HashMap<GlyphRasterConfig, TextRendererGlyph>,
    pub atlas_size: [usize; 3],
    pub image: Vec<u8>,
    pub renderables: Vec<GlyphPosition<UD>>,
}

#[derive(Clone)]
pub struct TextRenderer<UD: Copy = ()> {
    pub renderables_resize: usize,
    used_glyphs: HashMap<GlyphRasterConfig, TextRendererGlyph>,
    atlas_size: [usize; 3],
    image: Vec<u8>,
    atlases: Vec<AtlasAllocator>,
    ready_to_render: Vec<GlyphPosition<UD>>,
    _phantom: PhantomData<fn() -> UD>,
}

impl<UD: Copy> Default for TextRenderer<UD> {
    fn default() -> Self {
        Self::new(1024, 1024)
    }
}

impl<UD: Copy> TextRenderer<UD> {
    pub fn new(width: usize, height: usize) -> Self {
        Self {
            renderables_resize: 1024,
            used_glyphs: Default::default(),
            atlas_size: [width, height, 0],
            image: Default::default(),
            atlases: Default::default(),
            ready_to_render: Default::default(),
            _phantom: Default::default(),
        }
    }

    pub fn clear(&mut self) {
        self.used_glyphs.clear();
        self.atlas_size[2] = 0;
        self.image.clear();
        self.atlases.clear();
        self.ready_to_render.clear();
    }

    pub fn include(&mut self, fonts: &[Font], layout: &Layout<UD>) {
        for glyph in layout.glyphs() {
            if glyph.char_data.rasterize() {
                if self.ready_to_render.len() == self.ready_to_render.capacity() {
                    self.ready_to_render.reserve(self.renderables_resize);
                }
                self.ready_to_render.push(*glyph);
            }
            if let Entry::Vacant(entry) = self.used_glyphs.entry(glyph.key) {
                let font = &fonts[glyph.font_index];
                let (metrics, coverage) = font.rasterize_config(glyph.key);
                if glyph.char_data.rasterize() {
                    let allocation = self
                        .atlases
                        .iter_mut()
                        .enumerate()
                        .find_map(|(page, atlas)| {
                            Some((
                                page,
                                atlas
                                    .allocate(size2(
                                        metrics.width as i32 + 1,
                                        metrics.height as i32 + 1,
                                    ))?
                                    .rectangle
                                    .to_rect()
                                    .origin
                                    .to_u32(),
                            ))
                        })
                        .or_else(|| {
                            let w = self.atlas_size[0];
                            let h = self.atlas_size[1];
                            let mut atlas = AtlasAllocator::new(size2(w as _, h as _));
                            let page = self.atlases.len();
                            let origin = atlas
                                .allocate(size2(
                                    metrics.width as i32 + 1,
                                    metrics.height as i32 + 1,
                                ))?
                                .rectangle
                                .to_rect()
                                .origin
                                .to_u32();
                            self.atlases.push(atlas);
                            self.atlas_size[2] += 1;
                            let [w, h, d] = self.atlas_size;
                            self.image.resize(w * h * d, 0);
                            Some((page, origin))
                        });
                    if let Some((page, origin)) = allocation {
                        let [w, h, _] = self.atlas_size;
                        for (index, value) in coverage.iter().enumerate() {
                            let x = origin.x as usize + index % metrics.width;
                            let y = origin.y as usize + index / metrics.width;
                            let index = page * w * h + y * w + x;
                            self.image[index] = *value;
                        }
                        entry.insert(TextRendererGlyph {
                            page,
                            rectangle: Rect::new(
                                origin,
                                [metrics.width as _, metrics.height as _].into(),
                            ),
                        });
                    }
                }
            }
        }
    }

    pub fn include_consumed(
        &mut self,
        fonts: &[Font],
        layout: &Layout<UD>,
    ) -> impl Iterator<Item = (GlyphPosition<UD>, TextRendererGlyph)> + '_ {
        self.include(fonts, layout);
        self.consume_renderables()
    }

    pub fn glyph(&self, key: &GlyphRasterConfig) -> Option<TextRendererGlyph> {
        self.used_glyphs.get(key).copied()
    }

    pub fn consume_renderables(
        &mut self,
    ) -> impl Iterator<Item = (GlyphPosition<UD>, TextRendererGlyph)> + '_ {
        self.ready_to_render
            .drain(..)
            .filter_map(|glyph| Some((glyph, *self.used_glyphs.get(&glyph.key)?)))
    }

    pub fn image(&self) -> &[u8] {
        &self.image
    }

    pub fn atlas_size(&self) -> [usize; 3] {
        self.atlas_size
    }

    pub fn into_image(self) -> (Vec<u8>, [usize; 3]) {
        (self.image, self.atlas_size)
    }

    pub fn into_inner(self) -> TextRendererUnpacked<UD> {
        TextRendererUnpacked {
            glyphs: self.used_glyphs,
            atlas_size: self.atlas_size,
            image: self.image,
            renderables: self.ready_to_render,
        }
    }

    pub fn render_to_stream<V, B>(&mut self, stream: &mut VertexStream<V, B>)
    where
        V: TextVertex<UD> + Pod + Default,
    {
        let [w, h, _] = self.atlas_size;
        let w = w as f32;
        let h = h as f32;
        for glyph in self.ready_to_render.drain(..) {
            if let Some(data) = self.used_glyphs.get(&glyph.key) {
                let mut a = V::default();
                let mut b = V::default();
                let mut c = V::default();
                let mut d = V::default();
                a.apply(
                    [glyph.x, glyph.y],
                    [
                        data.rectangle.min_x() as f32 / w,
                        data.rectangle.min_y() as f32 / h,
                        data.page as f32,
                    ],
                    glyph.user_data,
                );
                b.apply(
                    [glyph.x + glyph.width as f32, glyph.y],
                    [
                        data.rectangle.max_x() as f32 / w,
                        data.rectangle.min_y() as f32 / h,
                        data.page as f32,
                    ],
                    glyph.user_data,
                );
                c.apply(
                    [glyph.x + glyph.width as f32, glyph.y + glyph.height as f32],
                    [
                        data.rectangle.max_x() as f32 / w,
                        data.rectangle.max_y() as f32 / h,
                        data.page as f32,
                    ],
                    glyph.user_data,
                );
                d.apply(
                    [glyph.x, glyph.y + glyph.height as f32],
                    [
                        data.rectangle.min_x() as f32 / w,
                        data.rectangle.max_y() as f32 / h,
                        data.page as f32,
                    ],
                    glyph.user_data,
                );
                stream.quad([a, b, c, d]);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::TextRenderer;
    use fontdue::{
        layout::{CoordinateSystem, Layout, TextStyle},
        Font,
    };
    use image::RgbImage;

    #[test]
    fn test_text_renderer() {
        let text = include_str!("../../../resources/text.txt");
        let font = include_bytes!("../../../resources/Roboto-Regular.ttf") as &[_];
        let font = Font::from_bytes(font, Default::default()).unwrap();
        let fonts = [font];
        let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
        let mut renderer = TextRenderer::new(256, 256);

        for line in text.lines() {
            layout.append(&fonts, &TextStyle::new(line, 32.0, 0));
        }

        renderer.include(&fonts, &layout);
        let (image, [width, height, _]) = renderer.into_image();
        let image = RgbImage::from_vec(
            width as _,
            height as _,
            image
                .into_iter()
                .flat_map(|value| [value, value, value])
                .collect(),
        )
        .unwrap();
        image.save("../../resources/test.png").unwrap();
    }
}