Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(372)

Side by Side Diff: ui/gfx/render_text.cc

Issue 1014163002: [RenderText] Added append text (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changed UpdateStyleLengths comment Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/render_text.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 // static 439 // static
440 RenderText* RenderText::CreateInstanceForEditing() { 440 RenderText* RenderText::CreateInstanceForEditing() {
441 return new RenderTextHarfBuzz; 441 return new RenderTextHarfBuzz;
442 } 442 }
443 443
444 void RenderText::SetText(const base::string16& text) { 444 void RenderText::SetText(const base::string16& text) {
445 DCHECK(!composition_range_.IsValid()); 445 DCHECK(!composition_range_.IsValid());
446 if (text_ == text) 446 if (text_ == text)
447 return; 447 return;
448 text_ = text; 448 text_ = text;
449 UpdateStyleLengths();
449 450
450 // Adjust ranged styles, baselines, and colors to accommodate a new text 451 // Clear style ranges as they might break new text graphemes and apply
451 // length. Clear style ranges as they might break new text graphemes and apply
452 // the first style to the whole text instead. 452 // the first style to the whole text instead.
453 const size_t text_length = text_.length(); 453 colors_.SetValue(colors_.breaks().begin()->second);
454 colors_.SetMax(text_length);
455 baselines_.SetValue(baselines_.breaks().begin()->second); 454 baselines_.SetValue(baselines_.breaks().begin()->second);
456 baselines_.SetMax(text_length); 455 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
457 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) { 456 styles_[style].SetValue(styles_[style].breaks().begin()->second);
458 BreakList<bool>& break_list = styles_[style];
459 break_list.SetValue(break_list.breaks().begin()->second);
460 break_list.SetMax(text_length);
461 }
462 cached_bounds_and_offset_valid_ = false; 457 cached_bounds_and_offset_valid_ = false;
463 458
464 // Reset selection model. SetText should always followed by SetSelectionModel 459 // Reset selection model. SetText should always followed by SetSelectionModel
465 // or SetCursorPosition in upper layer. 460 // or SetCursorPosition in upper layer.
466 SetSelectionModel(SelectionModel()); 461 SetSelectionModel(SelectionModel());
467 462
468 // Invalidate the cached text direction if it depends on the text contents. 463 // Invalidate the cached text direction if it depends on the text contents.
469 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT) 464 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT)
470 text_direction_ = base::i18n::UNKNOWN_DIRECTION; 465 text_direction_ = base::i18n::UNKNOWN_DIRECTION;
471 466
472 obscured_reveal_index_ = -1; 467 obscured_reveal_index_ = -1;
473 OnTextAttributeChanged(); 468 OnTextAttributeChanged();
474 } 469 }
475 470
471 void RenderText::AppendText(const base::string16& text) {
472 text_ += text;
473 UpdateStyleLengths();
474 cached_bounds_and_offset_valid_ = false;
475 obscured_reveal_index_ = -1;
476 OnTextAttributeChanged();
477 }
478
476 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { 479 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) {
477 if (horizontal_alignment_ != alignment) { 480 if (horizontal_alignment_ != alignment) {
478 horizontal_alignment_ = alignment; 481 horizontal_alignment_ = alignment;
479 display_offset_ = Vector2d(); 482 display_offset_ = Vector2d();
480 cached_bounds_and_offset_valid_ = false; 483 cached_bounds_and_offset_valid_ = false;
481 } 484 }
482 } 485 }
483 486
484 void RenderText::SetFontList(const FontList& font_list) { 487 void RenderText::SetFontList(const FontList& font_list) {
485 font_list_ = font_list; 488 font_list_ = font_list;
(...skipping 748 matching lines...) Expand 10 before | Expand all | Expand 10 after
1234 size_t RenderText::TextIndexToGivenTextIndex(const base::string16& given_text, 1237 size_t RenderText::TextIndexToGivenTextIndex(const base::string16& given_text,
1235 size_t index) { 1238 size_t index) {
1236 DCHECK(given_text == layout_text() || given_text == display_text()); 1239 DCHECK(given_text == layout_text() || given_text == display_text());
1237 DCHECK_LE(index, text().length()); 1240 DCHECK_LE(index, text().length());
1238 ptrdiff_t i = obscured() ? UTF16IndexToOffset(text(), 0, index) : index; 1241 ptrdiff_t i = obscured() ? UTF16IndexToOffset(text(), 0, index) : index;
1239 CHECK_GE(i, 0); 1242 CHECK_GE(i, 0);
1240 // Clamp indices to the length of the given layout or display text. 1243 // Clamp indices to the length of the given layout or display text.
1241 return std::min<size_t>(given_text.length(), i); 1244 return std::min<size_t>(given_text.length(), i);
1242 } 1245 }
1243 1246
1247 void RenderText::UpdateStyleLengths() {
1248 const size_t text_length = text_.length();
1249 colors_.SetMax(text_length);
1250 baselines_.SetMax(text_length);
1251 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
1252 styles_[style].SetMax(text_length);
1253 }
1254
1244 // static 1255 // static
1245 bool RenderText::RangeContainsCaret(const Range& range, 1256 bool RenderText::RangeContainsCaret(const Range& range,
1246 size_t caret_pos, 1257 size_t caret_pos,
1247 LogicalCursorDirection caret_affinity) { 1258 LogicalCursorDirection caret_affinity) {
1248 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9). 1259 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9).
1249 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ? 1260 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ?
1250 caret_pos - 1 : caret_pos + 1; 1261 caret_pos - 1 : caret_pos + 1;
1251 return range.Contains(Range(caret_pos, adjacent)); 1262 return range.Contains(Range(caret_pos, adjacent));
1252 } 1263 }
1253 1264
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
1496 1507
1497 SetDisplayOffset(display_offset_.x() + delta_x); 1508 SetDisplayOffset(display_offset_.x() + delta_x);
1498 } 1509 }
1499 1510
1500 void RenderText::DrawSelection(Canvas* canvas) { 1511 void RenderText::DrawSelection(Canvas* canvas) {
1501 for (const Rect& s : GetSubstringBounds(selection())) 1512 for (const Rect& s : GetSubstringBounds(selection()))
1502 canvas->FillRect(s, selection_background_focused_color_); 1513 canvas->FillRect(s, selection_background_focused_color_);
1503 } 1514 }
1504 1515
1505 } // namespace gfx 1516 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698