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 EnsureLayout(); |
348 canvas->ClipRect(display_rect_); | |
349 | 350 |
350 // Draw the selection. | 351 if (!text().empty()) { |
351 std::vector<Rect> selection(GetSubstringBounds(GetSelectionStart(), | 352 DrawSelection(canvas); |
352 GetCursorPosition())); | 353 DrawVisualText(canvas); |
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 } | 354 } |
360 | 355 DrawCursor(canvas); |
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 } | 356 } |
399 | 357 |
400 SelectionModel RenderText::FindCursorPosition(const Point& point) { | 358 SelectionModel RenderText::FindCursorPosition(const Point& point) { |
401 const Font& font = default_style_.font; | 359 const Font& font = default_style_.font; |
402 int left = 0; | 360 int left = 0; |
403 int left_pos = 0; | 361 int left_pos = 0; |
404 int right = font.GetStringWidth(text()); | 362 int right = font.GetStringWidth(text()); |
405 int right_pos = text().length(); | 363 int right_pos = text().length(); |
406 | 364 |
407 int x = point.x() - (display_rect_.x() + GetUpdatedDisplayOffset().x()); | 365 int x = point.x() - (display_rect_.x() + GetUpdatedDisplayOffset().x()); |
(...skipping 11 matching lines...) Expand all Loading... |
419 } else if (pivot == x) { | 377 } else if (pivot == x) { |
420 return SelectionModel(pivot_pos); | 378 return SelectionModel(pivot_pos); |
421 } else { | 379 } else { |
422 right = pivot; | 380 right = pivot; |
423 right_pos = pivot_pos; | 381 right_pos = pivot_pos; |
424 } | 382 } |
425 } | 383 } |
426 return SelectionModel(left_pos); | 384 return SelectionModel(left_pos); |
427 } | 385 } |
428 | 386 |
429 Rect RenderText::GetCursorBounds(const SelectionModel& selection, | |
430 bool insert_mode) { | |
431 size_t from = selection.selection_end(); | |
432 size_t to = insert_mode ? from : std::min(text_.length(), from + 1); | |
433 return GetSubstringBounds(from, to)[0]; | |
434 } | |
435 | |
436 const Rect& RenderText::GetUpdatedCursorBounds() { | 387 const Rect& RenderText::GetUpdatedCursorBounds() { |
437 UpdateCachedBoundsAndOffset(); | 388 UpdateCachedBoundsAndOffset(); |
438 return cursor_bounds_; | 389 return cursor_bounds_; |
439 } | 390 } |
440 | 391 |
441 size_t RenderText::GetIndexOfNextGrapheme(size_t position) { | 392 size_t RenderText::GetIndexOfNextGrapheme(size_t position) { |
442 return IndexOfAdjacentGrapheme(position, true); | 393 return IndexOfAdjacentGrapheme(position, true); |
443 } | 394 } |
444 | 395 |
445 SelectionModel RenderText::GetSelectionModelForSelectionStart() { | 396 SelectionModel RenderText::GetSelectionModelForSelectionStart() { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 } | 493 } |
543 | 494 |
544 SelectionModel RenderText::RightEndSelectionModel() { | 495 SelectionModel RenderText::RightEndSelectionModel() { |
545 size_t cursor = text().length(); | 496 size_t cursor = text().length(); |
546 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); | 497 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); |
547 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? | 498 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? |
548 SelectionModel::LEADING : SelectionModel::TRAILING; | 499 SelectionModel::LEADING : SelectionModel::TRAILING; |
549 return SelectionModel(cursor, caret_pos, placement); | 500 return SelectionModel(cursor, caret_pos, placement); |
550 } | 501 } |
551 | 502 |
| 503 void RenderText::SetSelectionModel(const SelectionModel& model) { |
| 504 DCHECK_LE(model.selection_start(), text().length()); |
| 505 selection_model_.set_selection_start(model.selection_start()); |
| 506 DCHECK_LE(model.selection_end(), text().length()); |
| 507 selection_model_.set_selection_end(model.selection_end()); |
| 508 DCHECK_LT(model.caret_pos(), |
| 509 std::max(text().length(), static_cast<size_t>(1))); |
| 510 selection_model_.set_caret_pos(model.caret_pos()); |
| 511 selection_model_.set_caret_placement(model.caret_placement()); |
| 512 |
| 513 cached_bounds_and_offset_valid_ = false; |
| 514 } |
| 515 |
552 size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) { | 516 size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) { |
553 return IndexOfAdjacentGrapheme(position, false); | 517 return IndexOfAdjacentGrapheme(position, false); |
554 } | 518 } |
555 | 519 |
556 std::vector<Rect> RenderText::GetSubstringBounds(size_t from, size_t to) { | |
557 size_t start = std::min(from, to); | |
558 size_t end = std::max(from, to); | |
559 const Font& font = default_style_.font; | |
560 int start_x = font.GetStringWidth(text().substr(0, start)); | |
561 int end_x = font.GetStringWidth(text().substr(0, end)); | |
562 Rect rect(start_x, 0, end_x - start_x, font.GetHeight()); | |
563 rect.Offset(display_rect_.origin()); | |
564 rect.Offset(GetUpdatedDisplayOffset()); | |
565 // Center the rect vertically in |display_rect_|. | |
566 rect.Offset(Point(0, (display_rect_.height() - rect.height()) / 2)); | |
567 return std::vector<Rect>(1, rect); | |
568 } | |
569 | |
570 void RenderText::ApplyCompositionAndSelectionStyles( | 520 void RenderText::ApplyCompositionAndSelectionStyles( |
571 StyleRanges* style_ranges) const { | 521 StyleRanges* style_ranges) const { |
572 // TODO(msw): This pattern ought to be reconsidered; what about composition | 522 // TODO(msw): This pattern ought to be reconsidered; what about composition |
573 // and selection overlaps, retain existing local style features? | 523 // and selection overlaps, retain existing local style features? |
574 // Apply a composition style override to a copy of the style ranges. | 524 // Apply a composition style override to a copy of the style ranges. |
575 if (composition_range_.IsValid() && !composition_range_.is_empty()) { | 525 if (composition_range_.IsValid() && !composition_range_.is_empty()) { |
576 StyleRange composition_style(default_style_); | 526 StyleRange composition_style(default_style_); |
577 composition_style.underline = true; | 527 composition_style.underline = true; |
578 composition_style.range.set_start(composition_range_.start()); | 528 composition_style.range.set_start(composition_range_.start()); |
579 composition_style.range.set_end(composition_range_.end()); | 529 composition_style.range.set_end(composition_range_.end()); |
(...skipping 18 matching lines...) Expand all Loading... |
598 } | 548 } |
599 | 549 |
600 Point RenderText::ToViewPoint(const Point& point) { | 550 Point RenderText::ToViewPoint(const Point& point) { |
601 Point p(point.Add(display_rect().origin())); | 551 Point p(point.Add(display_rect().origin())); |
602 p = p.Add(GetUpdatedDisplayOffset()); | 552 p = p.Add(GetUpdatedDisplayOffset()); |
603 if (base::i18n::IsRTL()) | 553 if (base::i18n::IsRTL()) |
604 p.Offset(display_rect().width() - GetStringWidth() - 1, 0); | 554 p.Offset(display_rect().width() - GetStringWidth() - 1, 0); |
605 return p; | 555 return p; |
606 } | 556 } |
607 | 557 |
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) { | 558 void RenderText::MoveCursorTo(size_t position, bool select) { |
623 size_t cursor = std::min(position, text().length()); | 559 size_t cursor = std::min(position, text().length()); |
624 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); | 560 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); |
625 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? | 561 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? |
626 SelectionModel::LEADING : SelectionModel::TRAILING; | 562 SelectionModel::LEADING : SelectionModel::TRAILING; |
627 size_t selection_start = select ? GetSelectionStart() : cursor; | 563 size_t selection_start = select ? GetSelectionStart() : cursor; |
628 if (IsCursorablePosition(cursor)) { | 564 if (IsCursorablePosition(cursor)) { |
629 SelectionModel sel(selection_start, cursor, caret_pos, placement); | 565 SelectionModel sel(selection_start, cursor, caret_pos, placement); |
630 SetSelectionModel(sel); | 566 SetSelectionModel(sel); |
631 } | 567 } |
(...skipping 25 matching lines...) Expand all Loading... |
657 // TODO(xji): have similar problem as above when overflow character is a | 593 // TODO(xji): have similar problem as above when overflow character is a |
658 // LTR character. | 594 // LTR character. |
659 // | 595 // |
660 // Pan to show the cursor when it overflows to the left. | 596 // Pan to show the cursor when it overflows to the left. |
661 delta_offset = display_rect_.x() - cursor_bounds_.x(); | 597 delta_offset = display_rect_.x() - cursor_bounds_.x(); |
662 } | 598 } |
663 display_offset_.Offset(delta_offset, 0); | 599 display_offset_.Offset(delta_offset, 0); |
664 cursor_bounds_.Offset(delta_offset, 0); | 600 cursor_bounds_.Offset(delta_offset, 0); |
665 } | 601 } |
666 | 602 |
| 603 void RenderText::DrawSelection(Canvas* canvas) { |
| 604 std::vector<Rect> sel; |
| 605 GetSubstringBounds(GetSelectionStart(), GetCursorPosition(), &sel); |
| 606 SkColor color = focused() ? kFocusedSelectionColor : kUnfocusedSelectionColor; |
| 607 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) |
| 608 canvas->FillRect(color, *i); |
| 609 } |
| 610 |
| 611 void RenderText::DrawCursor(Canvas* canvas) { |
| 612 // Paint cursor. Replace cursor is drawn as rectangle for now. |
| 613 // TODO(msw): Draw a better cursor with a better indication of association. |
| 614 if (cursor_visible() && focused()) { |
| 615 Rect r(GetUpdatedCursorBounds()); |
| 616 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); |
| 617 } |
| 618 } |
| 619 |
667 } // namespace gfx | 620 } // namespace gfx |
OLD | NEW |