Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: ui/gfx/render_text.cc

Issue 8725002: Draw text via Skia in RenderTextLinux. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 }
89
90 SkiaTextRenderer::~SkiaTextRenderer() {
91 }
92
93 void SkiaTextRenderer::Init() {
94 paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
msw 2011/12/06 18:40:56 Why not roll this into the ctor?
Alexei Svitkine (slow) 2011/12/06 19:10:12 I guess I followed our style that we shouldn't do
95 paint_.setStyle(SkPaint::kFill_Style);
96 paint_.setAntiAlias(true);
97 paint_.setSubpixelText(true);
98 paint_.setLCDRenderText(true);
99 }
100
101 void SkiaTextRenderer::SetFont(const gfx::Font& font) {
102 SkTypeface* typeface =
103 SkTypeface::CreateFromName(font.GetFontName().c_str(),
104 SkTypeface::kNormal);
105 if (typeface) {
106 paint_.setTypeface(typeface);
107 // |paint_| adds its own ref. Release the ref from |CreateFromName()|.
msw 2011/12/06 18:40:56 Then shouldn't |typeface| be a scoped refptr or so
Alexei Svitkine (slow) 2011/12/06 19:10:12 The Chromium scoped_refptr won't work, since its a
msw 2011/12/07 01:08:45 Thanks for enlightening me!
108 typeface->unref();
109 }
110 paint_.setTextSize(font.GetFontSize());
111 }
112
113 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) {
114 paint_.setColor(foreground);
115 }
116
117 void SkiaTextRenderer::DrawPosText(const SkPoint* pos,
118 const uint16* glyphs,
msw 2011/12/06 18:40:56 Indent these args.
Alexei Svitkine (slow) 2011/12/06 19:10:12 Done.
119 size_t glyph_count) {
120 size_t byte_length = glyph_count * sizeof(glyphs[0]);
xji 2011/12/05 23:47:09 Will uint16 cause problem? PangoGlyph is a guint32
Alexei Svitkine (slow) 2011/12/06 19:10:12 It's a good question. |drawPosText()| seems to onl
121 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], paint_);
122 }
123
124 // Draw underline and strike through text decorations.
125 // Based on |SkCanvas::DrawTextDecorations()| and constants from:
126 // third_party/skia/src/core/SkTextFormatParams.h
127 void SkiaTextRenderer::DrawDecorations(int x, int y, int width,
128 bool underline, bool strike) {
129 // Fraction of the text size to lower a strike through below the baseline.
130 const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21);
131 // Fraction of the text size to lower an underline below the baseline.
132 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9);
133 // Fraction of the text size to use for a strike through or under-line.
134 const SkScalar kLineThickness = (SK_Scalar1 / 18);
135
136 SkScalar text_size = paint_.getTextSize();
137 SkScalar height = SkScalarMul(text_size, kLineThickness);
138 SkRect r;
139
140 r.fLeft = x;
141 r.fRight = x + width;
142
143 if (underline) {
144 SkScalar offset = SkScalarMulAdd(text_size, kUnderlineOffset, y);
145 r.fTop = offset;
146 r.fBottom = offset + height;
147 canvas_skia_->drawRect(r, paint_);
148 }
149 if (strike) {
150 SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y);
151 r.fTop = offset;
152 r.fBottom = offset + height;
153 canvas_skia_->drawRect(r, paint_);
154 }
155 }
156
157 } // namespace internal
158
159
82 StyleRange::StyleRange() 160 StyleRange::StyleRange()
83 : font(), 161 : font(),
84 foreground(SK_ColorBLACK), 162 foreground(SK_ColorBLACK),
85 strike(false), 163 strike(false),
86 underline(false), 164 underline(false),
87 range() { 165 range() {
88 } 166 }
89 167
90 RenderText::~RenderText() { 168 RenderText::~RenderText() {
91 } 169 }
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698