diff --git a/src/list_fonts.rs b/src/list_fonts.rs index c7cd60ca..14f08596 100644 --- a/src/list_fonts.rs +++ b/src/list_fonts.rs @@ -1,4 +1,6 @@ +use std::collections::HashMap; use std::ffi::{CStr, CString}; +use std::fmt; use std::path::PathBuf; use std::ptr; use std::str::from_utf8; @@ -66,20 +68,36 @@ fn list_families() -> Vec { pub struct Variant { style: String, file: PathBuf, - index: usize, + index: isize, } impl Variant { #[inline] - pub fn filepath(&self) -> &::std::path::Path { + pub fn path(&self) -> &::std::path::Path { self.file.as_path() } + + #[inline] + pub fn index(&self) -> isize { + self.index + } } #[derive(Debug)] pub struct Family { name: String, - variants: Vec, + variants: HashMap, +} + +impl fmt::Display for Family { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + try!(write!(f, "{}: ", self.name)); + for (k, v) in &self.variants { + try!(write!(f, "{}, ", k)); + } + + Ok(()) + } } impl Family { @@ -89,8 +107,8 @@ impl Family { } #[inline] - pub fn variants(&self) -> &[Variant] { - &self.variants[..] + pub fn variants(&self) -> &HashMap { + &self.variants } } @@ -142,7 +160,7 @@ pub fn get_family_info(family: String) -> Family { members.push(Variant { style: style, file: PathBuf::from(file), - index: index as usize, + index: index as isize, }); } @@ -153,13 +171,13 @@ pub fn get_family_info(family: String) -> Family { Family { name: family, - variants: members + variants: members.into_iter().map(|v| (v.style.clone(), v)).collect() } } -pub fn get_font_families() -> Vec { +pub fn get_font_families() -> HashMap { list_families().into_iter() - .map(|family| get_family_info(family)) + .map(|family| (family.clone(), get_family_info(family))) .collect() } diff --git a/src/main.rs b/src/main.rs index 4ef326ac..78bf5cc5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ fn main() { gl::Viewport(0, 0, width as i32, height as i32); } - let rasterizer = text::Rasterizer::new(); + let mut rasterizer = text::Rasterizer::new(); let glyph_r = Glyph::new(&rasterizer.get_glyph(180., 'R')); let glyph_u = Glyph::new(&rasterizer.get_glyph(180., 'u')); diff --git a/src/text.rs b/src/text.rs index af40705f..355f1251 100644 --- a/src/text.rs +++ b/src/text.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use list_fonts::get_font_families; use freetype::Library; @@ -6,8 +7,9 @@ use freetype; /// Rasterizes glyphs for a single font face. pub struct Rasterizer { - face: Face<'static>, - _library: Library, + faces: HashMap>, + library: Library, + system_fonts: HashMap, } #[inline] @@ -15,28 +17,60 @@ fn to_freetype_26_6(f: f32) -> isize { ((1i32 << 6) as f32 * f) as isize } +#[derive(Clone, PartialEq, Eq, Hash)] +struct FontDesc { + name: String, + style: String, +} + +impl FontDesc { + pub fn new(name: S, style: S) -> FontDesc + where S: Into + { + FontDesc { + name: name.into(), + style: style.into() + } + } +} + impl Rasterizer { pub fn new() -> Rasterizer { let library = Library::init().unwrap(); - let family = get_font_families().into_iter() - .filter(|f| f.name() == "Inconsolata-dz") - .nth(0).unwrap(); // TODO - - let variant = family.variants().first().unwrap(); - let path = variant.filepath(); - Rasterizer { - face: library.new_face(path, 0).expect("Create font face"), - _library: library, + system_fonts: get_font_families(), + faces: HashMap::new(), + library: library, } } - pub fn get_glyph(&self, size: f32, c: char) -> RasterizedGlyph { + pub fn get_face(&mut self, desc: &FontDesc) -> Option> { + + if let Some(face) = self.faces.get(desc) { + return Some(face.clone()); + } + + if let Some(font) = self.system_fonts.get(&desc.name[..]) { + if let Some(variant) = font.variants().get(&desc.style[..]) { + let face = self.library.new_face(variant.path(), variant.index()) + .expect("TODO handle new_face error"); + + self.faces.insert(desc.to_owned(), face); + return Some(self.faces.get(desc).unwrap().clone()); + } + } + + None + } + + pub fn get_glyph(&mut self, size: f32, c: char) -> RasterizedGlyph { + let face = self.get_face(&FontDesc::new("Ubuntu Mono", "Regular")) + .expect("TODO handle get_face error"); // TODO DPI - self.face.set_char_size(to_freetype_26_6(size), 0, 96, 0).unwrap(); - self.face.load_char(c as usize, freetype::face::RENDER).unwrap(); - let glyph = self.face.glyph(); + face.set_char_size(to_freetype_26_6(size), 0, 96, 0).unwrap(); + face.load_char(c as usize, freetype::face::RENDER).unwrap(); + let glyph = face.glyph(); RasterizedGlyph { top: glyph.bitmap_top() as usize, @@ -64,7 +98,7 @@ mod tests { #[test] fn create_rasterizer_and_render_glyph() { - let rasterizer = Rasterizer::new(); + let mut rasterizer = Rasterizer::new(); let glyph = rasterizer.get_glyph(24., 'U'); println!("glyph: {:?}", glyph);