From a3f4b52c90ab0accd4173050857f506a91e3f1df Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Tue, 6 Nov 2012 12:22:29 -0800 Subject: [PATCH] Rename NativeFont to FontHandle. --- src/servo/gfx.rs | 1 - src/servo/gfx/font.rs | 18 +++++----- src/servo/gfx/font_cache.rs | 7 ++-- src/servo/gfx/font_handle.rs | 33 +++++++++++++++++ .../{native_font.rs => font_handle.rs} | 8 ++--- src/servo/gfx/native.rs | 8 +++++ src/servo/gfx/native_font.rs | 35 ------------------- .../quartz/{native_font.rs => font_handle.rs} | 8 ++--- src/servo/servo.rc | 12 +++---- 9 files changed, 68 insertions(+), 62 deletions(-) create mode 100644 src/servo/gfx/font_handle.rs rename src/servo/gfx/freetype/{native_font.rs => font_handle.rs} (97%) create mode 100644 src/servo/gfx/native.rs delete mode 100644 src/servo/gfx/native_font.rs rename src/servo/gfx/quartz/{native_font.rs => font_handle.rs} (97%) diff --git a/src/servo/gfx.rs b/src/servo/gfx.rs index 5e16ad50d70..ce7ab48e535 100644 --- a/src/servo/gfx.rs +++ b/src/servo/gfx.rs @@ -14,7 +14,6 @@ pub use font_cache::FontCache; pub use font_context::FontContext; pub use font_matcher::FontMatcher; pub use geometry::Au; -pub use native_font::NativeFont; pub use shaper::Shaper; pub use text_run::TextRun; pub use text_run::SendableTextRun; diff --git a/src/servo/gfx/font.rs b/src/servo/gfx/font.rs index 2d8d3e448c4..b17e8aa0269 100644 --- a/src/servo/gfx/font.rs +++ b/src/servo/gfx/font.rs @@ -10,6 +10,8 @@ use geom::{Point2D, Rect, Size2D}; use glyph::{GlyphStore, GlyphIndex}; use servo_util::range::Range; +use native::FontHandle; + // Used to abstract over the shaper's choice of fixed int representation. type FractionalPixel = float; @@ -72,7 +74,7 @@ and the renderer can use it to render text. */ struct Font { priv fontbuf: @~[u8], - priv native_font: NativeFont, + priv handle: FontHandle, priv mut azure_font: Option, priv mut shaper: Option<@Shaper>, style: FontStyle, @@ -86,12 +88,12 @@ struct Font { impl Font { // TODO: who should own fontbuf? - static fn new(fontbuf: @~[u8], native_font: NativeFont, style: FontStyle) -> Font { - let metrics = native_font.get_metrics(); + static fn new(fontbuf: @~[u8], handle: FontHandle, style: FontStyle) -> Font { + let metrics = handle.get_metrics(); Font { fontbuf : fontbuf, - native_font : move native_font, + handle : move handle, azure_font: None, shaper: None, style: move style, @@ -151,7 +153,7 @@ impl Font { fn get_cairo_face(font: &Font) -> *cairo_font_face_t { use cairo::cairo_ft::bindgen::{cairo_ft_font_face_create_for_ft_face}; - let ftface = font.native_font.face; + let ftface = font.handle.face; let cface = cairo_ft_font_face_create_for_ft_face(ftface, 0 as c_int); // FIXME: error handling return cface; @@ -161,7 +163,7 @@ impl Font { fn get_cairo_face(font: &Font) -> *cairo_font_face_t { use cairo::cairo_quartz::bindgen::cairo_quartz_font_face_create_for_cgfont; - let cgfont = font.native_font.cgfont; + let cgfont = font.handle.cgfont; let face = cairo_quartz_font_face_create_for_cgfont(cgfont); // FIXME: error handling return face; @@ -318,11 +320,11 @@ pub impl Font : FontMethods { } fn glyph_index(codepoint: char) -> Option { - self.native_font.glyph_index(codepoint) + self.handle.glyph_index(codepoint) } fn glyph_h_advance(glyph: GlyphIndex) -> FractionalPixel { - match self.native_font.glyph_h_advance(glyph) { + match self.handle.glyph_h_advance(glyph) { Some(adv) => adv, None => /* FIXME: Need fallback strategy */ 10f as FractionalPixel } diff --git a/src/servo/gfx/font_cache.rs b/src/servo/gfx/font_cache.rs index 5c06e54c600..7e2c06cf489 100644 --- a/src/servo/gfx/font_cache.rs +++ b/src/servo/gfx/font_cache.rs @@ -1,8 +1,7 @@ use font::{Font, FontStyle, FontWeight300}; -use native_font::NativeFont; -use font_context::FontContext; +use native::{FontHandle, FontContext}; -// TODO(Issue #164): delete, and get default font from NativeFontMatcher +// TODO(Issue #164): delete, and get default font from font list const TEST_FONT: [u8 * 33004] = #include_bin("JosefinSans-SemiBold.ttf"); fn test_font_bin() -> ~[u8] { @@ -44,7 +43,7 @@ impl FontCache { // TODO: maybe FontStyle should be canonicalized when used in FontCache? priv fn create_font(style: &FontStyle) -> Result<@Font, ()> { let font_bin = @test_font_bin(); - let native_font = NativeFont::new(self.fctx, font_bin, style.pt_size); + let native_font = FontHandle::new(self.fctx, font_bin, style.pt_size); let native_font = if native_font.is_ok() { result::unwrap(move native_font) } else { diff --git a/src/servo/gfx/font_handle.rs b/src/servo/gfx/font_handle.rs new file mode 100644 index 00000000000..0473a5e8755 --- /dev/null +++ b/src/servo/gfx/font_handle.rs @@ -0,0 +1,33 @@ +/** +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/& = quartz::font_handle::QuartzFontHandle; + +#[cfg(target_os = "linux")] +pub type FontHandle/& = freetype::font_handle::FreeTypeFontHandle; + +// TODO: `new` should be part of trait FontHandle + +// TODO(Issue #163): this is a workaround for static methods and +// typedefs not working well together. It should be removed. + +// TODO(Rust #1723): #cfg doesn't work for impl methods, so we have +// to conditionally define the entire impl. +#[cfg(target_os = "macos")] +impl FontHandle { + static pub fn new(fctx: &native::FontContext, buf: @~[u8], pt_size: float) -> Result { + quartz::font_handle::QuartzFontHandle::new(fctx, buf, pt_size) + } +} + +#[cfg(target_os = "linux")] +impl FontHandle { + static pub fn new(fctx: &native::FontContext, buf: @~[u8], pt_size: float) -> Result { + freetype::font_handle::FreeTypeFontHandle::new(fctx, buf, pt_size) + } +} \ No newline at end of file diff --git a/src/servo/gfx/freetype/native_font.rs b/src/servo/gfx/freetype/font_handle.rs similarity index 97% rename from src/servo/gfx/freetype/native_font.rs rename to src/servo/gfx/freetype/font_handle.rs index f877d23bc52..2218eb61e1e 100644 --- a/src/servo/gfx/freetype/native_font.rs +++ b/src/servo/gfx/freetype/font_handle.rs @@ -31,7 +31,7 @@ fn fixed_to_float_ft(f: i32) -> float { fixed_to_float(6, f) } -pub struct FreeTypeNativeFont { +pub struct FreeTypeFontHandle { /// The font binary. This must stay valid for the lifetime of the font buf: @~[u8], face: FT_Face, @@ -44,9 +44,9 @@ pub struct FreeTypeNativeFont { } } -pub impl FreeTypeNativeFont { +pub impl FreeTypeFontHandle { static pub fn new(fctx: &FreeTypeFontContext, - buf: @~[u8], pt_size: float) -> Result { + buf: @~[u8], pt_size: float) -> Result { let ft_ctx = fctx.ctx; assert ft_ctx.is_not_null(); let face: FT_Face = null(); @@ -59,7 +59,7 @@ pub impl FreeTypeNativeFont { 72, // horiz. DPI 72); // vert. DPI if !res.succeeded() { fail ~"unable to set font char size" } - Ok(FreeTypeNativeFont { face: face, buf: buf }) + Ok(FreeTypeFontHandle { face: face, buf: buf }) } else { Err(()) } diff --git a/src/servo/gfx/native.rs b/src/servo/gfx/native.rs new file mode 100644 index 00000000000..8d8154425aa --- /dev/null +++ b/src/servo/gfx/native.rs @@ -0,0 +1,8 @@ +/* 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.rc. This is not ideal and may be changed in the future. */ + +pub use font_handle::FontHandle; +pub use font_context::FontContext; \ No newline at end of file diff --git a/src/servo/gfx/native_font.rs b/src/servo/gfx/native_font.rs deleted file mode 100644 index 79c1b9ee4fc..00000000000 --- a/src/servo/gfx/native_font.rs +++ /dev/null @@ -1,35 +0,0 @@ -/** -NativeFont 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. -*/ - -use font_context::FontContext; - -#[cfg(target_os = "macos")] -pub type NativeFont/& = quartz::native_font::QuartzNativeFont; - -#[cfg(target_os = "linux")] -pub type NativeFont/& = freetype::native_font::FreeTypeNativeFont; - -// TODO: `new` should be part of trait NativeFont - -// TODO(Issue #163): this is a workaround for static methods and -// typedefs not working well together. It should be removed. - -// TODO(Rust #1723): #cfg doesn't work for impl methods, so we have -// to conditionally define the entire impl. -#[cfg(target_os = "macos")] -impl NativeFont { - static pub fn new(fctx: &FontContext, buf: @~[u8], pt_size: float) -> Result { - quartz::native_font::QuartzNativeFont::new(fctx, buf, pt_size) - } -} - -#[cfg(target_os = "linux")] -impl NativeFont { - static pub fn new(fctx: &FontContext, buf: @~[u8], pt_size: float) -> Result { - freetype::native_font::FreeTypeNativeFont::new(fctx, buf, pt_size) - } -} \ No newline at end of file diff --git a/src/servo/gfx/quartz/native_font.rs b/src/servo/gfx/quartz/font_handle.rs similarity index 97% rename from src/servo/gfx/quartz/native_font.rs rename to src/servo/gfx/quartz/font_handle.rs index d8a22614f51..8cad42cce1d 100644 --- a/src/servo/gfx/quartz/native_font.rs +++ b/src/servo/gfx/quartz/font_handle.rs @@ -53,7 +53,7 @@ use ct::font_descriptor::{ kCTFontDefaultOrientation, }; -pub struct QuartzNativeFont { +pub struct QuartzFontHandle { fontprov: CGDataProviderRef, cgfont: CGFontRef, ctfont: CTFontRef, @@ -69,8 +69,8 @@ pub struct QuartzNativeFont { } } -pub impl QuartzNativeFont { - static pub fn new(_fctx: &QuartzFontContext, buf: @~[u8], pt_size: float) -> Result { +pub impl QuartzFontHandle { + static pub fn new(_fctx: &QuartzFontContext, buf: @~[u8], pt_size: float) -> Result { let fontprov = vec::as_imm_buf(*buf, |cbuf, len| { CGDataProviderCreateWithData( null(), @@ -86,7 +86,7 @@ pub impl QuartzNativeFont { let ctfont = ctfont_from_cgfont(cgfont, pt_size); if ctfont.is_null() { return Err(()); } - Ok(QuartzNativeFont { + Ok(QuartzFontHandle { fontprov : fontprov, cgfont : cgfont, ctfont : ctfont, diff --git a/src/servo/servo.rc b/src/servo/servo.rc index fe5c991d70a..694561c408c 100755 --- a/src/servo/servo.rc +++ b/src/servo/servo.rc @@ -88,11 +88,11 @@ pub mod gfx { pub mod glyph; pub mod text_run; - // Typedefs and pub-uses for multiple implementations. If prefixed - // with 'native', then the specific implementataion is composed by - // a non-prefixed version, listed above. + // Typedefs and pub-uses for multiple implementations. + // native contains redirects, so one can write native::FontHandle. + pub mod native; pub mod font_context; - pub mod native_font; + pub mod font_handle; pub mod shaper; // Below are the actual platform-specific parts. @@ -103,13 +103,13 @@ pub mod gfx { #[cfg(target_os = "macos")] pub mod quartz { pub mod font_context; - pub mod native_font; + pub mod font_handle; } #[cfg(target_os = "linux")] pub mod freetype { pub mod font_context; - pub mod native_font; + pub mod font_handle; } }