diff options
author | Brian J. Burg <burg@cs.washington.edu> | 2012-10-30 12:01:18 -0700 |
---|---|---|
committer | Brian J. Burg <burg@cs.washington.edu> | 2012-10-30 12:07:16 -0700 |
commit | b7d4ec12174384fe7d373c45c5f7e712ea16d45b (patch) | |
tree | 548cfaf06db18cf53cac21152cfb96be902791b2 /src | |
parent | b545e4821e23bdbbeca15e293787fe507b1cf2e7 (diff) | |
download | servo-b7d4ec12174384fe7d373c45c5f7e712ea16d45b.tar.gz servo-b7d4ec12174384fe7d373c45c5f7e712ea16d45b.zip |
Create FontContext from matcher, remove platform-specific matchers. Move default font into FontCache. Fixes #166.
Diffstat (limited to 'src')
-rw-r--r-- | src/servo/gfx/render_task.rs | 4 | ||||
-rw-r--r-- | src/servo/layout/layout_task.rs | 7 | ||||
-rwxr-xr-x | src/servo/servo.rc | 6 | ||||
-rw-r--r-- | src/servo/text/font.rs | 25 | ||||
-rw-r--r-- | src/servo/text/font_cache.rs | 36 | ||||
-rw-r--r-- | src/servo/text/font_context.rs | 25 | ||||
-rw-r--r-- | src/servo/text/font_matcher.rs | 37 | ||||
-rw-r--r-- | src/servo/text/freetype/font_context.rs (renamed from src/servo/text/freetype/native_font_matcher.rs) | 16 | ||||
-rw-r--r-- | src/servo/text/freetype/native_font.rs | 10 | ||||
-rw-r--r-- | src/servo/text/native_font.rs | 10 | ||||
-rw-r--r-- | src/servo/text/native_font_matcher.rs | 25 | ||||
-rw-r--r-- | src/servo/text/quartz/font_context.rs | 12 | ||||
-rw-r--r-- | src/servo/text/quartz/native_font.rs | 4 | ||||
-rw-r--r-- | src/servo/text/quartz/native_font_matcher.rs | 13 |
14 files changed, 118 insertions, 112 deletions
diff --git a/src/servo/gfx/render_task.rs b/src/servo/gfx/render_task.rs index 2256fa94cb5..2df891d8f6e 100644 --- a/src/servo/gfx/render_task.rs +++ b/src/servo/gfx/render_task.rs @@ -15,7 +15,7 @@ use render_context::RenderContext; use render_layers::render_layers; use std::cell::Cell; use text::font_cache::FontCache; -use text::font_matcher::FontMatcher; +use text::font_context::FontContext; pub enum Msg { RenderMsg(RenderLayer), @@ -36,7 +36,7 @@ pub fn RenderTask<C: Compositor Send>(compositor: C) -> RenderTask { port: po, compositor: move compositor, mut layer_buffer_set_port: Cell(move layer_buffer_set_port), - font_cache: @FontCache::new(@FontMatcher::new()), + font_cache: @FontCache::new(@FontContext::new()), }.start(); } } diff --git a/src/servo/layout/layout_task.rs b/src/servo/layout/layout_task.rs index 454dfa0ec54..2ef9f60c321 100644 --- a/src/servo/layout/layout_task.rs +++ b/src/servo/layout/layout_task.rs @@ -25,6 +25,7 @@ use opt = core::option; use render_task::RenderTask; use resource::image_cache_task::{ImageCacheTask, ImageResponseMsg}; use resource::local_image_cache::LocalImageCache; +use servo_text::font_context::FontContext; use servo_text::font_cache::FontCache; use servo_text::font_matcher::FontMatcher; use std::arc::ARC; @@ -77,6 +78,7 @@ struct Layout { from_content: comm::Port<Msg>, font_cache: @FontCache, + font_matcher: @FontMatcher, // This is used to root auxilliary RCU reader data layout_refs: DVec<@LayoutData> } @@ -85,12 +87,15 @@ fn Layout(render_task: RenderTask, image_cache_task: ImageCacheTask, from_content: comm::Port<Msg>) -> Layout { + let fctx = @FontContext::new(); + Layout { render_task: render_task, image_cache_task: image_cache_task.clone(), local_image_cache: @LocalImageCache(move image_cache_task), from_content: from_content, - font_cache: @FontCache::new(@FontMatcher::new()), + font_matcher: @FontMatcher::new(fctx), + font_cache: @FontCache::new(fctx), layout_refs: DVec() } } diff --git a/src/servo/servo.rc b/src/servo/servo.rc index ebde8f1d538..7c86e97ec3d 100755 --- a/src/servo/servo.rc +++ b/src/servo/servo.rc @@ -109,8 +109,8 @@ pub mod text { pub mod util; // platform and library-specific implementations. + pub mod font_context; pub mod native_font; - pub mod native_font_matcher; pub mod shaper; pub mod harfbuzz { @@ -119,14 +119,14 @@ pub mod text { #[cfg(target_os = "macos")] pub mod quartz { + pub mod font_context; pub mod native_font; - pub mod native_font_matcher; } #[cfg(target_os = "linux")] pub mod freetype { + pub mod font_context; pub mod native_font; - pub mod native_font_matcher; } } diff --git a/src/servo/text/font.rs b/src/servo/text/font.rs index 7e290195efe..96e0a22ed09 100644 --- a/src/servo/text/font.rs +++ b/src/servo/text/font.rs @@ -340,16 +340,18 @@ fn should_destruct_on_fail_without_leaking() { #[test]; #[should_fail]; - let lib = FontCache(); - let _font = lib.get_test_font(); + let fctx = @FontContext(); + let matcher = @FontMatcher(fctx); + let _font = matcher.get_test_font(); fail; } fn should_get_glyph_indexes() { #[test]; - let lib = FontCache(); - let font = lib.get_test_font(); + let fctx = @FontContext(); + let matcher = @FontMatcher(fctx); + let font = matcher.get_test_font(); let glyph_idx = font.glyph_index('w'); assert glyph_idx == Some(40u as GlyphIndex); } @@ -358,8 +360,9 @@ fn should_get_glyph_advance() { #[test]; #[ignore]; - let lib = FontCache(); - let font = lib.get_test_font(); + let fctx = @FontContext(); + let matcher = @FontMatcher(fctx); + let font = matcher.get_test_font(); let x = font.glyph_h_advance(40u as GlyphIndex); assert x == 15f || x == 16f; } @@ -375,8 +378,9 @@ fn should_get_glyph_advance_stress() { let (chan, port) = pipes::stream(); ports += [@move port]; do task::spawn |move chan| { - let lib = FontCache(); - let font = lib.get_test_font(); + let fctx = @FontContext(); + let matcher = @FontMatcher(fctx); + let _font = matcher.get_test_font(); let x = font.glyph_h_advance(40u as GlyphIndex); assert x == 15f || x == 16f; chan.send(()); @@ -393,8 +397,9 @@ fn should_be_able_to_create_instances_in_multiple_threads() { for iter::repeat(10u) { do task::spawn { - let lib = FontCache(); - let _font = lib.get_test_font(); + let fctx = @FontContext(); + let matcher = @FontMatcher(fctx); + let _font = matcher.get_test_font(); } } } diff --git a/src/servo/text/font_cache.rs b/src/servo/text/font_cache.rs index 0c004c79127..5c06e54c600 100644 --- a/src/servo/text/font_cache.rs +++ b/src/servo/text/font_cache.rs @@ -1,16 +1,25 @@ use font::{Font, FontStyle, FontWeight300}; -use font_matcher::FontMatcher; +use native_font::NativeFont; +use font_context::FontContext; + +// TODO(Issue #164): delete, and get default font from NativeFontMatcher +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. struct FontCache { - matcher: @FontMatcher, + fctx: @FontContext, mut cached_font: Option<@Font> } impl FontCache { - static pub fn new(matcher: @FontMatcher) -> FontCache { + static pub fn new(fctx: @FontContext) -> FontCache { FontCache { - matcher: matcher, + fctx: fctx, cached_font: None } } @@ -25,10 +34,27 @@ impl FontCache { return match self.cached_font { Some(font) => font, - None => match self.matcher.get_font(&dummy_style) { + None => match self.get_font(&dummy_style) { Ok(font) => { self.cached_font = Some(font); font } 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 = 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)); + } + + pub fn get_font(@self, style: &FontStyle) -> Result<@Font, ()> { + self.create_font(style) + } }
\ No newline at end of file diff --git a/src/servo/text/font_context.rs b/src/servo/text/font_context.rs new file mode 100644 index 00000000000..fbaab9541b9 --- /dev/null +++ b/src/servo/text/font_context.rs @@ -0,0 +1,25 @@ +// 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")] +type FontContext/& = quartz::font_context::QuartzFontContext; + +#[cfg(target_os = "linux")] +type FontContext/& = freetype::font_context::FreeTypeFontContext; + +#[cfg(target_os = "macos")] +pub impl FontContext { + static pub fn new() -> FontContext { + quartz::font_context::QuartzFontContext::new() + } +} + +#[cfg(target_os = "linux")] +pub impl FontContext { + static pub fn new() -> FontContext { + freetype::font_context::FreeTypeFontContext::new() + } +} diff --git a/src/servo/text/font_matcher.rs b/src/servo/text/font_matcher.rs index 4ef065161a3..f819fc47bcf 100644 --- a/src/servo/text/font_matcher.rs +++ b/src/servo/text/font_matcher.rs @@ -1,43 +1,14 @@ use font::{Font, FontStyle}; -use native_font::NativeFont; -use native_font_matcher::NativeFontMatcher; - -// TODO(Issue #164): delete, and get default font from NativeFontMatcher -const TEST_FONT: [u8 * 33004] = #include_bin("JosefinSans-SemiBold.ttf"); - -fn test_font_bin() -> ~[u8] { - return vec::from_fn(33004, |i| TEST_FONT[i]); -} +use font_context::FontContext; struct FontMatcher { - native_matcher: NativeFontMatcher, - // TODO(Issue #165): move into FontCache - mut cached_font: Option<@Font>, + fctx: @FontContext, } impl FontMatcher { - static pub fn new() -> FontMatcher { + static pub fn new(fctx: @FontContext) -> FontMatcher { FontMatcher { - native_matcher: NativeFontMatcher::new(), - cached_font: None + fctx: fctx, } } - - // TODO: maybe FontStyle should be canonicalized when used in FontCache? - // TODO(Issue #166): move this to FontCache or something? At the least, use it there. - priv fn create_font(style: &FontStyle) -> Result<@Font, ()> { - let font_bin = @test_font_bin(); - let native_font = NativeFont::new(&self.native_matcher, 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)); - } - - pub fn get_font(@self, style: &FontStyle) -> Result<@Font, ()> { - self.create_font(style) - } } diff --git a/src/servo/text/freetype/native_font_matcher.rs b/src/servo/text/freetype/font_context.rs index ffb7f6e4dff..c28c0f18cd4 100644 --- a/src/servo/text/freetype/native_font_matcher.rs +++ b/src/servo/text/freetype/font_context.rs @@ -10,24 +10,24 @@ use freetype::bindgen::{ }; -pub struct FreeTypeNativeFontMatcher { - ft_lib: FT_Library, +pub struct FreeTypeFontContext { + ctx: FT_Library, drop { - assert self.ft_lib.is_not_null(); - FT_Done_FreeType(self.ft_lib); + assert self.ctx.is_not_null(); + FT_Done_FreeType(self.ctx); } } -pub impl FreeTypeNativeFontMatcher { - static pub fn new() -> FreeTypeNativeFontMatcher { +pub impl FreeTypeFontContext { + static pub fn new() -> FreeTypeFontContext { let lib: FT_Library = ptr::null(); let res = FT_Init_FreeType(ptr::addr_of(&lib)); // FIXME: error handling assert res == 0 as FT_Error; - FreeTypeNativeFontMatcher { - ft_lib: lib, + FreeTypeFontContext { + ctx: lib, } } }
\ No newline at end of file diff --git a/src/servo/text/freetype/native_font.rs b/src/servo/text/freetype/native_font.rs index 2fe1ddf9ad3..5f7ce50f083 100644 --- a/src/servo/text/freetype/native_font.rs +++ b/src/servo/text/freetype/native_font.rs @@ -1,7 +1,7 @@ extern mod freetype; use font::{FontMetrics, FractionalPixel}; -use native_font_matcher::FreeTypeNativeFontMatcher; +use font_context::FreeTypeFontContext; use gfx::geometry; use gfx::geometry::Au; @@ -45,13 +45,13 @@ pub struct FreeTypeNativeFont { } pub impl FreeTypeNativeFont { - static pub fn new(matcher: &FreeTypeNativeFontMatcher, + static pub fn new(fctx: &FreeTypeFontContext, buf: @~[u8], pt_size: float) -> Result<FreeTypeNativeFont, ()> { - let lib = matcher.ft_lib; - assert lib.is_not_null(); + 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(lib, cbuf, (*buf).len() as FT_Long, + 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 diff --git a/src/servo/text/native_font.rs b/src/servo/text/native_font.rs index 143825da2bc..79c1b9ee4fc 100644 --- a/src/servo/text/native_font.rs +++ b/src/servo/text/native_font.rs @@ -5,7 +5,7 @@ needed by the text shaper as well as access to the underlying font resources needed by the graphics layer to draw glyphs. */ -use native_font_matcher::NativeFontMatcher; +use font_context::FontContext; #[cfg(target_os = "macos")] pub type NativeFont/& = quartz::native_font::QuartzNativeFont; @@ -22,14 +22,14 @@ pub type NativeFont/& = freetype::native_font::FreeTypeNativeFont; // to conditionally define the entire impl. #[cfg(target_os = "macos")] impl NativeFont { - static pub fn new(native_lib: &NativeFontMatcher, buf: @~[u8], pt_size: float) -> Result<NativeFont, ()> { - quartz::native_font::QuartzNativeFont::new(native_lib, buf, pt_size) + 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(native_lib: &NativeFontMatcher, buf: @~[u8], pt_size: float) -> Result<NativeFont, ()> { - freetype::native_font::FreeTypeNativeFont::new(native_lib, buf, pt_size) + 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 diff --git a/src/servo/text/native_font_matcher.rs b/src/servo/text/native_font_matcher.rs deleted file mode 100644 index 36248ec9c67..00000000000 --- a/src/servo/text/native_font_matcher.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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")] -type NativeFontMatcher/& = quartz::native_font_matcher::QuartzNativeFontMatcher; - -#[cfg(target_os = "linux")] -type NativeFontMatcher/& = freetype::native_font_matcher::FreeTypeNativeFontMatcher; - -#[cfg(target_os = "macos")] -pub impl NativeFontMatcher { - static pub fn new() -> NativeFontMatcher { - quartz::native_font_matcher::QuartzNativeFontMatcher::new() - } -} - -#[cfg(target_os = "linux")] -pub impl NativeFontMatcher { - static pub fn new() -> NativeFontMatcher { - freetype::native_font_matcher::FreeTypeNativeFontMatcher::new() - } -} diff --git a/src/servo/text/quartz/font_context.rs b/src/servo/text/quartz/font_context.rs new file mode 100644 index 00000000000..b66cc499eba --- /dev/null +++ b/src/servo/text/quartz/font_context.rs @@ -0,0 +1,12 @@ +pub struct QuartzFontContext { + ctx: u8, + + drop { } +} + +pub impl QuartzFontContext { + // this is a placeholder until NSFontManager or whatever is bound in here. + static pub fn new() -> QuartzFontContext { + QuartzFontContext { ctx: 42 } + } +}
\ No newline at end of file diff --git a/src/servo/text/quartz/native_font.rs b/src/servo/text/quartz/native_font.rs index 363e29a3e75..e4f96a39ad6 100644 --- a/src/servo/text/quartz/native_font.rs +++ b/src/servo/text/quartz/native_font.rs @@ -3,7 +3,7 @@ extern mod core_graphics; extern mod core_text; use font::{FontMetrics, FractionalPixel}; -use native_font_matcher::QuartzNativeFontMatcher; +use font_context::QuartzFontContext; use au = gfx::geometry; use cast::transmute; @@ -68,7 +68,7 @@ pub struct QuartzNativeFont { } pub impl QuartzNativeFont { - static pub fn new(_lib: &QuartzNativeFontMatcher, buf: @~[u8], pt_size: float) -> Result<QuartzNativeFont, ()> { + static pub fn new(_fctx: &QuartzFontContext, buf: @~[u8], pt_size: float) -> Result<QuartzNativeFont, ()> { let fontprov = vec::as_imm_buf(*buf, |cbuf, len| { CGDataProviderCreateWithData( null(), diff --git a/src/servo/text/quartz/native_font_matcher.rs b/src/servo/text/quartz/native_font_matcher.rs deleted file mode 100644 index ac18aedea93..00000000000 --- a/src/servo/text/quartz/native_font_matcher.rs +++ /dev/null @@ -1,13 +0,0 @@ - -pub struct QuartzNativeFontMatcher { - dummy: int, - - drop { } -} - -pub impl QuartzNativeFontMatcher { - // this is a placeholder until NSFontManager or whatever is bound in here. - static pub fn new() -> QuartzNativeFontMatcher { - QuartzNativeFontMatcher { dummy: 42 } - } -}
\ No newline at end of file |