| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2015 Google Inc. All rights reserved. | 2 * Copyright (C) 2015 Google 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 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 return nullptr; | 93 return nullptr; |
| 94 | 94 |
| 95 if (cacheEntry) | 95 if (cacheEntry) |
| 96 cacheEntry->m_shapeResult = shapeResult; | 96 cacheEntry->m_shapeResult = shapeResult; |
| 97 | 97 |
| 98 return shapeResult.release(); | 98 return shapeResult.release(); |
| 99 } | 99 } |
| 100 | 100 |
| 101 bool nextWord(RefPtr<ShapeResult>* wordResult) | 101 bool nextWord(RefPtr<ShapeResult>* wordResult) |
| 102 { | 102 { |
| 103 unsigned length = m_textRun.length(); | 103 return shapeToEndIndex(wordResult, nextWordEndIndex()); |
| 104 if (m_startIndex < length) { | |
| 105 if (m_textRun[m_startIndex] == spaceCharacter | |
| 106 || m_textRun[m_startIndex] == tabulationCharacter) { | |
| 107 TextRun wordRun = m_textRun.subRun(m_startIndex, 1); | |
| 108 *wordResult = shapeWord(wordRun, m_font); | |
| 109 m_startIndex++; | |
| 110 return *wordResult; | |
| 111 } | |
| 112 | |
| 113 return nextUntilCharacterOrTab(wordResult, spaceCharacter); | |
| 114 } | |
| 115 return false; | |
| 116 } | 104 } |
| 117 | 105 |
| 118 bool nextUntilCharacterOrTab(RefPtr<ShapeResult>* wordResult, UChar delimite
r) | 106 static bool isWordDelimiter(UChar ch) |
| 107 { |
| 108 return ch == spaceCharacter || ch == tabulationCharacter; |
| 109 } |
| 110 |
| 111 unsigned nextWordEndIndex() |
| 112 { |
| 113 const unsigned length = m_textRun.length(); |
| 114 if (m_startIndex >= length) |
| 115 return 0; |
| 116 |
| 117 if (m_startIndex + 1u == length || isWordDelimiter(m_textRun[m_startInde
x])) |
| 118 return m_startIndex + 1; |
| 119 |
| 120 for (unsigned i = m_startIndex + 1; ; i++) { |
| 121 if (i == length || isWordDelimiter(m_textRun[i])) |
| 122 return i; |
| 123 } |
| 124 } |
| 125 |
| 126 bool shapeToEndIndex(RefPtr<ShapeResult>* result, unsigned endIndex) |
| 127 { |
| 128 if (!endIndex || endIndex <= m_startIndex) |
| 129 return false; |
| 130 |
| 131 const unsigned length = m_textRun.length(); |
| 132 if (!m_startIndex && endIndex == length) { |
| 133 *result = shapeWord(m_textRun, m_font); |
| 134 } else { |
| 135 ASSERT(endIndex <= length); |
| 136 TextRun subRun = m_textRun.subRun(m_startIndex, endIndex - m_startIn
dex); |
| 137 *result = shapeWord(subRun, m_font); |
| 138 } |
| 139 m_startIndex = endIndex; |
| 140 return *result; |
| 141 } |
| 142 |
| 143 unsigned endIndexUntil(UChar ch) |
| 119 { | 144 { |
| 120 unsigned length = m_textRun.length(); | 145 unsigned length = m_textRun.length(); |
| 121 ASSERT(m_startIndex < length); | 146 ASSERT(m_startIndex < length); |
| 122 for (unsigned i = m_startIndex + 1; ; i++) { | 147 for (unsigned i = m_startIndex + 1; ; i++) { |
| 123 if (i == length || m_textRun[i] == delimiter | 148 if (i == length || m_textRun[i] == ch) |
| 124 || m_textRun[i] == tabulationCharacter) { | 149 return i; |
| 125 TextRun wordRun = m_textRun.subRun(m_startIndex, | |
| 126 i - m_startIndex); | |
| 127 m_startIndex = i; | |
| 128 *wordResult = shapeWord(wordRun, m_font); | |
| 129 return *wordResult; | |
| 130 } | |
| 131 } | 150 } |
| 132 } | 151 } |
| 133 | 152 |
| 134 bool nextForAllowTabs(RefPtr<ShapeResult>* wordResult) | 153 bool nextForAllowTabs(RefPtr<ShapeResult>* wordResult) |
| 135 { | 154 { |
| 136 unsigned length = m_textRun.length(); | 155 unsigned length = m_textRun.length(); |
| 137 if (m_startIndex >= length) | 156 if (m_startIndex >= length) |
| 138 return false; | 157 return false; |
| 139 | 158 |
| 140 if (UNLIKELY(m_textRun[m_startIndex] == tabulationCharacter)) { | 159 if (UNLIKELY(m_textRun[m_startIndex] == tabulationCharacter)) { |
| 141 for (unsigned i = m_startIndex + 1; ; i++) { | 160 for (unsigned i = m_startIndex + 1; ; i++) { |
| 142 if (i == length || m_textRun[i] != tabulationCharacter) { | 161 if (i == length || m_textRun[i] != tabulationCharacter) { |
| 143 *wordResult = ShapeResult::createForTabulationCharacters( | 162 *wordResult = ShapeResult::createForTabulationCharacters( |
| 144 m_font, m_textRun, m_widthSoFar, i - m_startIndex); | 163 m_font, m_textRun, m_widthSoFar, i - m_startIndex); |
| 145 m_startIndex = i; | 164 m_startIndex = i; |
| 146 break; | 165 break; |
| 147 } | 166 } |
| 148 } | 167 } |
| 149 } else if (!m_shapeByWord) { | 168 } else if (!m_shapeByWord) { |
| 150 if (!nextUntilCharacterOrTab(wordResult, 0)) | 169 if (!shapeToEndIndex(wordResult, endIndexUntil(tabulationCharacter))
) |
| 151 return false; | 170 return false; |
| 152 } else { | 171 } else { |
| 153 if (!nextWord(wordResult)) | 172 if (!nextWord(wordResult)) |
| 154 return false; | 173 return false; |
| 155 } | 174 } |
| 156 if (!*wordResult) | 175 ASSERT(*wordResult); |
| 157 return false; | |
| 158 m_widthSoFar += (*wordResult)->width(); | 176 m_widthSoFar += (*wordResult)->width(); |
| 159 return true; | 177 return true; |
| 160 } | 178 } |
| 161 | 179 |
| 162 ShapeCache* m_shapeCache; | 180 ShapeCache* m_shapeCache; |
| 163 const TextRun& m_textRun; | 181 const TextRun& m_textRun; |
| 164 const Font* m_font; | 182 const Font* m_font; |
| 165 float m_widthSoFar; // Used only when allowTabs() | 183 float m_widthSoFar; // Used only when allowTabs() |
| 166 unsigned m_startIndex : 30; | 184 unsigned m_startIndex : 30; |
| 167 unsigned m_wordResultCachable : 1; | 185 unsigned m_wordResultCachable : 1; |
| 168 unsigned m_shapeByWord : 1; | 186 unsigned m_shapeByWord : 1; |
| 169 }; | 187 }; |
| 170 | 188 |
| 171 } // namespace blink | 189 } // namespace blink |
| 172 | 190 |
| 173 #endif // CachingWordShapeIterator_h | 191 #endif // CachingWordShapeIterator_h |
| OLD | NEW |