| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2006, 2007 Apple Computer, Inc. | |
| 3 * Copyright (c) 2006, 2007, 2008, 2009, 2012 Google Inc. All rights reserved. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are | |
| 7 * met: | |
| 8 * | |
| 9 * * Redistributions of source code must retain the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer. | |
| 11 * * Redistributions in binary form must reproduce the above | |
| 12 * copyright notice, this list of conditions and the following disclaimer | |
| 13 * in the documentation and/or other materials provided with the | |
| 14 * distribution. | |
| 15 * * Neither the name of Google Inc. nor the names of its | |
| 16 * contributors may be used to endorse or promote products derived from | |
| 17 * this software without specific prior written permission. | |
| 18 * | |
| 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 30 */ | |
| 31 | |
| 32 #include "config.h" | |
| 33 #include "core/platform/graphics/FontCache.h" | |
| 34 | |
| 35 #include "RuntimeEnabledFeatures.h" | |
| 36 #include "SkFontMgr.h" | |
| 37 #include "SkTypeface_win.h" | |
| 38 #include "core/platform/graphics/SimpleFontData.h" | |
| 39 #include "core/platform/graphics/win/FontPlatformDataWin.h" | |
| 40 #include "platform/fonts/FontDescription.h" | |
| 41 #include "platform/fonts/FontFallbackWin.h" | |
| 42 | |
| 43 namespace WebCore { | |
| 44 | |
| 45 FontCache::FontCache() | |
| 46 : m_purgePreventCount(0) | |
| 47 { | |
| 48 SkFontMgr* fontManager = 0; | |
| 49 | |
| 50 // Prefer DirectWrite (if runtime feature is enabled) but fallback | |
| 51 // to GDI on platforms where DirectWrite is not supported. | |
| 52 if (RuntimeEnabledFeatures::directWriteEnabled()) | |
| 53 fontManager = SkFontMgr_New_DirectWrite(); | |
| 54 | |
| 55 // Subpixel text positioning is not supported by the GDI backend. | |
| 56 m_useSubpixelPositioning = fontManager | |
| 57 ? RuntimeEnabledFeatures::subpixelFontScalingEnabled() | |
| 58 : false; | |
| 59 | |
| 60 if (!fontManager) | |
| 61 fontManager = SkFontMgr_New_GDI(); | |
| 62 | |
| 63 m_fontManager = adoptPtr(fontManager); | |
| 64 } | |
| 65 | |
| 66 static bool fontContainsCharacter(const FontPlatformData* fontData, const wchar_
t* family, UChar32 character) | |
| 67 { | |
| 68 SkPaint paint; | |
| 69 fontData->setupPaint(&paint); | |
| 70 paint.setTextEncoding(SkPaint::kUTF32_TextEncoding); | |
| 71 | |
| 72 uint16_t glyph; | |
| 73 paint.textToGlyphs(&character, sizeof(character), &glyph); | |
| 74 return glyph; | |
| 75 } | |
| 76 | |
| 77 // Given the desired base font, this will create a SimpleFontData for a specific | |
| 78 // font that can be used to render the given range of characters. | |
| 79 PassRefPtr<SimpleFontData> FontCache::platformFallbackForCharacter(const FontDes
cription& fontDescription, UChar32 character, const SimpleFontData*, bool) | |
| 80 { | |
| 81 // FIXME: Consider passing fontDescription.dominantScript() | |
| 82 // to GetFallbackFamily here. | |
| 83 UScriptCode script; | |
| 84 const wchar_t* family = getFallbackFamily(character, | |
| 85 fontDescription.genericFamily(), | |
| 86 &script); | |
| 87 FontPlatformData* data = 0; | |
| 88 if (family) | |
| 89 data = getFontPlatformData(fontDescription, AtomicString(family, wcslen
(family))); | |
| 90 | |
| 91 // Last resort font list : PanUnicode. CJK fonts have a pretty | |
| 92 // large repertoire. Eventually, we need to scan all the fonts | |
| 93 // on the system to have a Firefox-like coverage. | |
| 94 // Make sure that all of them are lowercased. | |
| 95 const static wchar_t* const cjkFonts[] = { | |
| 96 L"arial unicode ms", | |
| 97 L"ms pgothic", | |
| 98 L"simsun", | |
| 99 L"gulim", | |
| 100 L"pmingliu", | |
| 101 L"wenquanyi zen hei", // Partial CJK Ext. A coverage but more widely kno
wn to Chinese users. | |
| 102 L"ar pl shanheisun uni", | |
| 103 L"ar pl zenkai uni", | |
| 104 L"han nom a", // Complete CJK Ext. A coverage. | |
| 105 L"code2000" // Complete CJK Ext. A coverage. | |
| 106 // CJK Ext. B fonts are not listed here because it's of no use | |
| 107 // with our current non-BMP character handling because we use | |
| 108 // Uniscribe for it and that code path does not go through here. | |
| 109 }; | |
| 110 | |
| 111 const static wchar_t* const commonFonts[] = { | |
| 112 L"tahoma", | |
| 113 L"arial unicode ms", | |
| 114 L"lucida sans unicode", | |
| 115 L"microsoft sans serif", | |
| 116 L"palatino linotype", | |
| 117 // Six fonts below (and code2000 at the end) are not from MS, but | |
| 118 // once installed, cover a very wide range of characters. | |
| 119 L"dejavu serif", | |
| 120 L"dejavu sasns", | |
| 121 L"freeserif", | |
| 122 L"freesans", | |
| 123 L"gentium", | |
| 124 L"gentiumalt", | |
| 125 L"ms pgothic", | |
| 126 L"simsun", | |
| 127 L"gulim", | |
| 128 L"pmingliu", | |
| 129 L"code2000" | |
| 130 }; | |
| 131 | |
| 132 const wchar_t* const* panUniFonts = 0; | |
| 133 int numFonts = 0; | |
| 134 if (script == USCRIPT_HAN) { | |
| 135 panUniFonts = cjkFonts; | |
| 136 numFonts = WTF_ARRAY_LENGTH(cjkFonts); | |
| 137 } else { | |
| 138 panUniFonts = commonFonts; | |
| 139 numFonts = WTF_ARRAY_LENGTH(commonFonts); | |
| 140 } | |
| 141 // Font returned from getFallbackFamily may not cover |character| | |
| 142 // because it's based on script to font mapping. This problem is | |
| 143 // critical enough for non-Latin scripts (especially Han) to | |
| 144 // warrant an additional (real coverage) check with fontCotainsCharacter. | |
| 145 int i; | |
| 146 for (i = 0; (!data || !fontContainsCharacter(data, family, character)) && i
< numFonts; ++i) { | |
| 147 family = panUniFonts[i]; | |
| 148 data = getFontPlatformData(fontDescription, AtomicString(family, wcslen(
family))); | |
| 149 } | |
| 150 // When i-th font (0-base) in |panUniFonts| contains a character and | |
| 151 // we get out of the loop, |i| will be |i + 1|. That is, if only the | |
| 152 // last font in the array covers the character, |i| will be numFonts. | |
| 153 // So, we have to use '<=" rather than '<' to see if we found a font | |
| 154 // covering the character. | |
| 155 if (i <= numFonts) | |
| 156 return fontDataFromFontPlatformData(data, DoNotRetain); | |
| 157 | |
| 158 return 0; | |
| 159 } | |
| 160 | |
| 161 static inline bool equalIgnoringCase(const AtomicString& a, const SkString& b) | |
| 162 { | |
| 163 return equalIgnoringCase(a, AtomicString::fromUTF8(b.c_str())); | |
| 164 } | |
| 165 | |
| 166 static bool typefacesMatchesFamily(const SkTypeface* tf, const AtomicString& fam
ily) | |
| 167 { | |
| 168 SkTypeface::LocalizedStrings* actualFamilies = tf->createFamilyNameIterator(
); | |
| 169 bool matchesRequestedFamily = false; | |
| 170 SkTypeface::LocalizedString actualFamily; | |
| 171 | |
| 172 while (actualFamilies->next(&actualFamily)) { | |
| 173 if (equalIgnoringCase(family, actualFamily.fString)) { | |
| 174 matchesRequestedFamily = true; | |
| 175 break; | |
| 176 } | |
| 177 } | |
| 178 actualFamilies->unref(); | |
| 179 | |
| 180 // getFamilyName may return a name not returned by the createFamilyNameItera
tor. | |
| 181 // Specifically in cases where Windows substitutes the font based on the | |
| 182 // HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes registr
y entries. | |
| 183 if (!matchesRequestedFamily) { | |
| 184 SkString familyName; | |
| 185 tf->getFamilyName(&familyName); | |
| 186 if (equalIgnoringCase(family, familyName)) | |
| 187 matchesRequestedFamily = true; | |
| 188 } | |
| 189 | |
| 190 return matchesRequestedFamily; | |
| 191 } | |
| 192 | |
| 193 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontD
escription, const AtomicString& family, float fontSize) | |
| 194 { | |
| 195 CString name; | |
| 196 RefPtr<SkTypeface> tf = createTypeface(fontDescription, family, name); | |
| 197 if (!tf) | |
| 198 return 0; | |
| 199 | |
| 200 // Windows will always give us a valid pointer here, even if the face name | |
| 201 // is non-existent. We have to double-check and see if the family name was | |
| 202 // really used. | |
| 203 // FIXME: Do we need to use predefined fonts "guaranteed" to exist | |
| 204 // when we're running in layout-test mode? | |
| 205 if (!typefacesMatchesFamily(tf.get(), family)) { | |
| 206 return 0; | |
| 207 } | |
| 208 | |
| 209 FontPlatformData* result = new FontPlatformData(tf, | |
| 210 name.data(), | |
| 211 fontSize, | |
| 212 fontDescription.weight() >= FontWeightBold && !tf->isBold() || fontDescr
iption.isSyntheticBold(), | |
| 213 fontDescription.italic() && !tf->isItalic() || fontDescription.isSynthet
icItalic(), | |
| 214 fontDescription.orientation(), | |
| 215 m_useSubpixelPositioning); | |
| 216 return result; | |
| 217 } | |
| 218 | |
| 219 } | |
| OLD | NEW |