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