5 // Created by Doug Dickinson on Sat May 24 2003.
6 // Copyright (c) 2003 Doug Dickinson (dasher AT DressTheMonkey DOT plus DOT com). All rights reserved.
9 #import <AppKit/AppKit.h>
10 #import "ZippyStringGlyph.h"
12 static NSTextStorage *textStorage = nil;
13 static NSLayoutManager *layoutManager = nil;
14 static NSTextContainer *textContainer = nil;
16 @implementation ZippyStringGlyph
18 + (id)zippyStringWithString:(NSString *)aString attributes:(NSDictionary *)someAttributes {
19 if (textStorage == nil) {
20 textStorage = [[NSTextStorage alloc] initWithString:@"Here's to the crazy ones, the misfits, the rebels, the troublemakers, the round pegs in the square holes, the ones who see things differently."];
21 layoutManager = [[NSLayoutManager alloc] init];
22 textContainer = [[NSTextContainer alloc] init];
23 [layoutManager addTextContainer:textContainer];
24 [textContainer release]; // The layoutManager will retain the textContainer
25 [textStorage addLayoutManager:layoutManager];
26 [layoutManager release]; // The textStorage will retain the layoutManager
28 // Screen fonts are not suitable for scaled or rotated drawing.
29 // Views that use NSLayoutManager directly for text drawing should
30 // set this parameter appropriately.
31 [layoutManager setUsesScreenFonts:NO];
34 return [[[self alloc] initWithString:aString attributes:someAttributes] autorelease];
37 - (id)initWithString:(NSString *)aString attributes:(NSDictionary *)someAttributes {
38 if (self = [super initWithString:aString attributes:someAttributes]) {
39 NSAttributedString *as = [[NSAttributedString alloc] initWithString:aString attributes:someAttributes];
40 [textStorage replaceCharactersInRange:NSMakeRange(0, [textStorage length]) withAttributedString:as];
42 NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
43 int maxNumGlyphs = glyphRange.length + 1;
44 NSGlyph *glyphs = malloc(maxNumGlyphs);
45 glyphCount = [layoutManager getGlyphs:glyphs range:glyphRange];
46 [self setGlyphsData:[NSData dataWithBytesNoCopy:glyphs length:maxNumGlyphs]];
48 NSBezierPath *bp = [NSBezierPath bezierPath];
49 [bp moveToPoint:NSMakePoint(1, 1)];
50 [bp appendBezierPathWithGlyphs:(NSGlyph *)[[self glyphsData] bytes] count:glyphCount inFont:[[self attributes] objectForKey:NSFontAttributeName]];
52 [self setBezierPath:bp];
55 // TODO, can the NSGlyphs be turned into CGGlyphs, and then the +...packedGlyphs bezierpath method used?
61 - (void)drawAtPoint:(NSPoint)aPoint {
63 // NSBezierPath *bp = [NSBezierPath bezierPath];
64 // [bp moveToPoint:aPoint];
66 // [bp appendBezierPathWithGlyphs:(NSGlyph *)[[self glyphsData] bytes] count:glyphCount inFont:[[self attributes] objectForKey:NSFontAttributeName]];
68 // [[[self attributes] objectForKey:NSForegroundColorAttributeName] set];
71 NSAffineTransform *t = [NSAffineTransform transform];
72 [t translateXBy:aPoint.x yBy:aPoint.y];
73 //[t scaleXBy:5.0 yBy:5.0];
75 [[[self attributes] objectForKey:NSForegroundColorAttributeName] set];
76 [[self bezierPath] fill];
81 - (NSData *)glyphsData {
82 return [[glyphsData retain] autorelease];
85 - (void)setGlyphsData:(NSData *)value {
86 if (glyphsData != value) {
88 glyphsData = [value retain];
92 - (NSBezierPath *)bezierPath {
93 return [[bezierPath retain] autorelease];
96 - (void)setBezierPath:(NSBezierPath *)value {
97 if (bezierPath != value) {
99 bezierPath = [value retain];
105 [glyphsData release];
106 [bezierPath release];