Permalink
Browse files

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

…lified the encoding and decoding methods of NSAttributedString.
  • Loading branch information...
1 parent fc30cd3 commit f9271bfe6a0f9cb7798ff6e2480eb9a483aa4300 @mbrandonw committed Apr 8, 2012
Showing with 13 additions and 73 deletions.
  1. +6 −73 NSAttributedString+Encoding.m
  2. +7 −0 NSAttributedString+Encoding.podspec
@@ -7,6 +7,7 @@
//
#import "NSAttributedString+Encoding.h"
+#import "NSDictionary+OPCoreText.h"
const struct NSAttributedStringArchiveKeys {
__unsafe_unretained NSString *rootString;
@@ -25,8 +26,6 @@
@interface NSAttributedString (Encoding_Private)
-(NSDictionary*) dictionaryRepresentation;
+(id) attributedStringWithDictionaryRepresentation:(NSDictionary*)dictionary;
-+(NSDictionary*) dictionaryRepresentationOfFont:(CTFontRef)fontRef;
-+(CTFontRef) fontFromDictionaryRepresentation:(NSDictionary*)dictionary;
@end
@implementation NSAttributedString (Encoding)
@@ -59,8 +58,8 @@ +(id) attributedStringWithDictionaryRepresentation:(NSDictionary*)dictionary {
if ([key isEqual:(NSString*)kCTFontAttributeName])
{
- CTFontRef fontRef = [[self class] fontFromDictionaryRepresentation:attr];
- [retVal addAttribute:key value:(__bridge id)fontRef range:range];
+ CTFontRef fontRef = [attr createFontRef];
+ [retVal addAttribute:key value:(__bridge_transfer id)fontRef range:range];
}
else if([key isEqualToString:(NSString*)kCTForegroundColorFromContextAttributeName] ||
[key isEqualToString:(NSString*)kCTKernAttributeName] ||
@@ -81,32 +80,7 @@ +(id) attributedStringWithDictionaryRepresentation:(NSDictionary*)dictionary {
}
else if([key isEqualToString:(NSString*)kCTParagraphStyleAttributeName])
{
- CTParagraphStyleSetting settings[[attr count]];
- int settingIndex = 0;
-
-#define PARAGRAPH_SETTING(datatype, specifier, container) \
- datatype container = sizeof(datatype) == sizeof(CGFloat) ? [[attr objectForKey:[NSNumber numberWithInt:specifier]] floatValue] : [[attr objectForKey:[NSNumber numberWithInt:specifier]] intValue]; \
- settings[settingIndex].spec = specifier; \
- settings[settingIndex].valueSize = sizeof(datatype); \
- settings[settingIndex].value = &container; \
- settingIndex++; \
-
- PARAGRAPH_SETTING(uint8_t, kCTParagraphStyleSpecifierAlignment, alignment);
- PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierFirstLineHeadIndent, firstLineHeadIndent);
- PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierHeadIndent, headIndent);
- PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierTailIndent, tailIndent);
- PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierDefaultTabInterval, defaultTabInterval);
- PARAGRAPH_SETTING(uint8_t, kCTParagraphStyleSpecifierLineBreakMode, linebreakMode);
- PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierLineHeightMultiple, lineHeightMultiple);
- PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierMaximumLineHeight, maximumLineHeight);
- PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierMinimumLineHeight, minimumLineHeight);
- PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierLineSpacing, lineSpacing);
- PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierParagraphSpacing, paragraphSpacing);
- PARAGRAPH_SETTING(CGFloat, kCTParagraphStyleSpecifierParagraphSpacingBefore, paragraphSpacingBefore);
- PARAGRAPH_SETTING(int8_t, kCTParagraphStyleSpecifierBaseWritingDirection, baseWritingDirection);
-
- CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate(settings, [attr count]);
-
+ CTParagraphStyleRef paragraphStyleRef = [attr createParagraphStyleRef];
[retVal addAttribute:key value:(__bridge_transfer id)paragraphStyleRef range:range];
}
else if([key isEqualToString:(NSString*)kCTGlyphInfoAttributeName])
@@ -146,7 +120,7 @@ -(NSDictionary*) dictionaryRepresentation {
if ([key isEqual:(NSString*)kCTFontAttributeName])
{
- [attributeDictionary setObject:[[self class] dictionaryRepresentationOfFont:(CTFontRef)attr] forKey:key];
+ [attributeDictionary setObject:[[NSDictionary alloc] initWithFontRef:(__bridge CTFontRef)attr] forKey:key];
}
else if([key isEqualToString:(NSString*)kCTForegroundColorFromContextAttributeName] ||
[key isEqualToString:(NSString*)kCTKernAttributeName] ||
@@ -167,27 +141,7 @@ -(NSDictionary*) dictionaryRepresentation {
}
else if([key isEqualToString:(NSString*)kCTParagraphStyleAttributeName])
{
- NSMutableDictionary *paragraphDictionary = [NSMutableDictionary new];
-
-#define SPECIFIER_VALUE(datatype, specifier, container) {\
- datatype container; \
- CTParagraphStyleGetValueForSpecifier((__bridge CTParagraphStyleRef)attr, specifier, sizeof(datatype), &container); \
- [paragraphDictionary setObject:sizeof(datatype)==sizeof(CGFloat) ? [NSNumber numberWithFloat:container] : [NSNumber numberWithInt:container] forKey:[NSNumber numberWithInt:specifier]]; \
-}
- SPECIFIER_VALUE(uint8_t, kCTParagraphStyleSpecifierAlignment, alignment);
- SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierFirstLineHeadIndent, firstLineHeadIndent);
- SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierHeadIndent, headIndent);
- SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierTailIndent, tailIndent);
- SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierDefaultTabInterval, defaultTabInterval);
- SPECIFIER_VALUE(uint8_t, kCTParagraphStyleSpecifierLineBreakMode, linebreakMode);
- SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierLineHeightMultiple, lineHeightMultiple);
- SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierMaximumLineHeight, maximumLineHeight);
- SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierMinimumLineHeight, minimumLineHeight);
- SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierLineSpacing, lineSpacing);
- SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierParagraphSpacing, paragraphSpacing);
- SPECIFIER_VALUE(CGFloat, kCTParagraphStyleSpecifierParagraphSpacingBefore, paragraphSpacingBefore);
- SPECIFIER_VALUE(int8_t, kCTParagraphStyleSpecifierBaseWritingDirection, baseWritingDirection);
- [attributeDictionary setObject:paragraphDictionary forKey:key];
+ [attributeDictionary setObject:[[NSDictionary alloc] initWithParagraphStyleRef:(__bridge CTParagraphStyleRef)attr] forKey:key];
}
else if([key isEqualToString:(NSString*)kCTGlyphInfoAttributeName])
{
@@ -197,31 +151,10 @@ -(NSDictionary*) dictionaryRepresentation {
{
// TODO
}
-
}];
-
}];
return retVal;
}
-+(NSDictionary*) dictionaryRepresentationOfFont:(CTFontRef)fontRef {
-
- NSDictionary *retVal = nil;
- CTFontDescriptorRef descriptorRef = CTFontCopyFontDescriptor(fontRef);
- CFDictionaryRef attributesRef = CTFontDescriptorCopyAttributes(descriptorRef);
- retVal = (__bridge_transfer NSDictionary*)attributesRef;
- CFRelease(descriptorRef);
- return retVal;
-}
-
-+(CTFontRef) fontFromDictionaryRepresentation:(NSDictionary*)dictionary {
-
- CTFontRef retVal = NULL;
- CTFontDescriptorRef descriptorRef = CTFontDescriptorCreateWithAttributes((__bridge_retained CFDictionaryRef)dictionary);
- retVal = CTFontCreateWithFontDescriptor(descriptorRef, 0.0f, NULL);
- CFRelease(descriptorRef);
- return retVal;
-}
-
@end
@@ -13,5 +13,12 @@ Pod::Spec.new do |s|
s.requires_arc = true
s.frameworks = 'CoreText'
+
+ s.dependency do |p|
+ p.name = 'OPCoreText'
+ p.source = { :git => 'https://github.com/mbrandonw/OPCoreText' }
+ p.source_files = '*.{h,m}'
+ p.frameworks = 'CoreText'
+ end
end

0 comments on commit f9271bf

Please sign in to comment.