OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "public/platform/linux/WebFontInfo.h" | 32 #include "public/platform/linux/WebFontInfo.h" |
33 | 33 |
| 34 #include "public/platform/linux/WebFallbackFont.h" |
34 #include "public/platform/linux/WebFontFamily.h" | 35 #include "public/platform/linux/WebFontFamily.h" |
35 #include "public/platform/linux/WebFontRenderStyle.h" | 36 #include "public/platform/linux/WebFontRenderStyle.h" |
36 #include <fontconfig/fontconfig.h> | 37 #include <fontconfig/fontconfig.h> |
37 #include <string.h> | 38 #include <string.h> |
38 #include <unicode/utf16.h> | 39 #include <unicode/utf16.h> |
39 | 40 |
40 namespace blink { | 41 namespace blink { |
41 | 42 |
42 static bool useSubpixelPositioning = false; | 43 static bool useSubpixelPositioning = false; |
43 | 44 |
44 void WebFontInfo::setSubpixelPositioning(bool subpixelPositioning) | 45 void WebFontInfo::setSubpixelPositioning(bool subpixelPositioning) |
45 { | 46 { |
46 useSubpixelPositioning = subpixelPositioning; | 47 useSubpixelPositioning = subpixelPositioning; |
47 } | 48 } |
48 | 49 |
| 50 // TODO(dro): Remove this legacy version, kept until renames on the Chromium sid
e are done. crbug.com/382411 |
49 void WebFontInfo::familyForChar(WebUChar32 c, const char* preferredLocale, WebFo
ntFamily* family) | 51 void WebFontInfo::familyForChar(WebUChar32 c, const char* preferredLocale, WebFo
ntFamily* family) |
50 { | 52 { |
51 FcCharSet* cset = FcCharSetCreate(); | 53 FcCharSet* cset = FcCharSetCreate(); |
52 FcCharSetAddChar(cset, c); | 54 FcCharSetAddChar(cset, c); |
53 FcPattern* pattern = FcPatternCreate(); | 55 FcPattern* pattern = FcPatternCreate(); |
54 | 56 |
55 FcValue fcvalue; | 57 FcValue fcvalue; |
56 fcvalue.type = FcTypeCharSet; | 58 fcvalue.type = FcTypeCharSet; |
57 fcvalue.u.c = cset; | 59 fcvalue.u.c = cset; |
58 FcPatternAdd(pattern, FC_CHARSET, fcvalue, FcFalse); | 60 FcPatternAdd(pattern, FC_CHARSET, fcvalue, FcFalse); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 family->isItalic = slant != FC_SLANT_ROMAN; | 117 family->isItalic = slant != FC_SLANT_ROMAN; |
116 else | 118 else |
117 family->isItalic = false; | 119 family->isItalic = false; |
118 FcFontSetDestroy(fontSet); | 120 FcFontSetDestroy(fontSet); |
119 return; | 121 return; |
120 } | 122 } |
121 | 123 |
122 FcFontSetDestroy(fontSet); | 124 FcFontSetDestroy(fontSet); |
123 } | 125 } |
124 | 126 |
| 127 void WebFontInfo::fallbackFontForChar(WebUChar32 c, const char* preferredLocale,
WebFallbackFont* fallbackFont) |
| 128 { |
| 129 FcCharSet* cset = FcCharSetCreate(); |
| 130 FcCharSetAddChar(cset, c); |
| 131 FcPattern* pattern = FcPatternCreate(); |
| 132 |
| 133 FcValue fcvalue; |
| 134 fcvalue.type = FcTypeCharSet; |
| 135 fcvalue.u.c = cset; |
| 136 FcPatternAdd(pattern, FC_CHARSET, fcvalue, FcFalse); |
| 137 |
| 138 fcvalue.type = FcTypeBool; |
| 139 fcvalue.u.b = FcTrue; |
| 140 FcPatternAdd(pattern, FC_SCALABLE, fcvalue, FcFalse); |
| 141 |
| 142 if (preferredLocale) { |
| 143 FcLangSet* langset = FcLangSetCreate(); |
| 144 FcLangSetAdd(langset, reinterpret_cast<const FcChar8 *>(preferredLocale)
); |
| 145 FcPatternAddLangSet(pattern, FC_LANG, langset); |
| 146 FcLangSetDestroy(langset); |
| 147 } |
| 148 |
| 149 FcConfigSubstitute(0, pattern, FcMatchPattern); |
| 150 FcDefaultSubstitute(pattern); |
| 151 |
| 152 FcResult result; |
| 153 FcFontSet* fontSet = FcFontSort(0, pattern, 0, 0, &result); |
| 154 FcPatternDestroy(pattern); |
| 155 FcCharSetDestroy(cset); |
| 156 |
| 157 if (!fontSet) { |
| 158 fallbackFont->name = WebCString(); |
| 159 fallbackFont->isBold = false; |
| 160 fallbackFont->isItalic = false; |
| 161 return; |
| 162 } |
| 163 // Older versions of fontconfig have a bug where they cannot select |
| 164 // only scalable fonts so we have to manually filter the results. |
| 165 for (int i = 0; i < fontSet->nfont; ++i) { |
| 166 FcPattern* current = fontSet->fonts[i]; |
| 167 FcBool isScalable; |
| 168 |
| 169 if (FcPatternGetBool(current, FC_SCALABLE, 0, &isScalable) != FcResultMa
tch |
| 170 || !isScalable) |
| 171 continue; |
| 172 |
| 173 // fontconfig can also return fonts which are unreadable |
| 174 FcChar8* cFilename; |
| 175 if (FcPatternGetString(current, FC_FILE, 0, &cFilename) != FcResultMatch
) |
| 176 continue; |
| 177 |
| 178 if (access(reinterpret_cast<char*>(cFilename), R_OK)) |
| 179 continue; |
| 180 |
| 181 const char* fontFilename = reinterpret_cast<char*>(cFilename); |
| 182 fallbackFont->filename = WebCString(fontFilename, strlen(fontFilename)); |
| 183 |
| 184 // Index into font collection. |
| 185 int ttcIndex; |
| 186 if (FcPatternGetInteger(current, FC_INDEX, 0, &ttcIndex) != FcResultMatc
h && ttcIndex < 0) |
| 187 continue; |
| 188 fallbackFont->ttcIndex = ttcIndex; |
| 189 |
| 190 FcChar8* familyName; |
| 191 if (FcPatternGetString(current, FC_FAMILY, 0, &familyName) == FcResultMa
tch) { |
| 192 const char* charFamily = reinterpret_cast<char*>(familyName); |
| 193 fallbackFont->name = WebCString(charFamily, strlen(charFamily)); |
| 194 } |
| 195 int weight; |
| 196 if (FcPatternGetInteger(current, FC_WEIGHT, 0, &weight) == FcResultMatch
) |
| 197 fallbackFont->isBold = weight >= FC_WEIGHT_BOLD; |
| 198 else |
| 199 fallbackFont->isBold = false; |
| 200 int slant; |
| 201 if (FcPatternGetInteger(current, FC_SLANT, 0, &slant) == FcResultMatch) |
| 202 fallbackFont->isItalic = slant != FC_SLANT_ROMAN; |
| 203 else |
| 204 fallbackFont->isItalic = false; |
| 205 FcFontSetDestroy(fontSet); |
| 206 return; |
| 207 } |
| 208 |
| 209 FcFontSetDestroy(fontSet); |
| 210 } |
| 211 |
125 void WebFontInfo::renderStyleForStrike(const char* family, int sizeAndStyle, Web
FontRenderStyle* out) | 212 void WebFontInfo::renderStyleForStrike(const char* family, int sizeAndStyle, Web
FontRenderStyle* out) |
126 { | 213 { |
127 bool isBold = sizeAndStyle & 1; | 214 bool isBold = sizeAndStyle & 1; |
128 bool isItalic = sizeAndStyle & 2; | 215 bool isItalic = sizeAndStyle & 2; |
129 int pixelSize = sizeAndStyle >> 2; | 216 int pixelSize = sizeAndStyle >> 2; |
130 | 217 |
131 FcPattern* pattern = FcPatternCreate(); | 218 FcPattern* pattern = FcPatternCreate(); |
132 FcValue fcvalue; | 219 FcValue fcvalue; |
133 | 220 |
134 fcvalue.type = FcTypeString; | 221 fcvalue.type = FcTypeString; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 } | 286 } |
200 | 287 |
201 // FontConfig doesn't provide parameters to configure whether subpixel | 288 // FontConfig doesn't provide parameters to configure whether subpixel |
202 // positioning should be used or not, so we just use a global setting. | 289 // positioning should be used or not, so we just use a global setting. |
203 out->useSubpixelPositioning = useSubpixelPositioning; | 290 out->useSubpixelPositioning = useSubpixelPositioning; |
204 | 291 |
205 FcPatternDestroy(match); | 292 FcPatternDestroy(match); |
206 } | 293 } |
207 | 294 |
208 } // namespace blink | 295 } // namespace blink |
OLD | NEW |