|
|
@ -28,7 +28,7 @@ impl<'a> Font<'a> { |
|
|
|
///
|
|
|
|
/// The packer may enlarge the bitmap beyond its initial dimensions in order to fit all glyphs.
|
|
|
|
pub fn make_atlas<I>(&self, chrs: I, scale: f32, margin: u32, width: usize, height: usize)
|
|
|
|
-> (FontAtlas, Bitmap<u8>)
|
|
|
|
-> (FontAtlas, Bitmap<u8>, f32)
|
|
|
|
where I: Iterator<Item=char>
|
|
|
|
{
|
|
|
|
let mut atlas = FontAtlas {glyph_data: HashMap::new()};
|
|
|
@ -37,10 +37,25 @@ impl<'a> Font<'a> { |
|
|
|
for chr in chrs {
|
|
|
|
if let Some((mut info, rendered)) = self.rasterize_char(chr, scale) {
|
|
|
|
let r = packer.pack_resize(&rendered);
|
|
|
|
info.bounding_box = r;
|
|
|
|
atlas.glyph_data.insert(chr, info);
|
|
|
|
} else if chr == ' ' {
|
|
|
|
// generate a blank pixel for the space texture
|
|
|
|
let (mut info, _) = self.rasterize_char(' ', scale).unwrap();
|
|
|
|
let empty_bitmap = Bitmap::new(1, 1);
|
|
|
|
let r = packer.pack_resize(&empty_bitmap, |(w, h)| (w*2, h*2));
|
|
|
|
info.bounding_box = r;
|
|
|
|
atlas.glyph_data.insert(chr, info);
|
|
|
|
} else {
|
|
|
|
// TODO: use a Result instead of panic!ing
|
|
|
|
panic!("Cannot render character {}", chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(atlas, packer.into_buf())
|
|
|
|
let v_metrics = self.font.v_metrics(rusttype::Scale::uniform(scale));
|
|
|
|
let line_height = v_metrics.ascent + v_metrics.descent + v_metrics.line_gap;
|
|
|
|
|
|
|
|
(atlas, packer.into_buf(), line_height)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rasterize_char(&self, chr: char, scale: f32)
|
|
|
|