| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 split_style.range.set_end(new_range.start()); | 60 split_style.range.set_end(new_range.start()); |
| 61 i = style_ranges->insert(i, split_style) + 1; | 61 i = style_ranges->insert(i, split_style) + 1; |
| 62 i->range.set_start(new_range.end()); | 62 i->range.set_start(new_range.end()); |
| 63 break; | 63 break; |
| 64 } else if (i->range.start() < new_range.start()) { | 64 } else if (i->range.start() < new_range.start()) { |
| 65 i->range.set_end(new_range.start()); | 65 i->range.set_end(new_range.start()); |
| 66 i++; | 66 i++; |
| 67 } else if (i->range.end() > new_range.end()) { | 67 } else if (i->range.end() > new_range.end()) { |
| 68 i->range.set_start(new_range.end()); | 68 i->range.set_start(new_range.end()); |
| 69 break; | 69 break; |
| 70 } else { | 70 } else |
| 71 NOTREACHED(); | 71 NOTREACHED(); |
| 72 } | |
| 73 } | 72 } |
| 74 // Add the new range in its sorted location. | 73 // Add the new range in its sorted location. |
| 75 style_ranges->insert(i, style_range); | 74 style_ranges->insert(i, style_range); |
| 76 } | 75 } |
| 77 | 76 |
| 78 } // namespace | 77 } // namespace |
| 79 | 78 |
| 80 namespace gfx { | 79 namespace gfx { |
| 81 | 80 |
| 82 StyleRange::StyleRange() | 81 StyleRange::StyleRange() |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 if (break_type == WORD_BREAK) | 181 if (break_type == WORD_BREAK) |
| 183 position = GetRightSelectionModel(position, break_type); | 182 position = GetRightSelectionModel(position, break_type); |
| 184 } else { | 183 } else { |
| 185 position = GetRightSelectionModel(position, break_type); | 184 position = GetRightSelectionModel(position, break_type); |
| 186 } | 185 } |
| 187 if (select) | 186 if (select) |
| 188 position.set_selection_start(GetSelectionStart()); | 187 position.set_selection_start(GetSelectionStart()); |
| 189 MoveCursorTo(position); | 188 MoveCursorTo(position); |
| 190 } | 189 } |
| 191 | 190 |
| 192 bool RenderText::MoveCursorTo(const SelectionModel& model) { | 191 bool RenderText::MoveCursorTo(const SelectionModel& selection_model) { |
| 193 SelectionModel sel(model); | 192 SelectionModel sel(selection_model); |
| 194 size_t text_length = text().length(); | 193 size_t text_length = text().length(); |
| 195 // Enforce valid selection model components. | 194 // Enforce valid selection model components. |
| 196 if (sel.selection_start() > text_length) | 195 if (sel.selection_start() > text_length) |
| 197 sel.set_selection_start(text_length); | 196 sel.set_selection_start(text_length); |
| 198 if (sel.selection_end() > text_length) | 197 if (sel.selection_end() > text_length) |
| 199 sel.set_selection_end(text_length); | 198 sel.set_selection_end(text_length); |
| 200 // The current model only supports caret positions at valid character indices. | 199 // The current model only supports caret positions at valid character indices. |
| 201 if (text_length == 0) { | 200 if (text_length == 0) { |
| 202 sel.set_caret_pos(0); | 201 sel.set_caret_pos(0); |
| 203 sel.set_caret_placement(SelectionModel::LEADING); | 202 sel.set_caret_placement(SelectionModel::LEADING); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 int left_pos = 0; | 361 int left_pos = 0; |
| 363 int right = font.GetStringWidth(text()); | 362 int right = font.GetStringWidth(text()); |
| 364 int right_pos = text().length(); | 363 int right_pos = text().length(); |
| 365 | 364 |
| 366 int x = point.x() - (display_rect_.x() + GetUpdatedDisplayOffset().x()); | 365 int x = point.x() - (display_rect_.x() + GetUpdatedDisplayOffset().x()); |
| 367 if (x <= left) return SelectionModel(left_pos); | 366 if (x <= left) return SelectionModel(left_pos); |
| 368 if (x >= right) return SelectionModel(right_pos); | 367 if (x >= right) return SelectionModel(right_pos); |
| 369 // binary searching the cursor position. | 368 // binary searching the cursor position. |
| 370 // TODO(oshima): use the center of character instead of edge. | 369 // TODO(oshima): use the center of character instead of edge. |
| 371 // Binary search may not work for language like Arabic. | 370 // Binary search may not work for language like Arabic. |
| 372 while (std::abs(right_pos - left_pos) > 1) { | 371 while (std::abs(static_cast<long>(right_pos - left_pos)) > 1) { |
| 373 int pivot_pos = left_pos + (right_pos - left_pos) / 2; | 372 int pivot_pos = left_pos + (right_pos - left_pos) / 2; |
| 374 int pivot = font.GetStringWidth(text().substr(0, pivot_pos)); | 373 int pivot = font.GetStringWidth(text().substr(0, pivot_pos)); |
| 375 if (pivot < x) { | 374 if (pivot < x) { |
| 376 left = pivot; | 375 left = pivot; |
| 377 left_pos = pivot_pos; | 376 left_pos = pivot_pos; |
| 378 } else if (pivot == x) { | 377 } else if (pivot == x) { |
| 379 return SelectionModel(pivot_pos); | 378 return SelectionModel(pivot_pos); |
| 380 } else { | 379 } else { |
| 381 right = pivot; | 380 right = pivot; |
| 382 right_pos = pivot_pos; | 381 right_pos = pivot_pos; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 | 423 |
| 425 const Point& RenderText::GetUpdatedDisplayOffset() { | 424 const Point& RenderText::GetUpdatedDisplayOffset() { |
| 426 UpdateCachedBoundsAndOffset(); | 425 UpdateCachedBoundsAndOffset(); |
| 427 return display_offset_; | 426 return display_offset_; |
| 428 } | 427 } |
| 429 | 428 |
| 430 SelectionModel RenderText::GetLeftSelectionModel(const SelectionModel& current, | 429 SelectionModel RenderText::GetLeftSelectionModel(const SelectionModel& current, |
| 431 BreakType break_type) { | 430 BreakType break_type) { |
| 432 if (break_type == LINE_BREAK) | 431 if (break_type == LINE_BREAK) |
| 433 return LeftEndSelectionModel(); | 432 return LeftEndSelectionModel(); |
| 434 size_t pos = std::max<int>(current.selection_end() - 1, 0); | 433 size_t pos = std::max(static_cast<long>(current.selection_end() - 1), |
| 434 static_cast<long>(0)); |
| 435 if (break_type == CHARACTER_BREAK) | 435 if (break_type == CHARACTER_BREAK) |
| 436 return SelectionModel(pos, pos, SelectionModel::LEADING); | 436 return SelectionModel(pos, pos, SelectionModel::LEADING); |
| 437 | 437 |
| 438 // Notes: We always iterate words from the beginning. | 438 // Notes: We always iterate words from the beginning. |
| 439 // This is probably fast enough for our usage, but we may | 439 // This is probably fast enough for our usage, but we may |
| 440 // want to modify WordIterator so that it can start from the | 440 // want to modify WordIterator so that it can start from the |
| 441 // middle of string and advance backwards. | 441 // middle of string and advance backwards. |
| 442 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | 442 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
| 443 bool success = iter.Init(); | 443 bool success = iter.Init(); |
| 444 DCHECK(success); | 444 DCHECK(success); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? | 498 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? |
| 499 SelectionModel::LEADING : SelectionModel::TRAILING; | 499 SelectionModel::LEADING : SelectionModel::TRAILING; |
| 500 return SelectionModel(cursor, caret_pos, placement); | 500 return SelectionModel(cursor, caret_pos, placement); |
| 501 } | 501 } |
| 502 | 502 |
| 503 void RenderText::SetSelectionModel(const SelectionModel& model) { | 503 void RenderText::SetSelectionModel(const SelectionModel& model) { |
| 504 DCHECK_LE(model.selection_start(), text().length()); | 504 DCHECK_LE(model.selection_start(), text().length()); |
| 505 selection_model_.set_selection_start(model.selection_start()); | 505 selection_model_.set_selection_start(model.selection_start()); |
| 506 DCHECK_LE(model.selection_end(), text().length()); | 506 DCHECK_LE(model.selection_end(), text().length()); |
| 507 selection_model_.set_selection_end(model.selection_end()); | 507 selection_model_.set_selection_end(model.selection_end()); |
| 508 DCHECK_LT(model.caret_pos(), std::max<size_t>(text().length(), 1)); | 508 DCHECK_LT(model.caret_pos(), |
| 509 std::max(text().length(), static_cast<size_t>(1))); |
| 509 selection_model_.set_caret_pos(model.caret_pos()); | 510 selection_model_.set_caret_pos(model.caret_pos()); |
| 510 selection_model_.set_caret_placement(model.caret_placement()); | 511 selection_model_.set_caret_placement(model.caret_placement()); |
| 511 | 512 |
| 512 cached_bounds_and_offset_valid_ = false; | 513 cached_bounds_and_offset_valid_ = false; |
| 513 UpdateLayout(); | 514 UpdateLayout(); |
| 514 } | 515 } |
| 515 | 516 |
| 516 size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) { | 517 size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) { |
| 517 return IndexOfAdjacentGrapheme(position, false); | 518 return IndexOfAdjacentGrapheme(position, false); |
| 518 } | 519 } |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 void RenderText::DrawCursor(Canvas* canvas) { | 612 void RenderText::DrawCursor(Canvas* canvas) { |
| 612 // Paint cursor. Replace cursor is drawn as rectangle for now. | 613 // Paint cursor. Replace cursor is drawn as rectangle for now. |
| 613 // TODO(msw): Draw a better cursor with a better indication of association. | 614 // TODO(msw): Draw a better cursor with a better indication of association. |
| 614 if (cursor_visible() && focused()) { | 615 if (cursor_visible() && focused()) { |
| 615 Rect r(GetUpdatedCursorBounds()); | 616 Rect r(GetUpdatedCursorBounds()); |
| 616 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); | 617 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); |
| 617 } | 618 } |
| 618 } | 619 } |
| 619 | 620 |
| 620 } // namespace gfx | 621 } // namespace gfx |
| OLD | NEW |