1/*
2 * Copyright (C) 2005, 2006 Apple Computer, 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 Computer, 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 <fontconfig/fontconfig.h>
35#include <gtk/gtk.h>
36#include <wtf/gobject/GOwnPtr.h>
37
38namespace WTR {
39
40void initializeGtkSettings()
41{
42
43 GtkSettings* settings = gtk_settings_get_default();
44 if (!settings)
45 return;
46 g_object_set(settings,
47 "gtk-xft-dpi", 98304,
48 "gtk-xft-antialias", 1,
49 "gtk-xft-hinting", 0,
50 "gtk-font-name", "Liberation Sans 12",
51 "gtk-theme-name", "Raleigh",
52 "gtk-xft-rgba", "none", NULL);
53}
54
55void inititializeFontConfigSetting()
56{
57 FcInit();
58
59 // If a test resulted a font being added or removed via the @font-face rule, then
60 // we want to reset the FontConfig configuration to prevent it from affecting other tests.
61 static int numFonts = 0;
62 FcFontSet* appFontSet = FcConfigGetFonts(0, FcSetApplication);
63 if (appFontSet && numFonts && appFontSet->nfont == numFonts)
64 return;
65
66 // Load our configuration file, which sets up proper aliases for family
67 // names like sans, serif and monospace.
68 FcConfig* config = FcConfigCreate();
69 GOwnPtr<gchar> fontConfigFilename(g_build_filename(FONTS_CONF_DIR, "fonts.conf", NULL));
70 if (!g_file_test(fontConfigFilename.get(), G_FILE_TEST_IS_REGULAR))
71 g_error("Cannot find fonts.conf at %s\n", fontConfigFilename.get());
72 if (!FcConfigParseAndLoad(config, reinterpret_cast<FcChar8*>(fontConfigFilename.get()), true))
73 g_error("Couldn't load font configuration file from: %s", fontConfigFilename.get());
74
75 static const char *const fontPaths[][2] = {
76 { "/usr/share/fonts/truetype/ttf-liberation/LiberationMono-BoldItalic.ttf",
77 "/usr/share/fonts/liberation/LiberationMono-BoldItalic.ttf", },
78 { "/usr/share/fonts/truetype/ttf-liberation/LiberationMono-Bold.ttf",
79 "/usr/share/fonts/liberation/LiberationMono-Bold.ttf", },
80 { "/usr/share/fonts/truetype/ttf-liberation/LiberationMono-Italic.ttf",
81 "/usr/share/fonts/liberation/LiberationMono-Italic.ttf", },
82 { "/usr/share/fonts/truetype/ttf-liberation/LiberationMono-Regular.ttf",
83 "/usr/share/fonts/liberation/LiberationMono-Regular.ttf", },
84 { "/usr/share/fonts/truetype/ttf-liberation/LiberationSans-BoldItalic.ttf",
85 "/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf", },
86 { "/usr/share/fonts/truetype/ttf-liberation/LiberationSans-Bold.ttf",
87 "/usr/share/fonts/liberation/LiberationSans-Bold.ttf", },
88 { "/usr/share/fonts/truetype/ttf-liberation/LiberationSans-Italic.ttf",
89 "/usr/share/fonts/liberation/LiberationSans-Italic.ttf", },
90 { "/usr/share/fonts/truetype/ttf-liberation/LiberationSans-Regular.ttf",
91 "/usr/share/fonts/liberation/LiberationSans-Regular.ttf", },
92 { "/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-BoldItalic.ttf",
93 "/usr/share/fonts/liberation/LiberationSerif-BoldItalic.ttf", },
94 { "/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Bold.ttf",
95 "/usr/share/fonts/liberation/LiberationSerif-Bold.ttf", },
96 { "/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Italic.ttf",
97 "/usr/share/fonts/liberation/LiberationSerif-Italic.ttf", },
98 { "/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Regular.ttf",
99 "/usr/share/fonts/liberation/LiberationSerif-Regular.ttf", },
100 { "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",
101 "/usr/share/fonts/dejavu/DejaVuSans.ttf", },
102 { "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf",
103 "/usr/share/fonts/dejavu/DejaVuSerif.ttf", },
104
105 // MathML tests require the STIX fonts.
106 { "/usr/share/fonts/opentype/stix/STIXGeneral.otf",
107 "/usr/share/fonts/stix/STIXGeneral.otf" },
108 { "/usr/share/fonts/opentype/stix/STIXGeneralBolIta.otf",
109 "/usr/share/fonts/stix/STIXGeneralBolIta.otf" },
110 { "/usr/share/fonts/opentype/stix/STIXGeneralBol.otf",
111 "/usr/share/fonts/stix/STIXGeneralBol.otf" },
112 { "/usr/share/fonts/opentype/stix/STIXGeneralItalic.otf",
113 "/usr/share/fonts/stix/STIXGeneralItalic.otf" }
114 };
115
116 // TODO: Some tests use Lucida. We should load these as well, once it becomes
117 // clear how to install these fonts easily on Fedora.
118 for (size_t font = 0; font < G_N_ELEMENTS(fontPaths); font++) {
119 bool found = false;
120 for (size_t path = 0; path < 2; path++) {
121
122 if (g_file_test(fontPaths[font][path], G_FILE_TEST_EXISTS)) {
123 found = true;
124 if (!FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(fontPaths[font][path])))
125 g_error("Could not load font at %s!", fontPaths[font][path]);
126 else
127 break;
128 }
129 }
130
131 if (!found)
132 g_error("Could not find font at %s. Either install this font or file a bug "
133 "at http://bugs.webkit.org if it is installed in another location.",
134 fontPaths[font][0]);
135 }
136
137 // Ahem is used by many layout tests.
138 GOwnPtr<gchar> ahemFontFilename(g_build_filename(FONTS_CONF_DIR, "AHEM____.TTF", NULL));
139 if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(ahemFontFilename.get())))
140 g_error("Could not load font at %s!", ahemFontFilename.get());
141
142 static const char* fontFilenames[] = {
143 "ColorBits.ttf",
144 "WebKitWeightWatcher100.ttf",
145 "WebKitWeightWatcher200.ttf",
146 "WebKitWeightWatcher300.ttf",
147 "WebKitWeightWatcher400.ttf",
148 "WebKitWeightWatcher500.ttf",
149 "WebKitWeightWatcher600.ttf",
150 "WebKitWeightWatcher700.ttf",
151 "WebKitWeightWatcher800.ttf",
152 "WebKitWeightWatcher900.ttf",
153 0
154 };
155
156 for (size_t i = 0; fontFilenames[i]; ++i) {
157 GOwnPtr<gchar> fontFilename(g_build_filename(FONTS_CONF_DIR, "..", "..", "fonts", fontFilenames[i], NULL));
158 if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(fontFilename.get())))
159 g_error("Could not load font at %s!", fontFilename.get());
160 }
161
162 // A font with no valid Fontconfig encoding to test https://bugs.webkit.org/show_bug.cgi?id=47452
163 GOwnPtr<gchar> fontWithNoValidEncodingFilename(g_build_filename(FONTS_CONF_DIR, "FontWithNoValidEncoding.fon", NULL));
164 if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(fontWithNoValidEncodingFilename.get())))
165 g_error("Could not load font at %s!", fontWithNoValidEncodingFilename.get());
166
167 if (!FcConfigSetCurrent(config))
168 g_error("Could not set the current font configuration!");
169
170 numFonts = FcConfigGetFonts(config, FcSetApplication)->nfont;
171}
172
173void activateFonts()
174{
175 initializeGtkSettings();
176 inititializeFontConfigSetting();
177}
178
179}