summary refs log tree commit diff stats
path: root/libs/FontLabel/FontManager.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/FontLabel/FontManager.m')
-rwxr-xr-xlibs/FontLabel/FontManager.m123
1 files changed, 123 insertions, 0 deletions
diff --git a/libs/FontLabel/FontManager.m b/libs/FontLabel/FontManager.m new file mode 100755 index 0000000..12eac2d --- /dev/null +++ b/libs/FontLabel/FontManager.m
@@ -0,0 +1,123 @@
1//
2// FontManager.m
3// FontLabel
4//
5// Created by Kevin Ballard on 5/5/09.
6// Copyright © 2009 Zynga Game Networks
7//
8//
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12//
13// http://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//
21
22#import "FontManager.h"
23#import "ZFont.h"
24
25static FontManager *sharedFontManager = nil;
26
27@implementation FontManager
28+ (FontManager *)sharedManager {
29 @synchronized(self) {
30 if (sharedFontManager == nil) {
31 sharedFontManager = [[self alloc] init];
32 }
33 }
34 return sharedFontManager;
35}
36
37- (id)init {
38 if ((self = [super init])) {
39 fonts = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
40 urls = [[NSMutableDictionary alloc] init];
41 }
42 return self;
43}
44
45- (BOOL)loadFont:(NSString *)filename {
46 NSString *fontPath = [[NSBundle mainBundle] pathForResource:filename ofType:@"ttf"];
47 if (fontPath == nil) {
48 fontPath = [[NSBundle mainBundle] pathForResource:filename ofType:nil];
49 }
50 if (fontPath == nil) return NO;
51
52 NSURL *url = [NSURL fileURLWithPath:fontPath];
53 if ([self loadFontURL:url]) {
54 [urls setObject:url forKey:filename];
55 return YES;
56 }
57 return NO;
58}
59
60- (BOOL)loadFontURL:(NSURL *)url {
61 CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((CFURLRef)url);
62 if (fontDataProvider == NULL) return NO;
63 CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
64 CGDataProviderRelease(fontDataProvider);
65 if (newFont == NULL) return NO;
66
67 CFDictionarySetValue(fonts, url, newFont);
68 CGFontRelease(newFont);
69 return YES;
70}
71
72- (CGFontRef)fontWithName:(NSString *)filename {
73 CGFontRef font = NULL;
74 NSURL *url = [urls objectForKey:filename];
75 if (url == nil && [self loadFont:filename]) {
76 url = [urls objectForKey:filename];
77 }
78 if (url != nil) {
79 font = (CGFontRef)CFDictionaryGetValue(fonts, url);
80 }
81 return font;
82}
83
84- (ZFont *)zFontWithName:(NSString *)filename pointSize:(CGFloat)pointSize {
85 NSURL *url = [urls objectForKey:filename];
86 if (url == nil && [self loadFont:filename]) {
87 url = [urls objectForKey:filename];
88 }
89 if (url != nil) {
90 CGFontRef cgFont = (CGFontRef)CFDictionaryGetValue(fonts, url);
91 if (cgFont != NULL) {
92 return [ZFont fontWithCGFont:cgFont size:pointSize];
93 }
94 }
95 return nil;
96}
97
98- (ZFont *)zFontWithURL:(NSURL *)url pointSize:(CGFloat)pointSize {
99 CGFontRef cgFont = (CGFontRef)CFDictionaryGetValue(fonts, url);
100 if (cgFont == NULL && [self loadFontURL:url]) {
101 cgFont = (CGFontRef)CFDictionaryGetValue(fonts, url);
102 }
103 if (cgFont != NULL) {
104 return [ZFont fontWithCGFont:cgFont size:pointSize];
105 }
106 return nil;
107}
108
109- (CFArrayRef)copyAllFonts {
110 CFIndex count = CFDictionaryGetCount(fonts);
111 CGFontRef *values = (CGFontRef *)malloc(sizeof(CGFontRef) * count);
112 CFDictionaryGetKeysAndValues(fonts, NULL, (const void **)values);
113 CFArrayRef array = CFArrayCreate(NULL, (const void **)values, count, &kCFTypeArrayCallBacks);
114 free(values);
115 return array;
116}
117
118- (void)dealloc {
119 CFRelease(fonts);
120 [urls release];
121 [super dealloc];
122}
123@end