/
NSString+CSS.m
335 lines (273 loc) · 7.19 KB
/
NSString+CSS.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// NSString+CSS.m
// DTCoreText
//
// Created by Oliver Drobnik on 31.01.12.
// Copyright (c) 2012 Drobnik.com. All rights reserved.
//
#import "DTCoreText.h"
#import "NSString+CSS.h"
@implementation NSString (CSS)
#pragma mark CSS
- (NSDictionary *)dictionaryOfCSSStyles
{
// font-size:14px;
NSScanner *scanner = [NSScanner scannerWithString:self];
NSString *name = nil;
NSString *value = nil;
NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];
while ([scanner scanCSSAttribute:&name value:&value])
{
[tmpDict setObject:value forKey:name];
}
// converting to non-mutable costs 37.5% of method
// return [NSDictionary dictionaryWithDictionary:tmpDict];
return tmpDict;
}
- (CGFloat)pixelSizeOfCSSMeasureRelativeToCurrentTextSize:(CGFloat)textSize
{
NSUInteger stringLength = [self length];
unichar *_characters = calloc(stringLength, sizeof(unichar));
[self getCharacters:_characters range:NSMakeRange(0, stringLength)];
CGFloat value = 0;
BOOL commaSeen = NO;
BOOL negative = NO;
NSUInteger digitsPastComma = 0;
NSUInteger i=0;
for (; i<stringLength; i++)
{
unichar ch = _characters[i];
if (ch>='0' && ch<='9')
{
float digit = (float)(ch-'0');
value *= 10.0f;
value += digit;
if (commaSeen)
{
digitsPastComma++;
}
}
else if (ch=='.')
{
commaSeen = YES;
}
else if (ch=='-')
{
negative = YES;
}
else
{
// non-numeric character
break;
}
}
if (commaSeen)
{
value /= powf(10.0f, digitsPastComma);
}
// skip whitespace
while (i<stringLength && IS_WHITESPACE(_characters[i]))
{
i++;
}
if (i<stringLength)
{
unichar ch = _characters[i++];
if (ch == '%')
{
// percent value
value *= textSize / 100.0f;
}
else if (ch == 'e')
{
if (i<stringLength)
{
if (_characters[i] == 'm')
{
// em value
value *= textSize;
}
}
}
}
if (negative)
{
value *= -1;
}
free(_characters);
return value;
}
- (NSArray *)arrayOfCSSShadowsWithCurrentTextSize:(CGFloat)textSize currentColor:(DTColor *)color
{
NSScanner *scanner = [NSScanner scannerWithString:self];
NSMutableCharacterSet *tokenEndSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy];
[tokenEndSet addCharactersInString:@","];
NSMutableArray *tmpArray = [NSMutableArray array];
while (![scanner isAtEnd])
{
DTColor *shadowColor = nil;
NSString *offsetXString = nil;
NSString *offsetYString = nil;
NSString *blurString = nil;
if ([scanner scanHTMLColor:&shadowColor])
{
// format: <color> <length> <length> <length>?
if ([scanner scanUpToCharactersFromSet:tokenEndSet intoString:&offsetXString])
{
if ([scanner scanUpToCharactersFromSet:tokenEndSet intoString:&offsetYString])
{
// blur is optional
[scanner scanUpToCharactersFromSet:tokenEndSet intoString:&blurString];
CGFloat offset_x = [offsetXString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:textSize];
CGFloat offset_y = [offsetYString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:textSize];
CGSize offset = CGSizeMake(offset_x, offset_y);
CGFloat blur = [blurString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:textSize];
NSValue *offsetValue;
#if TARGET_OS_IPHONE
offsetValue = [NSValue valueWithCGSize:offset];
#else
offsetValue = [NSValue valueWithSize:offset];
#endif
NSDictionary *shadowDict = [NSDictionary dictionaryWithObjectsAndKeys:offsetValue, @"Offset",
[NSNumber numberWithFloat:blur], @"Blur",
shadowColor, @"Color", nil];
[tmpArray addObject:shadowDict];
}
}
}
else
{
// format: <length> <length> <length>? <color>?
if ([scanner scanUpToCharactersFromSet:tokenEndSet intoString:&offsetXString])
{
if ([scanner scanUpToCharactersFromSet:tokenEndSet intoString:&offsetYString])
{
// blur is optional
if (![scanner scanHTMLColor:&shadowColor])
{
if ([scanner scanUpToCharactersFromSet:tokenEndSet intoString:&blurString])
{
if (![scanner scanHTMLColor:&shadowColor])
{
}
}
}
if (!shadowColor)
{
// color is same as color attribute of style
shadowColor = color;
}
CGFloat offset_x = [offsetXString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:textSize];
CGFloat offset_y = [offsetYString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:textSize];
CGSize offset = CGSizeMake(offset_x, offset_y);
CGFloat blur = [blurString pixelSizeOfCSSMeasureRelativeToCurrentTextSize:textSize];
NSValue *offsetValue;
#if TARGET_OS_IPHONE
offsetValue = [NSValue valueWithCGSize:offset];
#else
offsetValue = [NSValue valueWithSize:offset];
#endif
NSDictionary *shadowDict = [NSDictionary dictionaryWithObjectsAndKeys:offsetValue, @"Offset",
[NSNumber numberWithFloat:blur], @"Blur",
shadowColor, @"Color", nil];
[tmpArray addObject:shadowDict];
}
}
}
// now there should be a comma
if (![scanner scanString:@"," intoString:NULL])
{
break;
}
}
return tmpArray;
}
- (CGFloat)CSSpixelSize
{
if ([self hasSuffix:@"px"])
{
return [self floatValue];
}
return [self floatValue];
}
- (NSString *)stringByDecodingCSSContentAttribute
{
NSUInteger length = [self length];
unichar *characters = calloc(length, sizeof(unichar));
unichar *final = calloc(length, sizeof(unichar));
[self getCharacters:characters range:NSMakeRange(0, length)];
NSUInteger outChars = 0;
BOOL inEscapedSequence = NO;
unichar decodedChar = 0;
NSUInteger escapedCharacterCount = 0;
for (NSUInteger idx=0; idx<length;idx++)
{
unichar character = characters[idx];
if (inEscapedSequence && (escapedCharacterCount<4))
{
if (character=='\\')
{
// escaped backslash
final[outChars++] = '\\';
inEscapedSequence = NO;
}
else if ((character>='0' && character<='9') || (character>='A' && character<='F') || (character>='a' && character<='f'))
{
// hex digit
decodedChar *= 16;
if (character>='0' && character<='9')
{
decodedChar += (character - '0');
}
else if (character>='A' && character<='F')
{
decodedChar += (character - 'A' + 10);
}
else if (character>='a' && character<='f')
{
decodedChar += (character - 'a' + 10);
}
escapedCharacterCount++;
}
else
{
// illegal character following slash
final[outChars++] = '\\';
final[outChars++] = character;
inEscapedSequence = NO;
}
}
else
{
if (inEscapedSequence)
{
// output what we have decoded so far
final[outChars++] = decodedChar;
}
if (character == '\\')
{
// begin of escape sequence
decodedChar = 0;
escapedCharacterCount = 0;
inEscapedSequence = YES;
}
else
{
inEscapedSequence = NO;
// just copy
final[outChars++] = character;
}
}
}
// if string ended in escaped sequence we still need to output
if (inEscapedSequence)
{
// output what we have decoded so far
final[outChars++] = decodedChar;
}
free(characters);
NSString *clean = [[NSString alloc] initWithCharacters:final length:outChars];
free(final);
return clean;
}
@end