2 * Copyright (C) 2011 ProFUSION Embedded Systems
3 * Copyright (C) 2011 Samsung Electronics
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "FontManagement.h"
29 #include <Ecore_File.h>
31 #include <fontconfig/fontconfig.h>
32 #include <wtf/Vector.h>
33 #include <wtf/text/CString.h>
34 #include <wtf/text/WTFString.h>
36 static Vector<String> getFontDirectories()
38 Vector<String> fontDirPaths;
40 fontDirPaths.append(String("/usr/share/fonts/TTF/"));
41 fontDirPaths.append(String("/usr/share/fonts/truetype/ttf-liberation/"));
42 fontDirPaths.append(String("/usr/share/fonts/liberation/"));
43 fontDirPaths.append(String("/usr/share/fonts/truetype/ttf-dejavu/"));
44 fontDirPaths.append(String("/usr/share/fonts/dejavu/"));
45 fontDirPaths.append(String("/usr/share/fonts/OTF/")); // MathML
46 fontDirPaths.append(String("/usr/share/fonts/opentype/stix/")); // MathML
47 fontDirPaths.append(String("/usr/share/fonts/stix/")); // MathML
52 static Vector<String> getFontFiles()
54 Vector<String> fontFilePaths;
56 // Ahem is used by many layout tests.
57 fontFilePaths.append(String(FONTS_CONF_DIR "/AHEM____.TTF"));
58 // A font with no valid Fontconfig encoding to test https://bugs.webkit.org/show_bug.cgi?id=47452
59 fontFilePaths.append(String(FONTS_CONF_DIR "/FontWithNoValidEncoding.fon"));
61 for (int i = 1; i <= 9; i++) {
62 char fontPath[PATH_MAX];
63 snprintf(fontPath, PATH_MAX - 1,
64 FONTS_CONF_DIR "/../../fonts/WebKitWeightWatcher%i00.ttf", i);
66 fontFilePaths.append(String(fontPath));
72 static size_t addFontDirectories(const Vector<String>& fontDirectories, FcConfig* config)
74 size_t addedDirectories = 0;
76 for (Vector<String>::const_iterator it = fontDirectories.begin();
77 it != fontDirectories.end(); ++it) {
78 const CString currentDirectory = (*it).utf8();
79 const char* path = currentDirectory.data();
81 if (ecore_file_is_dir(path)) {
82 if (!FcConfigAppFontAddDir(config, reinterpret_cast<const FcChar8*>(path))) {
83 fprintf(stderr, "Could not load font at %s!\n", path);
91 return addedDirectories;
94 static void addFontFiles(const Vector<String>& fontFiles, FcConfig* config)
96 for (Vector<String>::const_iterator it = fontFiles.begin(); it != fontFiles.end(); ++it) {
97 const CString currentFile = (*it).utf8();
98 const char* path = currentFile.data();
100 if (!FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(path)))
101 fprintf(stderr, "Could not load font at %s!\n", path);
105 void addFontsToEnvironment()
109 // Load our configuration file, which sets up proper aliases for family
110 // names like sans, serif and monospace.
111 FcConfig* config = FcConfigCreate();
112 const char* fontConfigFilename = FONTS_CONF_DIR "/fonts.conf";
113 if (!FcConfigParseAndLoad(config, reinterpret_cast<const FcChar8*>(fontConfigFilename), true)) {
114 fprintf(stderr, "Couldn't load font configuration file from: %s\n", fontConfigFilename);
118 if (!addFontDirectories(getFontDirectories(), config)) {
119 fprintf(stderr, "None of the font directories could be added. Either install them "
120 "or file a bug at http://bugs.webkit.org if they are installed in "
121 "another location.\n");
125 addFontFiles(getFontFiles(), config);
127 if (!FcConfigSetCurrent(config)) {
128 fprintf(stderr, "Could not set the current font configuration!\n");