为什么Helvetica Neue Bold字形不会作为NSBezierPath中的正常子路径绘制?

Ben*_*man 9 fonts cocoa typography core-graphics objective-c

我想要的是采用填充矩形并使用其他形状在其中打孔.这正是为此NSBezierPath而设计的那种东西.您添加一个矩形路径,然后添加将"穿"它的子路径,最后填充它.在我的例子中,子路径实际上是文本.没问题,效果很好:

替代文字

除非我使用Helvetica Neue Bold作为我的字体.当我使用它时,我最终得到一个没有任何文字的纯蓝色矩形.但是子路径确实在绘制 - 实际上,如果我将填充的矩形缩小一点,您实际上可以看到一些文本路径:

替代文字

我和Helvetica Neue Italic的行为相同.Helvetica Neue Medium的工作正常,Helvetica Bold,Times New Roman Bold和Arial Bold也是如此.

我已经尝试过使用NSEvenOddWindingRuleNSNonZeroWindingRule.(编辑:显然我并没有真正尝试过NSEvenOddWinding规则,因为事实上这确实可以起作用)

这是我在drawRect我的NSView子类的方法中使用的代码.

NSLayoutManager *layoutManger = [[[NSLayoutManager alloc] init] autorelease];
NSTextContainer *textContainer = [[[NSTextContainer alloc] 
      initWithContainerSize:NSMakeSize(300, 100)] autorelease];

NSFont *font = [NSFont fontWithName:@"Helvetica Neue Bold" size:100];

NSDictionary *textAttribs = [NSDictionary dictionaryWithObjectsAndKeys:
                         font, NSFontAttributeName, nil];

NSTextStorage *textStorage = [[[NSTextStorage alloc] initWithString:@"Hello" 
                                        attributes:textAttribs] autorelease];

[layoutManger addTextContainer:textContainer];
[layoutManger setTextStorage:textStorage];

NSRange glyphRange = [layoutManger glyphRangeForTextContainer:textContainer];

NSGlyph glyphArray[glyphRange.length];

NSUInteger glyphCount = [layoutManger getGlyphs:glyphArray range:glyphRange];

NSBezierPath *path = [NSBezierPath bezierPathWithRect:NSMakeRect(0, 0, 200, 100)];
[path appendBezierPathWithGlyphs:glyphArray count:glyphCount inFont:font];

[[NSColor blueColor] setFill];

[path fill];
Run Code Online (Sandbox Code Playgroud)

那么这里发生了什么?在将字形添加到路径时,为什么某些字体的行为与其他字体不同?

编辑:解决方案是使用NSEvenOddWindingRule.创建后path添加此行:

[path setWindingRule:NSEvenOddWindingRule];
Run Code Online (Sandbox Code Playgroud)

Peter Hosey的回答解释了为什么会这样.

Pet*_*sey 4

这可能与字体中路径的方向有关。一种字体可以使用顺时针路径;另一种可能使用逆时针路径。

\n\n

实现您想要的效果的更可靠方法\xc2\xa0 是简单地填充矩形,然后将图形上下文的合成模式设置为 ,然后从中绘制文本NSCompositeSourceOut。如果您只想从填充中剪切文本,则可以使用 Core Graphics 将填充和文本绘制包装在透明层中。

\n