diff --git a/src/servo-gfx/font.rs b/src/servo-gfx/font.rs index 354239bfd82..4ad4ad0a0a7 100644 --- a/src/servo-gfx/font.rs +++ b/src/servo-gfx/font.rs @@ -3,11 +3,11 @@ use color::Color; use font_context::FontContext; use geometry::Au; -use platform; +use platform::font_context::FontContextHandle; +use platform::font::{FontHandle, FontTable}; use render_context::RenderContext; use text::glyph::{GlyphStore, GlyphIndex}; use text::shaper::ShaperMethods; -use text::shaper::ShaperMethods; use text::{Shaper, TextRun}; use util::range::Range; @@ -16,20 +16,15 @@ use azure::scaled_font::ScaledFont; use azure::azure_hl::{BackendType, ColorPattern}; use geom::{Point2D, Rect, Size2D}; -use native; - // FontHandle encapsulates access to the platform's font API, // e.g. quartz, FreeType. It provides access to metrics and tables // needed by the text shaper as well as access to the underlying font // resources needed by the graphics layer to draw glyphs. -#[cfg(target_os = "macos")] -pub type FontHandle = platform::font::QuartzFontHandle; - -#[cfg(target_os = "linux")] -pub type FontHandle = platform::font::FreeTypeFontHandle; - pub trait FontHandleMethods { + fn new_from_buffer(fctx: &FontContextHandle, buf: ~[u8], style: &SpecifiedFontStyle) + -> Result; + // an identifier usable by FontContextHandle to recreate this FontHandle. fn face_identifier(&self) -> ~str; fn family_name(&self) -> ~str; @@ -37,36 +32,14 @@ pub trait FontHandleMethods { fn is_italic(&self) -> bool; fn boldness(&self) -> CSSFontWeight; - fn clone_with_style(&self, fctx: &native::FontContextHandle, style: &UsedFontStyle) -> Result; + fn clone_with_style(&self, fctx: &FontContextHandle, style: &UsedFontStyle) + -> Result; fn glyph_index(&self, codepoint: char) -> Option; fn glyph_h_advance(&self, GlyphIndex) -> Option; fn get_metrics(&self) -> FontMetrics; fn get_table_for_tag(&self, FontTableTag) -> Option; } -// TODO(Issue #163): this is a workaround for static methods and -// typedefs not working well together. It should be removed. -// -// `new` should be part of trait FontHandleMethods. - -pub impl FontHandle { - #[cfg(target_os = "macos")] - pub fn new_from_buffer(fctx: &native::FontContextHandle, - buf: ~[u8], - style: &SpecifiedFontStyle) - -> Result { - platform::font::QuartzFontHandle::new_from_buffer(fctx, buf, style) - } - - #[cfg(target_os = "linux")] - pub fn new_from_buffer(fctx: &native::FontContextHandle, - buf: ~[u8], - style: &SpecifiedFontStyle) - -> Result { - platform::font::FreeTypeFontHandle::new_from_buffer(fctx, @buf, style) - } -} - // Used to abstract over the shaper's choice of fixed int representation. pub type FractionalPixel = float; @@ -88,12 +61,6 @@ impl FontTableTagConversions for FontTableTag { } } -#[cfg(target_os = "macos")] -pub type FontTable = platform::font::QuartzFontTable; - -#[cfg(target_os = "linux")] -pub type FontTable = platform::font::FreeTypeFontTable; - pub trait FontTableMethods { fn with_buffer(&self, &fn(*u8, uint)); } @@ -246,8 +213,8 @@ pub impl Font { style: &SpecifiedFontStyle, backend: BackendType) -> Result<@mut Font, ()> { - let handle = FontHandle::new_from_buffer(&ctx.handle, buffer, style); - let handle = if handle.is_ok() { + let handle = FontHandleMethods::new_from_buffer(&ctx.handle, buffer, style); + let handle: FontHandle = if handle.is_ok() { result::unwrap(handle) } else { return Err(handle.get_err()); diff --git a/src/servo-gfx/font_context.rs b/src/servo-gfx/font_context.rs index bfc96f37a4d..4a3835de6ae 100644 --- a/src/servo-gfx/font_context.rs +++ b/src/servo-gfx/font_context.rs @@ -4,8 +4,8 @@ use font::{Font, FontDescriptor, FontGroup, FontStyle, SelectorPlatformIdentifie use font::{SelectorStubDummy, SpecifiedFontStyle, UsedFontStyle}; use font_context; use font_list::FontList; -use native::FontHandle; -use platform; +use platform::font::FontHandle; +use platform::font_context::FontContextHandle; use util::cache::Cache; use util::cache::MonoCache; @@ -32,31 +32,11 @@ pub fn dummy_style() -> FontStyle { } } -#[cfg(target_os = "macos")] -type FontContextHandle = platform::font_context::QuartzFontContextHandle; - -#[cfg(target_os = "linux")] -type FontContextHandle = platform::font_context::FreeTypeFontContextHandle; - pub trait FontContextHandleMethods { fn clone(&self) -> FontContextHandle; fn create_font_from_identifier(&self, ~str, UsedFontStyle) -> Result; } -// TODO(Issue #163): this is a workaround for static methods, traits, -// and typedefs not working well together. It should be removed. -pub impl FontContextHandle { - #[cfg(target_os = "macos")] - pub fn new() -> FontContextHandle { - platform::font_context::QuartzFontContextHandle::new() - } - - #[cfg(target_os = "linux")] - pub fn new() -> FontContextHandle { - platform::font_context::FreeTypeFontContextHandle::new() - } -} - #[allow(non_implicitly_copyable_typarams)] pub struct FontContext { instance_cache: MonoCache, diff --git a/src/servo-gfx/font_list.rs b/src/servo-gfx/font_list.rs index 32413043db1..7a6ddae6570 100644 --- a/src/servo-gfx/font_list.rs +++ b/src/servo-gfx/font_list.rs @@ -2,36 +2,17 @@ use font::{CSSFontWeight, SpecifiedFontStyle}; use gfx_font::FontHandleMethods; -use gfx_font::FontHandleMethods; -use native::FontHandle; -use native; -use platform; +use platform::font::FontHandle; +use platform::font_context::FontContextHandle; +use platform::font_list::FontListHandle; use util::time::time; use core::hashmap::HashMap; -#[cfg(target_os = "macos")] -type FontListHandle = platform::font_list::QuartzFontListHandle; - -#[cfg(target_os = "linux")] -type FontListHandle = platform::font_list::FontconfigFontListHandle; - -pub impl FontListHandle { - #[cfg(target_os = "macos")] - pub fn new(fctx: &native::FontContextHandle) -> Result { - Ok(platform::font_list::QuartzFontListHandle::new(fctx)) - } - - #[cfg(target_os = "linux")] - pub fn new(fctx: &native::FontContextHandle) -> Result { - Ok(platform::font_list::FontconfigFontListHandle::new(fctx)) - } -} - pub type FontFamilyMap = HashMap<~str, @mut FontFamily>; trait FontListHandleMethods { - fn get_available_families(&self, fctx: &native::FontContextHandle) -> FontFamilyMap; + fn get_available_families(&self, fctx: &FontContextHandle) -> FontFamilyMap; fn load_variations_for_family(&self, family: @mut FontFamily); } @@ -41,8 +22,8 @@ pub struct FontList { } pub impl FontList { - fn new(fctx: &native::FontContextHandle) -> FontList { - let handle = result::unwrap(FontListHandle::new(fctx)); + fn new(fctx: &FontContextHandle) -> FontList { + let handle = FontListHandle::new(fctx); let mut list = FontList { handle: handle, family_map: HashMap::new(), @@ -51,7 +32,7 @@ pub impl FontList { return list; } - priv fn refresh(&mut self, _fctx: &native::FontContextHandle) { + priv fn refresh(&mut self, _: &FontContextHandle) { // TODO(Issue #186): don't refresh unless something actually // changed. Does OSX have a notification for this event? // @@ -106,15 +87,15 @@ pub impl FontFamily { } } - priv fn load_family_variations(@mut self, list: &native::FontListHandle) { + priv fn load_family_variations(@mut self, list: &FontListHandle) { let this : &mut FontFamily = self; // FIXME: borrow checker workaround if this.entries.len() > 0 { return; } list.load_variations_for_family(self); assert!(this.entries.len() > 0); } - fn find_font_for_style(@mut self, list: &native::FontListHandle, style: &SpecifiedFontStyle) -> Option<@FontEntry> { - + fn find_font_for_style(@mut self, list: &FontListHandle, style: &SpecifiedFontStyle) + -> Option<@FontEntry> { self.load_family_variations(list); // TODO(Issue #189): optimize lookup for @@ -165,5 +146,8 @@ pub impl FontEntry { self.weight.is_bold() } - fn is_italic(&self) -> bool { self.italic } + fn is_italic(&self) -> bool { + self.italic + } } + diff --git a/src/servo-gfx/native.rs b/src/servo-gfx/native.rs deleted file mode 100644 index 9ae03bdc06b..00000000000 --- a/src/servo-gfx/native.rs +++ /dev/null @@ -1,9 +0,0 @@ -/* This file exists just to make it easier to import platform-specific - implementations. - -Note that you still must define each of the files as a module in -servo_gfx.rc. This is not ideal and may be changed in the future. */ - -pub use font::FontHandle; -pub use font_context::FontContextHandle; -pub use font_list::FontListHandle; diff --git a/src/servo-gfx/platform/linux/font.rs b/src/servo-gfx/platform/linux/font.rs index f09034cf4a0..6fa22fad275 100644 --- a/src/servo-gfx/platform/linux/font.rs +++ b/src/servo-gfx/platform/linux/font.rs @@ -8,8 +8,7 @@ use gfx_font::{CSSFontWeight, FontHandleMethods, FontMetrics, FontTable, FontTab use gfx_font::{FontTableTag, FractionalPixel, SpecifiedFontStyle, UsedFontStyle, FontWeight100}; use gfx_font::{FontWeight200, FontWeight300, FontWeight400, FontWeight500, FontWeight600}; use gfx_font::{FontWeight700, FontWeight800, FontWeight900}; -use native; -use platform::font_context::FreeTypeFontContextHandle; +use platform::font_context::{FreeTypeFontContextHandle, FontContextHandle}; use text::glyph::GlyphIndex; use text::util::{float_to_fixed, fixed_to_float}; @@ -24,6 +23,9 @@ use platform::font::freetype::freetype::{FT_SizeRec, FT_UInt, FT_Size_Metrics}; use platform::font::freetype::freetype::{ft_sfnt_os2}; use platform::font::freetype::tt_os2::TT_OS2; +pub use FontHandle = platform::linux::font::FreeTypeFontHandle; +pub use FontTable = platform::linux::font::FreeTypeFontTable; + fn float_to_fixed_ft(f: float) -> i32 { float_to_fixed(6, f) } @@ -113,9 +115,15 @@ pub impl FreeTypeFontHandle { Ok(FreeTypeFontHandle { source: FontSourceFile(file), face: face }) } +} +impl FontHandleMethods for FreeTypeFontHandle { pub fn new_from_buffer(fctx: &FreeTypeFontContextHandle, - buf: @~[u8], style: &SpecifiedFontStyle) -> Result { + buf: ~[u8], + style: &SpecifiedFontStyle) + -> Result { + let buf = @buf; + let ft_ctx: FT_Library = fctx.ctx.ctx; if ft_ctx.is_null() { return Err(()); } @@ -150,9 +158,7 @@ pub impl FreeTypeFontHandle { } } } -} -impl FontHandleMethods for FreeTypeFontHandle { // an identifier usable by FontContextHandle to recreate this FontHandle. fn face_identifier(&self) -> ~str { /* FT_Get_Postscript_Name seems like a better choice here, but it @@ -196,7 +202,7 @@ impl FontHandleMethods for FreeTypeFontHandle { } fn clone_with_style(&self, - fctx: &native::FontContextHandle, + fctx: &FontContextHandle, style: &UsedFontStyle) -> Result { match self.source { FontSourceMem(buf) => { diff --git a/src/servo-gfx/platform/linux/font_context.rs b/src/servo-gfx/platform/linux/font_context.rs index b81278b53e7..c809f89d225 100644 --- a/src/servo-gfx/platform/linux/font_context.rs +++ b/src/servo-gfx/platform/linux/font_context.rs @@ -9,6 +9,8 @@ use platform::font_list::path_from_identifier; use platform::font_context::freetype::freetype::{FTErrorMethods, FT_Library}; use platform::font_context::freetype::freetype::bindgen::{FT_Done_FreeType, FT_Init_FreeType}; +pub use FontContextHandle = platform::linux::FreeTypeFontContextHandle; + struct FreeTypeLibraryHandle { ctx: FT_Library, } diff --git a/src/servo-gfx/platform/linux/font_list.rs b/src/servo-gfx/platform/linux/font_list.rs index 79027fb8d84..d7dcc80d1df 100644 --- a/src/servo-gfx/platform/linux/font_list.rs +++ b/src/servo-gfx/platform/linux/font_list.rs @@ -6,9 +6,8 @@ extern mod fontconfig; use gfx_font::FontHandleMethods; use gfx_font_context::FontContextHandleMethods; use gfx_font_list::{FontEntry, FontFamily, FontFamilyMap}; -use native; use platform::font::FreeTypeFontHandle; -use platform::font_context::FreeTypeFontContextHandle; +use platform::font_context::{FontContextHandle, FreeTypeFontContextHandle}; use core::hashmap::HashMap; use core::libc::c_int; @@ -35,7 +34,7 @@ pub struct FontconfigFontListHandle { } pub impl FontconfigFontListHandle { - pub fn new(fctx: &native::FontContextHandle) -> FontconfigFontListHandle { + pub fn new(fctx: &FontContextHandle) -> FontconfigFontListHandle { FontconfigFontListHandle { fctx: fctx.clone() } } diff --git a/src/servo-gfx/platform/macos/font.rs b/src/servo-gfx/platform/macos/font.rs index 15280f6c6e2..9706aa110ae 100644 --- a/src/servo-gfx/platform/macos/font.rs +++ b/src/servo-gfx/platform/macos/font.rs @@ -5,7 +5,7 @@ extern mod core_graphics; extern mod core_text; use geometry::Au; -use gfx_font::{CSSFontWeight, FontHandleMethods, FontMetrics, FontTable, FontTableMethods}; +use gfx_font::{CSSFontWeight, FontHandleMethods, FontMetrics, FontTableMethods}; use gfx_font::{FontTableTag, FontWeight100, FontWeight200, FontWeight300, FontWeight400}; use gfx_font::{FontWeight500, FontWeight600, FontWeight700, FontWeight800, FontWeight900}; use gfx_font::{FractionalPixel, SpecifiedFontStyle}; @@ -19,59 +19,39 @@ use platform::macos::font::core_text::font::{CTFont, CTFontMethods, CTFontMethod use platform::macos::font::core_text::font_descriptor::{SymbolicTraitAccessors}; use platform::macos::font::core_text::font_descriptor::{TraitAccessors}; use platform::macos::font::core_text::font_descriptor::{kCTFontDefaultOrientation}; -use platform::macos::font_context::QuartzFontContextHandle; +use platform::macos::font_context::FontContextHandle; use platform; use text::glyph::GlyphIndex; -struct QuartzFontTable { +pub struct FontTable { data: CFData, } - // Noncopyable. -impl Drop for QuartzFontTable { fn finalize(&self) {} } +impl Drop for FontTable { + fn finalize(&self) {} +} -pub impl QuartzFontTable { - fn wrap(data: CFData) -> QuartzFontTable { - QuartzFontTable { data: data } +pub impl FontTable { + fn wrap(data: CFData) -> FontTable { + FontTable { data: data } } } -impl FontTableMethods for QuartzFontTable { +impl FontTableMethods for FontTable { fn with_buffer(&self, blk: &fn(*u8, uint)) { blk(self.data.bytes(), self.data.len()); } } -pub struct QuartzFontHandle { +pub struct FontHandle { priv cgfont: Option, ctfont: CTFont, } -pub impl QuartzFontHandle { - fn new_from_buffer(_fctx: &QuartzFontContextHandle, buf: ~[u8], - style: &SpecifiedFontStyle) -> Result { - let fontprov : CGDataProvider = vec::as_imm_buf(buf, |cbuf, len| { - platform::macos::font::core_graphics::data_provider::new_from_buffer(cbuf, len) - }); - - let cgfont = platform::macos::font::core_graphics::font::create_with_data_provider( - &fontprov); - let ctfont = platform::macos::font::core_text::font::new_from_CGFont(&cgfont, - style.pt_size); - - let result = Ok(QuartzFontHandle { - cgfont: Some(cgfont), - ctfont: ctfont, - }); - - return result; - } - - fn new_from_CTFont(_: &QuartzFontContextHandle, - ctfont: CTFont) - -> Result { - Ok(QuartzFontHandle { +pub impl FontHandle { + fn new_from_CTFont(_: &FontContextHandle, ctfont: CTFont) -> Result { + Ok(FontHandle { mut cgfont: None, ctfont: ctfont, }) @@ -89,7 +69,26 @@ pub impl QuartzFontHandle { } } -impl FontHandleMethods for QuartzFontHandle { +impl FontHandleMethods for FontHandle { + fn new_from_buffer(_: &FontContextHandle, buf: ~[u8], style: &SpecifiedFontStyle) + -> Result { + let fontprov : CGDataProvider = vec::as_imm_buf(buf, |cbuf, len| { + platform::macos::font::core_graphics::data_provider::new_from_buffer(cbuf, len) + }); + + let cgfont = platform::macos::font::core_graphics::font::create_with_data_provider( + &fontprov); + let ctfont = platform::macos::font::core_text::font::new_from_CGFont(&cgfont, + style.pt_size); + + let result = Ok(FontHandle { + cgfont: Some(cgfont), + ctfont: ctfont, + }); + + return result; + } + fn family_name(&self) -> ~str { self.ctfont.family_name() } @@ -118,11 +117,10 @@ impl FontHandleMethods for QuartzFontHandle { return FontWeight900; } - fn clone_with_style(&self, fctx: &QuartzFontContextHandle, - style: &SpecifiedFontStyle) - -> Result { + fn clone_with_style(&self, fctx: &FontContextHandle, style: &SpecifiedFontStyle) + -> Result { let new_font = self.ctfont.clone_with_font_size(style.pt_size); - return QuartzFontHandle::new_from_CTFont(fctx, new_font); + return FontHandle::new_from_CTFont(fctx, new_font); } fn glyph_index(&self, codepoint: char) -> Option { @@ -182,7 +180,7 @@ impl FontHandleMethods for QuartzFontHandle { fn get_table_for_tag(&self, tag: FontTableTag) -> Option { let result: Option = self.ctfont.get_font_table(tag); result.chain(|data| { - Some(QuartzFontTable::wrap(data)) + Some(FontTable::wrap(data)) }) } diff --git a/src/servo-gfx/platform/macos/font_context.rs b/src/servo-gfx/platform/macos/font_context.rs index fcd1c71914d..aacd51cdcb9 100644 --- a/src/servo-gfx/platform/macos/font_context.rs +++ b/src/servo-gfx/platform/macos/font_context.rs @@ -2,26 +2,28 @@ extern mod core_foundation; extern mod core_graphics; extern mod core_text; -use gfx_font::{FontHandle, UsedFontStyle}; +use gfx_font::UsedFontStyle; use gfx_font_context::FontContextHandleMethods; -use platform::macos::font::QuartzFontHandle; +use platform::macos::font::FontHandle; use platform; -pub struct QuartzFontContextHandle { +pub struct FontContextHandle { ctx: () } -pub impl QuartzFontContextHandle { +pub impl FontContextHandle { // this is a placeholder until NSFontManager or whatever is bound in here. - pub fn new() -> QuartzFontContextHandle { - QuartzFontContextHandle { ctx: () } + pub fn new() -> FontContextHandle { + FontContextHandle { ctx: () } } } -impl FontContextHandleMethods for QuartzFontContextHandle { - fn clone(&self) -> QuartzFontContextHandle { - QuartzFontContextHandle { ctx: self.ctx } +impl FontContextHandleMethods for FontContextHandle { + fn clone(&self) -> FontContextHandle { + FontContextHandle { + ctx: self.ctx + } } fn create_font_from_identifier(&self, @@ -32,7 +34,7 @@ impl FontContextHandleMethods for QuartzFontContextHandle { name, style.pt_size); do result::chain(ctfont_result) |ctfont| { - QuartzFontHandle::new_from_CTFont(self, ctfont) + FontHandle::new_from_CTFont(self, ctfont) } } } diff --git a/src/servo-gfx/platform/macos/font_list.rs b/src/servo-gfx/platform/macos/font_list.rs index 7adeb5f5cb6..0c7bbfe4f37 100644 --- a/src/servo-gfx/platform/macos/font_list.rs +++ b/src/servo-gfx/platform/macos/font_list.rs @@ -5,9 +5,8 @@ use gfx_font::FontHandleMethods; use gfx_font_context::FontContextHandleMethods; use gfx_font_list::{FontEntry, FontFamily, FontFamilyMap}; -use native; -use platform::macos::font::QuartzFontHandle; -use platform::macos::font_context::QuartzFontContextHandle; +use platform::macos::font::FontHandle; +use platform::macos::font_context::FontContextHandle; use platform::macos::font_list::core_foundation::array::CFArray; use platform::macos::font_list::core_foundation::base::CFWrapper; use platform::macos::font_list::core_foundation::string::{CFString, CFStringRef}; @@ -17,13 +16,15 @@ use platform; use core::hashmap::HashMap; -pub struct QuartzFontListHandle { - fctx: QuartzFontContextHandle, +pub struct FontListHandle { + fctx: FontContextHandle, } -pub impl QuartzFontListHandle { - fn new(fctx: &native::FontContextHandle) -> QuartzFontListHandle { - QuartzFontListHandle { fctx: fctx.clone() } +pub impl FontListHandle { + fn new(fctx: &FontContextHandle) -> FontListHandle { + FontListHandle { + fctx: fctx.clone() + } } fn get_available_families(&self) -> FontFamilyMap { @@ -52,7 +53,7 @@ pub impl QuartzFontListHandle { let desc = CFWrapper::wrap_shared(*descref); let font = platform::macos::font_list::core_text::font::new_from_descriptor(&desc, 0.0); - let handle = result::unwrap(QuartzFontHandle::new_from_CTFont(&self.fctx, font)); + let handle = result::unwrap(FontHandle::new_from_CTFont(&self.fctx, font)); debug!("Creating new FontEntry for face: %s", handle.face_name()); let entry = @FontEntry::new(family, handle); diff --git a/src/servo-gfx/servo_gfx.rc b/src/servo-gfx/servo_gfx.rc index 66d697aa465..caeef9d9cdd 100644 --- a/src/servo-gfx/servo_gfx.rc +++ b/src/servo-gfx/servo_gfx.rc @@ -37,10 +37,7 @@ pub mod font_list; // Misc. pub mod opts; -// Pub-uses for multiple implementations. Platform selection happens in -// font.rs, font_list.rs, font_context.rs -pub mod native; - +// Platform-specific implementations. #[path="platform/mod.rs"] pub mod platform; diff --git a/src/servo-gfx/text/harfbuzz/shaper.rs b/src/servo-gfx/text/harfbuzz/shaper.rs index a0f8cda7f0a..52424bb430e 100644 --- a/src/servo-gfx/text/harfbuzz/shaper.rs +++ b/src/servo-gfx/text/harfbuzz/shaper.rs @@ -4,7 +4,8 @@ use geom::Point2D; use geometry::Au; -use font::{Font, FontTable, FontTableMethods, FontTableTag}; +use font::{Font, FontTableMethods, FontTableTag}; +use platform::font::FontTable; use text::glyph::{GlyphStore, GlyphIndex, GlyphData}; use text::shaper::ShaperMethods;