Updated Danish translation
[dasher.git] / Src / MacOSX / ZippyStringGlyph.m
blob2930ad8bb1772aa7c5d12ae3f84f2d49fec1ae68
1 //
2 //  ZippyStringGlyph.m
3 //  RectAl
4 //
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.
7 //
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
27     
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]; 
32   }
33   
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];
41     [as release];
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];
53     
55 // TODO, can the NSGlyphs be turned into CGGlyphs, and then the +...packedGlyphs bezierpath method used?
56   }
58   return self;
61 - (void)drawAtPoint:(NSPoint)aPoint {
62   
63 //  NSBezierPath *bp = [NSBezierPath bezierPath];
64 //  [bp moveToPoint:aPoint];
66 //  [bp appendBezierPathWithGlyphs:(NSGlyph *)[[self glyphsData] bytes] count:glyphCount inFont:[[self attributes] objectForKey:NSFontAttributeName]];
67   
68 //  [[[self attributes] objectForKey:NSForegroundColorAttributeName] set];
69 //  [bp fill];
70   
71   NSAffineTransform *t = [NSAffineTransform transform];
72   [t translateXBy:aPoint.x yBy:aPoint.y];
73   //[t scaleXBy:5.0 yBy:5.0];
74   [t concat];
75   [[[self attributes] objectForKey:NSForegroundColorAttributeName] set];
76   [[self bezierPath] fill];
77   [t invert];
78   [t concat];
81 - (NSData *)glyphsData {
82   return [[glyphsData retain] autorelease];
85 - (void)setGlyphsData:(NSData *)value {
86   if (glyphsData != value) {
87     [glyphsData release];
88     glyphsData = [value retain];
89   }
92 - (NSBezierPath *)bezierPath {
93   return [[bezierPath retain] autorelease];
96 - (void)setBezierPath:(NSBezierPath *)value {
97   if (bezierPath != value) {
98     [bezierPath release];
99     bezierPath = [value retain];
100   }
104 - (void)dealloc {
105   [glyphsData release];
106   [bezierPath release];
107   [super dealloc];
110 @end