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 "core/platform/NotImplemented.h" |
| 36 #include "core/platform/graphics/Font.h" |
| 37 #include "core/platform/graphics/SimpleFontData.h" |
| 38 #include "core/platform/graphics/chromium/FontPlatformDataChromiumWin.h" |
| 39 #include "core/platform/graphics/chromium/FontUtilsChromiumWin.h" |
| 40 |
| 41 namespace WebCore { |
| 42 |
| 43 static bool fontContainsCharacter(const FontPlatformData* fontData, const wchar_
t* family, UChar32 character) |
| 44 { |
| 45 char characters[2] = {character, 0}; |
| 46 return fontData->typeface()->charsToGlyphs(characters, SkTypeface::kUTF32_En
coding, 0, 1) == 1; |
| 47 } |
| 48 |
| 49 // Given the desired base font, this will create a SimpleFontData for a specific |
| 50 // font that can be used to render the given range of characters. |
| 51 PassRefPtr<SimpleFontData> FontCache::getFontDataForCharacter(const Font& font,
UChar32 inputC) |
| 52 { |
| 53 // FIXME: We should fix getFallbackFamily to take a UChar32 |
| 54 // and remove this split-to-UChar16 code. |
| 55 UChar codeUnits[2]; |
| 56 int codeUnitsLength; |
| 57 if (inputC <= 0xFFFF) { |
| 58 codeUnits[0] = inputC; |
| 59 codeUnitsLength = 1; |
| 60 } else { |
| 61 codeUnits[0] = U16_LEAD(inputC); |
| 62 codeUnits[1] = U16_TRAIL(inputC); |
| 63 codeUnitsLength = 2; |
| 64 } |
| 65 |
| 66 // FIXME: Consider passing fontDescription.dominantScript() |
| 67 // to GetFallbackFamily here. |
| 68 FontDescription fontDescription = font.fontDescription(); |
| 69 UChar32 c; |
| 70 UScriptCode script; |
| 71 const wchar_t* family = getFallbackFamily(codeUnits, codeUnitsLength, fontDe
scription.genericFamily(), &c, &script); |
| 72 FontPlatformData* data = 0; |
| 73 if (family) |
| 74 data = getFontResourcePlatformData(font.fontDescription(), AtomicString
(family, wcslen(family)), false); |
| 75 |
| 76 // Last resort font list : PanUnicode. CJK fonts have a pretty |
| 77 // large repertoire. Eventually, we need to scan all the fonts |
| 78 // on the system to have a Firefox-like coverage. |
| 79 // Make sure that all of them are lowercased. |
| 80 const static wchar_t* const cjkFonts[] = { |
| 81 L"arial unicode ms", |
| 82 L"ms pgothic", |
| 83 L"simsun", |
| 84 L"gulim", |
| 85 L"pmingliu", |
| 86 L"wenquanyi zen hei", // Partial CJK Ext. A coverage but more widely kno
wn to Chinese users. |
| 87 L"ar pl shanheisun uni", |
| 88 L"ar pl zenkai uni", |
| 89 L"han nom a", // Complete CJK Ext. A coverage. |
| 90 L"code2000" // Complete CJK Ext. A coverage. |
| 91 // CJK Ext. B fonts are not listed here because it's of no use |
| 92 // with our current non-BMP character handling because we use |
| 93 // Uniscribe for it and that code path does not go through here. |
| 94 }; |
| 95 |
| 96 const static wchar_t* const commonFonts[] = { |
| 97 L"tahoma", |
| 98 L"arial unicode ms", |
| 99 L"lucida sans unicode", |
| 100 L"microsoft sans serif", |
| 101 L"palatino linotype", |
| 102 // Six fonts below (and code2000 at the end) are not from MS, but |
| 103 // once installed, cover a very wide range of characters. |
| 104 L"dejavu serif", |
| 105 L"dejavu sasns", |
| 106 L"freeserif", |
| 107 L"freesans", |
| 108 L"gentium", |
| 109 L"gentiumalt", |
| 110 L"ms pgothic", |
| 111 L"simsun", |
| 112 L"gulim", |
| 113 L"pmingliu", |
| 114 L"code2000" |
| 115 }; |
| 116 |
| 117 const wchar_t* const* panUniFonts = 0; |
| 118 int numFonts = 0; |
| 119 if (script == USCRIPT_HAN) { |
| 120 panUniFonts = cjkFonts; |
| 121 numFonts = WTF_ARRAY_LENGTH(cjkFonts); |
| 122 } else { |
| 123 panUniFonts = commonFonts; |
| 124 numFonts = WTF_ARRAY_LENGTH(commonFonts); |
| 125 } |
| 126 // Font returned from GetFallbackFamily may not cover |characters| |
| 127 // because it's based on script to font mapping. This problem is |
| 128 // critical enough for non-Latin scripts (especially Han) to |
| 129 // warrant an additional (real coverage) check with fontCotainsCharacter. |
| 130 int i; |
| 131 for (i = 0; (!data || !fontContainsCharacter(data, family, c)) && i < numFon
ts; ++i) { |
| 132 family = panUniFonts[i]; |
| 133 data = getFontResourcePlatformData(font.fontDescription(), AtomicString(
family, wcslen(family))); |
| 134 } |
| 135 // When i-th font (0-base) in |panUniFonts| contains a character and |
| 136 // we get out of the loop, |i| will be |i + 1|. That is, if only the |
| 137 // last font in the array covers the character, |i| will be numFonts. |
| 138 // So, we have to use '<=" rather than '<' to see if we found a font |
| 139 // covering the character. |
| 140 if (i <= numFonts) |
| 141 return getFontResourceData(data, DoNotRetain); |
| 142 |
| 143 return 0; |
| 144 } |
| 145 |
| 146 static bool typefacesMatchesFamily(const SkTypeface* tf, const AtomicString& fam
ily) |
| 147 { |
| 148 SkTypeface::LocalizedStrings* actualFamilies = tf->createFamilyNameIterator(
); |
| 149 bool matchesRequestedFamily = false; |
| 150 SkTypeface::LocalizedString actualFamily; |
| 151 |
| 152 while (actualFamilies->next(&actualFamily)) { |
| 153 if (equalIgnoringCase(family, actualFamily.fString.c_str())) { |
| 154 matchesRequestedFamily = true; |
| 155 break; |
| 156 } |
| 157 } |
| 158 actualFamilies->unref(); |
| 159 |
| 160 return matchesRequestedFamily; |
| 161 } |
| 162 |
| 163 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontD
escription, const AtomicString& family) |
| 164 { |
| 165 CString name; |
| 166 SkTypeface* tf = createTypeface(fontDescription, family, name); |
| 167 if (!tf) |
| 168 return 0; |
| 169 |
| 170 // Windows will always give us a valid pointer here, even if the face name |
| 171 // is non-existent. We have to double-check and see if the family name was |
| 172 // really used. |
| 173 // FIXME: Do we need to use predefined fonts "guaranteed" to exist |
| 174 // when we're running in layout-test mode? |
| 175 if (!typefacesMatchesFamily(tf, family)) { |
| 176 tf->unref(); |
| 177 return 0; |
| 178 } |
| 179 |
| 180 FontPlatformData* result = new FontPlatformData(tf, |
| 181 name.data(), |
| 182 fontDescription.computedSize(), |
| 183 fontDescription.weight() >= FontWeightBold && !tf->isBold(), |
| 184 fontDescription.italic() && !tf->isItalic(), |
| 185 fontDescription.orientation()); |
| 186 tf->unref(); |
| 187 return result; |
| 188 } |
| 189 |
| 190 } |
OLD | NEW |