Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 172 lines (121 sloc) 4.099 kb
5b63af9 @Cocoanetics Added font collection class
authored
1 //
2 // DTCoreTextFontCollection.m
3 // CoreTextExtensions
4 //
5 // Created by Oliver Drobnik on 5/23/11.
6 // Copyright 2011 Drobnik.com. All rights reserved.
7 //
8
9 #import "DTCoreTextFontCollection.h"
10 #import "DTCoreTextFontDescriptor.h"
11
2c45232 @jcbertin Fixed CoreText include for MacOS target.
jcbertin authored
12 #if TARGET_OS_IPHONE
5b63af9 @Cocoanetics Added font collection class
authored
13 #import <CoreText/CoreText.h>
2c45232 @jcbertin Fixed CoreText include for MacOS target.
jcbertin authored
14 #elif TARGET_OS_MAC
15 #import <ApplicationServices/ApplicationServices.h>
16 #endif
5b63af9 @Cocoanetics Added font collection class
authored
17
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
18 @interface DTCoreTextFontCollection ()
19
05aa188 @dhoerl Looking real good now - no leaks
dhoerl authored
20 @property (nonatomic, strong) NSArray *fontDescriptors;
21 @property (nonatomic, strong) NSCache *fontMatchCache;
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
22
23 @end
24
25 static DTCoreTextFontCollection *_availableFontsCollection = nil;
5b63af9 @Cocoanetics Added font collection class
authored
26
27
28 @implementation DTCoreTextFontCollection
06980a2 @dhoerl18 Debug a retain cycle; moved ivars to implementation; cleared all warning...
dhoerl18 authored
29 {
30 NSArray *_fontDescriptors;
31 NSCache *fontMatchCache;
32 }
5b63af9 @Cocoanetics Added font collection class
authored
33
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
34 + (DTCoreTextFontCollection *)availableFontsCollection
35 {
e5e1568 @johnezang Add multi-threading safe initialization of singleton objects. Off by de...
johnezang authored
36 static dispatch_once_t predicate;
c967589 @dhoerl Fixed up project so it would build (removed dependencies on DTWebArchive...
dhoerl authored
37
e5e1568 @johnezang Add multi-threading safe initialization of singleton objects. Off by de...
johnezang authored
38 dispatch_once(&predicate, ^{
39 _availableFontsCollection = [[DTCoreTextFontCollection alloc] initWithAvailableFonts];
40 });
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
41
42 return _availableFontsCollection;
43 }
44
45 - (id)initWithAvailableFonts
46 {
47 self = [super init];
48
49 if (self)
50 {
7528d11 @jonsterling Normalized indentation (tabs)
jonsterling authored
51
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
52 }
53
54 return self;
55 }
56
57
58 - (DTCoreTextFontDescriptor *)matchingFontDescriptorForFontDescriptor:(DTCoreTextFontDescriptor *)descriptor
59 {
60 DTCoreTextFontDescriptor *firstMatch = nil;
61 NSNumber *cacheKey = [NSString stringWithFormat:@"fontFamily BEGINSWITH[cd] %@ and boldTrait == %d and italicTrait == %d", descriptor.fontFamily, descriptor.boldTrait, descriptor.italicTrait];
7528d11 @jonsterling Normalized indentation (tabs)
jonsterling authored
62
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
63 // try cache
64 firstMatch = [self.fontMatchCache objectForKey:cacheKey];
65
66 if (firstMatch)
67 {
05aa188 @dhoerl Looking real good now - no leaks
dhoerl authored
68 DTCoreTextFontDescriptor *retMatch = [firstMatch copy];
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
69 retMatch.pointSize = descriptor.pointSize;
70 return retMatch;
71 }
72
73 // need to search
74 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"fontFamily BEGINSWITH[cd] %@ and boldTrait == %d and italicTrait == %d", descriptor.fontFamily, descriptor.boldTrait, descriptor.italicTrait];
75
76 NSArray *matchingDescriptors = [self.fontDescriptors filteredArrayUsingPredicate:predicate];
77
12c91a2 @johnezang Clean up logging statements. Remove a few, switch most from NSLog to FL...
johnezang authored
78 //NSLog(@"%@", matchingDescriptors);
7528d11 @jonsterling Normalized indentation (tabs)
jonsterling authored
79
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
80 if ([matchingDescriptors count])
5b63af9 @Cocoanetics Added font collection class
authored
81 {
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
82 firstMatch = [matchingDescriptors objectAtIndex:0];
83 [self.fontMatchCache setObject:firstMatch forKey:cacheKey];
5b63af9 @Cocoanetics Added font collection class
authored
84
05aa188 @dhoerl Looking real good now - no leaks
dhoerl authored
85 DTCoreTextFontDescriptor *retMatch = [firstMatch copy];
5b63af9 @Cocoanetics Added font collection class
authored
86
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
87 retMatch.pointSize = descriptor.pointSize;
88 return retMatch;
89 }
90
91 return nil;
92 }
93
94 #pragma mark Properties
95
96 - (NSArray *)fontDescriptors
97 {
98 if (!_fontDescriptors)
99 {
100 // try caches
101
102 NSString *cachesPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"FontDescriptors.cache"];
5b63af9 @Cocoanetics Added font collection class
authored
103
b400b66 @Cocoanetics Updated font overrides to contain all standard fonts that have the 4 com...
authored
104 self.fontDescriptors = nil;//[NSKeyedUnarchiver unarchiveObjectWithFile:cachesPath];
5b63af9 @Cocoanetics Added font collection class
authored
105
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
106 if (!_fontDescriptors)
5b63af9 @Cocoanetics Added font collection class
authored
107 {
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
108 CTFontCollectionRef fonts = CTFontCollectionCreateFromAvailableFonts(NULL);
109
110 CFArrayRef matchingFonts = CTFontCollectionCreateMatchingFontDescriptors(fonts);
7528d11 @jonsterling Normalized indentation (tabs)
jonsterling authored
111
112 if (matchingFonts)
113 {
114
115 // convert all to our objects
116 NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
117
118 for (NSInteger i=0; i<CFArrayGetCount(matchingFonts); i++)
119 {
120 CTFontDescriptorRef fontDesc = CFArrayGetValueAtIndex(matchingFonts, i);
121
b400b66 @Cocoanetics Updated font overrides to contain all standard fonts that have the 4 com...
authored
122
7528d11 @jonsterling Normalized indentation (tabs)
jonsterling authored
123 DTCoreTextFontDescriptor *desc = [[DTCoreTextFontDescriptor alloc] initWithCTFontDescriptor:fontDesc];
124 [tmpArray addObject:desc];
125 }
126
127
128 CFRelease(matchingFonts);
129
130 self.fontDescriptors = tmpArray;
131 }
132 }
133
134 // cache that
135 [NSKeyedArchiver archiveRootObject:self.fontDescriptors toFile:cachesPath];
136 }
137
138 return _fontDescriptors;
5b63af9 @Cocoanetics Added font collection class
authored
139 }
140
696397a @Cocoanetics Removed DTCache, no longer necessary because we only support >= 4.3
authored
141 - (NSCache *)fontMatchCache
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
142 {
143 if (!fontMatchCache)
144 {
696397a @Cocoanetics Removed DTCache, no longer necessary because we only support >= 4.3
authored
145 fontMatchCache = [[NSCache alloc] init];
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
146 }
147
148 return fontMatchCache;
149 }
150
b400b66 @Cocoanetics Updated font overrides to contain all standard fonts that have the 4 com...
authored
151 - (NSArray *)fontFamilyNames
152 {
153 NSMutableArray *tmpArray = [NSMutableArray array];
154
155 for (DTCoreTextFontDescriptor *oneDescriptor in [self fontDescriptors])
156 {
157 NSString *familyName = oneDescriptor.fontFamily;
158
159 if (![tmpArray containsObject:familyName])
160 {
161 [tmpArray addObject:familyName];
162 }
163 }
164
165 return [tmpArray sortedArrayUsingSelector:@selector(compare:)];
166 }
167
9dcfd0f @Cocoanetics added global font matching cache to speed up font creation
authored
168 @synthesize fontDescriptors = _fontDescriptors;
169 @synthesize fontMatchCache;
170
5b63af9 @Cocoanetics Added font collection class
authored
171 @end
Something went wrong with that request. Please try again.