Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(497)

Side by Side Diff: ui/gfx/render_text_harfbuzz.cc

Issue 1070223004: Stop combining text runs which are connected by 'COMMON' blocks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Mukai's comments. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "ui/gfx/render_text_harfbuzz.h" 5 #include "ui/gfx/render_text_harfbuzz.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <set> 8 #include <set>
9 9
10 #include "base/i18n/bidi_line_iterator.h" 10 #include "base/i18n/bidi_line_iterator.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 // The maximum number of scripts a Unicode character can belong to. This value 42 // The maximum number of scripts a Unicode character can belong to. This value
43 // is arbitrarily chosen to be a good limit because it is unlikely for a single 43 // is arbitrarily chosen to be a good limit because it is unlikely for a single
44 // character to belong to more scripts. 44 // character to belong to more scripts.
45 const size_t kMaxScripts = 5; 45 const size_t kMaxScripts = 5;
46 46
47 // Returns true if characters of |block_code| may trigger font fallback. 47 // Returns true if characters of |block_code| may trigger font fallback.
48 // Dingbats and emoticons can be rendered through the color emoji font file, 48 // Dingbats and emoticons can be rendered through the color emoji font file,
49 // therefore it needs to be trigerred as fallbacks. See crbug.com/448909 49 // therefore it needs to be trigerred as fallbacks. See crbug.com/448909
50 bool IsUnusualBlockCode(UBlockCode block_code) { 50 bool IsUnusualBlockCode(UBlockCode block_code) {
51 return block_code == UBLOCK_GEOMETRIC_SHAPES || 51 return block_code == UBLOCK_GEOMETRIC_SHAPES ||
52 block_code == UBLOCK_MISCELLANEOUS_SYMBOLS || 52 block_code == UBLOCK_MISCELLANEOUS_SYMBOLS;
53 block_code == UBLOCK_DINGBATS ||
54 block_code == UBLOCK_EMOTICONS;
55 } 53 }
56 54
57 bool IsBracket(UChar32 character) { 55 bool IsBracket(UChar32 character) {
58 static const char kBrackets[] = { '(', ')', '{', '}', '<', '>', }; 56 static const char kBrackets[] = { '(', ')', '{', '}', '<', '>', };
59 static const char* kBracketsEnd = kBrackets + arraysize(kBrackets); 57 static const char* kBracketsEnd = kBrackets + arraysize(kBrackets);
60 return std::find(kBrackets, kBracketsEnd, character) != kBracketsEnd; 58 return std::find(kBrackets, kBracketsEnd, character) != kBracketsEnd;
61 } 59 }
62 60
63 // Returns the boundary between a special and a regular character. Special 61 // Returns the boundary between a special and a regular character. Special
64 // characters are brackets or characters that satisfy |IsUnusualBlockCode|. 62 // characters are brackets or characters that satisfy |IsUnusualBlockCode|.
(...skipping 18 matching lines...) Expand all
83 const bool block_break = current_block != first_block && 81 const bool block_break = current_block != first_block &&
84 (first_block_unusual || IsUnusualBlockCode(current_block)); 82 (first_block_unusual || IsUnusualBlockCode(current_block));
85 if (block_break || current_char == '\n' || 83 if (block_break || current_char == '\n' ||
86 first_bracket != IsBracket(current_char)) { 84 first_bracket != IsBracket(current_char)) {
87 return run_start + iter.array_pos(); 85 return run_start + iter.array_pos();
88 } 86 }
89 } 87 }
90 return run_break; 88 return run_break;
91 } 89 }
92 90
93 // If the given scripts match, returns the one that isn't USCRIPT_COMMON or 91 // If the given scripts match, returns the one that isn't USCRIPT_INHERITED,
94 // USCRIPT_INHERITED, i.e. the more specific one. Otherwise returns 92 // i.e. the more specific one. Otherwise returns
95 // USCRIPT_INVALID_CODE. 93 // USCRIPT_INVALID_CODE.
msw 2015/05/27 17:38:00 nit: fits on line above.
xdai1 2015/06/01 16:51:15 Done.
96 UScriptCode ScriptIntersect(UScriptCode first, UScriptCode second) { 94 UScriptCode ScriptIntersect(UScriptCode first, UScriptCode second) {
97 if (first == second || 95 if (first == second || second == USCRIPT_INHERITED)
98 (second > USCRIPT_INVALID_CODE && second <= USCRIPT_INHERITED)) {
99 return first; 96 return first;
100 } 97 if (first == USCRIPT_INHERITED)
101 if (first > USCRIPT_INVALID_CODE && first <= USCRIPT_INHERITED)
102 return second; 98 return second;
103 return USCRIPT_INVALID_CODE; 99 return USCRIPT_INVALID_CODE;
104 } 100 }
105 101
106 // Writes the script and the script extensions of the character with the 102 // Writes the script and the script extensions of the character with the
107 // Unicode |codepoint|. Returns the number of written scripts. 103 // Unicode |codepoint|. Returns the number of written scripts.
108 int GetScriptExtensions(UChar32 codepoint, UScriptCode* scripts) { 104 int GetScriptExtensions(UChar32 codepoint, UScriptCode* scripts) {
109 UErrorCode icu_error = U_ZERO_ERROR; 105 UErrorCode icu_error = U_ZERO_ERROR;
110 // ICU documentation incorrectly states that the result of 106 // ICU documentation incorrectly states that the result of
111 // |uscript_getScriptExtensions| will contain the regular script property. 107 // |uscript_getScriptExtensions| will contain the regular script property.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 UScriptCode* script) { 155 UScriptCode* script) {
160 DCHECK_GT(length, 0U); 156 DCHECK_GT(length, 0U);
161 157
162 UScriptCode scripts[kMaxScripts] = { USCRIPT_INVALID_CODE }; 158 UScriptCode scripts[kMaxScripts] = { USCRIPT_INVALID_CODE };
163 159
164 base::i18n::UTF16CharIterator char_iterator(text.c_str() + start, length); 160 base::i18n::UTF16CharIterator char_iterator(text.c_str() + start, length);
165 size_t scripts_size = GetScriptExtensions(char_iterator.get(), scripts); 161 size_t scripts_size = GetScriptExtensions(char_iterator.get(), scripts);
166 *script = scripts[0]; 162 *script = scripts[0];
167 163
168 while (char_iterator.Advance()) { 164 while (char_iterator.Advance()) {
165 // Special handling to merge white space into the previous run.
166 if (u_isUWhiteSpace(char_iterator.get()))
167 continue;
169 ScriptSetIntersect(char_iterator.get(), scripts, &scripts_size); 168 ScriptSetIntersect(char_iterator.get(), scripts, &scripts_size);
170 if (scripts_size == 0U) 169 if (scripts_size == 0U)
171 return char_iterator.array_pos(); 170 return char_iterator.array_pos();
172 *script = scripts[0]; 171 *script = scripts[0];
173 } 172 }
174 173
175 return length; 174 return length;
176 } 175 }
177 176
178 // A port of hb_icu_script_to_script because harfbuzz on CrOS is built without 177 // A port of hb_icu_script_to_script because harfbuzz on CrOS is built without
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 // than |max_width|. If |multiline| is false, only outputs a single Line from 215 // than |max_width|. If |multiline| is false, only outputs a single Line from
217 // the given runs. |min_baseline| and |min_height| are the minimum baseline and 216 // the given runs. |min_baseline| and |min_height| are the minimum baseline and
218 // height for each line. 217 // height for each line.
219 // TODO(ckocagil): Expose the interface of this class in the header and test 218 // TODO(ckocagil): Expose the interface of this class in the header and test
220 // this class directly. 219 // this class directly.
221 class HarfBuzzLineBreaker { 220 class HarfBuzzLineBreaker {
222 public: 221 public:
223 HarfBuzzLineBreaker(size_t max_width, 222 HarfBuzzLineBreaker(size_t max_width,
224 int min_baseline, 223 int min_baseline,
225 float min_height, 224 float min_height,
226 bool multiline,
227 WordWrapBehavior word_wrap_behavior, 225 WordWrapBehavior word_wrap_behavior,
228 const base::string16& text, 226 const base::string16& text,
229 const BreakList<size_t>* words, 227 const BreakList<size_t>* words,
230 const internal::TextRunList& run_list) 228 const internal::TextRunList& run_list)
231 : max_width_((max_width == 0) ? SK_ScalarMax : SkIntToScalar(max_width)), 229 : max_width_((max_width == 0) ? SK_ScalarMax : SkIntToScalar(max_width)),
232 min_baseline_(min_baseline), 230 min_baseline_(min_baseline),
233 min_height_(min_height), 231 min_height_(min_height),
234 multiline_(multiline),
235 word_wrap_behavior_(word_wrap_behavior), 232 word_wrap_behavior_(word_wrap_behavior),
236 text_(text), 233 text_(text),
237 words_(words), 234 words_(words),
238 run_list_(run_list), 235 run_list_(run_list),
236 max_descent_(0),
237 max_ascent_(0),
239 text_x_(0), 238 text_x_(0),
240 line_x_(0), 239 available_width_(max_width_) {
241 max_descent_(0),
242 max_ascent_(0) {
243 DCHECK_EQ(multiline_, (words_ != nullptr));
244 AdvanceLine(); 240 AdvanceLine();
245 } 241 }
246 242
247 // Breaks the run at given |run_index| into Line structs. 243 // Constructs a single line for |text_| using |run_list_|.
248 void AddRun(int run_index) { 244 void ConstructSingleLine() {
249 const internal::TextRunHarfBuzz* run = run_list_.runs()[run_index]; 245 for (size_t i = 0; i < run_list_.size(); i++) {
250 base::char16 first_char = text_[run->range.start()]; 246 const internal::TextRunHarfBuzz& run = *(run_list_.runs()[i]);
251 if (multiline_ && first_char == '\n') { 247 internal::LineSegment segment;
252 AdvanceLine(); 248 segment.run = i;
253 } else if (multiline_ && (line_x_ + SkFloatToScalar(run->width)) > 249 segment.char_range = run.range;
254 max_width_) { 250 segment.x_range = Range(SkScalarCeilToInt(text_x_),
msw 2015/05/27 17:37:59 The segment ranges should probably be RangeF, but
xdai1 2015/06/01 16:51:15 Yes, we probably should use RangeF for the X coord
255 BreakRun(run_index); 251 SkScalarCeilToInt(text_x_ + run.width));
256 } else { 252 segment.width = run.width;
257 AddSegment(run_index, run->range, run->width); 253 AddLineSegment(segment);
254 }
255 }
256
257 // Constructs multiple lines for |text_| based on words iteration approach.
258 void ConstructMultiLines() {
259 DCHECK(words_);
260 for (auto iter = words_->breaks().begin(); iter != words_->breaks().end();
261 iter++) {
262 const Range word_range = words_->GetRange(iter);
263 std::vector<internal::LineSegment> word_segments;
264 SkScalar word_width = GetWordWidth(word_range, &word_segments);
msw 2015/05/28 18:16:16 The old algorithm created one segment per run, or
xdai1 2015/06/01 16:51:15 Yes, the number of the segments is the same with t
265
266 bool new_line = false;
267 if (!word_segments.empty()) {
268 // If the word is end with '\n', we should advance a new line after
msw 2015/05/28 18:16:16 nit: s/is end/ends/ or consider "If the last word
xdai1 2015/06/01 16:51:14 Done.
269 // adding the word to current line.
msw 2015/05/28 18:16:16 nit: "to the current"
xdai1 2015/06/01 16:51:14 Done.
270 const internal::LineSegment& last_segment = word_segments.back();
271 const base::char16 last_char = text_[last_segment.char_range.start()];
272 if (last_char == '\n') {
msw 2015/05/28 18:16:16 nit: inline if (text_[word_segments.back().char_ra
xdai1 2015/06/01 16:51:15 Done.
273 new_line = true;
274 word_width -= last_segment.width;
275 word_segments.pop_back();
276 }
277 }
278
279 // If the word is not the first word in the line and it can't fit into
280 // the current line, advance a new line.
281 if (word_width > available_width_ && available_width_ != max_width_)
282 AdvanceLine();
283 AddWordToLine(word_segments);
284 if (new_line)
285 AdvanceLine();
258 } 286 }
259 } 287 }
260 288
261 // Finishes line breaking and outputs the results. Can be called at most once. 289 // Finishes line breaking and outputs the results. Can be called at most once.
262 void Finalize(std::vector<internal::Line>* lines, SizeF* size) { 290 void FinalizeLines(std::vector<internal::Line>* lines, SizeF* size) {
263 DCHECK(!lines_.empty()); 291 DCHECK(!lines_.empty());
264 // Add an empty line to finish the line size calculation and remove it. 292 // Add an empty line to finish the line size calculation and remove it.
265 AdvanceLine(); 293 AdvanceLine();
266 lines_.pop_back(); 294 lines_.pop_back();
267 *size = total_size_; 295 *size = total_size_;
268 lines->swap(lines_); 296 lines->swap(lines_);
269 } 297 }
270 298
271 private: 299 private:
272 // A (line index, segment index) pair that specifies a segment in |lines_|. 300 // A (line index, segment index) pair that specifies a segment in |lines_|.
273 typedef std::pair<size_t, size_t> SegmentHandle; 301 typedef std::pair<size_t, size_t> SegmentHandle;
274 302
275 internal::LineSegment* SegmentFromHandle(const SegmentHandle& handle) { 303 internal::LineSegment* SegmentFromHandle(const SegmentHandle& handle) {
276 return &lines_[handle.first].segments[handle.second]; 304 return &lines_[handle.first].segments[handle.second];
277 } 305 }
278 306
279 // Breaks a run into segments that fit in the last line in |lines_| and adds
280 // them. Adds a new Line to the back of |lines_| whenever a new segment can't
281 // be added without the Line's width exceeding |max_width_|.
282 void BreakRun(int run_index) {
283 const internal::TextRunHarfBuzz& run = *(run_list_.runs()[run_index]);
284 SkScalar width = 0;
285 size_t next_char = run.range.start();
286
287 // Break the run until it fits the current line.
288 while (next_char < run.range.end()) {
289 const size_t current_char = next_char;
290 size_t end_char = next_char;
291 const bool skip_line =
292 BreakRunAtWidth(run, current_char, &width, &end_char, &next_char);
293 AddSegment(run_index, Range(current_char, end_char),
294 SkScalarToFloat(width));
295 if (skip_line)
296 AdvanceLine();
297 }
298 }
299
300 // Starting from |start_char|, finds a suitable line break position at or
301 // before available width using word break. If the current position is at the
302 // beginning of a line, this function will not roll back to |start_char| and
303 // |*next_char| will be greater than |start_char| (to avoid constructing empty
304 // lines). It stores the end of the segment range to |end_char|, which can be
305 // smaller than |*next_char| for certain word wrapping behavior.
306 // Returns whether to skip the line before |*next_char|.
307 // TODO(ckocagil): We might have to reshape after breaking at ligatures.
308 // See whether resolving the TODO above resolves this too.
309 // TODO(ckocagil): Do not reserve width for whitespace at the end of lines.
310 bool BreakRunAtWidth(const internal::TextRunHarfBuzz& run,
311 size_t start_char,
312 SkScalar* width,
313 size_t* end_char,
314 size_t* next_char) {
315 DCHECK(words_);
316 DCHECK(run.range.Contains(Range(start_char, start_char + 1)));
317 SkScalar available_width = max_width_ - line_x_;
318 BreakList<size_t>::const_iterator word = words_->GetBreak(start_char);
319 BreakList<size_t>::const_iterator next_word = word + 1;
320 // Width from |std::max(word->first, start_char)| to the current character.
321 SkScalar word_width = 0;
322 *width = 0;
323
324 Range char_range;
325 SkScalar truncated_width = 0;
326 for (size_t i = start_char; i < run.range.end(); i += char_range.length()) {
327 // |word| holds the word boundary at or before |i|, and |next_word| holds
328 // the word boundary right after |i|. Advance both |word| and |next_word|
329 // when |i| reaches |next_word|.
330 if (next_word != words_->breaks().end() && i >= next_word->first) {
331 if (*width > available_width) {
332 DCHECK_NE(WRAP_LONG_WORDS, word_wrap_behavior_);
333 *next_char = i;
334 if (word_wrap_behavior_ != TRUNCATE_LONG_WORDS)
335 *end_char = *next_char;
336 else
337 *width = truncated_width;
338 return true;
339 }
340 word = next_word++;
341 word_width = 0;
342 }
343
344 Range glyph_range;
345 run.GetClusterAt(i, &char_range, &glyph_range);
346 DCHECK_LT(0U, char_range.length());
347
348 SkScalar char_width = ((glyph_range.end() >= run.glyph_count)
349 ? SkFloatToScalar(run.width)
350 : run.positions[glyph_range.end()].x()) -
351 run.positions[glyph_range.start()].x();
352
353 *width += char_width;
354 word_width += char_width;
355
356 // TODO(mukai): implement ELIDE_LONG_WORDS.
357 if (*width > available_width) {
358 if (line_x_ != 0 || word_width < *width) {
359 // Roll back one word.
360 *width -= word_width;
361 *next_char = std::max(word->first, start_char);
362 *end_char = *next_char;
363 return true;
364 } else if (word_wrap_behavior_ == WRAP_LONG_WORDS) {
365 if (char_width < *width) {
366 // Roll back one character.
367 *width -= char_width;
368 *next_char = i;
369 } else {
370 // Continue from the next character.
371 *next_char = i + char_range.length();
372 }
373 *end_char = *next_char;
374 return true;
375 }
376 } else {
377 *end_char = char_range.end();
378 truncated_width = *width;
379 }
380 }
381
382 if (word_wrap_behavior_ == TRUNCATE_LONG_WORDS)
383 *width = truncated_width;
384 *end_char = *next_char = run.range.end();
385 return false;
386 }
387
388 // RTL runs are broken in logical order but displayed in visual order. To find
389 // the text-space coordinate (where it would fall in a single-line text)
390 // |x_range| of RTL segments, segment widths are applied in reverse order.
391 // e.g. {[5, 10], [10, 40]} will become {[35, 40], [5, 35]}.
392 void UpdateRTLSegmentRanges() {
393 if (rtl_segments_.empty())
394 return;
395 float x = SegmentFromHandle(rtl_segments_[0])->x_range.start();
396 for (size_t i = rtl_segments_.size(); i > 0; --i) {
397 internal::LineSegment* segment = SegmentFromHandle(rtl_segments_[i - 1]);
398 const float segment_width = segment->width;
399 segment->x_range = Range(x, x + segment_width);
400 x += segment_width;
401 }
402 rtl_segments_.clear();
403 }
404
405 // Finishes the size calculations of the last Line in |lines_|. Adds a new 307 // Finishes the size calculations of the last Line in |lines_|. Adds a new
406 // Line to the back of |lines_|. 308 // Line to the back of |lines_|.
407 void AdvanceLine() { 309 void AdvanceLine() {
408 if (!lines_.empty()) { 310 if (!lines_.empty()) {
409 internal::Line* line = &lines_.back(); 311 internal::Line* line = &lines_.back();
410 std::sort(line->segments.begin(), line->segments.end(), 312 std::sort(line->segments.begin(), line->segments.end(),
411 [this](const internal::LineSegment& s1, 313 [this](const internal::LineSegment& s1,
412 const internal::LineSegment& s2) -> bool { 314 const internal::LineSegment& s2) -> bool {
413 return run_list_.logical_to_visual(s1.run) < 315 return run_list_.logical_to_visual(s1.run) <
414 run_list_.logical_to_visual(s2.run); 316 run_list_.logical_to_visual(s2.run);
415 }); 317 });
416 line->size.set_height(std::max(min_height_, max_descent_ + max_ascent_)); 318 line->size.set_height(std::max(min_height_, max_descent_ + max_ascent_));
417 line->baseline = 319 line->baseline = std::max(min_baseline_, SkScalarRoundToInt(max_ascent_));
418 std::max(min_baseline_, SkScalarRoundToInt(max_ascent_));
419 line->preceding_heights = std::ceil(total_size_.height()); 320 line->preceding_heights = std::ceil(total_size_.height());
420 total_size_.set_height(total_size_.height() + line->size.height()); 321 total_size_.set_height(total_size_.height() + line->size.height());
421 total_size_.set_width(std::max(total_size_.width(), line->size.width())); 322 total_size_.set_width(std::max(total_size_.width(), line->size.width()));
422 } 323 }
423 max_descent_ = 0; 324 max_descent_ = 0;
424 max_ascent_ = 0; 325 max_ascent_ = 0;
425 line_x_ = 0; 326 available_width_ = max_width_;
426 lines_.push_back(internal::Line()); 327 lines_.push_back(internal::Line());
427 } 328 }
428 329
429 // Adds a new segment with the given properties to |lines_.back()|. 330 // Adds word to the current line. A word may contain multiple segments. If the
430 void AddSegment(int run_index, Range char_range, float width) { 331 // word is the first word in line and its width exceeds |available_width_|,
431 if (char_range.is_empty()) { 332 // ignore/truncate/wrap it according to |word_wrap_behavior_|.
432 DCHECK_EQ(0, width); 333 void AddWordToLine(const std::vector<internal::LineSegment>& word_segments) {
334 DCHECK(!lines_.empty());
335 if (word_segments.empty())
msw 2015/05/28 18:16:15 nit: this probably shouldn't happen either; make t
xdai1 2015/06/01 16:51:14 This might happen, see test function: RenderTextTe
msw 2015/06/01 23:27:18 When the overall text is empty? Should the code ba
xdai1 2015/06/02 03:49:56 For example, if the entire text is "\n" or just ""
433 return; 336 return;
337
338 bool has_truncated = false;
339 for (const internal::LineSegment& segment : word_segments) {
340 if (has_truncated)
341 break;
342 if (segment.width <= available_width_ ||
343 word_wrap_behavior_ == IGNORE_LONG_WORDS) {
344 AddLineSegment(segment);
345 } else {
346 DCHECK(word_wrap_behavior_ == TRUNCATE_LONG_WORDS ||
347 word_wrap_behavior_ == WRAP_LONG_WORDS);
348 has_truncated = (word_wrap_behavior_ == TRUNCATE_LONG_WORDS);
349
350 internal::LineSegment remain_segment = segment;
msw 2015/05/28 18:16:16 nit: remaining_segment or truncated_segment
xdai1 2015/06/01 16:51:15 Done.
351 while (!remain_segment.char_range.is_empty()) {
352 size_t cutoff_pos = 0;
353 SkScalar width = GetCutoffWidth(remain_segment, &cutoff_pos);
354 if (width == 0 && available_width_ == max_width_) {
355 // |max_width_| might be smaller than a single character. In this
356 // case we need to put at least one character in the line.
357 // See RenderTextTest.Multiline_MinWidth for example.
msw 2015/05/28 18:16:16 nit: for an example
xdai1 2015/06/01 16:51:16 Done.
358 cutoff_pos = remain_segment.char_range.start() + 1;
msw 2015/05/28 18:16:16 I wonder if this is correct/sufficient, what if th
xdai1 2015/06/01 16:51:15 You're right, we should special handle these cases
359 const internal::TextRunHarfBuzz& run =
360 *(run_list_.runs()[remain_segment.run]);
361 width = run.GetGlyphWidthForCharRange(
362 Range(remain_segment.char_range.start(), cutoff_pos));
363 }
364 if (width > 0) {
365 internal::LineSegment cut_segment;
366 cut_segment.run = remain_segment.run;
367 cut_segment.char_range =
368 Range(remain_segment.char_range.start(), cutoff_pos);
369 cut_segment.width = width;
370 cut_segment.x_range = Range(remain_segment.x_range.start(),
371 SkScalarCeilToInt(text_x_ + width));
msw 2015/05/28 18:16:16 Should this be remain_segment.x_range.start() + wi
xdai1 2015/06/01 16:51:16 It's preciser to use text_x_ instead of remain_seg
372 AddLineSegment(cut_segment);
373 // Updates old segment range.
374 remain_segment.char_range.set_start(cutoff_pos);
375 remain_segment.x_range.set_start(SkScalarCeilToInt(text_x_));
376 remain_segment.width -= width;
377 }
378 if (has_truncated)
379 break;
380 if (!remain_segment.char_range.is_empty())
381 AdvanceLine();
382 }
383 }
434 } 384 }
435 const internal::TextRunHarfBuzz& run = *(run_list_.runs()[run_index]); 385 }
436 386
437 internal::LineSegment segment; 387 // Add line segment to current line. Note in order to keep the visual order
msw 2015/05/27 17:37:59 nit: "Add a line segment to the current line. Note
xdai1 2015/06/01 16:51:15 Done.
438 segment.run = run_index; 388 // correct for ltr and rtl language, we need to merge segments that belong to
439 segment.char_range = char_range; 389 // a same run.
msw 2015/05/27 17:37:59 nit: "the same run"
xdai1 2015/06/01 16:51:15 Done.
440 segment.x_range = Range( 390 void AddLineSegment(const internal::LineSegment& segment) {
441 SkScalarCeilToInt(text_x_), 391 DCHECK(!lines_.empty());
442 SkScalarCeilToInt(text_x_ + SkFloatToScalar(width)));
443 segment.width = width;
444
445 internal::Line* line = &lines_.back(); 392 internal::Line* line = &lines_.back();
393 const internal::TextRunHarfBuzz& run = *(run_list_.runs()[segment.run]);
394 if (!line->segments.empty()) {
395 internal::LineSegment& last_segment = line->segments.back();
396 // Merge segments that belong to the same run.
397 if (last_segment.run == segment.run) {
398 last_segment.char_range.set_end(segment.char_range.end());
msw 2015/05/28 18:16:16 nit: can we dcheck that the segments actually cont
xdai1 2015/06/01 16:51:15 Done.
399 last_segment.width += segment.width;
400 last_segment.x_range.set_end(
msw 2015/05/28 18:16:16 ditto: can we dcheck that the x_ranges are adjacen
xdai1 2015/06/01 16:51:15 Done.
401 SkScalarCeilToInt(text_x_ + segment.width));
402 if (run.is_rtl && last_segment.char_range.end() == run.range.end())
403 UpdateRTLSegmentRanges();
404 line->size.set_width(line->size.width() + segment.width);
405 text_x_ += segment.width;
406 available_width_ -= segment.width;
407 return;
408 }
409 }
446 line->segments.push_back(segment); 410 line->segments.push_back(segment);
447 411
448 SkPaint paint; 412 SkPaint paint;
449 paint.setTypeface(run.skia_face.get()); 413 paint.setTypeface(run.skia_face.get());
450 paint.setTextSize(SkIntToScalar(run.font_size)); 414 paint.setTextSize(SkIntToScalar(run.font_size));
451 paint.setAntiAlias(run.render_params.antialiasing); 415 paint.setAntiAlias(run.render_params.antialiasing);
452 SkPaint::FontMetrics metrics; 416 SkPaint::FontMetrics metrics;
453 paint.getFontMetrics(&metrics); 417 paint.getFontMetrics(&metrics);
454 418
455 line->size.set_width(line->size.width() + width); 419 line->size.set_width(line->size.width() + segment.width);
456 // TODO(dschuyler): Account for stylized baselines in string sizing. 420 // TODO(dschuyler): Account for stylized baselines in string sizing.
457 max_descent_ = std::max(max_descent_, metrics.fDescent); 421 max_descent_ = std::max(max_descent_, metrics.fDescent);
458 // fAscent is always negative. 422 // fAscent is always negative.
459 max_ascent_ = std::max(max_ascent_, -metrics.fAscent); 423 max_ascent_ = std::max(max_ascent_, -metrics.fAscent);
460 424
461 if (run.is_rtl) { 425 if (run.is_rtl) {
462 rtl_segments_.push_back( 426 rtl_segments_.push_back(
463 SegmentHandle(lines_.size() - 1, line->segments.size() - 1)); 427 SegmentHandle(lines_.size() - 1, line->segments.size() - 1));
464 // If this is the last segment of an RTL run, reprocess the text-space x 428 // If this is the last segment of an RTL run, reprocess the text-space x
465 // ranges of all segments from the run. 429 // ranges of all segments from the run.
466 if (char_range.end() == run.range.end()) 430 if (segment.char_range.end() == run.range.end())
467 UpdateRTLSegmentRanges(); 431 UpdateRTLSegmentRanges();
468 } 432 }
469 text_x_ += SkFloatToScalar(width); 433 text_x_ += segment.width;
470 line_x_ += SkFloatToScalar(width); 434 available_width_ -= segment.width;
435 }
436
437 // Finds the end position |end_pos| in |segment| that the preceding width is
msw 2015/05/28 18:16:16 nit: s/that/where/
xdai1 2015/06/01 16:51:15 Done.
438 // no larger than |available_width_|, and returns the preceding width.
439 SkScalar GetCutoffWidth(const internal::LineSegment& segment,
440 size_t* end_pos) const {
441 DCHECK(!segment.char_range.is_empty());
442 const internal::TextRunHarfBuzz& run = *(run_list_.runs()[segment.run]);
443 *end_pos = segment.char_range.start();
444 SkScalar width = 0;
445 while (*end_pos < segment.char_range.end()) {
446 const SkScalar char_width =
447 run.GetGlyphWidthForCharRange(Range(*end_pos, *end_pos + 1));
448 if (width + char_width > available_width_)
449 break;
450 width += char_width;
451 *end_pos += 1;
msw 2015/05/28 18:16:15 nit: (*end_pos)++;
xdai1 2015/06/01 16:51:15 Done.
452 }
453 return width;
454 }
455
456 // Gets the glyph width for |word_range|, and splits the |word| into different
457 // segments based on its runs.
458 SkScalar GetWordWidth(const Range& word_range,
459 std::vector<internal::LineSegment>* segments) const {
460 DCHECK(words_);
461 if (word_range.is_empty() || segments == nullptr)
462 return 0;
463 size_t run_start_index = run_list_.GetRunIndexAt(word_range.start());
464 size_t run_end_index = run_list_.GetRunIndexAt(word_range.end() - 1);
465 SkScalar width = 0;
466 for (size_t idx = run_start_index; idx <= run_end_index; idx++) {
msw 2015/05/28 18:16:16 nit: why |idx| instead of |i|?
xdai1 2015/06/01 16:51:15 Done.
467 const internal::TextRunHarfBuzz& run = *(run_list_.runs()[idx]);
468 const Range char_range = run.range.Intersect(word_range);
469 if (char_range.is_empty())
msw 2015/05/28 18:16:15 It seems like this shouldn't be possible, maybe DC
xdai1 2015/06/01 16:51:15 Done.
470 continue;
471 const SkScalar char_width = run.GetGlyphWidthForCharRange(char_range);
472 width += char_width;
473
474 internal::LineSegment segment;
475 segment.run = idx;
476 segment.char_range = char_range;
477 segment.width = char_width;
478 segment.x_range = Range(SkScalarCeilToInt(text_x_ + width - char_width),
479 SkScalarCeilToInt(text_x_ + width));
480 segments->push_back(segment);
481 }
482 return width;
483 }
484
485 // RTL runs are broken in logical order but displayed in visual order. To find
486 // the text-space coordinate (where it would fall in a single-line text)
487 // |x_range| of RTL segments, segment widths are applied in reverse order.
488 // e.g. {[5, 10], [10, 40]} will become {[35, 40], [5, 35]}.
489 void UpdateRTLSegmentRanges() {
490 if (rtl_segments_.empty())
491 return;
492 float x = SegmentFromHandle(rtl_segments_[0])->x_range.start();
493 for (size_t i = rtl_segments_.size(); i > 0; --i) {
494 internal::LineSegment* segment = SegmentFromHandle(rtl_segments_[i - 1]);
495 const float segment_width = segment->width;
496 segment->x_range = Range(x, x + segment_width);
497 x += segment_width;
498 }
499 rtl_segments_.clear();
471 } 500 }
472 501
473 const SkScalar max_width_; 502 const SkScalar max_width_;
474 const int min_baseline_; 503 const int min_baseline_;
475 const float min_height_; 504 const float min_height_;
476 const bool multiline_;
477 const WordWrapBehavior word_wrap_behavior_; 505 const WordWrapBehavior word_wrap_behavior_;
478 const base::string16& text_; 506 const base::string16& text_;
479 const BreakList<size_t>* const words_; 507 const BreakList<size_t>* const words_;
480 const internal::TextRunList& run_list_; 508 const internal::TextRunList& run_list_;
481 509
482 // Stores the resulting lines. 510 // Stores the resulting lines.
483 std::vector<internal::Line> lines_; 511 std::vector<internal::Line> lines_;
484 512
485 // Text space and line space x coordinates of the next segment to be added.
486 SkScalar text_x_;
487 SkScalar line_x_;
488
489 float max_descent_; 513 float max_descent_;
490 float max_ascent_; 514 float max_ascent_;
491 515
516 // Text space x coordinates of the next segment to be added.
517 SkScalar text_x_;
518 // Stores available width in the current line.
519 SkScalar available_width_;
msw 2015/05/27 17:37:59 If |available_width_| == |max_width_ - text_x_|, w
xdai1 2015/06/01 16:51:15 |available_width_| will be reset when advancing a
520
492 // Size of the multiline text, not including the currently processed line. 521 // Size of the multiline text, not including the currently processed line.
493 SizeF total_size_; 522 SizeF total_size_;
494 523
495 // The current RTL run segments, to be applied by |UpdateRTLSegmentRanges()|. 524 // The current RTL run segments, to be applied by |UpdateRTLSegmentRanges()|.
496 std::vector<SegmentHandle> rtl_segments_; 525 std::vector<SegmentHandle> rtl_segments_;
497 526
498 DISALLOW_COPY_AND_ASSIGN(HarfBuzzLineBreaker); 527 DISALLOW_COPY_AND_ASSIGN(HarfBuzzLineBreaker);
499 }; 528 };
500 529
501 // Function object for case insensitive string comparison. 530 // Function object for case insensitive string comparison.
(...skipping 23 matching lines...) Expand all
525 baseline_offset(0), 554 baseline_offset(0),
526 baseline_type(0), 555 baseline_type(0),
527 font_style(0), 556 font_style(0),
528 strike(false), 557 strike(false),
529 diagonal_strike(false), 558 diagonal_strike(false),
530 underline(false) { 559 underline(false) {
531 } 560 }
532 561
533 TextRunHarfBuzz::~TextRunHarfBuzz() {} 562 TextRunHarfBuzz::~TextRunHarfBuzz() {}
534 563
535 void TextRunHarfBuzz::GetClusterAt(size_t pos,
536 Range* chars,
537 Range* glyphs) const {
538 DCHECK(range.Contains(Range(pos, pos + 1)));
539 DCHECK(chars);
540 DCHECK(glyphs);
541
542 if (glyph_count == 0) {
543 *chars = range;
544 *glyphs = Range();
545 return;
546 }
547
548 if (is_rtl) {
549 GetClusterAtImpl(pos, range, glyph_to_char.rbegin(), glyph_to_char.rend(),
550 true, chars, glyphs);
551 return;
552 }
553
554 GetClusterAtImpl(pos, range, glyph_to_char.begin(), glyph_to_char.end(),
555 false, chars, glyphs);
556 }
557
558 Range TextRunHarfBuzz::CharRangeToGlyphRange(const Range& char_range) const { 564 Range TextRunHarfBuzz::CharRangeToGlyphRange(const Range& char_range) const {
559 DCHECK(range.Contains(char_range)); 565 DCHECK(range.Contains(char_range));
560 DCHECK(!char_range.is_reversed()); 566 DCHECK(!char_range.is_reversed());
561 DCHECK(!char_range.is_empty()); 567 DCHECK(!char_range.is_empty());
562 568
563 Range start_glyphs; 569 Range start_glyphs;
564 Range end_glyphs; 570 Range end_glyphs;
565 Range temp_range; 571 Range temp_range;
566 GetClusterAt(char_range.start(), &temp_range, &start_glyphs); 572 GetClusterAt(char_range.start(), &temp_range, &start_glyphs);
567 GetClusterAt(char_range.end() - 1, &temp_range, &end_glyphs); 573 GetClusterAt(char_range.end() - 1, &temp_range, &end_glyphs);
568 574
569 return is_rtl ? Range(end_glyphs.start(), start_glyphs.end()) : 575 return is_rtl ? Range(end_glyphs.start(), start_glyphs.end()) :
570 Range(start_glyphs.start(), end_glyphs.end()); 576 Range(start_glyphs.start(), end_glyphs.end());
571 } 577 }
572 578
573 size_t TextRunHarfBuzz::CountMissingGlyphs() const { 579 size_t TextRunHarfBuzz::CountMissingGlyphs() const {
574 static const int kMissingGlyphId = 0; 580 static const int kMissingGlyphId = 0;
575 size_t missing = 0; 581 size_t missing = 0;
576 for (size_t i = 0; i < glyph_count; ++i) 582 for (size_t i = 0; i < glyph_count; ++i)
577 missing += (glyphs[i] == kMissingGlyphId) ? 1 : 0; 583 missing += (glyphs[i] == kMissingGlyphId) ? 1 : 0;
578 return missing; 584 return missing;
579 } 585 }
580 586
587 void TextRunHarfBuzz::GetClusterAt(size_t pos,
588 Range* chars,
589 Range* glyphs) const {
590 DCHECK(range.Contains(Range(pos, pos + 1)));
591 DCHECK(chars);
592 DCHECK(glyphs);
593
594 if (glyph_count == 0) {
595 *chars = range;
596 *glyphs = Range();
597 return;
598 }
599
600 if (is_rtl) {
601 GetClusterAtImpl(pos, range, glyph_to_char.rbegin(), glyph_to_char.rend(),
602 true, chars, glyphs);
603 return;
604 }
605
606 GetClusterAtImpl(pos, range, glyph_to_char.begin(), glyph_to_char.end(),
607 false, chars, glyphs);
608 }
609
581 RangeF TextRunHarfBuzz::GetGraphemeBounds( 610 RangeF TextRunHarfBuzz::GetGraphemeBounds(
582 base::i18n::BreakIterator* grapheme_iterator, 611 base::i18n::BreakIterator* grapheme_iterator,
583 size_t text_index) { 612 size_t text_index) {
584 DCHECK_LT(text_index, range.end()); 613 DCHECK_LT(text_index, range.end());
585 if (glyph_count == 0) 614 if (glyph_count == 0)
586 return RangeF(preceding_run_widths, preceding_run_widths + width); 615 return RangeF(preceding_run_widths, preceding_run_widths + width);
587 616
588 Range chars; 617 Range chars;
589 Range glyphs; 618 Range glyphs;
590 GetClusterAt(text_index, &chars, &glyphs); 619 GetClusterAt(text_index, &chars, &glyphs);
(...skipping 28 matching lines...) Expand all
619 cluster_width * (before + 1) / static_cast<float>(total)); 648 cluster_width * (before + 1) / static_cast<float>(total));
620 return RangeF(preceding_run_widths + grapheme_begin_x, 649 return RangeF(preceding_run_widths + grapheme_begin_x,
621 preceding_run_widths + grapheme_end_x); 650 preceding_run_widths + grapheme_end_x);
622 } 651 }
623 } 652 }
624 653
625 return RangeF(preceding_run_widths + cluster_begin_x, 654 return RangeF(preceding_run_widths + cluster_begin_x,
626 preceding_run_widths + cluster_end_x); 655 preceding_run_widths + cluster_end_x);
627 } 656 }
628 657
658 SkScalar TextRunHarfBuzz::GetGlyphWidthForCharRange(
659 const Range& char_range) const {
660 DCHECK(range.Contains(char_range));
661 Range glyph_range = CharRangeToGlyphRange(char_range);
662 return ((glyph_range.end() >= glyph_count)
msw 2015/05/27 17:37:59 When would the glyph range include anything beyond
xdai1 2015/06/01 16:51:15 Yes, it is not possible that glyph_range.end() inc
663 ? SkFloatToScalar(width)
msw 2015/05/27 17:37:59 This seems wrong; if the glyph_range is (4, 7) and
xdai1 2015/06/01 16:51:14 It will not return the full run width, it will ret
664 : positions[glyph_range.end()].x()) -
665 positions[glyph_range.start()].x();
msw 2015/05/27 17:37:59 nit: this indentation seems wrong.
xdai1 2015/06/01 16:51:15 It seems right. It's not inside of the conditional
666 }
667
629 TextRunList::TextRunList() : width_(0.0f) {} 668 TextRunList::TextRunList() : width_(0.0f) {}
630 669
631 TextRunList::~TextRunList() {} 670 TextRunList::~TextRunList() {}
632 671
633 void TextRunList::Reset() { 672 void TextRunList::Reset() {
634 runs_.clear(); 673 runs_.clear();
635 width_ = 0.0f; 674 width_ = 0.0f;
636 } 675 }
637 676
638 void TextRunList::InitIndexMap() { 677 void TextRunList::InitIndexMap() {
(...skipping 14 matching lines...) Expand all
653 void TextRunList::ComputePrecedingRunWidths() { 692 void TextRunList::ComputePrecedingRunWidths() {
654 // Precalculate run width information. 693 // Precalculate run width information.
655 width_ = 0.0f; 694 width_ = 0.0f;
656 for (size_t i = 0; i < runs_.size(); ++i) { 695 for (size_t i = 0; i < runs_.size(); ++i) {
657 TextRunHarfBuzz* run = runs_[visual_to_logical_[i]]; 696 TextRunHarfBuzz* run = runs_[visual_to_logical_[i]];
658 run->preceding_run_widths = width_; 697 run->preceding_run_widths = width_;
659 width_ += run->width; 698 width_ += run->width;
660 } 699 }
661 } 700 }
662 701
702 size_t TextRunList::GetRunIndexAt(size_t position) const {
msw 2015/05/27 17:37:59 Hmm, it'd be nice if this could share logic with G
xdai1 2015/06/01 16:51:14 Sorry, I didn't see how can we do that...
703 for (size_t i = 0; i < runs_.size(); ++i) {
704 if (runs_[i]->range.start() <= position && runs_[i]->range.end() > position)
705 return i;
706 }
707 return runs_.size();
708 }
709
663 } // namespace internal 710 } // namespace internal
664 711
665 RenderTextHarfBuzz::RenderTextHarfBuzz() 712 RenderTextHarfBuzz::RenderTextHarfBuzz()
666 : RenderText(), 713 : RenderText(),
667 update_layout_run_list_(false), 714 update_layout_run_list_(false),
668 update_display_run_list_(false), 715 update_display_run_list_(false),
669 update_grapheme_iterator_(false), 716 update_grapheme_iterator_(false),
670 update_display_text_(false), 717 update_display_text_(false),
671 glyph_width_for_test_(0u) { 718 glyph_width_for_test_(0u) {
672 set_truncate_length(kMaxTextLength); 719 set_truncate_length(kMaxTextLength);
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1001 if (lines().empty()) { 1048 if (lines().empty()) {
1002 // TODO(ckocagil): Remove ScopedTracker below once crbug.com/441028 is 1049 // TODO(ckocagil): Remove ScopedTracker below once crbug.com/441028 is
1003 // fixed. 1050 // fixed.
1004 scoped_ptr<tracked_objects::ScopedTracker> tracking_profile( 1051 scoped_ptr<tracked_objects::ScopedTracker> tracking_profile(
1005 new tracked_objects::ScopedTracker( 1052 new tracked_objects::ScopedTracker(
1006 FROM_HERE_WITH_EXPLICIT_FUNCTION("441028 HarfBuzzLineBreaker"))); 1053 FROM_HERE_WITH_EXPLICIT_FUNCTION("441028 HarfBuzzLineBreaker")));
1007 1054
1008 internal::TextRunList* run_list = GetRunList(); 1055 internal::TextRunList* run_list = GetRunList();
1009 HarfBuzzLineBreaker line_breaker( 1056 HarfBuzzLineBreaker line_breaker(
1010 display_rect().width(), font_list().GetBaseline(), 1057 display_rect().width(), font_list().GetBaseline(),
1011 std::max(font_list().GetHeight(), min_line_height()), multiline(), 1058 std::max(font_list().GetHeight(), min_line_height()),
1012 word_wrap_behavior(), GetDisplayText(), 1059 word_wrap_behavior(), GetDisplayText(),
1013 multiline() ? &GetLineBreaks() : nullptr, *run_list); 1060 multiline() ? &GetLineBreaks() : nullptr, *run_list);
1014 1061
1015 tracking_profile.reset(); 1062 tracking_profile.reset();
1016 1063
1017 for (size_t i = 0; i < run_list->size(); ++i) 1064 if (multiline())
1018 line_breaker.AddRun(i); 1065 line_breaker.ConstructMultiLines();
1066 else
1067 line_breaker.ConstructSingleLine();
1019 std::vector<internal::Line> lines; 1068 std::vector<internal::Line> lines;
1020 line_breaker.Finalize(&lines, &total_size_); 1069 line_breaker.FinalizeLines(&lines, &total_size_);
1021 set_lines(&lines); 1070 set_lines(&lines);
1022 } 1071 }
1023 } 1072 }
1024 1073
1025 void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) { 1074 void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) {
1026 internal::SkiaTextRenderer renderer(canvas); 1075 internal::SkiaTextRenderer renderer(canvas);
1027 DrawVisualTextInternal(&renderer); 1076 DrawVisualTextInternal(&renderer);
1028 } 1077 }
1029 1078
1030 void RenderTextHarfBuzz::DrawVisualTextInternal( 1079 void RenderTextHarfBuzz::DrawVisualTextInternal(
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
1479 DCHECK(!update_layout_run_list_); 1528 DCHECK(!update_layout_run_list_);
1480 DCHECK(!update_display_run_list_); 1529 DCHECK(!update_display_run_list_);
1481 return text_elided() ? display_run_list_.get() : &layout_run_list_; 1530 return text_elided() ? display_run_list_.get() : &layout_run_list_;
1482 } 1531 }
1483 1532
1484 const internal::TextRunList* RenderTextHarfBuzz::GetRunList() const { 1533 const internal::TextRunList* RenderTextHarfBuzz::GetRunList() const {
1485 return const_cast<RenderTextHarfBuzz*>(this)->GetRunList(); 1534 return const_cast<RenderTextHarfBuzz*>(this)->GetRunList();
1486 } 1535 }
1487 1536
1488 } // namespace gfx 1537 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698