summaryrefslogtreecommitdiff
path: root/font/src/darwin/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'font/src/darwin/mod.rs')
-rw-r--r--font/src/darwin/mod.rs60
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,