diff --git a/font-renderer/src/directwrite/mod.rs b/font-renderer/src/directwrite/mod.rs index 86ec3c57..fd16678f 100644 --- a/font-renderer/src/directwrite/mod.rs +++ b/font-renderer/src/directwrite/mod.rs @@ -22,6 +22,7 @@ use std::iter::Cloned; use std::mem; use std::os::raw::c_void; use std::ptr; +use std::collections::btree_map::Entry; use std::slice::{self, Iter}; use std::sync::Arc; use uuid::IID_ID2D1SimplifiedGeometrySink; @@ -36,7 +37,8 @@ use winapi::{IDWriteFontFileLoader, IDWriteFontFileLoaderVtbl, IDWriteFontFileSt use winapi::{IDWriteFontFileStreamVtbl, IDWriteGeometrySink, IUnknown, IUnknownVtbl, TRUE, UINT16}; use winapi::{UINT32, UINT64, UINT}; -use self::com::{PathfinderCoclass, PathfinderComObject, PathfinderComPtr}; +use self::com::{PathfinderCoclass, PathfinderComObject}; +pub use self::com::{PathfinderComPtr}; use {FontInstance, GlyphDimensions, GlyphImage, GlyphKey}; mod com; @@ -178,6 +180,19 @@ impl FontContext where FK: Clone + Hash + Eq + Ord { } } + /// Loads an OpenType font from a COM `IDWriteFontFace` handle. + pub fn add_native_font(&mut self, font_key: &FK, handle: H) -> Result<(), ()> + where H: Into> + { + match self.dwrite_font_faces.entry((*font_key).clone()) { + Entry::Occupied(_) => Ok(()), + Entry::Vacant(entry) => { + entry.insert(handle.into()); + Ok(()) + } + } + } + /// Unloads the font with the given font key from memory. /// /// If the font isn't loaded, does nothing. @@ -193,11 +208,14 @@ impl FontContext where FK: Clone + Hash + Eq + Ord { /// libraries (including Pathfinder) apply modifications to the outlines: for example, to /// dilate them for easier reading. To retrieve extents that account for these modifications, /// set `exact` to false. - pub fn glyph_dimensions(&self, font_instance: &FontInstance, glyph_key: &GlyphKey) - -> Option { + pub fn glyph_dimensions(&self, + font_instance: &FontInstance, + glyph_key: &GlyphKey, + _exact: bool) + -> Result { unsafe { let font_face = match self.dwrite_font_faces.get(&font_instance.font_key) { - None => return None, + None => return Err(()), Some(font_face) => (*font_face).clone(), }; @@ -206,10 +224,10 @@ impl FontContext where FK: Clone + Hash + Eq + Ord { let result = (**font_face).GetDesignGlyphMetrics(&glyph_index, 1, &mut metrics, FALSE); if !winerror::SUCCEEDED(result) { - return None + return Err(()); } - Some(GlyphDimensions { + Ok(GlyphDimensions { advance: metrics.advanceWidth as f32, origin: Point2D::new(metrics.leftSideBearing, metrics.bottomSideBearing), size: Size2D::new((metrics.rightSideBearing - metrics.leftSideBearing) as u32, diff --git a/font-renderer/src/lib.rs b/font-renderer/src/lib.rs index cce5471e..60aedb65 100644 --- a/font-renderer/src/lib.rs +++ b/font-renderer/src/lib.rs @@ -53,6 +53,11 @@ extern crate uuid; #[macro_use(DEFINE_GUID)] extern crate winapi; +#[cfg(target_os = "windows")] +pub use self::directwrite::PathfinderComPtr; +#[cfg(target_os = "windows")] +pub use winapi::IDWriteFontFace; + use app_units::Au; use euclid::{Point2D, Size2D};