initial import
[vuplus_webkit] / Tools / DumpRenderTree / efl / FontManagement.cpp
1 /*
2  * Copyright (C) 2011 ProFUSION Embedded Systems
3  * Copyright (C) 2011 Samsung Electronics
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  */
25
26 #include "config.h"
27 #include "FontManagement.h"
28
29 #include <Ecore_File.h>
30 #include <cstdio>
31 #include <fontconfig/fontconfig.h>
32 #include <wtf/Vector.h>
33 #include <wtf/text/CString.h>
34 #include <wtf/text/WTFString.h>
35
36 static Vector<String> getFontDirectories()
37 {
38     Vector<String> fontDirPaths;
39
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
48
49     return fontDirPaths;
50 }
51
52 static Vector<String> getFontFiles()
53 {
54     Vector<String> fontFilePaths;
55
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"));
60
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);
65
66         fontFilePaths.append(String(fontPath));
67     }
68
69     return fontFilePaths;
70 }
71
72 static size_t addFontDirectories(const Vector<String>& fontDirectories, FcConfig* config)
73 {
74     size_t addedDirectories = 0;
75
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();
80
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);
84                 continue;
85             }
86
87             ++addedDirectories;
88         }
89     }
90
91     return addedDirectories;
92 }
93
94 static void addFontFiles(const Vector<String>& fontFiles, FcConfig* config)
95 {
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();
99
100         if (!FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(path)))
101             fprintf(stderr, "Could not load font at %s!\n", path);
102     }
103 }
104
105 void addFontsToEnvironment()
106 {
107     FcInit();
108
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);
115         exit(1);
116     }
117
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");
122         exit(1);
123     }
124
125     addFontFiles(getFontFiles(), config);
126
127     if (!FcConfigSetCurrent(config)) {
128         fprintf(stderr, "Could not set the current font configuration!\n");
129         exit(1);
130     }
131 }
132