void GetGlyphsForUniChars( const UtilStr& inTypefaceName, const UniChar inChars[], int inNumChars, CGGlyph outGlyphs[] ) {
// A negative length denotes that inChars[] is NUL terminated.
if ( inNumChars < 0 ) {
inNumChars = 0;
while ( inChars && inChars[ inNumChars ] )
inNumChars++;
}
if ( inNumChars <= 0 )
return;
NSString* srcChars = [ NSString stringWithCharacters: inChars length: inNumChars ];
NSString* fontName = [ NSString stringWithUTF8String: inTypefaceName.CStr() ];
NSFont* font = [ NSFont fontWithName:fontName size:0 ];
NSTextStorage* textStorage = [[ NSTextStorage alloc ] initWithString: srcChars ];
if ( font ) {
[ textStorage setFont: font ];
} else {
ss_break();
}
NSLayoutManager* layoutMgr = [[ NSLayoutManager alloc ] init ];
[ textStorage addLayoutManager: layoutMgr ];
// Unicode strings can be arbitraily complex and all kinds of reordering and glyph combination can occur (including consolidation to fewer glyphs).
// We assume below that one unicode char maps to one glyph and that they occur in order. Since this assuption doesn't hold for asian languages,
// we must be careful. Fortunately, for typical western fonts and the text we intend to convert, this is good enough.
SInt32 numGlyphs = [ layoutMgr numberOfGlyphs ];
ss_assert( (int) numGlyphs == inNumChars );
NSGlyph* nsglyphs = (NSGlyph*) ss_malloc( sizeof( NSGlyph ) * numGlyphs );
NSRange range = { 0, numGlyphs };
[ layoutMgr getGlyphsInRange : range glyphs: nsglyphs characterIndexes:NULL glyphInscriptions:NULL elasticBits:NULL ];
{
int i;
for ( i = 0; i < numGlyphs; i++ ) {
if ( nsglyphs[ i ] < 0xFFFF ) {
outGlyphs[ i ] = (CGGlyph) nsglyphs[ i ];
} else {
outGlyphs[ i ] = 0;
}
}
for ( ; i < inNumChars; i++ ) {
outGlyphs[ i ] = 0;
}
}
ss_free( nsglyphs );
nsglyphs = NULL;
[ layoutMgr release ];
[ textStorage release ];
}