forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTTextStylePlatformSpecific.m
More file actions
261 lines (201 loc) · 8.15 KB
/
Copy pathCPTTextStylePlatformSpecific.m
File metadata and controls
261 lines (201 loc) · 8.15 KB
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
#import "CPTTextStylePlatformSpecific.h"
#import "CPTMutableTextStyle.h"
#import "CPTPlatformSpecificCategories.h"
#import "CPTPlatformSpecificFunctions.h"
@implementation CPTTextStyle(CPTPlatformSpecificTextStyleExtensions)
/** @property NSDictionary *attributes
* @brief A dictionary of standard text attributes suitable for formatting an NSAttributedString.
*
* The dictionary will contain values for the following keys that represent the receiver's text style:
* - #NSFontAttributeName: The font used to draw text. If missing, no font information was specified.
* - #NSForegroundColorAttributeName: The color used to draw text. If missing, no color information was specified.
* - #NSParagraphStyleAttributeName: The text alignment and line break mode used to draw multi-line text.
**/
@dynamic attributes;
#pragma mark -
#pragma mark Init/Dealloc
/** @brief Creates and returns a new CPTTextStyle instance initialized from a dictionary of text attributes.
*
* The text style will be initalized with values associated with the following keys:
* - #NSFontAttributeName: Sets the @link CPTTextStyle::fontName fontName @endlink
* and @link CPTTextStyle::fontSize fontSize @endlink.
* - #NSForegroundColorAttributeName: Sets the @link CPTTextStyle::color color @endlink.
* - #NSParagraphStyleAttributeName: Sets the @link CPTTextStyle::textAlignment textAlignment @endlink and @link CPTTextStyle::lineBreakMode lineBreakMode @endlink.
*
* Properties associated with missing keys will be inialized to their default values.
*
* @param attributes A dictionary of standard text attributes.
* @return A new CPTTextStyle instance.
**/
+(instancetype)textStyleWithAttributes:(NSDictionary *)attributes
{
CPTMutableTextStyle *newStyle = [CPTMutableTextStyle textStyle];
// Font
NSFont *styleFont = attributes[NSFontAttributeName];
if ( styleFont ) {
newStyle.fontName = styleFont.fontName;
newStyle.fontSize = styleFont.pointSize;
}
// Color
NSColor *styleColor = attributes[NSForegroundColorAttributeName];
if ( styleColor ) {
// CGColor property is available in Mac OS 10.8 and later
if ( [styleColor respondsToSelector:@selector(CGColor)] ) {
newStyle.color = [CPTColor colorWithCGColor:styleColor.CGColor];
}
else {
const NSInteger numberOfComponents = [styleColor numberOfComponents];
CGFloat *components = calloc( (size_t)numberOfComponents, sizeof(CGFloat) );
[styleColor getComponents:components];
CGColorSpaceRef colorSpace = [[styleColor colorSpace] CGColorSpace];
CGColorRef styleCGColor = CGColorCreate(colorSpace, components);
newStyle.color = [CPTColor colorWithCGColor:styleCGColor];
CGColorRelease(styleCGColor);
free(components);
}
}
// Text alignment and line break mode
NSParagraphStyle *paragraphStyle = attributes[NSParagraphStyleAttributeName];
if ( paragraphStyle ) {
newStyle.textAlignment = (CPTTextAlignment)paragraphStyle.alignment;
newStyle.lineBreakMode = paragraphStyle.lineBreakMode;
}
return [newStyle copy];
}
#pragma mark -
#pragma mark Accessors
/// @cond
-(NSDictionary *)attributes
{
NSMutableDictionary *myAttributes = [NSMutableDictionary dictionary];
// Font
NSFont *styleFont = nil;
NSString *fontName = self.fontName;
if ( fontName ) {
styleFont = [NSFont fontWithName:fontName size:self.fontSize];
}
if ( styleFont ) {
[myAttributes setValue:styleFont
forKey:NSFontAttributeName];
}
// Color
NSColor *styleColor = self.color.nsColor;
if ( styleColor ) {
[myAttributes setValue:styleColor
forKey:NSForegroundColorAttributeName];
}
// Text alignment and line break mode
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = (NSTextAlignment)self.textAlignment;
paragraphStyle.lineBreakMode = self.lineBreakMode;
[myAttributes setValue:paragraphStyle
forKey:NSParagraphStyleAttributeName];
return [myAttributes copy];
}
/// @endcond
@end
#pragma mark -
@implementation CPTMutableTextStyle(CPTPlatformSpecificMutableTextStyleExtensions)
/// @cond
+(instancetype)textStyleWithAttributes:(NSDictionary *)attributes
{
CPTMutableTextStyle *newStyle = [CPTMutableTextStyle textStyle];
// Font
NSFont *styleFont = attributes[NSFontAttributeName];
if ( styleFont ) {
newStyle.fontName = styleFont.fontName;
newStyle.fontSize = styleFont.pointSize;
}
// Color
NSColor *styleColor = attributes[NSForegroundColorAttributeName];
if ( styleColor ) {
// CGColor property is available in Mac OS 10.8 and later
if ( [styleColor respondsToSelector:@selector(CGColor)] ) {
newStyle.color = [CPTColor colorWithCGColor:styleColor.CGColor];
}
else {
const NSInteger numberOfComponents = [styleColor numberOfComponents];
CGFloat *components = calloc( (size_t)numberOfComponents, sizeof(CGFloat) );
[styleColor getComponents:components];
CGColorSpaceRef colorSpace = [[styleColor colorSpace] CGColorSpace];
CGColorRef styleCGColor = CGColorCreate(colorSpace, components);
newStyle.color = [CPTColor colorWithCGColor:styleCGColor];
CGColorRelease(styleCGColor);
free(components);
}
}
// Text alignment and line break mode
NSParagraphStyle *paragraphStyle = attributes[NSParagraphStyleAttributeName];
if ( paragraphStyle ) {
newStyle.textAlignment = (CPTTextAlignment)paragraphStyle.alignment;
newStyle.lineBreakMode = paragraphStyle.lineBreakMode;
}
return newStyle;
}
/// @endcond
@end
#pragma mark -
@implementation NSString(CPTTextStyleExtensions)
#pragma mark -
#pragma mark Layout
/** @brief Determines the size of text drawn with the given style.
* @param style The text style.
* @return The size of the text when drawn with the given style.
**/
-(CGSize)sizeWithTextStyle:(CPTTextStyle *)style
{
NSFont *theFont = nil;
NSString *fontName = style.fontName;
if ( fontName ) {
theFont = [NSFont fontWithName:fontName size:style.fontSize];
}
CGSize textSize;
if ( theFont ) {
NSDictionary *attributes = @{
NSFontAttributeName: theFont
};
textSize = NSSizeToCGSize([self sizeWithAttributes:attributes]);
}
else {
textSize = CGSizeZero;
}
return textSize;
}
#pragma mark -
#pragma mark Drawing
/** @brief Draws the text into the given graphics context using the given style.
* @param rect The bounding rectangle in which to draw the text.
* @param style The text style.
* @param context The graphics context to draw into.
**/
-(void)drawInRect:(CGRect)rect withTextStyle:(CPTTextStyle *)style inContext:(CGContextRef)context
{
if ( style.color == nil ) {
return;
}
CGColorRef textColor = style.color.cgColor;
CGContextSetStrokeColorWithColor(context, textColor);
CGContextSetFillColorWithColor(context, textColor);
CPTPushCGContext(context);
NSFont *theFont = nil;
NSString *fontName = style.fontName;
if ( fontName ) {
theFont = [NSFont fontWithName:fontName size:style.fontSize];
}
if ( theFont ) {
NSColor *foregroundColor = style.color.nsColor;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = (NSTextAlignment)style.textAlignment;
paragraphStyle.lineBreakMode = style.lineBreakMode;
NSDictionary *attributes = @{
NSFontAttributeName: theFont,
NSForegroundColorAttributeName: foregroundColor,
NSParagraphStyleAttributeName: paragraphStyle
};
[self drawWithRect:NSRectFromCGRect(rect)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine
attributes:attributes];
}
CPTPopCGContext();
}
@end