mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +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.
|
//! 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::{FontTableTag, FractionalPixel, SpecifiedFontStyle, UsedFontStyle, FontWeight100};
|
||||||
use font::{FontWeight200, FontWeight300, FontWeight400, FontWeight500, FontWeight600};
|
use font::{FontWeight200, FontWeight300, FontWeight400, FontWeight500, FontWeight600};
|
||||||
use font::{FontWeight700, FontWeight800, FontWeight900};
|
use font::{FontWeight700, FontWeight800, FontWeight900};
|
||||||
use geometry::Au;
|
use geometry::Au;
|
||||||
use geometry;
|
use geometry;
|
||||||
use platform::font_context::{FreeTypeFontContextHandle, FontContextHandle};
|
use platform::font_context::FontContextHandle;
|
||||||
use text::glyph::GlyphIndex;
|
use text::glyph::GlyphIndex;
|
||||||
use text::util::{float_to_fixed, fixed_to_float};
|
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::freetype::{ft_sfnt_os2};
|
||||||
use freetype::tt_os2::TT_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 {
|
fn float_to_fixed_ft(f: float) -> i32 {
|
||||||
float_to_fixed(6, f)
|
float_to_fixed(6, f)
|
||||||
}
|
}
|
||||||
|
@ -32,22 +29,22 @@ fn fixed_to_float_ft(f: i32) -> float {
|
||||||
fixed_to_float(6, f)
|
fixed_to_float(6, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FreeTypeFontTable {
|
pub struct FontTable {
|
||||||
bogus: ()
|
bogus: ()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FontTableMethods for FreeTypeFontTable {
|
impl FontTableMethods for FontTable {
|
||||||
fn with_buffer(&self, _blk: &fn(*u8, uint)) {
|
fn with_buffer(&self, _blk: &fn(*u8, uint)) {
|
||||||
fail!()
|
fail!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum FontSource {
|
enum FontSource {
|
||||||
FontSourceMem(@~[u8]),
|
FontSourceMem(~[u8]),
|
||||||
FontSourceFile(~str)
|
FontSourceFile(~str)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FreeTypeFontHandle {
|
pub struct FontHandle {
|
||||||
// The font binary. This must stay valid for the lifetime of the font,
|
// The font binary. This must stay valid for the lifetime of the font,
|
||||||
// if the font is created using FT_Memory_Face.
|
// if the font is created using FT_Memory_Face.
|
||||||
source: FontSource,
|
source: FontSource,
|
||||||
|
@ -55,7 +52,7 @@ pub struct FreeTypeFontHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe_destructor]
|
#[unsafe_destructor]
|
||||||
impl Drop for FreeTypeFontHandle {
|
impl Drop for FontHandle {
|
||||||
fn finalize(&self) {
|
fn finalize(&self) {
|
||||||
assert!(self.face.is_not_null());
|
assert!(self.face.is_not_null());
|
||||||
if !FT_Done_Face(self.face).succeeded() {
|
if !FT_Done_Face(self.face).succeeded() {
|
||||||
|
@ -64,68 +61,15 @@ impl Drop for FreeTypeFontHandle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl FreeTypeFontHandle {
|
impl FontHandleMethods for FontHandle {
|
||||||
priv fn set_char_size(face: FT_Face, pt_size: float) -> Result<(), ()>{
|
pub fn new_from_buffer(fctx: &FontContextHandle,
|
||||||
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,
|
|
||||||
buf: ~[u8],
|
buf: ~[u8],
|
||||||
style: &SpecifiedFontStyle)
|
style: &SpecifiedFontStyle)
|
||||||
-> Result<FreeTypeFontHandle, ()> {
|
-> Result<FontHandle, ()> {
|
||||||
let buf = @buf;
|
|
||||||
|
|
||||||
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(()); }
|
||||||
|
|
||||||
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)
|
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
|
// and moving buf into the struct ctor, but cant' move out of
|
||||||
// captured binding.
|
// captured binding.
|
||||||
return match face_result {
|
return match face_result {
|
||||||
Ok(face) => Ok(FreeTypeFontHandle { face: face, source: FontSourceMem(buf) }),
|
Ok(face) => Ok(FontHandle { face: face, source: FontSourceMem(buf) }),
|
||||||
Err(()) => Err(())
|
Err(()) => Err(())
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -149,7 +93,7 @@ impl FontHandleMethods for FreeTypeFontHandle {
|
||||||
if !result.succeeded() || face.is_null() {
|
if !result.succeeded() || face.is_null() {
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
if FreeTypeFontHandle::set_char_size(face, pt_size).is_ok() {
|
if FontHandle::set_char_size(face, pt_size).is_ok() {
|
||||||
Ok(face)
|
Ok(face)
|
||||||
} else {
|
} else {
|
||||||
Err(())
|
Err(())
|
||||||
|
@ -201,13 +145,13 @@ impl FontHandleMethods for FreeTypeFontHandle {
|
||||||
|
|
||||||
fn clone_with_style(&self,
|
fn clone_with_style(&self,
|
||||||
fctx: &FontContextHandle,
|
fctx: &FontContextHandle,
|
||||||
style: &UsedFontStyle) -> Result<FreeTypeFontHandle, ()> {
|
style: &UsedFontStyle) -> Result<FontHandle, ()> {
|
||||||
match self.source {
|
match self.source {
|
||||||
FontSourceMem(buf) => {
|
FontSourceMem(ref buf) => {
|
||||||
FreeTypeFontHandle::new_from_buffer(fctx, buf, style)
|
FontHandleMethods::new_from_buffer(fctx, buf.clone(), style)
|
||||||
}
|
}
|
||||||
FontSourceFile(copy file) => {
|
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 {
|
priv fn get_face_rec(&self) -> &'self FT_FaceRec {
|
||||||
unsafe {
|
unsafe {
|
||||||
&(*self.face)
|
&(*self.face)
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
use font::{FontHandle, UsedFontStyle};
|
use font::UsedFontStyle;
|
||||||
use platform::font::FreeTypeFontHandle;
|
use platform::font::FontHandle;
|
||||||
use platform::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, FT_Library};
|
||||||
use freetype::freetype::bindgen::{FT_Done_FreeType, FT_Init_FreeType};
|
use freetype::freetype::bindgen::{FT_Done_FreeType, FT_Init_FreeType};
|
||||||
|
|
||||||
pub use FontContextHandle = platform::linux::FreeTypeFontContextHandle;
|
|
||||||
|
|
||||||
struct FreeTypeLibraryHandle {
|
struct FreeTypeLibraryHandle {
|
||||||
ctx: FT_Library,
|
ctx: FT_Library,
|
||||||
|
@ -19,25 +18,25 @@ impl Drop for FreeTypeLibraryHandle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FreeTypeFontContextHandle {
|
pub struct FontContextHandle {
|
||||||
ctx: @FreeTypeLibraryHandle,
|
ctx: @FreeTypeLibraryHandle,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl FreeTypeFontContextHandle {
|
pub impl FontContextHandle {
|
||||||
pub fn new() -> FreeTypeFontContextHandle {
|
pub fn new() -> FontContextHandle {
|
||||||
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(ptr::to_unsafe_ptr(&ctx));
|
||||||
if !result.succeeded() { fail!(); }
|
if !result.succeeded() { fail!(); }
|
||||||
|
|
||||||
FreeTypeFontContextHandle {
|
FontContextHandle {
|
||||||
ctx: @FreeTypeLibraryHandle { ctx: ctx },
|
ctx: @FreeTypeLibraryHandle { ctx: ctx },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FontContextHandleMethods for FreeTypeFontContextHandle {
|
impl FontContextHandleMethods for FontContextHandle {
|
||||||
fn clone(&self) -> FreeTypeFontContextHandle {
|
fn clone(&self) -> FontContextHandle {
|
||||||
FreeTypeFontContextHandle { ctx: self.ctx }
|
FontContextHandle { ctx: self.ctx }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_font_from_identifier(&self, name: ~str, style: UsedFontStyle)
|
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);
|
debug!("Creating font handle for %s", name);
|
||||||
do path_from_identifier(name).chain |file_name| {
|
do path_from_identifier(name).chain |file_name| {
|
||||||
debug!("Opening font face %s", 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::FontHandleMethods;
|
||||||
use font_context::FontContextHandleMethods;
|
use font_context::FontContextHandleMethods;
|
||||||
use font_list::{FontEntry, FontFamily, FontFamilyMap};
|
use font_list::{FontEntry, FontFamily, FontFamilyMap};
|
||||||
use platform::font::FreeTypeFontHandle;
|
use platform::font::FontHandle;
|
||||||
use platform::font_context::{FontContextHandle, FreeTypeFontContextHandle};
|
use platform::font_context::FontContextHandle;
|
||||||
|
|
||||||
use core::hashmap::HashMap;
|
use core::hashmap::HashMap;
|
||||||
use core::libc::c_int;
|
use core::libc::c_int;
|
||||||
|
@ -27,13 +27,13 @@ use fontconfig::fontconfig::bindgen::{FcPatternDestroy};
|
||||||
use fontconfig::fontconfig::{FcChar8, FcResultMatch, FcSetSystem};
|
use fontconfig::fontconfig::{FcChar8, FcResultMatch, FcSetSystem};
|
||||||
use fontconfig::fontconfig::{FcMatchPattern, FcResultNoMatch};
|
use fontconfig::fontconfig::{FcMatchPattern, FcResultNoMatch};
|
||||||
|
|
||||||
pub struct FontconfigFontListHandle {
|
pub struct FontListHandle {
|
||||||
fctx: FreeTypeFontContextHandle,
|
fctx: FontContextHandle,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl FontconfigFontListHandle {
|
pub impl FontListHandle {
|
||||||
pub fn new(fctx: &FontContextHandle) -> FontconfigFontListHandle {
|
pub fn new(fctx: &FontContextHandle) -> FontListHandle {
|
||||||
FontconfigFontListHandle { fctx: fctx.clone() }
|
FontListHandle { fctx: fctx.clone() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_available_families(&self) -> FontFamilyMap {
|
fn get_available_families(&self) -> FontFamilyMap {
|
||||||
|
@ -105,7 +105,7 @@ pub impl FontconfigFontListHandle {
|
||||||
debug!("variation file: %?", file);
|
debug!("variation file: %?", file);
|
||||||
debug!("variation index: %?", index);
|
debug!("variation index: %?", index);
|
||||||
|
|
||||||
let font_handle = FreeTypeFontHandle::new_from_file_unstyled(&self.fctx,
|
let font_handle = FontHandle::new_from_file_unstyled(&self.fctx,
|
||||||
file);
|
file);
|
||||||
let font_handle = font_handle.unwrap();
|
let font_handle = font_handle.unwrap();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue