summaryrefslogtreecommitdiffstats
path: root/font
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-08-14 20:08:56 -0700
committerJoe Wilm <joe@jwilm.com>2016-08-14 20:08:56 -0700
commita62f9aee65b6c1fb4635568b53331bfe1bc3224d (patch)
tree3714342b69251159e50ab37964fd79a520da1afa /font
parentb70394170c0fa9cd4e4be8150121a698a426a069 (diff)
Support bold/italic font rendering on Linux
The FreeType font Rasterizer API is updated to match the CoreText Rasterizer. This enabled bold/italic fonts since the rest of the codebase has already been updated.
Diffstat (limited to 'font')
-rw-r--r--font/src/ft/mod.rs51
1 files changed, 29 insertions, 22 deletions
diff --git a/font/src/ft/mod.rs b/font/src/ft/mod.rs
index 024d33f6..55829e12 100644
--- a/font/src/ft/mod.rs
+++ b/font/src/ft/mod.rs
@@ -22,11 +22,11 @@ use freetype;
mod list_fonts;
use self::list_fonts::{Family, get_font_families};
-use super::{FontDesc, RasterizedGlyph, Metrics};
+use super::{FontDesc, RasterizedGlyph, Metrics, Size, FontKey, GlyphKey};
/// Rasterizes glyphs for a single font face.
pub struct Rasterizer {
- faces: HashMap<FontDesc, Face<'static>>,
+ faces: HashMap<FontKey, Face<'static>>,
library: Library,
system_fonts: HashMap<String, Family>,
dpi_x: u32,
@@ -58,10 +58,10 @@ impl Rasterizer {
}
}
- pub fn metrics(&mut self, desc: &FontDesc, size: f32) -> Metrics {
- let face = self.get_face(&desc).unwrap();
+ pub fn metrics(&self, key: FontKey, size: Size) -> Metrics {
+ let face = self.faces.get(&key).unwrap();
- let scale_size = self.dpr as f64 * size as f64;
+ let scale_size = self.dpr as f64 * size.as_f32_pts() as f64;
let em_size = face.em_size() as f64;
let w = face.max_advance_width() as f64;
@@ -76,27 +76,34 @@ impl Rasterizer {
}
}
- fn get_face(&mut self, desc: &FontDesc) -> Option<Face<'static>> {
- if let Some(face) = self.faces.get(desc) {
- return Some(face.clone());
- }
+ pub fn load_font(&mut self, desc: &FontDesc, _size: Size) -> Option<FontKey> {
+ self.get_face(desc)
+ .map(|face| {
+ let key = FontKey::next();
+ self.faces.insert(key, face);
+ key
+ })
+ }
- 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");
+ fn get_face(&mut self, desc: &FontDesc) -> Option<Face<'static>> {
+ self.system_fonts
+ .get(&desc.name[..])
+ .and_then(|font| font.variants().get(&desc.style[..]))
+ .map(|variant| {
+ 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());
- }
- }
+ pub fn get_glyph(&mut self, glyph_key: &GlyphKey) -> RasterizedGlyph {
+ let face = self.faces
+ .get(&glyph_key.font_key)
+ .expect("TODO handle get_face error");
- None
- }
+ let size = glyph_key.size.as_f32_pts() * self.dpr;
+ let c = glyph_key.c;
- pub fn get_glyph(&mut self, desc: &FontDesc, size: f32, c: char) -> RasterizedGlyph {
- let face = self.get_face(desc).expect("TODO handle get_face error");
- face.set_char_size(to_freetype_26_6(size * self.dpr), 0, self.dpi_x, self.dpi_y).unwrap();
+ face.set_char_size(to_freetype_26_6(size), 0, self.dpi_x, self.dpi_y).unwrap();
face.load_char(c as usize, freetype::face::TARGET_LIGHT).unwrap();
let glyph = face.glyph();
glyph.render_glyph(freetype::render_mode::RenderMode::Lcd).unwrap();