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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use riddle_common::Color;
use riddle_image::{packer::ImagePackerSizePolicy, Image, ImageError, ImagePacker};
use riddle_math::{Rect, SpacialNumericConversion, Vector2};
use std::collections::{HashMap, HashSet};

use crate::rusttype_ext::*;
use crate::*;

/// Represents an image font, which is an Image containing glyphs, glyph information
/// specifying which parts of the image map to which glyph, and layout information.
pub struct ImgFont {
	img: Image,
	glyphs: HashMap<char, ImgFontGlyph>,
	vertical_spacing: u32,
}

impl ImgFont {
	/// This represents the distance, in pixels, between the top of one line of text and the
	/// next line of text.
	///
	/// # Example
	///
	/// ```
	/// # use riddle_font::*;
	/// # fn main() -> Result<(), FontError> {
	/// # let font_bytes = include_bytes!("../../example_assets/Roboto-Regular.ttf");
	/// let ttf_font = TTFont::load(&font_bytes[..])?;
	/// let img_font = ImgFontGenerator::new("ABCDEF", 32).generate(&ttf_font)?;
	///
	/// assert!(img_font.vertical_spacing() > 0);
	/// # Ok(()) }
	/// ```
	pub fn vertical_spacing(&self) -> u32 {
		self.vertical_spacing
	}

	/// The image storing the glyph image data.
	///
	/// # Example
	///
	/// ```
	/// # use riddle_font::*;
	/// # fn main() -> Result<(), FontError> {
	/// # let font_bytes = include_bytes!("../../example_assets/Roboto-Regular.ttf");
	/// let ttf_font = TTFont::load(&font_bytes[..])?;
	/// let img_font = ImgFontGenerator::new("ABCDEF", 32).generate(&ttf_font)?;
	///
	/// assert!(img_font.image().width() > 0);
	/// # Ok(()) }
	/// ```
	pub fn image(&self) -> &Image {
		&self.img
	}

	/// The mapping from [`char`] to glyphs registered with the font.
	///
	/// # Example
	///
	/// ```
	/// # use riddle_font::*;
	/// # fn main() -> Result<(), FontError> {
	/// # let font_bytes = include_bytes!("../../example_assets/Roboto-Regular.ttf");
	/// let ttf_font = TTFont::load(&font_bytes[..])?;
	/// let img_font = ImgFontGenerator::new("ABCDEF", 32).generate(&ttf_font)?;
	///
	/// assert_eq!(6, img_font.glyphs().len());
	/// assert_eq!('A', img_font.glyphs().get(&'A').unwrap().character);
	/// # Ok(()) }
	/// ```
	pub fn glyphs(&self) -> &HashMap<char, ImgFontGlyph> {
		&self.glyphs
	}

	/// Render a string to a [`riddle_image::Image`] using the font at its native scale.
	///
	/// # Example
	///
	/// ```
	/// # use riddle_font::*;
	/// # fn main() -> Result<(), FontError> {
	/// # let font_bytes = include_bytes!("../../example_assets/Roboto-Regular.ttf");
	/// let ttf_font = TTFont::load(&font_bytes[..])?;
	/// let img_font = ImgFontGenerator::new("ABCDEF", 32).generate(&ttf_font)?;
	///
	/// let rendered_img = img_font.render_simple("AABCA")?;
	///
	/// assert_eq!(rendered_img.height(), img_font.vertical_spacing());
	/// assert!(rendered_img.width() > 0);
	/// # Ok(()) }
	/// ```
	pub fn render_simple(&self, text: &str) -> Result<Image> {
		let total_width = text
			.chars()
			.map(|c| {
				if let Some(glyph) = self.glyphs.get(&c) {
					glyph.horizontal_spacing
				} else {
					0
				}
			})
			.sum();

		let mut output_image = Image::new(total_width, self.vertical_spacing);
		let mut current_x = 0_u32;

		for c in text.chars() {
			if let Some(glyph) = self.glyphs.get(&c) {
				if let Some(source_rect) = &glyph.rect {
					output_image.blit_rect(
						&self.img,
						source_rect,
						Vector2::new(current_x as i32, 0) + glyph.placement_offset,
					);
				}
				current_x += glyph.horizontal_spacing;
			}
		}

		self.layout(text, |_, source_rect, location| {
			output_image.blit_rect(&self.img, source_rect, location.convert());
		});

		Ok(output_image)
	}

