-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
YYTextArchiver.m
252 lines (214 loc) · 6.95 KB
/
YYTextArchiver.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
//
// YYTextArchiver.m
// YYText <https://github.com/ibireme/YYText>
//
// Created by ibireme on 15/3/16.
// Copyright (c) 2015 ibireme.
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "YYTextArchiver.h"
#import "YYTextRunDelegate.h"
#import "YYTextRubyAnnotation.h"
/**
When call CTRunDelegateGetTypeID() on some devices (runs iOS6), I got the error:
"dyld: lazy symbol binding failed: Symbol not found: _CTRunDelegateGetTypeID"
Here's a workaround for this issue.
*/
static CFTypeID CTRunDelegateTypeID() {
static CFTypeID typeID;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
/*
if ((long)CTRunDelegateGetTypeID + 1 > 1) { //avoid compiler optimization
typeID = CTRunDelegateGetTypeID();
}
*/
YYTextRunDelegate *delegate = [YYTextRunDelegate new];
CTRunDelegateRef ref = delegate.CTRunDelegate;
typeID = CFGetTypeID(ref);
CFRelease(ref);
});
return typeID;
}
static CFTypeID CTRubyAnnotationTypeID() {
static CFTypeID typeID;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if ((long)CTRubyAnnotationGetTypeID + 1 > 1) { //avoid compiler optimization
typeID = CTRunDelegateGetTypeID();
} else {
typeID = kCFNotFound;
}
});
return typeID;
}
/**
A wrapper for CGColorRef. Used for Archive/Unarchive/Copy.
*/
@interface _YYCGColor : NSObject <NSCopying, NSCoding>
@property (nonatomic, assign) CGColorRef CGColor;
+ (instancetype)colorWithCGColor:(CGColorRef)CGColor;
@end
@implementation _YYCGColor
+ (instancetype)colorWithCGColor:(CGColorRef)CGColor {
_YYCGColor *color = [self new];
color.CGColor = CGColor;
return color;
}
- (void)setCGColor:(CGColorRef)CGColor {
if (_CGColor != CGColor) {
if (CGColor) CGColor = (CGColorRef)CFRetain(CGColor);
if (_CGColor) CFRelease(_CGColor);
_CGColor = CGColor;
}
}
- (void)dealloc {
if (_CGColor) CFRelease(_CGColor);
_CGColor = NULL;
}
- (id)copyWithZone:(NSZone *)zone {
_YYCGColor *color = [self.class new];
color.CGColor = self.CGColor;
return color;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
UIColor *color = [UIColor colorWithCGColor:_CGColor];
[aCoder encodeObject:color forKey:@"color"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [self init];
UIColor *color = [aDecoder decodeObjectForKey:@"color"];
self.CGColor = color.CGColor;
return self;
}
@end
/**
A wrapper for CGImageRef. Used for Archive/Unarchive/Copy.
*/
@interface _YYCGImage : NSObject <NSCoding, NSCopying>
@property (nonatomic, assign) CGImageRef CGImage;
+ (instancetype)imageWithCGImage:(CGImageRef)CGImage;
@end
@implementation _YYCGImage
+ (instancetype)imageWithCGImage:(CGImageRef)CGImage {
_YYCGImage *image = [self new];
image.CGImage = CGImage;
return image;
}
- (void)setCGImage:(CGImageRef)CGImage {
if (_CGImage != CGImage) {
if (CGImage) CGImage = (CGImageRef)CFRetain(CGImage);
if (_CGImage) CFRelease(_CGImage);
_CGImage = CGImage;
}
}
- (void)dealloc {
if (_CGImage) CFRelease(_CGImage);
}
- (id)copyWithZone:(NSZone *)zone {
_YYCGImage *image = [self.class new];
image.CGImage = self.CGImage;
return image;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
UIImage *image = [UIImage imageWithCGImage:_CGImage];
[aCoder encodeObject:image forKey:@"image"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [self init];
UIImage *image = [aDecoder decodeObjectForKey:@"image"];
self.CGImage = image.CGImage;
return self;
}
@end
@implementation YYTextArchiver
+ (NSData *)archivedDataWithRootObject:(id)rootObject {
if (!rootObject) return nil;
NSMutableData *data = [NSMutableData data];
YYTextArchiver *archiver = [[[self class] alloc] initForWritingWithMutableData:data];
[archiver encodeRootObject:rootObject];
[archiver finishEncoding];
return data;
}
+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path {
NSData *data = [self archivedDataWithRootObject:rootObject];
if (!data) return NO;
return [data writeToFile:path atomically:YES];
}
- (instancetype)init {
self = [super init];
self.delegate = self;
return self;
}
- (instancetype)initForWritingWithMutableData:(NSMutableData *)data {
self = [super initForWritingWithMutableData:data];
self.delegate = self;
return self;
}
- (id)archiver:(NSKeyedArchiver *)archiver willEncodeObject:(id)object {
CFTypeID typeID = CFGetTypeID((CFTypeRef)object);
if (typeID == CTRunDelegateTypeID()) {
CTRunDelegateRef runDelegate = (__bridge CFTypeRef)(object);
id ref = CTRunDelegateGetRefCon(runDelegate);
if (ref) return ref;
} else if (typeID == CTRubyAnnotationTypeID()) {
CTRubyAnnotationRef ctRuby = (__bridge CFTypeRef)(object);
YYTextRubyAnnotation *ruby = [YYTextRubyAnnotation rubyWithCTRubyRef:ctRuby];
if (ruby) return ruby;
} else if (typeID == CGColorGetTypeID()) {
return [_YYCGColor colorWithCGColor:(CGColorRef)object];
} else if (typeID == CGImageGetTypeID()) {
return [_YYCGImage imageWithCGImage:(CGImageRef)object];
}
return object;
}
@end
@implementation YYTextUnarchiver
+ (id)unarchiveObjectWithData:(NSData *)data {
if (data.length == 0) return nil;
YYTextUnarchiver *unarchiver = [[self alloc] initForReadingWithData:data];
return [unarchiver decodeObject];
}
+ (id)unarchiveObjectWithFile:(NSString *)path {
NSData *data = [NSData dataWithContentsOfFile:path];
return [self unarchiveObjectWithData:data];
}
- (instancetype)init {
self = [super init];
self.delegate = self;
return self;
}
- (instancetype)initForReadingWithData:(NSData *)data {
self = [super initForReadingWithData:data];
self.delegate = self;
return self;
}
- (id)unarchiver:(NSKeyedUnarchiver *)unarchiver didDecodeObject:(id) NS_RELEASES_ARGUMENT object NS_RETURNS_RETAINED {
if ([object class] == [YYTextRunDelegate class]) {
YYTextRunDelegate *runDelegate = object;
CTRunDelegateRef ct = runDelegate.CTRunDelegate;
id ctObj = (__bridge id)ct;
if (ct) CFRelease(ct);
return ctObj;
} else if ([object class] == [YYTextRubyAnnotation class]) {
YYTextRubyAnnotation *ruby = object;
if ([UIDevice currentDevice].systemVersion.floatValue >= 8) {
CTRubyAnnotationRef ct = ruby.CTRubyAnnotation;
id ctObj = (__bridge id)(ct);
if (ct) CFRelease(ct);
return ctObj;
} else {
return object;
}
} else if ([object class] == [_YYCGColor class]) {
_YYCGColor *color = object;
return (id)color.CGColor;
} else if ([object class] == [_YYCGImage class]) {
_YYCGImage *image = object;
return (id)image.CGImage;
}
return object;
}
@end