Re: Newbie: NSGlyph <-> unicode ?
Re: Newbie: NSGlyph <-> unicode ?
- Subject: Re: Newbie: NSGlyph <-> unicode ?
- From: Andy <email@hidden>
- Date: Mon, 07 Jan 2002 23:11:14 -0500
Anabelle Masour wrote:
>
>
I don't know if this is the right list to ask this but i have this problem.
>
I want to convert a number of unicode chars to NSGlyph for use with
>
NSBezierPath. It would be good if I could convert a NSGlyph back too. The
>
font to use is variable.
>
I do not find a method to convert between the two.
>
Can anybody help me please ?
I had the same problem, the key is NSLayoutManager... though you should
read the Text topic too because it explains how to assemble stuff... eg,
probably not ideal, but you can get the idea:
- (void) refreshCharacters {
UniChar pageChars[256];
unsigned int i=0;
UniChar c=0;
int firstChar = m_currentPage * 256;
NSButtonCell *current=nil;
NSString *title=nil;
NSString *strCharsOnPage=nil;
NSAttributedString *atrCharsOnPage=nil;
NSTextStorage *tsCharsOnPage=nil;
NSGlyph glpCurrent=nil;
NSLayoutManager *layoutManager=nil;
BOOL validIndex = NO;
//populate an array with all of the chars displayed on the current page
for(i=0, c=firstChar; i< 256; i++, c++) {
//set illegal unicode chars to null
pageChars[i] = ([m_illegal characterIsMember:c]) ? 0 : c;
}
//make an NSTextStorage out of the pageChars,
strCharsOnPage = [NSString stringWithCharacters: pageChars length: 256];
atrCharsOnPage = [[[NSAttributedString alloc] initWithString:
strCharsOnPage] autorelease];
tsCharsOnPage = [[[NSTextStorage alloc]
initWithAttributedString:atrCharsOnPage] autorelease];
layoutManager = [[[NSLayoutManager alloc] init] autorelease];
[tsCharsOnPage addLayoutManager:layoutManager];
//[layoutManager setUsesScreenFonts: NO];
//[layoutManager setShowsInvisibleCharacters: YES];
//for each char in the text storage, see if its invalid, not
supported by the font or
//valid and displatable
for (i=0; i < 256; i++) {
current = [self cellWithTag: i];
[current setFont: m_currentFont ];
//the only place we should see '0' is in the first slot on the
first page
if (pageChars[i] == 0 && (i != 0 || (i==0 && m_currentPage !=
0))) {
//illegal
[current setBordered:NO];
[current setEnabled:NO];
[current setTitle: @""];
} else {
glpCurrent = [layoutManager glyphAtIndex: i isValidIndex: &validIndex];
if (validIndex && [m_currentFont glyphIsEncoded:
glpCurrent]) {
//valid
title = [strCharsOnPage substringWithRange: NSMakeRange(i,1)];
if ([title length] > 0) {
[current setEnabled:YES];
} else {
title = @"???";
}
} else {
//not supported
title = @"??";
[current setEnabled:NO];
}
[current setBordered:YES];
[current setTitle: title];
}
}
//setup the numbers which define which chars are visible
[startIndex setTitleWithMnemonic: [[NSNumber numberWithInt:
firstChar] stringValue]];
[endIndex setTitleWithMnemonic: [[NSNumber numberWithInt:
firstChar+255] stringValue]];
}
--
AndyT (lordpixel - the cat who walks through walls)
A little bigger on the inside
I think we finally found the killer app for Flash: animated stick men