tizen beta release
[profile/ivi/webkit-efl.git] / Tools / WebKitTestRunner / InjectedBundle / mac / ActivateFonts.mm
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ActivateFonts.h"
28
29 #import <AppKit/AppKit.h>
30 #import <CoreFoundation/CoreFoundation.h>
31 #import <objc/objc-runtime.h>
32 #import <wtf/RetainPtr.h>
33
34 @interface WKTRFontActivatorDummyClass : NSObject
35 @end
36
37 @implementation WKTRFontActivatorDummyClass
38 @end
39
40 namespace WTR {
41
42
43 static NSSet *allowedFontFamilySet()
44 {
45     static NSSet *fontFamiliySet = [[NSSet setWithObjects:
46         @"Ahem",
47         @"Al Bayan",
48         @"American Typewriter",
49         @"Andale Mono",
50         @"Apple Braille",
51         @"Apple Color Emoji",
52         @"Apple Chancery",
53         @"Apple Garamond BT",
54         @"Apple LiGothic",
55         @"Apple LiSung",
56         @"Apple Symbols",
57         @"AppleGothic",
58         @"AppleMyungjo",
59         @"Arial Black",
60         @"Arial Hebrew",
61         @"Arial Narrow",
62         @"Arial Rounded MT Bold",
63         @"Arial Unicode MS",
64         @"Arial",
65         @"Ayuthaya",
66         @"Baghdad",
67         @"Baskerville",
68         @"BiauKai",
69         @"Big Caslon",
70         @"Brush Script MT",
71         @"Chalkboard",
72         @"Chalkduster",
73         @"Charcoal CY",
74         @"Cochin",
75         @"Comic Sans MS",
76         @"Copperplate",
77         @"Corsiva Hebrew",
78         @"Courier New",
79         @"Courier",
80         @"DecoType Naskh",
81         @"Devanagari MT",
82         @"Didot",
83         @"Euphemia UCAS",
84         @"Futura",
85         @"GB18030 Bitmap",
86         @"Geeza Pro",
87         @"Geneva CY",
88         @"Geneva",
89         @"Georgia",
90         @"Gill Sans",
91         @"Gujarati MT",
92         @"GungSeo",
93         @"Gurmukhi MT",
94         @"HeadLineA",
95         @"Hei",
96         @"Heiti SC",
97         @"Heiti TC",
98         @"Helvetica CY",
99         @"Helvetica Neue",
100         @"Helvetica",
101         @"Herculanum",
102         @"Hiragino Kaku Gothic Pro",
103         @"Hiragino Kaku Gothic ProN",
104         @"Hiragino Kaku Gothic Std",
105         @"Hiragino Kaku Gothic StdN",
106         @"Hiragino Maru Gothic Pro",
107         @"Hiragino Maru Gothic ProN",
108         @"Hiragino Mincho Pro",
109         @"Hiragino Mincho ProN",
110         @"Hiragino Sans GB",
111         @"Hoefler Text",
112         @"Impact",
113         @"InaiMathi",
114         @"Kai",
115         @"Kailasa",
116         @"Kokonor",
117         @"Krungthep",
118         @"KufiStandardGK",
119         @"LiHei Pro",
120         @"LiSong Pro",
121         @"Lucida Grande",
122         @"Marker Felt",
123         @"Menlo",
124         @"Microsoft Sans Serif",
125         @"Monaco",
126         @"Mshtakan",
127         @"Nadeem",
128         @"New Peninim MT",
129         @"Optima",
130         @"Osaka",
131         @"Papyrus",
132         @"PCMyungjo",
133         @"PilGi",
134         @"Plantagenet Cherokee",
135         @"Raanana",
136         @"Sathu",
137         @"Silom",
138         @"Skia",
139         @"STFangsong",
140         @"STHeiti",
141         @"STKaiti",
142         @"STSong",
143         @"Symbol",
144         @"Tahoma",
145         @"Thonburi",
146         @"Times New Roman",
147         @"Times",
148         @"Trebuchet MS",
149         @"Verdana",
150         @"Webdings",
151         @"WebKit WeightWatcher",
152         @"Wingdings 2",
153         @"Wingdings 3",
154         @"Wingdings",
155         @"Zapf Dingbats",
156         @"Zapfino",
157         nil] retain];
158     
159     return fontFamiliySet;
160 }
161
162 static IMP appKitAvailableFontFamiliesIMP;
163 static IMP appKitAvailableFontsIMP;
164
165 static NSArray *wtr_NSFontManager_availableFontFamilies(id self, SEL _cmd)
166 {
167     static NSArray *availableFontFamilies;
168     if (availableFontFamilies)
169         return availableFontFamilies;
170     
171     NSArray *availableFamilies = appKitAvailableFontFamiliesIMP(self, _cmd);
172
173     NSMutableSet *prunedFamiliesSet = [NSMutableSet setWithArray:availableFamilies];
174     [prunedFamiliesSet intersectSet:allowedFontFamilySet()];
175
176     availableFontFamilies = [[prunedFamiliesSet allObjects] retain];
177     return availableFontFamilies;
178 }
179
180 static NSArray *wtr_NSFontManager_availableFonts(id self, SEL _cmd)
181 {
182     static NSArray *availableFonts;
183     if (availableFonts)
184         return availableFonts;
185     
186     NSSet *allowedFamilies = allowedFontFamilySet();
187     NSMutableArray *availableFontList = [[NSMutableArray alloc] initWithCapacity:[allowedFamilies count] * 2];
188     for (NSString *fontFamily in allowedFontFamilySet()) {
189         NSArray* fontsForFamily = [[NSFontManager sharedFontManager] availableMembersOfFontFamily:fontFamily];
190         for (NSArray* fontInfo in fontsForFamily) {
191             // Font name is the first entry in the array.
192             [availableFontList addObject:[fontInfo objectAtIndex:0]];
193         }
194     }
195     
196     availableFonts = availableFontList;
197     return availableFonts;
198 }
199
200 static void swizzleNSFontManagerMethods()
201 {
202     Method availableFontFamiliesMethod = class_getInstanceMethod(objc_getClass("NSFontManager"), @selector(availableFontFamilies));
203     ASSERT(availableFontFamiliesMethod);
204     if (!availableFontFamiliesMethod) {
205         NSLog(@"Failed to swizzle the \"availableFontFamilies\" method on NSFontManager");
206         return;
207     }
208     
209     appKitAvailableFontFamiliesIMP = method_setImplementation(availableFontFamiliesMethod, (IMP)wtr_NSFontManager_availableFontFamilies);
210
211     Method availableFontsMethod = class_getInstanceMethod(objc_getClass("NSFontManager"), @selector(availableFonts));
212     ASSERT(availableFontsMethod);
213     if (!availableFontsMethod) {
214         NSLog(@"Failed to swizzle the \"availableFonts\" method on NSFontManager");
215         return;
216     }
217     
218     appKitAvailableFontsIMP = method_setImplementation(availableFontsMethod, (IMP)wtr_NSFontManager_availableFonts);
219 }
220
221 void activateFonts()
222 {
223     // Work around <rdar://problem/6698023> by activating fonts from disk
224
225     static const char* fontFileNames[] = {
226         "AHEM____.TTF",
227         "WebKitWeightWatcher100.ttf",
228         "WebKitWeightWatcher200.ttf",
229         "WebKitWeightWatcher300.ttf",
230         "WebKitWeightWatcher400.ttf",
231         "WebKitWeightWatcher500.ttf",
232         "WebKitWeightWatcher600.ttf",
233         "WebKitWeightWatcher700.ttf",
234         "WebKitWeightWatcher800.ttf",
235         "WebKitWeightWatcher900.ttf",
236         0
237     };
238
239     NSMutableArray *fontURLs = [NSMutableArray array];
240     NSURL *resourcesDirectory = [[NSBundle bundleForClass:[WKTRFontActivatorDummyClass class]] resourceURL];
241     for (unsigned i = 0; fontFileNames[i]; ++i) {
242         NSURL *fontURL = [resourcesDirectory URLByAppendingPathComponent:[NSString stringWithUTF8String:fontFileNames[i]]];
243         [fontURLs addObject:[fontURL absoluteURL]];
244     }
245
246     CFArrayRef errors = 0;
247     if (!CTFontManagerRegisterFontsForURLs((CFArrayRef)fontURLs, kCTFontManagerScopeProcess, &errors)) {
248         NSLog(@"Failed to activate fonts: %@", errors);
249         CFRelease(errors);
250         exit(1);
251     }
252
253     swizzleNSFontManagerMethods();
254 }
255
256 }
257