Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy won't Helvetica Neue Bold glyphs draw as a normal subpath in NSBezierPath?
    primarykey
    data
    text
    <p>What I want is to take a filled rectangle and punch a hole in it using another shape. This is exactly the kind of thing that <code>NSBezierPath</code> is designed for. You add a rectangular path, then add the subpath that will "punch" through it and finally fill it. In my case the subpath is actually text. No problem, works great:</p> <p><img src="https://i.stack.imgur.com/q44Ls.png" alt="alt text"></p> <p>Except when I use Helvetica Neue Bold for my font. When I use that, I just end up with a solid blue rectangle without any text at all. But the subpath is indeed drawing--in fact, if I shrink the filled rectangle a bit, you can actually see some of the text path:</p> <p><img src="https://i.stack.imgur.com/XNzLK.png" alt="alt text"></p> <p>I get the same behavior with Helvetica Neue Italic. Helvetica Neue Medium works fine, as does Helvetica Bold, Times New Roman Bold and Arial Bold. </p> <p>I've tried using both <code>NSEvenOddWindingRule</code> and <code>NSNonZeroWindingRule</code>. (<strong>EDIT</strong>: Apparently I didn't really try <code>NSEvenOddWinding</code> rule, because that does turn out to work after all)</p> <p>This is the code that I'm using inside the <code>drawRect</code> method of my <code>NSView</code> subclass.</p> <pre><code>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]; </code></pre> <p>So what's going on here? Why do some fonts behave differently than others when it comes to adding glyphs to a path?</p> <p><strong>EDIT</strong>: The solution is to use <code>NSEvenOddWindingRule</code>. After the creation of <code>path</code> add this line:</p> <pre><code>[path setWindingRule:NSEvenOddWindingRule]; </code></pre> <p>Peter Hosey's answer provides the explanation of why this is.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload