зеркало из https://github.com/mozilla/gecko-dev.git
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
This commit is contained in:
Родитель
ab233e59b2
Коммит
5c5ae3a56e
|
@ -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<u8>),
|
||||
FontSourceFile(String)
|
||||
}
|
||||
|
@ -264,7 +264,7 @@ impl<'a> FontHandle {
|
|||
}
|
||||
|
||||
pub fn new_from_file(fctx: &FontContextHandle, file: &str,
|
||||
style: &SpecifiedFontStyle) -> Result<FontHandle, ()> {
|
||||
maybe_style: Option<&SpecifiedFontStyle>) -> Result<FontHandle, ()> {
|
||||
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<FontHandle, ()> {
|
||||
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)
|
||||
|
|
|
@ -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::<struct_FT_MemoryRec_>() 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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -264,7 +264,7 @@ impl<'a> FontHandle {
|
|||
}
|
||||
|
||||
pub fn new_from_file(fctx: &FontContextHandle, file: &str,
|
||||
style: &SpecifiedFontStyle) -> Result<FontHandle, ()> {
|
||||
maybe_style: Option<&SpecifiedFontStyle>) -> Result<FontHandle, ()> {
|
||||
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<FontHandle, ()> {
|
||||
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)
|
||||
|
|
|
@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
Загрузка…
Ссылка в новой задаче