forked from nschum/FontAwesomeIconFactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNIKFontAwesomeIconFactory.m
155 lines (130 loc) · 4.71 KB
/
NIKFontAwesomeIconFactory.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
#import "NIKFontAwesomeIconFactory.h"
#import "NIKFontAwesomePathFactory.h"
#import "NIKFontAwesomePathRenderer.h"
#if TARGET_OS_IPHONE
typedef UIFont NIKFont;
typedef UIBezierPath NIKBezierPath;
#else
typedef NSFont NIKFont;
typedef NSBezierPath NIKBezierPath;
#endif
@implementation NIKFontAwesomeIconFactory
- (id)init {
self = [super init];
if (self) {
_size = 32.0;
_colors = @[[NIKColor darkGrayColor]];
_strokeColor = [NIKColor blackColor];
_strokeWidth = 0.0;
}
return self;
}
- (NIKFont *)createFont {
return [NIKFont fontWithName:@"FontAwesome" size:14.0];
}
#pragma mark - copy
- (id)copyWithZone:(NSZone *)zone {
NIKFontAwesomeIconFactory *copy = [[[self class] allocWithZone:zone] init];
if (copy != nil) {
copy.size = self.size;
copy.edgeInsets = self.edgeInsets;
copy.colors = self.colors;
copy.strokeColor = self.strokeColor;
copy.strokeWidth = self.strokeWidth;
}
return copy;
}
- (NIKImage *)createImageForIcon:(NIKFontAwesomeIcon)icon {
CGPathRef path = [self createPath:icon];
NIKImage *image = [self createImageWithPath:path];
CGPathRelease(path);
return image;
}
- (CGPathRef)createPath:(NIKFontAwesomeIcon)icon CF_RETURNS_RETAINED {
CGFloat width = _square ? _size : CGFLOAT_MAX;
return [[NIKFontAwesomePathFactory new] createPathForIcon:icon height:_size maxWidth:width];
}
- (NIKImage *)createImageWithPath:(CGPathRef)path {
CGRect bounds = CGPathGetBoundingBox(path);
CGPoint offset = CGPointZero;
if (_square) {
CGFloat diff = bounds.size.height - bounds.size.width;
if (diff > 0) {
offset.x += .5 * diff;
bounds.size.width = bounds.size.height;
} else {
offset.y += .5 * -diff;
bounds.size.height = bounds.size.width;
}
};
CGFloat padding = _strokeWidth * .5;
offset.x += padding + _edgeInsets.left;
offset.y += padding + _edgeInsets.bottom;
bounds.size.width += 2.0 * padding + _edgeInsets.left + _edgeInsets.right;
bounds.size.height += 2.0 * padding + _edgeInsets.top + _edgeInsets.bottom;
NIKFontAwesomePathRenderer *renderer = [self createRenderer:path];
renderer.offset = offset;
#if TARGET_OS_IPHONE
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
[renderer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
#else
if ([NSImage respondsToSelector:@selector(imageWithSize:flipped:drawingHandler:)]) {
return [NSImage imageWithSize:bounds.size
flipped:NO
drawingHandler:^(CGRect rect) {
NSGraphicsContext *graphicsContext = [NSGraphicsContext currentContext];
[renderer renderInContext:[graphicsContext graphicsPort]];
return YES;
}];
} else {
NSImage *image = [[NSImage alloc] initWithSize:bounds.size];
[image lockFocus];
NSGraphicsContext *graphicsContext = [NSGraphicsContext currentContext];
[renderer renderInContext:[graphicsContext graphicsPort]];
[image unlockFocus];
return image;
}
#endif
}
- (NIKFontAwesomePathRenderer *)createRenderer:(CGPathRef)path {
NIKFontAwesomePathRenderer *renderer = [NIKFontAwesomePathRenderer new];
renderer.path = path;
NSMutableArray *colors = [NSMutableArray arrayWithCapacity:_colors.count];
for (NIKColor *color in _colors) {
CGColorRef cgColor = copyCGColor(color);
[colors addObject:(__bridge id) cgColor];
CGColorRelease(cgColor);
}
renderer.colors = colors;
CGColorRef cgColor = copyCGColor(_strokeColor);
renderer.strokeColor = cgColor;
CGColorRelease(cgColor);
renderer.strokeWidth = _strokeWidth;
return renderer;
}
CF_RETURNS_RETAINED
static CGColorRef copyCGColor(NIKColor *color) {
CGColorRef cgColor;
#if TARGET_OS_IPHONE
cgColor = CGColorCreateCopy(color.CGColor);
#else
if ([color respondsToSelector:@selector(CGColor)]) {
cgColor = CGColorCreateCopy(color.CGColor);
} else {
NSColor *deviceColor = [color colorUsingColorSpace:[NSColorSpace deviceRGBColorSpace]];
CGFloat components[4];
[deviceColor getComponents:components];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
cgColor = CGColorCreate(colorSpace, components);
CGColorSpaceRelease(colorSpace);
}
#endif
return cgColor;
}
@end