| 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 | 8 |
| 9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 } | 387 } |
| 388 #ifndef NDEBUG | 388 #ifndef NDEBUG |
| 389 CheckStyleRanges(style_ranges_, text_.length()); | 389 CheckStyleRanges(style_ranges_, text_.length()); |
| 390 #endif | 390 #endif |
| 391 cached_bounds_and_offset_valid_ = false; | 391 cached_bounds_and_offset_valid_ = false; |
| 392 | 392 |
| 393 // Reset selection model. SetText should always followed by SetSelectionModel | 393 // Reset selection model. SetText should always followed by SetSelectionModel |
| 394 // or SetCursorPosition in upper layer. | 394 // or SetCursorPosition in upper layer. |
| 395 SetSelectionModel(SelectionModel()); | 395 SetSelectionModel(SelectionModel()); |
| 396 | 396 |
| 397 // Invalidate the cached text direction if it depends on the text contents. |
| 398 if (directionality_mode_ == DERIVE_FROM_TEXT) |
| 399 text_direction_ = base::i18n::UNKNOWN_DIRECTION; |
| 400 |
| 397 ResetLayout(); | 401 ResetLayout(); |
| 398 } | 402 } |
| 399 | 403 |
| 400 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { | 404 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { |
| 401 if (horizontal_alignment_ != alignment) { | 405 if (horizontal_alignment_ != alignment) { |
| 402 horizontal_alignment_ = alignment; | 406 horizontal_alignment_ = alignment; |
| 403 display_offset_ = Point(); | 407 display_offset_ = Point(); |
| 404 cached_bounds_and_offset_valid_ = false; | 408 cached_bounds_and_offset_valid_ = false; |
| 405 } | 409 } |
| 406 } | 410 } |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 | 593 |
| 590 void RenderText::ApplyDefaultStyle() { | 594 void RenderText::ApplyDefaultStyle() { |
| 591 style_ranges_.clear(); | 595 style_ranges_.clear(); |
| 592 StyleRange style = StyleRange(default_style_); | 596 StyleRange style = StyleRange(default_style_); |
| 593 style.range.set_end(text_.length()); | 597 style.range.set_end(text_.length()); |
| 594 style_ranges_.push_back(style); | 598 style_ranges_.push_back(style); |
| 595 cached_bounds_and_offset_valid_ = false; | 599 cached_bounds_and_offset_valid_ = false; |
| 596 ResetLayout(); | 600 ResetLayout(); |
| 597 } | 601 } |
| 598 | 602 |
| 603 void RenderText::SetDirectionalityMode(DirectionalityMode mode) { |
| 604 if (mode == directionality_mode_) |
| 605 return; |
| 606 |
| 607 directionality_mode_ = mode; |
| 608 text_direction_ = base::i18n::UNKNOWN_DIRECTION; |
| 609 ResetLayout(); |
| 610 } |
| 611 |
| 612 base::i18n::TextDirection RenderText::GetTextDirection() { |
| 613 if (text_direction_ == base::i18n::UNKNOWN_DIRECTION) { |
| 614 switch (directionality_mode_) { |
| 615 case DERIVE_FROM_TEXT: |
| 616 // Derive the direction from the display text, which differs from text() |
| 617 // in the case of obscured (password) textfields. |
| 618 text_direction_ = |
| 619 base::i18n::GetFirstStrongCharacterDirection(GetDisplayText()); |
| 620 break; |
| 621 case DERIVE_FROM_UI: |
| 622 text_direction_ = base::i18n::IsRTL() ? base::i18n::RIGHT_TO_LEFT : |
| 623 base::i18n::LEFT_TO_RIGHT; |
| 624 break; |
| 625 case FORCE_LTR: |
| 626 text_direction_ = base::i18n::LEFT_TO_RIGHT; |
| 627 break; |
| 628 case FORCE_RTL: |
| 629 text_direction_ = base::i18n::RIGHT_TO_LEFT; |
| 630 break; |
| 631 default: |
| 632 NOTREACHED(); |
| 633 } |
| 634 } |
| 635 |
| 636 return text_direction_; |
| 637 } |
| 638 |
| 599 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() { | 639 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() { |
| 600 return GetTextDirection() == base::i18n::LEFT_TO_RIGHT ? | 640 return GetTextDirection() == base::i18n::LEFT_TO_RIGHT ? |
| 601 CURSOR_RIGHT : CURSOR_LEFT; | 641 CURSOR_RIGHT : CURSOR_LEFT; |
| 602 } | 642 } |
| 603 | 643 |
| 604 void RenderText::Draw(Canvas* canvas) { | 644 void RenderText::Draw(Canvas* canvas) { |
| 605 EnsureLayout(); | 645 EnsureLayout(); |
| 606 | 646 |
| 607 if (clip_to_display_rect()) { | 647 if (clip_to_display_rect()) { |
| 608 gfx::Rect clip_rect(display_rect()); | 648 gfx::Rect clip_rect(display_rect()); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 return SelectionModel(sel.start(), | 734 return SelectionModel(sel.start(), |
| 695 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD); | 735 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD); |
| 696 } | 736 } |
| 697 | 737 |
| 698 void RenderText::SetTextShadows(const ShadowValues& shadows) { | 738 void RenderText::SetTextShadows(const ShadowValues& shadows) { |
| 699 text_shadows_ = shadows; | 739 text_shadows_ = shadows; |
| 700 } | 740 } |
| 701 | 741 |
| 702 RenderText::RenderText() | 742 RenderText::RenderText() |
| 703 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT), | 743 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT), |
| 744 directionality_mode_(DERIVE_FROM_TEXT), |
| 745 text_direction_(base::i18n::UNKNOWN_DIRECTION), |
| 704 cursor_enabled_(true), | 746 cursor_enabled_(true), |
| 705 cursor_visible_(false), | 747 cursor_visible_(false), |
| 706 insert_mode_(true), | 748 insert_mode_(true), |
| 707 cursor_color_(kDefaultCursorColor), | 749 cursor_color_(kDefaultCursorColor), |
| 708 selection_color_(kDefaultSelectionColor), | 750 selection_color_(kDefaultSelectionColor), |
| 709 selection_background_focused_color_(kDefaultSelectionBackgroundColor), | 751 selection_background_focused_color_(kDefaultSelectionBackgroundColor), |
| 710 selection_background_unfocused_color_(kDefaultSelectionBackgroundColor), | 752 selection_background_unfocused_color_(kDefaultSelectionBackgroundColor), |
| 711 focused_(false), | 753 focused_(false), |
| 712 composition_range_(ui::Range::InvalidRange()), | 754 composition_range_(ui::Range::InvalidRange()), |
| 713 obscured_(false), | 755 obscured_(false), |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 966 if (cursor_enabled() && cursor_visible() && focused()) { | 1008 if (cursor_enabled() && cursor_visible() && focused()) { |
| 967 const Rect& bounds = GetUpdatedCursorBounds(); | 1009 const Rect& bounds = GetUpdatedCursorBounds(); |
| 968 if (bounds.width() != 0) | 1010 if (bounds.width() != 0) |
| 969 canvas->FillRect(bounds, cursor_color_); | 1011 canvas->FillRect(bounds, cursor_color_); |
| 970 else | 1012 else |
| 971 canvas->DrawRect(bounds, cursor_color_); | 1013 canvas->DrawRect(bounds, cursor_color_); |
| 972 } | 1014 } |
| 973 } | 1015 } |
| 974 | 1016 |
| 975 } // namespace gfx | 1017 } // namespace gfx |
| OLD | NEW |