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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 NOTREACHED(); | 72 NOTREACHED(); |
72 } | 73 } |
73 // Add the new range in its sorted location. | 74 // Add the new range in its sorted location. |
74 style_ranges->insert(i, style_range); | 75 style_ranges->insert(i, style_range); |
75 } | 76 } |
76 | 77 |
77 } // namespace | 78 } // namespace |
78 | 79 |
79 namespace gfx { | 80 namespace gfx { |
80 | 81 |
| 82 namespace internal { |
| 83 |
| 84 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) |
| 85 : canvas_skia_(canvas->GetSkCanvas()) { |
| 86 DCHECK(canvas_skia_); |
| 87 } |
| 88 |
| 89 SkiaTextRenderer::~SkiaTextRenderer() { |
| 90 } |
| 91 |
| 92 void SkiaTextRenderer::Init() { |
| 93 paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 94 paint_.setStyle(SkPaint::kFill_Style); |
| 95 paint_.setAntiAlias(true); |
| 96 paint_.setSubpixelText(true); |
| 97 paint_.setLCDRenderText(true); |
| 98 } |
| 99 |
| 100 void SkiaTextRenderer::SetFont(const gfx::Font& font) { |
| 101 SkTypeface* typeface = |
| 102 SkTypeface::CreateFromName(font.GetFontName().c_str(), |
| 103 SkTypeface::kNormal); |
| 104 if (typeface) { |
| 105 paint_.setTypeface(typeface); |
| 106 // |paint_| adds its own ref. Release the ref from |CreateFromName()|. |
| 107 typeface->unref(); |
| 108 } |
| 109 paint_.setTextSize(font.GetFontSize()); |
| 110 } |
| 111 |
| 112 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
| 113 paint_.setColor(foreground); |
| 114 } |
| 115 |
| 116 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, |
| 117 const uint16* glyphs, |
| 118 size_t glyph_count) { |
| 119 size_t byte_length = glyph_count * sizeof(glyphs[0]); |
| 120 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], paint_); |
| 121 } |
| 122 |
| 123 // Draw underline and strike through text decorations. |
| 124 // Based on |SkCanvas::DrawTextDecorations()| and constants from: |
| 125 // third_party/skia/src/core/SkTextFormatParams.h |
| 126 void SkiaTextRenderer::DrawDecorations(int x, int y, int width, |
| 127 bool underline, bool strike) { |
| 128 // Fraction of the text size to lower a strike through below the baseline. |
| 129 const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21); |
| 130 // Fraction of the text size to lower an underline below the baseline. |
| 131 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9); |
| 132 // Fraction of the text size to use for a strike through or under-line. |
| 133 const SkScalar kLineThickness = (SK_Scalar1 / 18); |
| 134 |
| 135 SkScalar text_size = paint_.getTextSize(); |
| 136 SkScalar height = SkScalarMul(text_size, kLineThickness); |
| 137 SkRect r; |
| 138 |
| 139 r.fLeft = x; |
| 140 r.fRight = x + width; |
| 141 |
| 142 if (underline) { |
| 143 SkScalar offset = SkScalarMulAdd(text_size, kUnderlineOffset, y); |
| 144 r.fTop = offset; |
| 145 r.fBottom = offset + height; |
| 146 canvas_skia_->drawRect(r, paint_); |
| 147 } |
| 148 if (strike) { |
| 149 SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); |
| 150 r.fTop = offset; |
| 151 r.fBottom = offset + height; |
| 152 canvas_skia_->drawRect(r, paint_); |
| 153 } |
| 154 } |
| 155 |
| 156 } // namespace internal |
| 157 |
| 158 |
81 StyleRange::StyleRange() | 159 StyleRange::StyleRange() |
82 : font(), | 160 : font(), |
83 foreground(SK_ColorBLACK), | 161 foreground(SK_ColorBLACK), |
84 strike(false), | 162 strike(false), |
85 underline(false), | 163 underline(false), |
86 range() { | 164 range() { |
87 } | 165 } |
88 | 166 |
89 RenderText::~RenderText() { | 167 RenderText::~RenderText() { |
90 } | 168 } |
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 void RenderText::DrawCursor(Canvas* canvas) { | 689 void RenderText::DrawCursor(Canvas* canvas) { |
612 // Paint cursor. Replace cursor is drawn as rectangle for now. | 690 // Paint cursor. Replace cursor is drawn as rectangle for now. |
613 // TODO(msw): Draw a better cursor with a better indication of association. | 691 // TODO(msw): Draw a better cursor with a better indication of association. |
614 if (cursor_visible() && focused()) { | 692 if (cursor_visible() && focused()) { |
615 Rect r(GetUpdatedCursorBounds()); | 693 Rect r(GetUpdatedCursorBounds()); |
616 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); | 694 canvas->DrawRectInt(kCursorColor, r.x(), r.y(), r.width(), r.height()); |
617 } | 695 } |
618 } | 696 } |
619 | 697 |
620 } // namespace gfx | 698 } // namespace gfx |
OLD | NEW |