OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | 2 * Copyright (C) 2006 Apple Computer, 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 | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 } | 143 } |
144 | 144 |
145 // Begin to load the first custom font if needed. | 145 // Begin to load the first custom font if needed. |
146 if (shouldLoadCustomFont) { | 146 if (shouldLoadCustomFont) { |
147 shouldLoadCustomFont = false; | 147 shouldLoadCustomFont = false; |
148 fontDataForSpace->customFontData()->beginLoadIfNeeded(); | 148 fontDataForSpace->customFontData()->beginLoadIfNeeded(); |
149 } | 149 } |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
| 153 unsigned evaluateFontMatch(const FontDescription& desiredFont, PassRefPtr<FontDa
ta> actualFont) |
| 154 { |
| 155 const SkFontStyle& actualStyle = actualFont->fontDataForCharacter(' ')->plat
formData().typeface()->fontStyle(); |
| 156 // TODO: use desiredFont.skiaFontStyle(); |
| 157 |
| 158 // Blink maps font weights into [0,8], with 3 being 'normal' (400). |
| 159 // Skia maps the same font weights into [100,900] with 400 being 'normal'. |
| 160 int fontWeightScore = abs((desiredFont.weight() + 1) - (actualStyle.weight()
/ 100)); |
| 161 if (fontWeightScore == 1) |
| 162 fontWeightScore = 0; // Allow for a small amount of tolerance. |
| 163 if (fontWeightScore > 3) |
| 164 fontWeightScore *= 10; // Larger weight differenecs are disproportionate
ly bad. |
| 165 |
| 166 int fontStretchScore = abs(desiredFont.stretch() - actualStyle.width()); //
Skia and Blink agree on the definition of font stretch |
| 167 if (fontStretchScore == 1) |
| 168 fontStretchScore = 0; // Allow for a small amount of tolerance. |
| 169 if (fontStretchScore > 3) |
| 170 fontStretchScore *= 3; // Larger stretch differences are bad, but not as
noticeable as weight differences. |
| 171 |
| 172 // We allow italic and oblique to substitute for each other, but substitutin
g either for upright/normal is undesirable. |
| 173 int fontSlantScore = 0; |
| 174 if ((desiredFont.style() == FontStyleNormal) != (actualStyle.slant() == SkFo
ntStyle::kUpright_Slant)) |
| 175 fontSlantScore += 50; |
| 176 |
| 177 return fontWeightScore + fontStretchScore + fontSlantScore; |
| 178 } |
| 179 |
153 PassRefPtr<FontData> FontFallbackList::getFontData(const FontDescription& fontDe
scription, int& familyIndex) const | 180 PassRefPtr<FontData> FontFallbackList::getFontData(const FontDescription& fontDe
scription, int& familyIndex) const |
154 { | 181 { |
155 const FontFamily* currFamily = &fontDescription.family(); | 182 const FontFamily* currFamily = &fontDescription.family(); |
156 for (int i = 0; currFamily && i < familyIndex; i++) | 183 for (int i = 0; currFamily && i < familyIndex; i++) |
157 currFamily = currFamily->next(); | 184 currFamily = currFamily->next(); |
158 | 185 |
| 186 int bestMatchIndex = cAllFamiliesScanned; |
| 187 unsigned bestMatchScore = UINT_MAX; |
| 188 RefPtr<FontData> bestMatch; |
| 189 |
159 for (; currFamily; currFamily = currFamily->next()) { | 190 for (; currFamily; currFamily = currFamily->next()) { |
160 familyIndex++; | 191 familyIndex++; |
161 if (currFamily->family().length()) { | 192 if (currFamily->family().length()) { |
162 RefPtr<FontData> result; | 193 RefPtr<FontData> result; |
163 if (m_fontSelector) | 194 if (m_fontSelector) |
164 result = m_fontSelector->getFontData(fontDescription, currFamily
->family()); | 195 result = m_fontSelector->getFontData(fontDescription, currFamily
->family()); |
165 if (!result) | 196 if (!result) |
166 result = FontCache::fontCache()->getFontData(fontDescription, cu
rrFamily->family()); | 197 result = FontCache::fontCache()->getFontData(fontDescription, cu
rrFamily->family()); |
167 if (result) | 198 if (result) { |
168 return result.release(); | 199 unsigned fontMatchScore = evaluateFontMatch(fontDescription, res
ult); |
| 200 if (fontMatchScore == 0) |
| 201 return result.release(); |
| 202 if (fontMatchScore < bestMatchScore) { |
| 203 bestMatch = result; |
| 204 bestMatchScore = fontMatchScore; |
| 205 bestMatchIndex = familyIndex; |
| 206 } |
| 207 } |
169 } | 208 } |
170 } | 209 } |
| 210 if (bestMatchScore != UINT_MAX) { |
| 211 familyIndex = bestMatchIndex; |
| 212 return bestMatch.release(); |
| 213 } |
171 familyIndex = cAllFamiliesScanned; | 214 familyIndex = cAllFamiliesScanned; |
172 | 215 |
173 if (m_fontSelector) { | 216 if (m_fontSelector) { |
174 // Try the user's preferred standard font. | 217 // Try the user's preferred standard font. |
175 if (RefPtr<FontData> data = m_fontSelector->getFontData(fontDescription,
FontFamilyNames::webkit_standard)) | 218 if (RefPtr<FontData> data = m_fontSelector->getFontData(fontDescription,
FontFamilyNames::webkit_standard)) |
176 return data.release(); | 219 return data.release(); |
177 } | 220 } |
178 | 221 |
179 // Still no result. Hand back our last resort fallback font. | 222 // Still no result. Hand back our last resort fallback font. |
180 return FontCache::fontCache()->getLastResortFallbackFont(fontDescription); | 223 return FontCache::fontCache()->getLastResortFallbackFont(fontDescription); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 | 277 |
235 bool FontFallbackList::isValid() const | 278 bool FontFallbackList::isValid() const |
236 { | 279 { |
237 if (!m_fontSelector) | 280 if (!m_fontSelector) |
238 return m_fontSelectorVersion == 0; | 281 return m_fontSelectorVersion == 0; |
239 | 282 |
240 return m_fontSelector->version() == m_fontSelectorVersion; | 283 return m_fontSelector->version() == m_fontSelectorVersion; |
241 } | 284 } |
242 | 285 |
243 } // namespace blink | 286 } // namespace blink |
OLD | NEW |