mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
Font refactoring - remove unstyled method, update android freetype
init to use allocator hooks.
This commit is contained in:
parent
ebd7fb060d
commit
77d56034c9
6 changed files with 72 additions and 63 deletions
|
@ -5,7 +5,7 @@
|
||||||
extern crate freetype;
|
extern crate freetype;
|
||||||
|
|
||||||
use font::{FontHandleMethods, FontMetrics, FontTableMethods};
|
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::Au;
|
||||||
use servo_util::geometry;
|
use servo_util::geometry;
|
||||||
use platform::font_context::FontContextHandle;
|
use platform::font_context::FontContextHandle;
|
||||||
|
@ -46,7 +46,7 @@ impl FontTableMethods for FontTable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum FontSource {
|
pub enum FontSource {
|
||||||
FontSourceMem(Vec<u8>),
|
FontSourceMem(Vec<u8>),
|
||||||
FontSourceFile(String)
|
FontSourceFile(String)
|
||||||
}
|
}
|
||||||
|
@ -264,7 +264,7 @@ impl<'a> FontHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_from_file(fctx: &FontContextHandle, file: &str,
|
pub fn new_from_file(fctx: &FontContextHandle, file: &str,
|
||||||
style: &SpecifiedFontStyle) -> Result<FontHandle, ()> {
|
maybe_style: Option<&SpecifiedFontStyle>) -> Result<FontHandle, ()> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ft_ctx: FT_Library = fctx.ctx.ctx;
|
let ft_ctx: FT_Library = fctx.ctx.ctx;
|
||||||
if ft_ctx.is_null() { return Err(()); }
|
if ft_ctx.is_null() { return Err(()); }
|
||||||
|
@ -278,7 +278,13 @@ impl<'a> FontHandle {
|
||||||
if face.is_null() {
|
if face.is_null() {
|
||||||
return Err(());
|
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 {
|
Ok(FontHandle {
|
||||||
source: FontSourceFile(file.to_str()),
|
source: FontSourceFile(file.to_str()),
|
||||||
face: face,
|
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 {
|
fn get_face_rec(&'a self) -> &'a FT_FaceRec {
|
||||||
unsafe {
|
unsafe {
|
||||||
&(*self.face)
|
&(*self.face)
|
||||||
|
|
|
@ -7,12 +7,41 @@ use platform::font::FontHandle;
|
||||||
use font_context::FontContextHandleMethods;
|
use font_context::FontContextHandleMethods;
|
||||||
use platform::font_list::path_from_identifier;
|
use platform::font_list::path_from_identifier;
|
||||||
|
|
||||||
use freetype::freetype::{FTErrorMethods, FT_Library};
|
use freetype::freetype::FTErrorMethods;
|
||||||
use freetype::freetype::{FT_Done_FreeType, FT_Init_FreeType};
|
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::ptr;
|
||||||
use std::rc::Rc;
|
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)]
|
#[deriving(Clone)]
|
||||||
pub struct FreeTypeLibraryHandle {
|
pub struct FreeTypeLibraryHandle {
|
||||||
pub ctx: FT_Library,
|
pub ctx: FT_Library,
|
||||||
|
@ -33,9 +62,23 @@ impl Drop for FreeTypeLibraryHandle {
|
||||||
impl FontContextHandle {
|
impl FontContextHandle {
|
||||||
pub fn new() -> FontContextHandle {
|
pub fn new() -> FontContextHandle {
|
||||||
unsafe {
|
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 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"); }
|
if !result.succeeded() { fail!("Unable to initialize FreeType library"); }
|
||||||
|
|
||||||
|
FT_Add_Default_Modules(ctx);
|
||||||
|
|
||||||
FontContextHandle {
|
FontContextHandle {
|
||||||
ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx }),
|
ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx }),
|
||||||
}
|
}
|
||||||
|
@ -49,7 +92,7 @@ impl FontContextHandleMethods for FontContextHandle {
|
||||||
debug!("Creating font handle for {:s}", name);
|
debug!("Creating font handle for {:s}", name);
|
||||||
path_from_identifier(name, &style).and_then(|file_name| {
|
path_from_identifier(name, &style).and_then(|file_name| {
|
||||||
debug!("Opening font face {:s}", 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
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
#![allow(uppercase_variables)]
|
||||||
|
|
||||||
extern crate freetype;
|
extern crate freetype;
|
||||||
extern crate fontconfig;
|
extern crate fontconfig;
|
||||||
|
|
||||||
|
@ -114,8 +116,8 @@ impl FontListHandle {
|
||||||
debug!("variation file: {}", file);
|
debug!("variation file: {}", file);
|
||||||
debug!("variation index: {}", index);
|
debug!("variation index: {}", index);
|
||||||
|
|
||||||
let font_handle = FontHandle::new_from_file_unstyled(&self.fctx,
|
let font_handle = FontHandle::new_from_file(&self.fctx,
|
||||||
file);
|
file.as_slice(), None);
|
||||||
let font_handle = font_handle.unwrap();
|
let font_handle = font_handle.unwrap();
|
||||||
|
|
||||||
debug!("Creating new FontEntry for face: {:s}", font_handle.face_name());
|
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,
|
pub fn new_from_file(fctx: &FontContextHandle, file: &str,
|
||||||
style: &SpecifiedFontStyle) -> Result<FontHandle, ()> {
|
maybe_style: Option<&SpecifiedFontStyle>) -> Result<FontHandle, ()> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ft_ctx: FT_Library = fctx.ctx.ctx;
|
let ft_ctx: FT_Library = fctx.ctx.ctx;
|
||||||
if ft_ctx.is_null() { return Err(()); }
|
if ft_ctx.is_null() { return Err(()); }
|
||||||
|
@ -278,7 +278,13 @@ impl<'a> FontHandle {
|
||||||
if face.is_null() {
|
if face.is_null() {
|
||||||
return Err(());
|
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 {
|
Ok(FontHandle {
|
||||||
source: FontSourceFile(file.to_str()),
|
source: FontSourceFile(file.to_str()),
|
||||||
face: face,
|
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 {
|
fn get_face_rec(&'a self) -> &'a FT_FaceRec {
|
||||||
unsafe {
|
unsafe {
|
||||||
&(*self.face)
|
&(*self.face)
|
||||||
|
|
|
@ -92,7 +92,7 @@ impl FontContextHandleMethods for FontContextHandle {
|
||||||
debug!("Creating font handle for {:s}", name);
|
debug!("Creating font handle for {:s}", name);
|
||||||
path_from_identifier(name, &style).and_then(|file_name| {
|
path_from_identifier(name, &style).and_then(|file_name| {
|
||||||
debug!("Opening font face {:s}", 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 file: {}", file);
|
||||||
debug!("variation index: {}", index);
|
debug!("variation index: {}", index);
|
||||||
|
|
||||||
let font_handle = FontHandle::new_from_file_unstyled(&self.fctx,
|
let font_handle = FontHandle::new_from_file(&self.fctx,
|
||||||
file);
|
file.as_slice(), None);
|
||||||
let font_handle = font_handle.unwrap();
|
let font_handle = font_handle.unwrap();
|
||||||
|
|
||||||
debug!("Creating new FontEntry for face: {:s}", font_handle.face_name());
|
debug!("Creating new FontEntry for face: {:s}", font_handle.face_name());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue