Permalink
Browse files

Lock for font actions

  • Loading branch information...
1 parent 7ef4451 commit dce97b9e7521721f374c32d70e8f56ed681e4903 @dhoerl dhoerl committed Aug 17, 2011
Showing with 24 additions and 4 deletions.
  1. +24 −4 Classes/DTCoreTextFontDescriptor.m
@@ -11,10 +11,22 @@
static DTCache *_fontCache = nil;
static NSMutableDictionary *_fontOverrides = nil;
+@interface DTCoreTextFontDescriptor ()
+{
+ CTFontRef fontRef;
+}
+- (void)_newMatchingFont;
-@implementation DTCoreTextFontDescriptor
+@end
+
+static pthread_mutex_t mutex;
+@implementation DTCoreTextFontDescriptor
++ (void)initialize
+{
+ pthread_mutex_init(&mutex, NULL);
+}
+ (DTCache *)fontCache
{
@@ -395,6 +407,14 @@ - (BOOL)supportsNativeSmallCaps
- (CTFontRef)newMatchingFont
{
+ pthread_mutex_lock(&mutex);
+ [self performSelectorOnMainThread:@selector(_newMatchingFont) withObject:nil waitUntilDone:YES];
+ pthread_mutex_unlock(&mutex);
+ return fontRef;
+}
+
+- (void)_newMatchingFont
+{
NSDictionary *attributes = [self fontAttributes];
DTCache *fontCache = [DTCoreTextFontDescriptor fontCache];
@@ -405,7 +425,8 @@ - (CTFontRef)newMatchingFont
if (cachedFont)
{
CFRetain(cachedFont);
- return cachedFont;
+ fontRef = cachedFont;
+ return;
}
CTFontDescriptorRef fontDesc = NULL;
@@ -414,7 +435,6 @@ - (CTFontRef)newMatchingFont
NSString *usedName = fontName;
-
// override fontName if a small caps or regular override is registered
if (fontFamily)
{
@@ -487,7 +507,7 @@ - (CTFontRef)newMatchingFont
[fontCache setObject:(id)matchingFont forKey:cacheKey];
}
- return matchingFont;
+ fontRef = matchingFont;
}
- (void)normalizeSlow

0 comments on commit dce97b9

Please sign in to comment.