1 | /* |
2 | * Copyright (C) 2005, 2006 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
4 | * Copyright (C) 2010 Igalia S.L. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
16 | * its contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
20 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
23 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | #include "config.h" |
32 | #include "ActivateFonts.h" |
33 | |
34 | #include "InjectedBundleUtilities.h" |
35 | #include <fontconfig/fontconfig.h> |
36 | #include <gtk/gtk.h> |
37 | #include <wtf/glib/GLibUtilities.h> |
38 | #include <wtf/glib/GUniquePtr.h> |
39 | |
40 | namespace WTR { |
41 | |
42 | void initializeGtkSettings() |
43 | { |
44 | GtkSettings* settings = gtk_settings_get_default(); |
45 | if (!settings) |
46 | return; |
47 | g_object_set(settings, |
48 | "gtk-xft-dpi" , 98304, |
49 | "gtk-xft-antialias" , 1, |
50 | "gtk-xft-hinting" , 0, |
51 | "gtk-font-name" , "Liberation Sans 12" , |
52 | "gtk-xft-rgba" , "none" , nullptr); |
53 | } |
54 | |
55 | CString getOutputDir() |
56 | { |
57 | const char* webkitOutputDir = g_getenv("WEBKIT_OUTPUTDIR" ); |
58 | if (webkitOutputDir) |
59 | return webkitOutputDir; |
60 | |
61 | CString topLevelPath = WTR::topLevelPath(); |
62 | GUniquePtr<char> outputDir(g_build_filename(topLevelPath.data(), "WebKitBuild" , nullptr)); |
63 | return outputDir.get(); |
64 | } |
65 | |
66 | static CString getFontsPath() |
67 | { |
68 | CString webkitOutputDir = getOutputDir(); |
69 | GUniquePtr<char> fontsPath(g_build_filename(webkitOutputDir.data(), "DependenciesGTK" , "Root" , "webkitgtk-test-fonts" , nullptr)); |
70 | if (g_file_test(fontsPath.get(), static_cast<GFileTest>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) |
71 | return fontsPath.get(); |
72 | |
73 | // Try alternative fonts path. |
74 | fontsPath.reset(g_build_filename(webkitOutputDir.data(), "webkitgtk-test-fonts" , NULL)); |
75 | if (g_file_test(fontsPath.get(), static_cast<GFileTest>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) |
76 | return fontsPath.get(); |
77 | |
78 | return CString(); |
79 | } |
80 | |
81 | void initializeFontConfigSetting() |
82 | { |
83 | if (g_getenv("WEBKIT_SKIP_WEBKITTESTRUNNER_FONTCONFIG_INITIALIZATION" )) |
84 | return; |
85 | |
86 | FcInit(); |
87 | |
88 | // If a test resulted a font being added or removed via the @font-face rule, then |
89 | // we want to reset the FontConfig configuration to prevent it from affecting other tests. |
90 | static int numFonts = 0; |
91 | FcFontSet* appFontSet = FcConfigGetFonts(0, FcSetApplication); |
92 | if (appFontSet && numFonts && appFontSet->nfont == numFonts) |
93 | return; |
94 | |
95 | // Load our configuration file, which sets up proper aliases for family |
96 | // names like sans, serif and monospace. |
97 | FcConfig* config = FcConfigCreate(); |
98 | GUniquePtr<gchar> fontConfigFilename(g_build_filename(FONTS_CONF_DIR, "fonts.conf" , nullptr)); |
99 | if (!g_file_test(fontConfigFilename.get(), G_FILE_TEST_IS_REGULAR)) |
100 | g_error("Cannot find fonts.conf at %s\n" , fontConfigFilename.get()); |
101 | if (!FcConfigParseAndLoad(config, reinterpret_cast<FcChar8*>(fontConfigFilename.get()), true)) |
102 | g_error("Couldn't load font configuration file from: %s" , fontConfigFilename.get()); |
103 | |
104 | CString fontsPath = getFontsPath(); |
105 | if (fontsPath.isNull()) |
106 | g_error("Could not locate test fonts at %s. Is WEBKIT_TOP_LEVEL set?" , fontsPath.data()); |
107 | |
108 | GUniquePtr<GDir> fontsDirectory(g_dir_open(fontsPath.data(), 0, nullptr)); |
109 | while (const char* directoryEntry = g_dir_read_name(fontsDirectory.get())) { |
110 | if (!g_str_has_suffix(directoryEntry, ".ttf" ) && !g_str_has_suffix(directoryEntry, ".otf" )) |
111 | continue; |
112 | GUniquePtr<gchar> fontPath(g_build_filename(fontsPath.data(), directoryEntry, nullptr)); |
113 | if (!FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(fontPath.get()))) |
114 | g_error("Could not load font at %s!" , fontPath.get()); |
115 | } |
116 | |
117 | // Ahem is used by many layout tests. |
118 | GUniquePtr<gchar> ahemFontFilename(g_build_filename(FONTS_CONF_DIR, "AHEM____.TTF" , nullptr)); |
119 | if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(ahemFontFilename.get()))) |
120 | g_error("Could not load font at %s!" , ahemFontFilename.get()); |
121 | |
122 | static const char* fontFilenames[] = { |
123 | "WebKitWeightWatcher100.ttf" , |
124 | "WebKitWeightWatcher200.ttf" , |
125 | "WebKitWeightWatcher300.ttf" , |
126 | "WebKitWeightWatcher400.ttf" , |
127 | "WebKitWeightWatcher500.ttf" , |
128 | "WebKitWeightWatcher600.ttf" , |
129 | "WebKitWeightWatcher700.ttf" , |
130 | "WebKitWeightWatcher800.ttf" , |
131 | "WebKitWeightWatcher900.ttf" , |
132 | 0 |
133 | }; |
134 | |
135 | for (size_t i = 0; fontFilenames[i]; ++i) { |
136 | GUniquePtr<gchar> fontFilename(g_build_filename(FONTS_CONF_DIR, ".." , ".." , "fonts" , fontFilenames[i], nullptr)); |
137 | if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(fontFilename.get()))) |
138 | g_error("Could not load font at %s!" , fontFilename.get()); |
139 | } |
140 | |
141 | // A font with no valid Fontconfig encoding to test https://bugs.webkit.org/show_bug.cgi?id=47452 |
142 | GUniquePtr<gchar> fontWithNoValidEncodingFilename(g_build_filename(FONTS_CONF_DIR, "FontWithNoValidEncoding.fon" , nullptr)); |
143 | if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(fontWithNoValidEncodingFilename.get()))) |
144 | g_error("Could not load font at %s!" , fontWithNoValidEncodingFilename.get()); |
145 | |
146 | if (!FcConfigSetCurrent(config)) |
147 | g_error("Could not set the current font configuration!" ); |
148 | |
149 | numFonts = FcConfigGetFonts(config, FcSetApplication)->nfont; |
150 | } |
151 | |
152 | void activateFonts() |
153 | { |
154 | initializeGtkSettings(); |
155 | initializeFontConfigSetting(); |
156 | } |
157 | |
158 | void installFakeHelvetica(WKStringRef) |
159 | { |
160 | } |
161 | |
162 | void uninstallFakeHelvetica() |
163 | { |
164 | } |
165 | |
166 | } |
167 | |