Skip to content

Commit

Permalink
Add method to copy stylistic traits between font descriptors, option …
Browse files Browse the repository at this point in the history
…to add font descriptors, option to add tag path
  • Loading branch information
odrobnik committed Mar 22, 2011
1 parent eec6191 commit f4b290c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 31 deletions.
2 changes: 2 additions & 0 deletions Classes/DTCoreTextFontDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
@property (nonatomic) BOOL verticalTrait;
@property (nonatomic) BOOL UIoptimizedTrait;

@property (nonatomic, assign) CTFontSymbolicTraits symbolicTraits;

@property (nonatomic) CTFontStylisticClass stylisticClass;


Expand Down
66 changes: 36 additions & 30 deletions Classes/DTCoreTextFontDescriptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -260,68 +260,72 @@ - (void)setFontAttributes:(NSDictionary *)attributes

CTFontSymbolicTraits traitsValue = [[traitsDict objectForKey:(id)kCTFontSymbolicTrait ] unsignedIntValue];

if (traitsValue & kCTFontBoldTrait)
self.symbolicTraits = traitsValue;


NSNumber *pointNum = [attributes objectForKey:(id)kCTFontSizeAttribute];
if (pointNum)
{
boldTrait = YES;
pointSize = [pointNum floatValue];
}

if (traitsValue & kCTFontItalicTrait)
NSString *family = [attributes objectForKey:(id)kCTFontFamilyNameAttribute];

if (family)
{
italicTrait = YES;
self.fontFamily = family;
}

if (traitsValue & kCTFontExpandedTrait)
NSString *name = [attributes objectForKey:(id)kCTFontNameAttribute];

if (name)
{
expandedTrait = YES;
self.fontName = name;
}

if (traitsValue & kCTFontCondensedTrait)
}

- (void)setSymbolicTraits:(CTFontSymbolicTraits)symbolicTraits
{
if (symbolicTraits & kCTFontBoldTrait)
{
condensedTrait = YES;
boldTrait = YES;
}

if (traitsValue & kCTFontMonoSpaceTrait)
if (symbolicTraits & kCTFontItalicTrait)
{
monospaceTrait = YES;
italicTrait = YES;
}

if (traitsValue & kCTFontVerticalTrait)
if (symbolicTraits & kCTFontExpandedTrait)
{
verticalTrait = YES;
expandedTrait = YES;
}

if (traitsValue & kCTFontUIOptimizedTrait)
if (symbolicTraits & kCTFontCondensedTrait)
{
UIoptimizedTrait = YES;
condensedTrait = YES;
}

// stylistic class is bundled in the traits
stylisticClass = traitsValue & kCTFontClassMaskTrait;


NSNumber *pointNum = [attributes objectForKey:(id)kCTFontSizeAttribute];
if (pointNum)
if (symbolicTraits & kCTFontMonoSpaceTrait)
{
pointSize = [pointNum floatValue];
monospaceTrait = YES;
}

NSString *family = [attributes objectForKey:(id)kCTFontFamilyNameAttribute];

if (family)
if (symbolicTraits & kCTFontVerticalTrait)
{
self.fontFamily = family;
verticalTrait = YES;
}

NSString *name = [attributes objectForKey:(id)kCTFontNameAttribute];

if (name)
if (symbolicTraits & kCTFontUIOptimizedTrait)
{
self.fontName = name;
UIoptimizedTrait = YES;
}

// stylistic class is bundled in the traits
stylisticClass = symbolicTraits & kCTFontClassMaskTrait;
}


@synthesize fontFamily;
@synthesize fontName;

Expand All @@ -334,6 +338,8 @@ - (void)setFontAttributes:(NSDictionary *)attributes
@synthesize verticalTrait;
@synthesize UIoptimizedTrait;

@synthesize symbolicTraits;

@synthesize stylisticClass;

@end
Expand Down
32 changes: 31 additions & 1 deletion Classes/NSAttributedString+HTML.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
// Allows variations to cater for different behavior on iOS than OSX to have similar visual output
#define ALLOW_IPHONE_SPECIAL_CASES 1

// adds the path of tags to attributes dict
//#define ADD_TAG_PATH 1

// adds the original font descriptors to the attributes dicts
//#define ADD_FONT_DESCRIPTORS 1

/* Known Differences:
- OSX has an entire attributes block for an UL block
- OSX does not add extra space after UL block
Expand Down Expand Up @@ -927,6 +933,13 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib

// create font
CTFontRef font = [currentFontDescriptor newMatchingFont];

#if ADD_FONT_DESCRIPTORS
// adds the font description of this string to the attribute dictionary
// e.g. for overriding later

[attributes setObject:currentFontDescriptor forKey:@"FontDescriptor"];
#endif

CGFloat paragraphSpacing = [[currentTag objectForKey:@"ParagraphSpacing"] floatValue];
CGFloat paragraphSpacingBefore = [[currentTag objectForKey:@"ParagraphSpacingBefore"] floatValue];
Expand Down Expand Up @@ -1069,10 +1082,27 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
[attributes setObject:guid forKey:@"DTGUID"];
}

#if ADD_TAG_PATH
// adds the path of the tag containing this string to the attribute dictionary
NSMutableArray *tagPath = [[NSMutableArray alloc] init];
for (NSDictionary *oneTag in tagStack)
{
NSString *tag = [oneTag objectForKey:@"_tag"];
if (!tag)
{
tag = @"";
}
[tagPath addObject:tag];
}

[attributes setObject:[tagPath componentsJoinedByString:@"/"] forKey:@"Path"];
[tagPath release];

NSAttributedString *tagString = [[NSAttributedString alloc] initWithString:tagContents attributes:attributes];
[tmpString appendAttributedString:tagString];
[tagString release];

#endif

previousAttributes = attributes;
}

Expand Down

0 comments on commit f4b290c

Please sign in to comment.