Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added a dependency on my OPCoreText repository, and thus greatly simp…

…lified the encoding and decoding methods of NSAttributedString.
  • Loading branch information...
commit f9271bfe6a0f9cb7798ff6e2480eb9a483aa4300 1 parent fc30cd3
Brandon Williams authored
79 NSAttributedString+Encoding.m
@@ -7,6 +7,7 @@
7 7 //
8 8
9 9 #import "NSAttributedString+Encoding.h"
  10 +#import "NSDictionary+OPCoreText.h"
10 11
11 12 const struct NSAttributedStringArchiveKeys {
12 13 __unsafe_unretained NSString *rootString;
@@ -25,8 +26,6 @@
25 26 @interface NSAttributedString (Encoding_Private)
26 27 -(NSDictionary*) dictionaryRepresentation;
27 28 +(id) attributedStringWithDictionaryRepresentation:(NSDictionary*)dictionary;
28   -+(NSDictionary*) dictionaryRepresentationOfFont:(CTFontRef)fontRef;
29   -+(CTFontRef) fontFromDictionaryRepresentation:(NSDictionary*)dictionary;
30 29 @end
31 30
32 31 @implementation NSAttributedString (Encoding)
@@ -59,8 +58,8 @@ +(id) attributedStringWithDictionaryRepresentation:(NSDictionary*)dictionary {
59 58
60 59 if ([key isEqual:(NSString*)kCTFontAttributeName])
61 60 {
62   - CTFontRef fontRef = [[self class] fontFromDictionaryRepresentation:attr];
63   - [retVal addAttribute:key value:(__bridge id)fontRef range:range];
  61 + CTFontRef fontRef = [attr createFontRef];
  62 + [retVal addAttribute:key value:(__bridge_transfer id)fontRef range:range];
64 63 }
65 64 else if([key isEqualToString:(NSString*)kCTForegroundColorFromContextAttributeName] ||
66 65 [key isEqualToString:(NSString*)kCTKernAttributeName] ||
@@ -81,32 +80,7 @@ +(id) attributedStringWithDictionaryRepresentation:(NSDictionary*)dictionary {
81 80 }
82 81 else if([key isEqualToString:(NSString*)kCTParagraphStyleAttributeName])
83 82 {
84   - CTParagraphStyleSetting settings[[attr count]];
85   - int settingIndex = 0;
86   -
87   -#define PARAGRAPH_SETTING(datatype, specifier, container) \
88   - datatype container = sizeof(datatype) == sizeof(CGFloat) ? [[attr objectForKey:[NSNumber numberWithInt:specifier]] floatValue] : [[attr objectForKey:[NSNumber numberWithInt:specifier]] intValue]; \
89   - settings[settingIndex].spec = specifier; \
90   - settings[settingIndex].valueSize = sizeof(datatype); \
91   - settings[settingIndex].value = &container; \
92   - settingIndex++; \
93   -
94   - PARAGRAPH_SETTING(uint8_t, kCTParagraphStyleSpecifierAlignment, alignment);
95   - PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierFirstLineHeadIndent, firstLineHeadIndent);
96   - PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierHeadIndent, headIndent);
97   - PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierTailIndent, tailIndent);
98   - PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierDefaultTabInterval, defaultTabInterval);
99   - PARAGRAPH_SETTING(uint8_t, kCTParagraphStyleSpecifierLineBreakMode, linebreakMode);
100   - PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierLineHeightMultiple, lineHeightMultiple);
101   - PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierMaximumLineHeight, maximumLineHeight);
102   - PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierMinimumLineHeight, minimumLineHeight);
103   - PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierLineSpacing, lineSpacing);
104   - PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierParagraphSpacing, paragraphSpacing);
105   - PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierParagraphSpacingBefore, paragraphSpacingBefore);
106   - PARAGRAPH_SETTING(int8_t, kCTParagraphStyleSpecifierBaseWritingDirection, baseWritingDirection);
107   -
108   - CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate(settings, [attr count]);
109   -
  83 + CTParagraphStyleRef paragraphStyleRef = [attr createParagraphStyleRef];
