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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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); |
| 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 = next_word == words_->breaks().end() | |
| 259 ? text_.size() | |
| 260 : next_word->first; | |
| 261 if (current_word_end_pos <= word_end_pos_ && first_char != '\n') { | |
|
Jun Mukai
2015/05/06 17:38:52
This will ignore too much if TRUNCATE_LONG_WORDS i
xdai1
2015/05/06 20:23:44
Because of the way we handle the white space, it w
Jun Mukai
2015/05/06 20:47:55
Wait. Do we split runs by whitespaces? Don't we w
| |
| 262 word_width_ = 0; | |
| 263 return; | |
| 264 } | |
| 265 } | |
| 266 | |
| 251 if (multiline_ && first_char == '\n') { | 267 if (multiline_ && first_char == '\n') { |
| 252 AdvanceLine(); | 268 AdvanceLine(); |
| 253 } else if (multiline_ && (line_x_ + SkFloatToScalar(run->width)) > | 269 } else if (multiline_ && (line_x_ + SkFloatToScalar(run->width)) > |
| 254 max_width_) { | 270 max_width_) { |
| 255 BreakRun(run_index); | 271 BreakRun(run_index); |
| 256 } else { | 272 } else { |
| 257 AddSegment(run_index, run->range, run->width); | 273 AddSegment(run_index, run->range, run->width); |
| 258 } | 274 } |
| 259 } | 275 } |
| 260 | 276 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 bool BreakRunAtWidth(const internal::TextRunHarfBuzz& run, | 326 bool BreakRunAtWidth(const internal::TextRunHarfBuzz& run, |
| 311 size_t start_char, | 327 size_t start_char, |
| 312 SkScalar* width, | 328 SkScalar* width, |
| 313 size_t* end_char, | 329 size_t* end_char, |
| 314 size_t* next_char) { | 330 size_t* next_char) { |
| 315 DCHECK(words_); | 331 DCHECK(words_); |
| 316 DCHECK(run.range.Contains(Range(start_char, start_char + 1))); | 332 DCHECK(run.range.Contains(Range(start_char, start_char + 1))); |
| 317 SkScalar available_width = max_width_ - line_x_; | 333 SkScalar available_width = max_width_ - line_x_; |
| 318 BreakList<size_t>::const_iterator word = words_->GetBreak(start_char); | 334 BreakList<size_t>::const_iterator word = words_->GetBreak(start_char); |
| 319 BreakList<size_t>::const_iterator next_word = word + 1; | 335 BreakList<size_t>::const_iterator next_word = word + 1; |
| 320 // Width from |std::max(word->first, start_char)| to the current character. | 336 word_end_pos_ = |
| 321 SkScalar word_width = 0; | 337 (next_word == words_->breaks().end()) ? text_.size() : next_word->first; |
| 322 *width = 0; | 338 *width = 0; |
| 323 | 339 |
| 324 Range char_range; | 340 Range char_range; |
| 325 SkScalar truncated_width = 0; | 341 SkScalar truncated_width = 0; |
| 326 for (size_t i = start_char; i < run.range.end(); i += char_range.length()) { | 342 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 | 343 // |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| | 344 // the word boundary right after |i|. Advance both |word| and |next_word| |
| 329 // when |i| reaches |next_word|. | 345 // when |i| reaches |next_word|. |
| 330 if (next_word != words_->breaks().end() && i >= next_word->first) { | 346 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++; | 347 word = next_word++; |
| 341 word_width = 0; | 348 word_end_pos_ = (next_word == words_->breaks().end()) |
| 349 ? text_.size() | |
| 350 : next_word->first; | |
| 351 word_width_ = 0; | |
| 342 } | 352 } |
| 343 | 353 |
| 344 Range glyph_range; | 354 Range glyph_range; |
| 345 run.GetClusterAt(i, &char_range, &glyph_range); | 355 run.GetClusterAt(i, &char_range, &glyph_range); |
| 346 DCHECK_LT(0U, char_range.length()); | 356 DCHECK_LT(0U, char_range.length()); |
| 347 | 357 |
| 348 SkScalar char_width = ((glyph_range.end() >= run.glyph_count) | 358 SkScalar char_width = ((glyph_range.end() >= run.glyph_count) |
| 349 ? SkFloatToScalar(run.width) | 359 ? SkFloatToScalar(run.width) |
| 350 : run.positions[glyph_range.end()].x()) - | 360 : run.positions[glyph_range.end()].x()) - |
| 351 run.positions[glyph_range.start()].x(); | 361 run.positions[glyph_range.start()].x(); |
| 352 | 362 |
| 353 *width += char_width; | 363 *width += char_width; |
| 354 word_width += char_width; | 364 word_width_ += char_width; |
| 355 | 365 |
| 356 // TODO(mukai): implement ELIDE_LONG_WORDS. | 366 // TODO(mukai): implement ELIDE_LONG_WORDS. |
| 357 if (*width > available_width) { | 367 if (*width > available_width) { |
| 358 if (line_x_ != 0 || word_width < *width) { | 368 if ((line_x_ != 0 && word_width_ <= *width) || |
| 369 (line_x_ == 0 && word_width_ < *width)) { | |
| 359 // Roll back one word. | 370 // Roll back one word. |
| 360 *width -= word_width; | 371 *width -= word_width_; |
| 361 *next_char = std::max(word->first, start_char); | 372 *next_char = std::max(word->first, start_char); |
| 362 *end_char = *next_char; | 373 *end_char = *next_char; |
| 374 word_width_ = 0; | |
| 363 return true; | 375 return true; |
| 364 } else if (word_wrap_behavior_ == WRAP_LONG_WORDS) { | 376 } else if (word_wrap_behavior_ == WRAP_LONG_WORDS) { |
| 365 if (char_width < *width) { | 377 if (char_width < *width) { |
| 366 // Roll back one character. | 378 // Roll back one character. |
| 367 *width -= char_width; | 379 *width -= char_width; |
| 368 *next_char = i; | 380 *next_char = i; |
| 369 } else { | 381 } else { |
| 370 // Continue from the next character. | 382 // Continue from the next character. |
| 371 *next_char = i + char_range.length(); | 383 *next_char = i + char_range.length(); |
| 372 } | 384 } |
| 373 *end_char = *next_char; | 385 *end_char = *next_char; |
| 386 word_width_ = 0; | |
| 387 return true; | |
| 388 } else if (word_wrap_behavior_ == TRUNCATE_LONG_WORDS) { | |
| 389 *width = truncated_width; | |
| 390 *next_char = word_end_pos_; | |
| 391 word_width_ = 0; | |
| 374 return true; | 392 return true; |
| 375 } | 393 } |
| 376 } else { | 394 } else { |
| 377 *end_char = char_range.end(); | 395 *end_char = char_range.end(); |
| 378 truncated_width = *width; | 396 truncated_width = *width; |
| 379 } | 397 } |
| 380 } | 398 } |
| 381 | 399 |
| 382 if (word_wrap_behavior_ == TRUNCATE_LONG_WORDS) | 400 if (word_wrap_behavior_ == TRUNCATE_LONG_WORDS) |
| 383 *width = truncated_width; | 401 *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_)); | 434 line->size.set_height(std::max(min_height_, max_descent_ + max_ascent_)); |
| 417 line->baseline = | 435 line->baseline = |
| 418 std::max(min_baseline_, SkScalarRoundToInt(max_ascent_)); | 436 std::max(min_baseline_, SkScalarRoundToInt(max_ascent_)); |
| 419 line->preceding_heights = std::ceil(total_size_.height()); | 437 line->preceding_heights = std::ceil(total_size_.height()); |
| 420 total_size_.set_height(total_size_.height() + line->size.height()); | 438 total_size_.set_height(total_size_.height() + line->size.height()); |
| 421 total_size_.set_width(std::max(total_size_.width(), line->size.width())); | 439 total_size_.set_width(std::max(total_size_.width(), line->size.width())); |
| 422 } | 440 } |
| 423 max_descent_ = 0; | 441 max_descent_ = 0; |
| 424 max_ascent_ = 0; | 442 max_ascent_ = 0; |
| 425 line_x_ = 0; | 443 line_x_ = 0; |
| 444 word_width_ = 0; | |
| 426 lines_.push_back(internal::Line()); | 445 lines_.push_back(internal::Line()); |
| 427 } | 446 } |
| 428 | 447 |
| 429 // Adds a new segment with the given properties to |lines_.back()|. | 448 // Adds a new segment with the given properties to |lines_.back()|. |
| 430 void AddSegment(int run_index, Range char_range, float width) { | 449 void AddSegment(int run_index, Range char_range, float width) { |
| 431 if (char_range.is_empty()) { | 450 if (char_range.is_empty()) { |
| 432 DCHECK_EQ(0, width); | 451 DCHECK_EQ(0, width); |
| 433 return; | 452 return; |
| 434 } | 453 } |
| 435 const internal::TextRunHarfBuzz& run = *(run_list_.runs()[run_index]); | 454 const internal::TextRunHarfBuzz& run = *(run_list_.runs()[run_index]); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 461 if (run.is_rtl) { | 480 if (run.is_rtl) { |
| 462 rtl_segments_.push_back( | 481 rtl_segments_.push_back( |
| 463 SegmentHandle(lines_.size() - 1, line->segments.size() - 1)); | 482 SegmentHandle(lines_.size() - 1, line->segments.size() - 1)); |
| 464 // If this is the last segment of an RTL run, reprocess the text-space x | 483 // If this is the last segment of an RTL run, reprocess the text-space x |
| 465 // ranges of all segments from the run. | 484 // ranges of all segments from the run. |
| 466 if (char_range.end() == run.range.end()) | 485 if (char_range.end() == run.range.end()) |
| 467 UpdateRTLSegmentRanges(); | 486 UpdateRTLSegmentRanges(); |
| 468 } | 487 } |
| 469 text_x_ += SkFloatToScalar(width); | 488 text_x_ += SkFloatToScalar(width); |
| 470 line_x_ += SkFloatToScalar(width); | 489 line_x_ += SkFloatToScalar(width); |
| 490 | |
| 491 // Calculate |word_width_|. | |
| 492 if (words_) { | |
| 493 BreakList<size_t>::const_iterator word_for_current_run = | |
|
Jun Mukai
2015/05/06 17:38:52
This isn't correct if the segment contains more th
xdai1
2015/05/06 20:23:44
Because of the way we handle white space, there ar
| |
| 494 words_->GetBreak(char_range.start()); | |
| 495 BreakList<size_t>::const_iterator word_for_next_run = | |
| 496 (char_range.end() == text_.size()) | |
| 497 ? words_->breaks().end() | |
| 498 : words_->GetBreak(char_range.end()); | |
| 499 if (word_width_ == 0 && word_for_current_run == word_for_next_run) { | |
| 500 Range tmp_range; | |
| 501 Range glyph_range; | |
| 502 for (size_t pos = char_range.start(); pos < char_range.end(); ++pos) { | |
|
Jun Mukai
2015/05/06 17:38:52
TextRunHarfBuzz::CharRangeToGlyphRange() can be us
xdai1
2015/05/06 20:23:44
Done.
| |
| 503 run.GetClusterAt(pos, &tmp_range, &glyph_range); | |
| 504 SkScalar char_width = ((glyph_range.end() >= run.glyph_count) | |
| 505 ? SkFloatToScalar(run.width) | |
| 506 : run.positions[glyph_range.end()].x()) - | |
| 507 run.positions[glyph_range.start()].x(); | |
| 508 word_width_ += char_width; | |
| 509 } | |
| 510 } else if (word_for_current_run != word_for_next_run) { | |
| 511 word_width_ = 0; | |
| 512 } | |
| 513 } | |
| 471 } | 514 } |
| 472 | 515 |
| 473 const SkScalar max_width_; | 516 const SkScalar max_width_; |
| 474 const int min_baseline_; | 517 const int min_baseline_; |
| 475 const float min_height_; | 518 const float min_height_; |
| 476 const bool multiline_; | 519 const bool multiline_; |
| 477 const WordWrapBehavior word_wrap_behavior_; | 520 const WordWrapBehavior word_wrap_behavior_; |
| 478 const base::string16& text_; | 521 const base::string16& text_; |
| 479 const BreakList<size_t>* const words_; | 522 const BreakList<size_t>* const words_; |
| 480 const internal::TextRunList& run_list_; | 523 const internal::TextRunList& run_list_; |
| 481 | 524 |
| 482 // Stores the resulting lines. | 525 // Stores the resulting lines. |
| 483 std::vector<internal::Line> lines_; | 526 std::vector<internal::Line> lines_; |
| 484 | 527 |
| 485 // Text space and line space x coordinates of the next segment to be added. | 528 // Text space and line space x coordinates of the next segment to be added. |
| 486 SkScalar text_x_; | 529 SkScalar text_x_; |
| 487 SkScalar line_x_; | 530 SkScalar line_x_; |
| 488 | 531 |
| 489 float max_descent_; | 532 float max_descent_; |
| 490 float max_ascent_; | 533 float max_ascent_; |
| 491 | 534 |
| 535 // Stores the end position of the current word in process. | |
| 536 size_t word_end_pos_; | |
| 537 // Stores the glyph width between the current position and the start position | |
| 538 // of the current word. If the current position is pointing to the first | |
| 539 // character of the current word, |word_width_| is set to 0. | |
| 540 SkScalar word_width_; | |
| 541 | |
| 492 // Size of the multiline text, not including the currently processed line. | 542 // Size of the multiline text, not including the currently processed line. |
| 493 SizeF total_size_; | 543 SizeF total_size_; |
| 494 | 544 |
| 495 // The current RTL run segments, to be applied by |UpdateRTLSegmentRanges()|. | 545 // The current RTL run segments, to be applied by |UpdateRTLSegmentRanges()|. |
| 496 std::vector<SegmentHandle> rtl_segments_; | 546 std::vector<SegmentHandle> rtl_segments_; |
| 497 | 547 |
| 498 DISALLOW_COPY_AND_ASSIGN(HarfBuzzLineBreaker); | 548 DISALLOW_COPY_AND_ASSIGN(HarfBuzzLineBreaker); |
| 499 }; | 549 }; |
| 500 | 550 |
| 501 // Function object for case insensitive string comparison. | 551 // 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_); | 1529 DCHECK(!update_layout_run_list_); |
| 1480 DCHECK(!update_display_run_list_); | 1530 DCHECK(!update_display_run_list_); |
| 1481 return text_elided() ? display_run_list_.get() : &layout_run_list_; | 1531 return text_elided() ? display_run_list_.get() : &layout_run_list_; |
| 1482 } | 1532 } |
| 1483 | 1533 |
| 1484 const internal::TextRunList* RenderTextHarfBuzz::GetRunList() const { | 1534 const internal::TextRunList* RenderTextHarfBuzz::GetRunList() const { |
| 1485 return const_cast<RenderTextHarfBuzz*>(this)->GetRunList(); | 1535 return const_cast<RenderTextHarfBuzz*>(this)->GetRunList(); |
| 1486 } | 1536 } |
| 1487 | 1537 |
| 1488 } // namespace gfx | 1538 } // namespace gfx |
| OLD | NEW |