aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-02-27 14:38:38 -0800
committerJoe Wilm <joe@jwilm.com>2016-02-27 14:38:38 -0800
commit7e9888b1f5ea19aee1da6abda581ebccf9c88720 (patch)
treed5fba9486c114c32e4ca6ea6a66660ce3b24b6f7 /src
parent97c1a17bf1f9aa0740656becb4e4b2af64e5a88a (diff)
downloadalacritty-7e9888b1f5ea19aee1da6abda581ebccf9c88720.tar.gz
alacritty-7e9888b1f5ea19aee1da6abda581ebccf9c88720.zip
Add support for multiple font faces in rasterizer
Diffstat (limited to 'src')
-rw-r--r--src/list_fonts.rs36
-rw-r--r--src/main.rs2
-rw-r--r--src/text.rs64
3 files changed, 77 insertions, 25 deletions
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<String> {
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<Variant>,
+ variants: HashMap<String, Variant>,
+}
+
+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<String, Variant> {
+ &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<Family> {
+pub fn get_font_families() -> HashMap<String, Family> {
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<FontDesc, Face<'static>>,
+ library: Library,
+ system_fonts: HashMap<String, ::list_fonts::Family>,
}
#[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<S>(name: S, style: S) -> FontDesc
+ where S: Into<String>
+ {
+ 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
+ Rasterizer {
+ system_fonts: get_font_families(),
+ faces: HashMap::new(),
+ library: library,
+ }
+ }
- let variant = family.variants().first().unwrap();
- let path = variant.filepath();
+ pub fn get_face(&mut self, desc: &FontDesc) -> Option<Face<'static>> {
- Rasterizer {
- face: library.new_face(path, 0).expect("Create font face"),
- _library: library,
+ 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(&self, size: f32, c: char) -> RasterizedGlyph {
+ 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);