-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAttributedPlainText.m
executable file
·323 lines (261 loc) · 11.6 KB
/
AttributedPlainText.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
//
// AttributedPlainText.m
// Notation
//
// Created by Zachary Schneirov on 1/16/06.
// Copyright 2006 Zachary Schneirov. All rights reserved.
//
#import "AttributedPlainText.h"
#import "NSCollection_utils.h"
#import "GlobalPrefs.h"
#import "NSString_NV.h"
@implementation NSMutableAttributedString (AttributedPlainText)
- (void)trimLeadingWhitespace {
NSMutableCharacterSet *whiteSet = [[[NSMutableCharacterSet alloc] init] autorelease];
[whiteSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//include attachment characters and non-breaking spaces. anything else?
unichar badChars[2] = { NSAttachmentCharacter, 0x00A0 };
[whiteSet addCharactersInString:[NSString stringWithCharacters:badChars length:2]];
NSScanner *scanner = [NSScanner scannerWithString:[self string]];
if ([scanner scanCharactersFromSet:whiteSet intoString:NULL]) {
if ([scanner scanLocation] > 0) {
[self deleteCharactersInRange:NSMakeRange(0, [scanner scanLocation])];
//NSLog(@"deleting %d chars", [scanner scanLocation]);
}
}
}
- (void)removeAttachments {
unsigned loc = 0;
unsigned end = [self length];
while (loc < end) {
/* Run through the string in terms of attachment runs */
NSRange attachmentRange; /* Attachment attribute run */
NSTextAttachment *attachment = [self attribute:NSAttachmentAttributeName atIndex:loc longestEffectiveRange:&attachmentRange inRange:NSMakeRange(loc, end-loc)];
if (attachment != nil) { /* If there is an attachment, make sure it is valid */
unichar ch = [[self string] characterAtIndex:loc];
if (ch == NSAttachmentCharacter) {
[self replaceCharactersInRange:NSMakeRange(loc, 1) withString:@""];
end = [self length]; /* New length */
} else {
loc++; /* Just skip over the current character... */
}
} else {
loc = NSMaxRange(attachmentRange);
}
}
}
- (void)prefixWithSourceString:(NSString*)source {
source = [NSString stringWithFormat:@"From <%@>:\n\n", source];
[self insertAttributedString:[[[NSAttributedString alloc] initWithString:source] autorelease] atIndex:0];
}
- (void)santizeForeignStylesForImporting {
NSRange range = NSMakeRange(0, [self length]);
[self removeAttribute:NSLinkAttributeName range:range];
[self restyleTextToFont:[[GlobalPrefs defaultPrefs] noteBodyFont] usingBaseFont:nil];
[self addLinkAttributesForRange:range];
}
- (BOOL)restyleTextToFont:(NSFont*)currentFont usingBaseFont:(NSFont*)baseFont {
NSRange effectiveRange = NSMakeRange(0,0);
unsigned int stringLength = [self length];
int rangesChanged = 0;
NSFontManager *fontMan = [NSFontManager sharedFontManager];
NSDictionary *defaultBodyAttributes = [[GlobalPrefs defaultPrefs] noteBodyAttributes];
NSAssert(currentFont != nil, @"restyleTextToFont needs a current font!");
NS_DURING
while (NSMaxRange(effectiveRange) < stringLength) {
// Get the attributes for the current range
NSDictionary *attributes = [self attributesAtIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange];
NSMutableDictionary *newAttributes = [defaultBodyAttributes mutableCopyWithZone:nil];
[newAttributes addDesiredAttributesFromDictionary:attributes];
NSFont *aFont = [attributes objectForKey:NSFontAttributeName];
NSFont *newFont = currentFont;
BOOL needToMatchAttributes = NO;
NSFontTraitMask traits = 0;
if (!baseFont) {
if (aFont) {
//we have a font--try to match its traits, if there are any
traits = [fontMan traitsOfFont:aFont];
if (traits & NSBoldFontMask || traits & NSItalicFontMask)
//do we need to check stroke width & obliqueness? the base font should only be nonexistent when we have new foreign text
needToMatchAttributes = YES;
}
} else if (!aFont || [[aFont fontName] isEqualToString:[baseFont fontName]]) {
//just change the font to currentFont
newFont = currentFont;
} else if ([[aFont familyName] isEqualToString:[baseFont familyName]]) {
traits = [fontMan traitsOfFont:aFont];
needToMatchAttributes = YES;
}
BOOL hasFakeItalic = [attributes objectForKey:NSObliquenessAttributeName] != nil;
BOOL hasFakeBold = [attributes objectForKey:NSStrokeWidthAttributeName] != nil;
if (needToMatchAttributes || hasFakeItalic || hasFakeBold) {
newFont = [fontMan convertFont:aFont toFamily:[currentFont familyName]];
if (hasFakeItalic) newFont = [fontMan convertFont:newFont toHaveTrait:NSItalicFontMask];
if (hasFakeBold) newFont = [fontMan convertFont:newFont toHaveTrait:NSBoldFontMask];
NSFontTraitMask newTraits = [fontMan traitsOfFont:newFont];
if (!(newTraits & NSItalicFontMask) && (traits & NSItalicFontMask)) {
[newAttributes setObject:[NSNumber numberWithFloat:0.20] forKey:NSObliquenessAttributeName];
} else if (newTraits & NSItalicFontMask) {
[newAttributes removeObjectForKey:NSObliquenessAttributeName];
}
if (!(newTraits & NSBoldFontMask) && (traits & NSBoldFontMask)) {
[newAttributes setObject:[NSNumber numberWithFloat:-3.50] forKey:NSStrokeWidthAttributeName];
} else if (newTraits & NSBoldFontMask) {
[newAttributes removeObjectForKey:NSStrokeWidthAttributeName];
}
}
if (newFont && [newFont pointSize] != [currentFont pointSize]) {
//also make the font have the same size
newFont = [fontMan convertFont:newFont toSize:[currentFont pointSize]];
}
[newAttributes setObject:newFont ? newFont : currentFont forKey:NSFontAttributeName];
[self setAttributes:newAttributes range:effectiveRange];
[newAttributes release];
rangesChanged++;
}
NS_HANDLER
NSLog(@"Error trying to re-style text (%@, %@)", [localException name], [localException reason]);
NS_ENDHANDLER
return rangesChanged > 0;
}
- (void)addLinkAttributesForRange:(NSRange)changedRange {
NSCharacterSet *antiURLSpace = [NSAttributedString antiURLCharacterSet];
NSString *string = [self string];
NSRange totalRange = NSMakeRange(0, [string length]);
NSString *substring = string;
if (!NSEqualRanges(totalRange, changedRange)) {
substring = [string substringWithRange:changedRange];
}
if ([substring rangeOfCharacterFromSet:antiURLSpace options:NSLiteralSearch].location == NSNotFound)
substring = [substring stringByAppendingString:@" "];
NSScanner *wordScanner = [NSScanner scannerWithString:substring];
//loop until end of string
while (![wordScanner isAtEnd]) {
NSRange wordRange = NSMakeRange(NSNotFound, 0);
NSString *word = nil;
while ([wordScanner scanUpToCharactersFromSet:antiURLSpace intoString:&word]) {
if (word) {
wordRange.length = [word length];
wordRange.location = changedRange.location + [wordScanner scanLocation] - wordRange.length;
NSAssert([word isEqualToString:[string substringWithRange:wordRange]], @"derived range is wrong!");
NSURL *url = nil;
if (wordRange.length && NSMaxRange(wordRange) <= [string length] && (url = [word linkForWord])) {
if (url) [self addAttribute:NSLinkAttributeName value:url range:wordRange];
}
}
}
unsigned int newLocation = [wordScanner scanLocation] + 1;
if (newLocation >= [substring length])
break;
[wordScanner setScanLocation:newLocation];
}
}
#if SEPARATE_ATTRS
#define VLISTBUFCOUNT 32
//string after apply an array of attributes
+ (NSMutableAttributedString*)attributedStringWithString:(NSString*)text attributesByRange:(NSDictionary*)attributes font:(NSFont*)font {
id *keys, *values;
id keysBuffer[VLISTBUFCOUNT], valuesBuffer[VLISTBUFCOUNT];
int i, discreteRangeCount = [attributes count];
NSMutableAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
keys = (discreteRangeCount <= VLISTBUFCOUNT) ? keysBuffer : (id*)malloc(sizeof(id) * discreteRangeCount);
values = (discreteRangeCount <= VLISTBUFCOUNT) ? valuesBuffer : (id*)malloc(sizeof(id) * discreteRangeCount);
if (keys && values && attributes) {
CFDictionaryGetKeysAndValues((CFDictionaryRef)attributes, (void*)keys, (void*)values);
NS_DURING
for (i=0; i<discreteRangeCount; i++) {
NSValue *rangeValue = keys[i];
NSDictionary *theseAttributes = values[i];
if (rangeValue && theseAttributes) {
//we ought to do font substitution here, too -- convertFont:(NSFont *)aFont toFace:, for those matching default font
[attributedString setAttributes:theseAttributes range:[rangeValue rangeValue]];
}
}
NS_HANDLER
NSLog(@"Error setting attributes for string. %@: %@", [localException name], [localException reason]);
NS_ENDHANDLER
if (keys != keysBuffer)
free(keys);
if (values != valuesBuffer)
free(values);
} else {
NSLog(@"Could not get values or keys! Not applying any attributes.");
}
return [attributedString autorelease];
}
#endif
@end
@implementation NSAttributedString (AttributedPlainText)
+ (NSCharacterSet*)antiURLCharacterSet {
static NSMutableCharacterSet *antiURLSpace = nil;
if (!antiURLSpace) {
antiURLSpace = [[NSMutableCharacterSet alloc] init];
[antiURLSpace formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[antiURLSpace addCharactersInString:@"<>[]\""];
}
return antiURLSpace;
}
- (NSArray*)allLinks {
NSRange range;
unsigned int startIndex = 0;
NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
while (startIndex < [self length]) {
id alink = [self findNextLinkAtIndex:startIndex effectiveRange:&range];
if ([alink isKindOfClass:[NSURL class]]) {
[array addObject:alink];
}
startIndex = range.location+range.length;
}
return array;
}
- (id)findNextLinkAtIndex:(unsigned int)startIndex effectiveRange:(NSRange *)range {
NSRange linkRange;
id alink = nil;
while (!alink && startIndex < [self length]) {
alink = [self attribute:NSLinkAttributeName atIndex:startIndex effectiveRange:&linkRange];
startIndex++;
}
if (alink) {
range->location = linkRange.location;
range->length = linkRange.length;
} else {
range->location = NSNotFound;
range->length = 0;
}
return alink;
}
#if SEPARATE_ATTRS
//extract the attributes using their ranges as keys
- (NSDictionary*)attributesByRange {
NSMutableDictionary *allAttributes = [NSMutableDictionary dictionaryWithCapacity:1];
NSDictionary *attributes;
NSRange effectiveRange = NSMakeRange(0,0);
NSUInteger stringLength = [self length];
NS_DURING
while (NSMaxRange(effectiveRange) < stringLength) {
// Get the attributes for the current range
attributes = [self attributesAtIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange];
[allAttributes setObject:attributes forKey:[NSValue valueWithRange:effectiveRange]];
}
NS_HANDLER
NSLog(@"Error getting attributes: %@", [localException reason]);
NS_ENDHANDLER
return allAttributes;
}
#endif
+ (NSAttributedString*)timeDelayStringWithNumberOfSeconds:(double)seconds {
unichar ch = 0x2245;
static NSAttributedString *approxCharStr = nil;
if (!approxCharStr) {
NSMutableParagraphStyle *centerStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
[centerStyle setAlignment:NSCenterTextAlignment];
approxCharStr = [[NSAttributedString alloc] initWithString:[NSString stringWithCharacters:&ch length:1] attributes:
[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Symbol" size:16.0f], NSFontAttributeName, centerStyle, NSParagraphStyleAttributeName, nil]];
}
NSMutableAttributedString *mutableStr = [approxCharStr mutableCopy];
NSString *timeStr = seconds < 1.0 ? [NSString stringWithFormat:@" %0.0f ms", seconds*1000] : [NSString stringWithFormat:@" %0.2f secs", seconds];
[mutableStr appendAttributedString:[[[NSAttributedString alloc] initWithString:timeStr attributes:
[NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:13.0f] forKey:NSFontAttributeName]] autorelease]];
return [mutableStr autorelease];
}
@end