mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Fix refactored code on linux.
This commit is contained in:
parent
cf9e02e3a0
commit
c3f3e71c1c
3 changed files with 87 additions and 95 deletions
|
@ -1,12 +1,12 @@
|
|||
//! Linux (FreeType) representation of fonts.
|
||||
|
||||
use font::{CSSFontWeight, FontHandleMethods, FontMetrics, FontTable, FontTableMethods};
|
||||
use font::{CSSFontWeight, FontHandleMethods, FontMetrics, FontTableMethods};
|
||||
use font::{FontTableTag, FractionalPixel, SpecifiedFontStyle, UsedFontStyle, FontWeight100};
|
||||
use font::{FontWeight200, FontWeight300, FontWeight400, FontWeight500, FontWeight600};
|
||||
use font::{FontWeight700, FontWeight800, FontWeight900};
|
||||
use geometry::Au;
|
||||
use geometry;
|
||||
use platform::font_context::{FreeTypeFontContextHandle, FontContextHandle};
|
||||
use platform::font_context::FontContextHandle;
|
||||
use text::glyph::GlyphIndex;
|
||||
use text::util::{float_to_fixed, fixed_to_float};
|
||||
|
||||
|
@ -21,9 +21,6 @@ use freetype::freetype::{FT_SizeRec, FT_UInt, FT_Size_Metrics};
|
|||
use freetype::freetype::{ft_sfnt_os2};
|
||||
use freetype::tt_os2::TT_OS2;
|
||||
|
||||
pub use FontHandle = platform::linux::font::FreeTypeFontHandle;
|
||||
pub use FontTable = platform::linux::font::FreeTypeFontTable;
|
||||
|
||||
fn float_to_fixed_ft(f: float) -> i32 {
|
||||
float_to_fixed(6, f)
|
||||
}
|
||||
|
@ -32,22 +29,22 @@ fn fixed_to_float_ft(f: i32) -> float {
|
|||
fixed_to_float(6, f)
|
||||
}
|
||||
|
||||
pub struct FreeTypeFontTable {
|
||||
pub struct FontTable {
|
||||
bogus: ()
|
||||
}
|
||||
|
||||
impl FontTableMethods for FreeTypeFontTable {
|
||||
impl FontTableMethods for FontTable {
|
||||
fn with_buffer(&self, _blk: &fn(*u8, uint)) {
|
||||
fail!()
|
||||
}
|
||||
}
|
||||
|
||||
enum FontSource {
|
||||
FontSourceMem(@~[u8]),
|
||||
FontSourceMem(~[u8]),
|
||||
FontSourceFile(~str)
|
||||
}
|
||||
|
||||
pub struct FreeTypeFontHandle {
|
||||
pub struct FontHandle {
|
||||
// The font binary. This must stay valid for the lifetime of the font,
|
||||
// if the font is created using FT_Memory_Face.
|
||||
source: FontSource,
|
||||
|
@ -55,7 +52,7 @@ pub struct FreeTypeFontHandle {
|
|||
}
|
||||
|
||||
#[unsafe_destructor]
|
||||
impl Drop for FreeTypeFontHandle {
|
||||
impl Drop for FontHandle {
|
||||
fn finalize(&self) {
|
||||
assert!(self.face.is_not_null());
|
||||
if !FT_Done_Face(self.face).succeeded() {
|
||||
|
@ -64,68 +61,15 @@ impl Drop for FreeTypeFontHandle {
|
|||
}
|
||||
}
|
||||
|
||||
pub impl FreeTypeFontHandle {
|
||||
priv fn set_char_size(face: FT_Face, pt_size: float) -> Result<(), ()>{
|
||||
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 h_dpi = 72;
|
||||
let v_dpi = 72;
|
||||
|
||||
let result = FT_Set_Char_Size(face, char_width, char_height, h_dpi, v_dpi);
|
||||
if result.succeeded() { Ok(()) } else { Err(()) }
|
||||
}
|
||||
|
||||
pub fn new_from_file(fctx: &FreeTypeFontContextHandle, file: ~str,
|
||||
style: &SpecifiedFontStyle) -> Result<FreeTypeFontHandle, ()> {
|
||||
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;
|
||||
do str::as_c_str(file) |file_str| {
|
||||
FT_New_Face(ft_ctx, file_str,
|
||||
face_index, ptr::to_unsafe_ptr(&face));
|
||||
}
|
||||
if face.is_null() {
|
||||
return Err(());
|
||||
}
|
||||
if FreeTypeFontHandle::set_char_size(face, style.pt_size).is_ok() {
|
||||
Ok(FreeTypeFontHandle { source: FontSourceFile(file), face: face })
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_from_file_unstyled(fctx: &FreeTypeFontContextHandle, file: ~str)
|
||||
-> Result<FreeTypeFontHandle, ()> {
|
||||
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;
|
||||
do str::as_c_str(file) |file_str| {
|
||||
FT_New_Face(ft_ctx, file_str,
|
||||
face_index, ptr::to_unsafe_ptr(&face));
|
||||
}
|
||||
if face.is_null() {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
Ok(FreeTypeFontHandle { source: FontSourceFile(file), face: face })
|
||||
}
|
||||
}
|
||||
|
||||
impl FontHandleMethods for FreeTypeFontHandle {
|
||||
pub fn new_from_buffer(fctx: &FreeTypeFontContextHandle,
|
||||
impl FontHandleMethods for FontHandle {
|
||||
pub fn new_from_buffer(fctx: &FontContextHandle,
|
||||
buf: ~[u8],
|
||||
style: &SpecifiedFontStyle)
|
||||
-> Result<FreeTypeFontHandle, ()> {
|
||||
let buf = @buf;
|
||||
|
||||
-> Result<FontHandle, ()> {
|
||||
let ft_ctx: FT_Library = fctx.ctx.ctx;
|
||||
if ft_ctx.is_null() { return Err(()); }
|
||||
|
||||
let face_result = do vec::as_imm_buf(*buf) |bytes: *u8, len: uint| {
|
||||
let face_result = do vec::as_imm_buf(buf) |bytes: *u8, len: uint| {
|
||||
create_face_from_buffer(ft_ctx, bytes, len, style.pt_size)
|
||||
};
|
||||
|
||||
|
@ -133,7 +77,7 @@ impl FontHandleMethods for FreeTypeFontHandle {
|
|||
// and moving buf into the struct ctor, but cant' move out of
|
||||
// captured binding.
|
||||
return match face_result {
|
||||
Ok(face) => Ok(FreeTypeFontHandle { face: face, source: FontSourceMem(buf) }),
|
||||
Ok(face) => Ok(FontHandle { face: face, source: FontSourceMem(buf) }),
|
||||
Err(()) => Err(())
|
||||
};
|
||||
|
||||
|
@ -149,7 +93,7 @@ impl FontHandleMethods for FreeTypeFontHandle {
|
|||
if !result.succeeded() || face.is_null() {
|
||||
return Err(());
|
||||
}
|
||||
if FreeTypeFontHandle::set_char_size(face, pt_size).is_ok() {
|
||||
if FontHandle::set_char_size(face, pt_size).is_ok() {
|
||||
Ok(face)
|
||||
} else {
|
||||
Err(())
|
||||
|
@ -201,13 +145,13 @@ impl FontHandleMethods for FreeTypeFontHandle {
|
|||
|
||||
fn clone_with_style(&self,
|
||||
fctx: &FontContextHandle,
|
||||
style: &UsedFontStyle) -> Result<FreeTypeFontHandle, ()> {
|
||||
style: &UsedFontStyle) -> Result<FontHandle, ()> {
|
||||
match self.source {
|
||||
FontSourceMem(buf) => {
|
||||
FreeTypeFontHandle::new_from_buffer(fctx, buf, style)
|
||||
FontSourceMem(ref buf) => {
|
||||
FontHandleMethods::new_from_buffer(fctx, buf.clone(), style)
|
||||
}
|
||||
FontSourceFile(copy file) => {
|
||||
FreeTypeFontHandle::new_from_file(fctx, file, style)
|
||||
FontHandle::new_from_file(fctx, file, style)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -273,7 +217,56 @@ impl FontHandleMethods for FreeTypeFontHandle {
|
|||
}
|
||||
}
|
||||
|
||||
pub impl FreeTypeFontHandle {
|
||||
pub impl FontHandle {
|
||||
priv fn set_char_size(face: FT_Face, pt_size: float) -> Result<(), ()>{
|
||||
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 h_dpi = 72;
|
||||
let v_dpi = 72;
|
||||
|
||||
let result = FT_Set_Char_Size(face, char_width, char_height, h_dpi, v_dpi);
|
||||
if result.succeeded() { Ok(()) } else { Err(()) }
|
||||
}
|
||||
|
||||
pub fn new_from_file(fctx: &FontContextHandle, file: ~str,
|
||||
style: &SpecifiedFontStyle) -> Result<FontHandle, ()> {
|
||||
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;
|
||||
do str::as_c_str(file) |file_str| {
|
||||
FT_New_Face(ft_ctx, file_str,
|
||||
face_index, ptr::to_unsafe_ptr(&face));
|
||||
}
|
||||
if face.is_null() {
|
||||
return Err(());
|
||||
}
|
||||
if FontHandle::set_char_size(face, style.pt_size).is_ok() {
|
||||
Ok(FontHandle { source: FontSourceFile(file), face: face })
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_from_file_unstyled(fctx: &FontContextHandle, file: ~str)
|
||||
-> Result<FontHandle, ()> {
|
||||
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;
|
||||
do str::as_c_str(file) |file_str| {
|
||||
FT_New_Face(ft_ctx, file_str,
|
||||
face_index, ptr::to_unsafe_ptr(&face));
|
||||
}
|
||||
if face.is_null() {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
Ok(FontHandle { source: FontSourceFile(file), face: face })
|
||||
}
|
||||
|
||||
priv fn get_face_rec(&self) -> &'self FT_FaceRec {
|
||||
unsafe {
|
||||
&(*self.face)
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
use font::{FontHandle, UsedFontStyle};
|
||||
use platform::font::FreeTypeFontHandle;
|
||||
use platform::font_context::FontContextHandleMethods;
|
||||
use font::UsedFontStyle;
|
||||
use platform::font::FontHandle;
|
||||
use font_context::FontContextHandleMethods;
|
||||
use platform::font_list::path_from_identifier;
|
||||
|
||||
use freetype::freetype::{FTErrorMethods, FT_Library};
|
||||
use freetype::freetype::bindgen::{FT_Done_FreeType, FT_Init_FreeType};
|
||||
|
||||
pub use FontContextHandle = platform::linux::FreeTypeFontContextHandle;
|
||||
|
||||
struct FreeTypeLibraryHandle {
|
||||
ctx: FT_Library,
|
||||
|
@ -19,25 +18,25 @@ impl Drop for FreeTypeLibraryHandle {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct FreeTypeFontContextHandle {
|
||||
pub struct FontContextHandle {
|
||||
ctx: @FreeTypeLibraryHandle,
|
||||
}
|
||||
|
||||
pub impl FreeTypeFontContextHandle {
|
||||
pub fn new() -> FreeTypeFontContextHandle {
|
||||
pub impl FontContextHandle {
|
||||
pub fn new() -> FontContextHandle {
|
||||
let ctx: FT_Library = ptr::null();
|
||||
let result = FT_Init_FreeType(ptr::to_unsafe_ptr(&ctx));
|
||||
if !result.succeeded() { fail!(); }
|
||||
|
||||
FreeTypeFontContextHandle {
|
||||
FontContextHandle {
|
||||
ctx: @FreeTypeLibraryHandle { ctx: ctx },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FontContextHandleMethods for FreeTypeFontContextHandle {
|
||||
fn clone(&self) -> FreeTypeFontContextHandle {
|
||||
FreeTypeFontContextHandle { ctx: self.ctx }
|
||||
impl FontContextHandleMethods for FontContextHandle {
|
||||
fn clone(&self) -> FontContextHandle {
|
||||
FontContextHandle { ctx: self.ctx }
|
||||
}
|
||||
|
||||
fn create_font_from_identifier(&self, name: ~str, style: UsedFontStyle)
|
||||
|
@ -45,7 +44,7 @@ impl FontContextHandleMethods for FreeTypeFontContextHandle {
|
|||
debug!("Creating font handle for %s", name);
|
||||
do path_from_identifier(name).chain |file_name| {
|
||||
debug!("Opening font face %s", file_name);
|
||||
FreeTypeFontHandle::new_from_file(self, file_name, &style)
|
||||
FontHandle::new_from_file(self, file_name, &style)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
use font::FontHandleMethods;
|
||||
use font_context::FontContextHandleMethods;
|
||||
use font_list::{FontEntry, FontFamily, FontFamilyMap};
|
||||
use platform::font::FreeTypeFontHandle;
|
||||
use platform::font_context::{FontContextHandle, FreeTypeFontContextHandle};
|
||||
use platform::font::FontHandle;
|
||||
use platform::font_context::FontContextHandle;
|
||||
|
||||
use core::hashmap::HashMap;
|
||||
use core::libc::c_int;
|
||||
|
@ -27,13 +27,13 @@ use fontconfig::fontconfig::bindgen::{FcPatternDestroy};
|
|||
use fontconfig::fontconfig::{FcChar8, FcResultMatch, FcSetSystem};
|
||||
use fontconfig::fontconfig::{FcMatchPattern, FcResultNoMatch};
|
||||
|
||||
pub struct FontconfigFontListHandle {
|
||||
fctx: FreeTypeFontContextHandle,
|
||||
pub struct FontListHandle {
|
||||
fctx: FontContextHandle,
|
||||
}
|
||||
|
||||
pub impl FontconfigFontListHandle {
|
||||
pub fn new(fctx: &FontContextHandle) -> FontconfigFontListHandle {
|
||||
FontconfigFontListHandle { fctx: fctx.clone() }
|
||||
pub impl FontListHandle {
|
||||
pub fn new(fctx: &FontContextHandle) -> FontListHandle {
|
||||
FontListHandle { fctx: fctx.clone() }
|
||||
}
|
||||
|
||||
fn get_available_families(&self) -> FontFamilyMap {
|
||||
|
@ -105,8 +105,8 @@ pub impl FontconfigFontListHandle {
|
|||
debug!("variation file: %?", file);
|
||||
debug!("variation index: %?", index);
|
||||
|
||||
let font_handle = FreeTypeFontHandle::new_from_file_unstyled(&self.fctx,
|
||||
file);
|
||||
let font_handle = FontHandle::new_from_file_unstyled(&self.fctx,
|
||||
file);
|
||||
let font_handle = font_handle.unwrap();
|
||||
|
||||
debug!("Creating new FontEntry for face: %s", font_handle.face_name());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue