From 5c5ae3a56e97b8650589f656bab44844569454e6 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Wed, 2 Jul 2014 15:23:01 -0500 Subject: [PATCH] servo: Merge #2745 - Font refactoring - remove unstyled method, update android freetype (from glennw:remove-unstyled-method) Source-Repo: https://github.com/servo/servo Source-Revision: d637bc71c2ca27f12b63bf7e185402f3d474146c --- .../components/gfx/platform/android/font.rs | 38 ++++---------- .../gfx/platform/android/font_context.rs | 51 +++++++++++++++++-- .../gfx/platform/android/font_list.rs | 6 ++- .../src/components/gfx/platform/linux/font.rs | 34 +++---------- .../gfx/platform/linux/font_context.rs | 2 +- .../gfx/platform/linux/font_list.rs | 4 +- 6 files changed, 72 insertions(+), 63 deletions(-) diff --git a/servo/src/components/gfx/platform/android/font.rs b/servo/src/components/gfx/platform/android/font.rs index 3ba5b3e629ac..1b564c6f7c49 100644 --- a/servo/src/components/gfx/platform/android/font.rs +++ b/servo/src/components/gfx/platform/android/font.rs @@ -5,7 +5,7 @@ extern crate freetype; use font::{FontHandleMethods, FontMetrics, FontTableMethods}; -use font::{FontTableTag, FractionalPixel, SpecifiedFontStyle, UsedFontStyle}; +use font::{FontTableTag, FractionalPixel, SpecifiedFontStyle}; use servo_util::geometry::Au; use servo_util::geometry; use platform::font_context::FontContextHandle; @@ -46,7 +46,7 @@ impl FontTableMethods for FontTable { } } -enum FontSource { +pub enum FontSource { FontSourceMem(Vec), FontSourceFile(String) } @@ -264,7 +264,7 @@ impl<'a> FontHandle { } pub fn new_from_file(fctx: &FontContextHandle, file: &str, - style: &SpecifiedFontStyle) -> Result { + maybe_style: Option<&SpecifiedFontStyle>) -> Result { unsafe { let ft_ctx: FT_Library = fctx.ctx.ctx; if ft_ctx.is_null() { return Err(()); } @@ -278,7 +278,13 @@ impl<'a> FontHandle { if face.is_null() { return Err(()); } - if FontHandle::set_char_size(face, style.pt_size).is_ok() { + + let ok = match maybe_style { + Some(style) => FontHandle::set_char_size(face, style.pt_size).is_ok(), + None => true, + }; + + if ok { Ok(FontHandle { source: FontSourceFile(file.to_str()), face: face, @@ -290,30 +296,6 @@ impl<'a> FontHandle { } } - pub fn new_from_file_unstyled(fctx: &FontContextHandle, file: String) - -> Result { - unsafe { - let ft_ctx: FT_Library = fctx.ctx.ctx; - if ft_ctx.is_null() { return Err(()); } - - let mut face: FT_Face = ptr::null(); - let face_index = 0 as FT_Long; - file.to_c_str().with_ref(|file_str| { - FT_New_Face(ft_ctx, file_str, - face_index, &mut face); - }); - if face.is_null() { - return Err(()); - } - - Ok(FontHandle { - source: FontSourceFile(file), - face: face, - handle: fctx.clone() - }) - } - } - fn get_face_rec(&'a self) -> &'a FT_FaceRec { unsafe { &(*self.face) diff --git a/servo/src/components/gfx/platform/android/font_context.rs b/servo/src/components/gfx/platform/android/font_context.rs index cea1cbf5cb06..dad6e6252cf1 100644 --- a/servo/src/components/gfx/platform/android/font_context.rs +++ b/servo/src/components/gfx/platform/android/font_context.rs @@ -7,12 +7,41 @@ use platform::font::FontHandle; use font_context::FontContextHandleMethods; use platform::font_list::path_from_identifier; -use freetype::freetype::{FTErrorMethods, FT_Library}; -use freetype::freetype::{FT_Done_FreeType, FT_Init_FreeType}; +use freetype::freetype::FTErrorMethods; +use freetype::freetype::FT_Add_Default_Modules; +use freetype::freetype::FT_Done_FreeType; +use freetype::freetype::FT_Library; +use freetype::freetype::FT_Memory; +use freetype::freetype::FT_New_Library; +use freetype::freetype::struct_FT_MemoryRec_; use std::ptr; use std::rc::Rc; +use libc; +use libc::{c_void, c_long, size_t, malloc}; +use std::mem; + +extern fn ft_alloc(_mem: FT_Memory, size: c_long) -> *c_void { + unsafe { + let ptr = libc::malloc(size as size_t); + ptr as *c_void + } +} + +extern fn ft_free(_mem: FT_Memory, block: *c_void) { + unsafe { + libc::free(block as *mut c_void); + } +} + +extern fn ft_realloc(_mem: FT_Memory, _cur_size: c_long, new_size: c_long, block: *c_void) -> *c_void { + unsafe { + let ptr = libc::realloc(block as *mut c_void, new_size as size_t); + ptr as *c_void + } +} + #[deriving(Clone)] pub struct FreeTypeLibraryHandle { pub ctx: FT_Library, @@ -33,9 +62,23 @@ impl Drop for FreeTypeLibraryHandle { impl FontContextHandle { pub fn new() -> FontContextHandle { unsafe { + + let ptr = libc::malloc(mem::size_of::() as size_t); + let allocator: &mut struct_FT_MemoryRec_ = mem::transmute(ptr); + mem::overwrite(allocator, struct_FT_MemoryRec_ { + user: ptr::null(), + alloc: ft_alloc, + free: ft_free, + realloc: ft_realloc, + }); + let ctx: FT_Library = ptr::null(); - let result = FT_Init_FreeType(&ctx); + + let result = FT_New_Library(ptr as FT_Memory, &ctx); if !result.succeeded() { fail!("Unable to initialize FreeType library"); } + + FT_Add_Default_Modules(ctx); + FontContextHandle { ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx }), } @@ -49,7 +92,7 @@ impl FontContextHandleMethods for FontContextHandle { debug!("Creating font handle for {:s}", name); path_from_identifier(name, &style).and_then(|file_name| { debug!("Opening font face {:s}", file_name); - FontHandle::new_from_file(self, file_name.as_slice(), &style) + FontHandle::new_from_file(self, file_name.as_slice(), Some(&style)) }) } } diff --git a/servo/src/components/gfx/platform/android/font_list.rs b/servo/src/components/gfx/platform/android/font_list.rs index db990e55af2c..378cf42e71fc 100644 --- a/servo/src/components/gfx/platform/android/font_list.rs +++ b/servo/src/components/gfx/platform/android/font_list.rs @@ -2,6 +2,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#![allow(uppercase_variables)] + extern crate freetype; extern crate fontconfig; @@ -114,8 +116,8 @@ impl FontListHandle { debug!("variation file: {}", file); debug!("variation index: {}", index); - let font_handle = FontHandle::new_from_file_unstyled(&self.fctx, - file); + let font_handle = FontHandle::new_from_file(&self.fctx, + file.as_slice(), None); let font_handle = font_handle.unwrap(); debug!("Creating new FontEntry for face: {:s}", font_handle.face_name()); diff --git a/servo/src/components/gfx/platform/linux/font.rs b/servo/src/components/gfx/platform/linux/font.rs index 4df1b775aa99..1b564c6f7c49 100644 --- a/servo/src/components/gfx/platform/linux/font.rs +++ b/servo/src/components/gfx/platform/linux/font.rs @@ -264,7 +264,7 @@ impl<'a> FontHandle { } pub fn new_from_file(fctx: &FontContextHandle, file: &str, - style: &SpecifiedFontStyle) -> Result { + maybe_style: Option<&SpecifiedFontStyle>) -> Result { unsafe { let ft_ctx: FT_Library = fctx.ctx.ctx; if ft_ctx.is_null() { return Err(()); } @@ -278,7 +278,13 @@ impl<'a> FontHandle { if face.is_null() { return Err(()); } - if FontHandle::set_char_size(face, style.pt_size).is_ok() { + + let ok = match maybe_style { + Some(style) => FontHandle::set_char_size(face, style.pt_size).is_ok(), + None => true, + }; + + if ok { Ok(FontHandle { source: FontSourceFile(file.to_str()), face: face, @@ -290,30 +296,6 @@ impl<'a> FontHandle { } } - pub fn new_from_file_unstyled(fctx: &FontContextHandle, file: String) - -> Result { - unsafe { - let ft_ctx: FT_Library = fctx.ctx.ctx; - if ft_ctx.is_null() { return Err(()); } - - let mut face: FT_Face = ptr::null(); - let face_index = 0 as FT_Long; - file.to_c_str().with_ref(|file_str| { - FT_New_Face(ft_ctx, file_str, - face_index, &mut face); - }); - if face.is_null() { - return Err(()); - } - - Ok(FontHandle { - source: FontSourceFile(file), - face: face, - handle: fctx.clone() - }) - } - } - fn get_face_rec(&'a self) -> &'a FT_FaceRec { unsafe { &(*self.face) diff --git a/servo/src/components/gfx/platform/linux/font_context.rs b/servo/src/components/gfx/platform/linux/font_context.rs index 520ac5111b65..dad6e6252cf1 100644 --- a/servo/src/components/gfx/platform/linux/font_context.rs +++ b/servo/src/components/gfx/platform/linux/font_context.rs @@ -92,7 +92,7 @@ impl FontContextHandleMethods for FontContextHandle { debug!("Creating font handle for {:s}", name); path_from_identifier(name, &style).and_then(|file_name| { debug!("Opening font face {:s}", file_name); - FontHandle::new_from_file(self, file_name.as_slice(), &style) + FontHandle::new_from_file(self, file_name.as_slice(), Some(&style)) }) } } diff --git a/servo/src/components/gfx/platform/linux/font_list.rs b/servo/src/components/gfx/platform/linux/font_list.rs index 6f7c0b4bc787..9dafbd781c08 100644 --- a/servo/src/components/gfx/platform/linux/font_list.rs +++ b/servo/src/components/gfx/platform/linux/font_list.rs @@ -116,8 +116,8 @@ impl FontListHandle { debug!("variation file: {}", file); debug!("variation index: {}", index); - let font_handle = FontHandle::new_from_file_unstyled(&self.fctx, - file); + let font_handle = FontHandle::new_from_file(&self.fctx, + file.as_slice(), None); let font_handle = font_handle.unwrap(); debug!("Creating new FontEntry for face: {:s}", font_handle.face_name());