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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::metrics::CharMetrics;
use crate::*;
use ab_glyph_rasterizer::{Point as AbPoint, Rasterizer};
use pathfinder_content::outline::{Contour, ContourIterFlags, Outline};
use pathfinder_content::stroke::{LineCap, LineJoin, OutlineStrokeToFill, StrokeStyle};
use pathfinder_geometry::vector::Vector2F;
use ttf_parser::{OutlineBuilder, Rect};
use usvg::PathData;

impl Font {
    /// Output the outline instructions of a glyph
    pub fn outline(&self, c: char) -> Option<(Glyph, Outline)> {
        #[cfg(not(wasm))]
        self.load().ok()?;
        let mut builder = PathBuilder::new();
        let f = self.face.load();
        let f = f.as_ref().as_ref().unwrap();
        let f = f.borrow_face();
        let CharMetrics {
            glyph_id,
            bbox,
            advanced_x,
            units,
            ..
        } = self.measure_char(c)?;
        let outline = f.outline_glyph(glyph_id, &mut builder).unwrap_or(bbox);
        builder.finish();
        let glyph = Glyph {
            units: units as u16,
            path: builder.path,
            bbox: outline,
            advanced_x,
        };
        Some((glyph, builder.outline))
    }

    /// Rasterize the outline of a glyph for a certain font_size, and a possible
    /// stroke. This method is costy
    pub fn bitmap(&self, c: char, font_size: f32, stroke_width: f32) -> Option<GlyphBitmap> {
        if !self.has_glyph(c) {
            return None;
        }
        #[cfg(not(wasm))]
        self.load().ok()?;

        let f = self.face.load();
        let f = f.as_ref().as_ref().unwrap();
        let f = f.borrow_face();
        let a = f.ascender();
        let d = f.descender();
        let units = f.units_per_em() as f32;
        let factor = font_size / units;
        let (glyph, outline) = self.outline(c)?;
        let advanced_x = glyph.advanced_x as f32 * factor;
        let mut width =
            (glyph.bbox.x_max as f32 * factor).ceil() - (glyph.bbox.x_min as f32 * factor).floor();
        if width == 0.0 {
            width = advanced_x;
        }
        if width == 0.0 {
            width = font_size;
        }
        let mut height =
            (glyph.bbox.y_max as f32 * factor).ceil() - (glyph.bbox.y_min as f32 * factor).floor();

        // try to render stroke
        let stroke_bitmap = if stroke_width > 0.0 {
            let mut filler = OutlineStrokeToFill::new(
                &outline,
                StrokeStyle {
                    line_width: stroke_width / factor,
                    line_cap: LineCap::default(),
                    line_join: LineJoin::default(),
                },
            );
            filler.offset();
            let outline = filler.into_outline();
            let bounds = outline.bounds();
            let width = (bounds.max_x() * factor).ceil() - (bounds.min_x() * factor).floor();
            let height = (bounds.max_y() * factor).ceil() - (bounds.min_y() * factor).floor();
            let mut ras = FontkitRas {
                ras: Rasterizer::new(width as usize, height as usize),
                factor,
                x_min: (bounds.origin_x() * factor).floor(),
                y_max: ((bounds.size().y() + bounds.origin_y()) * factor).ceil(),
                prev: None,
                start: None,
            };
            ras.load_outline(outline);
            let mut bitmap = vec![0_u8; width as usize * height as usize];
            ras.ras.for_each_pixel_2d(|x, y, alpha| {
                if x < width as u32 && y < height as u32 {
                    bitmap[((height as u32 - y - 1) * width as u32 + x) as usize] =
                        (alpha * 255.0) as u8;
                }
            });
            Some((bitmap, width as u32))
        } else {
            None
        };
        width = width.ceil();
        height = height.ceil();

        let mut ras = FontkitRas {
            ras: Rasterizer::new(width as usize, height as usize),
            factor,
            x_min: (glyph.bbox.x_min as f32 * factor).floor(),
            y_max: (glyph.bbox.y_max as f32 * factor).ceil(),
            prev: None,
            start: None,
        };
        ras.load_outline(outline);
        let mut bitmap = vec![0_u8; width as usize * height as usize];
        ras.ras.for_each_pixel_2d(|x, y, alpha| {
            if x < width as u32 && y < height as u32 {
                bitmap[((height as u32 - y - 1) * width as u32 + x) as usize] =
                    (alpha * 255.0) as u8;
            }
        });

        Some(GlyphBitmap {
            width: width as u16,
            bbox: glyph.bbox,
            factor,
            ascender: a as f32 * factor,
            descender: d as f32 * factor,
            advanced_x,
            bitmap,
            stroke_bitmap,
        })
    }
}

struct PathBuilder {
    path: PathData,
    outline: Outline,
    contour: Contour,
}

impl PathBuilder {
    pub fn new() -> Self {
        PathBuilder {
            path: PathData::default(),
            outline: Outline::new(),
            contour: Contour::new(),
        }
    }

    pub fn finish(&mut self) {
        if !self.contour.is_empty() {
            self.outline
                .push_contour(std::mem::replace(&mut self.contour, Contour::new()));
        }
    }
}

