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

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

Issue 22835002: Supports gfx::FontList in gfx::Canvas and ui::ElideText family. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: layout_text.h => text_utils.h Created 7 years, 4 months 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
« no previous file with comments | « ui/gfx/canvas.h ('k') | ui/gfx/canvas_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/canvas.h" 5 #include "ui/gfx/canvas.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/i18n/rtl.h" 9 #include "base/i18n/rtl.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "third_party/skia/include/core/SkBitmap.h" 11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/effects/SkGradientShader.h" 12 #include "third_party/skia/include/effects/SkGradientShader.h"
13 #include "ui/gfx/canvas.h" 13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/font.h" 14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/rect.h" 15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/size_conversions.h" 16 #include "ui/gfx/size_conversions.h"
17 #include "ui/gfx/skia_util.h" 17 #include "ui/gfx/skia_util.h"
18 #include "ui/gfx/transform.h" 18 #include "ui/gfx/transform.h"
19 19
20 #if defined(OS_WIN) 20 #if defined(OS_WIN)
21 #include "ui/gfx/canvas_skia_paint.h" 21 #include "ui/gfx/canvas_skia_paint.h"
22 #endif 22 #endif
23 23
24 namespace gfx { 24 namespace gfx {
25 25
26 Canvas::Canvas(const gfx::Size& size, 26 Canvas::Canvas(const Size& size, ui::ScaleFactor scale_factor, bool is_opaque)
27 ui::ScaleFactor scale_factor, 27 : scale_factor_(scale_factor),
28 bool is_opaque) 28 canvas_(NULL) {
29 : scale_factor_(scale_factor), 29 Size pixel_size = ToCeiledSize(
30 canvas_(NULL) { 30 ScaleSize(size, ui::GetScaleFactorScale(scale_factor)));
31 gfx::Size pixel_size = gfx::ToCeiledSize(
32 gfx::ScaleSize(size, ui::GetScaleFactorScale(scale_factor)));
33 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(), 31 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(),
34 pixel_size.height(), 32 pixel_size.height(),
35 is_opaque)); 33 is_opaque));
36 canvas_ = owned_canvas_.get(); 34 canvas_ = owned_canvas_.get();
37 #if defined(OS_WIN) || defined(OS_MACOSX) 35 #if defined(OS_WIN) || defined(OS_MACOSX)
38 // skia::PlatformCanvas instances are initialized to 0 by Cairo on Linux, but 36 // skia::PlatformCanvas instances are initialized to 0 by Cairo on Linux, but
39 // uninitialized on Win and Mac. 37 // uninitialized on Win and Mac.
40 if (!is_opaque) 38 if (!is_opaque)
41 owned_canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); 39 owned_canvas_->clear(SkColorSetARGB(0, 0, 0, 0));
42 #endif 40 #endif
43 41
44 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor)); 42 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor));
45 canvas_->scale(scale, scale); 43 canvas_->scale(scale, scale);
46 } 44 }
47 45
48 Canvas::Canvas(const gfx::ImageSkiaRep& image_rep, bool is_opaque) 46 Canvas::Canvas(const ImageSkiaRep& image_rep, bool is_opaque)
49 : scale_factor_(image_rep.scale_factor()), 47 : scale_factor_(image_rep.scale_factor()),
50 owned_canvas_(skia::AdoptRef( 48 owned_canvas_(skia::AdoptRef(
51 skia::CreatePlatformCanvas(image_rep.pixel_width(), 49 skia::CreatePlatformCanvas(image_rep.pixel_width(),
52 image_rep.pixel_height(), 50 image_rep.pixel_height(),
53 is_opaque))), 51 is_opaque))),
54 canvas_(owned_canvas_.get()) { 52 canvas_(owned_canvas_.get()) {
55 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_)); 53 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_));
56 canvas_->scale(scale, scale); 54 canvas_->scale(scale, scale);
57 DrawImageInt(gfx::ImageSkia(image_rep), 0, 0); 55 DrawImageInt(ImageSkia(image_rep), 0, 0);
58 } 56 }
59 57
60 Canvas::Canvas() 58 Canvas::Canvas()
61 : scale_factor_(ui::SCALE_FACTOR_100P), 59 : scale_factor_(ui::SCALE_FACTOR_100P),
62 owned_canvas_(skia::AdoptRef(skia::CreatePlatformCanvas(0, 0, false))), 60 owned_canvas_(skia::AdoptRef(skia::CreatePlatformCanvas(0, 0, false))),
63 canvas_(owned_canvas_.get()) { 61 canvas_(owned_canvas_.get()) {
64 } 62 }
65 63
66 Canvas::~Canvas() { 64 Canvas::~Canvas() {
67 } 65 }
68 66
69 // static 67 // static
70 Canvas* Canvas::CreateCanvasWithoutScaling(SkCanvas* canvas, 68 Canvas* Canvas::CreateCanvasWithoutScaling(SkCanvas* canvas,
71 ui::ScaleFactor scale_factor) { 69 ui::ScaleFactor scale_factor) {
72 return new Canvas(canvas, scale_factor); 70 return new Canvas(canvas, scale_factor);
73 } 71 }
74 72
75 void Canvas::RecreateBackingCanvas(const gfx::Size& size, 73 void Canvas::RecreateBackingCanvas(const Size& size,
76 ui::ScaleFactor scale_factor, 74 ui::ScaleFactor scale_factor,
77 bool is_opaque) { 75 bool is_opaque) {
78 scale_factor_ = scale_factor; 76 scale_factor_ = scale_factor;
79 gfx::Size pixel_size = gfx::ToFlooredSize( 77 Size pixel_size = ToFlooredSize(
80 gfx::ScaleSize(size, ui::GetScaleFactorScale(scale_factor))); 78 ScaleSize(size, ui::GetScaleFactorScale(scale_factor)));
81 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(), 79 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(),
82 pixel_size.height(), 80 pixel_size.height(),
83 is_opaque)); 81 is_opaque));
84 canvas_ = owned_canvas_.get(); 82 canvas_ = owned_canvas_.get();
85 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_)); 83 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_));
86 canvas_->scale(scale, scale); 84 canvas_->scale(scale, scale);
87 } 85 }
88 86
89 // static 87 // static
90 int Canvas::GetStringWidth(const base::string16& text, const gfx::Font& font) { 88 void Canvas::SizeStringInt(const base::string16& text,
89 const Font& font,
90 int* width,
91 int* height,
92 int line_height,
93 int flags) {
94 SizeStringInt(text, FontList(font), width, height, line_height, flags);
95 }
96
97 // static
98 int Canvas::GetStringWidth(const base::string16& text,
99 const FontList& font_list) {
91 int width = 0, height = 0; 100 int width = 0, height = 0;
92 Canvas::SizeStringInt(text, font, &width, &height, 0, NO_ELLIPSIS); 101 SizeStringInt(text, font_list, &width, &height, 0, NO_ELLIPSIS);
93 return width; 102 return width;
94 } 103 }
95 104
105 // static
106 int Canvas::GetStringWidth(const base::string16& text, const Font& font) {
107 int width = 0, height = 0;
108 SizeStringInt(text, FontList(font), &width, &height, 0, NO_ELLIPSIS);
109 return width;
110 }
111
96 // static 112 // static
97 int Canvas::DefaultCanvasTextAlignment() { 113 int Canvas::DefaultCanvasTextAlignment() {
98 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; 114 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT;
99 } 115 }
100 116
101 gfx::ImageSkiaRep Canvas::ExtractImageRep() const { 117 void Canvas::DrawStringWithHalo(const base::string16& text,
118 const Font& font,
119 SkColor text_color,
120 SkColor halo_color_in,
121 int x,
122 int y,
123 int w,
124 int h,
125 int flags) {
126 DrawStringRectWithHalo(text, FontList(font), text_color, halo_color_in,
127 Rect(x, y, w, h), flags);
128 }
129
130 ImageSkiaRep Canvas::ExtractImageRep() const {
102 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false); 131 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false);
103 132
104 // Make a bitmap to return, and a canvas to draw into it. We don't just want 133 // Make a bitmap to return, and a canvas to draw into it. We don't just want
105 // to call extractSubset or the copy constructor, since we want an actual copy 134 // to call extractSubset or the copy constructor, since we want an actual copy
106 // of the bitmap. 135 // of the bitmap.
107 SkBitmap result; 136 SkBitmap result;
108 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); 137 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config);
109 138
110 return gfx::ImageSkiaRep(result, scale_factor_); 139 return ImageSkiaRep(result, scale_factor_);
111 } 140 }
112 141
113 void Canvas::DrawDashedRect(const gfx::Rect& rect, SkColor color) { 142 void Canvas::DrawDashedRect(const Rect& rect, SkColor color) {
114 // Create a 2D bitmap containing alternating on/off pixels - we do this 143 // Create a 2D bitmap containing alternating on/off pixels - we do this
115 // so that you never get two pixels of the same color around the edges 144 // so that you never get two pixels of the same color around the edges
116 // of the focus rect (this may mean that opposing edges of the rect may 145 // of the focus rect (this may mean that opposing edges of the rect may
117 // have a dot pattern out of phase to each other). 146 // have a dot pattern out of phase to each other).
118 static SkColor last_color; 147 static SkColor last_color;
119 static SkBitmap* dots = NULL; 148 static SkBitmap* dots = NULL;
120 if (!dots || last_color != color) { 149 if (!dots || last_color != color) {
121 int col_pixels = 32; 150 int col_pixels = 32;
122 int row_pixels = 32; 151 int row_pixels = 32;
123 152
(...skipping 18 matching lines...) Expand all
142 // shader is refcounted and will have an initial refcount of 1. 171 // shader is refcounted and will have an initial refcount of 1.
143 skia::RefPtr<SkShader> shader = skia::AdoptRef( 172 skia::RefPtr<SkShader> shader = skia::AdoptRef(
144 SkShader::CreateBitmapShader( 173 SkShader::CreateBitmapShader(
145 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode)); 174 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode));
146 // Assign the shader to the paint & release our reference. The paint will 175 // Assign the shader to the paint & release our reference. The paint will
147 // now own the shader and the shader will be destroyed when the paint goes 176 // now own the shader and the shader will be destroyed when the paint goes
148 // out of scope. 177 // out of scope.
149 SkPaint paint; 178 SkPaint paint;
150 paint.setShader(shader.get()); 179 paint.setShader(shader.get());
151 180
152 DrawRect(gfx::Rect(rect.x(), rect.y(), rect.width(), 1), paint); 181 DrawRect(Rect(rect.x(), rect.y(), rect.width(), 1), paint);
153 DrawRect(gfx::Rect(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1), 182 DrawRect(Rect(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1),
154 paint); 183 paint);
155 DrawRect(gfx::Rect(rect.x(), rect.y(), 1, rect.height()), paint); 184 DrawRect(Rect(rect.x(), rect.y(), 1, rect.height()), paint);
156 DrawRect(gfx::Rect(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()), 185 DrawRect(Rect(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()),
157 paint); 186 paint);
158 } 187 }
159 188
160 void Canvas::Save() { 189 void Canvas::Save() {
161 canvas_->save(); 190 canvas_->save();
162 } 191 }
163 192
164 void Canvas::SaveLayerAlpha(uint8 alpha) { 193 void Canvas::SaveLayerAlpha(uint8 alpha) {
165 canvas_->saveLayerAlpha(NULL, alpha); 194 canvas_->saveLayerAlpha(NULL, alpha);
166 } 195 }
167 196
168 197
169 void Canvas::SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds) { 198 void Canvas::SaveLayerAlpha(uint8 alpha, const Rect& layer_bounds) {
170 SkRect bounds(gfx::RectToSkRect(layer_bounds)); 199 SkRect bounds(RectToSkRect(layer_bounds));
171 canvas_->saveLayerAlpha(&bounds, alpha); 200 canvas_->saveLayerAlpha(&bounds, alpha);
172 } 201 }
173 202
174 void Canvas::Restore() { 203 void Canvas::Restore() {
175 canvas_->restore(); 204 canvas_->restore();
176 } 205 }
177 206
178 bool Canvas::ClipRect(const gfx::Rect& rect) { 207 bool Canvas::ClipRect(const Rect& rect) {
179 return canvas_->clipRect(gfx::RectToSkRect(rect)); 208 return canvas_->clipRect(RectToSkRect(rect));
180 } 209 }
181 210
182 bool Canvas::ClipPath(const SkPath& path) { 211 bool Canvas::ClipPath(const SkPath& path) {
183 return canvas_->clipPath(path); 212 return canvas_->clipPath(path);
184 } 213 }
185 214
186 bool Canvas::GetClipBounds(gfx::Rect* bounds) { 215 bool Canvas::GetClipBounds(Rect* bounds) {
187 SkRect out; 216 SkRect out;
188 bool has_non_empty_clip = canvas_->getClipBounds(&out); 217 bool has_non_empty_clip = canvas_->getClipBounds(&out);
189 bounds->SetRect(out.left(), out.top(), out.width(), out.height()); 218 bounds->SetRect(out.left(), out.top(), out.width(), out.height());
190 return has_non_empty_clip; 219 return has_non_empty_clip;
191 } 220 }
192 221
193 void Canvas::Translate(const gfx::Vector2d& offset) { 222 void Canvas::Translate(const Vector2d& offset) {
194 canvas_->translate(SkIntToScalar(offset.x()), SkIntToScalar(offset.y())); 223 canvas_->translate(SkIntToScalar(offset.x()), SkIntToScalar(offset.y()));
195 } 224 }
196 225
197 void Canvas::Scale(int x_scale, int y_scale) { 226 void Canvas::Scale(int x_scale, int y_scale) {
198 canvas_->scale(SkIntToScalar(x_scale), SkIntToScalar(y_scale)); 227 canvas_->scale(SkIntToScalar(x_scale), SkIntToScalar(y_scale));
199 } 228 }
200 229
201 void Canvas::DrawColor(SkColor color) { 230 void Canvas::DrawColor(SkColor color) {
202 DrawColor(color, SkXfermode::kSrcOver_Mode); 231 DrawColor(color, SkXfermode::kSrcOver_Mode);
203 } 232 }
204 233
205 void Canvas::DrawColor(SkColor color, SkXfermode::Mode mode) { 234 void Canvas::DrawColor(SkColor color, SkXfermode::Mode mode) {
206 canvas_->drawColor(color, mode); 235 canvas_->drawColor(color, mode);
207 } 236 }
208 237
209 void Canvas::FillRect(const gfx::Rect& rect, SkColor color) { 238 void Canvas::FillRect(const Rect& rect, SkColor color) {
210 FillRect(rect, color, SkXfermode::kSrcOver_Mode); 239 FillRect(rect, color, SkXfermode::kSrcOver_Mode);
211 } 240 }
212 241
213 void Canvas::FillRect(const gfx::Rect& rect, 242 void Canvas::FillRect(const Rect& rect,
214 SkColor color, 243 SkColor color,
215 SkXfermode::Mode mode) { 244 SkXfermode::Mode mode) {
216 SkPaint paint; 245 SkPaint paint;
217 paint.setColor(color); 246 paint.setColor(color);
218 paint.setStyle(SkPaint::kFill_Style); 247 paint.setStyle(SkPaint::kFill_Style);
219 paint.setXfermodeMode(mode); 248 paint.setXfermodeMode(mode);
220 DrawRect(rect, paint); 249 DrawRect(rect, paint);
221 } 250 }
222 251
223 void Canvas::DrawRect(const gfx::Rect& rect, SkColor color) { 252 void Canvas::DrawRect(const Rect& rect, SkColor color) {
224 DrawRect(rect, color, SkXfermode::kSrcOver_Mode); 253 DrawRect(rect, color, SkXfermode::kSrcOver_Mode);
225 } 254 }
226 255
227 void Canvas::DrawRect(const gfx::Rect& rect, 256 void Canvas::DrawRect(const Rect& rect,
228 SkColor color, 257 SkColor color,
229 SkXfermode::Mode mode) { 258 SkXfermode::Mode mode) {
230 SkPaint paint; 259 SkPaint paint;
231 paint.setColor(color); 260 paint.setColor(color);
232 paint.setStyle(SkPaint::kStroke_Style); 261 paint.setStyle(SkPaint::kStroke_Style);
233 // Set a stroke width of 0, which will put us down the stroke rect path. If 262 // Set a stroke width of 0, which will put us down the stroke rect path. If
234 // we set a stroke width of 1, for example, this will internally create a 263 // we set a stroke width of 1, for example, this will internally create a
235 // path and fill it, which causes problems near the edge of the canvas. 264 // path and fill it, which causes problems near the edge of the canvas.
236 paint.setStrokeWidth(SkIntToScalar(0)); 265 paint.setStrokeWidth(SkIntToScalar(0));
237 paint.setXfermodeMode(mode); 266 paint.setXfermodeMode(mode);
238 267
239 DrawRect(rect, paint); 268 DrawRect(rect, paint);
240 } 269 }
241 270
242 void Canvas::DrawRect(const gfx::Rect& rect, const SkPaint& paint) { 271 void Canvas::DrawRect(const Rect& rect, const SkPaint& paint) {
243 canvas_->drawIRect(RectToSkIRect(rect), paint); 272 canvas_->drawIRect(RectToSkIRect(rect), paint);
244 } 273 }
245 274
246 void Canvas::DrawPoint(const gfx::Point& p1, const SkPaint& paint) { 275 void Canvas::DrawPoint(const Point& p1, const SkPaint& paint) {
247 canvas_->drawPoint(SkIntToScalar(p1.x()), SkIntToScalar(p1.y()), paint); 276 canvas_->drawPoint(SkIntToScalar(p1.x()), SkIntToScalar(p1.y()), paint);
248 } 277 }
249 278
250 void Canvas::DrawLine(const gfx::Point& p1, 279 void Canvas::DrawLine(const Point& p1, const Point& p2, SkColor color) {
251 const gfx::Point& p2,
252 SkColor color) {
253 SkPaint paint; 280 SkPaint paint;
254 paint.setColor(color); 281 paint.setColor(color);
255 paint.setStrokeWidth(SkIntToScalar(1)); 282 paint.setStrokeWidth(SkIntToScalar(1));
256 DrawLine(p1, p2, paint); 283 DrawLine(p1, p2, paint);
257 } 284 }
258 285
259 void Canvas::DrawLine(const gfx::Point& p1, 286 void Canvas::DrawLine(const Point& p1, const Point& p2, const SkPaint& paint) {
260 const gfx::Point& p2,
261 const SkPaint& paint) {
262 canvas_->drawLine(SkIntToScalar(p1.x()), SkIntToScalar(p1.y()), 287 canvas_->drawLine(SkIntToScalar(p1.x()), SkIntToScalar(p1.y()),
263 SkIntToScalar(p2.x()), SkIntToScalar(p2.y()), paint); 288 SkIntToScalar(p2.x()), SkIntToScalar(p2.y()), paint);
264 } 289 }
265 290
266 void Canvas::DrawCircle(const gfx::Point& center_point, 291 void Canvas::DrawCircle(const Point& center_point,
267 int radius, 292 int radius,
268 const SkPaint& paint) { 293 const SkPaint& paint) {
269 canvas_->drawCircle(SkIntToScalar(center_point.x()), 294 canvas_->drawCircle(SkIntToScalar(center_point.x()),
270 SkIntToScalar(center_point.y()), SkIntToScalar(radius), paint); 295 SkIntToScalar(center_point.y()), SkIntToScalar(radius), paint);
271 } 296 }
272 297
273 void Canvas::DrawRoundRect(const gfx::Rect& rect, 298 void Canvas::DrawRoundRect(const Rect& rect,
274 int radius, 299 int radius,
275 const SkPaint& paint) { 300 const SkPaint& paint) {
276 canvas_->drawRoundRect(RectToSkRect(rect), SkIntToScalar(radius), 301 canvas_->drawRoundRect(RectToSkRect(rect), SkIntToScalar(radius),
277 SkIntToScalar(radius), paint); 302 SkIntToScalar(radius), paint);
278 } 303 }
279 304
280 void Canvas::DrawPath(const SkPath& path, const SkPaint& paint) { 305 void Canvas::DrawPath(const SkPath& path, const SkPaint& paint) {
281 canvas_->drawPath(path, paint); 306 canvas_->drawPath(path, paint);
282 } 307 }
283 308
284 void Canvas::DrawFocusRect(const gfx::Rect& rect) { 309 void Canvas::DrawFocusRect(const Rect& rect) {
285 DrawDashedRect(rect, SK_ColorGRAY); 310 DrawDashedRect(rect, SK_ColorGRAY);
286 } 311 }
287 312
288 void Canvas::DrawImageInt(const gfx::ImageSkia& image, int x, int y) { 313 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y) {
289 SkPaint paint; 314 SkPaint paint;
290 DrawImageInt(image, x, y, paint); 315 DrawImageInt(image, x, y, paint);
291 } 316 }
292 317
293 void Canvas::DrawImageInt(const gfx::ImageSkia& image, int x, int y, uint8 a) { 318 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8 a) {
294 SkPaint paint; 319 SkPaint paint;
295 paint.setAlpha(a); 320 paint.setAlpha(a);
296 DrawImageInt(image, x, y, paint); 321 DrawImageInt(image, x, y, paint);
297 } 322 }
298 323
299 void Canvas::DrawImageInt(const gfx::ImageSkia& image, 324 void Canvas::DrawImageInt(const ImageSkia& image,
300 int x, int y, 325 int x,
326 int y,
301 const SkPaint& paint) { 327 const SkPaint& paint) {
302 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image); 328 const ImageSkiaRep& image_rep = GetImageRepToPaint(image);
303 if (image_rep.is_null()) 329 if (image_rep.is_null())
304 return; 330 return;
305 const SkBitmap& bitmap = image_rep.sk_bitmap(); 331 const SkBitmap& bitmap = image_rep.sk_bitmap();
306 float bitmap_scale = image_rep.GetScale(); 332 float bitmap_scale = image_rep.GetScale();
307 333
308 canvas_->save(); 334 canvas_->save();
309 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), 335 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale),
310 SkFloatToScalar(1.0f / bitmap_scale)); 336 SkFloatToScalar(1.0f / bitmap_scale));
311 canvas_->drawBitmap(bitmap, 337 canvas_->drawBitmap(bitmap,
312 SkFloatToScalar(x * bitmap_scale), 338 SkFloatToScalar(x * bitmap_scale),
313 SkFloatToScalar(y * bitmap_scale), 339 SkFloatToScalar(y * bitmap_scale),
314 &paint); 340 &paint);
315 canvas_->restore(); 341 canvas_->restore();
316 } 342 }
317 343
318 void Canvas::DrawImageInt(const gfx::ImageSkia& image, 344 void Canvas::DrawImageInt(const ImageSkia& image,
319 int src_x, int src_y, int src_w, int src_h, 345 int src_x,
320 int dest_x, int dest_y, int dest_w, int dest_h, 346 int src_y,
347 int src_w,
348 int src_h,
349 int dest_x,
350 int dest_y,
351 int dest_w,
352 int dest_h,
321 bool filter) { 353 bool filter) {
322 SkPaint p; 354 SkPaint p;
323 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, 355 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y,
324 dest_w, dest_h, filter, p); 356 dest_w, dest_h, filter, p);
325 } 357 }
326 358
327 void Canvas::DrawImageInt(const gfx::ImageSkia& image, 359 void Canvas::DrawImageInt(const ImageSkia& image,
328 int src_x, int src_y, int src_w, int src_h, 360 int src_x,
329 int dest_x, int dest_y, int dest_w, int dest_h, 361 int src_y,
362 int src_w,
363 int src_h,
364 int dest_x,
365 int dest_y,
366 int dest_w,
367 int dest_h,
330 bool filter, 368 bool filter,
331 const SkPaint& paint) { 369 const SkPaint& paint) {
332 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && 370 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() &&
333 src_y + src_h < std::numeric_limits<int16_t>::max()); 371 src_y + src_h < std::numeric_limits<int16_t>::max());
334 if (src_w <= 0 || src_h <= 0) { 372 if (src_w <= 0 || src_h <= 0) {
335 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; 373 NOTREACHED() << "Attempting to draw bitmap from an empty rect!";
336 return; 374 return;
337 } 375 }
338 376
339 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) 377 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h))
340 return; 378 return;
341 379
342 float user_scale_x = static_cast<float>(dest_w) / src_w; 380 float user_scale_x = static_cast<float>(dest_w) / src_w;
343 float user_scale_y = static_cast<float>(dest_h) / src_h; 381 float user_scale_y = static_cast<float>(dest_h) / src_h;
344 382
345 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image, 383 const ImageSkiaRep& image_rep = GetImageRepToPaint(image,
346 user_scale_x, user_scale_y); 384 user_scale_x, user_scale_y);
347 if (image_rep.is_null()) 385 if (image_rep.is_null())
348 return; 386 return;
349 387
350 SkRect dest_rect = { SkIntToScalar(dest_x), 388 SkRect dest_rect = { SkIntToScalar(dest_x),
351 SkIntToScalar(dest_y), 389 SkIntToScalar(dest_y),
352 SkIntToScalar(dest_x + dest_w), 390 SkIntToScalar(dest_x + dest_w),
353 SkIntToScalar(dest_y + dest_h) }; 391 SkIntToScalar(dest_y + dest_h) };
354 392
355 if (src_w == dest_w && src_h == dest_h && 393 if (src_w == dest_w && src_h == dest_h &&
(...skipping 10 matching lines...) Expand all
366 // Make a bitmap shader that contains the bitmap we want to draw. This is 404 // Make a bitmap shader that contains the bitmap we want to draw. This is
367 // basically what SkCanvas.drawBitmap does internally, but it gives us 405 // basically what SkCanvas.drawBitmap does internally, but it gives us
368 // more control over quality and will use the mipmap in the source image if 406 // more control over quality and will use the mipmap in the source image if
369 // it has one, whereas drawBitmap won't. 407 // it has one, whereas drawBitmap won't.
370 SkMatrix shader_scale; 408 SkMatrix shader_scale;
371 shader_scale.setScale(SkFloatToScalar(user_scale_x), 409 shader_scale.setScale(SkFloatToScalar(user_scale_x),
372 SkFloatToScalar(user_scale_y)); 410 SkFloatToScalar(user_scale_y));
373 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); 411 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
374 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); 412 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
375 413
376 skia::RefPtr<SkShader> shader = gfx::CreateImageRepShader( 414 skia::RefPtr<SkShader> shader = CreateImageRepShader(
377 image_rep, 415 image_rep,
378 SkShader::kRepeat_TileMode, 416 SkShader::kRepeat_TileMode,
379 shader_scale); 417 shader_scale);
380 418
381 // Set up our paint to use the shader & release our reference (now just owned 419 // Set up our paint to use the shader & release our reference (now just owned
382 // by the paint). 420 // by the paint).
383 SkPaint p(paint); 421 SkPaint p(paint);
384 p.setFilterBitmap(filter); 422 p.setFilterBitmap(filter);
385 p.setShader(shader.get()); 423 p.setShader(shader.get());
386 424
387 // The rect will be filled by the bitmap. 425 // The rect will be filled by the bitmap.
388 canvas_->drawRect(dest_rect, p); 426 canvas_->drawRect(dest_rect, p);
389 } 427 }
390 428
391 void Canvas::DrawImageInPath(const gfx::ImageSkia& image, 429 void Canvas::DrawImageInPath(const ImageSkia& image,
392 int x, 430 int x,
393 int y, 431 int y,
394 const SkPath& path, 432 const SkPath& path,
395 const SkPaint& paint) { 433 const SkPaint& paint) {
396 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image); 434 const ImageSkiaRep& image_rep = GetImageRepToPaint(image);
397 if (image_rep.is_null()) 435 if (image_rep.is_null())
398 return; 436 return;
399 437
400 SkMatrix matrix; 438 SkMatrix matrix;
401 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); 439 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
402 skia::RefPtr<SkShader> shader = gfx::CreateImageRepShader( 440 skia::RefPtr<SkShader> shader = CreateImageRepShader(
403 image_rep, 441 image_rep,
404 SkShader::kRepeat_TileMode, 442 SkShader::kRepeat_TileMode,
405 matrix); 443 matrix);
406 444
407 SkPaint p(paint); 445 SkPaint p(paint);
408 p.setShader(shader.get()); 446 p.setShader(shader.get());
409 canvas_->drawPath(path, p); 447 canvas_->drawPath(path, p);
410 } 448 }
411 449
450 void Canvas::DrawStringRect(const base::string16& text,
451 const FontList& font_list,
452 SkColor color,
453 const Rect& display_rect) {
454 DrawStringRectWithFlags(text, font_list, color, display_rect,
455 DefaultCanvasTextAlignment());
456 }
457
458 void Canvas::DrawStringRectWithFlags(const base::string16& text,
459 const FontList& font_list,
460 SkColor color,
461 const Rect& display_rect,
462 int flags) {
463 DrawStringRectWithShadows(text, font_list, color, display_rect, 0, flags,
464 ShadowValues());
465 }
466
412 void Canvas::DrawStringInt(const base::string16& text, 467 void Canvas::DrawStringInt(const base::string16& text,
413 const gfx::Font& font, 468 const Font& font,
414 SkColor color, 469 SkColor color,
415 int x, int y, int w, int h) { 470 int x,
471 int y,
472 int w,
473 int h) {
416 DrawStringInt(text, font, color, x, y, w, h, DefaultCanvasTextAlignment()); 474 DrawStringInt(text, font, color, x, y, w, h, DefaultCanvasTextAlignment());
417 } 475 }
418 476
419 void Canvas::DrawStringInt(const base::string16& text, 477 void Canvas::DrawStringInt(const base::string16& text,
420 const gfx::Font& font, 478 const Font& font,
421 SkColor color, 479 SkColor color,
422 const gfx::Rect& display_rect) { 480 const Rect& display_rect) {
423 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), 481 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(),
424 display_rect.width(), display_rect.height()); 482 display_rect.width(), display_rect.height());
425 } 483 }
426 484
427 void Canvas::DrawStringInt(const base::string16& text, 485 void Canvas::DrawStringInt(const base::string16& text,
428 const gfx::Font& font, 486 const Font& font,
429 SkColor color, 487 SkColor color,
430 int x, int y, int w, int h, 488 int x,
489 int y,
490 int w,
491 int h,
431 int flags) { 492 int flags) {
432 DrawStringWithShadows(text, 493 DrawStringWithShadows(text, font, color, Rect(x, y, w, h), 0, flags,
433 font,
434 color,
435 gfx::Rect(x, y, w, h),
436 0,
437 flags,
438 ShadowValues()); 494 ShadowValues());
439 } 495 }
440 496
441 void Canvas::TileImageInt(const gfx::ImageSkia& image, 497 void Canvas::DrawStringWithShadows(const base::string16& text,
442 int x, int y, int w, int h) { 498 const Font& font,
499 SkColor color,
500 const Rect& text_bounds,
501 int line_height,
502 int flags,
503 const ShadowValues& shadows) {
504 DrawStringRectWithShadows(text, FontList(font), color, text_bounds,
505 line_height, flags, shadows);
506 }
507
508 void Canvas::TileImageInt(const ImageSkia& image,
509 int x,
510 int y,
511 int w,
512 int h) {
443 TileImageInt(image, 0, 0, x, y, w, h); 513 TileImageInt(image, 0, 0, x, y, w, h);
444 } 514 }
445 515
446 void Canvas::TileImageInt(const gfx::ImageSkia& image, 516 void Canvas::TileImageInt(const ImageSkia& image,
447 int src_x, int src_y, 517 int src_x,
448 int dest_x, int dest_y, int w, int h) { 518 int src_y,
519 int dest_x,
520 int dest_y,
521 int w,
522 int h) {
449 TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h); 523 TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h);
450 } 524 }
451 525
452 void Canvas::TileImageInt(const gfx::ImageSkia& image, 526 void Canvas::TileImageInt(const ImageSkia& image,
453 int src_x, int src_y, 527 int src_x,
454 float tile_scale_x, float tile_scale_y, 528 int src_y,
455 int dest_x, int dest_y, int w, int h) { 529 float tile_scale_x,
530 float tile_scale_y,
531 int dest_x,
532 int dest_y,
533 int w,
534 int h) {
456 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) 535 if (!IntersectsClipRectInt(dest_x, dest_y, w, h))
457 return; 536 return;
458 537
459 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image, 538 const ImageSkiaRep& image_rep = GetImageRepToPaint(
460 tile_scale_x, tile_scale_y); 539 image, tile_scale_x, tile_scale_y);
461 if (image_rep.is_null()) 540 if (image_rep.is_null())
462 return; 541 return;
463 542
464 SkMatrix shader_scale; 543 SkMatrix shader_scale;
465 shader_scale.setScale(SkFloatToScalar(tile_scale_x), 544 shader_scale.setScale(SkFloatToScalar(tile_scale_x),
466 SkFloatToScalar(tile_scale_y)); 545 SkFloatToScalar(tile_scale_y));
467 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); 546 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
468 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); 547 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
469 548
470 skia::RefPtr<SkShader> shader = gfx::CreateImageRepShader( 549 skia::RefPtr<SkShader> shader = CreateImageRepShader(
471 image_rep, 550 image_rep,
472 SkShader::kRepeat_TileMode, 551 SkShader::kRepeat_TileMode,
473 shader_scale); 552 shader_scale);
474 553
475 SkPaint paint; 554 SkPaint paint;
476 paint.setShader(shader.get()); 555 paint.setShader(shader.get());
477 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); 556 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
478 557
479 SkRect dest_rect = { SkIntToScalar(dest_x), 558 SkRect dest_rect = { SkIntToScalar(dest_x),
480 SkIntToScalar(dest_y), 559 SkIntToScalar(dest_y),
481 SkIntToScalar(dest_x + w), 560 SkIntToScalar(dest_x + w),
482 SkIntToScalar(dest_y + h) }; 561 SkIntToScalar(dest_y + h) };
483 canvas_->drawRect(dest_rect, paint); 562 canvas_->drawRect(dest_rect, paint);
484 } 563 }
485 564
486 gfx::NativeDrawingContext Canvas::BeginPlatformPaint() { 565 NativeDrawingContext Canvas::BeginPlatformPaint() {
487 return skia::BeginPlatformPaint(canvas_); 566 return skia::BeginPlatformPaint(canvas_);
488 } 567 }
489 568
490 void Canvas::EndPlatformPaint() { 569 void Canvas::EndPlatformPaint() {
491 skia::EndPlatformPaint(canvas_); 570 skia::EndPlatformPaint(canvas_);
492 } 571 }
493 572
494 void Canvas::Transform(const gfx::Transform& transform) { 573 void Canvas::Transform(const gfx::Transform& transform) {
495 canvas_->concat(transform.matrix()); 574 canvas_->concat(transform.matrix());
496 } 575 }
497 576
498 Canvas::Canvas(SkCanvas* canvas, ui::ScaleFactor scale_factor) 577 Canvas::Canvas(SkCanvas* canvas, ui::ScaleFactor scale_factor)
499 : scale_factor_(scale_factor), 578 : scale_factor_(scale_factor),
500 owned_canvas_(), 579 owned_canvas_(),
501 canvas_(canvas) { 580 canvas_(canvas) {
502 DCHECK(canvas); 581 DCHECK(canvas);
503 } 582 }
504 583
505 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) { 584 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) {
506 SkRect clip; 585 SkRect clip;
507 return canvas_->getClipBounds(&clip) && 586 return canvas_->getClipBounds(&clip) &&
508 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), 587 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w),
509 SkIntToScalar(y + h)); 588 SkIntToScalar(y + h));
510 } 589 }
511 590
512 bool Canvas::IntersectsClipRect(const gfx::Rect& rect) { 591 bool Canvas::IntersectsClipRect(const Rect& rect) {
513 return IntersectsClipRectInt(rect.x(), rect.y(), 592 return IntersectsClipRectInt(rect.x(), rect.y(),
514 rect.width(), rect.height()); 593 rect.width(), rect.height());
515 } 594 }
516 595
517 const gfx::ImageSkiaRep& Canvas::GetImageRepToPaint( 596 const ImageSkiaRep& Canvas::GetImageRepToPaint(const ImageSkia& image) const {
518 const gfx::ImageSkia& image) const {
519 return GetImageRepToPaint(image, 1.0f, 1.0f); 597 return GetImageRepToPaint(image, 1.0f, 1.0f);
520 } 598 }
521 599
522 const gfx::ImageSkiaRep& Canvas::GetImageRepToPaint( 600 const ImageSkiaRep& Canvas::GetImageRepToPaint(
523 const gfx::ImageSkia& image, 601 const ImageSkia& image,
524 float user_additional_scale_x, 602 float user_additional_scale_x,
525 float user_additional_scale_y) const { 603 float user_additional_scale_y) const {
526 const gfx::ImageSkiaRep& image_rep = image.GetRepresentation(scale_factor_); 604 const ImageSkiaRep& image_rep = image.GetRepresentation(scale_factor_);
527 605
528 if (!image_rep.is_null()) { 606 if (!image_rep.is_null()) {
529 SkMatrix m = canvas_->getTotalMatrix(); 607 SkMatrix m = canvas_->getTotalMatrix();
530 float scale_x = SkScalarToFloat(SkScalarAbs(m.getScaleX())) * 608 float scale_x = SkScalarToFloat(SkScalarAbs(m.getScaleX())) *
531 user_additional_scale_x; 609 user_additional_scale_x;
532 float scale_y = SkScalarToFloat(SkScalarAbs(m.getScaleY())) * 610 float scale_y = SkScalarToFloat(SkScalarAbs(m.getScaleY())) *
533 user_additional_scale_y; 611 user_additional_scale_y;
534 612
535 float bitmap_scale = image_rep.GetScale(); 613 float bitmap_scale = image_rep.GetScale();
536 if (scale_x < bitmap_scale || scale_y < bitmap_scale) 614 if (scale_x < bitmap_scale || scale_y < bitmap_scale)
537 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); 615 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap();
538 } 616 }
539 617
540 return image_rep; 618 return image_rep;
541 } 619 }
542 620
543 } // namespace gfx 621 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/canvas.h ('k') | ui/gfx/canvas_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698