Chromium Code Reviews| 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" |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 | 407 |
| 408 void RenderText::SetText(const base::string16& text) { | 408 void RenderText::SetText(const base::string16& text) { |
| 409 DCHECK(!composition_range_.IsValid()); | 409 DCHECK(!composition_range_.IsValid()); |
| 410 if (text_ == text) | 410 if (text_ == text) |
| 411 return; | 411 return; |
| 412 text_ = text; | 412 text_ = text; |
| 413 | 413 |
| 414 // Adjust ranged styles and colors to accommodate a new text length. | 414 // Adjust ranged styles and colors to accommodate a new text length. |
| 415 const size_t text_length = text_.length(); | 415 const size_t text_length = text_.length(); |
| 416 colors_.SetMax(text_length); | 416 colors_.SetMax(text_length); |
| 417 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) | 417 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) { |
| 418 styles_[style].SetMax(text_length); | 418 BreakList<bool>& break_list = styles_[style]; |
| 419 break_list.SetMax(text_length); | |
| 420 // Clear style ranges as they might break new text graphemes. | |
|
msw
2014/09/08 19:29:34
nit: Merge this comment with the one on line 414.
Tomasz Moniuszko
2014/09/09 09:59:15
Done.
| |
| 421 // Apply first style to the whole text instead. | |
|
msw
2014/09/08 19:29:34
nit: "Apply the first".
Tomasz Moniuszko
2014/09/09 09:59:15
Done.
| |
| 422 if (break_list.breaks().size() > 1) { | |
| 423 break_list.SetValue(break_list.breaks().begin()->second); | |
|
msw
2014/09/08 19:29:34
Do this unconditionally, before calling SetMax.
Tomasz Moniuszko
2014/09/09 09:59:15
Done.
| |
| 424 } | |
| 425 } | |
| 419 cached_bounds_and_offset_valid_ = false; | 426 cached_bounds_and_offset_valid_ = false; |
| 420 | 427 |
| 421 // Reset selection model. SetText should always followed by SetSelectionModel | 428 // Reset selection model. SetText should always followed by SetSelectionModel |
| 422 // or SetCursorPosition in upper layer. | 429 // or SetCursorPosition in upper layer. |
| 423 SetSelectionModel(SelectionModel()); | 430 SetSelectionModel(SelectionModel()); |
| 424 | 431 |
| 425 // Invalidate the cached text direction if it depends on the text contents. | 432 // Invalidate the cached text direction if it depends on the text contents. |
| 426 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT) | 433 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT) |
| 427 text_direction_ = base::i18n::UNKNOWN_DIRECTION; | 434 text_direction_ = base::i18n::UNKNOWN_DIRECTION; |
| 428 | 435 |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 644 } | 651 } |
| 645 | 652 |
| 646 void RenderText::SetStyle(TextStyle style, bool value) { | 653 void RenderText::SetStyle(TextStyle style, bool value) { |
| 647 styles_[style].SetValue(value); | 654 styles_[style].SetValue(value); |
| 648 | 655 |
| 649 cached_bounds_and_offset_valid_ = false; | 656 cached_bounds_and_offset_valid_ = false; |
| 650 ResetLayout(); | 657 ResetLayout(); |
| 651 } | 658 } |
| 652 | 659 |
| 653 void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) { | 660 void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) { |
| 654 styles_[style].ApplyValue(value, range); | 661 // Do not change styles mid-grapheme to avoid breaking ligatures. |
| 662 const size_t start = IsValidCursorIndex(range.start()) ? range.start() : | |
| 663 IndexOfAdjacentGrapheme(range.start(), CURSOR_BACKWARD); | |
| 664 const size_t end = IsValidCursorIndex(range.end()) ? range.end() : | |
| 665 IndexOfAdjacentGrapheme(range.end(), CURSOR_FORWARD); | |
| 666 styles_[style].ApplyValue(value, Range(start, end)); | |
| 655 | 667 |
| 656 cached_bounds_and_offset_valid_ = false; | 668 cached_bounds_and_offset_valid_ = false; |
| 657 ResetLayout(); | 669 ResetLayout(); |
| 658 } | 670 } |
| 659 | 671 |
| 660 bool RenderText::GetStyle(TextStyle style) const { | 672 bool RenderText::GetStyle(TextStyle style) const { |
| 661 return (styles_[style].breaks().size() == 1) && | 673 return (styles_[style].breaks().size() == 1) && |
| 662 styles_[style].breaks().front().second; | 674 styles_[style].breaks().front().second; |
| 663 } | 675 } |
| 664 | 676 |
| (...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1368 SetDisplayOffset(display_offset_.x() + delta_x); | 1380 SetDisplayOffset(display_offset_.x() + delta_x); |
| 1369 } | 1381 } |
| 1370 | 1382 |
| 1371 void RenderText::DrawSelection(Canvas* canvas) { | 1383 void RenderText::DrawSelection(Canvas* canvas) { |
| 1372 const std::vector<Rect> sel = GetSubstringBounds(selection()); | 1384 const std::vector<Rect> sel = GetSubstringBounds(selection()); |
| 1373 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | 1385 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) |
| 1374 canvas->FillRect(*i, selection_background_focused_color_); | 1386 canvas->FillRect(*i, selection_background_focused_color_); |
| 1375 } | 1387 } |
| 1376 | 1388 |
| 1377 } // namespace gfx | 1389 } // namespace gfx |
| OLD | NEW |