| 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/Logging.h" | 7 #include "platform/Logging.h" |
| 8 #include "platform/fonts/FontCache.h" | 8 #include "platform/fonts/FontCache.h" |
| 9 #include "platform/fonts/FontDescription.h" | 9 #include "platform/fonts/FontDescription.h" |
| 10 #include "platform/fonts/FontFallbackList.h" | 10 #include "platform/fonts/FontFallbackList.h" |
| 11 #include "platform/fonts/SegmentedFontData.h" | 11 #include "platform/fonts/SegmentedFontData.h" |
| 12 #include "platform/fonts/SimpleFontData.h" | 12 #include "platform/fonts/SimpleFontData.h" |
| 13 | 13 |
| 14 namespace blink { | 14 namespace blink { |
| 15 | 15 |
| 16 PassRefPtr<FontFallbackIterator> FontFallbackIterator::create(const FontDescript
ion& description, PassRefPtr<FontFallbackList> fallbackList) | 16 PassRefPtr<FontFallbackIterator> FontFallbackIterator::create( |
| 17 const FontDescription& description, |
| 18 PassRefPtr<FontFallbackList> fallbackList, |
| 19 FontFallbackPriority fontFallbackPriority) |
| 17 { | 20 { |
| 18 return adoptRef(new FontFallbackIterator(description, fallbackList)); | 21 return adoptRef(new FontFallbackIterator( |
| 22 description, fallbackList, fontFallbackPriority)); |
| 19 } | 23 } |
| 20 | 24 |
| 21 FontFallbackIterator::FontFallbackIterator(const FontDescription& description, P
assRefPtr<FontFallbackList> fallbackList) | 25 FontFallbackIterator::FontFallbackIterator(const FontDescription& description, |
| 26 PassRefPtr<FontFallbackList> fallbackList, |
| 27 FontFallbackPriority fontFallbackPriority) |
| 22 : m_fontDescription(description) | 28 : m_fontDescription(description) |
| 23 , m_fontFallbackList(fallbackList) | 29 , m_fontFallbackList(fallbackList) |
| 24 , m_currentFontDataIndex(0) | 30 , m_currentFontDataIndex(0) |
| 25 , m_segmentedIndex(0) | 31 , m_segmentedIndex(0) |
| 26 , m_fallbackStage(FontGroupFonts) | 32 , m_fallbackStage(FontGroupFonts) |
| 33 , m_fontFallbackPriority(fontFallbackPriority) |
| 27 { | 34 { |
| 28 } | 35 } |
| 29 | 36 |
| 30 bool FontFallbackIterator::needsHintList() const | 37 bool FontFallbackIterator::needsHintList() const |
| 31 { | 38 { |
| 32 if (m_fallbackStage != FontGroupFonts && m_fallbackStage != SegmentedFace) { | 39 if (m_fallbackStage != FontGroupFonts && m_fallbackStage != SegmentedFace) { |
| 33 return false; | 40 return false; |
| 34 } | 41 } |
| 35 | 42 |
| 36 const FontData* fontData = m_fontFallbackList->fontDataAt(m_fontDescription,
m_currentFontDataIndex); | 43 const FontData* fontData = m_fontFallbackList->fontDataAt(m_fontDescription,
m_currentFontDataIndex); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 64 return; | 71 return; |
| 65 | 72 |
| 66 selector->willUseRange(m_fontDescription, family, range); | 73 selector->willUseRange(m_fontDescription, family, range); |
| 67 } | 74 } |
| 68 | 75 |
| 69 const FontDataRange FontFallbackIterator::next(const Vector<UChar32>& hintList) | 76 const FontDataRange FontFallbackIterator::next(const Vector<UChar32>& hintList) |
| 70 { | 77 { |
| 71 if (m_fallbackStage == OutOfLuck) | 78 if (m_fallbackStage == OutOfLuck) |
| 72 return FontDataRange(); | 79 return FontDataRange(); |
| 73 | 80 |
| 74 const FontData* fontData = m_fontFallbackList->fontDataAt(m_fontDescription,
m_currentFontDataIndex); | 81 if (m_fallbackStage == FallbackPriorityFonts) { |
| 82 // Only try one fallback priority font, |
| 83 // then proceed to regular system fallback. |
| 84 m_fallbackStage = SystemFonts; |
| 85 FontDataRange fallbackPriorityFontRange(fallbackPriorityFont(hintList[0]
)); |
| 86 if (fallbackPriorityFontRange.hasFontData()) |
| 87 return fallbackPriorityFontRange; |
| 88 return next(hintList); |
| 89 } |
| 75 | 90 |
| 76 // If there is no fontData coming from the fallback list, it means | 91 if (m_fallbackStage == SystemFonts) { |
| 77 // we have reached the system fallback stage. | |
| 78 if (!fontData) { | |
| 79 m_fallbackStage = SystemFonts; | |
| 80 // We've reached pref + system fallback. | 92 // We've reached pref + system fallback. |
| 81 ASSERT(hintList.size()); | 93 ASSERT(hintList.size()); |
| 82 RefPtr<SimpleFontData> systemFont = uniqueSystemFontForHint(hintList[0])
; | 94 RefPtr<SimpleFontData> systemFont = uniqueSystemFontForHint(hintList[0])
; |
| 83 if (systemFont) | 95 if (systemFont) |
| 84 return FontDataRange(systemFont); | 96 return FontDataRange(systemFont); |
| 85 | 97 |
| 86 // If we don't have options from the system fallback anymore or had | 98 // If we don't have options from the system fallback anymore or had |
| 87 // previously returned them, we only have the last resort font left. | 99 // previously returned them, we only have the last resort font left. |
| 88 // TODO: crbug.com/42217 Improve this by doing the last run with a last | 100 // TODO: crbug.com/42217 Improve this by doing the last run with a last |
| 89 // resort font that has glyphs for everything, for example the Unicode | 101 // resort font that has glyphs for everything, for example the Unicode |
| 90 // LastResort font, not just Times or Arial. | 102 // LastResort font, not just Times or Arial. |
| 91 FontCache* fontCache = FontCache::fontCache(); | 103 FontCache* fontCache = FontCache::fontCache(); |
| 92 m_fallbackStage = OutOfLuck; | 104 m_fallbackStage = OutOfLuck; |
| 93 RefPtr<SimpleFontData> lastResort = fontCache->getLastResortFallbackFont
(m_fontDescription).get(); | 105 RefPtr<SimpleFontData> lastResort = fontCache->getLastResortFallbackFont
(m_fontDescription).get(); |
| 94 RELEASE_ASSERT(lastResort); | 106 RELEASE_ASSERT(lastResort); |
| 95 return FontDataRange(lastResort); | 107 return FontDataRange(lastResort); |
| 96 } | 108 } |
| 97 | 109 |
| 110 ASSERT(m_fallbackStage == FontGroupFonts |
| 111 || m_fallbackStage == SegmentedFace); |
| 112 const FontData* fontData = m_fontFallbackList->fontDataAt( |
| 113 m_fontDescription, m_currentFontDataIndex); |
| 114 |
| 115 if (!fontData) { |
| 116 // If there is no fontData coming from the fallback list, it means |
| 117 // we are now looking at system fonts, either for prioritized symbol |
| 118 // or emoji fonts or by calling system fallback API. |
| 119 m_fallbackStage = isNonTextFallbackPriority(m_fontFallbackPriority) |
| 120 ? FallbackPriorityFonts |
| 121 : SystemFonts; |
| 122 return next(hintList); |
| 123 } |
| 124 |
| 98 // Otherwise we've received a fontData from the font-family: set of fonts, | 125 // Otherwise we've received a fontData from the font-family: set of fonts, |
| 99 // and a non-segmented one in this case. | 126 // and a non-segmented one in this case. |
| 100 if (!fontData->isSegmented()) { | 127 if (!fontData->isSegmented()) { |
| 101 // Skip forward to the next font family for the next call to next(). | 128 // Skip forward to the next font family for the next call to next(). |
| 102 m_currentFontDataIndex++; | 129 m_currentFontDataIndex++; |
| 103 if (!fontData->isLoading()) { | 130 if (!fontData->isLoading()) { |
| 104 RefPtr<SimpleFontData> nonSegmented = const_cast<SimpleFontData*>(to
SimpleFontData(fontData)); | 131 RefPtr<SimpleFontData> nonSegmented = const_cast<SimpleFontData*>(to
SimpleFontData(fontData)); |
| 105 return FontDataRange(nonSegmented); | 132 return FontDataRange(nonSegmented); |
| 106 } | 133 } |
| 107 return next(hintList); | 134 return next(hintList); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 130 if (currentRange.fontData()->customFontData()) | 157 if (currentRange.fontData()->customFontData()) |
| 131 currentRange.fontData()->customFontData()->beginLoadIfNeeded(); | 158 currentRange.fontData()->customFontData()->beginLoadIfNeeded(); |
| 132 if (!currentRange.fontData()->isLoading()) | 159 if (!currentRange.fontData()->isLoading()) |
| 133 return currentRange; | 160 return currentRange; |
| 134 m_loadingCustomFontForRanges.append(currentRange); | 161 m_loadingCustomFontForRanges.append(currentRange); |
| 135 } | 162 } |
| 136 | 163 |
| 137 return next(hintList); | 164 return next(hintList); |
| 138 } | 165 } |
| 139 | 166 |
| 167 const PassRefPtr<SimpleFontData> FontFallbackIterator::fallbackPriorityFont( |
| 168 UChar32 hint) |
| 169 { |
| 170 return FontCache::fontCache()->fallbackFontForCharacter( |
| 171 m_fontDescription, |
| 172 hint, |
| 173 m_fontFallbackList->primarySimpleFontData(m_fontDescription), |
| 174 m_fontFallbackPriority); |
| 175 } |
| 176 |
| 140 const PassRefPtr<SimpleFontData> FontFallbackIterator::uniqueSystemFontForHint(U
Char32 hint) | 177 const PassRefPtr<SimpleFontData> FontFallbackIterator::uniqueSystemFontForHint(U
Char32 hint) |
| 141 { | 178 { |
| 142 FontCache* fontCache = FontCache::fontCache(); | 179 FontCache* fontCache = FontCache::fontCache(); |
| 143 | 180 |
| 144 // When we're asked for a fallback for the same characters again, we give up | 181 // When we're asked for a fallback for the same characters again, we give up |
| 145 // because the shaper must have previously tried shaping with the font | 182 // because the shaper must have previously tried shaping with the font |
| 146 // already. | 183 // already. |
| 147 if (m_visitedSystemFonts.find(hint) != m_visitedSystemFonts.end()) { | 184 if (m_visitedSystemFonts.find(hint) != m_visitedSystemFonts.end()) { |
| 148 return nullptr; | 185 return nullptr; |
| 149 } | 186 } |
| 150 | 187 |
| 151 RefPtr<SimpleFontData> fallbackFont = fontCache->fallbackFontForCharacter(m_
fontDescription, hint, m_fontFallbackList->primarySimpleFontData(m_fontDescripti
on)); | 188 RefPtr<SimpleFontData> fallbackFont = fontCache->fallbackFontForCharacter(m_
fontDescription, hint, m_fontFallbackList->primarySimpleFontData(m_fontDescripti
on)); |
| 152 | 189 |
| 153 return m_visitedSystemFonts.add(hint, fallbackFont).storedValue->value; | 190 return m_visitedSystemFonts.add(hint, fallbackFont).storedValue->value; |
| 154 } | 191 } |
| 155 | 192 |
| 156 } // namespace blink | 193 } // namespace blink |
| OLD | NEW |