|
|
@ -4,14 +4,15 @@ use rusttype; |
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use ::{FontAtlas, GlyphMetadata};
|
|
|
|
use ::{FontTexture, FontAtlas, GlyphMetadata};
|
|
|
|
use ::geometry::Rect;
|
|
|
|
use ::texture::{Buffer2d, Bitmap};
|
|
|
|
use ::packer::{Packer, GrowingPacker, SkylinePacker};
|
|
|
|
|
|
|
|
/// Wraps a rusttype font and provides atlas packing and rasterization functions.
|
|
|
|
pub struct Font<'a> {
|
|
|
|
font: rusttype::Font<'a>
|
|
|
|
font: rusttype::Font<'a>,
|
|
|
|
texture: Option<FontTexture>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
@ -19,7 +20,8 @@ impl<'a> Font<'a> { |
|
|
|
/// Construct a Textium font from a RustType one.
|
|
|
|
pub fn new(rt_font: rusttype::Font<'a>) -> Font {
|
|
|
|
Font {
|
|
|
|
font: rt_font
|
|
|
|
font: rt_font,
|
|
|
|
texture: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
@ -34,14 +36,14 @@ impl<'a> Font<'a> { |
|
|
|
-> (FontAtlas, Bitmap<u8>, f32)
|
|
|
|
where I: Iterator<Item=char>
|
|
|
|
{
|
|
|
|
let mut atlas = FontAtlas {glyph_data: HashMap::new()};
|
|
|
|
let mut atlas = FontAtlas {glyph_metadata: HashMap::new()};
|
|
|
|
let mut packer = SkylinePacker::new(Bitmap::new(width, height));
|
|
|
|
packer.set_margin(margin);
|
|
|
|
for chr in chrs {
|
|
|
|
if let Some((mut info, rendered)) = self.rasterize_char(chr, scale) {
|
|
|
|
let r = packer.pack_resize(&rendered, |(w, h)| (w*2, h*2));
|
|
|
|
info.bounding_box = r;
|
|
|
|
atlas.glyph_data.insert(chr, info);
|
|
|
|
atlas.glyph_metadata.insert(chr, info);
|
|
|
|
} else if chr == ' ' {
|
|
|
|
// generate a blank pixel for the space texture
|
|
|
|
// TODO: this is bad and should be fixed
|
|
|
@ -49,7 +51,7 @@ impl<'a> Font<'a> { |
|
|
|
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);
|
|
|
|
atlas.glyph_metadata.insert(chr, info);
|
|
|
|
} else {
|
|
|
|
// TODO: use a Result instead of panic!ing
|
|
|
|
panic!("Cannot render character {}", chr);
|
|
|
|