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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 public: | 43 public: |
44 CachingWordShapeIterator(ShapeCache* cache, | 44 CachingWordShapeIterator(ShapeCache* cache, |
45 const TextRun& run, | 45 const TextRun& run, |
46 const Font* font) | 46 const Font* font) |
47 : shape_cache_(cache), | 47 : shape_cache_(cache), |
48 text_run_(run), | 48 text_run_(run), |
49 font_(font), | 49 font_(font), |
50 spacing_(run, font->GetFontDescription()), | 50 spacing_(run, font->GetFontDescription()), |
51 width_so_far_(0), | 51 width_so_far_(0), |
52 start_index_(0) { | 52 start_index_(0) { |
53 ASSERT(font); | 53 DCHECK(font); |
54 | 54 |
55 // Shaping word by word is faster as each word is cached. If we cannot | 55 // Shaping word by word is faster as each word is cached. If we cannot |
56 // use the cache or if the font doesn't support word by word shaping | 56 // use the cache or if the font doesn't support word by word shaping |
57 // fall back on shaping the entire run. | 57 // fall back on shaping the entire run. |
58 shape_by_word_ = font_->CanShapeWordByWord(); | 58 shape_by_word_ = font_->CanShapeWordByWord(); |
59 } | 59 } |
60 | 60 |
61 bool Next(RefPtr<const ShapeResult>* word_result) { | 61 bool Next(RefPtr<const ShapeResult>* word_result) { |
62 if (UNLIKELY(text_run_.AllowTabs())) | 62 if (UNLIKELY(text_run_.AllowTabs())) |
63 return NextForAllowTabs(word_result); | 63 return NextForAllowTabs(word_result); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 } | 148 } |
149 | 149 |
150 bool ShapeToEndIndex(RefPtr<const ShapeResult>* result, unsigned end_index) { | 150 bool ShapeToEndIndex(RefPtr<const ShapeResult>* result, unsigned end_index) { |
151 if (!end_index || end_index <= start_index_) | 151 if (!end_index || end_index <= start_index_) |
152 return false; | 152 return false; |
153 | 153 |
154 const unsigned length = text_run_.length(); | 154 const unsigned length = text_run_.length(); |
155 if (!start_index_ && end_index == length) { | 155 if (!start_index_ && end_index == length) { |
156 *result = ShapeWord(text_run_, font_); | 156 *result = ShapeWord(text_run_, font_); |
157 } else { | 157 } else { |
158 ASSERT(end_index <= length); | 158 DCHECK_LE(end_index, length); |
159 TextRun sub_run = | 159 TextRun sub_run = |
160 text_run_.SubRun(start_index_, end_index - start_index_); | 160 text_run_.SubRun(start_index_, end_index - start_index_); |
161 *result = ShapeWord(sub_run, font_); | 161 *result = ShapeWord(sub_run, font_); |
162 } | 162 } |
163 start_index_ = end_index; | 163 start_index_ = end_index; |
164 return result->Get(); | 164 return result->Get(); |
165 } | 165 } |
166 | 166 |
167 unsigned EndIndexUntil(UChar ch) const { | 167 unsigned EndIndexUntil(UChar ch) const { |
168 unsigned length = text_run_.length(); | 168 unsigned length = text_run_.length(); |
169 ASSERT(start_index_ < length); | 169 DCHECK_LT(start_index_, length); |
170 for (unsigned i = start_index_ + 1;; i++) { | 170 for (unsigned i = start_index_ + 1;; i++) { |
171 if (i == length || text_run_[i] == ch) | 171 if (i == length || text_run_[i] == ch) |
172 return i; | 172 return i; |
173 } | 173 } |
174 } | 174 } |
175 | 175 |
176 bool NextForAllowTabs(RefPtr<const ShapeResult>* word_result) { | 176 bool NextForAllowTabs(RefPtr<const ShapeResult>* word_result) { |
177 unsigned length = text_run_.length(); | 177 unsigned length = text_run_.length(); |
178 if (start_index_ >= length) | 178 if (start_index_ >= length) |
179 return false; | 179 return false; |
180 | 180 |
181 if (UNLIKELY(text_run_[start_index_] == kTabulationCharacter)) { | 181 if (UNLIKELY(text_run_[start_index_] == kTabulationCharacter)) { |
182 for (unsigned i = start_index_ + 1;; i++) { | 182 for (unsigned i = start_index_ + 1;; i++) { |
183 if (i == length || text_run_[i] != kTabulationCharacter) { | 183 if (i == length || text_run_[i] != kTabulationCharacter) { |
184 *word_result = ShapeResult::CreateForTabulationCharacters( | 184 *word_result = ShapeResult::CreateForTabulationCharacters( |
185 font_, text_run_, width_so_far_, i - start_index_); | 185 font_, text_run_, width_so_far_, i - start_index_); |
186 start_index_ = i; | 186 start_index_ = i; |
187 break; | 187 break; |
188 } | 188 } |
189 } | 189 } |
190 } else if (!shape_by_word_) { | 190 } else if (!shape_by_word_) { |
191 if (!ShapeToEndIndex(word_result, EndIndexUntil(kTabulationCharacter))) | 191 if (!ShapeToEndIndex(word_result, EndIndexUntil(kTabulationCharacter))) |
192 return false; | 192 return false; |
193 } else { | 193 } else { |
194 if (!NextWord(word_result)) | 194 if (!NextWord(word_result)) |
195 return false; | 195 return false; |
196 } | 196 } |
197 ASSERT(*word_result); | 197 DCHECK(*word_result); |
198 width_so_far_ += (*word_result)->Width(); | 198 width_so_far_ += (*word_result)->Width(); |
199 return true; | 199 return true; |
200 } | 200 } |
201 | 201 |
202 ShapeCache* shape_cache_; | 202 ShapeCache* shape_cache_; |
203 const TextRun& text_run_; | 203 const TextRun& text_run_; |
204 const Font* font_; | 204 const Font* font_; |
205 ShapeResultSpacing spacing_; | 205 ShapeResultSpacing spacing_; |
206 float width_so_far_; // Used only when allowTabs() | 206 float width_so_far_; // Used only when allowTabs() |
207 unsigned start_index_ : 31; | 207 unsigned start_index_ : 31; |
208 unsigned shape_by_word_ : 1; | 208 unsigned shape_by_word_ : 1; |
209 }; | 209 }; |
210 | 210 |
211 } // namespace blink | 211 } // namespace blink |
212 | 212 |
213 #endif // CachingWordShapeIterator_h | 213 #endif // CachingWordShapeIterator_h |
OLD | NEW |