From 23c35c4c889436060940844837ec7a20cf452841 Mon Sep 17 00:00:00 2001 From: "Brian J. Burg" Date: Mon, 12 Nov 2012 15:24:25 -0800 Subject: [PATCH] Don't pass around a managed font buffer since it's not shared, and not used by font table code at all. Fixes #176. --- src/servo-gfx/font.rs | 20 ++++++-------------- src/servo-gfx/font_context.rs | 2 +- src/servo-gfx/freetype/font.rs | 18 +++++++++++------- src/servo-gfx/quartz/font.rs | 4 ++-- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/servo-gfx/font.rs b/src/servo-gfx/font.rs index 09694edac8b..234efd237bb 100644 --- a/src/servo-gfx/font.rs +++ b/src/servo-gfx/font.rs @@ -42,13 +42,13 @@ pub trait FontHandleMethods { impl FontHandle { #[cfg(target_os = "macos")] - static pub fn new(fctx: &native::FontContextHandle, buf: @~[u8], style: &SpecifiedFontStyle) -> Result { - quartz::font::QuartzFontHandle::new_from_buffer(fctx, buf, style) + static pub fn new(fctx: &native::FontContextHandle, buf: ~[u8], style: &SpecifiedFontStyle) -> Result { + quartz::font::QuartzFontHandle::new_from_buffer(fctx, move buf, style) } #[cfg(target_os = "linux")] - static pub fn new(fctx: &native::FontContextHandle, buf: @~[u8], style: &SpecifiedFontStyle) -> Result { - freetype::font::FreeTypeFontHandle::new(fctx, buf, style) + static pub fn new(fctx: &native::FontContextHandle, buf: ~[u8], style: &SpecifiedFontStyle) -> Result { + freetype::font::FreeTypeFontHandle::new(fctx, move buf, style) } } @@ -238,7 +238,6 @@ A font instance. Layout can use this to calculate glyph metrics and the renderer can use it to render text. */ pub struct Font { - priv mut fontbuf: Option<@~[u8]>, priv handle: FontHandle, priv mut azure_font: Option, priv mut shaper: Option<@Shaper>, @@ -253,10 +252,10 @@ pub struct Font { } impl Font { - static fn new_from_buffer(ctx: &FontContext, buffer: @~[u8], + static fn new_from_buffer(ctx: &FontContext, buffer: ~[u8], style: &SpecifiedFontStyle, backend: BackendType) -> Result<@Font, ()> { - let handle = FontHandle::new(&ctx.handle, buffer, style); + let handle = FontHandle::new(&ctx.handle, move buffer, style); let handle = if handle.is_ok() { result::unwrap(move handle) } else { @@ -267,7 +266,6 @@ impl Font { // TODO(Issue #179): convert between specified and used font style here? return Ok(@Font { - fontbuf : Some(buffer), handle : move handle, azure_font: None, shaper: None, @@ -287,7 +285,6 @@ impl Font { }; return Ok(@Font { - fontbuf : None, handle : move styled_handle, azure_font: None, shaper: None, @@ -353,7 +350,6 @@ pub trait FontMethods { fn shape_text(@self, &str) -> GlyphStore; fn get_descriptor() -> FontDescriptor; - fn buf(&self) -> @~[u8]; // these are used to get glyphs and advances in the case that the // shaper can't figure it out. fn glyph_index(char) -> Option; @@ -461,10 +457,6 @@ pub impl Font : FontMethods { FontDescriptor::new(&font_context::dummy_style(), &SelectorStubDummy) } - fn buf(&self) -> @~[u8] { - option::expect(self.fontbuf, ~"This font has no buffer") - } - fn glyph_index(codepoint: char) -> Option { self.handle.glyph_index(codepoint) } diff --git a/src/servo-gfx/font_context.rs b/src/servo-gfx/font_context.rs index 240da0b3c7a..c46dfa52e35 100644 --- a/src/servo-gfx/font_context.rs +++ b/src/servo-gfx/font_context.rs @@ -125,7 +125,7 @@ pub impl FontContext { priv fn create_font_instance(desc: &FontDescriptor) -> Result<@Font, ()> { return match desc.selector { SelectorStubDummy => { - Font::new_from_buffer(&self, @test_font_bin(), &desc.style, self.backend) + Font::new_from_buffer(&self, test_font_bin(), &desc.style, self.backend) }, // TODO(Issue #174): implement by-platform-name font selectors. SelectorPlatformName(_) => { fail ~"FontContext::create_font_instance() can't yet handle SelectorPlatformName." } diff --git a/src/servo-gfx/freetype/font.rs b/src/servo-gfx/freetype/font.rs index f7ea0d34e40..9b9968380bd 100644 --- a/src/servo-gfx/freetype/font.rs +++ b/src/servo-gfx/freetype/font.rs @@ -31,8 +31,10 @@ fn fixed_to_float_ft(f: i32) -> float { } pub struct FreeTypeFontHandle { - /// The font binary. This must stay valid for the lifetime of the font - buf: @~[u8], + // The font binary. This must stay valid for the lifetime of the font, + // if the font is created using FT_Memory_Face. + // TODO: support both FT_Memory_Face (from memory) and FT_Face (from file) + buf: ~[u8], face: FT_Face, drop { @@ -45,11 +47,10 @@ pub struct FreeTypeFontHandle { 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(); - return vec_as_buf(*buf, |cbuf, _len| { + let face_result: Result = 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 @@ -58,11 +59,14 @@ pub impl FreeTypeFontHandle { 72, // horiz. DPI 72); // vert. DPI if !res.succeeded() { fail ~"unable to set font char size" } - Ok(FreeTypeFontHandle { face: face, buf: buf }) + Ok(face) } else { Err(()) } - }) + }); + return do result::chain(face_result) |face| { + Ok(FreeTypeFontHandle { face: face, buf: move buf }) + }; } pub fn glyph_index(codepoint: char) -> Option { diff --git a/src/servo-gfx/quartz/font.rs b/src/servo-gfx/quartz/font.rs index 98a8d76d8e9..e566e8fcc62 100644 --- a/src/servo-gfx/quartz/font.rs +++ b/src/servo-gfx/quartz/font.rs @@ -56,9 +56,9 @@ pub struct QuartzFontHandle { } pub impl QuartzFontHandle { - static fn new_from_buffer(_fctx: &QuartzFontContextHandle, buf: @~[u8], + static fn new_from_buffer(_fctx: &QuartzFontContextHandle, buf: ~[u8], style: &SpecifiedFontStyle) -> Result { - let fontprov = vec::as_imm_buf(*buf, |cbuf, len| { + let fontprov = vec::as_imm_buf(buf, |cbuf, len| { CGDataProvider::new_from_buffer(cbuf, len) });