Added a lock around newMatchingFont #63

Merged
merged 4 commits into from Aug 23, 2011
@@ -29,15 +29,12 @@
CTFontStylisticClass stylisticClass;
- BOOL smallCapsFeature;
+ BOOL smallCapsFeature;
- BOOL _hashSet;
- NSUInteger _hash;
+ BOOL _hashSet;
+ NSUInteger _hash;
}
-// generated fonts are cached
-+ (DTCache *)fontCache;
-
// sets the font face name to use for a specific font family
+ (void)setSmallCapsFontName:(NSString *)fontName forFontFamily:(NSString *)fontFamily bold:(BOOL)bold italic:(BOOL)italic;
+ (NSString *)smallCapsFontNameforFontFamily:(NSString *)fontFamily bold:(BOOL)bold italic:(BOOL)italic;
@@ -11,18 +11,42 @@
static DTCache *_fontCache = nil;
static NSMutableDictionary *_fontOverrides = nil;
+#ifndef __IPHONE_4_3
+ #define __IPHONE_4_3 40300
+#endif
+
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
+static dispatch_semaphore_t fontLock;
+#else
+static pthread_mutex_t fontLock;
+#endif
+
+@interface DTCoreTextFontDescriptor ()
+
+// generated fonts are cached
++ (DTCache *)fontCache;
+
+@end
@implementation DTCoreTextFontDescriptor
++ (void)initialize
+{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
+ fontLock = dispatch_semaphore_create(1);
+#else
+ pthread_mutex_init(&fontLock, NULL);
+#endif
+}
+ (DTCache *)fontCache
{
if (!_fontCache)
{
_fontCache = [[DTCache alloc] init];
}
-
+
return _fontCache;
}
@@ -395,6 +419,12 @@ - (BOOL)supportsNativeSmallCaps
- (CTFontRef)newMatchingFont
{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
+ dispatch_semaphore_wait(fontLock, DISPATCH_TIME_FOREVER);
+#else
+ pthread_mutex_lock(&fontLock);
+#endif
+
NSDictionary *attributes = [self fontAttributes];
DTCache *fontCache = [DTCoreTextFontDescriptor fontCache];
@@ -405,6 +435,11 @@ - (CTFontRef)newMatchingFont
if (cachedFont)
{
CFRetain(cachedFont);
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
+ dispatch_semaphore_signal(fontLock);
+#else
+ pthread_mutex_unlock(&fontLock);
+#endif
return cachedFont;
}
@@ -414,7 +449,6 @@ - (CTFontRef)newMatchingFont
NSString *usedName = fontName;
-
// override fontName if a small caps or regular override is registered
if (fontFamily)
{
@@ -486,7 +520,12 @@ - (CTFontRef)newMatchingFont
// cache it
[fontCache setObject:(id)matchingFont forKey:cacheKey];
}
-
+
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
+ dispatch_semaphore_signal(fontLock);
+#else
+ pthread_mutex_unlock(&fontLock);
+#endif
return matchingFont;
}