OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/render_text.h" | 5 #include "ui/gfx/render_text.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <climits> | 8 #include <climits> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 1169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1180 text_direction_ = base::i18n::RIGHT_TO_LEFT; | 1180 text_direction_ = base::i18n::RIGHT_TO_LEFT; |
1181 break; | 1181 break; |
1182 default: | 1182 default: |
1183 NOTREACHED(); | 1183 NOTREACHED(); |
1184 } | 1184 } |
1185 } | 1185 } |
1186 | 1186 |
1187 return text_direction_; | 1187 return text_direction_; |
1188 } | 1188 } |
1189 | 1189 |
| 1190 size_t RenderText::TextIndexToGivenTextIndex(const base::string16& given_text, |
| 1191 size_t index) { |
| 1192 DCHECK(given_text == layout_text() || given_text == display_text()); |
| 1193 DCHECK_LE(index, text().length()); |
| 1194 ptrdiff_t i = obscured() ? UTF16IndexToOffset(text(), 0, index) : index; |
| 1195 CHECK_GE(i, 0); |
| 1196 // Clamp indices to the length of the given layout or display text. |
| 1197 return std::min<size_t>(given_text.length(), i); |
| 1198 } |
| 1199 |
1190 // static | 1200 // static |
1191 bool RenderText::RangeContainsCaret(const Range& range, | 1201 bool RenderText::RangeContainsCaret(const Range& range, |
1192 size_t caret_pos, | 1202 size_t caret_pos, |
1193 LogicalCursorDirection caret_affinity) { | 1203 LogicalCursorDirection caret_affinity) { |
1194 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9). | 1204 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9). |
1195 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ? | 1205 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ? |
1196 caret_pos - 1 : caret_pos + 1; | 1206 caret_pos - 1 : caret_pos + 1; |
1197 return range.Contains(Range(caret_pos, adjacent)); | 1207 return range.Contains(Range(caret_pos, adjacent)); |
1198 } | 1208 } |
1199 | 1209 |
1200 void RenderText::MoveCursorTo(size_t position, bool select) { | 1210 void RenderText::MoveCursorTo(size_t position, bool select) { |
1201 size_t cursor = std::min(position, text().length()); | 1211 size_t cursor = std::min(position, text().length()); |
1202 if (IsValidCursorIndex(cursor)) | 1212 if (IsValidCursorIndex(cursor)) |
1203 SetSelectionModel(SelectionModel( | 1213 SetSelectionModel(SelectionModel( |
1204 Range(select ? selection().start() : cursor, cursor), | 1214 Range(select ? selection().start() : cursor, cursor), |
1205 (cursor == 0) ? CURSOR_FORWARD : CURSOR_BACKWARD)); | 1215 (cursor == 0) ? CURSOR_FORWARD : CURSOR_BACKWARD)); |
1206 } | 1216 } |
1207 | 1217 |
1208 void RenderText::OnTextAttributeChanged() { | 1218 void RenderText::OnTextAttributeChanged() { |
1209 layout_text_.clear(); | 1219 layout_text_.clear(); |
1210 display_text_.clear(); | 1220 display_text_.clear(); |
| 1221 text_elided_ = false; |
1211 line_breaks_.SetMax(0); | 1222 line_breaks_.SetMax(0); |
1212 | 1223 |
1213 if (obscured_) { | 1224 if (obscured_) { |
1214 size_t obscured_text_length = | 1225 size_t obscured_text_length = |
1215 static_cast<size_t>(UTF16IndexToOffset(text_, 0, text_.length())); | 1226 static_cast<size_t>(UTF16IndexToOffset(text_, 0, text_.length())); |
1216 layout_text_.assign(obscured_text_length, kPasswordReplacementChar); | 1227 layout_text_.assign(obscured_text_length, kPasswordReplacementChar); |
1217 | 1228 |
1218 if (obscured_reveal_index_ >= 0 && | 1229 if (obscured_reveal_index_ >= 0 && |
1219 obscured_reveal_index_ < static_cast<int>(text_.length())) { | 1230 obscured_reveal_index_ < static_cast<int>(text_.length())) { |
1220 // Gets the index range in |text_| to be revealed. | 1231 // Gets the index range in |text_| to be revealed. |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1453 | 1464 |
1454 SetDisplayOffset(display_offset_.x() + delta_x); | 1465 SetDisplayOffset(display_offset_.x() + delta_x); |
1455 } | 1466 } |
1456 | 1467 |
1457 void RenderText::DrawSelection(Canvas* canvas) { | 1468 void RenderText::DrawSelection(Canvas* canvas) { |
1458 for (const Rect& s : GetSubstringBounds(selection())) | 1469 for (const Rect& s : GetSubstringBounds(selection())) |
1459 canvas->FillRect(s, selection_background_focused_color_); | 1470 canvas->FillRect(s, selection_background_focused_color_); |
1460 } | 1471 } |
1461 | 1472 |
1462 } // namespace gfx | 1473 } // namespace gfx |
OLD | NEW |