2
* Copyright (C) 2011 ProFUSION Embedded Systems
3
* Copyright (C) 2011 Samsung Electronics
4
* Copyright (C) 2012 Intel Corporation. All rights reserved.
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
15
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
16
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "FontManagement.h"
30
#include <Ecore_File.h>
32
#include <fontconfig/fontconfig.h>
33
#include <wtf/Vector.h>
34
#include <wtf/text/CString.h>
35
#include <wtf/text/StringBuilder.h>
37
static CString buildPath(const char* base, const char* first, ...)
43
if (const char* current = first) {
47
result.append(current);
48
} while ((current = va_arg(ap, const char*)));
52
return result.toString().utf8();
55
static Vector<CString> getCoreFontFiles()
57
Vector<CString> fontFilePaths;
59
// Ahem is used by many layout tests.
60
fontFilePaths.append(CString(FONTS_CONF_DIR "/AHEM____.TTF"));
61
// A font with no valid Fontconfig encoding to test https://bugs.webkit.org/show_bug.cgi?id=47452
62
fontFilePaths.append(CString(FONTS_CONF_DIR "/FontWithNoValidEncoding.fon"));
64
for (int i = 1; i <= 9; i++) {
65
char fontPath[EINA_PATH_MAX];
66
snprintf(fontPath, EINA_PATH_MAX - 1, FONTS_CONF_DIR "/../../fonts/WebKitWeightWatcher%i00.ttf", i);
67
fontFilePaths.append(CString(fontPath));
73
static void addFontDirectory(const CString& fontDirectory, FcConfig* config)
75
const char* fontPath = fontDirectory.data();
76
if (!fontPath || !FcConfigAppFontAddDir(config, reinterpret_cast<const FcChar8*>(fontPath)))
77
fprintf(stderr, "Could not add font directory %s!\n", fontPath);
80
static void addFontFiles(const Vector<CString>& fontFiles, FcConfig* config)
82
Vector<CString>::const_iterator it, end = fontFiles.end();
83
for (it = fontFiles.begin(); it != end; ++it) {
84
const char* filePath = (*it).data();
85
if (!FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(filePath)))
86
fprintf(stderr, "Could not load font at %s!\n", filePath);
90
static CString getCustomBuildDir()
92
if (const char* userChosenBuildDir = getenv("WEBKITOUTPUTDIR")) {
93
if (ecore_file_is_dir(userChosenBuildDir))
94
return userChosenBuildDir;
95
fprintf(stderr, "WEBKITOUTPUTDIR set to '%s', but path doesn't exist.\n", userChosenBuildDir);
101
static CString getPlatformFontsPath()
103
CString customBuildDir = getCustomBuildDir();
104
if (!customBuildDir.isNull()) {
105
CString fontsPath = buildPath(customBuildDir.data(), "Dependencies", "Root", "webkitgtk-test-fonts", 0);
106
if (!ecore_file_exists(fontsPath.data()))
107
fprintf(stderr, "WEBKITOUTPUTDIR set to '%s', but could not local test fonts.\n", customBuildDir.data());
111
CString fontsPath = CString(DOWNLOADED_FONTS_DIR);
112
if (ecore_file_exists(fontsPath.data()))
115
fprintf(stderr, "Could not locate tests fonts, try setting WEBKITOUTPUTDIR.\n");
119
void addFontsToEnvironment()
123
// Load our configuration file, which sets up proper aliases for family
124
// names like sans, serif and monospace.
125
FcConfig* config = FcConfigCreate();
126
const char* fontConfigFilename = FONTS_CONF_DIR "/fonts.conf";
127
if (!FcConfigParseAndLoad(config, reinterpret_cast<const FcChar8*>(fontConfigFilename), true)) {
128
fprintf(stderr, "Couldn't load font configuration file from: %s\n", fontConfigFilename);
132
addFontFiles(getCoreFontFiles(), config);
133
addFontDirectory(getPlatformFontsPath(), config);
135
if (!FcConfigSetCurrent(config)) {
136
fprintf(stderr, "Could not set the current font configuration!\n");