servo: Rename NativeFont to FontHandle.
authorBrian J. Burg <burg@cs.washington.edu>
Tue, 06 Nov 2012 12:22:29 -0800
changeset 333215 34c4c41c66b7a649e190f34cd5870ada4cc5b5d4
parent 333214 bc4619ac1437e1308962cd9e43595df655cf61c2
child 333216 d7159f8f67e41edd7803915b6ace84275f3f2568
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
servo: Rename NativeFont to FontHandle. Source-Repo: https://github.com/servo/servo Source-Revision: a3f4b52c90ab0accd4173050857f506a91e3f1df
servo/src/servo/gfx.rs
servo/src/servo/gfx/font.rs
servo/src/servo/gfx/font_cache.rs
servo/src/servo/gfx/font_handle.rs
servo/src/servo/gfx/freetype/font_handle.rs
servo/src/servo/gfx/freetype/native_font.rs
servo/src/servo/gfx/native.rs
servo/src/servo/gfx/native_font.rs
servo/src/servo/gfx/quartz/font_handle.rs
servo/src/servo/gfx/quartz/native_font.rs
servo/src/servo/servo.rc
--- a/servo/src/servo/gfx.rs
+++ b/servo/src/servo/gfx.rs
@@ -9,15 +9,14 @@ pub use au = geometry;
 pub use dl = display_list;
 
 pub use display_list::DisplayList;
 pub use font::Font;
 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;
 
 pub use render_context::RenderContext;
 pub use render_layers::RenderLayer;
\ No newline at end of file
--- a/servo/src/servo/gfx/font.rs
+++ b/servo/src/servo/gfx/font.rs
@@ -5,16 +5,18 @@ use azure::{
     AzScaledFontRef,
 };
 use core::dvec::DVec;
 use gfx::RenderContext;
 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;
 
 struct FontMetrics {
     underline_size:   Au,
     underline_offset: Au,
     leading:          Au,
     x_height:         Au,
@@ -67,36 +69,36 @@ struct RunMetrics {
 }
 
 /**
 A font handle. Layout can use this to calculate glyph metrics
 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<AzScaledFontRef>,
     priv mut shaper: Option<@Shaper>,
     style: FontStyle,
     metrics: FontMetrics,
 
     drop {
         use azure::bindgen::AzReleaseScaledFont;
         do (copy self.azure_font).iter |fontref| { AzReleaseScaledFont(*fontref); }
     }
 }
 
 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,
             metrics: move metrics,
         }
     }
 
     priv fn get_shaper(@self) -> @Shaper {
@@ -146,27 +148,27 @@ impl Font {
 
         // TODO: these cairo-related things should be in rust-cairo.
         // creating a cairo font/face from a native font resource
         // should be part of the NativeFont API, not exposed here.
         #[cfg(target_os = "linux")]
         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;
         }
 
         #[cfg(target_os = "macos")]
         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;
         }
 
         fn get_cairo_font(font: &Font) -> *cairo_scaled_font_t {
             use cairo::cairo_matrix_t;
             use cairo::bindgen::{cairo_matrix_init_identity,
@@ -313,21 +315,21 @@ pub impl Font : FontMethods {
         return move store;
     }
 
     fn buf(&self) -> @~[u8] {
         self.fontbuf
     }
 
     fn glyph_index(codepoint: char) -> Option<GlyphIndex> {
-        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
         }
     }
 }
 
 /*fn should_destruct_on_fail_without_leaking() {
     #[test];
--- a/servo/src/servo/gfx/font_cache.rs
+++ b/servo/src/servo/gfx/font_cache.rs
@@ -1,13 +1,12 @@
 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] {
     return vec::from_fn(33004, |i| TEST_FONT[i]);
 }
 
 // Dummy font cache.
 
@@ -39,17 +38,17 @@ impl FontCache {
                 Err(*) => /* FIXME */ fail
             }
         }
     }
 
     // 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 {
             return Err(native_font.get_err());
         };
 
         return Ok(@Font::new(font_bin, move native_font, copy *style));
     }
new file mode 100644
--- /dev/null
+++ b/servo/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<FontHandle, ()> {
+        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<FontHandle, ()> {
+        freetype::font_handle::FreeTypeFontHandle::new(fctx, buf, pt_size)
+    }
+}
\ No newline at end of file
rename from servo/src/servo/gfx/freetype/native_font.rs
rename to servo/src/servo/gfx/freetype/font_handle.rs
--- a/servo/src/servo/gfx/freetype/native_font.rs
+++ b/servo/src/servo/gfx/freetype/font_handle.rs
@@ -26,45 +26,45 @@ use freetype::bindgen::{
 fn float_to_fixed_ft(f: float) -> i32 {
     float_to_fixed(6, f)
 }
 
 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,
 
     drop {
         assert self.face.is_not_null();
         if !FT_Done_Face(self.face).succeeded() {
             fail ~"FT_Done_Face failed";
         }
     }
 }
 
