Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / exported / linux / WebFontInfo.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "public/platform/linux/WebFontInfo.h"
33
34 #include "public/platform/linux/WebFontFamily.h"
35 #include "public/platform/linux/WebFontRenderStyle.h"
36 #include <fontconfig/fontconfig.h>
37 #include <string.h>
38 #include <unicode/utf16.h>
39
40 namespace blink {
41
42 static bool useSubpixelPositioning = false;
43
44 void WebFontInfo::setSubpixelPositioning(bool subpixelPositioning)
45 {
46     useSubpixelPositioning = subpixelPositioning;
47 }
48
49 void WebFontInfo::familyForChar(WebUChar32 c, const char* preferredLocale, WebFontFamily* family)
50 {
51     FcCharSet* cset = FcCharSetCreate();
52     FcCharSetAddChar(cset, c);
53     FcPattern* pattern = FcPatternCreate();
54
55     FcValue fcvalue;
56     fcvalue.type = FcTypeCharSet;
57     fcvalue.u.c = cset;
58     FcPatternAdd(pattern, FC_CHARSET, fcvalue, FcFalse);
59
60     fcvalue.type = FcTypeBool;
61     fcvalue.u.b = FcTrue;
62     FcPatternAdd(pattern, FC_SCALABLE, fcvalue, FcFalse);
63
64     if (preferredLocale) {
65         FcLangSet* langset = FcLangSetCreate();
66         FcLangSetAdd(langset, reinterpret_cast<const FcChar8 *>(preferredLocale));
67         FcPatternAddLangSet(pattern, FC_LANG, langset);
68         FcLangSetDestroy(langset);
69     }
70
71     FcConfigSubstitute(0, pattern, FcMatchPattern);
72     FcDefaultSubstitute(pattern);
73
74     FcResult result;
75     FcFontSet* fontSet = FcFontSort(0, pattern, 0, 0, &result);
76     FcPatternDestroy(pattern);
77     FcCharSetDestroy(cset);
78
79     if (!fontSet) {
80         family->name = WebCString();
81         family->isBold = false;
82         family->isItalic = false;
83         return;
84     }
85     // Older versions of fontconfig have a bug where they cannot select
86     // only scalable fonts so we have to manually filter the results.
87     for (int i = 0; i < fontSet->nfont; ++i) {
88         FcPattern* current = fontSet->fonts[i];
89         FcBool isScalable;
90
91         if (FcPatternGetBool(current, FC_SCALABLE, 0, &isScalable) != FcResultMatch
92             || !isScalable)
93             continue;
94
95         // fontconfig can also return fonts which are unreadable
96         FcChar8* cFilename;
97         if (FcPatternGetString(current, FC_FILE, 0, &cFilename) != FcResultMatch)
98             continue;
99
100         if (access(reinterpret_cast<char*>(cFilename), R_OK))
101             continue;
102
103         FcChar8* familyName;
104         if (FcPatternGetString(current, FC_FAMILY, 0, &familyName) == FcResultMatch) {
105             const char* charFamily = reinterpret_cast<char*>(familyName);
106             family->name = WebCString(charFamily, strlen(charFamily));
107         }
108         int weight;
109         if (FcPatternGetInteger(current, FC_WEIGHT, 0, &weight) == FcResultMatch)
110             family->isBold = weight >= FC_WEIGHT_BOLD;
111         else
112             family->isBold = false;
113         int slant;
114         if (FcPatternGetInteger(current, FC_SLANT, 0, &slant) == FcResultMatch)
115             family->isItalic = slant != FC_SLANT_ROMAN;
116         else
117             family->isItalic = false;
118         FcFontSetDestroy(fontSet);
119         return;
120     }
121
122     FcFontSetDestroy(fontSet);
123 }
124
125 void WebFontInfo::renderStyleForStrike(const char* family, int sizeAndStyle, WebFontRenderStyle* out)
126 {
127     bool isBold = sizeAndStyle & 1;
128     bool isItalic = sizeAndStyle & 2;
129     int pixelSize = sizeAndStyle >> 2;
130
131     FcPattern* pattern = FcPatternCreate();
132     FcValue fcvalue;
133
134     fcvalue.type = FcTypeString;
135     fcvalue.u.s = reinterpret_cast<const FcChar8 *>(family);
136     FcPatternAdd(pattern, FC_FAMILY, fcvalue, FcFalse);
137
138     fcvalue.type = FcTypeInteger;
139     fcvalue.u.i = isBold ? FC_WEIGHT_BOLD : FC_WEIGHT_NORMAL;
140     FcPatternAdd(pattern, FC_WEIGHT, fcvalue, FcFalse);
141
142     fcvalue.type = FcTypeInteger;
143     fcvalue.u.i = isItalic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN;
144     FcPatternAdd(pattern, FC_SLANT, fcvalue, FcFalse);
145
146     fcvalue.type = FcTypeBool;
147     fcvalue.u.b = FcTrue;
148     FcPatternAdd(pattern, FC_SCALABLE, fcvalue, FcFalse);
149
150     fcvalue.type = FcTypeDouble;
151     fcvalue.u.d = pixelSize;
152     FcPatternAdd(pattern, FC_SIZE, fcvalue, FcFalse);
153
154     FcConfigSubstitute(0, pattern, FcMatchPattern);
155     FcDefaultSubstitute(pattern);
156
157     FcResult result;
158     // Some versions of fontconfig don't actually write a value into result.
159     // However, it's not clear from the documentation if result should be a
160     // non-0 pointer: future versions might expect to be able to write to
161     // it. So we pass in a valid pointer and ignore it.
162     FcPattern* match = FcFontMatch(0, pattern, &result);
163     FcPatternDestroy(pattern);
164
165     out->setDefaults();
166
167     if (!match)
168         return;
169
170     FcBool b;
171     int i;
172
173     if (FcPatternGetBool(match, FC_ANTIALIAS, 0, &b) == FcResultMatch)
174         out->useAntiAlias = b;
175     if (FcPatternGetBool(match, FC_EMBEDDED_BITMAP, 0, &b) == FcResultMatch)
176         out->useBitmaps = b;
177     if (FcPatternGetBool(match, FC_AUTOHINT, 0, &b) == FcResultMatch)
178         out->useAutoHint = b;
179     if (FcPatternGetBool(match, FC_HINTING, 0, &b) == FcResultMatch)
180         out->useHinting = b;
181     if (FcPatternGetInteger(match, FC_HINT_STYLE, 0, &i) == FcResultMatch)
182         out->hintStyle = i;
183     if (FcPatternGetInteger(match, FC_RGBA, 0, &i) == FcResultMatch) {
184         switch (i) {
185         case FC_RGBA_NONE:
186             out->useSubpixelRendering = 0;
187             break;
188         case FC_RGBA_RGB:
189         case FC_RGBA_BGR:
190         case FC_RGBA_VRGB:
191         case FC_RGBA_VBGR:
192             out->useSubpixelRendering = 1;
193             break;
194         default:
195             // This includes FC_RGBA_UNKNOWN.
196             out->useSubpixelRendering = 2;
197             break;
198         }
199     }
200
201     // FontConfig doesn't provide parameters to configure whether subpixel
202     // positioning should be used or not, so we just use a global setting.
203     out->useSubpixelPositioning = useSubpixelPositioning;
204
205     FcPatternDestroy(match);
206 }
207
208 } // namespace blink