	/// Layout a string using the font, relative to a (0,0) point representing the top left of the
	/// layout space.
	///
	/// The callback fuction is invoked once for each character, providing the part of the image to
	/// be used, and the location to draw that image.
	///
	/// # Example
	///
	/// ```
	/// # use riddle_font::*;
	/// # fn main() -> Result<(), FontError> {
	/// # let font_bytes = include_bytes!("../../example_assets/Roboto-Regular.ttf");
	/// let ttf_font = TTFont::load(&font_bytes[..])?;
	/// let img_font = ImgFontGenerator::new("ABCDEF", 32).generate(&ttf_font)?;
	///
	/// let mut last_x = 0;
	/// let mut total_width = 0;
	/// let mut string = String::from("");
	///
	/// img_font.layout("AABCA", |c, rect, location| {
	///     assert!(location.x >= last_x);
	///     last_x = location.x;
	///     total_width += rect.dimensions.x;
	///     string += c.to_string().as_str();
	/// });
	///
	/// assert!(last_x > 0);
	/// assert!(total_width > 0);
	/// assert_eq!("AABCA", string);
	/// # Ok(()) }
	/// ```
	pub fn layout<F: FnMut(char, &Rect<u32>, Vector2<u32>)>(&self, text: &str, mut f: F) {
		let mut current_x = 0_u32;

		for c in text.chars() {
			if let Some(glyph) = self.glyphs.get(&c) {
				if let Some(source_rect) = &glyph.rect {
					let position: Vector2<u32> =
						Vector2::new(current_x, 0) + glyph.placement_offset.convert();
					f(c, &source_rect, position);
				}
				current_x += glyph.horizontal_spacing;
			}
		}
	}
}

/// Details describing a glyph stored in an image font
#[derive(Debug, Clone, Default)]
pub struct ImgFontGlyph {
	/// The character that the glyph maps to.
	pub character: char,

	/// The part of the imgfont that contains this character.
	pub rect: Option<Rect<u32>>,

	/// Offset relative to to the expected location the glyph should be placed, useful if the rect
	/// is a different size than the horizontal or vertical spacing of the glyph/font.
	pub placement_offset: Vector2<i32>,

	/// The horizontal spacing between the left of this glyph and the left of the next glyph
	pub horizontal_spacing: u32,
}

/// Layout will happen relative to a (0,0) point representing the top left of the layout space.
pub struct ImgFontLayoutRule {
	/// The maximum width the space.
	pub max_width: Option<u32>,
}

/// A utility to construct an [`ImgFont`] given an image and a collection of glyph data.
///
/// # Example
///
/// ```
/// # use riddle_font::*; use riddle_image::*; use riddle_math::*;
/// # fn main() -> Result<(), FontError> {
/// let img = Image::new(64, 64);
/// let mut img_font_builder = ImgFontBuilder::new();
/// img_font_builder.vertical_spacing(32);
///
/// img_font_builder.with_glyph(ImgFontGlyph {
///     character: 'a',
///     rect: Some(Rect::new([0,0], [32, 32])),
///     horizontal_spacing: 32,
///     .. Default::default()
/// });
///
/// let img_font = img_font_builder.build(img);
///
/// assert_eq!(128, img_font.render_simple("aaaa")?.width());
/// # Ok(()) }
/// ```
#[derive(Default, Debug, Clone)]
pub struct ImgFontBuilder {
	glyphs: Vec<ImgFontGlyph>,
	vertical_spacing: u32,
}

impl ImgFontBuilder {
	/// Construct a new builder.
	pub fn new() -> Self {
		Self::default()
	}

	/// Set the vertical spacing of the font. See [`ImgFont::vertical_spacing`].
	pub fn vertical_spacing(&mut self, spacing: u32) -> &mut Self {
		self.vertical_spacing = spacing;
		self
	}

	/// Register a glyph with the font. If more that one glyph is added with the same value for
	/// [`ImgFontGlyph::character`] the last one added will be the one that is respected.
	pub fn with_glyph(&mut self, glyph: ImgFontGlyph) -> &mut Self {
		self.glyphs.push(glyph);
		self
	}

	/// Take ownership of the given image, and finalize building of the [`ImgFont`].
	pub fn build(self, img: Image) -> ImgFont {
		ImgFont {
			img,
			glyphs: self
				.glyphs
				.iter()
				.map(|g| (g.character, g.clone()))
				.collect(),
			vertical_spacing: self.vertical_spacing,
		}
	}
}

