~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Tools/DumpRenderTree/efl/FontManagement.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 ProFUSION Embedded Systems
 
3
 * Copyright (C) 2011 Samsung Electronics
 
4
 * Copyright (C) 2012 Intel Corporation. All rights reserved.
 
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
 * 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.
 
14
 *
 
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.
 
25
 */
 
26
 
 
27
#include "config.h"
 
28
#include "FontManagement.h"
 
29
 
 
30
#include <Ecore_File.h>
 
31
#include <cstdio>
 
32
#include <fontconfig/fontconfig.h>
 
33
#include <wtf/Vector.h>
 
34
#include <wtf/text/CString.h>
 
35
#include <wtf/text/StringBuilder.h>
 
36
 
 
37
static CString buildPath(const char* base, const char* first, ...)
 
38
{
 
39
    va_list ap;
 
40
    StringBuilder result;
 
41
    result.append(base);
 
42
 
 
43
    if (const char* current = first) {
 
44
        va_start(ap, first);
 
45
        do {
 
46
            result.append('/');
 
47
            result.append(current);
 
48
        } while ((current = va_arg(ap, const char*)));
 
49
        va_end(ap);
 
50
    }
 
51
 
 
52
    return result.toString().utf8();
 
53
}
 
54
 
 
55
static Vector<CString> getCoreFontFiles()
 
56
{
 
57
    Vector<CString> fontFilePaths;
 
58
 
 
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"));
 
63
 
 
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));
 
68
    }
 
69
 
 
70
    return fontFilePaths;
 
71
}
 
72
 
 
73
static void addFontDirectory(const CString& fontDirectory, FcConfig* config)
 
74
{
 
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);
 
78
}
 
79
 
 
80
static void addFontFiles(const Vector<CString>& fontFiles, FcConfig* config)
 
81
{
 
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);
 
87
    }
 
88
}
 
89
 
 
90
static CString getCustomBuildDir()
 
91
{
 
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);
 
96
    }
 
97
 
 
98
    return CString();
 
99
}
 
100
 
 
101
static CString getPlatformFontsPath()
 
102
{
 
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());
 
108
        return fontsPath;
 
109
    }
 
110
 
 
111
    CString fontsPath = CString(DOWNLOADED_FONTS_DIR);
 
112
    if (ecore_file_exists(fontsPath.data()))
 
113
        return fontsPath;
 
114
 
 
115
    fprintf(stderr, "Could not locate tests fonts, try setting WEBKITOUTPUTDIR.\n");
 
116
    return CString();
 
117
}
 
118
 
 
119
void addFontsToEnvironment()
 
120
{
 
121
    FcInit();
 
122
 
 
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);
 
129
        exit(1);
 
130
    }
 
131
 
 
132
    addFontFiles(getCoreFontFiles(), config);
 
133
    addFontDirectory(getPlatformFontsPath(), config);
 
134
 
 
135
    if (!FcConfigSetCurrent(config)) {
 
136
        fprintf(stderr, "Could not set the current font configuration!\n");
 
137
        exit(1);
 
138
    }
 
139
}
 
140