NSGlyph from Unicode
NSGlyph from Unicode
- Subject: NSGlyph from Unicode
- From: Pascal Goguey <email@hidden>
- Date: Wed, 13 Sep 2006 19:51:10 +0900
Hello!
I have been looking around for a while and tried tons of code
snippets, modified them, etc...
No satisfactory behavior so far.
What I would like to do:
Suppose I am in the drawRect method of an NSView, and that I want to
get the NSBezierPath of a
glyph corresponding to a Unicode value.
So basically (see functions hereafrer):
1. Getting the glyph by name works for all alphabetical ASCII character.
2. Getting the glyph by name does not work for any non-ASCII.
3. There is no other method to fetch a glyph ID. I know only the
glyphWithName
message sent to the font.
I also have messed a lot with the NSLayoutManager without success,
My second example shows the use of NSLayoutManager, etc.
It's extremely slow (I don't know if the path calculation or the
drawing is slow...).
Does anybody know how I can get the glyph ID from a given font and
Unicode
value?
Thanks,
Pascal
-(void) drawBezierPathFromString:(NSString*)strForBezier {
NSBezierPath *path = [NSBezierPath bezierPath];
int n, len;
len = [strForBezier length];
NSFont * font = [NSFont fontWithName:@"Helvetica" size:16];
NSColor * color = [NSColor blackColor];
[color set];
[path moveToPoint:startPoint];
NSGlyph glyph;
// I tried a few things with this one... As I couldn't display the
input string,
// I tried to display simple characters. For instance @"a", or
display a unicode
// by setting its value.
// unichar unicodechar[1] = {0x611B};
NSString * glyphname = @"a";
for(n = 0; n < len; n++) {
glyph = [font glyphWithName:glyphname];
[path appendBezierPathWithGlyph: glyph inFont: font];
}
[path fill];
}
- (NSBezierPath *) createBezierPathFromString:(NSString *)string
ofSize:(float)size {
NSBezierPath * bezier = [NSBezierPath bezierPath];
// Make our text storage and layout.
NSTextStorage *storage = [[NSTextStorage alloc] initWithString:string];
[storage setFont:[NSFont boldSystemFontOfSize:size]];
// We don't hold on to the layout and container objects because the
NSTextStorage
// object retains them for us.
NSLayoutManager *layout = [[[NSLayoutManager alloc] init]
autorelease];
NSTextContainer *container = [[[NSTextContainer alloc] init]
autorelease];
[layout addTextContainer:container];
[storage addLayoutManager:layout];
// Turn off padding in the container; otherwise we get extra space
padding out each style run.
[container setLineFragmentPadding:0.0];
// Make a bezier path that will represent our text.
bezier = [[NSBezierPath alloc] init];
[bezier setCachesBezierPath:YES];
[bezier moveToPoint:NSMakePoint(0.0, 0.0)];
// Find each style run in the NSTextStorage.
int position = 0;
int count = [layout numberOfGlyphs];
NSRange r = NSMakeRange(0,count);
NSGlyph g;
while(position < count) {
g = [layout glyphAtIndex:position];
[bezier appendBezierPathWithGlyph:g inFont: [NSFont
boldSystemFontOfSize:size]];
position++;
}
return bezier;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden