OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2006, 2007, 2008, 2009, 2010, 2012 Google Inc. All rights reser
ved. | 2 * Copyright (c) 2006, 2007, 2008, 2009, 2010, 2012 Google Inc. All rights reser
ved. |
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 "platform/fonts/win/FontFallbackWin.h" | 32 #include "platform/fonts/win/FontFallbackWin.h" |
33 | 33 |
| 34 #include "SkFontMgr.h" |
34 #include "SkTypeface.h" | 35 #include "SkTypeface.h" |
35 #include "wtf/HashMap.h" | 36 #include "wtf/HashMap.h" |
36 #include "wtf/text/StringHash.h" | 37 #include "wtf/text/StringHash.h" |
37 #include "wtf/text/WTFString.h" | 38 #include "wtf/text/WTFString.h" |
38 #include <limits> | 39 #include <limits> |
39 #include <unicode/locid.h> | 40 #include <unicode/locid.h> |
40 #include <unicode/uchar.h> | 41 #include <unicode/uchar.h> |
41 | 42 |
42 namespace WebCore { | 43 namespace WebCore { |
43 | 44 |
44 namespace { | 45 namespace { |
45 | 46 |
46 static inline bool isFontPresent(const UChar* fontName) | 47 static inline bool isFontPresent(const UChar* fontName, SkFontMgr* fontManager) |
47 { | 48 { |
48 String family = fontName; | 49 String family = fontName; |
49 RefPtr<SkTypeface> tf = adoptRef(SkTypeface::CreateFromName(family.utf8().da
ta(), SkTypeface::kNormal)); | 50 RefPtr<SkTypeface> tf = adoptRef(fontManager->legacyCreateTypeface(family.ut
f8().data(), SkTypeface::kNormal)); |
50 if (!tf) | 51 if (!tf) |
51 return false; | 52 return false; |
52 | 53 |
53 SkTypeface::LocalizedStrings* actualFamilies = tf->createFamilyNameIterator(
); | 54 SkTypeface::LocalizedStrings* actualFamilies = tf->createFamilyNameIterator(
); |
54 bool matchesRequestedFamily = false; | 55 bool matchesRequestedFamily = false; |
55 SkTypeface::LocalizedString actualFamily; | 56 SkTypeface::LocalizedString actualFamily; |
56 while (actualFamilies->next(&actualFamily)) { | 57 while (actualFamilies->next(&actualFamily)) { |
57 if (equalIgnoringCase(family, AtomicString::fromUTF8(actualFamily.fStrin
g.c_str()))) { | 58 if (equalIgnoringCase(family, AtomicString::fromUTF8(actualFamily.fStrin
g.c_str()))) { |
58 matchesRequestedFamily = true; | 59 matchesRequestedFamily = true; |
59 break; | 60 break; |
60 } | 61 } |
61 } | 62 } |
62 actualFamilies->unref(); | 63 actualFamilies->unref(); |
63 | 64 |
64 return matchesRequestedFamily; | 65 return matchesRequestedFamily; |
65 } | 66 } |
66 | 67 |
67 // A simple mapping from UScriptCode to family name. This is a sparse array, | 68 // A simple mapping from UScriptCode to family name. This is a sparse array, |
68 // which works well since the range of UScriptCode values is small. | 69 // which works well since the range of UScriptCode values is small. |
69 typedef const UChar* ScriptToFontMap[USCRIPT_CODE_LIMIT]; | 70 typedef const UChar* ScriptToFontMap[USCRIPT_CODE_LIMIT]; |
70 | 71 |
71 void initializeScriptFontMap(ScriptToFontMap& scriptFontMap) | 72 void initializeScriptFontMap(ScriptToFontMap& scriptFontMap, SkFontMgr* fontMana
ger) |
72 { | 73 { |
73 struct FontMap { | 74 struct FontMap { |
74 UScriptCode script; | 75 UScriptCode script; |
75 const UChar* family; | 76 const UChar* family; |
76 }; | 77 }; |
77 | 78 |
78 static const FontMap fontMap[] = { | 79 static const FontMap fontMap[] = { |
79 {USCRIPT_LATIN, L"times new roman"}, | 80 {USCRIPT_LATIN, L"times new roman"}, |
80 {USCRIPT_GREEK, L"times new roman"}, | 81 {USCRIPT_GREEK, L"times new roman"}, |
81 {USCRIPT_CYRILLIC, L"times new roman"}, | 82 {USCRIPT_CYRILLIC, L"times new roman"}, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 157 |
157 // FIXME: Instead of scanning the hard-coded list, we have to | 158 // FIXME: Instead of scanning the hard-coded list, we have to |
158 // use EnumFont* to 'inspect' fonts to pick up fonts covering scripts | 159 // use EnumFont* to 'inspect' fonts to pick up fonts covering scripts |
159 // when it's possible (e.g. using OS/2 table). If we do that, this | 160 // when it's possible (e.g. using OS/2 table). If we do that, this |
160 // had better be pulled out of here. | 161 // had better be pulled out of here. |
161 for (size_t i = 0; i < WTF_ARRAY_LENGTH(scriptToFontFamilies); ++i) { | 162 for (size_t i = 0; i < WTF_ARRAY_LENGTH(scriptToFontFamilies); ++i) { |
162 UScriptCode script = scriptToFontFamilies[i].script; | 163 UScriptCode script = scriptToFontFamilies[i].script; |
163 scriptFontMap[script] = 0; | 164 scriptFontMap[script] = 0; |
164 const UChar** familyPtr = scriptToFontFamilies[i].families; | 165 const UChar** familyPtr = scriptToFontFamilies[i].families; |
165 while (*familyPtr) { | 166 while (*familyPtr) { |
166 if (isFontPresent(*familyPtr)) { | 167 if (isFontPresent(*familyPtr, fontManager)) { |
167 scriptFontMap[script] = *familyPtr; | 168 scriptFontMap[script] = *familyPtr; |
168 break; | 169 break; |
169 } | 170 } |
170 ++familyPtr; | 171 ++familyPtr; |
171 } | 172 } |
172 } | 173 } |
173 | 174 |
174 // Initialize the locale-dependent mapping. | 175 // Initialize the locale-dependent mapping. |
175 // Since Chrome synchronizes the ICU default locale with its UI locale, | 176 // Since Chrome synchronizes the ICU default locale with its UI locale, |
176 // this ICU locale tells the current UI locale of Chrome. | 177 // this ICU locale tells the current UI locale of Chrome. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 // - If the default font for a script is not available, | 248 // - If the default font for a script is not available, |
248 // try some more fonts known to support it. Finally, we can | 249 // try some more fonts known to support it. Finally, we can |
249 // use EnumFontFamilies or similar APIs to come up with a list of | 250 // use EnumFontFamilies or similar APIs to come up with a list of |
250 // fonts supporting the script and cache the result. | 251 // fonts supporting the script and cache the result. |
251 // - Consider using UnicodeSet (or UnicodeMap) converted from | 252 // - Consider using UnicodeSet (or UnicodeMap) converted from |
252 // GLYPHSET (BMP) or directly read from truetype cmap tables to | 253 // GLYPHSET (BMP) or directly read from truetype cmap tables to |
253 // keep track of which character is supported by which font | 254 // keep track of which character is supported by which font |
254 // - Update script_font_cache in response to WM_FONTCHANGE | 255 // - Update script_font_cache in response to WM_FONTCHANGE |
255 | 256 |
256 const UChar* getFontFamilyForScript(UScriptCode script, | 257 const UChar* getFontFamilyForScript(UScriptCode script, |
257 FontDescription::GenericFamilyType generic) | 258 FontDescription::GenericFamilyType generic, |
| 259 SkFontMgr* fontManager) |
258 { | 260 { |
259 static ScriptToFontMap scriptFontMap; | 261 static ScriptToFontMap scriptFontMap; |
260 static bool initialized = false; | 262 static bool initialized = false; |
261 if (!initialized) { | 263 if (!initialized) { |
262 initializeScriptFontMap(scriptFontMap); | 264 initializeScriptFontMap(scriptFontMap, fontManager); |
263 initialized = true; | 265 initialized = true; |
264 } | 266 } |
265 if (script == USCRIPT_INVALID_CODE) | 267 if (script == USCRIPT_INVALID_CODE) |
266 return 0; | 268 return 0; |
267 ASSERT(script < USCRIPT_CODE_LIMIT); | 269 ASSERT(script < USCRIPT_CODE_LIMIT); |
268 return scriptFontMap[script]; | 270 return scriptFontMap[script]; |
269 } | 271 } |
270 | 272 |
271 // FIXME: | 273 // FIXME: |
272 // - Handle 'Inherited', 'Common' and 'Unknown' | 274 // - Handle 'Inherited', 'Common' and 'Unknown' |
273 // (see http://www.unicode.org/reports/tr24/#Usage_Model ) | 275 // (see http://www.unicode.org/reports/tr24/#Usage_Model ) |
274 // For 'Inherited' and 'Common', perhaps we need to | 276 // For 'Inherited' and 'Common', perhaps we need to |
275 // accept another parameter indicating the previous family | 277 // accept another parameter indicating the previous family |
276 // and just return it. | 278 // and just return it. |
277 // - All the characters (or characters up to the point a single | 279 // - All the characters (or characters up to the point a single |
278 // font can cover) need to be taken into account | 280 // font can cover) need to be taken into account |
279 const UChar* getFallbackFamily(UChar32 character, | 281 const UChar* getFallbackFamily(UChar32 character, |
280 FontDescription::GenericFamilyType generic, | 282 FontDescription::GenericFamilyType generic, |
281 UScriptCode* scriptChecked) | 283 UScriptCode* scriptChecked, |
| 284 SkFontMgr* fontManager) |
282 { | 285 { |
283 ASSERT(character); | 286 ASSERT(character); |
284 UScriptCode script = getScript(character); | 287 UScriptCode script = getScript(character); |
285 | 288 |
286 // For the full-width ASCII characters (U+FF00 - U+FF5E), use the font for | 289 // For the full-width ASCII characters (U+FF00 - U+FF5E), use the font for |
287 // Han (determined in a locale-dependent way above). Full-width ASCII | 290 // Han (determined in a locale-dependent way above). Full-width ASCII |
288 // characters are rather widely used in Japanese and Chinese documents and | 291 // characters are rather widely used in Japanese and Chinese documents and |
289 // they're fully covered by Chinese, Japanese and Korean fonts. | 292 // they're fully covered by Chinese, Japanese and Korean fonts. |
290 if (0xFF00 < character && character < 0xFF5F) | 293 if (0xFF00 < character && character < 0xFF5F) |
291 script = USCRIPT_HAN; | 294 script = USCRIPT_HAN; |
292 | 295 |
293 if (script == USCRIPT_COMMON) | 296 if (script == USCRIPT_COMMON) |
294 script = getScriptBasedOnUnicodeBlock(character); | 297 script = getScriptBasedOnUnicodeBlock(character); |
295 | 298 |
296 const UChar* family = getFontFamilyForScript(script, generic); | 299 const UChar* family = getFontFamilyForScript(script, generic, fontManager); |
297 // Another lame work-around to cover non-BMP characters. | 300 // Another lame work-around to cover non-BMP characters. |
298 // If the font family for script is not found or the character is | 301 // If the font family for script is not found or the character is |
299 // not in BMP (> U+FFFF), we resort to the hard-coded list of | 302 // not in BMP (> U+FFFF), we resort to the hard-coded list of |
300 // fallback fonts for now. | 303 // fallback fonts for now. |
301 if (!family || character > 0xFFFF) { | 304 if (!family || character > 0xFFFF) { |
302 int plane = character >> 16; | 305 int plane = character >> 16; |
303 switch (plane) { | 306 switch (plane) { |
304 case 1: | 307 case 1: |
305 family = L"code2001"; | 308 family = L"code2001"; |
306 break; | 309 break; |
(...skipping 11 matching lines...) Expand all Loading... |
318 default: | 321 default: |
319 family = L"lucida sans unicode"; | 322 family = L"lucida sans unicode"; |
320 } | 323 } |
321 } | 324 } |
322 | 325 |
323 if (scriptChecked) | 326 if (scriptChecked) |
324 *scriptChecked = script; | 327 *scriptChecked = script; |
325 return family; | 328 return family; |
326 } | 329 } |
327 | 330 |
328 | |
329 const UChar* getFallbackFamilyForFirstNonCommonCharacter(const UChar* characters
, | |
330 int length, | |
331 FontDescription::GenericFamilyType generic) | |
332 { | |
333 ASSERT(characters && characters[0] && length > 0); | |
334 UScriptCode script = USCRIPT_COMMON; | |
335 | |
336 // Sometimes characters common to script (e.g. space) is at | |
337 // the beginning of a string so that we need to skip them | |
338 // to get a font required to render the string. | |
339 int i = 0; | |
340 UChar32 ucs4 = 0; | |
341 while (i < length && script == USCRIPT_COMMON) { | |
342 U16_NEXT(characters, i, length, ucs4); | |
343 script = getScript(ucs4); | |
344 } | |
345 | |
346 const UChar* family = getFallbackFamily(ucs4, generic, 0); | |
347 | |
348 return family; | |
349 } | |
350 | |
351 } // namespace WebCore | 331 } // namespace WebCore |
OLD | NEW |