Rename NativeFont to FontHandle.

This commit is contained in:
Brian J. Burg 2012-11-06 12:22:29 -08:00
parent 75a88e4f08
commit a3f4b52c90
9 changed files with 68 additions and 62 deletions

View file

@ -14,7 +14,6 @@ pub use font_cache::FontCache;
pub use font_context::FontContext;
pub use font_matcher::FontMatcher;
pub use geometry::Au;
pub use native_font::NativeFont;
pub use shaper::Shaper;
pub use text_run::TextRun;
pub use text_run::SendableTextRun;

View file

@ -10,6 +10,8 @@ use geom::{Point2D, Rect, Size2D};
use glyph::{GlyphStore, GlyphIndex};
use servo_util::range::Range;
use native::FontHandle;
// Used to abstract over the shaper's choice of fixed int representation.
type FractionalPixel = float;
@ -72,7 +74,7 @@ and the renderer can use it to render text.
*/
struct Font {
priv fontbuf: @~[u8],
priv native_font: NativeFont,
priv handle: FontHandle,
priv mut azure_font: Option<AzScaledFontRef>,
priv mut shaper: Option<@Shaper>,
style: FontStyle,
@ -86,12 +88,12 @@ struct Font {
impl Font {
// TODO: who should own fontbuf?
static fn new(fontbuf: @~[u8], native_font: NativeFont, style: FontStyle) -> Font {
let metrics = native_font.get_metrics();
static fn new(fontbuf: @~[u8], handle: FontHandle, style: FontStyle) -> Font {
let metrics = handle.get_metrics();
Font {
fontbuf : fontbuf,
native_font : move native_font,
handle : move handle,
azure_font: None,
shaper: None,
style: move style,
@ -151,7 +153,7 @@ impl Font {
fn get_cairo_face(font: &Font) -> *cairo_font_face_t {
use cairo::cairo_ft::bindgen::{cairo_ft_font_face_create_for_ft_face};
let ftface = font.native_font.face;
let ftface = font.handle.face;
let cface = cairo_ft_font_face_create_for_ft_face(ftface, 0 as c_int);
// FIXME: error handling
return cface;
@ -161,7 +163,7 @@ impl Font {
fn get_cairo_face(font: &Font) -> *cairo_font_face_t {
use cairo::cairo_quartz::bindgen::cairo_quartz_font_face_create_for_cgfont;
let cgfont = font.native_font.cgfont;
let cgfont = font.handle.cgfont;
let face = cairo_quartz_font_face_create_for_cgfont(cgfont);
// FIXME: error handling
return face;
@ -318,11 +320,11 @@ pub impl Font : FontMethods {
}
fn glyph_index(codepoint: char) -> Option<GlyphIndex> {
self.native_font.glyph_index(codepoint)
self.handle.glyph_index(codepoint)
}
fn glyph_h_advance(glyph: GlyphIndex) -> FractionalPixel {
match self.native_font.glyph_h_advance(glyph) {
match self.handle.glyph_h_advance(glyph) {
Some(adv) => adv,
None => /* FIXME: Need fallback strategy */ 10f as FractionalPixel
}

View file

@ -1,8 +1,7 @@
use font::{Font, FontStyle, FontWeight300};
use native_font::NativeFont;
use font_context::FontContext;
use native::{FontHandle, FontContext};
// TODO(Issue #164): delete, and get default font from NativeFontMatcher
// TODO(Issue #164): delete, and get default font from font list
const TEST_FONT: [u8 * 33004] = #include_bin("JosefinSans-SemiBold.ttf");
fn test_font_bin() -> ~[u8] {
@ -44,7 +43,7 @@ impl FontCache {
// TODO: maybe FontStyle should be canonicalized when used in FontCache?
priv fn create_font(style: &FontStyle) -> Result<@Font, ()> {
let font_bin = @test_font_bin();
let native_font = NativeFont::new(self.fctx, font_bin, style.pt_size);
let native_font = FontHandle::new(self.fctx, font_bin, style.pt_size);
let native_font = if native_font.is_ok() {
result::unwrap(move native_font)
} else {

View file

@ -0,0 +1,33 @@
/**
FontHandle encapsulates access to the platform's font API,
e.g. quartz, FreeType. It provides access to metrics and tables
needed by the text shaper as well as access to the underlying
font resources needed by the graphics layer to draw glyphs.
*/
#[cfg(target_os = "macos")]
pub type FontHandle/& = quartz::font_handle::QuartzFontHandle;
#[cfg(target_os = "linux")]
pub type FontHandle/& = freetype::font_handle::FreeTypeFontHandle;
// TODO: `new` should be part of trait FontHandle
// TODO(Issue #163): this is a workaround for static methods and
// typedefs not working well together. It should be removed.
// TODO(Rust #1723): #cfg doesn't work for impl methods, so we have
// to conditionally define the entire impl.
#[cfg(target_os = "macos")]
impl FontHandle {
static pub fn new(fctx: &native::FontContext, buf: @~[u8], pt_size: float) -> Result<FontHandle, ()> {
quartz::font_handle::QuartzFontHandle::new(fctx, buf, pt_size)
}
}
#[cfg(target_os = "linux")]
impl FontHandle {
static pub fn new(fctx: &native::FontContext, buf: @~[u8], pt_size: float) -> Result<FontHandle, ()> {
freetype::font_handle::FreeTypeFontHandle::new(fctx, buf, pt_size)
}
}

View file

@ -31,7 +31,7 @@ fn fixed_to_float_ft(f: i32) -> float {
fixed_to_float(6, f)
}
pub struct FreeTypeNativeFont {
pub struct FreeTypeFontHandle {
/// The font binary. This must stay valid for the lifetime of the font
buf: @~[u8],
face: FT_Face,
@ -44,9 +44,9 @@ pub struct FreeTypeNativeFont {
}
}
pub impl FreeTypeNativeFont {
pub impl FreeTypeFontHandle {
static pub fn new(fctx: &FreeTypeFontContext,
buf: @~[u8], pt_size: float) -> Result<FreeTypeNativeFont, ()> {
buf: @~[u8], pt_size: float) -> Result<FreeTypeFontHandle, ()> {
let ft_ctx = fctx.ctx;
assert ft_ctx.is_not_null();
let face: FT_Face = null();
@ -59,7 +59,7 @@ pub impl FreeTypeNativeFont {
72, // horiz. DPI
72); // vert. DPI
if !res.succeeded() { fail ~"unable to set font char size" }
Ok(FreeTypeNativeFont { face: face, buf: buf })
Ok(FreeTypeFontHandle { face: face, buf: buf })
} else {
Err(())
}

8
src/servo/gfx/native.rs Normal file
View file

@ -0,0 +1,8 @@
/* This file exists just to make it easier to import platform-specific
implementations.
Note that you still must define each of the files as a module in
servo.rc. This is not ideal and may be changed in the future. */
pub use font_handle::FontHandle;
pub use font_context::FontContext;

View file

@ -1,35 +0,0 @@
/**
NativeFont encapsulates access to the platform's font API,
e.g. quartz, FreeType. It provides access to metrics and tables
needed by the text shaper as well as access to the underlying
font resources needed by the graphics layer to draw glyphs.
*/
use font_context::FontContext;
#[cfg(target_os = "macos")]
pub type NativeFont/& = quartz::native_font::QuartzNativeFont;
#[cfg(target_os = "linux")]
pub type NativeFont/& = freetype::native_font::FreeTypeNativeFont;
// TODO: `new` should be part of trait NativeFont
// TODO(Issue #163): this is a workaround for static methods and
// typedefs not working well together. It should be removed.
// TODO(Rust #1723): #cfg doesn't work for impl methods, so we have
// to conditionally define the entire impl.
#[cfg(target_os = "macos")]
impl NativeFont {
static pub fn new(fctx: &FontContext, buf: @~[u8], pt_size: float) -> Result<NativeFont, ()> {
quartz::native_font::QuartzNativeFont::new(fctx, buf, pt_size)
}
}
#[cfg(target_os = "linux")]
impl NativeFont {
static pub fn new(fctx: &FontContext, buf: @~[u8], pt_size: float) -> Result<NativeFont, ()> {
freetype::native_font::FreeTypeNativeFont::new(fctx, buf, pt_size)
}
}

View file

@ -53,7 +53,7 @@ use ct::font_descriptor::{
kCTFontDefaultOrientation,
};
pub struct QuartzNativeFont {
pub struct QuartzFontHandle {
fontprov: CGDataProviderRef,
cgfont: CGFontRef,
ctfont: CTFontRef,
@ -69,8 +69,8 @@ pub struct QuartzNativeFont {
}
}
pub impl QuartzNativeFont {
static pub fn new(_fctx: &QuartzFontContext, buf: @~[u8], pt_size: float) -> Result<QuartzNativeFont, ()> {
pub impl QuartzFontHandle {
static pub fn new(_fctx: &QuartzFontContext, buf: @~[u8], pt_size: float) -> Result<QuartzFontHandle, ()> {
let fontprov = vec::as_imm_buf(*buf, |cbuf, len| {
CGDataProviderCreateWithData(
null(),
@ -86,7 +86,7 @@ pub impl QuartzNativeFont {
let ctfont = ctfont_from_cgfont(cgfont, pt_size);
if ctfont.is_null() { return Err(()); }
Ok(QuartzNativeFont {
Ok(QuartzFontHandle {
fontprov : fontprov,
cgfont : cgfont,
ctfont : ctfont,

View file

@ -88,11 +88,11 @@ pub mod gfx {
pub mod glyph;
pub mod text_run;
// Typedefs and pub-uses for multiple implementations. If prefixed
// with 'native', then the specific implementataion is composed by
// a non-prefixed version, listed above.
// Typedefs and pub-uses for multiple implementations.
// native contains redirects, so one can write native::FontHandle.
pub mod native;
pub mod font_context;
pub mod native_font;
pub mod font_handle;
pub mod shaper;
// Below are the actual platform-specific parts.
@ -103,13 +103,13 @@ pub mod gfx {
#[cfg(target_os = "macos")]
pub mod quartz {
pub mod font_context;
pub mod native_font;
pub mod font_handle;
}
#[cfg(target_os = "linux")]
pub mod freetype {
pub mod font_context;
pub mod native_font;
pub mod font_handle;
}
}