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
| static bool s_isIOS7OrHigher = false;
static inline void lazyCheckIOS7()
{
static bool isInited = false;
if (!isInited)
{
s_isIOS7OrHigher = [[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending;
isInited = true;
}
}
// refer CCImage::ETextAlign
#define ALIGN_TOP 1
#define ALIGN_CENTER 3
#define ALIGN_BOTTOM 2
static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAlign, const char * pFontName, int nSize, tImageInfo* pInfo)
{
// lazy check whether it is iOS7 device
lazyCheckIOS7();
bool bRet = false;
do
{
CC_BREAK_IF(! pText || ! pInfo);
NSString * str = [NSString stringWithUTF8String:pText];
NSString * fntName = [NSString stringWithUTF8String:pFontName];
CGSize dim, constrainSize;
constrainSize.width = pInfo->width;
constrainSize.height = pInfo->height;
// On iOS custom fonts must be listed beforehand in the App info.plist (in order to be usable) and referenced only the by the font family name itself when
// calling [UIFont fontWithName]. Therefore even if the developer adds 'SomeFont.ttf' or 'fonts/SomeFont.ttf' to the App .plist, the font must
// be referenced as 'SomeFont' when calling [UIFont fontWithName]. Hence we strip out the folder path components and the extension here in order to get just
// the font family name itself. This stripping step is required especially for references to user fonts stored in CCB files; CCB files appear to store
// the '.ttf' extensions when referring to custom fonts.
fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
// create the font
id font = [UIFont fontWithName:fntName size:nSize];
if (font)
{
dim = _calculateStringSize(str, font, &constrainSize);
}
else
{
if (!font)
{
font = [UIFont systemFontOfSize:nSize];
}
if (font)
{
dim = _calculateStringSize(str, font, &constrainSize);
}
}
CC_BREAK_IF(! font);
// compute start point
int startH = 0;
if (constrainSize.height > dim.height)
{
// vertical alignment
unsigned int vAlignment = (eAlign >> 4) & 0x0F;
if (vAlignment == ALIGN_TOP)
{
startH = 0;
}
else if (vAlignment == ALIGN_CENTER)
{
startH = (constrainSize.height - dim.height) / 2;
}
else
{
startH = constrainSize.height - dim.height;
}
}
// adjust text rect
if (constrainSize.width > 0 && constrainSize.width > dim.width)
{
dim.width = constrainSize.width;
}
if (constrainSize.height > 0 && constrainSize.height > dim.height)
{
dim.height = constrainSize.height;
}
// compute the padding needed by shadow and stroke
float shadowStrokePaddingX = 0.0f;
float shadowStrokePaddingY = 0.0f;
if ( pInfo->hasStroke )
{
shadowStrokePaddingX = (ceilf(pInfo->strokeSize))*2;
shadowStrokePaddingY = (ceilf(pInfo->strokeSize))*2;
}
if ( pInfo->hasShadow )
{
shadowStrokePaddingX = std::max(shadowStrokePaddingX, (float)abs(pInfo->shadowOffset.width));
shadowStrokePaddingY = std::max(shadowStrokePaddingY, (float)abs(pInfo->shadowOffset.height));
}
// add the padding (this could be 0 if no shadow and no stroke)
dim.width += shadowStrokePaddingX;
dim.height += shadowStrokePaddingY;
unsigned char* data = new unsigned char[(int)(dim.width * dim.height * 4)];
memset(data, 0, (int)(dim.width * dim.height * 4));
// draw text
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(data,
dim.width,
dim.height,
8,
(int)(dim.width) * 4,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
if (!context)
{
delete[] data;
break;
}
// text color
CGContextSetRGBFillColor(context, pInfo->tintColorR, pInfo->tintColorG, pInfo->tintColorB, 1);
// move Y rendering to the top of the image
CGContextTranslateCTM(context, 0.0f, (dim.height - shadowStrokePaddingY));
CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
// store the current context
//UIGraphicsPushContext(context);
// measure text size with specified font and determine the rectangle to draw text in
unsigned uHoriFlag = eAlign & 0x0f;
UITextAlignment align = (UITextAlignment)((2 == uHoriFlag) ? UITextAlignmentRight
: (3 == uHoriFlag) ? UITextAlignmentCenter
: UITextAlignmentLeft);
//------------------------------------------------------------------------------------
CGRect rect = CGRectMake(0, startH - shadowStrokePaddingY, dim.width, dim.height);
UIGraphicsPushContext(context);
CGContextSetShouldSubpixelQuantizeFonts(context, false);
CGContextBeginTransparencyLayerWithRect(context, rect, NULL);
float textOriginX = 0.0;
float textOrigingY = 0.0;
float textWidth = dim.width - shadowStrokePaddingX;
float textHeight = dim.height - shadowStrokePaddingY;
if ( pInfo->shadowOffset.width < 0 )
{
textOriginX = shadowStrokePaddingX;
}
else
{
textOriginX = 0.0;
}
if (pInfo->shadowOffset.height > 0)
{
textOrigingY = startH;
}
else
{
textOrigingY = startH - shadowStrokePaddingY;
}
CGRect textRect = CGRectMake(textOriginX, textOrigingY, textWidth, textHeight);
if ( pInfo->hasStroke )
{
CGContextSetTextDrawingMode(context, kCGTextStroke);
if(s_isIOS7OrHigher)
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = align;
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
[str drawInRect:textRect withAttributes:@{
NSFontAttributeName: font,
NSStrokeWidthAttributeName: [NSNumber numberWithFloat: pInfo->strokeSize / nSize * 100 ],
NSForegroundColorAttributeName:[UIColor colorWithRed:pInfo->tintColorR
green:pInfo->tintColorG
blue:pInfo->tintColorB
alpha:1.0f],
NSParagraphStyleAttributeName:paragraphStyle,
NSStrokeColorAttributeName: [UIColor colorWithRed:pInfo->strokeColorR
green:pInfo->strokeColorG
blue:pInfo->strokeColorB
alpha:1.0f]
}
];
[paragraphStyle release];
}
else
{
CGContextSetRGBStrokeColor(context, pInfo->strokeColorR, pInfo->strokeColorG, pInfo->strokeColorB, 1);
CGContextSetLineWidth(context, pInfo->strokeSize);
//original code that was not working in iOS 7
[str drawInRect: textRect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:align];
}
}
// take care of shadow if needed
if ( pInfo->hasShadow )
{
CGSize offset;
offset.height = pInfo->shadowOffset.height;
offset.width = pInfo->shadowOffset.width;
CGContextSetShadow(context, offset, pInfo->shadowBlur);
}
//------------------------------------------------------------------------------------
// normal fonts
//if( [font isKindOfClass:[UIFont class] ] )
//{
// [str drawInRect:CGRectMake(0, startH, dim.width, dim.height) withFont:font lineBreakMode:(UILineBreakMode)UILineBreakModeWordWrap alignment:align];
//}
//else // ZFont class
//{
// [FontLabelStringDrawingHelper drawInRect:str rect:CGRectMake(0, startH, dim.width, dim.height) withZFont:font lineBreakMode:(UILineBreakMode)UILineBreakModeWordWrap
////alignment:align];
//}
//------------------------------------------------------------------------------------
// actually draw the text in the context
// XXX: ios7 casting
//[str drawInRect:CGRectMake(textOriginX, textOrigingY, textWidth, textHeight) withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:(NSTextAlignment)align];
CGContextSetTextDrawingMode(context, kCGTextFill);
// actually draw the text in the context
[str drawInRect: textRect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:align];
CGContextEndTransparencyLayer(context);
// pop the context
UIGraphicsPopContext();
// release the context
CGContextRelease(context);
// output params
pInfo->data = data;
pInfo->hasAlpha = true;
pInfo->isPremultipliedAlpha = true;
pInfo->bitsPerComponent = 8;
pInfo->width = dim.width;
pInfo->height = dim.height;
bRet = true;
} while (0);
return bRet;
}
|