forked from atg/Ingredients
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHSymbolButtonImage.m
311 lines (248 loc) · 8.77 KB
/
CHSymbolButtonImage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// CHSymbolButtonImage.m
// Chocolat
//
// Created by Alex Gordon on 11/09/2009.
// Copyright 2009 Fileability. Written in 2010 by Fileability..
//
#import "CHSymbolButtonImage.h"
@implementation CHSymbolButtonImage
static NSMutableDictionary *sharedSymbolButtonImageCache = nil;
+ (NSArray *)symbolImageWithTypeString:(NSString *)typeString drawBorder:(BOOL)drawBorder
{
CHSymbolButtonImageMask m = 0;
if (!drawBorder)
m |= CHSymbolButtonDontDrawBorder;
if ([typeString isEqual:@"f"])
m |= CHSymbolButtonFunction;
else if ([typeString isEqual:@"@"])
m |= CHSymbolButtonObjcClass | CHSymbolButtonObjcInterface;
else if ([typeString isEqual:@"FF"])
m |= CHSymbolButtonFunctionContainer;
else if ([typeString isEqual:@"@p"])
m |= CHSymbolButtonObjcProtocol | CHSymbolButtonObjcInterface;
else if ([typeString isEqual:@"@+"])
m |= CHSymbolButtonObjcCategory | CHSymbolButtonObjcInterface;
else if ([typeString isEqual:@"@ip"])
m |= CHSymbolButtonObjcClass | CHSymbolButtonObjcImplementation;
else if ([typeString isEqual:@"@pr"])
m |= CHSymbolButtonObjcProperty;
else if ([typeString isEqual:@"@m"])
m |= CHSymbolButtonObjcMethod;
else if ([typeString isEqual:@"@@m"])
m |= CHSymbolButtonObjcMethod | CHSymbolButtonStaticScope;
else if ([typeString isEqual:@"iv"])
m |= CHSymbolButtonVariable | CHSymbolButtonInstanceScope;
else if ([typeString isEqual:@"v"])
m |= CHSymbolButtonVariable;
else if ([typeString isEqual:@"ev"])
m |= CHSymbolButtonVariable;
if (m)
return [self symbolImageWithMask:m];
return nil;
}
+ (NSArray *)symbolImageWithMask:(CHSymbolButtonImageMask)mask
{
@synchronized(sharedSymbolButtonImageCache)
{
if (sharedSymbolButtonImageCache == nil)
sharedSymbolButtonImageCache = [[NSMutableDictionary alloc] initWithCapacity:20];
NSArray *cachedImage = [sharedSymbolButtonImageCache objectForKey:[NSNumber numberWithUnsignedLongLong:mask]];
if (cachedImage)
return cachedImage;
NSString *str = @"";
CGFloat offset = 0.0;
BOOL bold = NO;
if (mask & CHSymbolButtonMacro)
{
str = @"#";
offset = 0.0;
}
if (mask & CHSymbolButtonTypedef)
{
offset = 30;
if (mask & CHSymbolButtonStruct)
str = @"S";
else if (mask & CHSymbolButtonUnion)
str = @"U";
else if (mask & CHSymbolButtonEnum)
str = @"E";
else if (mask & CHSymbolButtonCppClass)
str = @"C";
else
str = @"T";
}
else if (mask & CHSymbolButtonFunction)
{
offset = 209.0;
str = [NSString stringWithFormat:@"%C", 0x0192];
}
else if (mask & CHSymbolButtonFunctionContainer)
{
offset = 209.0;
str = @"F";
}
else if (mask & CHSymbolButtonObjcMethod)
{
if (mask & CHSymbolButtonStaticScope)
offset = 231.0;
else
offset = 209.0;
str = @"M";
}
else if (mask & CHSymbolButtonObjcProperty)
{
offset = 170.0 + (209.0 - 170.0) * 2.0 / 4.0;
str = @"P";
}
else if (mask & CHSymbolButtonStruct)
{
offset = 330;
str = @"S";
}
else if ((mask & CHSymbolButtonUnion) || (mask & CHSymbolButtonEnum))
{
offset = 60;
if (mask & CHSymbolButtonUnion)
str = @"U";
else //if (mask & CHSymbolButtonEnum)
str = @"E";
}
else if (mask & CHSymbolButtonUnion)
{
offset = 330;
str = @"C";
}
else if ((mask & CHSymbolButtonObjcClass) || (mask & CHSymbolButtonCppClass))
{
offset = 252.0;
str = @"C";
}
else if (mask & CHSymbolButtonObjcBindingsListing)
{
offset = 252.0;
str = @"B";
}
else if (mask & CHSymbolButtonObjcProtocol)
{
offset = 252.0;
str = @"P";
}
else if (mask & CHSymbolButtonObjcCategory)
{
offset = 252.0;
str = @"+";
bold = YES;
}
else if (mask & CHSymbolButtonVariable)
{
str = @"v";
if (mask & CHSymbolButtonInstanceScope)
offset = 170.0;//180.0;
else
offset = 120.0;
}
else if (mask & CHSymbolButtonNotification)
{
str = @"N";
offset = 120.0;
}
if (![str length])
return nil;
NSArray *imageTuple = [NSArray arrayWithObjects:
[self symbolImageWithString:str offset:offset bold:bold isWhite:NO drawBorder:(mask & CHSymbolButtonDontDrawBorder) == 0],
[self symbolImageWithString:str offset:offset bold:bold isWhite:YES drawBorder:(mask & CHSymbolButtonDontDrawBorder) == 0],
nil];
[sharedSymbolButtonImageCache setObject:imageTuple forKey:[NSNumber numberWithUnsignedLongLong:mask]];
return imageTuple;
}
}
+ (NSImage *)symbolImageWithString:(NSString *)str offset:(CGFloat)offset bold:(BOOL)isBold isWhite:(BOOL)isWhite drawBorder:(BOOL)drawBorder
{
if (isWhite)
isBold = YES;
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(16, 17)];
[image lockFocus];
float radius = 3;
NSRect rect = NSMakeRect(0, 1, 16, 16);
offset -= 209.0;
offset /= 360.0;
NSBezierPath *bp = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
NSGradient *g;
float backgroundFactor = 0.9;
if (isWhite == NO)
{
g = [[NSGradient alloc] initWithStartingColor:[self rotateColor:[NSColor colorWithCalibratedRed:0.644 green:0.716 blue:0.834 alpha:1.000] byOffset:offset]
endingColor:[self rotateColor:[NSColor colorWithCalibratedRed:0.779 green:0.846 blue:0.909 alpha:1.000] byOffset:offset]];
[g drawInBezierPath:bp angle:90];
}
else
{
[[NSColor colorWithCalibratedWhite:0.0 alpha:0.23] set];
[[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:0.6 * backgroundFactor] set];
[[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(rect, 0.5, 0.5) xRadius:radius - 1 yRadius:radius - 1] stroke];
}
//Inner
bp = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(rect, 1, 1) xRadius:radius - 1 yRadius:radius - 1];
if (isWhite == NO)
{
g = [[NSGradient alloc] initWithStartingColor:[self rotateColor:[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:0.4] byOffset:offset]
endingColor:[self rotateColor:[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:0.4] byOffset:offset]];
if (drawBorder)
[g drawInBezierPath:bp angle:90];
}
else
{
[[NSColor colorWithCalibratedWhite:0.75 alpha:0.625 * backgroundFactor] set];
[[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(NSInsetRect(rect, 1.0, 1.0), 0.5, 0.5) xRadius:radius - 1 yRadius:radius - 1] stroke];
}
//Fill
bp = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(rect, 2, 2) xRadius:radius - 2 yRadius:radius - 2];
if (isWhite == NO)
{
g = [[NSGradient alloc] initWithStartingColor:[self rotateColor:[NSColor colorWithCalibratedRed:0.712 green:0.799 blue:0.905 alpha:1.000] byOffset:offset]
endingColor:[self rotateColor:[NSColor colorWithCalibratedRed:0.817 green:0.889 blue:0.953 alpha:1.000] byOffset:offset]];
if (drawBorder)
[g drawInBezierPath:bp angle:90];
}
else
{
g = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:0.25 * backgroundFactor]
endingColor:[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:0.5 * backgroundFactor]];
[g drawInBezierPath:bp angle:90];
}
//Text
NSMutableDictionary *attrs = [[NSMutableDictionary alloc] initWithCapacity:2];
[attrs setValue:(isBold ? [NSFont boldSystemFontOfSize:11] : [NSFont systemFontOfSize:11]) forKey:NSFontAttributeName];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0, -1)];
[shadow setShadowBlurRadius:0.0];
if (isWhite)
{
[attrs setValue:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0] forKey:NSForegroundColorAttributeName];
[shadow setShadowOffset:NSMakeSize(0, -1)];
[shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.5]];
[shadow setShadowBlurRadius:1.0];
}
else
{
[attrs setValue:[self rotateColor:[NSColor colorWithCalibratedRed:0.045 green:0.209 blue:0.384 alpha:1.000] byOffset:offset] forKey:NSForegroundColorAttributeName];
[shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.55]];
}
[attrs setValue:shadow forKey:NSShadowAttributeName];
BOOL isUppercase = [str length] > 0 && [str characterAtIndex:0] >= 'A' && [str characterAtIndex:0] <= 'Z';
NSSize size = [str sizeWithAttributes:attrs];
NSPoint p = NSMakePoint(rect.origin.x + rect.size.width/2.0 - size.width/2.0 + 0.5, rect.origin.y + rect.size.height/2.0 - size.height/2.0 + 1 + (isUppercase ? -1 : 0));
[str drawAtPoint:p withAttributes:attrs];
[image unlockFocus];
return image;
}
+ (NSColor *)rotateColor:(NSColor *)c byOffset:(CGFloat)offset
{
return [NSColor colorWithCalibratedHue:fmod([c hueComponent] + offset, 1.0) saturation:[c saturationComponent] brightness:[c brightnessComponent] alpha:[c alphaComponent]];
}
+ (NSColor *)rotateColor:(NSColor *)c toOffset:(CGFloat)offset
{
return [NSColor colorWithCalibratedHue:offset saturation:[c saturationComponent] brightness:[c brightnessComponent] alpha:[c alphaComponent]];
}
@end