Skip to content
Browse files

Implemented validation of font family set for fallback #661

  • Loading branch information...
1 parent 0a85f7a commit fb129b6f0da1b73703f5fa0c403056d466377c49 @odrobnik odrobnik committed Nov 15, 2013
Showing with 40 additions and 2 deletions.
  1. +3 −0 Core/Source/DTCoreTextConstants.h
  2. +7 −1 Core/Source/DTCoreTextConstants.m
  3. +30 −1 Core/Source/DTCoreTextFontDescriptor.m
View
3 Core/Source/DTCoreTextConstants.h
@@ -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)
View
8 Core/Source/DTCoreTextConstants.m
@@ -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";
+
View
31 Core/Source/DTCoreTextFontDescriptor.m
@@ -9,6 +9,7 @@
#import "DTCoreTextFontDescriptor.h"
#import "DTCoreTextFontCollection.h"
#import "DTCompatibility.h"
+#import "DTCoreTextConstants.h"
static NSCache *_fontCache = nil;
static NSMutableDictionary *_fontOverrides = nil;
@@ -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];
}

0 comments on commit fb129b6

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