summaryrefslogtreecommitdiffstats
path: root/ui/gfx
diff options
context:
space:
mode:
authordschuyler <dschuyler@chromium.org>2015-03-19 20:24:21 -0700
committerCommit bot <commit-bot@chromium.org>2015-03-20 03:25:12 +0000
commit92bc0de851bc0c094bbd1e3ac5e6b24202011d69 (patch)
treebbf6f67c997b0c20a6a317aed65e4fbc794e9379 /ui/gfx
parent9ace848b1e29993c394e79404466f60c142c7bba (diff)
downloadchromium_src-92bc0de851bc0c094bbd1e3ac5e6b24202011d69.zip
chromium_src-92bc0de851bc0c094bbd1e3ac5e6b24202011d69.tar.gz
chromium_src-92bc0de851bc0c094bbd1e3ac5e6b24202011d69.tar.bz2
[RenderText] Added append text
RenderText previously only had a SetText to alter the text. Calling SetText would reset the text styling already setup. It can be more efficient in some cases to preserve the existing font styling and when only adding text to the end of the RenderText. This patch adds AppendText that allows adding to an exiting RenderText without disturbing the existing styling. This change is related to (and used by) CL 795933009. BUG= Review URL: https://codereview.chromium.org/1014163002 Cr-Commit-Position: refs/heads/master@{#321507}
Diffstat (limited to 'ui/gfx')
-rw-r--r--ui/gfx/render_text.cc31
-rw-r--r--ui/gfx/render_text.h4
2 files changed, 25 insertions, 10 deletions
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc
index 2b51d8a..c3a4f00 100644
--- a/ui/gfx/render_text.cc
+++ b/ui/gfx/render_text.cc
@@ -446,19 +446,14 @@ void RenderText::SetText(const base::string16& text) {
if (text_ == text)
return;
text_ = text;
+ UpdateStyleLengths();
- // Adjust ranged styles, baselines, and colors to accommodate a new text
- // length. Clear style ranges as they might break new text graphemes and apply
+ // Clear style ranges as they might break new text graphemes and apply
// the first style to the whole text instead.
- const size_t text_length = text_.length();
- colors_.SetMax(text_length);
+ colors_.SetValue(colors_.breaks().begin()->second);
baselines_.SetValue(baselines_.breaks().begin()->second);
- baselines_.SetMax(text_length);
- for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) {
- BreakList<bool>& break_list = styles_[style];
- break_list.SetValue(break_list.breaks().begin()->second);
- break_list.SetMax(text_length);
- }
+ for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
+ styles_[style].SetValue(styles_[style].breaks().begin()->second);
cached_bounds_and_offset_valid_ = false;
// Reset selection model. SetText should always followed by SetSelectionModel
@@ -473,6 +468,14 @@ void RenderText::SetText(const base::string16& text) {
OnTextAttributeChanged();
}
+void RenderText::AppendText(const base::string16& text) {
+ text_ += text;
+ UpdateStyleLengths();
+ cached_bounds_and_offset_valid_ = false;
+ obscured_reveal_index_ = -1;
+ OnTextAttributeChanged();
+}
+
void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) {
if (horizontal_alignment_ != alignment) {
horizontal_alignment_ = alignment;
@@ -1241,6 +1244,14 @@ size_t RenderText::TextIndexToGivenTextIndex(const base::string16& given_text,
return std::min<size_t>(given_text.length(), i);
}
+void RenderText::UpdateStyleLengths() {
+ const size_t text_length = text_.length();
+ colors_.SetMax(text_length);
+ baselines_.SetMax(text_length);
+ for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
+ styles_[style].SetMax(text_length);
+}
+
// static
bool RenderText::RangeContainsCaret(const Range& range,
size_t caret_pos,
diff --git a/ui/gfx/render_text.h b/ui/gfx/render_text.h
index 6170c57..fe70f47 100644
--- a/ui/gfx/render_text.h
+++ b/ui/gfx/render_text.h
@@ -207,6 +207,7 @@ class GFX_EXPORT RenderText {
const base::string16& text() const { return text_; }
void SetText(const base::string16& text);
+ void AppendText(const base::string16& text);
HorizontalAlignment horizontal_alignment() const {
return horizontal_alignment_;
@@ -603,6 +604,9 @@ class GFX_EXPORT RenderText {
size_t TextIndexToGivenTextIndex(const base::string16& given_text,
size_t index);
+ // Adjust ranged styles to accommodate a new text length.
+ void UpdateStyleLengths();
+
// A convenience function to check whether the glyph attached to the caret
// is within the given range.
static bool RangeContainsCaret(const Range& range,