Skip to content

Commit

Permalink
Implemented validation of font family set for fallback Cocoanetics#661
Browse files Browse the repository at this point in the history
  • Loading branch information
odrobnik committed Nov 15, 2013
1 parent 0a85f7a commit fb129b6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Core/Source/DTCoreTextConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ extern NSString * const DTListPrefixField;
// iOS 6 compatibility
extern BOOL ___useiOS6Attributes;

// exceptions
extern NSString * const DTCoreTextFontDescriptorException;

// macros

#define IS_WHITESPACE(_c) (_c == ' ' || _c == '\t' || _c == 0xA || _c == 0xB || _c == 0xC || _c == 0xD || _c == 0x85)
Expand Down
8 changes: 7 additions & 1 deletion Core/Source/DTCoreTextConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@

// iOS 6 compatibility

BOOL ___useiOS6Attributes = NO; // this gets set globally by DTHTMLAttributedStringBuilder
BOOL ___useiOS6Attributes = NO; // this gets set globally by DTHTMLAttributedStringBuilder


// exceptions

NSString * const DTCoreTextFontDescriptorException = @"DTCoreTextFontDescriptorException";

31 changes: 30 additions & 1 deletion Core/Source/DTCoreTextFontDescriptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "DTCoreTextFontDescriptor.h"
#import "DTCoreTextFontCollection.h"
#import "DTCompatibility.h"
#import "DTCoreTextConstants.h"

static NSCache *_fontCache = nil;
static NSMutableDictionary *_fontOverrides = nil;
Expand Down Expand Up @@ -152,8 +153,36 @@ + (void)_createDictionaryOfAllAvailableFontOverrideNamesWithCompletion:(void(^)(

+ (void)setFallbackFontFamily:(NSString *)fontFamily
{
NSParameterAssert(fontFamily);
if (!fontFamily)
{
[NSException raise:DTCoreTextFontDescriptorException format:@"Fallback Font Family cannot be nil"];
}

// make sure that only valid font families can be registered
NSDictionary *attributes = [NSDictionary dictionaryWithObject:fontFamily forKey:(id)kCTFontFamilyNameAttribute];
CTFontDescriptorRef fontDesc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)(attributes));
CTFontRef font = CTFontCreateWithFontDescriptor(fontDesc, 12, NULL);

BOOL isValid = NO;

if (font)
{
NSString *usedFontFamily = CFBridgingRelease(CTFontCopyFamilyName(font));

if ([usedFontFamily isEqualToString:fontFamily])
{
isValid = YES;
}

CFRelease(fontDesc);
CFRelease(font);
}

if (!isValid)
{
[NSException raise:DTCoreTextFontDescriptorException format:@"Fallback Font Family '%@' not registered on the system", fontFamily];
}

_fallbackFontFamily = [fontFamily copy];
}

Expand Down

0 comments on commit fb129b6

Please sign in to comment.