110 84 [retVal addAttribute:key value:(__bridge_transfer id)paragraphStyleRef range:range];
111 85 }
112 86 else if([key isEqualToString:(NSString*)kCTGlyphInfoAttributeName])
@@ -146,7 +120,7 @@ -(NSDictionary*) dictionaryRepresentation {
146 120
147 121 if ([key isEqual:(NSString*)kCTFontAttributeName])
148 122 {
149   - [attributeDictionary setObject:[[self class] dictionaryRepresentationOfFont:(CTFontRef)attr] forKey:key];
  123 + [attributeDictionary setObject:[[NSDictionary alloc] initWithFontRef:(__bridge CTFontRef)attr] forKey:key];
150 124 }
151 125 else if([key isEqualToString:(NSString*)kCTForegroundColorFromContextAttributeName] ||
152 126 [key isEqualToString:(NSString*)kCTKernAttributeName] ||
@@ -167,27 +141,7 @@ -(NSDictionary*) dictionaryRepresentation {
167 141 }
168 142 else if([key isEqualToString:(NSString*)kCTParagraphStyleAttributeName])
169 143 {
170   - NSMutableDictionary *paragraphDictionary = [NSMutableDictionary new];
171   -
172   -#define SPECIFIER_VALUE(datatype, specifier, container) {\
173   - datatype container; \
174   - CTParagraphStyleGetValueForSpecifier((__bridge CTParagraphStyleRef)attr, specifier, sizeof(datatype), &container); \
175   - [paragraphDictionary setObject:sizeof(datatype)==sizeof(CGFloat) ? [NSNumber numberWithFloat:container] : [NSNumber numberWithInt:container] forKey:[NSNumber numberWithInt:specifier]]; \
176   -}
177   - SPECIFIER_VALUE(uint8_t, kCTParagraphStyleSpecifierAlignment, alignment);
178   - SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierFirstLineHeadIndent, firstLineHeadIndent);
179   - SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierHeadIndent, headIndent);
180   - SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierTailIndent, tailIndent);
181   - SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierDefaultTabInterval, defaultTabInterval);
182   - SPECIFIER_VALUE(uint8_t, kCTParagraphStyleSpecifierLineBreakMode, linebreakMode);
183   - SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierLineHeightMultiple, lineHeightMultiple);
184   - SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierMaximumLineHeight, maximumLineHeight);
185   - SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierMinimumLineHeight, minimumLineHeight);
186   - SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierLineSpacing, lineSpacing);
187   - SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierParagraphSpacing, paragraphSpacing);
188   - SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierParagraphSpacingBefore, paragraphSpacingBefore);
189   - SPECIFIER_VALUE(int8_t, kCTParagraphStyleSpecifierBaseWritingDirection, baseWritingDirection);
190   - [attributeDictionary setObject:paragraphDictionary forKey:key];
  144 + [attributeDictionary setObject:[[NSDictionary alloc] initWithParagraphStyleRef:(__bridge CTParagraphStyleRef)attr] forKey:key];
191 145 }
192 146 else if([key isEqualToString:(NSString*)kCTGlyphInfoAttributeName])
193 147 {
@@ -197,31 +151,10 @@ -(NSDictionary*) dictionaryRepresentation {
197 151 {
198 152 // TODO
199 153 }
200   -
201 154 }];
202   -
203 155 }];
204 156
205 157 return retVal;
206 158 }
207 159
208   -+(NSDictionary*) dictionaryRepresentationOfFont:(CTFontRef)fontRef {
209   -
210   - NSDictionary *retVal = nil;
211   - CTFontDescriptorRef descriptorRef = CTFontCopyFontDescriptor(fontRef);
212   - CFDictionaryRef attributesRef = CTFontDescriptorCopyAttributes(descriptorRef);
213   - retVal = (__bridge_transfer NSDictionary*)attributesRef;
214   - CFRelease(descriptorRef);
215   - return retVal;
216   -}
217   -
218   -+(CTFontRef) fontFromDictionaryRepresentation:(NSDictionary*)dictionary {
219   -
220   - CTFontRef retVal = NULL;
221   - CTFontDescriptorRef descriptorRef = CTFontDescriptorCreateWithAttributes((__bridge_retained CFDictionaryRef)dictionary);
222   - retVal = CTFontCreateWithFontDescriptor(descriptorRef, 0.0f, NULL);
223   - CFRelease(descriptorRef);
224   - return retVal;
225   -}
226   -
227 160 @end
7 NSAttributedString+Encoding.podspec
@@ -13,5 +13,12 @@ Pod::Spec.new do |s|
13 13 s.requires_arc = true
14 14
15 15 s.frameworks = 'CoreText'
  16 +
  17 + s.dependency do |p|
  18 + p.name = 'OPCoreText'
  19 + p.source = { :git => 'https://github.com/mbrandonw/OPCoreText' }
  20 + p.source_files = '*.{h,m}'
  21 + p.frameworks = 'CoreText'
  22 + end
16 23
17 24 end

0 comments on commit f9271bf

Please sign in to comment.
Something went wrong with that request. Please try again.