objective-c - NSString 到 NSBezierPath 某些标准字体的奇怪字形

标签 objective-c macos fonts bezier nsbezierpath

我从 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];

With Arial With Baskerville 有人知道这种行为的解释吗?是否有解决方案使用这些字体获得正确的路径

最佳答案

问题是您没有为 -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/

相关文章:

css - 为什么我的网站不使用 Tahoma 字体?

ios - UISegmentedControl:如何在使用自定义图像时不突出显示已选择的片段

objective-c - 如何在 Objective-C 中运行代码片段

objective-c - UIBarButtonItem with UIButton as CustomView - 从 UIButton,如何访问它的 UIBarButtonItem?

macos - macOS 10.14 Mojave 上 GTK# 中的字体看起来很粗

Python/Pygame导入字体问题

html - 为什么我的 Open Sans 文本都聚集在这里,我该如何避免这种情况发生?

objective-c - 什么是发件人?

python - 如何在 Python3 和 Mac/Linux 终端中获得相同的哈希值?

eclipse - 从 Eclipse 中删除 "Build Workspace"键盘快捷键