Skip to content

Commit

Permalink
Store the CTFont inside QuartzNativeFont
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Sep 15, 2012
1 parent b493447 commit d2da516
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/servo/text/native_font/quartz_native_font.rs
Expand Up @@ -16,6 +16,8 @@ use cocoa::cg::cg::{
CGFontRelease
};
use unsafe::transmute;
use coretext::CTFontRef;
use coretext::coretext::CFRelease;

mod coretext {

Expand Down Expand Up @@ -52,11 +54,14 @@ mod coretext {
struct QuartzNativeFont {
fontprov: CGDataProviderRef,
cgfont: CGFontRef,
ctfont: CTFontRef,

drop {
assert self.ctfont.is_not_null();
assert self.cgfont.is_not_null();
assert self.fontprov.is_not_null();

CFRelease(self.ctfont);
CGFontRelease(self.cgfont);
CGDataProviderRelease(self.fontprov);
}
Expand All @@ -66,27 +71,30 @@ fn QuartzNativeFont(fontprov: CGDataProviderRef, cgfont: CGFontRef) -> QuartzNat
assert fontprov.is_not_null();
assert cgfont.is_not_null();

let ctfont = ctfont_from_cgfont(cgfont);
assert ctfont.is_not_null();

QuartzNativeFont {
fontprov : fontprov,
cgfont : cgfont,
ctfont : ctfont,
}
}

impl QuartzNativeFont {
fn glyph_index(codepoint: char) -> Option<GlyphIndex> {

use coretext::{UniChar, CGGlyph, CFIndex};
use coretext::coretext::{CFRelease, CTFontGetGlyphsForCharacters};
use coretext::coretext::{CTFontGetGlyphsForCharacters};

let ctfont = ctfont_from_cgfont(self.cgfont);
assert ctfont.is_not_null();
assert self.ctfont.is_not_null();
let characters: ~[UniChar] = ~[codepoint as UniChar];
let glyphs: ~[mut CGGlyph] = ~[mut 0 as CGGlyph];
let count: CFIndex = 1;

let result = do vec::as_imm_buf(characters) |character_buf, _l| {
do vec::as_imm_buf(glyphs) |glyph_buf, _l| {
CTFontGetGlyphsForCharacters(ctfont, character_buf, glyph_buf, count)
CTFontGetGlyphsForCharacters(self.ctfont, character_buf, glyph_buf, count)
}
};

Expand All @@ -95,26 +103,21 @@ impl QuartzNativeFont {
return None;
}

CFRelease(ctfont);

assert glyphs[0] != 0; // FIXME: error handling
return Some(glyphs[0] as GlyphIndex);
}

// FIXME: What unit is this returning? Let's have a custom type
fn glyph_h_advance(glyph: GlyphIndex) -> Option<int> {
use coretext::{CGGlyph, kCTFontDefaultOrientation};
use coretext::coretext::{CFRelease, CTFontGetAdvancesForGlyphs};
use coretext::coretext::{CTFontGetAdvancesForGlyphs};

let ctfont = ctfont_from_cgfont(self.cgfont);
assert ctfont.is_not_null();
assert self.ctfont.is_not_null();
let glyphs = ~[glyph as CGGlyph];
let advance = do vec::as_imm_buf(glyphs) |glyph_buf, _l| {
CTFontGetAdvancesForGlyphs(ctfont, kCTFontDefaultOrientation, glyph_buf, null(), 1)
CTFontGetAdvancesForGlyphs(self.ctfont, kCTFontDefaultOrientation, glyph_buf, null(), 1)
};

CFRelease(ctfont);

return Some(advance as int);
}
}
Expand Down

0 comments on commit d2da516

Please sign in to comment.