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