| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 5 #include "ui/gfx/render_text.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <climits> | 8 #include <climits> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/i18n/break_iterator.h" | 11 #include "base/i18n/break_iterator.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/trace_event/trace_event.h" |
| 16 #include "third_party/icu/source/common/unicode/rbbi.h" | 17 #include "third_party/icu/source/common/unicode/rbbi.h" |
| 17 #include "third_party/icu/source/common/unicode/utf16.h" | 18 #include "third_party/icu/source/common/unicode/utf16.h" |
| 18 #include "third_party/skia/include/core/SkTypeface.h" | 19 #include "third_party/skia/include/core/SkTypeface.h" |
| 19 #include "third_party/skia/include/effects/SkGradientShader.h" | 20 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 20 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
| 21 #include "ui/gfx/geometry/insets.h" | 22 #include "ui/gfx/geometry/insets.h" |
| 22 #include "ui/gfx/geometry/safe_integer_conversions.h" | 23 #include "ui/gfx/geometry/safe_integer_conversions.h" |
| 23 #include "ui/gfx/render_text_harfbuzz.h" | 24 #include "ui/gfx/render_text_harfbuzz.h" |
| 24 #include "ui/gfx/scoped_canvas.h" | 25 #include "ui/gfx/scoped_canvas.h" |
| 25 #include "ui/gfx/skia_util.h" | 26 #include "ui/gfx/skia_util.h" |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 | 438 |
| 438 // Reset selection model. SetText should always followed by SetSelectionModel | 439 // Reset selection model. SetText should always followed by SetSelectionModel |
| 439 // or SetCursorPosition in upper layer. | 440 // or SetCursorPosition in upper layer. |
| 440 SetSelectionModel(SelectionModel()); | 441 SetSelectionModel(SelectionModel()); |
| 441 | 442 |
| 442 // Invalidate the cached text direction if it depends on the text contents. | 443 // Invalidate the cached text direction if it depends on the text contents. |
| 443 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT) | 444 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT) |
| 444 text_direction_ = base::i18n::UNKNOWN_DIRECTION; | 445 text_direction_ = base::i18n::UNKNOWN_DIRECTION; |
| 445 | 446 |
| 446 obscured_reveal_index_ = -1; | 447 obscured_reveal_index_ = -1; |
| 447 UpdateLayoutText(); | 448 OnTextAttributeChanged(); |
| 448 } | 449 } |
| 449 | 450 |
| 450 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { | 451 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { |
| 451 if (horizontal_alignment_ != alignment) { | 452 if (horizontal_alignment_ != alignment) { |
| 452 horizontal_alignment_ = alignment; | 453 horizontal_alignment_ = alignment; |
| 453 display_offset_ = Vector2d(); | 454 display_offset_ = Vector2d(); |
| 454 cached_bounds_and_offset_valid_ = false; | 455 cached_bounds_and_offset_valid_ = false; |
| 455 } | 456 } |
| 456 } | 457 } |
| 457 | 458 |
| 458 void RenderText::SetFontList(const FontList& font_list) { | 459 void RenderText::SetFontList(const FontList& font_list) { |
| 459 font_list_ = font_list; | 460 font_list_ = font_list; |
| 460 const int font_style = font_list.GetFontStyle(); | 461 const int font_style = font_list.GetFontStyle(); |
| 461 SetStyle(BOLD, (font_style & gfx::Font::BOLD) != 0); | 462 SetStyle(BOLD, (font_style & gfx::Font::BOLD) != 0); |
| 462 SetStyle(ITALIC, (font_style & gfx::Font::ITALIC) != 0); | 463 SetStyle(ITALIC, (font_style & gfx::Font::ITALIC) != 0); |
| 463 SetStyle(UNDERLINE, (font_style & gfx::Font::UNDERLINE) != 0); | 464 SetStyle(UNDERLINE, (font_style & gfx::Font::UNDERLINE) != 0); |
| 464 baseline_ = kInvalidBaseline; | 465 baseline_ = kInvalidBaseline; |
| 465 cached_bounds_and_offset_valid_ = false; | 466 cached_bounds_and_offset_valid_ = false; |
| 466 ResetLayout(); | 467 OnLayoutTextAttributeChanged(false); |
| 467 } | 468 } |
| 468 | 469 |
| 469 void RenderText::SetCursorEnabled(bool cursor_enabled) { | 470 void RenderText::SetCursorEnabled(bool cursor_enabled) { |
| 470 cursor_enabled_ = cursor_enabled; | 471 cursor_enabled_ = cursor_enabled; |
| 471 cached_bounds_and_offset_valid_ = false; | 472 cached_bounds_and_offset_valid_ = false; |
| 472 } | 473 } |
| 473 | 474 |
| 474 void RenderText::ToggleInsertMode() { | 475 void RenderText::ToggleInsertMode() { |
| 475 insert_mode_ = !insert_mode_; | 476 insert_mode_ = !insert_mode_; |
| 476 cached_bounds_and_offset_valid_ = false; | 477 cached_bounds_and_offset_valid_ = false; |
| 477 } | 478 } |
| 478 | 479 |
| 479 void RenderText::SetObscured(bool obscured) { | 480 void RenderText::SetObscured(bool obscured) { |
| 480 if (obscured != obscured_) { | 481 if (obscured != obscured_) { |
| 481 obscured_ = obscured; | 482 obscured_ = obscured; |
| 482 obscured_reveal_index_ = -1; | 483 obscured_reveal_index_ = -1; |
| 483 cached_bounds_and_offset_valid_ = false; | 484 cached_bounds_and_offset_valid_ = false; |
| 484 UpdateLayoutText(); | 485 OnTextAttributeChanged(); |
| 485 } | 486 } |
| 486 } | 487 } |
| 487 | 488 |
| 488 void RenderText::SetObscuredRevealIndex(int index) { | 489 void RenderText::SetObscuredRevealIndex(int index) { |
| 489 if (obscured_reveal_index_ == index) | 490 if (obscured_reveal_index_ == index) |
| 490 return; | 491 return; |
| 491 | 492 |
| 492 obscured_reveal_index_ = index; | 493 obscured_reveal_index_ = index; |
| 493 cached_bounds_and_offset_valid_ = false; | 494 cached_bounds_and_offset_valid_ = false; |
| 494 UpdateLayoutText(); | 495 OnTextAttributeChanged(); |
| 495 } | |
| 496 | |
| 497 void RenderText::SetReplaceNewlineCharsWithSymbols(bool replace) { | |
| 498 replace_newline_chars_with_symbols_ = replace; | |
| 499 cached_bounds_and_offset_valid_ = false; | |
| 500 UpdateLayoutText(); | |
| 501 } | 496 } |
| 502 | 497 |
| 503 void RenderText::SetMultiline(bool multiline) { | 498 void RenderText::SetMultiline(bool multiline) { |
| 504 if (multiline != multiline_) { | 499 if (multiline != multiline_) { |
| 505 multiline_ = multiline; | 500 multiline_ = multiline; |
| 506 cached_bounds_and_offset_valid_ = false; | 501 cached_bounds_and_offset_valid_ = false; |
| 507 lines_.clear(); | 502 lines_.clear(); |
| 503 OnDisplayTextAttributeChanged(); |
| 508 } | 504 } |
| 509 } | 505 } |
| 510 | 506 |
| 511 void RenderText::SetMinLineHeight(int line_height) { | 507 void RenderText::SetMinLineHeight(int line_height) { |
| 512 if (min_line_height_ == line_height) | 508 if (min_line_height_ == line_height) |
| 513 return; | 509 return; |
| 514 min_line_height_ = line_height; | 510 min_line_height_ = line_height; |
| 515 cached_bounds_and_offset_valid_ = false; | 511 cached_bounds_and_offset_valid_ = false; |
| 516 lines_.clear(); | 512 lines_.clear(); |
| 513 OnDisplayTextAttributeChanged(); |
| 517 } | 514 } |
| 518 | 515 |
| 519 void RenderText::SetElideBehavior(ElideBehavior elide_behavior) { | 516 void RenderText::SetElideBehavior(ElideBehavior elide_behavior) { |
| 520 // TODO(skanuj) : Add a test for triggering layout change. | 517 // TODO(skanuj) : Add a test for triggering layout change. |
| 521 if (elide_behavior_ != elide_behavior) { | 518 if (elide_behavior_ != elide_behavior) { |
| 522 elide_behavior_ = elide_behavior; | 519 elide_behavior_ = elide_behavior; |
| 523 UpdateLayoutText(); | 520 OnDisplayTextAttributeChanged(); |
| 524 } | 521 } |
| 525 } | 522 } |
| 526 | 523 |
| 527 void RenderText::SetDisplayRect(const Rect& r) { | 524 void RenderText::SetDisplayRect(const Rect& r) { |
| 528 if (r != display_rect_) { | 525 if (r != display_rect_) { |
| 529 display_rect_ = r; | 526 display_rect_ = r; |
| 530 baseline_ = kInvalidBaseline; | 527 baseline_ = kInvalidBaseline; |
| 531 cached_bounds_and_offset_valid_ = false; | 528 cached_bounds_and_offset_valid_ = false; |
| 532 lines_.clear(); | 529 lines_.clear(); |
| 533 if (elide_behavior_ != NO_ELIDE) | 530 if (elide_behavior_ != NO_ELIDE && |
| 534 UpdateLayoutText(); | 531 elide_behavior_ != FADE_TAIL) { |
| 532 OnDisplayTextAttributeChanged(); |
| 533 } |
| 535 } | 534 } |
| 536 } | 535 } |
| 537 | 536 |
| 538 void RenderText::SetCursorPosition(size_t position) { | 537 void RenderText::SetCursorPosition(size_t position) { |
| 539 MoveCursorTo(position, false); | 538 MoveCursorTo(position, false); |
| 540 } | 539 } |
| 541 | 540 |
| 542 void RenderText::MoveCursor(BreakType break_type, | 541 void RenderText::MoveCursor(BreakType break_type, |
| 543 VisualCursorDirection direction, | 542 VisualCursorDirection direction, |
| 544 bool select) { | 543 bool select) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 | 649 |
| 651 const Range& RenderText::GetCompositionRange() const { | 650 const Range& RenderText::GetCompositionRange() const { |
| 652 return composition_range_; | 651 return composition_range_; |
| 653 } | 652 } |
| 654 | 653 |
| 655 void RenderText::SetCompositionRange(const Range& composition_range) { | 654 void RenderText::SetCompositionRange(const Range& composition_range) { |
| 656 CHECK(!composition_range.IsValid() || | 655 CHECK(!composition_range.IsValid() || |
| 657 Range(0, text_.length()).Contains(composition_range)); | 656 Range(0, text_.length()).Contains(composition_range)); |
| 658 composition_range_.set_end(composition_range.end()); | 657 composition_range_.set_end(composition_range.end()); |
| 659 composition_range_.set_start(composition_range.start()); | 658 composition_range_.set_start(composition_range.start()); |
| 660 ResetLayout(); | 659 // TODO(oshima|msw): Altering composition underlines shouldn't |
| 660 // require layout changes. It's currently necessary because |
| 661 // RenderTextHarfBuzz paints text decorations by run, and |
| 662 // RenderTextMac applies all styles during layout. |
| 663 OnLayoutTextAttributeChanged(false); |
| 661 } | 664 } |
| 662 | 665 |
| 663 void RenderText::SetColor(SkColor value) { | 666 void RenderText::SetColor(SkColor value) { |
| 664 colors_.SetValue(value); | 667 colors_.SetValue(value); |
| 665 } | 668 } |
| 666 | 669 |
| 667 void RenderText::ApplyColor(SkColor value, const Range& range) { | 670 void RenderText::ApplyColor(SkColor value, const Range& range) { |
| 668 colors_.ApplyValue(value, range); | 671 colors_.ApplyValue(value, range); |
| 669 } | 672 } |
| 670 | 673 |
| 671 void RenderText::SetStyle(TextStyle style, bool value) { | 674 void RenderText::SetStyle(TextStyle style, bool value) { |
| 672 styles_[style].SetValue(value); | 675 styles_[style].SetValue(value); |
| 673 | 676 |
| 674 cached_bounds_and_offset_valid_ = false; | 677 cached_bounds_and_offset_valid_ = false; |
| 675 ResetLayout(); | 678 OnLayoutTextAttributeChanged(false); |
| 676 } | 679 } |
| 677 | 680 |
| 678 void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) { | 681 void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) { |
| 679 // Do not change styles mid-grapheme to avoid breaking ligatures. | 682 // Do not change styles mid-grapheme to avoid breaking ligatures. |
| 680 const size_t start = IsValidCursorIndex(range.start()) ? range.start() : | 683 const size_t start = IsValidCursorIndex(range.start()) ? range.start() : |
| 681 IndexOfAdjacentGrapheme(range.start(), CURSOR_BACKWARD); | 684 IndexOfAdjacentGrapheme(range.start(), CURSOR_BACKWARD); |
| 682 const size_t end = IsValidCursorIndex(range.end()) ? range.end() : | 685 const size_t end = IsValidCursorIndex(range.end()) ? range.end() : |
| 683 IndexOfAdjacentGrapheme(range.end(), CURSOR_FORWARD); | 686 IndexOfAdjacentGrapheme(range.end(), CURSOR_FORWARD); |
| 684 styles_[style].ApplyValue(value, Range(start, end)); | 687 styles_[style].ApplyValue(value, Range(start, end)); |
| 685 | 688 |
| 686 cached_bounds_and_offset_valid_ = false; | 689 cached_bounds_and_offset_valid_ = false; |
| 687 ResetLayout(); | 690 OnLayoutTextAttributeChanged(false); |
| 688 } | 691 } |
| 689 | 692 |
| 690 bool RenderText::GetStyle(TextStyle style) const { | 693 bool RenderText::GetStyle(TextStyle style) const { |
| 691 return (styles_[style].breaks().size() == 1) && | 694 return (styles_[style].breaks().size() == 1) && |
| 692 styles_[style].breaks().front().second; | 695 styles_[style].breaks().front().second; |
| 693 } | 696 } |
| 694 | 697 |
| 695 void RenderText::SetDirectionalityMode(DirectionalityMode mode) { | 698 void RenderText::SetDirectionalityMode(DirectionalityMode mode) { |
| 696 if (mode == directionality_mode_) | 699 if (mode == directionality_mode_) |
| 697 return; | 700 return; |
| 698 | 701 |
| 699 directionality_mode_ = mode; | 702 directionality_mode_ = mode; |
| 700 text_direction_ = base::i18n::UNKNOWN_DIRECTION; | 703 text_direction_ = base::i18n::UNKNOWN_DIRECTION; |
| 701 cached_bounds_and_offset_valid_ = false; | 704 cached_bounds_and_offset_valid_ = false; |
| 702 ResetLayout(); | 705 OnLayoutTextAttributeChanged(false); |
| 703 } | 706 } |
| 704 | 707 |
| 705 base::i18n::TextDirection RenderText::GetTextDirection() { | 708 base::i18n::TextDirection RenderText::GetDisplayTextDirection() { |
| 706 if (text_direction_ == base::i18n::UNKNOWN_DIRECTION) { | 709 return GetTextDirection(GetDisplayText()); |
| 707 switch (directionality_mode_) { | |
| 708 case DIRECTIONALITY_FROM_TEXT: | |
| 709 // Derive the direction from the display text, which differs from text() | |
| 710 // in the case of obscured (password) textfields. | |
| 711 text_direction_ = | |
| 712 base::i18n::GetFirstStrongCharacterDirection(GetLayoutText()); | |
| 713 break; | |
| 714 case DIRECTIONALITY_FROM_UI: | |
| 715 text_direction_ = base::i18n::IsRTL() ? base::i18n::RIGHT_TO_LEFT : | |
| 716 base::i18n::LEFT_TO_RIGHT; | |
| 717 break; | |
| 718 case DIRECTIONALITY_FORCE_LTR: | |
| 719 text_direction_ = base::i18n::LEFT_TO_RIGHT; | |
| 720 break; | |
| 721 case DIRECTIONALITY_FORCE_RTL: | |
| 722 text_direction_ = base::i18n::RIGHT_TO_LEFT; | |
| 723 break; | |
| 724 default: | |
| 725 NOTREACHED(); | |
| 726 } | |
| 727 } | |
| 728 | |
| 729 return text_direction_; | |
| 730 } | 710 } |
| 731 | 711 |
| 732 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() { | 712 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() { |
| 733 return GetTextDirection() == base::i18n::LEFT_TO_RIGHT ? | 713 return GetDisplayTextDirection() == base::i18n::LEFT_TO_RIGHT ? |
| 734 CURSOR_RIGHT : CURSOR_LEFT; | 714 CURSOR_RIGHT : CURSOR_LEFT; |
| 735 } | 715 } |
| 736 | 716 |
| 737 SizeF RenderText::GetStringSizeF() { | 717 SizeF RenderText::GetStringSizeF() { |
| 738 return GetStringSize(); | 718 return GetStringSize(); |
| 739 } | 719 } |
| 740 | 720 |
| 741 float RenderText::GetContentWidthF() { | 721 float RenderText::GetContentWidthF() { |
| 742 const float string_size = GetStringSizeF().width(); | 722 const float string_size = GetStringSizeF().width(); |
| 743 // The cursor is drawn one pixel beyond the int-enclosed text bounds. | 723 // The cursor is drawn one pixel beyond the int-enclosed text bounds. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 } | 760 } |
| 781 | 761 |
| 782 void RenderText::DrawCursor(Canvas* canvas, const SelectionModel& position) { | 762 void RenderText::DrawCursor(Canvas* canvas, const SelectionModel& position) { |
| 783 // Paint cursor. Replace cursor is drawn as rectangle for now. | 763 // Paint cursor. Replace cursor is drawn as rectangle for now. |
| 784 // TODO(msw): Draw a better cursor with a better indication of association. | 764 // TODO(msw): Draw a better cursor with a better indication of association. |
| 785 canvas->FillRect(GetCursorBounds(position, true), cursor_color_); | 765 canvas->FillRect(GetCursorBounds(position, true), cursor_color_); |
| 786 } | 766 } |
| 787 | 767 |
| 788 bool RenderText::IsValidLogicalIndex(size_t index) { | 768 bool RenderText::IsValidLogicalIndex(size_t index) { |
| 789 // Check that the index is at a valid code point (not mid-surrgate-pair) and | 769 // Check that the index is at a valid code point (not mid-surrgate-pair) and |
| 790 // that it's not truncated from the layout text (its glyph may be shown). | 770 // that it's not truncated from the display text (its glyph may be shown). |
| 791 // | 771 // |
| 792 // Indices within truncated text are disallowed so users can easily interact | 772 // Indices within truncated text are disallowed so users can easily interact |
| 793 // with the underlying truncated text using the ellipsis as a proxy. This lets | 773 // with the underlying truncated text using the ellipsis as a proxy. This lets |
| 794 // users select all text, select the truncated text, and transition from the | 774 // users select all text, select the truncated text, and transition from the |
| 795 // last rendered glyph to the end of the text without getting invisible cursor | 775 // last rendered glyph to the end of the text without getting invisible cursor |
| 796 // positions nor needing unbounded arrow key presses to traverse the ellipsis. | 776 // positions nor needing unbounded arrow key presses to traverse the ellipsis. |
| 797 return index == 0 || index == text().length() || | 777 return index == 0 || index == text().length() || |
| 798 (index < text().length() && | 778 (index < text().length() && |
| 799 (truncate_length_ == 0 || index < truncate_length_) && | 779 (truncate_length_ == 0 || index < truncate_length_) && |
| 800 IsValidCodePointIndex(text(), index)); | 780 IsValidCodePointIndex(text(), index)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 811 DCHECK(IsValidLogicalIndex(caret_pos)); | 791 DCHECK(IsValidLogicalIndex(caret_pos)); |
| 812 // In overtype mode, ignore the affinity and always indicate that we will | 792 // In overtype mode, ignore the affinity and always indicate that we will |
| 813 // overtype the next character. | 793 // overtype the next character. |
| 814 LogicalCursorDirection caret_affinity = | 794 LogicalCursorDirection caret_affinity = |
| 815 insert_mode ? caret.caret_affinity() : CURSOR_FORWARD; | 795 insert_mode ? caret.caret_affinity() : CURSOR_FORWARD; |
| 816 int x = 0, width = 1; | 796 int x = 0, width = 1; |
| 817 Size size = GetStringSize(); | 797 Size size = GetStringSize(); |
| 818 if (caret_pos == (caret_affinity == CURSOR_BACKWARD ? 0 : text().length())) { | 798 if (caret_pos == (caret_affinity == CURSOR_BACKWARD ? 0 : text().length())) { |
| 819 // The caret is attached to the boundary. Always return a 1-dip width caret, | 799 // The caret is attached to the boundary. Always return a 1-dip width caret, |
| 820 // since there is nothing to overtype. | 800 // since there is nothing to overtype. |
| 821 if ((GetTextDirection() == base::i18n::RIGHT_TO_LEFT) == (caret_pos == 0)) | 801 if ((GetDisplayTextDirection() == base::i18n::RIGHT_TO_LEFT) |
| 802 == (caret_pos == 0)) { |
| 822 x = size.width(); | 803 x = size.width(); |
| 804 } |
| 823 } else { | 805 } else { |
| 824 size_t grapheme_start = (caret_affinity == CURSOR_FORWARD) ? | 806 size_t grapheme_start = (caret_affinity == CURSOR_FORWARD) ? |
| 825 caret_pos : IndexOfAdjacentGrapheme(caret_pos, CURSOR_BACKWARD); | 807 caret_pos : IndexOfAdjacentGrapheme(caret_pos, CURSOR_BACKWARD); |
| 826 Range xspan(GetGlyphBounds(grapheme_start)); | 808 Range xspan(GetGlyphBounds(grapheme_start)); |
| 827 if (insert_mode) { | 809 if (insert_mode) { |
| 828 x = (caret_affinity == CURSOR_BACKWARD) ? xspan.end() : xspan.start(); | 810 x = (caret_affinity == CURSOR_BACKWARD) ? xspan.end() : xspan.start(); |
| 829 } else { // overtype mode | 811 } else { // overtype mode |
| 830 x = xspan.GetMin(); | 812 x = xspan.GetMin(); |
| 831 width = xspan.length(); | 813 width = xspan.length(); |
| 832 } | 814 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 924 selection_background_focused_color_(kDefaultSelectionBackgroundColor), | 906 selection_background_focused_color_(kDefaultSelectionBackgroundColor), |
| 925 focused_(false), | 907 focused_(false), |
| 926 composition_range_(Range::InvalidRange()), | 908 composition_range_(Range::InvalidRange()), |
| 927 colors_(kDefaultColor), | 909 colors_(kDefaultColor), |
| 928 styles_(NUM_TEXT_STYLES), | 910 styles_(NUM_TEXT_STYLES), |
| 929 composition_and_selection_styles_applied_(false), | 911 composition_and_selection_styles_applied_(false), |
| 930 obscured_(false), | 912 obscured_(false), |
| 931 obscured_reveal_index_(-1), | 913 obscured_reveal_index_(-1), |
| 932 truncate_length_(0), | 914 truncate_length_(0), |
| 933 elide_behavior_(NO_ELIDE), | 915 elide_behavior_(NO_ELIDE), |
| 934 replace_newline_chars_with_symbols_(true), | 916 text_elided_(false), |
| 935 min_line_height_(0), | 917 min_line_height_(0), |
| 936 multiline_(false), | 918 multiline_(false), |
| 937 background_is_transparent_(false), | 919 background_is_transparent_(false), |
| 938 clip_to_display_rect_(true), | 920 clip_to_display_rect_(true), |
| 939 baseline_(kInvalidBaseline), | 921 baseline_(kInvalidBaseline), |
| 940 cached_bounds_and_offset_valid_(false) { | 922 cached_bounds_and_offset_valid_(false) { |
| 941 } | 923 } |
| 942 | 924 |
| 943 SelectionModel RenderText::GetAdjacentSelectionModel( | 925 SelectionModel RenderText::GetAdjacentSelectionModel( |
| 944 const SelectionModel& current, | 926 const SelectionModel& current, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 960 return SelectionModel(text().length(), CURSOR_FORWARD); | 942 return SelectionModel(text().length(), CURSOR_FORWARD); |
| 961 return SelectionModel(0, CURSOR_BACKWARD); | 943 return SelectionModel(0, CURSOR_BACKWARD); |
| 962 } | 944 } |
| 963 | 945 |
| 964 void RenderText::SetSelectionModel(const SelectionModel& model) { | 946 void RenderText::SetSelectionModel(const SelectionModel& model) { |
| 965 DCHECK_LE(model.selection().GetMax(), text().length()); | 947 DCHECK_LE(model.selection().GetMax(), text().length()); |
| 966 selection_model_ = model; | 948 selection_model_ = model; |
| 967 cached_bounds_and_offset_valid_ = false; | 949 cached_bounds_and_offset_valid_ = false; |
| 968 } | 950 } |
| 969 | 951 |
| 970 const base::string16& RenderText::GetLayoutText() const { | 952 void RenderText::UpdateDisplayText(float text_width) { |
| 971 return layout_text_; | 953 // TODO(oshima): Consider support eliding for multi-line text. |
| 954 // This requires max_line support first. |
| 955 if (multiline_ || |
| 956 elide_behavior() == NO_ELIDE || |
| 957 elide_behavior() == FADE_TAIL || |
| 958 text_width < display_rect_.width() || |
| 959 layout_text_.empty()) { |
| 960 text_elided_ = false; |
| 961 display_text_.clear(); |
| 962 return; |
| 963 } |
| 964 |
| 965 // This doesn't trim styles so ellipsis may get rendered as a different |
| 966 // style than the preceding text. See crbug.com/327850. |
| 967 display_text_.assign(Elide(layout_text_, |
| 968 text_width, |
| 969 static_cast<float>(display_rect_.width()), |
| 970 elide_behavior_)); |
| 971 |
| 972 text_elided_ = display_text_ != layout_text_; |
| 973 if (!text_elided_) |
| 974 display_text_.clear(); |
| 972 } | 975 } |
| 973 | 976 |
| 974 const BreakList<size_t>& RenderText::GetLineBreaks() { | 977 const BreakList<size_t>& RenderText::GetLineBreaks() { |
| 975 if (line_breaks_.max() != 0) | 978 if (line_breaks_.max() != 0) |
| 976 return line_breaks_; | 979 return line_breaks_; |
| 977 | 980 |
| 978 const base::string16& layout_text = GetLayoutText(); | 981 const base::string16& layout_text = GetDisplayText(); |
| 979 const size_t text_length = layout_text.length(); | 982 const size_t text_length = layout_text.length(); |
| 980 line_breaks_.SetValue(0); | 983 line_breaks_.SetValue(0); |
| 981 line_breaks_.SetMax(text_length); | 984 line_breaks_.SetMax(text_length); |
| 982 base::i18n::BreakIterator iter(layout_text, | 985 base::i18n::BreakIterator iter(layout_text, |
| 983 base::i18n::BreakIterator::BREAK_LINE); | 986 base::i18n::BreakIterator::BREAK_LINE); |
| 984 const bool success = iter.Init(); | 987 const bool success = iter.Init(); |
| 985 DCHECK(success); | 988 DCHECK(success); |
| 986 if (success) { | 989 if (success) { |
| 987 do { | 990 do { |
| 988 line_breaks_.ApplyValue(iter.pos(), Range(iter.pos(), text_length)); | 991 line_breaks_.ApplyValue(iter.pos(), Range(iter.pos(), text_length)); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1074 line_x += segment->x_range.length(); | 1077 line_x += segment->x_range.length(); |
| 1075 } | 1078 } |
| 1076 } | 1079 } |
| 1077 | 1080 |
| 1078 return rects; | 1081 return rects; |
| 1079 } | 1082 } |
| 1080 | 1083 |
| 1081 HorizontalAlignment RenderText::GetCurrentHorizontalAlignment() { | 1084 HorizontalAlignment RenderText::GetCurrentHorizontalAlignment() { |
| 1082 if (horizontal_alignment_ != ALIGN_TO_HEAD) | 1085 if (horizontal_alignment_ != ALIGN_TO_HEAD) |
| 1083 return horizontal_alignment_; | 1086 return horizontal_alignment_; |
| 1084 return GetTextDirection() == base::i18n::RIGHT_TO_LEFT ? ALIGN_RIGHT | 1087 return GetDisplayTextDirection() == base::i18n::RIGHT_TO_LEFT ? |
| 1085 : ALIGN_LEFT; | 1088 ALIGN_RIGHT : ALIGN_LEFT; |
| 1086 } | 1089 } |
| 1087 | 1090 |
| 1088 Vector2d RenderText::GetAlignmentOffset(size_t line_number) { | 1091 Vector2d RenderText::GetAlignmentOffset(size_t line_number) { |
| 1089 // TODO(ckocagil): Enable |lines_| usage in other platforms. | 1092 // TODO(ckocagil): Enable |lines_| usage in other platforms. |
| 1090 #if defined(OS_WIN) | 1093 #if defined(OS_WIN) |
| 1091 DCHECK_LT(line_number, lines_.size()); | 1094 DCHECK_LT(line_number, lines_.size()); |
| 1092 #endif | 1095 #endif |
| 1093 Vector2d offset; | 1096 Vector2d offset; |
| 1094 HorizontalAlignment horizontal_alignment = GetCurrentHorizontalAlignment(); | 1097 HorizontalAlignment horizontal_alignment = GetCurrentHorizontalAlignment(); |
| 1095 if (horizontal_alignment != ALIGN_LEFT) { | 1098 if (horizontal_alignment != ALIGN_LEFT) { |
| 1096 #if defined(OS_WIN) | 1099 #if defined(OS_WIN) |
| 1097 const int width = std::ceil(lines_[line_number].size.width()) + | 1100 const int width = std::ceil(lines_[line_number].size.width()) + |
| 1098 (cursor_enabled_ ? 1 : 0); | 1101 (cursor_enabled_ ? 1 : 0); |
| 1099 #else | 1102 #else |
| 1100 const int width = GetContentWidth(); | 1103 const int width = GetContentWidth(); |
| 1101 #endif | 1104 #endif |
| 1102 offset.set_x(display_rect().width() - width); | 1105 offset.set_x(display_rect().width() - width); |
| 1103 // Put any extra margin pixel on the left to match legacy behavior. | 1106 // Put any extra margin pixel on the left to match legacy behavior. |
| 1104 if (horizontal_alignment == ALIGN_CENTER) | 1107 if (horizontal_alignment == ALIGN_CENTER) |
| 1105 offset.set_x((offset.x() + 1) / 2); | 1108 offset.set_x((offset.x() + 1) / 2); |
| 1106 } | 1109 } |
| 1107 | 1110 |
| 1108 // Vertically center the text. | 1111 // Vertically center the text. |
| 1109 if (multiline_) { | 1112 if (multiline_) { |
| 1110 const int text_height = lines_.back().preceding_heights + | 1113 const int text_height = lines_.back().preceding_heights + |
| 1111 lines_.back().size.height(); | 1114 lines_.back().size.height(); |
| 1112 offset.set_y((display_rect_.height() - text_height) / 2); | 1115 offset.set_y((display_rect_.height() - text_height) / 2); |
| 1113 } else { | 1116 } else { |
| 1114 offset.set_y(GetBaseline() - GetLayoutTextBaseline()); | 1117 offset.set_y(GetBaseline() - GetDisplayTextBaseline()); |
| 1115 } | 1118 } |
| 1116 | 1119 |
| 1117 return offset; | 1120 return offset; |
| 1118 } | 1121 } |
| 1119 | 1122 |
| 1120 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) { | 1123 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) { |
| 1121 const int width = display_rect().width(); | 1124 const int width = display_rect().width(); |
| 1122 if (multiline() || elide_behavior_ != FADE_TAIL || GetContentWidth() <= width) | 1125 if (multiline() || elide_behavior_ != FADE_TAIL || GetContentWidth() <= width) |
| 1123 return; | 1126 return; |
| 1124 | 1127 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1149 text_rect, left_part, right_part, colors_.breaks().front().second); | 1152 text_rect, left_part, right_part, colors_.breaks().front().second); |
| 1150 if (shader) | 1153 if (shader) |
| 1151 renderer->SetShader(shader.get()); | 1154 renderer->SetShader(shader.get()); |
| 1152 } | 1155 } |
| 1153 | 1156 |
| 1154 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { | 1157 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { |
| 1155 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); | 1158 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); |
| 1156 renderer->SetDrawLooper(looper.get()); | 1159 renderer->SetDrawLooper(looper.get()); |
| 1157 } | 1160 } |
| 1158 | 1161 |
| 1162 base::i18n::TextDirection RenderText::GetTextDirection( |
| 1163 const base::string16& text) { |
| 1164 if (text_direction_ == base::i18n::UNKNOWN_DIRECTION) { |
| 1165 switch (directionality_mode_) { |
| 1166 case DIRECTIONALITY_FROM_TEXT: |
| 1167 // Derive the direction from the display text, which differs from text() |
| 1168 // in the case of obscured (password) textfields. |
| 1169 text_direction_ = |
| 1170 base::i18n::GetFirstStrongCharacterDirection(text); |
| 1171 break; |
| 1172 case DIRECTIONALITY_FROM_UI: |
| 1173 text_direction_ = base::i18n::IsRTL() ? base::i18n::RIGHT_TO_LEFT : |
| 1174 base::i18n::LEFT_TO_RIGHT; |
| 1175 break; |
| 1176 case DIRECTIONALITY_FORCE_LTR: |
| 1177 text_direction_ = base::i18n::LEFT_TO_RIGHT; |
| 1178 break; |
| 1179 case DIRECTIONALITY_FORCE_RTL: |
| 1180 text_direction_ = base::i18n::RIGHT_TO_LEFT; |
| 1181 break; |
| 1182 default: |
| 1183 NOTREACHED(); |
| 1184 } |
| 1185 } |
| 1186 |
| 1187 return text_direction_; |
| 1188 } |
| 1189 |
| 1159 // static | 1190 // static |
| 1160 bool RenderText::RangeContainsCaret(const Range& range, | 1191 bool RenderText::RangeContainsCaret(const Range& range, |
| 1161 size_t caret_pos, | 1192 size_t caret_pos, |
| 1162 LogicalCursorDirection caret_affinity) { | 1193 LogicalCursorDirection caret_affinity) { |
| 1163 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9). | 1194 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9). |
| 1164 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ? | 1195 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ? |
| 1165 caret_pos - 1 : caret_pos + 1; | 1196 caret_pos - 1 : caret_pos + 1; |
| 1166 return range.Contains(Range(caret_pos, adjacent)); | 1197 return range.Contains(Range(caret_pos, adjacent)); |
| 1167 } | 1198 } |
| 1168 | 1199 |
| 1169 void RenderText::MoveCursorTo(size_t position, bool select) { | 1200 void RenderText::MoveCursorTo(size_t position, bool select) { |
| 1170 size_t cursor = std::min(position, text().length()); | 1201 size_t cursor = std::min(position, text().length()); |
| 1171 if (IsValidCursorIndex(cursor)) | 1202 if (IsValidCursorIndex(cursor)) |
| 1172 SetSelectionModel(SelectionModel( | 1203 SetSelectionModel(SelectionModel( |
| 1173 Range(select ? selection().start() : cursor, cursor), | 1204 Range(select ? selection().start() : cursor, cursor), |
| 1174 (cursor == 0) ? CURSOR_FORWARD : CURSOR_BACKWARD)); | 1205 (cursor == 0) ? CURSOR_FORWARD : CURSOR_BACKWARD)); |
| 1175 } | 1206 } |
| 1176 | 1207 |
| 1177 void RenderText::UpdateLayoutText() { | 1208 void RenderText::OnTextAttributeChanged() { |
| 1178 layout_text_.clear(); | 1209 layout_text_.clear(); |
| 1210 display_text_.clear(); |
| 1179 line_breaks_.SetMax(0); | 1211 line_breaks_.SetMax(0); |
| 1180 | 1212 |
| 1181 if (obscured_) { | 1213 if (obscured_) { |
| 1182 size_t obscured_text_length = | 1214 size_t obscured_text_length = |
| 1183 static_cast<size_t>(UTF16IndexToOffset(text_, 0, text_.length())); | 1215 static_cast<size_t>(UTF16IndexToOffset(text_, 0, text_.length())); |
| 1184 layout_text_.assign(obscured_text_length, kPasswordReplacementChar); | 1216 layout_text_.assign(obscured_text_length, kPasswordReplacementChar); |
| 1185 | 1217 |
| 1186 if (obscured_reveal_index_ >= 0 && | 1218 if (obscured_reveal_index_ >= 0 && |
| 1187 obscured_reveal_index_ < static_cast<int>(text_.length())) { | 1219 obscured_reveal_index_ < static_cast<int>(text_.length())) { |
| 1188 // Gets the index range in |text_| to be revealed. | 1220 // Gets the index range in |text_| to be revealed. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1217 const size_t ellipsis_end = iter.getIndex(); | 1249 const size_t ellipsis_end = iter.getIndex(); |
| 1218 DCHECK_LE(ellipsis_start, ellipsis_end); | 1250 DCHECK_LE(ellipsis_start, ellipsis_end); |
| 1219 layout_text_.assign(text.substr(0, ellipsis_start) + kEllipsisUTF16 + | 1251 layout_text_.assign(text.substr(0, ellipsis_start) + kEllipsisUTF16 + |
| 1220 text.substr(ellipsis_end)); | 1252 text.substr(ellipsis_end)); |
| 1221 } else { | 1253 } else { |
| 1222 iter.setIndex32(truncate_length_ - 1); | 1254 iter.setIndex32(truncate_length_ - 1); |
| 1223 layout_text_.assign(text.substr(0, iter.getIndex()) + kEllipsisUTF16); | 1255 layout_text_.assign(text.substr(0, iter.getIndex()) + kEllipsisUTF16); |
| 1224 } | 1256 } |
| 1225 } | 1257 } |
| 1226 | 1258 |
| 1227 if (elide_behavior_ != NO_ELIDE && | 1259 OnLayoutTextAttributeChanged(true); |
| 1228 elide_behavior_ != FADE_TAIL && | |
| 1229 !layout_text_.empty() && | |
| 1230 GetContentWidth() > display_rect_.width()) { | |
| 1231 // This doesn't trim styles so ellipsis may get rendered as a different | |
| 1232 // style than the preceding text. See crbug.com/327850. | |
| 1233 layout_text_.assign(Elide(layout_text_, | |
| 1234 static_cast<float>(display_rect_.width()), | |
| 1235 elide_behavior_)); | |
| 1236 } | |
| 1237 | |
| 1238 // Replace the newline character with a newline symbol in single line mode. | |
| 1239 static const base::char16 kNewline[] = { '\n', 0 }; | |
| 1240 static const base::char16 kNewlineSymbol[] = { 0x2424, 0 }; | |
| 1241 if (!multiline_ && replace_newline_chars_with_symbols_) | |
| 1242 base::ReplaceChars(layout_text_, kNewline, kNewlineSymbol, &layout_text_); | |
| 1243 | |
| 1244 ResetLayout(); | |
| 1245 } | 1260 } |
| 1246 | 1261 |
| 1247 base::string16 RenderText::Elide(const base::string16& text, | 1262 base::string16 RenderText::Elide(const base::string16& text, |
| 1263 float text_width, |
| 1248 float available_width, | 1264 float available_width, |
| 1249 ElideBehavior behavior) { | 1265 ElideBehavior behavior) { |
| 1250 if (available_width <= 0 || text.empty()) | 1266 if (available_width <= 0 || text.empty()) |
| 1251 return base::string16(); | 1267 return base::string16(); |
| 1252 if (behavior == ELIDE_EMAIL) | 1268 if (behavior == ELIDE_EMAIL) |
| 1253 return ElideEmail(text, available_width); | 1269 return ElideEmail(text, available_width); |
| 1270 if (text_width > 0 && text_width < available_width) |
| 1271 return text; |
| 1272 |
| 1273 TRACE_EVENT0("ui", "RenderText::Elide"); |
| 1254 | 1274 |
| 1255 // Create a RenderText copy with attributes that affect the rendering width. | 1275 // Create a RenderText copy with attributes that affect the rendering width. |
| 1256 scoped_ptr<RenderText> render_text = CreateInstanceOfSameType(); | 1276 scoped_ptr<RenderText> render_text = CreateInstanceOfSameType(); |
| 1257 render_text->SetFontList(font_list_); | 1277 render_text->SetFontList(font_list_); |
| 1258 render_text->SetDirectionalityMode(directionality_mode_); | 1278 render_text->SetDirectionalityMode(directionality_mode_); |
| 1259 render_text->SetCursorEnabled(cursor_enabled_); | 1279 render_text->SetCursorEnabled(cursor_enabled_); |
| 1260 render_text->set_truncate_length(truncate_length_); | 1280 render_text->set_truncate_length(truncate_length_); |
| 1261 render_text->styles_ = styles_; | 1281 render_text->styles_ = styles_; |
| 1262 render_text->colors_ = colors_; | 1282 render_text->colors_ = colors_; |
| 1263 render_text->SetText(text); | 1283 if (text_width == 0) { |
| 1264 if (render_text->GetContentWidthF() <= available_width) | 1284 render_text->SetText(text); |
| 1285 text_width = render_text->GetContentWidthF(); |
| 1286 } |
| 1287 if (text_width <= available_width) |
| 1265 return text; | 1288 return text; |
| 1266 | 1289 |
| 1267 const base::string16 ellipsis = base::string16(kEllipsisUTF16); | 1290 const base::string16 ellipsis = base::string16(kEllipsisUTF16); |
| 1268 const bool insert_ellipsis = (behavior != TRUNCATE); | 1291 const bool insert_ellipsis = (behavior != TRUNCATE); |
| 1269 const bool elide_in_middle = (behavior == ELIDE_MIDDLE); | 1292 const bool elide_in_middle = (behavior == ELIDE_MIDDLE); |
| 1270 const bool elide_at_beginning = (behavior == ELIDE_HEAD); | 1293 const bool elide_at_beginning = (behavior == ELIDE_HEAD); |
| 1294 |
| 1295 if (insert_ellipsis) { |
| 1296 render_text->SetText(ellipsis); |
| 1297 const float ellipsis_width = render_text->GetContentWidthF(); |
| 1298 if (ellipsis_width > available_width) |
| 1299 return base::string16(); |
| 1300 } |
| 1301 |
| 1271 StringSlicer slicer(text, ellipsis, elide_in_middle, elide_at_beginning); | 1302 StringSlicer slicer(text, ellipsis, elide_in_middle, elide_at_beginning); |
| 1272 | 1303 |
| 1273 render_text->SetText(ellipsis); | |
| 1274 const float ellipsis_width = render_text->GetContentWidthF(); | |
| 1275 | |
| 1276 if (insert_ellipsis && (ellipsis_width > available_width)) | |
| 1277 return base::string16(); | |
| 1278 | |
| 1279 // Use binary search to compute the elided text. | 1304 // Use binary search to compute the elided text. |
| 1280 size_t lo = 0; | 1305 size_t lo = 0; |
| 1281 size_t hi = text.length() - 1; | 1306 size_t hi = text.length() - 1; |
| 1282 const base::i18n::TextDirection text_direction = GetTextDirection(); | 1307 const base::i18n::TextDirection text_direction = GetTextDirection(text); |
| 1283 for (size_t guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { | 1308 for (size_t guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { |
| 1284 // Restore colors. They will be truncated to size by SetText. | 1309 // Restore colors. They will be truncated to size by SetText. |
| 1285 render_text->colors_ = colors_; | 1310 render_text->colors_ = colors_; |
| 1286 base::string16 new_text = | 1311 base::string16 new_text = |
| 1287 slicer.CutString(guess, insert_ellipsis && behavior != ELIDE_TAIL); | 1312 slicer.CutString(guess, insert_ellipsis && behavior != ELIDE_TAIL); |
| 1288 render_text->SetText(new_text); | 1313 render_text->SetText(new_text); |
| 1289 | 1314 |
| 1290 // This has to be an additional step so that the ellipsis is rendered with | 1315 // This has to be an additional step so that the ellipsis is rendered with |
| 1291 // same style as trailing part of the text. | 1316 // same style as trailing part of the text. |
| 1292 if (insert_ellipsis && behavior == ELIDE_TAIL) { | 1317 if (insert_ellipsis && behavior == ELIDE_TAIL) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1377 // Elide the domain so that it only takes half of the available width. | 1402 // Elide the domain so that it only takes half of the available width. |
| 1378 // Should the username not need all the width available in its half, the | 1403 // Should the username not need all the width available in its half, the |
| 1379 // domain will occupy the leftover width. | 1404 // domain will occupy the leftover width. |
| 1380 // If |desired_domain_width| is greater than |available_domain_width|: the | 1405 // If |desired_domain_width| is greater than |available_domain_width|: the |
| 1381 // minimal username elision allowed by the specifications will not fit; thus | 1406 // minimal username elision allowed by the specifications will not fit; thus |
| 1382 // |desired_domain_width| must be <= |available_domain_width| at all cost. | 1407 // |desired_domain_width| must be <= |available_domain_width| at all cost. |
| 1383 const float desired_domain_width = | 1408 const float desired_domain_width = |
| 1384 std::min<float>(available_domain_width, | 1409 std::min<float>(available_domain_width, |
| 1385 std::max<float>(available_width - full_username_width, | 1410 std::max<float>(available_width - full_username_width, |
| 1386 available_width / 2)); | 1411 available_width / 2)); |
| 1387 domain = Elide(domain, desired_domain_width, ELIDE_MIDDLE); | 1412 domain = Elide(domain, 0, desired_domain_width, ELIDE_MIDDLE); |
| 1388 // Failing to elide the domain such that at least one character remains | 1413 // Failing to elide the domain such that at least one character remains |
| 1389 // (other than the ellipsis itself) remains: return a single ellipsis. | 1414 // (other than the ellipsis itself) remains: return a single ellipsis. |
| 1390 if (domain.length() <= 1U) | 1415 if (domain.length() <= 1U) |
| 1391 return base::string16(kEllipsisUTF16); | 1416 return base::string16(kEllipsisUTF16); |
| 1392 } | 1417 } |
| 1393 | 1418 |
| 1394 // Fit the username in the remaining width (at this point the elided username | 1419 // Fit the username in the remaining width (at this point the elided username |
| 1395 // is guaranteed to fit with at least one character remaining given all the | 1420 // is guaranteed to fit with at least one character remaining given all the |
| 1396 // precautions taken earlier). | 1421 // precautions taken earlier). |
| 1397 available_width -= GetStringWidthF(domain, font_list()); | 1422 available_width -= GetStringWidthF(domain, font_list()); |
| 1398 username = Elide(username, available_width, ELIDE_TAIL); | 1423 username = Elide(username, 0, available_width, ELIDE_TAIL); |
| 1399 return username + kAtSignUTF16 + domain; | 1424 return username + kAtSignUTF16 + domain; |
| 1400 } | 1425 } |
| 1401 | 1426 |
| 1402 void RenderText::UpdateCachedBoundsAndOffset() { | 1427 void RenderText::UpdateCachedBoundsAndOffset() { |
| 1403 if (cached_bounds_and_offset_valid_) | 1428 if (cached_bounds_and_offset_valid_) |
| 1404 return; | 1429 return; |
| 1405 | 1430 |
| 1406 // TODO(ckocagil): Add support for scrolling multiline text. | 1431 // TODO(ckocagil): Add support for scrolling multiline text. |
| 1407 | 1432 |
| 1408 int delta_x = 0; | 1433 int delta_x = 0; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1419 if (cursor_bounds_.right() > display_rect_.right()) | 1444 if (cursor_bounds_.right() > display_rect_.right()) |
| 1420 delta_x = display_rect_.right() - cursor_bounds_.right(); | 1445 delta_x = display_rect_.right() - cursor_bounds_.right(); |
| 1421 else if (cursor_bounds_.x() < display_rect_.x()) | 1446 else if (cursor_bounds_.x() < display_rect_.x()) |
| 1422 delta_x = display_rect_.x() - cursor_bounds_.x(); | 1447 delta_x = display_rect_.x() - cursor_bounds_.x(); |
| 1423 } | 1448 } |
| 1424 | 1449 |
| 1425 SetDisplayOffset(display_offset_.x() + delta_x); | 1450 SetDisplayOffset(display_offset_.x() + delta_x); |
| 1426 } | 1451 } |
| 1427 | 1452 |
| 1428 void RenderText::DrawSelection(Canvas* canvas) { | 1453 void RenderText::DrawSelection(Canvas* canvas) { |
| 1429 const std::vector<Rect> sel = GetSubstringBounds(selection()); | 1454 for (const Rect& s : GetSubstringBounds(selection())) |
| 1430 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | 1455 canvas->FillRect(s, selection_background_focused_color_); |
| 1431 canvas->FillRect(*i, selection_background_focused_color_); | |
| 1432 } | 1456 } |
| 1433 | 1457 |
| 1434 } // namespace gfx | 1458 } // namespace gfx |
| OLD | NEW |