| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/fonts/FontFallbackIterator.h" | 5 #include "platform/fonts/FontFallbackIterator.h" |
| 6 | 6 |
| 7 #include "platform/fonts/FontCache.h" | 7 #include "platform/fonts/FontCache.h" |
| 8 #include "platform/fonts/FontDescription.h" | 8 #include "platform/fonts/FontDescription.h" |
| 9 #include "platform/fonts/FontFallbackList.h" | 9 #include "platform/fonts/FontFallbackList.h" |
| 10 #include "platform/fonts/SegmentedFontData.h" | 10 #include "platform/fonts/SegmentedFontData.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 m_fallbackStage = SystemFonts; | 92 m_fallbackStage = SystemFonts; |
| 93 RefPtr<FontDataForRangeSet> fallbackPriorityFontRange = | 93 RefPtr<FontDataForRangeSet> fallbackPriorityFontRange = |
| 94 adoptRef(new FontDataForRangeSet(fallbackPriorityFont(hintList[0]))); | 94 adoptRef(new FontDataForRangeSet(fallbackPriorityFont(hintList[0]))); |
| 95 if (fallbackPriorityFontRange->hasFontData()) | 95 if (fallbackPriorityFontRange->hasFontData()) |
| 96 return uniqueOrNext(fallbackPriorityFontRange.release(), hintList); | 96 return uniqueOrNext(fallbackPriorityFontRange.release(), hintList); |
| 97 return next(hintList); | 97 return next(hintList); |
| 98 } | 98 } |
| 99 | 99 |
| 100 if (m_fallbackStage == SystemFonts) { | 100 if (m_fallbackStage == SystemFonts) { |
| 101 // We've reached pref + system fallback. | 101 // We've reached pref + system fallback. |
| 102 RefPtr<SimpleFontData> systemFont = uniqueSystemFontForHintList(hintList); | 102 ASSERT(hintList.size()); |
| 103 RefPtr<SimpleFontData> systemFont = uniqueSystemFontForHint(hintList[0]); |
| 103 if (systemFont) { | 104 if (systemFont) { |
| 104 // Fallback fonts are not retained in the FontDataCache. | 105 // Fallback fonts are not retained in the FontDataCache. |
| 105 return uniqueOrNext(adoptRef(new FontDataForRangeSet(systemFont)), | 106 return uniqueOrNext(adoptRef(new FontDataForRangeSet(systemFont)), |
| 106 hintList); | 107 hintList); |
| 107 } | 108 } |
| 108 | 109 |
| 109 // If we don't have options from the system fallback anymore or had | 110 // If we don't have options from the system fallback anymore or had |
| 110 // previously returned them, we only have the last resort font left. | 111 // previously returned them, we only have the last resort font left. |
| 111 // TODO: crbug.com/42217 Improve this by doing the last run with a last | 112 // TODO: crbug.com/42217 Improve this by doing the last run with a last |
| 112 // resort font that has glyphs for everything, for example the Unicode | 113 // resort font that has glyphs for everything, for example the Unicode |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 } | 186 } |
| 186 | 187 |
| 187 PassRefPtr<SimpleFontData> FontFallbackIterator::fallbackPriorityFont( | 188 PassRefPtr<SimpleFontData> FontFallbackIterator::fallbackPriorityFont( |
| 188 UChar32 hint) { | 189 UChar32 hint) { |
| 189 return FontCache::fontCache()->fallbackFontForCharacter( | 190 return FontCache::fontCache()->fallbackFontForCharacter( |
| 190 m_fontDescription, hint, | 191 m_fontDescription, hint, |
| 191 m_fontFallbackList->primarySimpleFontData(m_fontDescription), | 192 m_fontFallbackList->primarySimpleFontData(m_fontDescription), |
| 192 m_fontFallbackPriority); | 193 m_fontFallbackPriority); |
| 193 } | 194 } |
| 194 | 195 |
| 195 static inline unsigned chooseHintIndex(const Vector<UChar32>& hintList) { | 196 PassRefPtr<SimpleFontData> FontFallbackIterator::uniqueSystemFontForHint( |
| 196 // crbug.com/618178 has a test case where no Myanmar font is ever found, | 197 UChar32 hint) { |
| 197 // because the run starts with a punctuation character with a script value of | |
| 198 // common. Our current font fallback code does not find a very meaningful | |
| 199 // result for this. | |
| 200 // TODO crbug.com/668706 - Improve this situation. | |
| 201 // So if we have multiple hint characters (which indicates that a | |
| 202 // multi-character grapheme or more failed to shape, then we can try to be | |
| 203 // smarter and select the first character that has an actual script value. | |
| 204 DCHECK(hintList.size()); | |
| 205 if (hintList.size() <= 1) | |
| 206 return 0; | |
| 207 | |
| 208 UErrorCode err = U_ZERO_ERROR; | |
| 209 UScriptCode hintCharScript = uscript_getScript(hintList[0], &err); | |
| 210 if (!U_SUCCESS(err) || hintCharScript > USCRIPT_INHERITED) | |
| 211 return 0; | |
| 212 | |
| 213 for (size_t i = 1; i < hintList.size(); ++i) { | |
| 214 UScriptCode newHintScript = uscript_getScript(hintList[i], &err); | |
| 215 if (!U_SUCCESS(err)) | |
| 216 return 0; | |
| 217 if (newHintScript > USCRIPT_INHERITED) | |
| 218 return i; | |
| 219 } | |
| 220 return 0; | |
| 221 } | |
| 222 | |
| 223 PassRefPtr<SimpleFontData> FontFallbackIterator::uniqueSystemFontForHintList( | |
| 224 const Vector<UChar32>& hintList) { | |
| 225 // When we're asked for a fallback for the same characters again, we give up | 198 // When we're asked for a fallback for the same characters again, we give up |
| 226 // because the shaper must have previously tried shaping with the font | 199 // because the shaper must have previously tried shaping with the font |
| 227 // already. | 200 // already. |
| 228 if (!hintList.size()) | 201 if (!hint || m_previouslyAskedForHint.contains(hint)) |
| 229 return nullptr; | 202 return nullptr; |
| 230 | 203 |
| 231 FontCache* fontCache = FontCache::fontCache(); | 204 FontCache* fontCache = FontCache::fontCache(); |
| 232 UChar32 hint = hintList[chooseHintIndex(hintList)]; | |
| 233 | |
| 234 if (!hint || m_previouslyAskedForHint.contains(hint)) | |
| 235 return nullptr; | |
| 236 m_previouslyAskedForHint.add(hint); | 205 m_previouslyAskedForHint.add(hint); |
| 237 return fontCache->fallbackFontForCharacter( | 206 return fontCache->fallbackFontForCharacter( |
| 238 m_fontDescription, hint, | 207 m_fontDescription, hint, |
| 239 m_fontFallbackList->primarySimpleFontData(m_fontDescription)); | 208 m_fontFallbackList->primarySimpleFontData(m_fontDescription)); |
| 240 } | 209 } |
| 241 | 210 |
| 242 } // namespace blink | 211 } // namespace blink |
| OLD | NEW |