|
| 1 | +// |
| 2 | +// Vandercook.m |
| 3 | +// PlotDevice |
| 4 | +// |
| 5 | +// Created by Christian Swinehart on 10/24/14. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +#import "Vandercook.h" |
| 10 | + |
| 11 | +@implementation Vandercook |
| 12 | + |
| 13 | ++ (NSBezierPath *)traceGlyphs:(NSRange)glyph_range atOffset:(NSPoint)offset withLayout:(NSLayoutManager *)layout{ |
| 14 | + NSBezierPath *path = [NSBezierPath bezierPath]; |
| 15 | + NSTextStorage *store = layout.textStorage; |
| 16 | + NSUInteger start = glyph_range.location; |
| 17 | + NSUInteger end = start + glyph_range.length; |
| 18 | + |
| 19 | + for (NSUInteger glyph_idx=start; glyph_idx<end; glyph_idx++){ |
| 20 | + // don't draw tabs, newlines, etc. |
| 21 | + if([layout notShownAttributeForGlyphAtIndex:glyph_idx]) continue; |
| 22 | + |
| 23 | + // shift the glyph's location by the line- and frame-offsets |
| 24 | + NSRect line_rect = [layout lineFragmentRectForGlyphAtIndex:glyph_idx effectiveRange:nil]; |
| 25 | + NSPoint glyph_pt = [layout locationForGlyphAtIndex:glyph_idx]; |
| 26 | + glyph_pt.x += line_rect.origin.x + offset.x; |
| 27 | + glyph_pt.y += line_rect.origin.y + offset.y; |
| 28 | + glyph_pt.y *= -1; |
| 29 | + [path moveToPoint:glyph_pt]; |
| 30 | + |
| 31 | + // add the glyph to the path |
| 32 | + NSUInteger txt_idx = [layout characterIndexForGlyphAtIndex:glyph_idx]; |
| 33 | + NSFont *font = [store attribute:@"NSFont" atIndex:txt_idx effectiveRange:nil]; |
| 34 | + NSGlyph glyph = [layout glyphAtIndex:glyph_idx]; |
| 35 | + [path appendBezierPathWithGlyph:glyph inFont:font]; |
| 36 | + [path closePath]; |
| 37 | + } |
| 38 | + |
| 39 | + return path; |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | ++ (CGPathRef)cgPath:(NSBezierPath *)nsPath{ |
| 44 | + NSInteger i, numElements; |
| 45 | + |
| 46 | + // Need to begin a path here. |
| 47 | + CGPathRef immutablePath = NULL; |
| 48 | + |
| 49 | + // Then draw the path elements. |
| 50 | + numElements = [nsPath elementCount]; |
| 51 | + if (numElements > 0){ |
| 52 | + CGMutablePathRef path = CGPathCreateMutable(); |
| 53 | + NSPoint points[3]; |
| 54 | + BOOL didClosePath = YES; |
| 55 | + |
| 56 | + for (i = 0; i < numElements; i++){ |
| 57 | + switch ([nsPath elementAtIndex:i associatedPoints:points]){ |
| 58 | + case NSMoveToBezierPathElement: |
| 59 | + CGPathMoveToPoint(path, NULL, points[0].x, points[0].y); |
| 60 | + break; |
| 61 | + |
| 62 | + case NSLineToBezierPathElement: |
| 63 | + CGPathAddLineToPoint(path, NULL, points[0].x, points[0].y); |
| 64 | + didClosePath = NO; |
| 65 | + break; |
| 66 | + |
| 67 | + case NSCurveToBezierPathElement: |
| 68 | + CGPathAddCurveToPoint(path, NULL, points[0].x, points[0].y, |
| 69 | + points[1].x, points[1].y, |
| 70 | + points[2].x, points[2].y); |
| 71 | + didClosePath = NO; |
| 72 | + break; |
| 73 | + |
| 74 | + case NSClosePathBezierPathElement: |
| 75 | + CGPathCloseSubpath(path); |
| 76 | + didClosePath = YES; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Be sure the path is closed or Quartz may not do valid hit detection. |
| 82 | + if (!didClosePath) |
| 83 | + CGPathCloseSubpath(path); |
| 84 | + |
| 85 | + immutablePath = CGPathCreateCopy(path); |
| 86 | + CGPathRelease(path); |
| 87 | + } |
| 88 | + |
| 89 | + return immutablePath; |
| 90 | +} |
| 91 | + |
| 92 | +@end |
0 commit comments