我从 NSAttributedString 中获取单个字符的字形,然后使用 appendBezierPathWithGlyphs:count:inFont:
获取 NSBezierPath ,最后将扁平路径点输出到 NSArray (请参阅下面的代码)在 Quartz Composer 中使用 OpenGL 显示它们。
对于某些 NSFont,包括标准字体(American Typewriter、Baskerville 等),字母完全错误(参见下图)。
重要的是要说我正在使用法语键盘和语言。
NSBezierPath* path = [NSBezierPath bezierPath];
NSTextStorage *storage = [[[NSTextStorage alloc] initWithString:character] autorelease];
NSLayoutManager *manager = [[[NSLayoutManager alloc] init] autorelease];
NSTextContainer *container = [[[NSTextContainer alloc] init] autorelease];
[storage addLayoutManager:manager];
[manager addTextContainer:container];
NSRange glyphRange = [manager glyphRangeForTextContainer:container];
NSGlyph glyphArray[glyphRange.length];
NSUInteger glyphCount = [manager getGlyphs:glyphArray range:glyphRange];
[manager getGlyphs:glyphArray range:glyphRange];
// ---- Necesary
[path moveToPoint: NSMakePoint(0, 0)];
// STRANGE CHARACTERS
[path appendBezierPathWithGlyphs:glyphArray count:glyphCount inFont:selectedFont];
有人知道这种行为的解释吗?是否有解决方案使用这些字体获得正确的路径?
最佳答案
问题是您没有为 -appendBezierPathWithGlyphs:count:inFont:
中使用的字体生成字形。由于您没有将 NSFontAttributeName
属性设置为 NSTextStorage
,因此会使用默认字体生成字形。
使用正确的字体创建您的NSTextStorage
,您的问题将得到解决:
NSTextStorage *storage = [[[NSTextStorage alloc] initWithString:character
attributes:@{NSFontAttributeName : selectedFont}] autorelease];
关于objective-c - NSString 到 NSBezierPath 某些标准字体的奇怪字形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22631197/