update gfx font stuffs for android

This commit is contained in:
aydin.kim 2013-12-30 10:22:43 +09:00
parent b4943c00b5
commit 28757e2257
3 changed files with 50 additions and 57 deletions

View file

@ -29,11 +29,11 @@ use std::cast;
use std::ptr; use std::ptr;
use std::str; use std::str;
fn float_to_fixed_ft(f: float) -> i32 { fn float_to_fixed_ft(f: f64) -> i32 {
float_to_fixed(6, f) float_to_fixed(6, f)
} }
fn fixed_to_float_ft(f: i32) -> float { fn fixed_to_float_ft(f: i32) -> f64 {
fixed_to_float(6, f) fixed_to_float(6, f)
} }
@ -63,7 +63,7 @@ pub struct FontHandle {
#[unsafe_destructor] #[unsafe_destructor]
impl Drop for FontHandle { impl Drop for FontHandle {
#[fixed_stack_segment] #[fixed_stack_segment]
fn drop(&self) { fn drop(&mut self) {
assert!(self.face.is_not_null()); assert!(self.face.is_not_null());
unsafe { unsafe {
if !FT_Done_Face(self.face).succeeded() { if !FT_Done_Face(self.face).succeeded() {
@ -78,7 +78,7 @@ impl FontHandleMethods for FontHandle {
buf: ~[u8], buf: ~[u8],
style: &SpecifiedFontStyle) style: &SpecifiedFontStyle)
-> Result<FontHandle, ()> { -> Result<FontHandle, ()> {
let ft_ctx: FT_Library = fctx.ctx.ctx; let ft_ctx: FT_Library = fctx.ctx.borrow().ctx;
if ft_ctx.is_null() { return Err(()); } if ft_ctx.is_null() { return Err(()); }
let face_result = do buf.as_imm_buf |bytes: *u8, len: uint| { let face_result = do buf.as_imm_buf |bytes: *u8, len: uint| {
@ -93,7 +93,7 @@ impl FontHandleMethods for FontHandle {
let handle = FontHandle { let handle = FontHandle {
face: face, face: face,
source: FontSourceMem(buf), source: FontSourceMem(buf),
handle: *fctx handle: fctx.clone()
}; };
Ok(handle) Ok(handle)
} }
@ -101,10 +101,8 @@ impl FontHandleMethods for FontHandle {
}; };
#[fixed_stack_segment] #[fixed_stack_segment]
fn create_face_from_buffer(lib: FT_Library, fn create_face_from_buffer(lib: FT_Library, cbuf: *u8, cbuflen: uint, pt_size: f64)
cbuf: *u8, cbuflen: uint, pt_size: float) -> Result<FT_Face, ()> {
-> Result<FT_Face, ()> {
unsafe { unsafe {
let mut face: FT_Face = ptr::null(); let mut face: FT_Face = ptr::null();
let face_index = 0 as FT_Long; let face_index = 0 as FT_Long;
@ -207,7 +205,7 @@ impl FontHandleMethods for FontHandle {
let void_glyph = (*self.face).glyph; let void_glyph = (*self.face).glyph;
let slot: FT_GlyphSlot = cast::transmute(void_glyph); let slot: FT_GlyphSlot = cast::transmute(void_glyph);
assert!(slot.is_not_null()); assert!(slot.is_not_null());
debug!("metrics: {}", (*slot).metrics); debug!("metrics: {:?}", (*slot).metrics);
let advance = (*slot).metrics.horiAdvance; let advance = (*slot).metrics.horiAdvance;
debug!("h_advance for {} is {}", glyph, advance); debug!("h_advance for {} is {}", glyph, advance);
let advance = advance as i32; let advance = advance as i32;
@ -224,12 +222,12 @@ impl FontHandleMethods for FontHandle {
/* TODO(Issue #76): complete me */ /* TODO(Issue #76): complete me */
let face = self.get_face_rec(); let face = self.get_face_rec();
let underline_size = self.font_units_to_au(face.underline_thickness as float); let underline_size = self.font_units_to_au(face.underline_thickness as f64);
let underline_offset = self.font_units_to_au(face.underline_position as float); let underline_offset = self.font_units_to_au(face.underline_position as f64);
let em_size = self.font_units_to_au(face.units_per_EM as float); let em_size = self.font_units_to_au(face.units_per_EM as f64);
let ascent = self.font_units_to_au(face.ascender as float); let ascent = self.font_units_to_au(face.ascender as f64);
let descent = self.font_units_to_au(face.descender as float); let descent = self.font_units_to_au(face.descender as f64);
let max_advance = self.font_units_to_au(face.max_advance_width as float); let max_advance = self.font_units_to_au(face.max_advance_width as f64);
// 'leading' is supposed to be the vertical distance between two baselines, // 'leading' is supposed to be the vertical distance between two baselines,
// reflected by the height attibute in freetype. On OS X (w/ CTFont), // reflected by the height attibute in freetype. On OS X (w/ CTFont),
@ -237,7 +235,7 @@ impl FontHandleMethods for FontHandle {
// the top of the next line's ascent or: (line_height - ascent - descent), // the top of the next line's ascent or: (line_height - ascent - descent),
// see http://stackoverflow.com/a/5635981 for CTFont implementation. // see http://stackoverflow.com/a/5635981 for CTFont implementation.
// Convert using a formular similar to what CTFont returns for consistency. // Convert using a formular similar to what CTFont returns for consistency.
let height = self.font_units_to_au(face.height as float); let height = self.font_units_to_au(face.height as f64);
let leading = height - (ascent + descent); let leading = height - (ascent + descent);
let mut strikeout_size = geometry::from_pt(0.0); let mut strikeout_size = geometry::from_pt(0.0);
@ -247,9 +245,9 @@ impl FontHandleMethods for FontHandle {
let os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2) as *TT_OS2; let os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2) as *TT_OS2;
let valid = os2.is_not_null() && (*os2).version != 0xffff; let valid = os2.is_not_null() && (*os2).version != 0xffff;
if valid { if valid {
strikeout_size = self.font_units_to_au((*os2).yStrikeoutSize as float); strikeout_size = self.font_units_to_au((*os2).yStrikeoutSize as f64);
strikeout_offset = self.font_units_to_au((*os2).yStrikeoutPosition as float); strikeout_offset = self.font_units_to_au((*os2).yStrikeoutPosition as f64);
x_height = self.font_units_to_au((*os2).sxHeight as float); x_height = self.font_units_to_au((*os2).sxHeight as f64);
} }
} }
@ -277,7 +275,7 @@ impl FontHandleMethods for FontHandle {
impl<'self> FontHandle { impl<'self> FontHandle {
#[fixed_stack_segment] #[fixed_stack_segment]
fn set_char_size(face: FT_Face, pt_size: float) -> Result<(), ()>{ fn set_char_size(face: FT_Face, pt_size: f64) -> Result<(), ()>{
let char_width = float_to_fixed_ft(pt_size) as FT_F26Dot6; let char_width = float_to_fixed_ft(pt_size) as FT_F26Dot6;
let char_height = float_to_fixed_ft(pt_size) as FT_F26Dot6; let char_height = float_to_fixed_ft(pt_size) as FT_F26Dot6;
let h_dpi = 72; let h_dpi = 72;
@ -290,10 +288,10 @@ impl<'self> FontHandle {
} }
#[fixed_stack_segment] #[fixed_stack_segment]
pub fn new_from_file(fctx: &FontContextHandle, file: ~str, pub fn new_from_file(fctx: &FontContextHandle, file: &str,
style: &SpecifiedFontStyle) -> Result<FontHandle, ()> { style: &SpecifiedFontStyle) -> Result<FontHandle, ()> {
unsafe { unsafe {
let ft_ctx: FT_Library = fctx.ctx.ctx; let ft_ctx: FT_Library = fctx.ctx.borrow().ctx;
if ft_ctx.is_null() { return Err(()); } if ft_ctx.is_null() { return Err(()); }
let mut face: FT_Face = ptr::null(); let mut face: FT_Face = ptr::null();
@ -307,9 +305,9 @@ impl<'self> FontHandle {
} }
if FontHandle::set_char_size(face, style.pt_size).is_ok() { if FontHandle::set_char_size(face, style.pt_size).is_ok() {
Ok(FontHandle { Ok(FontHandle {
source: FontSourceFile(file), source: FontSourceFile(file.to_str()),
face: face, face: face,
handle: *fctx handle: fctx.clone()
}) })
} else { } else {
Err(()) Err(())
@ -321,7 +319,7 @@ impl<'self> FontHandle {
pub fn new_from_file_unstyled(fctx: &FontContextHandle, file: ~str) pub fn new_from_file_unstyled(fctx: &FontContextHandle, file: ~str)
-> Result<FontHandle, ()> { -> Result<FontHandle, ()> {
unsafe { unsafe {
let ft_ctx: FT_Library = fctx.ctx.ctx; let ft_ctx: FT_Library = fctx.ctx.borrow().ctx;
if ft_ctx.is_null() { return Err(()); } if ft_ctx.is_null() { return Err(()); }
let mut face: FT_Face = ptr::null(); let mut face: FT_Face = ptr::null();
@ -337,7 +335,7 @@ impl<'self> FontHandle {
Ok(FontHandle { Ok(FontHandle {
source: FontSourceFile(file), source: FontSourceFile(file),
face: face, face: face,
handle: *fctx handle: fctx.clone()
}) })
} }
} }
@ -348,7 +346,7 @@ impl<'self> FontHandle {
} }
} }
fn font_units_to_au(&self, value: float) -> Au { fn font_units_to_au(&self, value: f64) -> Au {
let face = self.get_face_rec(); let face = self.get_face_rec();
// face.size is a *c_void in the bindings, presumably to avoid // face.size is a *c_void in the bindings, presumably to avoid
@ -356,8 +354,8 @@ impl<'self> FontHandle {
let size: &FT_SizeRec = unsafe { cast::transmute(&(*face.size)) }; let size: &FT_SizeRec = unsafe { cast::transmute(&(*face.size)) };
let metrics: &FT_Size_Metrics = &(*size).metrics; let metrics: &FT_Size_Metrics = &(*size).metrics;
let em_size = face.units_per_EM as float; let em_size = face.units_per_EM as f64;
let x_scale = (metrics.x_ppem as float) / em_size as float; let x_scale = (metrics.x_ppem as f64) / em_size as f64;
// If this isn't true then we're scaling one of the axes wrong // If this isn't true then we're scaling one of the axes wrong
assert!(metrics.x_ppem == metrics.y_ppem); assert!(metrics.x_ppem == metrics.y_ppem);

View file

@ -11,23 +11,24 @@ use freetype::freetype::{FTErrorMethods, FT_Library};
use freetype::freetype::{FT_Done_FreeType, FT_Init_FreeType}; use freetype::freetype::{FT_Done_FreeType, FT_Init_FreeType};
use std::ptr; use std::ptr;
use std::rc::Rc;
#[deriving(Clone)]
struct FreeTypeLibraryHandle { struct FreeTypeLibraryHandle {
ctx: FT_Library, ctx: FT_Library,
} }
impl Drop for FreeTypeLibraryHandle { #[deriving(Clone)]
#[fixed_stack_segment] pub struct FontContextHandle {
fn drop(&self) { ctx: Rc<FreeTypeLibraryHandle>,
assert!(self.ctx.is_not_null());
unsafe {
FT_Done_FreeType(self.ctx);
}
}
} }
pub struct FontContextHandle { impl Drop for FreeTypeLibraryHandle {
ctx: @FreeTypeLibraryHandle, #[fixed_stack_segment]
fn drop(&mut self) {
assert!(self.ctx.is_not_null());
unsafe { FT_Done_FreeType(self.ctx) };
}
} }
impl FontContextHandle { impl FontContextHandle {
@ -35,27 +36,22 @@ impl FontContextHandle {
pub fn new() -> FontContextHandle { pub fn new() -> FontContextHandle {
unsafe { unsafe {
let ctx: FT_Library = ptr::null(); let ctx: FT_Library = ptr::null();
let result = FT_Init_FreeType(ptr::to_unsafe_ptr(&ctx)); let result = FT_Init_FreeType(&ctx);
if !result.succeeded() { fail!(); } if !result.succeeded() { fail!("Unable to initialize FreeType library"); }
FontContextHandle {
FontContextHandle { ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx }),
ctx: @FreeTypeLibraryHandle { ctx: ctx },
} }
} }
} }
} }
impl FontContextHandleMethods for FontContextHandle { impl FontContextHandleMethods for FontContextHandle {
fn clone(&self) -> FontContextHandle {
FontContextHandle { ctx: self.ctx }
}
fn create_font_from_identifier(&self, name: ~str, style: UsedFontStyle) fn create_font_from_identifier(&self, name: ~str, style: UsedFontStyle)
-> Result<FontHandle, ()> { -> Result<FontHandle, ()> {
debug!("Creating font handle for {:s}", name); debug!("Creating font handle for {:s}", name);
do path_from_identifier(name, &style).chain |file_name| { do 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, &style) FontHandle::new_from_file(self, file_name.to_owned(), &style)
} }
} }
} }

View file

@ -19,7 +19,6 @@ use fontconfig::fontconfig::{
use font::{FontHandleMethods, UsedFontStyle}; use font::{FontHandleMethods, UsedFontStyle};
use font_context::FontContextHandleMethods;
use font_list::{FontEntry, FontFamily, FontFamilyMap}; use font_list::{FontEntry, FontFamily, FontFamilyMap};
use platform::font::FontHandle; use platform::font::FontHandle;
use platform::font_context::FontContextHandle; use platform::font_context::FontContextHandle;
@ -53,7 +52,7 @@ impl FontListHandle {
while FcPatternGetString(*font, FC_FAMILY, v, &family) == FcResultMatch { while FcPatternGetString(*font, FC_FAMILY, v, &family) == FcResultMatch {
let family_name = str::raw::from_c_str(family as *c_char); let family_name = str::raw::from_c_str(family as *c_char);
debug!("Creating new FontFamily for family: {:s}", family_name); debug!("Creating new FontFamily for family: {:s}", family_name);
let new_family = @mut FontFamily::new(family_name); let new_family = FontFamily::new(family_name);
family_map.insert(family_name, new_family); family_map.insert(family_name, new_family);
v += 1; v += 1;
} }
@ -64,8 +63,8 @@ impl FontListHandle {
} }
#[fixed_stack_segment] #[fixed_stack_segment]
pub fn load_variations_for_family(&self, family: @mut FontFamily) { pub fn load_variations_for_family(&self, family: &mut FontFamily) {
debug!("getting variations for {}", family); debug!("getting variations for {:?}", family);
unsafe { unsafe {
let config = FcConfigGetCurrent(); let config = FcConfigGetCurrent();
let font_set = FcConfigGetFonts(config, FcSetSystem); let font_set = FcConfigGetFonts(config, FcSetSystem);
@ -120,7 +119,7 @@ impl FontListHandle {
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());
let entry = @FontEntry::new(font_handle); let entry = FontEntry::new(font_handle);
family.entries.push(entry); family.entries.push(entry);
} }
@ -141,7 +140,7 @@ struct AutoPattern {
impl Drop for AutoPattern { impl Drop for AutoPattern {
#[fixed_stack_segment] #[fixed_stack_segment]
fn drop(&self) { fn drop(&mut self) {
unsafe { unsafe {
FcPatternDestroy(self.pattern); FcPatternDestroy(self.pattern);
} }