diff options
author | Joe Wilm <joe@jwilm.com> | 2016-08-12 13:38:41 -0500 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2016-08-12 19:30:14 -0500 |
commit | b70394170c0fa9cd4e4be8150121a698a426a069 (patch) | |
tree | 41d84240a4e58df201fbd7f4f984c3814c7cb055 /font/src/darwin | |
parent | 874719580ce3e12f09d01a9a53c5e64205ef5b5d (diff) | |
download | alacritty-b70394170c0fa9cd4e4be8150121a698a426a069.tar.gz alacritty-b70394170c0fa9cd4e4be8150121a698a426a069.zip |
Support bold/italic font rendering on macOS
This patch adds support for rendering italic fonts and bold fonts.
The `font` crate has a couple of new paradigms to support this: font
keys and glyph keys. `FontKey` is a lightweight (4 byte) identifier for
a font loaded out of the rasterizer. This replaces `FontDesc` for
rasterizing glyphs from a loaded font. `FontDesc` had the problem that
it contained two strings, and the glyph cache needs to store a copy of
the font key for every loaded glyph. `GlyphKey` is now passed to the
glyph rasterization method instead of a simple `char`. `GlyphKey`
contains information including font, size, and the character.
The rasterizer APIs do not define what happens when loading the same
font from a `FontDesc` more than once. It is assumed that the
application will track the resulting `FontKey` instead of asking the
font to be loaded multiple times.
Diffstat (limited to 'font/src/darwin')
-rw-r--r-- | font/src/darwin/mod.rs | 60 |
1 files changed, 42 insertions, 18 deletions
diff --git a/font/src/darwin/mod.rs b/font/src/darwin/mod.rs index 31eb1292..4db8dad1 100644 --- a/font/src/darwin/mod.rs +++ b/font/src/darwin/mod.rs @@ -43,7 +43,7 @@ use euclid::point::Point2D; use euclid::rect::Rect; use euclid::size::Size2D; -use super::{FontDesc, RasterizedGlyph, Metrics}; +use super::{FontDesc, RasterizedGlyph, Metrics, FontKey, GlyphKey}; pub mod cg_color; use self::cg_color::{CGColorRef, CGColor}; @@ -52,6 +52,10 @@ pub mod byte_order; use self::byte_order::kCGBitmapByteOrder32Host; use self::byte_order::extract_rgb; +use super::Size; + +static FONT_LOAD_ERROR: &'static str = "font specified by FontKey has been loaded"; + /// Font descriptor /// /// The descriptor provides data about a font and supports creating a font. @@ -70,7 +74,7 @@ pub struct Descriptor { /// /// Given a fontdesc, can rasterize fonts. pub struct Rasterizer { - fonts: HashMap<FontDesc, Font>, + fonts: HashMap<FontKey, Font>, device_pixel_ratio: f32, } @@ -83,22 +87,35 @@ impl Rasterizer { } } - pub fn metrics(&mut self, desc: &FontDesc, size: f32) -> Metrics { - let scaled_size = self.device_pixel_ratio * size; - self.get_font(desc, scaled_size).unwrap().metrics() + /// Get metrics for font specified by FontKey + /// + /// # Panics + /// + /// If FontKey was not generated by `load_font`, this method will panic. + pub fn metrics(&self, key: FontKey, _size: Size) -> Metrics { + // NOTE size is not needed here since the font loaded already contains + // it. It's part of the API due to platform differences. + let font = self.fonts.get(&key).expect(FONT_LOAD_ERROR); + font.metrics() } - fn get_font(&mut self, desc: &FontDesc, size: f32) -> Option<Font> { - if let Some(font) = self.fonts.get(desc) { - return Some(font.clone()); - } + pub fn load_font(&mut self, desc: &FontDesc, size: Size) -> Option<FontKey> { + self.get_font(desc, size) + .map(|font| { + let key = FontKey::next(); + self.fonts.insert(key, font); + key + }) + } + + fn get_font(&mut self, desc: &FontDesc, size: Size) -> Option<Font> { let descriptors = descriptors_for_family(&desc.name[..]); for descriptor in descriptors { if descriptor.style_name == desc.style { // Found the font we want - let font = descriptor.to_font(size as _); - self.fonts.insert(desc.to_owned(), font.clone()); + let scaled_size = size.as_f32_pts() as f64 * self.device_pixel_ratio as f64; + let font = descriptor.to_font(scaled_size); return Some(font); } } @@ -106,11 +123,18 @@ impl Rasterizer { None } - pub fn get_glyph(&mut self, desc: &FontDesc, size: f32, c: char) -> RasterizedGlyph { - let scaled_size = self.device_pixel_ratio * size; - let glyph = self.get_font(desc, scaled_size).unwrap().get_glyph(c, scaled_size as _); - - glyph + /// Get rasterized glyph for given glyph key + /// + /// # Panics + /// + /// Panics if the FontKey specified in GlyphKey was not generated from `load_font` + pub fn get_glyph(&mut self, glyph: &GlyphKey) -> RasterizedGlyph { + let scaled_size = self.device_pixel_ratio * glyph.size.as_f32_pts(); + + self.fonts + .get(&glyph.font_key) + .expect(FONT_LOAD_ERROR) + .get_glyph(glyph.c, scaled_size as _) } } @@ -181,8 +205,8 @@ pub fn descriptors_for_family(family: &str) -> Vec<Descriptor> { impl Descriptor { /// Create a Font from this descriptor - pub fn to_font(&self, pt_size: f64) -> Font { - let ct_font = ct_new_from_descriptor(&self.ct_descriptor, pt_size); + pub fn to_font(&self, size: f64) -> Font { + let ct_font = ct_new_from_descriptor(&self.ct_descriptor, size); let cg_font = ct_font.copy_to_CGFont(); Font { ct_font: ct_font, |