Lo que quiero es tomar un rectángulo lleno y hacer un agujero con otra forma. Este es exactamente el tipo de cosa para la que está diseñado NSBezierPath
. Agregue una ruta rectangular, luego agregue el subtrayecto que lo "perforará" y finalmente lo llenará. En mi caso, el subpaso es en realidad texto. No hay problema, funciona muy bien:¿Por qué los glifos Helvetica Neue Bold no se dibujarán como subrutas normales en NSBezierPath?
Excepto cuando se utiliza Helvetica Neue Negrita para mi fuente. Cuando uso eso, acabo con un rectángulo azul sólido sin ningún texto. Pero el subtrazado es de hecho el dibujo - de hecho, si reducir el tamaño del rectángulo relleno un poco, en realidad se puede ver algo de la trayectoria de texto:
me sale el mismo comportamiento con Helvetica Neue cursiva. Helvetica Neue Medium funciona bien, al igual que Helvetica Bold, Times New Roman Bold y Arial Bold.
He intentado usar tanto NSEvenOddWindingRule
como NSNonZeroWindingRule
. (EDITAR: Al parecer realmente no trato NSEvenOddWinding
regla, porque eso resultan para trabajar después de todo)
Este es el código que estoy usando el método dentro de mi NSView
subclase drawRect
.
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];
¿Qué está pasando aquí? ¿Por qué algunas fuentes se comportan de manera diferente a otras cuando se trata de agregar glifos a una ruta?
EDITAR: La solución es utilizar NSEvenOddWindingRule
. Después de la creación de path
añadir esta línea: la respuesta de
[path setWindingRule:NSEvenOddWindingRule];
Peter Hosey ofrece la explicación de por qué es.
Me pregunto si Helvetica Neue es un tipo diferente de letra. –