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" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "third_party/skia/include/core/SkTypeface.h" |
12 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
13 #include "ui/gfx/canvas_skia.h" | 14 #include "ui/gfx/canvas_skia.h" |
14 #include "unicode/uchar.h" | 15 #include "unicode/uchar.h" |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 #ifndef NDEBUG | 19 #ifndef NDEBUG |
19 // Check StyleRanges invariant conditions: sorted and non-overlapping ranges. | 20 // Check StyleRanges invariant conditions: sorted and non-overlapping ranges. |
20 void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { | 21 void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { |
21 if (length == 0) { | 22 if (length == 0) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 73 } |
73 } | 74 } |
74 // Add the new range in its sorted location. | 75 // Add the new range in its sorted location. |
75 style_ranges->insert(i, style_range); | 76 style_ranges->insert(i, style_range); |
76 } | 77 } |
77 | 78 |
78 } // namespace | 79 } // namespace |
79 | 80 |
80 namespace gfx { | 81 namespace gfx { |
81 | 82 |
| 83 namespace internal { |
| 84 |
| 85 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) |
| 86 : canvas_skia_(canvas->GetSkCanvas()) { |
| 87 DCHECK(canvas_skia_); |
| 88 paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 89 paint_.setStyle(SkPaint::kFill_Style); |
| 90 paint_.setAntiAlias(true); |
| 91 paint_.setSubpixelText(true); |
| 92 paint_.setLCDRenderText(true); |
| 93 } |
| 94 |
| 95 SkiaTextRenderer::~SkiaTextRenderer() { |
| 96 } |
| 97 |
| 98 void SkiaTextRenderer::SetFont(const gfx::Font& font) { |
| 99 SkAutoTUnref<SkTypeface> typeface( |
| 100 SkTypeface::CreateFromName(font.GetFontName().c_str(), |
| 101 SkTypeface::kNormal)); |
| 102 if (typeface.get()) { |
| 103 // |paint_| adds its own ref. So don't |release()| it from the ref ptr here. |
| 104 paint_.setTypeface(typeface.get()); |
| 105 } |
| 106 paint_.setTextSize(font.GetFontSize()); |
| 107 } |
| 108 |
| 109 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
| 110 paint_.setColor(foreground); |
| 111 } |
| 112 |
| 113 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, |
| 114 const uint16* glyphs, |
| 115 size_t glyph_count) { |
| 116 size_t byte_length = glyph_count * sizeof(glyphs[0]); |
| 117 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], paint_); |
| 118 } |
| 119 |
| 120 // Draw underline and strike through text decorations. |
| 121 // Based on |SkCanvas::DrawTextDecorations()| and constants from: |
| 122 // third_party/skia/src/core/SkTextFormatParams.h |
| 123 void SkiaTextRenderer::DrawDecorations(int x, int y, int width, |
| 124 bool underline, bool strike) { |
| 125 // Fraction of the text size to lower a strike through below the baseline. |
| 126 const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21); |
| 127 // Fraction of the text size to lower an underline below the baseline. |
| 128 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9); |
| 129 // Fraction of the text size to use for a strike through or under-line. |
| 130 const SkScalar kLineThickness = (SK_Scalar1 / 18); |
| 131 |
| 132 SkScalar text_size = paint_.getTextSize(); |
| 133 SkScalar height = SkScalarMul(text_size, kLineThickness); |
| 134 SkRect r; |
| 135 |
| 136 r.fLeft = x; |
| 137 r.fRight = x + width; |
| 138 |
| 139 if (underline) { |
| 140 SkScalar offset = SkScalarMulAdd(text_size, kUnderlineOffset, y); |
| 141 r.fTop = offset; |
| 142 r.fBottom = offset + height; |
| 143 canvas_skia_->drawRect(r, paint_); |
| 144 } |
| 145 if (strike) { |
| 146 SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); |
| 147 r.fTop = offset; |
| 148 r.fBottom = offset + height; |
| 149 canvas_skia_->drawRect(r, paint_); |
| 150 } |
| 151 } |
| 152 |
| 153 } // namespace internal |
| 154 |
| 155 |
82 StyleRange::StyleRange() | 156 StyleRange::StyleRange() |
83 : font(), | 157 : font(), |
84 foreground(SK_ColorBLACK), | 158 foreground(SK_ColorBLACK), |
85 strike(false), | 159 strike(false), |
86 underline(false), | 160 underline(false), |
87 range() { | 161 range() { |
88 } | 162 } |
89 | 163 |
90 RenderText::~RenderText() { | 164 RenderText::~RenderText() { |
91 } | 165 } |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 } | 622 } |
549 | 623 |
550 Point RenderText::ToViewPoint(const Point& point) { | 624 Point RenderText::ToViewPoint(const Point& point) { |
551 Point p(point.Add(display_rect().origin())); | 625 Point p(point.Add(display_rect().origin())); |
552 p = p.Add(GetUpdatedDisplayOffset()); | 626 p = p.Add(GetUpdatedDisplayOffset()); |
553 if (base::i18n::IsRTL()) | 627 if (base::i18n::IsRTL()) |
554 p.Offset(display_rect().width() - GetStringWidth() - 1, 0); | 628 p.Offset(display_rect().width() - GetStringWidth() - 1, 0); |
555 return p; | 629 return p; |
556 } | 630 } |
557 | 631 |
| 632 Point RenderText::GetOriginForSkiaDrawing() { |
| 633 Point origin(ToViewPoint(Point())); |
| 634 // TODO(msw): Establish a vertical baseline for strings of mixed font heights. |
| 635 const Font& font = default_style().font; |
| 636 size_t height = font.GetHeight(); |
| 637 // Center the text vertically in the display area. |
| 638 origin.Offset(0, (display_rect().height() - height) / 2); |
| 639 // Offset by the font size to account for Skia expecting y to be the bottom. |
| 640 origin.Offset(0, font.GetFontSize()); |
| 641 return origin; |
| 642 } |
| 643 |
558 void RenderText::MoveCursorTo(size_t position, bool select) { | 644 void RenderText::MoveCursorTo(size_t position, bool select) { |
559 size_t cursor = std::min(position, text().length()); | 645 size_t cursor = std::min(position, text().length()); |
560 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); | 646 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); |
561 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? | 647 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? |
562 SelectionModel::LEADING : SelectionModel::TRAILING; | 648 SelectionModel::LEADING : SelectionModel::TRAILING; |
563 size_t selection_start = select ? GetSelectionStart() : cursor; | 649 size_t selection_start = select ? GetSelectionStart() : cursor; |
564 if (IsCursorablePosition(cursor)) { | 650 if (IsCursorablePosition(cursor)) { |
565 SelectionModel sel(selection_start, cursor, caret_pos, placement); | 651 SelectionModel sel(selection_start, cursor, caret_pos, placement); |
566 SetSelectionModel(sel); | 652 SetSelectionModel(sel); |
567 } | 653 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 void RenderText::DrawCursor(Canvas* canvas) { | 697 void RenderText::DrawCursor(Canvas* canvas) { |
612 // Paint cursor. Replace cursor is drawn as rectangle for now. | 698 // Paint cursor. Replace cursor is drawn as rectangle for now. |
613 // TODO(msw): Draw a better cursor with a better indication of association. | 699 // TODO(msw): Draw a better cursor with a better indication of association. |
614 if (cursor_visible() && focused()) { | 700 if (cursor_visible() && focused()) { |
615 Rect r(GetUpdatedCursorBounds()); | 701 Rect r(GetUpdatedCursorBounds()); |
616 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); | 702 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); |
617 } | 703 } |
618 } | 704 } |
619 | 705 |
620 } // namespace gfx | 706 } // namespace gfx |
OLD | NEW |