| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
| 13 #include "ui/gfx/canvas_skia.h" | 13 #include "ui/gfx/canvas_skia.h" |
| 14 #include "unicode/uchar.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 #ifndef NDEBUG | 18 #ifndef NDEBUG |
| 18 // Check StyleRanges invariant conditions: sorted and non-overlapping ranges. | 19 // Check StyleRanges invariant conditions: sorted and non-overlapping ranges. |
| 19 void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { | 20 void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { |
| 20 if (length == 0) { | 21 if (length == 0) { |
| 21 DCHECK(style_ranges.empty()) << "Style ranges exist for empty text."; | 22 DCHECK(style_ranges.empty()) << "Style ranges exist for empty text."; |
| 22 return; | 23 return; |
| 23 } | 24 } |
| 24 for (gfx::StyleRanges::size_type i = 0; i < style_ranges.size() - 1; i++) { | 25 for (gfx::StyleRanges::size_type i = 0; i < style_ranges.size() - 1; i++) { |
| 25 const ui::Range& former = style_ranges[i].range; | 26 const ui::Range& former = style_ranges[i].range; |
| 26 const ui::Range& latter = style_ranges[i + 1].range; | 27 const ui::Range& latter = style_ranges[i + 1].range; |
| 27 DCHECK(!former.is_empty()) << "Empty range at " << i << ":" << former; | 28 DCHECK(!former.is_empty()) << "Empty range at " << i << ":" << former; |
| 28 DCHECK(former.IsValid()) << "Invalid range at " << i << ":" << former; | 29 DCHECK(former.IsValid()) << "Invalid range at " << i << ":" << former; |
| 29 DCHECK(!former.is_reversed()) << "Reversed range at " << i << ":" << former; | 30 DCHECK(!former.is_reversed()) << "Reversed range at " << i << ":" << former; |
| 30 DCHECK(former.end() == latter.start()) << "Ranges gap/overlap/unsorted." << | 31 DCHECK(former.end() == latter.start()) << "Ranges gap/overlap/unsorted." << |
| 31 "former:" << former << ", latter:" << latter; | 32 "former:" << former << ", latter:" << latter; |
| 32 } | 33 } |
| 33 const gfx::StyleRange& end_style = *style_ranges.rbegin(); | 34 const gfx::StyleRange& end_style = *style_ranges.rbegin(); |
| 34 DCHECK(!end_style.range.is_empty()) << "Empty range at end."; | 35 DCHECK(!end_style.range.is_empty()) << "Empty range at end."; |
| 35 DCHECK(end_style.range.IsValid()) << "Invalid range at end."; | 36 DCHECK(end_style.range.IsValid()) << "Invalid range at end."; |
| 36 DCHECK(!end_style.range.is_reversed()) << "Reversed range at end."; | 37 DCHECK(!end_style.range.is_reversed()) << "Reversed range at end."; |
| 37 DCHECK(end_style.range.end() == length) << "Style and text length mismatch."; | 38 DCHECK(end_style.range.end() == length) << "Style and text length mismatch."; |
| 38 } | 39 } |
| 39 #endif | 40 #endif |
| 40 | 41 |
| 41 void ApplyStyleRangeImpl(gfx::StyleRanges& style_ranges, | 42 void ApplyStyleRangeImpl(gfx::StyleRanges* style_ranges, |
| 42 gfx::StyleRange style_range) { | 43 gfx::StyleRange style_range) { |
| 43 const ui::Range& new_range = style_range.range; | 44 const ui::Range& new_range = style_range.range; |
| 44 // Follow StyleRanges invariant conditions: sorted and non-overlapping ranges. | 45 // Follow StyleRanges invariant conditions: sorted and non-overlapping ranges. |
| 45 gfx::StyleRanges::iterator i; | 46 gfx::StyleRanges::iterator i; |
| 46 for (i = style_ranges.begin(); i != style_ranges.end();) { | 47 for (i = style_ranges->begin(); i != style_ranges->end();) { |
| 47 if (i->range.end() < new_range.start()) { | 48 if (i->range.end() < new_range.start()) { |
| 48 i++; | 49 i++; |
| 49 } else if (i->range.start() == new_range.end()) { | 50 } else if (i->range.start() == new_range.end()) { |
| 50 break; | 51 break; |
| 51 } else if (new_range.Contains(i->range)) { | 52 } else if (new_range.Contains(i->range)) { |
| 52 i = style_ranges.erase(i); | 53 i = style_ranges->erase(i); |
| 53 if (i == style_ranges.end()) | 54 if (i == style_ranges->end()) |
| 54 break; | 55 break; |
| 55 } else if (i->range.start() < new_range.start() && | 56 } else if (i->range.start() < new_range.start() && |
| 56 i->range.end() > new_range.end()) { | 57 i->range.end() > new_range.end()) { |
| 57 // Split the current style into two styles. | 58 // Split the current style into two styles. |
| 58 gfx::StyleRange split_style = gfx::StyleRange(*i); | 59 gfx::StyleRange split_style = gfx::StyleRange(*i); |
| 59 split_style.range.set_end(new_range.start()); | 60 split_style.range.set_end(new_range.start()); |
| 60 i = style_ranges.insert(i, split_style) + 1; | 61 i = style_ranges->insert(i, split_style) + 1; |
| 61 i->range.set_start(new_range.end()); | 62 i->range.set_start(new_range.end()); |
| 62 break; | 63 break; |
| 63 } else if (i->range.start() < new_range.start()) { | 64 } else if (i->range.start() < new_range.start()) { |
| 64 i->range.set_end(new_range.start()); | 65 i->range.set_end(new_range.start()); |
| 65 i++; | 66 i++; |
| 66 } else if (i->range.end() > new_range.end()) { | 67 } else if (i->range.end() > new_range.end()) { |
| 67 i->range.set_start(new_range.end()); | 68 i->range.set_start(new_range.end()); |
| 68 break; | 69 break; |
| 69 } else | 70 } else |
| 70 NOTREACHED(); | 71 NOTREACHED(); |
| 71 } | 72 } |
| 72 // Add the new range in its sorted location. | 73 // Add the new range in its sorted location. |
| 73 style_ranges.insert(i, style_range); | 74 style_ranges->insert(i, style_range); |
| 74 } | 75 } |
| 75 | 76 |
| 76 } // namespace | 77 } // namespace |
| 77 | 78 |
| 78 namespace gfx { | 79 namespace gfx { |
| 79 | 80 |
| 80 StyleRange::StyleRange() | 81 StyleRange::StyleRange() |
| 81 : font(), | 82 : font(), |
| 82 foreground(SK_ColorBLACK), | 83 foreground(SK_ColorBLACK), |
| 83 strike(false), | 84 strike(false), |
| 84 underline(false), | 85 underline(false), |
| 85 range() { | 86 range() { |
| 86 } | 87 } |
| 87 | 88 |
| 89 SelectionModel::SelectionModel() { |
| 90 Init(0, 0, 0, PREVIOUS_GRAPHEME_TRAILING); |
| 91 } |
| 92 |
| 93 SelectionModel::SelectionModel(size_t pos) { |
| 94 Init(pos, pos, pos, PREVIOUS_GRAPHEME_TRAILING); |
| 95 } |
| 96 |
| 97 SelectionModel::SelectionModel(size_t end, size_t pos, |
| 98 CaretPlacement placement) { |
| 99 Init(end, end, pos, placement); |
| 100 } |
| 101 |
| 102 SelectionModel::SelectionModel(size_t start, size_t end, size_t pos, |
| 103 CaretPlacement placement) { |
| 104 Init(start, end, pos, placement); |
| 105 } |
| 106 |
| 107 SelectionModel::~SelectionModel() { |
| 108 } |
| 109 |
| 110 bool SelectionModel::Equals(const SelectionModel& sel) const { |
| 111 return selection_start_ == sel.selection_start() |
| 112 && selection_end_ == sel.selection_end() |
| 113 && caret_pos_ == sel.caret_pos() |
| 114 && caret_placement_ == sel.caret_placement(); |
| 115 } |
| 116 |
| 117 void SelectionModel::Init(size_t start, size_t end, size_t pos, |
| 118 CaretPlacement placement) { |
| 119 selection_start_ = start; |
| 120 selection_end_ = end; |
| 121 caret_pos_ = pos; |
| 122 caret_placement_ = placement; |
| 123 } |
| 124 |
| 125 RenderText::~RenderText() { |
| 126 } |
| 127 |
| 88 void RenderText::SetText(const string16& text) { | 128 void RenderText::SetText(const string16& text) { |
| 89 size_t old_text_length = text_.length(); | 129 size_t old_text_length = text_.length(); |
| 90 text_ = text; | 130 text_ = text; |
| 91 | 131 |
| 92 // Update the style ranges as needed. | 132 // Update the style ranges as needed. |
| 93 if (text_.empty()) { | 133 if (text_.empty()) { |
| 94 style_ranges_.clear(); | 134 style_ranges_.clear(); |
| 95 } else if (style_ranges_.empty()) { | 135 } else if (style_ranges_.empty()) { |
| 96 ApplyDefaultStyle(); | 136 ApplyDefaultStyle(); |
| 97 } else if (text_.length() > old_text_length) { | 137 } else if (text_.length() > old_text_length) { |
| 98 style_ranges_.back().range.set_end(text_.length()); | 138 style_ranges_.back().range.set_end(text_.length()); |
| 99 } else if (text_.length() < old_text_length) { | 139 } else if (text_.length() < old_text_length) { |
| 100 StyleRanges::iterator i; | 140 StyleRanges::iterator i; |
| 101 for (i = style_ranges_.begin(); i != style_ranges_.end(); i++) { | 141 for (i = style_ranges_.begin(); i != style_ranges_.end(); i++) { |
| 102 if (i->range.start() >= text_.length()) { | 142 if (i->range.start() >= text_.length()) { |
| 103 i = style_ranges_.erase(i); | 143 i = style_ranges_.erase(i); |
| 104 if (i == style_ranges_.end()) | 144 if (i == style_ranges_.end()) |
| 105 break; | 145 break; |
| 106 } else if (i->range.end() > text_.length()) { | 146 } else if (i->range.end() > text_.length()) { |
| 107 i->range.set_end(text_.length()); | 147 i->range.set_end(text_.length()); |
| 108 } | 148 } |
| 109 } | 149 } |
| 110 style_ranges_.back().range.set_end(text_.length()); | 150 style_ranges_.back().range.set_end(text_.length()); |
| 111 } | 151 } |
| 112 #ifndef NDEBUG | 152 #ifndef NDEBUG |
| 113 CheckStyleRanges(style_ranges_, text_.length()); | 153 CheckStyleRanges(style_ranges_, text_.length()); |
| 114 #endif | 154 #endif |
| 115 } | 155 } |
| 116 | 156 |
| 117 size_t RenderText::GetCursorPosition() const { | 157 void RenderText::SetSelectionModel(const SelectionModel& sel) { |
| 118 return GetSelection().end(); | 158 size_t start = sel.selection_start(); |
| 159 size_t end = sel.selection_end(); |
| 160 selection_model_.set_selection_start(std::min(start, text().length())); |
| 161 selection_model_.set_selection_end(std::min(end, text().length())); |
| 162 selection_model_.set_caret_pos(std::min(sel.caret_pos(), text().length())); |
| 163 selection_model_.set_caret_placement(sel.caret_placement()); |
| 164 |
| 165 cursor_bounds_valid_ = false; |
| 119 } | 166 } |
| 120 | 167 |
| 168 |
| 169 size_t RenderText::GetCursorPosition() const { |
| 170 return selection_model_.selection_end(); |
| 171 } |
| 121 void RenderText::SetCursorPosition(const size_t position) { | 172 void RenderText::SetCursorPosition(const size_t position) { |
| 122 SetSelection(ui::Range(position, position)); | 173 SelectionModel sel(selection_model()); |
| 174 sel.set_selection_start(position); |
| 175 sel.set_selection_end(position); |
| 176 SetSelectionModel(sel); |
| 123 } | 177 } |
| 124 | 178 |
| 125 void RenderText::MoveCursorLeft(BreakType break_type, bool select) { | 179 void RenderText::MoveCursorLeft(BreakType break_type, bool select) { |
| 126 if (break_type == LINE_BREAK) { | 180 if (break_type == LINE_BREAK) { |
| 127 MoveCursorTo(0, select); | 181 SelectionModel selection(GetSelectionStart(), 0, |
| 182 0, SelectionModel::LEADING); |
| 183 if (!select) |
| 184 selection.set_selection_start(selection.selection_end()); |
| 185 MoveCursorTo(selection); |
| 128 return; | 186 return; |
| 129 } | 187 } |
| 130 size_t position = GetCursorPosition(); | 188 SelectionModel position = selection_model_; |
| 131 // Cancelling a selection moves to the edge of the selection. | 189 // Cancelling a selection moves to the edge of the selection. |
| 132 if (!GetSelection().is_empty() && !select) { | 190 if (!EmptySelection() && !select) { |
| 133 // Use the selection start if it is left of the selection end. | 191 // Use the selection start if it is left of the selection end. |
| 134 if (GetCursorBounds(GetSelection().start(), false).x() < | 192 SelectionModel selection_start(GetSelectionStart(), GetSelectionStart(), |
| 193 GetSelectionStart(), SelectionModel::LEADING); |
| 194 if (GetCursorBounds(selection_start, false).x() < |
| 135 GetCursorBounds(position, false).x()) | 195 GetCursorBounds(position, false).x()) |
| 136 position = GetSelection().start(); | 196 position = selection_start; |
| 137 // If |move_by_word|, use the nearest word boundary left of the selection. | 197 // If |move_by_word|, use the nearest word boundary left of the selection. |
| 138 if (break_type == WORD_BREAK) | 198 if (break_type == WORD_BREAK) |
| 139 position = GetLeftCursorPosition(position, true); | 199 position = GetLeftCursorPosition(position, true); |
| 140 } else { | 200 } else { |
| 141 position = GetLeftCursorPosition(position, break_type == WORD_BREAK); | 201 position = GetLeftCursorPosition(position, break_type == WORD_BREAK); |
| 142 } | 202 } |
| 143 MoveCursorTo(position, select); | 203 if (!select) |
| 204 position.set_selection_start(position.selection_end()); |
| 205 MoveCursorTo(position); |
| 144 } | 206 } |
| 145 | 207 |
| 146 void RenderText::MoveCursorRight(BreakType break_type, bool select) { | 208 void RenderText::MoveCursorRight(BreakType break_type, bool select) { |
| 147 if (break_type == LINE_BREAK) { | 209 if (break_type == LINE_BREAK) { |
| 148 MoveCursorTo(text().length(), select); | 210 SelectionModel selection(GetSelectionStart(), text().length(), |
| 211 text().length(), SelectionModel::PREVIOUS_GRAPHEME_TRAILING); |
| 212 if (!select) |
| 213 selection.set_selection_start(selection.selection_end()); |
| 214 MoveCursorTo(selection); |
| 149 return; | 215 return; |
| 150 } | 216 } |
| 151 size_t position = GetCursorPosition(); | 217 SelectionModel position = selection_model_; |
| 152 // Cancelling a selection moves to the edge of the selection. | 218 // Cancelling a selection moves to the edge of the selection. |
| 153 if (!GetSelection().is_empty() && !select) { | 219 if (!EmptySelection() && !select) { |
| 154 // Use the selection start if it is right of the selection end. | 220 // Use the selection start if it is right of the selection end. |
| 155 if (GetCursorBounds(GetSelection().start(), false).x() > | 221 SelectionModel selection_start(GetSelectionStart(), GetSelectionStart(), |
| 222 GetSelectionStart(), SelectionModel::LEADING); |
| 223 if (GetCursorBounds(selection_start, false).x() > |
| 156 GetCursorBounds(position, false).x()) | 224 GetCursorBounds(position, false).x()) |
| 157 position = GetSelection().start(); | 225 position = selection_start; |
| 158 // If |move_by_word|, use the nearest word boundary right of the selection. | 226 // If |move_by_word|, use the nearest word boundary right of the selection. |
| 159 if (break_type == WORD_BREAK) | 227 if (break_type == WORD_BREAK) |
| 160 position = GetRightCursorPosition(position, true); | 228 position = GetRightCursorPosition(position, true); |
| 161 } else { | 229 } else { |
| 162 position = GetRightCursorPosition(position, break_type == WORD_BREAK); | 230 position = GetRightCursorPosition(position, break_type == WORD_BREAK); |
| 163 } | 231 } |
| 164 MoveCursorTo(position, select); | 232 if (!select) |
| 233 position.set_selection_start(position.selection_end()); |
| 234 MoveCursorTo(position); |
| 165 } | 235 } |
| 166 | 236 |
| 167 bool RenderText::MoveCursorTo(size_t position, bool select) { | 237 bool RenderText::MoveCursorTo(const SelectionModel& selection) { |
| 168 bool changed = GetCursorPosition() != position || | 238 bool changed = !selection.Equals(selection_model_); |
| 169 select == GetSelection().is_empty(); | 239 SetSelectionModel(selection); |
| 170 if (select) | |
| 171 SetSelection(ui::Range(GetSelection().start(), position)); | |
| 172 else | |
| 173 SetSelection(ui::Range(position, position)); | |
| 174 return changed; | 240 return changed; |
| 175 } | 241 } |
| 176 | 242 |
| 177 bool RenderText::MoveCursorTo(const Point& point, bool select) { | 243 bool RenderText::MoveCursorTo(const Point& point, bool select) { |
| 178 // TODO(msw): Make this function support cursor placement via mouse near BiDi | 244 SelectionModel selection = FindCursorPosition(point); |
| 179 // level changes. The visual cursor appearance will depend on the location | 245 if (select) |
| 180 // clicked, not solely the resulting logical cursor position. See the TODO | 246 selection.set_selection_start(GetSelectionStart()); |
| 181 // note pertaining to selection_range_ for more information. | 247 else |
| 182 return MoveCursorTo(FindCursorPosition(point), select); | 248 selection.set_selection_start(selection.selection_end()); |
| 249 return MoveCursorTo(selection); |
| 183 } | 250 } |
| 184 | 251 |
| 185 const ui::Range& RenderText::GetSelection() const { | 252 bool RenderText::IsPointInSelection(const Point& point) { |
| 186 return selection_range_; | 253 // TODO(xji): should this check whether the point is inside the visual |
| 187 } | 254 // selection bounds? In case of "abcFED", if "ED" is selected, |point| points |
| 188 | 255 // to the right half of 'c', is the point in selection? |
| 189 void RenderText::SetSelection(const ui::Range& range) { | 256 size_t pos = FindCursorPosition(point).selection_end(); |
| 190 selection_range_.set_end(std::min(range.end(), text().length())); | 257 return (pos >= MinOfSelection() && pos < MaxOfSelection()); |
| 191 selection_range_.set_start(std::min(range.start(), text().length())); | |
| 192 | |
| 193 // Update |display_offset_| to ensure the current cursor is visible. | |
| 194 Rect cursor_bounds(GetCursorBounds(GetCursorPosition(), insert_mode())); | |
| 195 int display_width = display_rect_.width(); | |
| 196 int string_width = GetStringWidth(); | |
| 197 if (string_width < display_width) { | |
| 198 // Show all text whenever the text fits to the size. | |
| 199 display_offset_.set_x(0); | |
| 200 } else if ((display_offset_.x() + cursor_bounds.right()) > display_width) { | |
| 201 // Pan to show the cursor when it overflows to the right, | |
| 202 display_offset_.set_x(display_width - cursor_bounds.right()); | |
| 203 } else if ((display_offset_.x() + cursor_bounds.x()) < 0) { | |
| 204 // Pan to show the cursor when it overflows to the left. | |
| 205 display_offset_.set_x(-cursor_bounds.x()); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 bool RenderText::IsPointInSelection(const Point& point) const { | |
| 210 size_t pos = FindCursorPosition(point); | |
| 211 return (pos >= GetSelection().GetMin() && pos < GetSelection().GetMax()); | |
| 212 } | 258 } |
| 213 | 259 |
| 214 void RenderText::ClearSelection() { | 260 void RenderText::ClearSelection() { |
| 215 SetCursorPosition(GetCursorPosition()); | 261 SetCursorPosition(GetCursorPosition()); |
| 216 } | 262 } |
| 217 | 263 |
| 218 void RenderText::SelectAll() { | 264 void RenderText::SelectAll() { |
| 219 SetSelection(ui::Range(0, text().length())); | 265 SelectionModel sel(0, text().length(), |
| 266 text().length(), SelectionModel::LEADING); |
| 267 SetSelectionModel(sel); |
| 220 } | 268 } |
| 221 | 269 |
| 222 void RenderText::SelectWord() { | 270 void RenderText::SelectWord() { |
| 223 size_t selection_start = GetSelection().start(); | 271 size_t selection_start = GetSelectionStart(); |
| 224 size_t cursor_position = GetCursorPosition(); | 272 size_t cursor_position = GetCursorPosition(); |
| 225 // First we setup selection_start_ and cursor_pos_. There are so many cases | 273 // First we setup selection_start_ and selection_end_. There are so many cases |
| 226 // because we try to emulate what select-word looks like in a gtk textfield. | 274 // because we try to emulate what select-word looks like in a gtk textfield. |
| 227 // See associated testcase for different cases. | 275 // See associated testcase for different cases. |
| 228 if (cursor_position > 0 && cursor_position < text().length()) { | 276 if (cursor_position > 0 && cursor_position < text().length()) { |
| 229 if (isalnum(text()[cursor_position])) { | 277 if (u_isalnum(text()[cursor_position])) { |
| 230 selection_start = cursor_position; | 278 selection_start = cursor_position; |
| 231 cursor_position++; | 279 cursor_position++; |
| 232 } else | 280 } else |
| 233 selection_start = cursor_position - 1; | 281 selection_start = cursor_position - 1; |
| 234 } else if (cursor_position == 0) { | 282 } else if (cursor_position == 0) { |
| 235 selection_start = cursor_position; | 283 selection_start = cursor_position; |
| 236 if (text().length() > 0) | 284 if (text().length() > 0) |
| 237 cursor_position++; | 285 cursor_position++; |
| 238 } else { | 286 } else { |
| 239 selection_start = cursor_position - 1; | 287 selection_start = cursor_position - 1; |
| 240 } | 288 } |
| 241 | 289 |
| 242 // Now we move selection_start_ to beginning of selection. Selection boundary | 290 // Now we move selection_start_ to beginning of selection. Selection boundary |
| 243 // is defined as the position where we have alpha-num character on one side | 291 // is defined as the position where we have alpha-num character on one side |
| 244 // and non-alpha-num char on the other side. | 292 // and non-alpha-num char on the other side. |
| 245 for (; selection_start > 0; selection_start--) { | 293 for (; selection_start > 0; selection_start--) { |
| 246 if (IsPositionAtWordSelectionBoundary(selection_start)) | 294 if (IsPositionAtWordSelectionBoundary(selection_start)) |
| 247 break; | 295 break; |
| 248 } | 296 } |
| 249 | 297 |
| 250 // Now we move cursor_pos_ to end of selection. Selection boundary | 298 // Now we move selection_end_ to end of selection. Selection boundary |
| 251 // is defined as the position where we have alpha-num character on one side | 299 // is defined as the position where we have alpha-num character on one side |
| 252 // and non-alpha-num char on the other side. | 300 // and non-alpha-num char on the other side. |
| 253 for (; cursor_position < text().length(); cursor_position++) { | 301 for (; cursor_position < text().length(); cursor_position++) { |
| 254 if (IsPositionAtWordSelectionBoundary(cursor_position)) | 302 if (IsPositionAtWordSelectionBoundary(cursor_position)) |
| 255 break; | 303 break; |
| 256 } | 304 } |
| 257 | 305 |
| 258 SetSelection(ui::Range(selection_start, cursor_position)); | 306 SelectionModel sel(selection_model()); |
| 307 sel.set_selection_start(selection_start); |
| 308 sel.set_selection_end(cursor_position); |
| 309 sel.set_caret_placement(SelectionModel::PREVIOUS_GRAPHEME_TRAILING); |
| 310 SetSelectionModel(sel); |
| 259 } | 311 } |
| 260 | 312 |
| 261 const ui::Range& RenderText::GetCompositionRange() const { | 313 const ui::Range& RenderText::GetCompositionRange() const { |
| 262 return composition_range_; | 314 return composition_range_; |
| 263 } | 315 } |
| 264 | 316 |
| 265 void RenderText::SetCompositionRange(const ui::Range& composition_range) { | 317 void RenderText::SetCompositionRange(const ui::Range& composition_range) { |
| 266 CHECK(!composition_range.IsValid() || | 318 CHECK(!composition_range.IsValid() || |
| 267 ui::Range(0, text_.length()).Contains(composition_range)); | 319 ui::Range(0, text_.length()).Contains(composition_range)); |
| 268 composition_range_.set_end(composition_range.end()); | 320 composition_range_.set_end(composition_range.end()); |
| 269 composition_range_.set_start(composition_range.start()); | 321 composition_range_.set_start(composition_range.start()); |
| 270 } | 322 } |
| 271 | 323 |
| 272 void RenderText::ApplyStyleRange(StyleRange style_range) { | 324 void RenderText::ApplyStyleRange(StyleRange style_range) { |
| 273 const ui::Range& new_range = style_range.range; | 325 const ui::Range& new_range = style_range.range; |
| 274 if (!new_range.IsValid() || new_range.is_empty()) | 326 if (!new_range.IsValid() || new_range.is_empty()) |
| 275 return; | 327 return; |
| 276 CHECK(!new_range.is_reversed()); | 328 CHECK(!new_range.is_reversed()); |
| 277 CHECK(ui::Range(0, text_.length()).Contains(new_range)); | 329 CHECK(ui::Range(0, text_.length()).Contains(new_range)); |
| 278 ApplyStyleRangeImpl(style_ranges_, style_range); | 330 ApplyStyleRangeImpl(&style_ranges_, style_range); |
| 279 #ifndef NDEBUG | 331 #ifndef NDEBUG |
| 280 CheckStyleRanges(style_ranges_, text_.length()); | 332 CheckStyleRanges(style_ranges_, text_.length()); |
| 281 #endif | 333 #endif |
| 334 // TODO(xji): only invalidate cursor_bounds if font or underline change. |
| 335 cursor_bounds_valid_ = false; |
| 282 } | 336 } |
| 283 | 337 |
| 284 void RenderText::ApplyDefaultStyle() { | 338 void RenderText::ApplyDefaultStyle() { |
| 285 style_ranges_.clear(); | 339 style_ranges_.clear(); |
| 286 StyleRange style = StyleRange(default_style_); | 340 StyleRange style = StyleRange(default_style_); |
| 287 style.range.set_end(text_.length()); | 341 style.range.set_end(text_.length()); |
| 288 style_ranges_.push_back(style); | 342 style_ranges_.push_back(style); |
| 343 cursor_bounds_valid_ = false; |
| 289 } | 344 } |
| 290 | 345 |
| 291 base::i18n::TextDirection RenderText::GetTextDirection() const { | 346 base::i18n::TextDirection RenderText::GetTextDirection() const { |
| 292 // TODO(msw): Bidi implementation, intended to replace the functionality added | 347 // TODO(msw): Bidi implementation, intended to replace the functionality added |
| 293 // in crrev.com/91881 (discussed in codereview.chromium.org/7324011). | 348 // in crrev.com/91881 (discussed in codereview.chromium.org/7324011). |
| 294 return base::i18n::LEFT_TO_RIGHT; | 349 return base::i18n::LEFT_TO_RIGHT; |
| 295 } | 350 } |
| 296 | 351 |
| 297 int RenderText::GetStringWidth() const { | 352 int RenderText::GetStringWidth() { |
| 298 return GetSubstringBounds(ui::Range(0, text_.length()))[0].width(); | 353 return GetSubstringBounds(0, text_.length())[0].width(); |
| 299 } | 354 } |
| 300 | 355 |
| 301 void RenderText::Draw(Canvas* canvas) { | 356 void RenderText::Draw(Canvas* canvas) { |
| 302 // Clip the canvas to the text display area. | 357 // Clip the canvas to the text display area. |
| 303 canvas->ClipRectInt(display_rect_.x(), display_rect_.y(), | 358 canvas->ClipRectInt(display_rect_.x(), display_rect_.y(), |
| 304 display_rect_.width(), display_rect_.height()); | 359 display_rect_.width(), display_rect_.height()); |
| 305 | 360 |
| 306 // Draw the selection. | 361 // Draw the selection. |
| 307 std::vector<Rect> selection(GetSubstringBounds(GetSelection())); | 362 std::vector<Rect> selection(GetSubstringBounds(GetSelectionStart(), |
| 363 GetCursorPosition())); |
| 308 SkColor selection_color = | 364 SkColor selection_color = |
| 309 focused() ? kFocusedSelectionColor : kUnfocusedSelectionColor; | 365 focused() ? kFocusedSelectionColor : kUnfocusedSelectionColor; |
| 310 for (std::vector<Rect>::const_iterator i = selection.begin(); | 366 for (std::vector<Rect>::const_iterator i = selection.begin(); |
| 311 i < selection.end(); ++i) { | 367 i < selection.end(); ++i) { |
| 312 Rect r(*i); | 368 Rect r(*i); |
| 313 r.Offset(display_rect_.origin()); | 369 r.Offset(display_rect_.origin()); |
| 314 r.Offset(display_offset_); | 370 r.Offset(display_offset_); |
| 315 // Center the rect vertically in |display_rect_|. | 371 // Center the rect vertically in |display_rect_|. |
| 316 r.Offset(Point(0, (display_rect_.height() - r.height()) / 2)); | 372 r.Offset(Point(0, (display_rect_.height() - r.height()) / 2)); |
| 317 canvas->FillRectInt(selection_color, r.x(), r.y(), r.width(), r.height()); | 373 canvas->FillRectInt(selection_color, r.x(), r.y(), r.width(), r.height()); |
| 318 } | 374 } |
| 319 | 375 |
| 320 // Create a temporary copy of the style ranges for composition and selection. | 376 // Create a temporary copy of the style ranges for composition and selection. |
| 321 // TODO(msw): This pattern ought to be reconsidered; what about composition | |
| 322 // and selection overlaps, retain existing local style features? | |
| 323 StyleRanges style_ranges(style_ranges_); | 377 StyleRanges style_ranges(style_ranges_); |
| 324 // Apply a composition style override to a copy of the style ranges. | 378 ApplyCompositionAndSelectionStyles(&style_ranges); |
| 325 if (composition_range_.IsValid() && !composition_range_.is_empty()) { | |
| 326 StyleRange composition_style(default_style_); | |
| 327 composition_style.underline = true; | |
| 328 composition_style.range.set_start(composition_range_.start()); | |
| 329 composition_style.range.set_end(composition_range_.end()); | |
| 330 ApplyStyleRangeImpl(style_ranges, composition_style); | |
| 331 } | |
| 332 // Apply a selection style override to a copy of the style ranges. | |
| 333 if (selection_range_.IsValid() && !selection_range_.is_empty()) { | |
| 334 StyleRange selection_style(default_style_); | |
| 335 selection_style.foreground = kSelectedTextColor; | |
| 336 selection_style.range.set_start(selection_range_.GetMin()); | |
| 337 selection_style.range.set_end(selection_range_.GetMax()); | |
| 338 ApplyStyleRangeImpl(style_ranges, selection_style); | |
| 339 } | |
| 340 | 379 |
| 341 // Draw the text. | 380 // Draw the text. |
| 342 Rect bounds(display_rect_); | 381 Rect bounds(display_rect_); |
| 343 bounds.Offset(display_offset_); | 382 bounds.Offset(display_offset_); |
| 344 for (StyleRanges::const_iterator i = style_ranges.begin(); | 383 for (StyleRanges::const_iterator i = style_ranges.begin(); |
| 345 i < style_ranges.end(); ++i) { | 384 i < style_ranges.end(); ++i) { |
| 346 const Font& font = !i->underline ? i->font : | 385 const Font& font = !i->underline ? i->font : |
| 347 i->font.DeriveFont(0, i->font.GetStyle() | Font::UNDERLINED); | 386 i->font.DeriveFont(0, i->font.GetStyle() | Font::UNDERLINED); |
| 348 string16 text = text_.substr(i->range.start(), i->range.length()); | 387 string16 text = text_.substr(i->range.start(), i->range.length()); |
| 349 bounds.set_width(font.GetStringWidth(text)); | 388 bounds.set_width(font.GetStringWidth(text)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 361 SkIntToScalar(bounds.right()), | 400 SkIntToScalar(bounds.right()), |
| 362 SkIntToScalar(bounds.y()), | 401 SkIntToScalar(bounds.y()), |
| 363 paint); | 402 paint); |
| 364 } | 403 } |
| 365 | 404 |
| 366 bounds.set_x(bounds.x() + bounds.width()); | 405 bounds.set_x(bounds.x() + bounds.width()); |
| 367 } | 406 } |
| 368 | 407 |
| 369 // Paint cursor. Replace cursor is drawn as rectangle for now. | 408 // Paint cursor. Replace cursor is drawn as rectangle for now. |
| 370 if (cursor_visible() && focused()) { | 409 if (cursor_visible() && focused()) { |
| 371 bounds = GetCursorBounds(GetCursorPosition(), insert_mode()); | 410 bounds = CursorBounds(); |
| 372 bounds.Offset(display_offset_); | 411 bounds.Offset(display_offset_); |
| 373 if (!bounds.IsEmpty()) | 412 if (!bounds.IsEmpty()) |
| 374 canvas->DrawRectInt(kCursorColor, | 413 canvas->DrawRectInt(kCursorColor, |
| 375 bounds.x(), | 414 bounds.x(), |
| 376 bounds.y(), | 415 bounds.y(), |
| 377 bounds.width(), | 416 bounds.width(), |
| 378 bounds.height()); | 417 bounds.height()); |
| 379 } | 418 } |
| 380 } | 419 } |
| 381 | 420 |
| 382 size_t RenderText::FindCursorPosition(const Point& point) const { | 421 SelectionModel RenderText::FindCursorPosition(const Point& point) { |
| 383 const Font& font = default_style_.font; | 422 const Font& font = default_style_.font; |
| 384 int left = 0; | 423 int left = 0; |
| 385 int left_pos = 0; | 424 int left_pos = 0; |
| 386 int right = font.GetStringWidth(text()); | 425 int right = font.GetStringWidth(text()); |
| 387 int right_pos = text().length(); | 426 int right_pos = text().length(); |
| 388 | 427 |
| 389 int x = point.x(); | 428 int x = point.x(); |
| 390 if (x <= left) return left_pos; | 429 if (x <= left) return SelectionModel(left_pos); |
| 391 if (x >= right) return right_pos; | 430 if (x >= right) return SelectionModel(right_pos); |
| 392 // binary searching the cursor position. | 431 // binary searching the cursor position. |
| 393 // TODO(oshima): use the center of character instead of edge. | 432 // TODO(oshima): use the center of character instead of edge. |
| 394 // Binary search may not work for language like arabic. | 433 // Binary search may not work for language like arabic. |
| 395 while (std::abs(static_cast<long>(right_pos - left_pos) > 1)) { | 434 while (std::abs(static_cast<long>(right_pos - left_pos)) > 1) { |
| 396 int pivot_pos = left_pos + (right_pos - left_pos) / 2; | 435 int pivot_pos = left_pos + (right_pos - left_pos) / 2; |
| 397 int pivot = font.GetStringWidth(text().substr(0, pivot_pos)); | 436 int pivot = font.GetStringWidth(text().substr(0, pivot_pos)); |
| 398 if (pivot < x) { | 437 if (pivot < x) { |
| 399 left = pivot; | 438 left = pivot; |
| 400 left_pos = pivot_pos; | 439 left_pos = pivot_pos; |
| 401 } else if (pivot == x) { | 440 } else if (pivot == x) { |
| 402 return pivot_pos; | 441 return SelectionModel(pivot_pos); |
| 403 } else { | 442 } else { |
| 404 right = pivot; | 443 right = pivot; |
| 405 right_pos = pivot_pos; | 444 right_pos = pivot_pos; |
| 406 } | 445 } |
| 407 } | 446 } |
| 408 return left_pos; | 447 return SelectionModel(left_pos); |
| 409 } | 448 } |
| 410 | 449 |
| 411 std::vector<Rect> RenderText::GetSubstringBounds( | 450 std::vector<Rect> RenderText::GetSubstringBounds( |
| 412 const ui::Range& range) const { | 451 size_t from, size_t to) const { |
| 413 size_t start = range.GetMin(); | 452 size_t start = std::min(from, to); |
| 414 size_t end = range.GetMax(); | 453 size_t end = std::max(from, to); |
| 415 const Font& font = default_style_.font; | 454 const Font& font = default_style_.font; |
| 416 int start_x = font.GetStringWidth(text().substr(0, start)); | 455 int start_x = font.GetStringWidth(text().substr(0, start)); |
| 417 int end_x = font.GetStringWidth(text().substr(0, end)); | 456 int end_x = font.GetStringWidth(text().substr(0, end)); |
| 418 std::vector<Rect> bounds; | 457 std::vector<Rect> bounds; |
| 419 bounds.push_back(Rect(start_x, 0, end_x - start_x, font.GetHeight())); | 458 bounds.push_back(Rect(start_x, 0, end_x - start_x, font.GetHeight())); |
| 420 return bounds; | 459 return bounds; |
| 421 } | 460 } |
| 422 | 461 |
| 423 Rect RenderText::GetCursorBounds(size_t cursor_pos, bool insert_mode) const { | 462 Rect RenderText::GetCursorBounds(const SelectionModel& selection, |
| 463 bool insert_mode) { |
| 464 size_t cursor_pos = selection.selection_end(); |
| 424 const Font& font = default_style_.font; | 465 const Font& font = default_style_.font; |
| 425 int x = font.GetStringWidth(text_.substr(0U, cursor_pos)); | 466 int x = font.GetStringWidth(text_.substr(0U, cursor_pos)); |
| 426 DCHECK_GE(x, 0); | 467 DCHECK_GE(x, 0); |
| 427 int h = std::min(display_rect_.height(), font.GetHeight()); | 468 int h = std::min(display_rect_.height(), font.GetHeight()); |
| 428 Rect bounds(x, (display_rect_.height() - h) / 2, 1, h); | 469 Rect bounds(x, (display_rect_.height() - h) / 2, 1, h); |
| 429 if (!insert_mode && text_.length() != cursor_pos) | 470 if (!insert_mode && text_.length() != cursor_pos) |
| 430 bounds.set_width(font.GetStringWidth(text_.substr(0, cursor_pos + 1)) - x); | 471 bounds.set_width(font.GetStringWidth(text_.substr(0, cursor_pos + 1)) - x); |
| 431 return bounds; | 472 return bounds; |
| 432 } | 473 } |
| 433 | 474 |
| 434 size_t RenderText::GetLeftCursorPosition(size_t position, | 475 Rect RenderText::CursorBounds() { |
| 435 bool move_by_word) const { | 476 if (cursor_bounds_valid_ == false) { |
| 436 if (!move_by_word) | 477 UpdateCursorBoundsAndDisplayOffset(); |
| 437 return position == 0? position : position - 1; | 478 cursor_bounds_valid_ = true; |
| 479 } |
| 480 return cursor_bounds_; |
| 481 } |
| 482 |
| 483 RenderText::RenderText() |
| 484 : text_(), |
| 485 selection_model_(), |
| 486 cursor_bounds_(), |
| 487 cursor_bounds_valid_(false), |
| 488 cursor_visible_(false), |
| 489 insert_mode_(true), |
| 490 composition_range_(), |
| 491 style_ranges_(), |
| 492 default_style_(), |
| 493 display_rect_(), |
| 494 display_offset_() { |
| 495 } |
| 496 |
| 497 SelectionModel RenderText::GetLeftCursorPosition(const SelectionModel& current, |
| 498 bool move_by_word) { |
| 499 size_t position = current.selection_end(); |
| 500 SelectionModel left = current; |
| 501 if (!move_by_word) { |
| 502 left.set_selection_end(std::max(static_cast<long>(position - 1), |
| 503 static_cast<long>(0))); |
| 504 return left; |
| 505 } |
| 438 // Notes: We always iterate words from the begining. | 506 // Notes: We always iterate words from the begining. |
| 439 // This is probably fast enough for our usage, but we may | 507 // This is probably fast enough for our usage, but we may |
| 440 // want to modify WordIterator so that it can start from the | 508 // want to modify WordIterator so that it can start from the |
| 441 // middle of string and advance backwards. | 509 // middle of string and advance backwards. |
| 442 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | 510 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
| 443 bool success = iter.Init(); | 511 bool success = iter.Init(); |
| 444 DCHECK(success); | 512 DCHECK(success); |
| 445 if (!success) | 513 if (!success) { |
| 446 return position; | 514 left.set_selection_end(position); |
| 515 return left; |
| 516 } |
| 447 int last = 0; | 517 int last = 0; |
| 448 while (iter.Advance()) { | 518 while (iter.Advance()) { |
| 449 if (iter.IsWord()) { | 519 if (iter.IsWord()) { |
| 450 size_t begin = iter.pos() - iter.GetString().length(); | 520 size_t begin = iter.pos() - iter.GetString().length(); |
| 451 if (begin == position) { | 521 if (begin == position) { |
| 452 // The cursor is at the beginning of a word. | 522 // The cursor is at the beginning of a word. |
| 453 // Move to previous word. | 523 // Move to previous word. |
| 454 break; | 524 break; |
| 455 } else if(iter.pos() >= position) { | 525 } else if (iter.pos() >= position) { |
| 456 // The cursor is in the middle or at the end of a word. | 526 // The cursor is in the middle or at the end of a word. |
| 457 // Move to the top of current word. | 527 // Move to the top of current word. |
| 458 last = begin; | 528 last = begin; |
| 459 break; | 529 break; |
| 460 } else { | 530 } else { |
| 461 last = iter.pos() - iter.GetString().length(); | 531 last = iter.pos() - iter.GetString().length(); |
| 462 } | 532 } |
| 463 } | 533 } |
| 464 } | 534 } |
| 465 | 535 |
| 466 return last; | 536 left.set_selection_end(last); |
| 537 return left; |
| 467 } | 538 } |
| 468 | 539 |
| 469 size_t RenderText::GetRightCursorPosition(size_t position, | 540 SelectionModel RenderText::GetRightCursorPosition(const SelectionModel& current, |
| 470 bool move_by_word) const { | 541 bool move_by_word) { |
| 471 if (!move_by_word) | 542 size_t position = current.selection_end(); |
| 472 return std::min(position + 1, text().length()); | 543 SelectionModel right = current; |
| 544 |
| 545 if (!move_by_word) { |
| 546 right.set_selection_end(std::min(position + 1, text().length())); |
| 547 return right; |
| 548 } |
| 549 |
| 473 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | 550 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
| 474 bool success = iter.Init(); | 551 bool success = iter.Init(); |
| 475 DCHECK(success); | 552 DCHECK(success); |
| 476 if (!success) | 553 if (!success) { |
| 477 return position; | 554 right.set_selection_end(position); |
| 555 return right; |
| 556 } |
| 478 size_t pos = 0; | 557 size_t pos = 0; |
| 479 while (iter.Advance()) { | 558 while (iter.Advance()) { |
| 480 pos = iter.pos(); | 559 pos = iter.pos(); |
| 481 if (iter.IsWord() && pos > position) { | 560 if (iter.IsWord() && pos > position) { |
| 482 break; | 561 break; |
| 483 } | 562 } |
| 484 } | 563 } |
| 485 return pos; | 564 right.set_selection_end(pos); |
| 565 return right; |
| 486 } | 566 } |
| 487 | 567 |
| 488 RenderText::RenderText() | 568 void RenderText::ApplyCompositionAndSelectionStyles( |
| 489 : text_(), | 569 StyleRanges* style_ranges) const { |
| 490 selection_range_(), | 570 // TODO(msw): This pattern ought to be reconsidered; what about composition |
| 491 cursor_visible_(false), | 571 // and selection overlaps, retain existing local style features? |
| 492 insert_mode_(true), | 572 // Apply a composition style override to a copy of the style ranges. |
| 493 composition_range_(), | 573 if (composition_range_.IsValid() && !composition_range_.is_empty()) { |
| 494 style_ranges_(), | 574 StyleRange composition_style(default_style_); |
| 495 default_style_(), | 575 composition_style.underline = true; |
| 496 display_rect_(), | 576 composition_style.range.set_start(composition_range_.start()); |
| 497 display_offset_() { | 577 composition_style.range.set_end(composition_range_.end()); |
| 498 } | 578 ApplyStyleRangeImpl(style_ranges, composition_style); |
| 499 | 579 } |
| 500 RenderText::~RenderText() { | 580 // Apply a selection style override to a copy of the style ranges. |
| 581 if (!EmptySelection()) { |
| 582 StyleRange selection_style(default_style_); |
| 583 selection_style.foreground = kSelectedTextColor; |
| 584 selection_style.range.set_start(MinOfSelection()); |
| 585 selection_style.range.set_end(MaxOfSelection()); |
| 586 ApplyStyleRangeImpl(style_ranges, selection_style); |
| 587 } |
| 501 } | 588 } |
| 502 | 589 |
| 503 bool RenderText::IsPositionAtWordSelectionBoundary(size_t pos) { | 590 bool RenderText::IsPositionAtWordSelectionBoundary(size_t pos) { |
| 504 return pos == 0 || (isalnum(text()[pos - 1]) && !isalnum(text()[pos])) || | 591 return pos == 0 || (u_isalnum(text()[pos - 1]) && !u_isalnum(text()[pos])) || |
| 505 (!isalnum(text()[pos - 1]) && isalnum(text()[pos])); | 592 (!u_isalnum(text()[pos - 1]) && u_isalnum(text()[pos])); |
| 593 } |
| 594 |
| 595 void RenderText::UpdateCursorBoundsAndDisplayOffset() { |
| 596 cursor_bounds_ = GetCursorBounds(selection_model_, insert_mode()); |
| 597 // Update |display_offset_| to ensure the current cursor is visible. |
| 598 int display_width = display_rect_.width(); |
| 599 int string_width = GetStringWidth(); |
| 600 if (string_width < display_width) { |
| 601 // Show all text whenever the text fits to the size. |
| 602 display_offset_.set_x(0); |
| 603 } else if ((display_offset_.x() + cursor_bounds_.right()) > display_width) { |
| 604 // Pan to show the cursor when it overflows to the right, |
| 605 display_offset_.set_x(display_width - cursor_bounds_.right()); |
| 606 } else if ((display_offset_.x() + cursor_bounds_.x()) < 0) { |
| 607 // Pan to show the cursor when it overflows to the left. |
| 608 display_offset_.set_x(-cursor_bounds_.x()); |
| 609 } |
| 506 } | 610 } |
| 507 | 611 |
| 508 } // namespace gfx | 612 } // namespace gfx |
| OLD | NEW |