/// A utility for generating [`ImgFont`]s from [`TTFont`]s.
///
/// # Example
///
///
/// ```
/// # use riddle_font::*;
/// # fn main() -> Result<(), FontError> {
/// # let font_bytes = include_bytes!("../../example_assets/Roboto-Regular.ttf");
/// let ttf_font = TTFont::load(&font_bytes[..])?;
///
/// let img_font = ImgFontGenerator::new("ABCDEF", 32).generate(&ttf_font)?;
///
/// assert_eq!(6, img_font.glyphs().len());
/// # Ok(()) }
/// ```
#[derive(Default, Clone)]
pub struct ImgFontGenerator {
	characters: HashSet<char>,
	pixel_height: u32,
	fore_color: Color<u8>,
	packing_size_policy: ImagePackerSizePolicy,
}

impl ImgFontGenerator {
	/// Create a new generator, specifying the set of characters to generate and the pixel height at
	/// which the font will be rendered.
	pub fn new(chars: &str, pixel_height: u32) -> Self {
		Self {
			characters: chars.chars().collect(),
			pixel_height,
			fore_color: Color::WHITE,
			..Default::default()
		}
	}

	/// Override the packing rule which controls how the individual glyph images are combined in to
	/// the final image. See [`riddle_image::packer::ImagePackerSizePolicy`].
	///
	/// The default is [`riddle_image::packer::ImagePackerSizePolicy::Pow2Square`].
	pub fn packing_size_policy(&mut self, policy: ImagePackerSizePolicy) -> &mut Self {
		self.packing_size_policy = policy;
		self
	}

	/// Build the ImgFont for the given [`TTFont`] using the parameters specified in the generator.
	pub fn generate(&self, ttfont: &TTFont) -> Result<ImgFont> {
		let characters: Vec<char> = self.characters.iter().copied().collect();
		let mut images: Vec<Image> = vec![];
		let mut imgfont_glyphs: Vec<ImgFontGlyph> = vec![];

		let font = ttfont.rustype_font();
		let scale = rusttype::Scale {
			x: self.pixel_height as f32,
			y: self.pixel_height as f32,
		};

		let v_metrics = font.v_metrics(scale);
		let char_height = v_metrics.ascent - v_metrics.descent;
		let vertical_spacing = char_height + v_metrics.line_gap;

		for c in characters.iter() {
			let glyph = font
				.glyph(*c)
				.scaled(rusttype::Scale {
					x: self.pixel_height as f32,
					y: self.pixel_height as f32,
				})
				.positioned(rusttype::Point {
					x: 0.0,
					y: v_metrics.ascent,
				});
			let h_metrics = glyph.unpositioned().h_metrics();

			let (image, img_glyph) = match glyph.pixel_bounding_box() {
				Some(pixel_bounds) => {
					let mut image =
						Image::new(pixel_bounds.width() as u32, pixel_bounds.height() as u32);
					glyph.draw(|x, y, v| {
						let mut c = self.fore_color.clone();
						c.a = (255.0 * v) as u8;
						image.set_pixel([x as u32, y as u32], c);
					});
					let img_glyph = ImgFontGlyph {
						character: *c,
						horizontal_spacing: h_metrics.advance_width as u32,
						placement_offset: Vector2::new(pixel_bounds.min.x, pixel_bounds.min.y),
						rect: None,
					};
					(image, img_glyph)
				}
				_ => {
					let image = Image::new(0, 0);
					let img_glyph = ImgFontGlyph {
						character: *c,
						horizontal_spacing: h_metrics.advance_width as u32,
						placement_offset: Vector2::new(0, 0),
						rect: None,
					};
					(image, img_glyph)
				}
			};

			images.push(image);
			imgfont_glyphs.push(img_glyph);
		}

		let img_list: Vec<&Image> = images.iter().collect();
		let packer = ImagePacker::new()
			.padding(2)
			.size_policy(self.packing_size_policy.clone())
			.pack(&img_list[..])
			.map_err(|_| ImageError::Unknown)?;

		let mut img_font_builder = ImgFontBuilder::new();
		img_font_builder.vertical_spacing(vertical_spacing as u32);

		for (packed_rect, img_glyph) in packer.rects().iter().zip(imgfont_glyphs.iter()) {
			let mut glyph = img_glyph.clone();
			glyph.rect = if packed_rect.dimensions == Vector2::new(0, 0) {
				None
			} else {
				Some(packed_rect.clone())
			};

			img_font_builder.with_glyph(glyph);
		}

		Ok(img_font_builder.build(packer.take_image()))
	}
}