Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 const bool block_break = current_block != first_block && | 83 const bool block_break = current_block != first_block && |
| 84 (first_block_unusual || IsUnusualBlockCode(current_block)); | 84 (first_block_unusual || IsUnusualBlockCode(current_block)); |
| 85 if (block_break || current_char == '\n' || | 85 if (block_break || current_char == '\n' || |
| 86 first_bracket != IsBracket(current_char)) { | 86 first_bracket != IsBracket(current_char)) { |
| 87 return run_start + iter.array_pos(); | 87 return run_start + iter.array_pos(); |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 return run_break; | 90 return run_break; |
| 91 } | 91 } |
| 92 | 92 |
| 93 // If the given scripts match, returns the one that isn't USCRIPT_COMMON or | 93 // If the given scripts match, returns the one that isn't USCRIPT_INHERITED, |
| 94 // USCRIPT_INHERITED, i.e. the more specific one. Otherwise returns | 94 // i.e. the more specific one. Otherwise returns USCRIPT_INVALID_CODE. |
| 95 // USCRIPT_INVALID_CODE. | |
| 96 UScriptCode ScriptIntersect(UScriptCode first, UScriptCode second) { | 95 UScriptCode ScriptIntersect(UScriptCode first, UScriptCode second) { |
| 97 if (first == second || | 96 if (first == second || second == USCRIPT_INHERITED) |
| 98 (second > USCRIPT_INVALID_CODE && second <= USCRIPT_INHERITED)) { | |
| 99 return first; | 97 return first; |
| 100 } | 98 if (first == USCRIPT_INHERITED) |
| 101 if (first > USCRIPT_INVALID_CODE && first <= USCRIPT_INHERITED) | |
| 102 return second; | 99 return second; |
| 103 return USCRIPT_INVALID_CODE; | 100 return USCRIPT_INVALID_CODE; |
| 104 } | 101 } |
| 105 | 102 |
| 106 // Writes the script and the script extensions of the character with the | 103 // Writes the script and the script extensions of the character with the |
| 107 // Unicode |codepoint|. Returns the number of written scripts. | 104 // Unicode |codepoint|. Returns the number of written scripts. |
| 108 int GetScriptExtensions(UChar32 codepoint, UScriptCode* scripts) { | 105 int GetScriptExtensions(UChar32 codepoint, UScriptCode* scripts) { |
| 109 UErrorCode icu_error = U_ZERO_ERROR; | 106 UErrorCode icu_error = U_ZERO_ERROR; |
| 110 // ICU documentation incorrectly states that the result of | 107 // ICU documentation incorrectly states that the result of |
| 111 // |uscript_getScriptExtensions| will contain the regular script property. | 108 // |uscript_getScriptExtensions| will contain the regular script property. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 UScriptCode* script) { | 156 UScriptCode* script) { |
| 160 DCHECK_GT(length, 0U); | 157 DCHECK_GT(length, 0U); |
| 161 | 158 |
| 162 UScriptCode scripts[kMaxScripts] = { USCRIPT_INVALID_CODE }; | 159 UScriptCode scripts[kMaxScripts] = { USCRIPT_INVALID_CODE }; |
| 163 | 160 |
| 164 base::i18n::UTF16CharIterator char_iterator(text.c_str() + start, length); | 161 base::i18n::UTF16CharIterator char_iterator(text.c_str() + start, length); |
| 165 size_t scripts_size = GetScriptExtensions(char_iterator.get(), scripts); | 162 size_t scripts_size = GetScriptExtensions(char_iterator.get(), scripts); |
| 166 *script = scripts[0]; | 163 *script = scripts[0]; |
| 167 | 164 |
| 168 while (char_iterator.Advance()) { | 165 while (char_iterator.Advance()) { |
| 169 ScriptSetIntersect(char_iterator.get(), scripts, &scripts_size); | 166 ScriptSetIntersect(char_iterator.get(), scripts, &scripts_size); |
|
Jun Mukai
2015/05/06 20:47:55
You should move ScriptSetIntersect below of if-cla
| |
| 167 // Special handling to merge white space into the previous run. | |
| 168 if (u_isUWhiteSpace(char_iterator.get())) | |
| 169 continue; | |
| 170 if (scripts_size == 0U) | 170 if (scripts_size == 0U) |
| 171 return char_iterator.array_pos(); | 171 return char_iterator.array_pos(); |
| 172 *script = scripts[0]; | 172 *script = scripts[0]; |
| 173 } | 173 } |
| 174 | 174 |
| 175 return length; | 175 return length; |
| 176 } | 176 } |
| 177 | 177 |
| 178 // A port of hb_icu_script_to_script because harfbuzz on CrOS is built without | 178 // A port of hb_icu_script_to_script because harfbuzz on CrOS is built without |
| 179 // hb-icu. See http://crbug.com/356929 | 179 // hb-icu. See http://crbug.com/356929 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 min_baseline_(min_baseline), | 232 min_baseline_(min_baseline), |
| 233 min_height_(min_height), | 233 min_height_(min_height), |
| 234 multiline_(multiline), | 234 multiline_(multiline), |
| 235 word_wrap_behavior_(word_wrap_behavior), | 235 word_wrap_behavior_(word_wrap_behavior), |
| 236 text_(text), | 236 text_(text), |
| 237 words_(words), | 237 words_(words), |
| 238 run_list_(run_list), | 238 run_list_(run_list), |
| 239 text_x_(0), | 239 text_x_(0), |
| 240 line_x_(0), | 240 line_x_(0), |
| 241 max_descent_(0), | 241 max_descent_(0), |
| 242 max_ascent_(0) { | 242 max_ascent_(0), |
| 243 word_end_pos_(0), | |
| 244 word_width_(0) { | |
| 243 DCHECK_EQ(multiline_, (words_ != nullptr)); | 245 DCHECK_EQ(multiline_, (words_ != nullptr)); |
| 244 AdvanceLine(); | 246 AdvanceLine(); |
| 245 } | 247 } |
| 246 | 248 |
| 247 // Breaks the run at given |run_index| into Line structs. | 249 // Breaks the run at given |run_index| into Line structs. |
| 248 void AddRun(int run_index) { | 250 void AddRun(int run_index) { |
| 249 const internal::TextRunHarfBuzz* run = run_list_.runs()[run_index]; | 251 const internal::TextRunHarfBuzz* run = run_list_.runs()[run_index]; |
| 250 base::char16 first_char = text_[run->range.start()]; | 252 base::char16 first_char = text_[run->range.start()]; |
| 253 | |
| 254 if (word_wrap_behavior_ == TRUNCATE_LONG_WORDS && words_) { | |
| 255 // If the run has been processed, skip it. | |
| 256 BreakList<size_t>::const_iterator next_word = | |
| 257 words_->GetBreak(run->range.start()) + 1; | |
| 258 size_t current_word_end_pos = | |
| 259 next_word == words_->breaks().end() ? text_.size() : next_word->first; | |
| 260 if (current_word_end_pos <= word_end_pos_ && first_char != '\n') { | |
| 261 word_width_ = 0; | |
| 262 return; | |
| 263 } | |
| 264 } | |
| 265 | |
| 251 if (multiline_ && first_char == '\n') { | 266 if (multiline_ && first_char == '\n') { |
| 252 AdvanceLine(); | 267 AdvanceLine(); |
| 253 } else if (multiline_ && (line_x_ + SkFloatToScalar(run->width)) > | 268 } else if (multiline_ && (line_x_ + SkFloatToScalar(run->width)) > |
| 254 max_width_) { | 269 max_width_) { |
| 255 BreakRun(run_index); | 270 BreakRun(run_index); |
| 256 } else { | 271 } else { |
| 257 AddSegment(run_index, run->range, run->width); | 272 AddSegment(run_index, run->range, run->width); |
| 258 } | 273 } |
| 259 } | 274 } |
| 260 | 275 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 bool BreakRunAtWidth(const internal::TextRunHarfBuzz& run, | 325 bool BreakRunAtWidth(const internal::TextRunHarfBuzz& run, |
| 311 size_t start_char, | 326 size_t start_char, |
| 312 SkScalar* width, | 327 SkScalar* width, |
| 313 size_t* end_char, | 328 size_t* end_char, |
| 314 size_t* next_char) { | 329 size_t* next_char) { |
| 315 DCHECK(words_); | 330 DCHECK(words_); |
| 316 DCHECK(run.range.Contains(Range(start_char, start_char + 1))); | 331 DCHECK(run.range.Contains(Range(start_char, start_char + 1))); |
| 317 SkScalar available_width = max_width_ - line_x_; | 332 SkScalar available_width = max_width_ - line_x_; |
| 318 BreakList<size_t>::const_iterator word = words_->GetBreak(start_char); | 333 BreakList<size_t>::const_iterator word = words_->GetBreak(start_char); |
| 319 BreakList<size_t>::const_iterator next_word = word + 1; | 334 BreakList<size_t>::const_iterator next_word = word + 1; |
| 320 // Width from |std::max(word->first, start_char)| to the current character. | 335 word_end_pos_ = |
| 321 SkScalar word_width = 0; | 336 (next_word == words_->breaks().end()) ? text_.size() : next_word->first; |
| 322 *width = 0; | 337 *width = 0; |
| 323 | 338 |
| 324 Range char_range; | 339 Range char_range; |
| 325 SkScalar truncated_width = 0; | 340 SkScalar truncated_width = 0; |
| 326 for (size_t i = start_char; i < run.range.end(); i += char_range.length()) { | 341 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 | 342 // |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| | 343 // the word boundary right after |i|. Advance both |word| and |next_word| |
| 329 // when |i| reaches |next_word|. | 344 // when |i| reaches |next_word|. |
| 330 if (next_word != words_->breaks().end() && i >= next_word->first) { | 345 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++; | 346 word = next_word++; |
| 341 word_width = 0; | 347 word_end_pos_ = (next_word == words_->breaks().end()) |
| 348 ? text_.size() | |
| 349 : next_word->first; | |
| 350 word_width_ = 0; | |
| 342 } | 351 } |
| 343 | 352 |
| 344 Range glyph_range; | 353 Range glyph_range; |
| 345 run.GetClusterAt(i, &char_range, &glyph_range); | 354 run.GetClusterAt(i, &char_range, &glyph_range); |
| 346 DCHECK_LT(0U, char_range.length()); | 355 DCHECK_LT(0U, char_range.length()); |
| 347 | 356 |
| 348 SkScalar char_width = ((glyph_range.end() >= run.glyph_count) | 357 SkScalar char_width = ((glyph_range.end() >= run.glyph_count) |
| 349 ? SkFloatToScalar(run.width) | 358 ? SkFloatToScalar(run.width) |
| 350 : run.positions[glyph_range.end()].x()) - | 359 : run.positions[glyph_range.end()].x()) - |
| 351 run.positions[glyph_range.start()].x(); | 360 run.positions[glyph_range.start()].x(); |
| 352 | 361 |
| 353 *width += char_width; | 362 *width += char_width; |
| 354 word_width += char_width; | 363 word_width_ += char_width; |
| 355 | 364 |
| 356 // TODO(mukai): implement ELIDE_LONG_WORDS. | 365 // TODO(mukai): implement ELIDE_LONG_WORDS. |
| 357 if (*width > available_width) { | 366 if (*width > available_width) { |
| 358 if (line_x_ != 0 || word_width < *width) { | 367 if ((line_x_ != 0 && word_width_ <= *width) || |
| 368 (line_x_ == 0 && word_width_ < *width)) { | |
| 359 // Roll back one word. | 369 // Roll back one word. |
| 360 *width -= word_width; | 370 *width -= word_width_; |
| 361 *next_char = std::max(word->first, start_char); | 371 *next_char = std::max(word->first, start_char); |
| 362 *end_char = *next_char; | 372 *end_char = *next_char; |
| 373 word_width_ = 0; | |
| 363 return true; | 374 return true; |
| 364 } else if (word_wrap_behavior_ == WRAP_LONG_WORDS) { | 375 } else if (word_wrap_behavior_ == WRAP_LONG_WORDS) { |
| 365 if (char_width < *width) { | 376 if (char_width < *width) { |
| 366 // Roll back one character. | 377 // Roll back one character. |
| 367 *width -= char_width; | 378 *width -= char_width; |
| 368 *next_char = i; | 379 *next_char = i; |
| 369 } else { | 380 } else { |
| 370 // Continue from the next character. | 381 // Continue from the next character. |
| 371 *next_char = i + char_range.length(); | 382 *next_char = i + char_range.length(); |
| 372 } | 383 } |
| 373 *end_char = *next_char; | 384 *end_char = *next_char; |
| 385 word_width_ = 0; | |
| 386 return true; | |
| 387 } else if (word_wrap_behavior_ == TRUNCATE_LONG_WORDS) { | |
| 388 *width = truncated_width; | |
| 389 *next_char = word_end_pos_; | |
| 390 word_width_ = 0; | |
| 374 return true; | 391 return true; |
| 375 } | 392 } |
| 376 } else { | 393 } else { |
| 377 *end_char = char_range.end(); | 394 *end_char = char_range.end(); |
| 378 truncated_width = *width; | 395 truncated_width = *width; |
| 379 } | 396 } |
| 380 } | 397 } |
| 381 | 398 |
| 382 if (word_wrap_behavior_ == TRUNCATE_LONG_WORDS) | 399 if (word_wrap_behavior_ == TRUNCATE_LONG_WORDS) |
| 383 *width = truncated_width; | 400 *width = truncated_width; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 416 line->size.set_height(std::max(min_height_, max_descent_ + max_ascent_)); | 433 line->size.set_height(std::max(min_height_, max_descent_ + max_ascent_)); |
| 417 line->baseline = | 434 line->baseline = |
| 418 std::max(min_baseline_, SkScalarRoundToInt(max_ascent_)); | 435 std::max(min_baseline_, SkScalarRoundToInt(max_ascent_)); |
| 419 line->preceding_heights = std::ceil(total_size_.height()); | 436 line->preceding_heights = std::ceil(total_size_.height()); |
| 420 total_size_.set_height(total_size_.height() + line->size.height()); | 437 total_size_.set_height(total_size_.height() + line->size.height()); |
| 421 total_size_.set_width(std::max(total_size_.width(), line->size.width())); | 438 total_size_.set_width(std::max(total_size_.width(), line->size.width())); |
| 422 } | 439 } |
| 423 max_descent_ = 0; | 440 max_descent_ = 0; |
| 424 max_ascent_ = 0; | 441 max_ascent_ = 0; |
| 425 line_x_ = 0; | 442 line_x_ = 0; |
| 443 word_width_ = 0; | |
| 426 lines_.push_back(internal::Line()); | 444 lines_.push_back(internal::Line()); |
| 427 } | 445 } |
| 428 | 446 |
| 429 // Adds a new segment with the given properties to |lines_.back()|. | 447 // Adds a new segment with the given properties to |lines_.back()|. |
| 430 void AddSegment(int run_index, Range char_range, float width) { | 448 void AddSegment(int run_index, Range char_range, float width) { |
| 431 if (char_range.is_empty()) { | 449 if (char_range.is_empty()) { |
| 432 DCHECK_EQ(0, width); | 450 DCHECK_EQ(0, width); |
| 433 return; | 451 return; |
| 434 } | 452 } |
| 435 const internal::TextRunHarfBuzz& run = *(run_list_.runs()[run_index]); | 453 const internal::TextRunHarfBuzz& run = *(run_list_.runs()[run_index]); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 461 if (run.is_rtl) { | 479 if (run.is_rtl) { |
| 462 rtl_segments_.push_back( | 480 rtl_segments_.push_back( |
| 463 SegmentHandle(lines_.size() - 1, line->segments.size() - 1)); | 481 SegmentHandle(lines_.size() - 1, line->segments.size() - 1)); |
| 464 // If this is the last segment of an RTL run, reprocess the text-space x | 482 // If this is the last segment of an RTL run, reprocess the text-space x |
| 465 // ranges of all segments from the run. | 483 // ranges of all segments from the run. |
| 466 if (char_range.end() == run.range.end()) | 484 if (char_range.end() == run.range.end()) |
| 467 UpdateRTLSegmentRanges(); | 485 UpdateRTLSegmentRanges(); |
| 468 } | 486 } |
| 469 text_x_ += SkFloatToScalar(width); | 487 text_x_ += SkFloatToScalar(width); |
| 470 line_x_ += SkFloatToScalar(width); | 488 line_x_ += SkFloatToScalar(width); |
| 489 | |
| 490 // Calculate |word_width_|. | |
| 491 if (words_) { | |
| 492 BreakList<size_t>::const_iterator word_for_current_run = | |
| 493 words_->GetBreak(char_range.start()); | |
| 494 BreakList<size_t>::const_iterator word_for_next_run = | |
| 495 (char_range.end() == text_.size()) | |
| 496 ? words_->breaks().end() | |
| 497 : words_->GetBreak(char_range.end()); | |
| 498 | |
| 499 if (word_width_ == 0 && word_for_current_run == word_for_next_run) { | |
| 500 Range glyph_range = run.CharRangeToGlyphRange(char_range); | |
| 501 SkScalar char_width = ((glyph_range.end() >= run.glyph_count) | |
| 502 ? SkFloatToScalar(run.width) | |
| 503 : run.positions[glyph_range.end()].x()) - | |
| 504 run.positions[glyph_range.start()].x(); | |
| 505 word_width_ += char_width; | |
| 506 } else if (word_for_current_run != word_for_next_run) { | |
| 507 word_width_ = 0; | |
| 508 } | |
| 509 } | |
| 471 } | 510 } |
| 472 | 511 |
| 473 const SkScalar max_width_; | 512 const SkScalar max_width_; |
| 474 const int min_baseline_; | 513 const int min_baseline_; |
| 475 const float min_height_; | 514 const float min_height_; |
| 476 const bool multiline_; | 515 const bool multiline_; |
| 477 const WordWrapBehavior word_wrap_behavior_; | 516 const WordWrapBehavior word_wrap_behavior_; |
| 478 const base::string16& text_; | 517 const base::string16& text_; |
| 479 const BreakList<size_t>* const words_; | 518 const BreakList<size_t>* const words_; |
| 480 const internal::TextRunList& run_list_; | 519 const internal::TextRunList& run_list_; |
| 481 | 520 |
| 482 // Stores the resulting lines. | 521 // Stores the resulting lines. |
| 483 std::vector<internal::Line> lines_; | 522 std::vector<internal::Line> lines_; |
| 484 | 523 |
| 485 // Text space and line space x coordinates of the next segment to be added. | 524 // Text space and line space x coordinates of the next segment to be added. |
| 486 SkScalar text_x_; | 525 SkScalar text_x_; |
| 487 SkScalar line_x_; | 526 SkScalar line_x_; |
| 488 | 527 |
| 489 float max_descent_; | 528 float max_descent_; |
| 490 float max_ascent_; | 529 float max_ascent_; |
| 491 | 530 |
| 531 // Stores the end position of the current word in process. | |
| 532 size_t word_end_pos_; | |
| 533 // Stores the glyph width between the current position and the start position | |
| 534 // of the current word. If the current position is pointing to the first | |
| 535 // character of the current word, |word_width_| is set to 0. | |
| 536 SkScalar word_width_; | |
| 537 | |
| 492 // Size of the multiline text, not including the currently processed line. | 538 // Size of the multiline text, not including the currently processed line. |
| 493 SizeF total_size_; | 539 SizeF total_size_; |
| 494 | 540 |
| 495 // The current RTL run segments, to be applied by |UpdateRTLSegmentRanges()|. | 541 // The current RTL run segments, to be applied by |UpdateRTLSegmentRanges()|. |
| 496 std::vector<SegmentHandle> rtl_segments_; | 542 std::vector<SegmentHandle> rtl_segments_; |
| 497 | 543 |
| 498 DISALLOW_COPY_AND_ASSIGN(HarfBuzzLineBreaker); | 544 DISALLOW_COPY_AND_ASSIGN(HarfBuzzLineBreaker); |
| 499 }; | 545 }; |
| 500 | 546 |
| 501 // Function object for case insensitive string comparison. | 547 // Function object for case insensitive string comparison. |
| (...skipping 977 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1479 DCHECK(!update_layout_run_list_); | 1525 DCHECK(!update_layout_run_list_); |
| 1480 DCHECK(!update_display_run_list_); | 1526 DCHECK(!update_display_run_list_); |
| 1481 return text_elided() ? display_run_list_.get() : &layout_run_list_; | 1527 return text_elided() ? display_run_list_.get() : &layout_run_list_; |
| 1482 } | 1528 } |
| 1483 | 1529 |
| 1484 const internal::TextRunList* RenderTextHarfBuzz::GetRunList() const { | 1530 const internal::TextRunList* RenderTextHarfBuzz::GetRunList() const { |
| 1485 return const_cast<RenderTextHarfBuzz*>(this)->GetRunList(); | 1531 return const_cast<RenderTextHarfBuzz*>(this)->GetRunList(); |
| 1486 } | 1532 } |
| 1487 | 1533 |
| 1488 } // namespace gfx | 1534 } // namespace gfx |
| OLD | NEW |