-
Notifications
You must be signed in to change notification settings - Fork 6
/
MVGraphicsFunctions.m
192 lines (167 loc) · 7.48 KB
/
MVGraphicsFunctions.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
#import "MVGraphicsFunctions.h"
#import "MVShadow.h"
CGImageRef MVCreateNoiseImageRef(NSUInteger width, NSUInteger height, CGFloat factor)
{
NSUInteger size = width*height;
char *rgba = (char *)malloc(size); srand(124);
for(NSUInteger i=0; i < size; ++i){rgba[i] = rand()%256*factor;}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapContext =
CGBitmapContextCreate(rgba, width, height, 8, width, colorSpace, kCGImageAlphaNone);
CFRelease(colorSpace);
free(rgba);
CGImageRef image = CGBitmapContextCreateImage(bitmapContext);
CFRelease(bitmapContext);
return image;
}
NSBezierPath* MVRoundedRectBezierPath(CGRect rrect, CGFloat radius)
{
if(rrect.size.width == 0 || rrect.size.height == 0 ||
rrect.origin.x == INFINITY || rrect.origin.y == INFINITY)
return [NSBezierPath bezierPathWithRect:CGRectZero];
NSBezierPath *path = [NSBezierPath bezierPath];
CGFloat minx = CGRectGetMinX(rrect);
CGFloat midx = CGRectGetMidX(rrect);
CGFloat maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect);
CGFloat midy = CGRectGetMidY(rrect);
CGFloat maxy = CGRectGetMaxY(rrect);
[path moveToPoint:CGPointMake(minx, midy)];
[path appendBezierPathWithArcFromPoint:CGPointMake(minx, miny)
toPoint:CGPointMake(midx, miny)
radius:radius];
[path appendBezierPathWithArcFromPoint:CGPointMake(maxx, miny)
toPoint:CGPointMake(maxx, midy)
radius:radius];
[path appendBezierPathWithArcFromPoint:CGPointMake(maxx, maxy)
toPoint:CGPointMake(midx, maxy)
radius:radius];
[path appendBezierPathWithArcFromPoint:CGPointMake(minx, maxy)
toPoint:CGPointMake(minx, midy)
radius:radius];
[path closePath];
return path;
}
NSFont* MVFontFromStyle(float fontSize, int style)
{
if(style == kMVStringTypeMedium)
return [NSFont fontWithName:@"Helvetica Neue Medium" size:fontSize];
if(style == kMVStringTypeBold)
return [NSFont fontWithName:@"Helvetica Neue Bold" size:fontSize];
return [NSFont fontWithName:@"Helvetica Neue" size:fontSize];
}
#pragma mark -int style
#pragma mark String Drawing Methods
NSSize MVSizeOfString (NSString *string, float fontSize, int style)
{
NSDictionary* dict = MVDictionaryForStringDrawing(fontSize, style);
NSSize size = [string sizeWithAttributes:dict];
return size;
}
NSDictionary* MVDictionaryForStringDrawing (float fontSize, int style)
{
NSDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:MVFontFromStyle(fontSize, style) forKey:NSFontAttributeName];
return dict;
}
void MVDrawString (NSString *string, NSRect aRect, NSColor* fontColor, float fontSize,
int style, NSColor* shadowColor, NSSize shadowOffset, float shadowBlur)
{
MVDrawStringAlign(string, aRect, fontColor, fontSize, style,
shadowColor, shadowOffset, shadowBlur, 0);
}
void MVDrawStringAlignLineBreakMode (NSString *string, NSRect aRect, NSColor* fontColor,
float fontSize, int style, NSColor* shadowColor,
NSSize shadowOffset, float shadowBlur,
int alignment, int lineBreakMode)
{
NSDictionary* dict = MVDictionaryForStringDrawing(fontSize, bold);
NSShadow *shadow = nil;
if(shadowColor != nil) {
shadow = [[MVShadow alloc] init];
[shadow setShadowOffset:shadowOffset];
[shadow setShadowBlurRadius:shadowBlur];
[shadow setShadowColor:shadowColor];
}
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
if(alignment == 1)
[pStyle setAlignment:NSCenterTextAlignment];
else if(alignment == 2)
[pStyle setAlignment:NSRightTextAlignment];
[pStyle setLineBreakMode:lineBreakMode];
[dict setValue:pStyle forKey:NSParagraphStyleAttributeName];
[dict setValue:fontColor forKey:NSForegroundColorAttributeName];
[dict setValue:MVFontFromStyle(fontSize, style) forKey:NSFontAttributeName];
[NSGraphicsContext saveGraphicsState];
if(shadow)
[shadow set];
[string drawInRect:aRect withAttributes:dict];
[NSGraphicsContext restoreGraphicsState];
}
void MVDrawStringAlign (NSString *string, NSRect aRect, NSColor* fontColor, float fontSize,
int style, NSColor* shadowColor, NSSize shadowOffset, float shadowBlur,
int alignment)
{
MVDrawStringAlignLineBreakMode(string, aRect, fontColor, fontSize, style, shadowColor,
shadowOffset, shadowBlur, alignment, NSLineBreakByTruncatingTail);
}
void MVDrawAttributedStringWithColor(NSAttributedString *string, NSRect aRect, NSColor* fontColor,
NSColor* shadowColor, NSSize shadowOffset, int alignment,
int lineBreakMode)
{
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithAttributedString:string];
NSShadow *shadow = nil;
if(shadowColor != nil) {
shadow = [[MVShadow alloc] init];
[shadow setShadowOffset:shadowOffset];
[shadow setShadowBlurRadius:1.0];
[shadow setShadowColor:shadowColor];
}
[NSGraphicsContext saveGraphicsState];
if(shadow)
[shadow set];
NSMutableParagraphStyle *style = [[mutableString attribute:NSParagraphStyleAttributeName
atIndex:0
effectiveRange:NULL] mutableCopy];
if(alignment == 1)
[style setAlignment:NSCenterTextAlignment];
else if(alignment == 2)
[style setAlignment:NSRightTextAlignment];
[style setLineBreakMode:lineBreakMode];
if(fontColor)
{
[mutableString addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
fontColor,NSForegroundColorAttributeName,
nil]
range:NSMakeRange(0, [mutableString length])];
}
[mutableString addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
style,NSParagraphStyleAttributeName,
nil]
range:NSMakeRange(0, [mutableString length])];
[mutableString drawInRect:aRect];
[NSGraphicsContext restoreGraphicsState];
}
#pragma mark -
#pragma mark Window Chrome
void MVDrawWindowTitleBar (CGRect drawingRect, NSWindow *window)
{
BOOL drawsAsMainWindow = ([window isMainWindow] &&
[[NSApplication sharedApplication] isActive]);
NSColor *startColor = drawsAsMainWindow ? IN_COLOR_MAIN_START_L : IN_COLOR_NOTMAIN_START_L;
NSColor *endColor = drawsAsMainWindow ? IN_COLOR_MAIN_END_L : IN_COLOR_NOTMAIN_END_L;
NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:startColor endingColor:endColor];
[gradient drawInRect:drawingRect angle:90];
if (drawsAsMainWindow) {
static CGImageRef noisePattern = nil;
if (noisePattern == nil) noisePattern = MVCreateNoiseImageRef(128, 128, 0.015);
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext] setCompositingOperation:NSCompositePlusLighter];
CGRect noisePatternRect = CGRectZero;
noisePatternRect.size = CGSizeMake(CGImageGetWidth(noisePattern),
CGImageGetHeight(noisePattern));
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextDrawTiledImage(context, noisePatternRect, noisePattern);
[NSGraphicsContext restoreGraphicsState];
}
}