Store the CTFont inside QuartzNativeFont

This commit is contained in:
Brian Anderson 2012-09-15 14:42:00 -07:00
parent b4934473a0
commit d2da516ec0

View file

@ -16,6 +16,8 @@ use cocoa::cg::cg::{
CGFontRelease CGFontRelease
}; };
use unsafe::transmute; use unsafe::transmute;
use coretext::CTFontRef;
use coretext::coretext::CFRelease;
mod coretext { mod coretext {
@ -52,11 +54,14 @@ mod coretext {
struct QuartzNativeFont { struct QuartzNativeFont {
fontprov: CGDataProviderRef, fontprov: CGDataProviderRef,
cgfont: CGFontRef, cgfont: CGFontRef,
ctfont: CTFontRef,
drop { drop {
assert self.ctfont.is_not_null();
assert self.cgfont.is_not_null(); assert self.cgfont.is_not_null();
assert self.fontprov.is_not_null(); assert self.fontprov.is_not_null();
CFRelease(self.ctfont);
CGFontRelease(self.cgfont); CGFontRelease(self.cgfont);
CGDataProviderRelease(self.fontprov); CGDataProviderRelease(self.fontprov);
} }
@ -66,9 +71,13 @@ fn QuartzNativeFont(fontprov: CGDataProviderRef, cgfont: CGFontRef) -> QuartzNat
assert fontprov.is_not_null(); assert fontprov.is_not_null();
assert cgfont.is_not_null(); assert cgfont.is_not_null();
let ctfont = ctfont_from_cgfont(cgfont);
assert ctfont.is_not_null();
QuartzNativeFont { QuartzNativeFont {
fontprov : fontprov, fontprov : fontprov,
cgfont : cgfont, cgfont : cgfont,
ctfont : ctfont,
} }
} }
@ -76,17 +85,16 @@ impl QuartzNativeFont {
fn glyph_index(codepoint: char) -> Option<GlyphIndex> { fn glyph_index(codepoint: char) -> Option<GlyphIndex> {
use coretext::{UniChar, CGGlyph, CFIndex}; use coretext::{UniChar, CGGlyph, CFIndex};
use coretext::coretext::{CFRelease, CTFontGetGlyphsForCharacters}; use coretext::coretext::{CTFontGetGlyphsForCharacters};
let ctfont = ctfont_from_cgfont(self.cgfont); assert self.ctfont.is_not_null();
assert ctfont.is_not_null();
let characters: ~[UniChar] = ~[codepoint as UniChar]; let characters: ~[UniChar] = ~[codepoint as UniChar];
let glyphs: ~[mut CGGlyph] = ~[mut 0 as CGGlyph]; let glyphs: ~[mut CGGlyph] = ~[mut 0 as CGGlyph];
let count: CFIndex = 1; let count: CFIndex = 1;
let result = do vec::as_imm_buf(characters) |character_buf, _l| { let result = do vec::as_imm_buf(characters) |character_buf, _l| {
do vec::as_imm_buf(glyphs) |glyph_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)
} }
}; };
@ -95,8 +103,6 @@ impl QuartzNativeFont {
return None; return None;
} }
CFRelease(ctfont);
assert glyphs[0] != 0; // FIXME: error handling assert glyphs[0] != 0; // FIXME: error handling
return Some(glyphs[0] as GlyphIndex); return Some(glyphs[0] as GlyphIndex);
} }
@ -104,17 +110,14 @@ impl QuartzNativeFont {
// FIXME: What unit is this returning? Let's have a custom type // FIXME: What unit is this returning? Let's have a custom type
fn glyph_h_advance(glyph: GlyphIndex) -> Option<int> { fn glyph_h_advance(glyph: GlyphIndex) -> Option<int> {
use coretext::{CGGlyph, kCTFontDefaultOrientation}; use coretext::{CGGlyph, kCTFontDefaultOrientation};
use coretext::coretext::{CFRelease, CTFontGetAdvancesForGlyphs}; use coretext::coretext::{CTFontGetAdvancesForGlyphs};
let ctfont = ctfont_from_cgfont(self.cgfont); assert self.ctfont.is_not_null();
assert ctfont.is_not_null();
let glyphs = ~[glyph as CGGlyph]; let glyphs = ~[glyph as CGGlyph];
let advance = do vec::as_imm_buf(glyphs) |glyph_buf, _l| { 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); return Some(advance as int);
} }
} }