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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
336 base::i18n::TextDirection RenderText::GetTextDirection() { | 338 base::i18n::TextDirection RenderText::GetTextDirection() { |
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) { |
Alexei Svitkine (slow)
2011/11/29 15:41:37
Can you make this return early if text().empty()?
xji
2011/11/29 23:07:15
bypass DrawSelection and DrarVisualText when text(
| |
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 11 matching lines...) Expand all Loading... | |
419 } else if (pivot == x) { | 373 } else if (pivot == x) { |
420 return SelectionModel(pivot_pos); | 374 return SelectionModel(pivot_pos); |
421 } else { | 375 } else { |
422 right = pivot; | 376 right = pivot; |
423 right_pos = pivot_pos; | 377 right_pos = pivot_pos; |
424 } | 378 } |
425 } | 379 } |
426 return SelectionModel(left_pos); | 380 return SelectionModel(left_pos); |
427 } | 381 } |
428 | 382 |
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() { | 383 const Rect& RenderText::GetUpdatedCursorBounds() { |
437 UpdateCachedBoundsAndOffset(); | 384 UpdateCachedBoundsAndOffset(); |
438 return cursor_bounds_; | 385 return cursor_bounds_; |
439 } | 386 } |
440 | 387 |
441 size_t RenderText::GetIndexOfNextGrapheme(size_t position) { | 388 size_t RenderText::GetIndexOfNextGrapheme(size_t position) { |
442 return IndexOfAdjacentGrapheme(position, true); | 389 return IndexOfAdjacentGrapheme(position, true); |
443 } | 390 } |
444 | 391 |
445 SelectionModel RenderText::GetSelectionModelForSelectionStart() { | 392 SelectionModel RenderText::GetSelectionModelForSelectionStart() { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
542 } | 489 } |
543 | 490 |
544 SelectionModel RenderText::RightEndSelectionModel() { | 491 SelectionModel RenderText::RightEndSelectionModel() { |
545 size_t cursor = text().length(); | 492 size_t cursor = text().length(); |
546 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); | 493 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); |
547 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? | 494 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? |
548 SelectionModel::LEADING : SelectionModel::TRAILING; | 495 SelectionModel::LEADING : SelectionModel::TRAILING; |
549 return SelectionModel(cursor, caret_pos, placement); | 496 return SelectionModel(cursor, caret_pos, placement); |
550 } | 497 } |
551 | 498 |
499 void RenderText::SetSelectionModel(const SelectionModel& selection_model) { | |
500 DCHECK_LE(selection_model.selection_start(), text().length()); | |
501 selection_model_.set_selection_start(selection_model.selection_start()); | |
502 DCHECK_LE(selection_model.selection_end(), text().length()); | |
503 selection_model_.set_selection_end(selection_model.selection_end()); | |
504 DCHECK_LT(selection_model.caret_pos(), | |
505 std::max(text().length(), static_cast<size_t>(1))); | |
506 selection_model_.set_caret_pos(selection_model.caret_pos()); | |
507 selection_model_.set_caret_placement(selection_model.caret_placement()); | |
508 | |
509 cached_bounds_and_offset_valid_ = false; | |
510 } | |
511 | |
552 size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) { | 512 size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) { |
553 return IndexOfAdjacentGrapheme(position, false); | 513 return IndexOfAdjacentGrapheme(position, false); |
554 } | 514 } |
555 | 515 |
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( | 516 void RenderText::ApplyCompositionAndSelectionStyles( |
571 StyleRanges* style_ranges) const { | 517 StyleRanges* style_ranges) const { |
572 // TODO(msw): This pattern ought to be reconsidered; what about composition | 518 // TODO(msw): This pattern ought to be reconsidered; what about composition |
573 // and selection overlaps, retain existing local style features? | 519 // and selection overlaps, retain existing local style features? |
574 // Apply a composition style override to a copy of the style ranges. | 520 // Apply a composition style override to a copy of the style ranges. |
575 if (composition_range_.IsValid() && !composition_range_.is_empty()) { | 521 if (composition_range_.IsValid() && !composition_range_.is_empty()) { |
576 StyleRange composition_style(default_style_); | 522 StyleRange composition_style(default_style_); |
577 composition_style.underline = true; | 523 composition_style.underline = true; |
578 composition_style.range.set_start(composition_range_.start()); | 524 composition_style.range.set_start(composition_range_.start()); |
579 composition_style.range.set_end(composition_range_.end()); | 525 composition_style.range.set_end(composition_range_.end()); |
(...skipping 18 matching lines...) Expand all Loading... | |
598 } | 544 } |
599 | 545 |
600 Point RenderText::ToViewPoint(const Point& point) { | 546 Point RenderText::ToViewPoint(const Point& point) { |
601 Point p(point.Add(display_rect().origin())); | 547 Point p(point.Add(display_rect().origin())); |
602 p = p.Add(GetUpdatedDisplayOffset()); | 548 p = p.Add(GetUpdatedDisplayOffset()); |
603 if (base::i18n::IsRTL()) | 549 if (base::i18n::IsRTL()) |
604 p.Offset(display_rect().width() - GetStringWidth() - 1, 0); | 550 p.Offset(display_rect().width() - GetStringWidth() - 1, 0); |
605 return p; | 551 return p; |
606 } | 552 } |
607 | 553 |
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) { | 554 void RenderText::MoveCursorTo(size_t position, bool select) { |
623 size_t cursor = std::min(position, text().length()); | 555 size_t cursor = std::min(position, text().length()); |
624 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); | 556 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); |
625 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? | 557 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? |
626 SelectionModel::LEADING : SelectionModel::TRAILING; | 558 SelectionModel::LEADING : SelectionModel::TRAILING; |
627 size_t selection_start = select ? GetSelectionStart() : cursor; | 559 size_t selection_start = select ? GetSelectionStart() : cursor; |
628 if (IsCursorablePosition(cursor)) { | 560 if (IsCursorablePosition(cursor)) { |
629 SelectionModel sel(selection_start, cursor, caret_pos, placement); | 561 SelectionModel sel(selection_start, cursor, caret_pos, placement); |
630 SetSelectionModel(sel); | 562 SetSelectionModel(sel); |
631 } | 563 } |
(...skipping 25 matching lines...) Expand all Loading... | |
657 // TODO(xji): have similar problem as above when overflow character is a | 589 // TODO(xji): have similar problem as above when overflow character is a |
658 // LTR character. | 590 // LTR character. |
659 // | 591 // |
660 // Pan to show the cursor when it overflows to the left. | 592 // Pan to show the cursor when it overflows to the left. |
661 delta_offset = display_rect_.x() - cursor_bounds_.x(); | 593 delta_offset = display_rect_.x() - cursor_bounds_.x(); |
662 } | 594 } |
663 display_offset_.Offset(delta_offset, 0); | 595 display_offset_.Offset(delta_offset, 0); |
664 cursor_bounds_.Offset(delta_offset, 0); | 596 cursor_bounds_.Offset(delta_offset, 0); |
665 } | 597 } |
666 | 598 |
599 void RenderText::DrawSelection(Canvas* canvas) { | |
600 std::vector<Rect> sel; | |
601 GetSubstringBounds(GetSelectionStart(), GetCursorPosition(), &sel); | |
602 SkColor color = focused() ? kFocusedSelectionColor : kUnfocusedSelectionColor; | |
603 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | |
604 canvas->FillRect(color, *i); | |
605 } | |
606 | |
607 void RenderText::DrawCursor(Canvas* canvas) { | |
608 // Paint cursor. Replace cursor is drawn as rectangle for now. | |
609 // TODO(msw): Draw a better cursor with a better indication of association. | |
610 if (cursor_visible() && focused()) { | |
611 Rect r(GetUpdatedCursorBounds()); | |
612 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); | |
613 } | |
614 } | |
615 | |
667 } // namespace gfx | 616 } // namespace gfx |
OLD | NEW |