-pub impl FreeTypeNativeFont {
+pub impl FreeTypeFontHandle {
     static pub fn new(fctx: &FreeTypeFontContext,
-                      buf: @~[u8], pt_size: float) -> Result<FreeTypeNativeFont, ()> {
+                      buf: @~[u8], pt_size: float) -> Result<FreeTypeFontHandle, ()> {
         let ft_ctx = fctx.ctx;
         assert ft_ctx.is_not_null();
         let face: FT_Face = null();
         return vec_as_buf(*buf, |cbuf, _len| {
             if FT_New_Memory_Face(ft_ctx, cbuf, (*buf).len() as FT_Long,
                                   0 as FT_Long, addr_of(&face)).succeeded() {
                 let res = FT_Set_Char_Size(face, // the face
                                            float_to_fixed_ft(pt_size) as FT_F26Dot6, // char width
                                            float_to_fixed_ft(pt_size) as FT_F26Dot6, // char height
                                            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(())
             }
         })
     }
 
     pub fn glyph_index(codepoint: char) -> Option<GlyphIndex> {
         assert self.face.is_not_null();
new file mode 100644
--- /dev/null
+++ b/servo/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
deleted file mode 100644
--- a/servo/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<NativeFont, ()> {
-        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<NativeFont, ()> {
-        freetype::native_font::FreeTypeNativeFont::new(fctx, buf, pt_size)
-    }
-}
\ No newline at end of file
rename from servo/src/servo/gfx/quartz/native_font.rs
rename to servo/src/servo/gfx/quartz/font_handle.rs
--- a/servo/src/servo/gfx/quartz/native_font.rs
+++ b/servo/src/servo/gfx/quartz/font_handle.rs
@@ -48,50 +48,50 @@ use ct::font::{
     CTFontGetUnderlinePosition,
     CTFontGetUnderlineThickness,
     CTFontGetXHeight,
 };
 use ct::font_descriptor::{
     kCTFontDefaultOrientation,
 };
 
-pub struct QuartzNativeFont {
+pub struct QuartzFontHandle {
     fontprov: CGDataProviderRef,
     cgfont: CGFontRef,
     ctfont: CTFontRef,
 
     drop {
         assert self.ctfont.is_not_null();
         assert self.cgfont.is_not_null();
         assert self.fontprov.is_not_null();
 
         CFRelease(self.ctfont as CFTypeRef);
         CGFontRelease(self.cgfont);
         CGDataProviderRelease(self.fontprov);
     }
 }
 
-pub impl QuartzNativeFont {
-    static pub fn new(_fctx: &QuartzFontContext, buf: @~[u8], pt_size: float) -> Result<QuartzNativeFont, ()> {
+pub impl QuartzFontHandle {
+    static pub fn new(_fctx: &QuartzFontContext, buf: @~[u8], pt_size: float) -> Result<QuartzFontHandle, ()> {
         let fontprov = vec::as_imm_buf(*buf, |cbuf, len| {
             CGDataProviderCreateWithData(
                 null(),
                 unsafe { transmute(copy cbuf) },
                 len as size_t,
                 null())
         });
         if fontprov.is_null() { return Err(()); }
 
         let cgfont = CGFontCreateWithDataProvider(fontprov);
         if cgfont.is_null() { return Err(()); }
 
         let ctfont = ctfont_from_cgfont(cgfont, pt_size);
         if ctfont.is_null() { return Err(()); }
 
-        Ok(QuartzNativeFont {
+        Ok(QuartzFontHandle {
             fontprov : fontprov,
             cgfont : cgfont,
             ctfont : ctfont,
         })
     }
 
     fn glyph_index(codepoint: char) -> Option<GlyphIndex> {
         assert self.ctfont.is_not_null();
--- a/servo/src/servo/servo.rc
+++ b/servo/src/servo/servo.rc
@@ -83,38 +83,38 @@ pub mod gfx {
 
     // fonts
     pub mod font;
     pub mod font_cache;
     pub mod font_matcher;
     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.
     pub mod harfbuzz {
         pub mod shaper;
     }
 
     #[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;
     }
 }
 
 pub mod image {
     pub mod base;
     pub mod encode {
         pub mod tga;
     }