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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 style_ranges_.back().range.set_end(text_.length()); | 116 style_ranges_.back().range.set_end(text_.length()); |
117 } | 117 } |
118 #ifndef NDEBUG | 118 #ifndef NDEBUG |
119 CheckStyleRanges(style_ranges_, text_.length()); | 119 CheckStyleRanges(style_ranges_, text_.length()); |
120 #endif | 120 #endif |
121 cached_bounds_and_offset_valid_ = false; | 121 cached_bounds_and_offset_valid_ = false; |
122 | 122 |
123 // Reset selection model. SetText should always followed by SetSelectionModel | 123 // Reset selection model. SetText should always followed by SetSelectionModel |
124 // or SetCursorPosition in upper layer. | 124 // or SetCursorPosition in upper layer. |
125 SetSelectionModel(SelectionModel(0, 0, SelectionModel::LEADING)); | 125 SetSelectionModel(SelectionModel(0, 0, SelectionModel::LEADING)); |
| 126 |
| 127 UpdateLayout(); |
126 } | 128 } |
127 | 129 |
128 void RenderText::ToggleInsertMode() { | 130 void RenderText::ToggleInsertMode() { |
129 insert_mode_ = !insert_mode_; | 131 insert_mode_ = !insert_mode_; |
130 cached_bounds_and_offset_valid_ = false; | 132 cached_bounds_and_offset_valid_ = false; |
131 } | 133 } |
132 | 134 |
133 void RenderText::SetDisplayRect(const Rect& r) { | 135 void RenderText::SetDisplayRect(const Rect& r) { |
134 display_rect_ = r; | 136 display_rect_ = r; |
135 cached_bounds_and_offset_valid_ = false; | 137 cached_bounds_and_offset_valid_ = false; |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 if (base::i18n::IsRTL()) | 339 if (base::i18n::IsRTL()) |
338 return base::i18n::RIGHT_TO_LEFT; | 340 return base::i18n::RIGHT_TO_LEFT; |
339 return base::i18n::LEFT_TO_RIGHT; | 341 return base::i18n::LEFT_TO_RIGHT; |
340 } | 342 } |
341 | 343 |
342 int RenderText::GetStringWidth() { | 344 int RenderText::GetStringWidth() { |
343 return default_style_.font.GetStringWidth(text()); | 345 return default_style_.font.GetStringWidth(text()); |
344 } | 346 } |
345 | 347 |
346 void RenderText::Draw(Canvas* canvas) { | 348 void RenderText::Draw(Canvas* canvas) { |
347 // Clip the canvas to the text display area. | 349 DrawSelection(canvas); |
348 canvas->ClipRect(display_rect_); | 350 DrawVisualText(canvas); |
349 | 351 DrawCursor(canvas); |
350 // Draw the selection. | |
351 std::vector<Rect> selection(GetSubstringBounds(GetSelectionStart(), | |
352 GetCursorPosition())); | |
353 SkColor selection_color = | |
354 focused() ? kFocusedSelectionColor : kUnfocusedSelectionColor; | |
355 for (std::vector<Rect>::const_iterator i = selection.begin(); | |
356 i < selection.end(); ++i) { | |
357 Rect r(*i); | |
358 canvas->FillRect(selection_color, r); | |
359 } | |
360 | |
361 // Create a temporary copy of the style ranges for composition and selection. | |
362 StyleRanges style_ranges(style_ranges_); | |
363 ApplyCompositionAndSelectionStyles(&style_ranges); | |
364 | |
365 // Draw the text. | |
366 Rect bounds(display_rect_); | |
367 bounds.Offset(GetUpdatedDisplayOffset()); | |
368 for (StyleRanges::const_iterator i = style_ranges.begin(); | |
369 i < style_ranges.end(); ++i) { | |
370 const Font& font = !i->underline ? i->font : | |
371 i->font.DeriveFont(0, i->font.GetStyle() | Font::UNDERLINED); | |
372 string16 text = text_.substr(i->range.start(), i->range.length()); | |
373 bounds.set_width(font.GetStringWidth(text)); | |
374 canvas->DrawStringInt(text, font, i->foreground, bounds); | |
375 | |
376 // Draw the strikethrough. | |
377 if (i->strike) { | |
378 SkPaint paint; | |
379 paint.setAntiAlias(true); | |
380 paint.setStyle(SkPaint::kFill_Style); | |
381 paint.setColor(i->foreground); | |
382 paint.setStrokeWidth(kStrikeWidth); | |
383 canvas->GetSkCanvas()->drawLine(SkIntToScalar(bounds.x()), | |
384 SkIntToScalar(bounds.bottom()), | |
385 SkIntToScalar(bounds.right()), | |
386 SkIntToScalar(bounds.y()), | |
387 paint); | |
388 } | |
389 | |
390 bounds.set_x(bounds.x() + bounds.width()); | |
391 } | |
392 | |
393 // Paint cursor. Replace cursor is drawn as rectangle for now. | |
394 Rect cursor(GetUpdatedCursorBounds()); | |
395 if (cursor_visible() && focused()) | |
396 canvas->DrawRectInt(kCursorColor, cursor.x(), cursor.y(), | |
397 cursor.width(), cursor.height()); | |
398 } | 352 } |
399 | 353 |
400 SelectionModel RenderText::FindCursorPosition(const Point& point) { | 354 SelectionModel RenderText::FindCursorPosition(const Point& point) { |
401 const Font& font = default_style_.font; | 355 const Font& font = default_style_.font; |
402 int left = 0; | 356 int left = 0; |
403 int left_pos = 0; | 357 int left_pos = 0; |
404 int right = font.GetStringWidth(text()); | 358 int right = font.GetStringWidth(text()); |
405 int right_pos = text().length(); | 359 int right_pos = text().length(); |
406 | 360 |
407 int x = point.x() - (display_rect_.x() + GetUpdatedDisplayOffset().x()); | 361 int x = point.x() - (display_rect_.x() + GetUpdatedDisplayOffset().x()); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 } | 496 } |
543 | 497 |
544 SelectionModel RenderText::RightEndSelectionModel() { | 498 SelectionModel RenderText::RightEndSelectionModel() { |
545 size_t cursor = text().length(); | 499 size_t cursor = text().length(); |
546 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); | 500 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); |
547 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? | 501 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? |
548 SelectionModel::LEADING : SelectionModel::TRAILING; | 502 SelectionModel::LEADING : SelectionModel::TRAILING; |
549 return SelectionModel(cursor, caret_pos, placement); | 503 return SelectionModel(cursor, caret_pos, placement); |
550 } | 504 } |
551 | 505 |
552 size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) { | |
553 return IndexOfAdjacentGrapheme(position, false); | |
554 } | |
555 | |
556 std::vector<Rect> RenderText::GetSubstringBounds(size_t from, size_t to) { | 506 std::vector<Rect> RenderText::GetSubstringBounds(size_t from, size_t to) { |
557 size_t start = std::min(from, to); | 507 size_t start = std::min(from, to); |
558 size_t end = std::max(from, to); | 508 size_t end = std::max(from, to); |
559 const Font& font = default_style_.font; | 509 const Font& font = default_style_.font; |
560 int start_x = font.GetStringWidth(text().substr(0, start)); | 510 int start_x = font.GetStringWidth(text().substr(0, start)); |
561 int end_x = font.GetStringWidth(text().substr(0, end)); | 511 int end_x = font.GetStringWidth(text().substr(0, end)); |
562 Rect rect(start_x, 0, end_x - start_x, font.GetHeight()); | 512 Rect rect(start_x, 0, end_x - start_x, font.GetHeight()); |
563 rect.Offset(display_rect_.origin()); | 513 rect.Offset(display_rect_.origin()); |
564 rect.Offset(GetUpdatedDisplayOffset()); | 514 rect.Offset(GetUpdatedDisplayOffset()); |
565 // Center the rect vertically in |display_rect_|. | 515 // Center the rect vertically in |display_rect_|. |
566 rect.Offset(Point(0, (display_rect_.height() - rect.height()) / 2)); | 516 rect.Offset(Point(0, (display_rect_.height() - rect.height()) / 2)); |
567 return std::vector<Rect>(1, rect); | 517 return std::vector<Rect>(1, rect); |
568 } | 518 } |
569 | 519 |
| 520 void RenderText::SetSelectionModel(const SelectionModel& selection_model) { |
| 521 DCHECK_LE(selection_model.selection_start(), text().length()); |
| 522 selection_model_.set_selection_start(selection_model.selection_start()); |
| 523 DCHECK_LE(selection_model.selection_end(), text().length()); |
| 524 selection_model_.set_selection_end(selection_model.selection_end()); |
| 525 DCHECK_LT(selection_model.caret_pos(), |
| 526 std::max(text().length(), static_cast<size_t>(1))); |
| 527 selection_model_.set_caret_pos(selection_model.caret_pos()); |
| 528 selection_model_.set_caret_placement(selection_model.caret_placement()); |
| 529 |
| 530 cached_bounds_and_offset_valid_ = false; |
| 531 } |
| 532 |
| 533 size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) { |
| 534 return IndexOfAdjacentGrapheme(position, false); |
| 535 } |
| 536 |
570 void RenderText::ApplyCompositionAndSelectionStyles( | 537 void RenderText::ApplyCompositionAndSelectionStyles( |
571 StyleRanges* style_ranges) const { | 538 StyleRanges* style_ranges) const { |
572 // TODO(msw): This pattern ought to be reconsidered; what about composition | 539 // TODO(msw): This pattern ought to be reconsidered; what about composition |
573 // and selection overlaps, retain existing local style features? | 540 // and selection overlaps, retain existing local style features? |
574 // Apply a composition style override to a copy of the style ranges. | 541 // Apply a composition style override to a copy of the style ranges. |
575 if (composition_range_.IsValid() && !composition_range_.is_empty()) { | 542 if (composition_range_.IsValid() && !composition_range_.is_empty()) { |
576 StyleRange composition_style(default_style_); | 543 StyleRange composition_style(default_style_); |
577 composition_style.underline = true; | 544 composition_style.underline = true; |
578 composition_style.range.set_start(composition_range_.start()); | 545 composition_style.range.set_start(composition_range_.start()); |
579 composition_style.range.set_end(composition_range_.end()); | 546 composition_style.range.set_end(composition_range_.end()); |
(...skipping 18 matching lines...) Expand all Loading... |
598 } | 565 } |
599 | 566 |
600 Point RenderText::ToViewPoint(const Point& point) { | 567 Point RenderText::ToViewPoint(const Point& point) { |
601 Point p(point.Add(display_rect().origin())); | 568 Point p(point.Add(display_rect().origin())); |
602 p = p.Add(GetUpdatedDisplayOffset()); | 569 p = p.Add(GetUpdatedDisplayOffset()); |
603 if (base::i18n::IsRTL()) | 570 if (base::i18n::IsRTL()) |
604 p.Offset(display_rect().width() - GetStringWidth() - 1, 0); | 571 p.Offset(display_rect().width() - GetStringWidth() - 1, 0); |
605 return p; | 572 return p; |
606 } | 573 } |
607 | 574 |
608 void RenderText::SetSelectionModel(const SelectionModel& selection_model) { | |
609 DCHECK_LE(selection_model.selection_start(), text().length()); | |
610 selection_model_.set_selection_start(selection_model.selection_start()); | |
611 DCHECK_LE(selection_model.selection_end(), text().length()); | |
612 selection_model_.set_selection_end(selection_model.selection_end()); | |
613 DCHECK_LT(selection_model.caret_pos(), | |
614 std::max(text().length(), static_cast<size_t>(1))); | |
615 selection_model_.set_caret_pos(selection_model.caret_pos()); | |
616 selection_model_.set_caret_placement(selection_model.caret_placement()); | |
617 | |
618 cached_bounds_and_offset_valid_ = false; | |
619 UpdateLayout(); | |
620 } | |
621 | |
622 void RenderText::MoveCursorTo(size_t position, bool select) { | 575 void RenderText::MoveCursorTo(size_t position, bool select) { |
623 size_t cursor = std::min(position, text().length()); | 576 size_t cursor = std::min(position, text().length()); |
624 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); | 577 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); |
625 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? | 578 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? |
626 SelectionModel::LEADING : SelectionModel::TRAILING; | 579 SelectionModel::LEADING : SelectionModel::TRAILING; |
627 size_t selection_start = select ? GetSelectionStart() : cursor; | 580 size_t selection_start = select ? GetSelectionStart() : cursor; |
628 if (IsCursorablePosition(cursor)) { | 581 if (IsCursorablePosition(cursor)) { |
629 SelectionModel sel(selection_start, cursor, caret_pos, placement); | 582 SelectionModel sel(selection_start, cursor, caret_pos, placement); |
630 SetSelectionModel(sel); | 583 SetSelectionModel(sel); |
631 } | 584 } |
(...skipping 25 matching lines...) Expand all Loading... |
657 // TODO(xji): have similar problem as above when overflow character is a | 610 // TODO(xji): have similar problem as above when overflow character is a |
658 // LTR character. | 611 // LTR character. |
659 // | 612 // |
660 // Pan to show the cursor when it overflows to the left. | 613 // Pan to show the cursor when it overflows to the left. |
661 delta_offset = display_rect_.x() - cursor_bounds_.x(); | 614 delta_offset = display_rect_.x() - cursor_bounds_.x(); |
662 } | 615 } |
663 display_offset_.Offset(delta_offset, 0); | 616 display_offset_.Offset(delta_offset, 0); |
664 cursor_bounds_.Offset(delta_offset, 0); | 617 cursor_bounds_.Offset(delta_offset, 0); |
665 } | 618 } |
666 | 619 |
| 620 void RenderText::DrawSelection(Canvas* canvas) { |
| 621 std::vector<Rect> sel( |
| 622 GetSubstringBounds(GetSelectionStart(), GetCursorPosition())); |
| 623 SkColor color = focused() ? kFocusedSelectionColor : kUnfocusedSelectionColor; |
| 624 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) |
| 625 canvas->FillRect(color, *i); |
| 626 } |
| 627 |
| 628 void RenderText::DrawCursor(Canvas* canvas) { |
| 629 // Paint cursor. Replace cursor is drawn as rectangle for now. |
| 630 // TODO(msw): Draw a better cursor with a better indication of association. |
| 631 if (cursor_visible() && focused()) { |
| 632 Rect r(GetUpdatedCursorBounds()); |
| 633 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); |
| 634 } |
| 635 } |
| 636 |
667 } // namespace gfx | 637 } // namespace gfx |
OLD | NEW |