impl OutlineBuilder for PathBuilder {
    fn move_to(&mut self, x: f32, y: f32) {
        self.path.push_move_to(x as f64, y as f64);
        let mut c = Contour::new();
        c.push_endpoint(Vector2F::new(x, y));
        self.contour = c;
    }

    fn line_to(&mut self, x: f32, y: f32) {
        self.contour.push_endpoint(Vector2F::new(x, y));
        self.path.push_line_to(x as f64, y as f64);
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        self.contour
            .push_quadratic(Vector2F::new(x1, y1), Vector2F::new(x, y));
        self.path
            .push_quad_to(x1 as f64, y1 as f64, x as f64, y as f64);
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        self.contour.push_cubic(
            Vector2F::new(x1, y1),
            Vector2F::new(x2, y2),
            Vector2F::new(x, y),
        );
        self.path.push_curve_to(
            x1 as f64, y1 as f64, x2 as f64, y2 as f64, x as f64, y as f64,
        );
    }

    fn close(&mut self) {
        self.contour.close();
        let c = std::mem::replace(&mut self.contour, Contour::new());
        self.outline.push_contour(c);
        self.path.push_close_path();
    }
}

struct FontkitRas {
    ras: Rasterizer,
    factor: f32,
    x_min: f32,
    y_max: f32,
    prev: Option<AbPoint>,
    start: Option<AbPoint>,
}

impl FontkitRas {
    fn load_outline(&mut self, outline: Outline) {
        for contour in outline.into_contours() {
            let mut started = false;
            for segment in contour.iter(ContourIterFlags::IGNORE_CLOSE_SEGMENT) {
                if !started {
                    let start = segment.baseline.from();
                    self.move_to(start.x(), start.y());
                    started = true;
                }
                let to = segment.baseline.to();
                if segment.is_line() {
                    self.line_to(to.x(), to.y());
                } else if segment.is_quadratic() {
                    let ctrl = segment.ctrl.from();
                    self.quad_to(ctrl.x(), ctrl.y(), to.x(), to.y());
                } else if segment.is_cubic() {
                    let ctrl1 = segment.ctrl.from();
                    let ctrl2 = segment.ctrl.to();
                    self.curve_to(ctrl1.x(), ctrl1.y(), ctrl2.x(), ctrl2.y(), to.x(), to.y());
                }
            }
            if contour.is_closed() {
                self.close();
            }
        }
    }
}

impl OutlineBuilder for FontkitRas {
    fn move_to(&mut self, x: f32, y: f32) {
        let p = AbPoint {
            x: x * self.factor - self.x_min,
            y: self.y_max - y * self.factor,
        };
        self.prev = Some(p);
        self.start = Some(p);
    }

    fn line_to(&mut self, x: f32, y: f32) {
        let to = AbPoint {
            x: x * self.factor - self.x_min,
            y: self.y_max - y * self.factor,
        };
        if let Some(prev) = self.prev.take() {
            self.ras.draw_line(prev, to);
        }
        self.prev = Some(to);
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        let to = AbPoint {
            x: x * self.factor - self.x_min,
            y: self.y_max - y * self.factor,
        };
        let c = AbPoint {
            x: x1 * self.factor - self.x_min,
            y: self.y_max - y1 * self.factor,
        };
        if let Some(prev) = self.prev.take() {
            self.ras.draw_quad(prev, c, to);
        }
        self.prev = Some(to);
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        let to = AbPoint {
            x: x * self.factor - self.x_min,
            y: self.y_max - y * self.factor,
        };

        let c1 = AbPoint {
            x: x1 * self.factor - self.x_min,
            y: self.y_max - y1 * self.factor,
        };
        let c2 = AbPoint {
            x: x2 * self.factor - self.x_min,
            y: self.y_max - y2 * self.factor,
        };
        if let Some(prev) = self.prev.take() {
            self.ras.draw_cubic(prev, c1, c2, to);
        }
        self.prev = Some(to);
    }

    fn close(&mut self) {
        if let (Some(a), Some(b)) = (self.start.take(), self.prev.take()) {
            self.ras.draw_line(b, a);
        }
    }
}

/// The outline of a glyph, with some metrics data
pub struct Glyph {
    pub units: u16,
    pub path: PathData,
    pub bbox: Rect,
    pub advanced_x: u16,
}

/// Rasterized data of a [Glyph](Glyph)
#[derive(Clone, Debug)]
pub struct GlyphBitmap {
    width: u16,
    bbox: ttf_parser::Rect,
    factor: f32,
    pub ascender: f32,
    pub descender: f32,
    pub advanced_x: f32,
    pub bitmap: Vec<u8>,
    pub stroke_bitmap: Option<(Vec<u8>, u32)>,
}

impl GlyphBitmap {
    pub fn width(&self) -> u32 {
        self.width as u32
    }

    pub fn height(&self) -> u32 {
        self.bitmap.len() as u32 / self.width as u32
    }

    pub fn x_min(&self) -> f32 {
        self.bbox.x_min as f32 * self.factor
    }

    pub fn y_min(&self) -> f32 {
        self.bbox.y_min as f32 * self.factor
    }

    pub fn x_max(&self) -> f32 {
        self.bbox.x_max as f32 * self.factor
    }

    pub fn y_max(&self) -> f32 {
        self.bbox.y_max as f32 * self.factor
    }
}