Skip to content

Commit

Permalink
Format gfx platform #21373
Browse files Browse the repository at this point in the history
  • Loading branch information
kingdido999 committed Sep 5, 2018
1 parent 5063ac4 commit c57c99d
Show file tree
Hide file tree
Showing 13 changed files with 497 additions and 373 deletions.
175 changes: 99 additions & 76 deletions components/gfx/platform/freetype/android/font_list.rs

Large diffs are not rendered by default.

133 changes: 84 additions & 49 deletions components/gfx/platform/freetype/font.rs
Expand Up @@ -98,14 +98,21 @@ fn create_face(
let face_index = 0 as FT_Long;

let result = if let Some(ref bytes) = template.bytes {
FT_New_Memory_Face(lib, bytes.as_ptr(), bytes.len() as FT_Long, face_index, &mut face)
FT_New_Memory_Face(
lib,
bytes.as_ptr(),
bytes.len() as FT_Long,
face_index,
&mut face,
)
} else {
// This will trigger a synchronous file read in the layout thread, which we may want to
// revisit at some point. See discussion here:
//
// https://github.com/servo/servo/pull/20506#issuecomment-378838800

let filename = CString::new(&*template.identifier).expect("filename contains NUL byte!");
let filename =
CString::new(&*template.identifier).expect("filename contains NUL byte!");
FT_New_Face(lib, filename.as_ptr(), face_index, &mut face)
};

Expand All @@ -122,25 +129,27 @@ fn create_face(
}

impl FontHandleMethods for FontHandle {
fn new_from_template(fctx: &FontContextHandle,
template: Arc<FontTemplateData>,
pt_size: Option<Au>)
-> Result<FontHandle, ()> {
fn new_from_template(
fctx: &FontContextHandle,
template: Arc<FontTemplateData>,
pt_size: Option<Au>,
) -> Result<FontHandle, ()> {
let ft_ctx: FT_Library = fctx.ctx.ctx;
if ft_ctx.is_null() { return Err(()); }
if ft_ctx.is_null() {
return Err(());
}

let face = create_face(ft_ctx, &template, pt_size)?;

let mut handle = FontHandle {
face: face,
font_data: template.clone(),
handle: fctx.clone(),
can_do_fast_shaping: false,
face: face,
font_data: template.clone(),
handle: fctx.clone(),
can_do_fast_shaping: false,
};
// TODO (#11310): Implement basic support for GPOS and GSUB.
handle.can_do_fast_shaping = handle.has_table(KERN) &&
!handle.has_table(GPOS) &&
!handle.has_table(GSUB);
handle.can_do_fast_shaping =
handle.has_table(KERN) && !handle.has_table(GPOS) && !handle.has_table(GSUB);
Ok(handle)
}

Expand Down Expand Up @@ -203,7 +212,7 @@ impl FontHandleMethods for FontHandle {
7 => FontStretchKeyword::Expanded,
8 => FontStretchKeyword::ExtraExpanded,
9 => FontStretchKeyword::UltraExpanded,
_ => FontStretchKeyword::Normal
_ => FontStretchKeyword::Normal,
}
} else {
FontStretchKeyword::Normal
Expand All @@ -218,20 +227,26 @@ impl FontHandleMethods for FontHandle {
if idx != 0 as FT_UInt {
Some(idx as GlyphId)
} else {
debug!("Invalid codepoint: U+{:04X} ('{}')", codepoint as u32, codepoint);
debug!(
"Invalid codepoint: U+{:04X} ('{}')",
codepoint as u32, codepoint
);
None
}
}
}

fn glyph_h_kerning(&self, first_glyph: GlyphId, second_glyph: GlyphId)
-> FractionalPixel {
fn glyph_h_kerning(&self, first_glyph: GlyphId, second_glyph: GlyphId) -> FractionalPixel {
assert!(!self.face.is_null());
let mut delta = FT_Vector { x: 0, y: 0 };
unsafe {
FT_Get_Kerning(self.face, first_glyph, second_glyph,
FT_Kerning_Mode::FT_KERNING_DEFAULT as FT_UInt,
&mut delta);
FT_Get_Kerning(
self.face,
first_glyph,
second_glyph,
FT_Kerning_Mode::FT_KERNING_DEFAULT as FT_UInt,
&mut delta,
);
}
fixed_to_float_ft(delta.x as i32)
}
Expand All @@ -243,9 +258,7 @@ impl FontHandleMethods for FontHandle {
fn glyph_h_advance(&self, glyph: GlyphId) -> Option<FractionalPixel> {
assert!(!self.face.is_null());
unsafe {
let res = FT_Load_Glyph(self.face,
glyph as FT_UInt,
GLYPH_LOAD_FLAGS);
let res = FT_Load_Glyph(self.face, glyph as FT_UInt, GLYPH_LOAD_FLAGS);
if succeeded(res) {
let void_glyph = (*self.face).glyph;
let slot: FT_GlyphSlot = mem::transmute(void_glyph);
Expand Down Expand Up @@ -291,23 +304,24 @@ impl FontHandleMethods for FontHandle {
x_height = self.font_units_to_au(os2.sx_height as f64);
}

let average_advance = self.glyph_index('0')
.and_then(|idx| self.glyph_h_advance(idx))
.map_or(max_advance, |advance| self.font_units_to_au(advance));
let average_advance = self
.glyph_index('0')
.and_then(|idx| self.glyph_h_advance(idx))
.map_or(max_advance, |advance| self.font_units_to_au(advance));

let metrics = FontMetrics {
underline_size: underline_size,
underline_size: underline_size,
underline_offset: underline_offset,
strikeout_size: strikeout_size,
strikeout_size: strikeout_size,
strikeout_offset: strikeout_offset,
leading: leading,
x_height: x_height,
em_size: em_size,
ascent: ascent,
descent: -descent, // linux font's seem to use the opposite sign from mac
max_advance: max_advance,
average_advance: average_advance,
line_gap: height,
leading: leading,
x_height: x_height,
em_size: em_size,
ascent: ascent,
descent: -descent, // linux font's seem to use the opposite sign from mac
max_advance: max_advance,
average_advance: average_advance,
line_gap: height,
};

debug!("Font metrics (@{}px): {:?}", em_size.to_f32_px(), metrics);
Expand All @@ -320,13 +334,25 @@ impl FontHandleMethods for FontHandle {
unsafe {
// Get the length
let mut len = 0;
if !succeeded(FT_Load_Sfnt_Table(self.face, tag, 0, ptr::null_mut(), &mut len)) {
return None
if !succeeded(FT_Load_Sfnt_Table(
self.face,
tag,
0,
ptr::null_mut(),
&mut len,
)) {
return None;
}
// Get the bytes
let mut buf = vec![0u8; len as usize];
if !succeeded(FT_Load_Sfnt_Table(self.face, tag, 0, buf.as_mut_ptr(), &mut len)) {
return None
if !succeeded(FT_Load_Sfnt_Table(
self.face,
tag,
0,
buf.as_mut_ptr(),
&mut len,
)) {
return None;
}
Some(FontTable { buffer: buf })
}
Expand All @@ -338,25 +364,33 @@ impl FontHandleMethods for FontHandle {
}

impl<'a> FontHandle {
fn set_char_size(face: FT_Face, pt_size: Au) -> Result<(), ()>{
fn set_char_size(face: FT_Face, pt_size: Au) -> Result<(), ()> {
let char_size = pt_size.to_f64_px() * 64.0 + 0.5;

unsafe {
let result = FT_Set_Char_Size(face, char_size as FT_F26Dot6, 0, 0, 0);
if succeeded(result) { Ok(()) } else { Err(()) }
if succeeded(result) {
Ok(())
} else {
Err(())
}
}
}

fn has_table(&self, tag: FontTableTag) -> bool {
unsafe {
succeeded(FT_Load_Sfnt_Table(self.face, tag as FT_ULong, 0, ptr::null_mut(), &mut 0))
succeeded(FT_Load_Sfnt_Table(
self.face,
tag as FT_ULong,
0,
ptr::null_mut(),
&mut 0,
))
}
}

fn face_rec_mut(&'a self) -> &'a mut FT_FaceRec {
unsafe {
&mut (*self.face)
}
unsafe { &mut (*self.face) }
}

fn font_units_to_au(&self, value: f64) -> Au {
Expand All @@ -378,11 +412,12 @@ impl<'a> FontHandle {

fn os2_table(&self) -> Option<OS2Table> {
unsafe {
let os2 = FT_Get_Sfnt_Table(self.face_rec_mut(), FT_Sfnt_Tag::FT_SFNT_OS2) as *mut TT_OS2;
let os2 =
FT_Get_Sfnt_Table(self.face_rec_mut(), FT_Sfnt_Tag::FT_SFNT_OS2) as *mut TT_OS2;
let valid = !os2.is_null() && (*os2).version != 0xffff;

if !valid {
return None
return None;
}

Some(OS2Table {
Expand Down
28 changes: 18 additions & 10 deletions components/gfx/platform/freetype/font_context.rs
Expand Up @@ -23,18 +23,18 @@ pub struct User {
size: usize,
}

extern fn ft_alloc(mem: FT_Memory, req_size: c_long) -> *mut c_void {
extern "C" fn ft_alloc(mem: FT_Memory, req_size: c_long) -> *mut c_void {
unsafe {
let ptr = malloc(req_size as usize);
let ptr = ptr as *mut c_void; // libc::c_void vs std::os::raw::c_void
let ptr = ptr as *mut c_void; // libc::c_void vs std::os::raw::c_void
let actual_size = usable_size(ptr);
let user = (*mem).user as *mut User;
(*user).size += actual_size;
ptr
}
}

extern fn ft_free(mem: FT_Memory, ptr: *mut c_void) {
extern "C" fn ft_free(mem: FT_Memory, ptr: *mut c_void) {
unsafe {
let actual_size = usable_size(ptr);
let user = (*mem).user as *mut User;
Expand All @@ -43,8 +43,12 @@ extern fn ft_free(mem: FT_Memory, ptr: *mut c_void) {
}
}

extern fn ft_realloc(mem: FT_Memory, _old_size: c_long, new_req_size: c_long,
old_ptr: *mut c_void) -> *mut c_void {
extern "C" fn ft_realloc(
mem: FT_Memory,
_old_size: c_long,
new_req_size: c_long,
old_ptr: *mut c_void,
) -> *mut c_void {
unsafe {
let old_actual_size = usable_size(old_ptr);
let new_ptr = realloc(old_ptr as *mut _, new_req_size as usize);
Expand Down Expand Up @@ -108,9 +112,7 @@ impl MallocSizeOf for FontContextHandle {

impl FontContextHandle {
pub fn new() -> FontContextHandle {
let user = Box::into_raw(Box::new(User {
size: 0,
}));
let user = Box::into_raw(Box::new(User { size: 0 }));
let mem = Box::into_raw(Box::new(FT_MemoryRec_ {
user: user as *mut c_void,
alloc: Some(ft_alloc),
Expand All @@ -121,12 +123,18 @@ impl FontContextHandle {
let mut ctx: FT_Library = ptr::null_mut();

let result = FT_New_Library(mem, &mut ctx);
if !succeeded(result) { panic!("Unable to initialize FreeType library"); }
if !succeeded(result) {
panic!("Unable to initialize FreeType library");
}

FT_Add_Default_Modules(ctx);

FontContextHandle {
ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx, mem: mem, user: user }),
ctx: Rc::new(FreeTypeLibraryHandle {
ctx: ctx,
mem: mem,
user: user,
}),
}
}
}
Expand Down

0 comments on commit c57c99d

Please sign in to comment.