2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "public/platform/linux/WebFontInfo.h"
34 #include "public/platform/linux/WebFontFamily.h"
35 #include "public/platform/linux/WebFontRenderStyle.h"
36 #include <fontconfig/fontconfig.h>
38 #include <unicode/utf16.h>
42 static bool useSubpixelPositioning = false;
44 void WebFontInfo::setSubpixelPositioning(bool subpixelPositioning)
46 useSubpixelPositioning = subpixelPositioning;
49 void WebFontInfo::familyForChar(WebUChar32 c, const char* preferredLocale, WebFontFamily* family)
51 FcCharSet* cset = FcCharSetCreate();
52 FcCharSetAddChar(cset, c);
53 FcPattern* pattern = FcPatternCreate();
56 fcvalue.type = FcTypeCharSet;
58 FcPatternAdd(pattern, FC_CHARSET, fcvalue, FcFalse);
60 fcvalue.type = FcTypeBool;
62 FcPatternAdd(pattern, FC_SCALABLE, fcvalue, FcFalse);
64 if (preferredLocale) {
65 FcLangSet* langset = FcLangSetCreate();
66 FcLangSetAdd(langset, reinterpret_cast<const FcChar8 *>(preferredLocale));
67 FcPatternAddLangSet(pattern, FC_LANG, langset);
68 FcLangSetDestroy(langset);
71 FcConfigSubstitute(0, pattern, FcMatchPattern);
72 FcDefaultSubstitute(pattern);
75 FcFontSet* fontSet = FcFontSort(0, pattern, 0, 0, &result);
76 FcPatternDestroy(pattern);
77 FcCharSetDestroy(cset);
80 family->name = WebCString();
81 family->isBold = false;
82 family->isItalic = false;
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];
91 if (FcPatternGetBool(current, FC_SCALABLE, 0, &isScalable) != FcResultMatch
95 // fontconfig can also return fonts which are unreadable
97 if (FcPatternGetString(current, FC_FILE, 0, &cFilename) != FcResultMatch)
100 if (access(reinterpret_cast<char*>(cFilename), R_OK))
104 if (FcPatternGetString(current, FC_FAMILY, 0, &familyName) == FcResultMatch) {
105 const char* charFamily = reinterpret_cast<char*>(familyName);
106 family->name = WebCString(charFamily, strlen(charFamily));
109 if (FcPatternGetInteger(current, FC_WEIGHT, 0, &weight) == FcResultMatch)
110 family->isBold = weight >= FC_WEIGHT_BOLD;
112 family->isBold = false;
114 if (FcPatternGetInteger(current, FC_SLANT, 0, &slant) == FcResultMatch)
115 family->isItalic = slant != FC_SLANT_ROMAN;
117 family->isItalic = false;
118 FcFontSetDestroy(fontSet);
122 FcFontSetDestroy(fontSet);
125 void WebFontInfo::renderStyleForStrike(const char* family, int sizeAndStyle, WebFontRenderStyle* out)
127 bool isBold = sizeAndStyle & 1;
128 bool isItalic = sizeAndStyle & 2;
129 int pixelSize = sizeAndStyle >> 2;
131 FcPattern* pattern = FcPatternCreate();
134 fcvalue.type = FcTypeString;
135 fcvalue.u.s = reinterpret_cast<const FcChar8 *>(family);
136 FcPatternAdd(pattern, FC_FAMILY, fcvalue, FcFalse);
138 fcvalue.type = FcTypeInteger;
139 fcvalue.u.i = isBold ? FC_WEIGHT_BOLD : FC_WEIGHT_NORMAL;
140 FcPatternAdd(pattern, FC_WEIGHT, fcvalue, FcFalse);
142 fcvalue.type = FcTypeInteger;
143 fcvalue.u.i = isItalic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN;
144 FcPatternAdd(pattern, FC_SLANT, fcvalue, FcFalse);
146 fcvalue.type = FcTypeBool;
147 fcvalue.u.b = FcTrue;
148 FcPatternAdd(pattern, FC_SCALABLE, fcvalue, FcFalse);
150 fcvalue.type = FcTypeDouble;
151 fcvalue.u.d = pixelSize;
152 FcPatternAdd(pattern, FC_SIZE, fcvalue, FcFalse);
154 FcConfigSubstitute(0, pattern, FcMatchPattern);
155 FcDefaultSubstitute(pattern);
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);
173 if (FcPatternGetBool(match, FC_ANTIALIAS, 0, &b) == FcResultMatch)
174 out->useAntiAlias = b;
175 if (FcPatternGetBool(match, FC_EMBEDDED_BITMAP, 0, &b) == FcResultMatch)
177 if (FcPatternGetBool(match, FC_AUTOHINT, 0, &b) == FcResultMatch)
178 out->useAutoHint = b;
179 if (FcPatternGetBool(match, FC_HINTING, 0, &b) == FcResultMatch)
181 if (FcPatternGetInteger(match, FC_HINT_STYLE, 0, &i) == FcResultMatch)
183 if (FcPatternGetInteger(match, FC_RGBA, 0, &i) == FcResultMatch) {
186 out->useSubpixelRendering = 0;
192 out->useSubpixelRendering = 1;
195 // This includes FC_RGBA_UNKNOWN.
196 out->useSubpixelRendering = 2;
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;
205 FcPatternDestroy(match);