1use ab_glyph::{point, Font, FontVec, Glyph, Point, PxScale, ScaleFont};
5use image::{DynamicImage, ImageBuffer, Rgba};
6
7pub struct FontRenderer {
8 font_path: String,
9 font: Option<FontVec>,
10}
11
12impl FontRenderer {
13 pub fn new(font_path: String) -> Self {
14 let mut font_renderer = FontRenderer {
15 font_path: "".to_string(),
16 font: None,
17 };
18 font_renderer.load_font(font_path);
19
20 font_renderer
21 }
22
23 pub fn load_font(&mut self, font_path: String) {
24 println!("load_font: {}", font_path);
25 let font_path = std::env::current_dir().unwrap().join(font_path);
26 let data = std::fs::read(&font_path).unwrap();
27
28 match &mut self.font {
29 Some(_font) => {
30 println!("font already loaded");
31 }
32 None => {
33 println!("loading font...");
34 self.font = Some(FontVec::try_from_vec(data).unwrap_or_else(|_| {
35 panic!("error constructing a Font from data at {:?}", font_path);
36 }));
37 if let Some(name) = font_path.file_name().and_then(|n| n.to_str()) {
38 eprintln!("Using font: {name}");
39 }
40
41 self.font_path = font_path.to_str().unwrap().to_string();
42 }
43 }
44 }
45
46 pub fn render(&mut self, text: &str) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
47 let scale = PxScale::from(45.0);
49
50 let font = self.font.as_ref().unwrap();
51 let scaled_font = font.as_scaled(scale);
52
53 let mut glyphs = Vec::new();
54 self.layout_paragraph(scaled_font, point(20.0, 20.0), 9999.0, text, &mut glyphs);
55
56 let colour = (255, 0, 0);
58
59 let glyphs_height = scaled_font.height().ceil() as u32;
61 let glyphs_width = {
62 let min_x = glyphs.first().unwrap().position.x;
63 let last_glyph = glyphs.last().unwrap();
64 let max_x = last_glyph.position.x + scaled_font.h_advance(last_glyph.id);
65 (max_x - min_x).ceil() as u32
66 };
67
68 println!(
69 "glyphs_width: {}, glyphs_height: {}",
70 glyphs_width, glyphs_height
71 );
72
73 let mut image = DynamicImage::new_rgba8(glyphs_width + 40, glyphs_height + 40).to_rgba8();
75
76 for glyph in glyphs {
78 if let Some(outlined) = scaled_font.outline_glyph(glyph) {
79 let bounds = outlined.px_bounds();
80 outlined.draw(|x, y, v| {
82 let px = image.get_pixel_mut(x + bounds.min.x as u32, y + bounds.min.y as u32);
84 *px = Rgba([
86 colour.0,
87 colour.1,
88 colour.2,
89 px.0[3].saturating_add((v * 255.0) as u8),
90 ]);
91 });
92 }
93 }
94
95 image
97 }
98
99 pub fn layout_paragraph<F, SF>(
100 &self,
101 font: SF,
102 position: Point,
103 max_width: f32,
104 text: &str,
105 target: &mut Vec<Glyph>,
106 ) where
107 F: Font,
108 SF: ScaleFont<F>,
109 {
110 let v_advance = font.height() + font.line_gap();
111 let mut caret = position + point(0.0, font.ascent());
112 let mut last_glyph: Option<Glyph> = None;
113 for c in text.chars() {
114 if c.is_control() {
115 if c == '\n' {
116 caret = point(position.x, caret.y + v_advance);
117 last_glyph = None;
118 }
119 continue;
120 }
121 let mut glyph = font.scaled_glyph(c);
122 if let Some(previous) = last_glyph.take() {
123 caret.x += font.kern(previous.id, glyph.id);
124 }
125 glyph.position = caret;
126
127 last_glyph = Some(glyph.clone());
128 caret.x += font.h_advance(glyph.id);
129
130 if !c.is_whitespace() && caret.x > position.x + max_width {
131 caret = Point {
132 x: position.x,
133 y: caret.y + v_advance,
134 };
135 glyph.position = caret;
136 last_glyph = None;
137 }
138
139 target.push(glyph);
140 }
141 }
142}