OLD | NEW |
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 { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 gfx::ScaleSize(size, ui::GetScaleFactorScale(scale_factor))); | 80 gfx::ScaleSize(size, ui::GetScaleFactorScale(scale_factor))); |
81 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(), | 81 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(), |
82 pixel_size.height(), | 82 pixel_size.height(), |
83 is_opaque)); | 83 is_opaque)); |
84 canvas_ = owned_canvas_.get(); | 84 canvas_ = owned_canvas_.get(); |
85 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_)); | 85 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_)); |
86 canvas_->scale(scale, scale); | 86 canvas_->scale(scale, scale); |
87 } | 87 } |
88 | 88 |
89 // static | 89 // static |
90 int Canvas::GetStringWidth(const base::string16& text, const gfx::Font& font) { | |
91 int width = 0, height = 0; | |
92 Canvas::SizeStringInt(text, font, &width, &height, 0, NO_ELLIPSIS); | |
93 return width; | |
94 } | |
95 | |
96 // static | |
97 int Canvas::DefaultCanvasTextAlignment() { | 90 int Canvas::DefaultCanvasTextAlignment() { |
98 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; | 91 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; |
99 } | 92 } |
100 | 93 |
| 94 void Canvas::DrawStringWithHalo(const base::string16& text, |
| 95 const gfx::Font& font, |
| 96 SkColor text_color, |
| 97 SkColor halo_color_in, |
| 98 int x, |
| 99 int y, |
| 100 int w, |
| 101 int h, |
| 102 int flags) { |
| 103 DrawStringWithHaloRect(text, gfx::FontList(font), text_color, halo_color_in, |
| 104 gfx::Rect(x, y, w, h), flags); |
| 105 } |
| 106 |
101 gfx::ImageSkiaRep Canvas::ExtractImageRep() const { | 107 gfx::ImageSkiaRep Canvas::ExtractImageRep() const { |
102 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false); | 108 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false); |
103 | 109 |
104 // Make a bitmap to return, and a canvas to draw into it. We don't just want | 110 // 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 | 111 // to call extractSubset or the copy constructor, since we want an actual copy |
106 // of the bitmap. | 112 // of the bitmap. |
107 SkBitmap result; | 113 SkBitmap result; |
108 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); | 114 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); |
109 | 115 |
110 return gfx::ImageSkiaRep(result, scale_factor_); | 116 return gfx::ImageSkiaRep(result, scale_factor_); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 DrawImageInt(image, x, y, paint); | 296 DrawImageInt(image, x, y, paint); |
291 } | 297 } |
292 | 298 |
293 void Canvas::DrawImageInt(const gfx::ImageSkia& image, int x, int y, uint8 a) { | 299 void Canvas::DrawImageInt(const gfx::ImageSkia& image, int x, int y, uint8 a) { |
294 SkPaint paint; | 300 SkPaint paint; |
295 paint.setAlpha(a); | 301 paint.setAlpha(a); |
296 DrawImageInt(image, x, y, paint); | 302 DrawImageInt(image, x, y, paint); |
297 } | 303 } |
298 | 304 |
299 void Canvas::DrawImageInt(const gfx::ImageSkia& image, | 305 void Canvas::DrawImageInt(const gfx::ImageSkia& image, |
300 int x, int y, | 306 int x, |
| 307 int y, |
301 const SkPaint& paint) { | 308 const SkPaint& paint) { |
302 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image); | 309 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image); |
303 if (image_rep.is_null()) | 310 if (image_rep.is_null()) |
304 return; | 311 return; |
305 const SkBitmap& bitmap = image_rep.sk_bitmap(); | 312 const SkBitmap& bitmap = image_rep.sk_bitmap(); |
306 float bitmap_scale = image_rep.GetScale(); | 313 float bitmap_scale = image_rep.GetScale(); |
307 | 314 |
308 canvas_->save(); | 315 canvas_->save(); |
309 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), | 316 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), |
310 SkFloatToScalar(1.0f / bitmap_scale)); | 317 SkFloatToScalar(1.0f / bitmap_scale)); |
311 canvas_->drawBitmap(bitmap, | 318 canvas_->drawBitmap(bitmap, |
312 SkFloatToScalar(x * bitmap_scale), | 319 SkFloatToScalar(x * bitmap_scale), |
313 SkFloatToScalar(y * bitmap_scale), | 320 SkFloatToScalar(y * bitmap_scale), |
314 &paint); | 321 &paint); |
315 canvas_->restore(); | 322 canvas_->restore(); |
316 } | 323 } |
317 | 324 |
318 void Canvas::DrawImageInt(const gfx::ImageSkia& image, | 325 void Canvas::DrawImageInt(const gfx::ImageSkia& image, |
319 int src_x, int src_y, int src_w, int src_h, | 326 int src_x, |
320 int dest_x, int dest_y, int dest_w, int dest_h, | 327 int src_y, |
| 328 int src_w, |
| 329 int src_h, |
| 330 int dest_x, |
| 331 int dest_y, |
| 332 int dest_w, |
| 333 int dest_h, |
321 bool filter) { | 334 bool filter) { |
322 SkPaint p; | 335 SkPaint p; |
323 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, | 336 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, |
324 dest_w, dest_h, filter, p); | 337 dest_w, dest_h, filter, p); |
325 } | 338 } |
326 | 339 |
327 void Canvas::DrawImageInt(const gfx::ImageSkia& image, | 340 void Canvas::DrawImageInt(const gfx::ImageSkia& image, |
328 int src_x, int src_y, int src_w, int src_h, | 341 int src_x, |
329 int dest_x, int dest_y, int dest_w, int dest_h, | 342 int src_y, |
| 343 int src_w, |
| 344 int src_h, |
| 345 int dest_x, |
| 346 int dest_y, |
| 347 int dest_w, |
| 348 int dest_h, |
330 bool filter, | 349 bool filter, |
331 const SkPaint& paint) { | 350 const SkPaint& paint) { |
332 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && | 351 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
333 src_y + src_h < std::numeric_limits<int16_t>::max()); | 352 src_y + src_h < std::numeric_limits<int16_t>::max()); |
334 if (src_w <= 0 || src_h <= 0) { | 353 if (src_w <= 0 || src_h <= 0) { |
335 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; | 354 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; |
336 return; | 355 return; |
337 } | 356 } |
338 | 357 |
339 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) | 358 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 skia::RefPtr<SkShader> shader = gfx::CreateImageRepShader( | 421 skia::RefPtr<SkShader> shader = gfx::CreateImageRepShader( |
403 image_rep, | 422 image_rep, |
404 SkShader::kRepeat_TileMode, | 423 SkShader::kRepeat_TileMode, |
405 matrix); | 424 matrix); |
406 | 425 |
407 SkPaint p(paint); | 426 SkPaint p(paint); |
408 p.setShader(shader.get()); | 427 p.setShader(shader.get()); |
409 canvas_->drawPath(path, p); | 428 canvas_->drawPath(path, p); |
410 } | 429 } |
411 | 430 |
| 431 void Canvas::DrawStringRect(const base::string16& text, |
| 432 const gfx::FontList& font_list, |
| 433 SkColor color, |
| 434 const gfx::Rect& display_rect) { |
| 435 DrawStringWithFlagsRect(text, font_list, color, display_rect, |
| 436 DefaultCanvasTextAlignment()); |
| 437 } |
| 438 |
| 439 void Canvas::DrawStringWithFlagsRect(const base::string16& text, |
| 440 const gfx::FontList& font_list, |
| 441 SkColor color, |
| 442 const gfx::Rect& display_rect, |
| 443 int flags) { |
| 444 DrawStringWithShadowsRect(text, font_list, color, display_rect, 0, flags, |
| 445 ShadowValues()); |
| 446 } |
| 447 |
412 void Canvas::DrawStringInt(const base::string16& text, | 448 void Canvas::DrawStringInt(const base::string16& text, |
413 const gfx::Font& font, | 449 const gfx::Font& font, |
414 SkColor color, | 450 SkColor color, |
415 int x, int y, int w, int h) { | 451 int x, |
| 452 int y, |
| 453 int w, |
| 454 int h) { |
416 DrawStringInt(text, font, color, x, y, w, h, DefaultCanvasTextAlignment()); | 455 DrawStringInt(text, font, color, x, y, w, h, DefaultCanvasTextAlignment()); |
417 } | 456 } |
418 | 457 |
419 void Canvas::DrawStringInt(const base::string16& text, | 458 void Canvas::DrawStringInt(const base::string16& text, |
420 const gfx::Font& font, | 459 const gfx::Font& font, |
421 SkColor color, | 460 SkColor color, |
422 const gfx::Rect& display_rect) { | 461 const gfx::Rect& display_rect) { |
423 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), | 462 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), |
424 display_rect.width(), display_rect.height()); | 463 display_rect.width(), display_rect.height()); |
425 } | 464 } |
426 | 465 |
427 void Canvas::DrawStringInt(const base::string16& text, | 466 void Canvas::DrawStringInt(const base::string16& text, |
428 const gfx::Font& font, | 467 const gfx::Font& font, |
429 SkColor color, | 468 SkColor color, |
430 int x, int y, int w, int h, | 469 int x, |
| 470 int y, |
| 471 int w, |
| 472 int h, |
431 int flags) { | 473 int flags) { |
432 DrawStringWithShadows(text, | 474 DrawStringWithShadows(text, font, color, gfx::Rect(x, y, w, h), 0, flags, |
433 font, | |
434 color, | |
435 gfx::Rect(x, y, w, h), | |
436 0, | |
437 flags, | |
438 ShadowValues()); | 475 ShadowValues()); |
439 } | 476 } |
440 | 477 |
| 478 void Canvas::DrawStringWithShadows(const base::string16& text, |
| 479 const gfx::Font& font, |
| 480 SkColor color, |
| 481 const gfx::Rect& text_bounds, |
| 482 int line_height, |
| 483 int flags, |
| 484 const ShadowValues& shadows) { |
| 485 DrawStringWithShadowsRect(text, gfx::FontList(font), color, text_bounds, |
| 486 line_height, flags, shadows); |
| 487 } |
| 488 |
441 void Canvas::TileImageInt(const gfx::ImageSkia& image, | 489 void Canvas::TileImageInt(const gfx::ImageSkia& image, |
442 int x, int y, int w, int h) { | 490 int x, |
| 491 int y, |
| 492 int w, |
| 493 int h) { |
443 TileImageInt(image, 0, 0, x, y, w, h); | 494 TileImageInt(image, 0, 0, x, y, w, h); |
444 } | 495 } |
445 | 496 |
446 void Canvas::TileImageInt(const gfx::ImageSkia& image, | 497 void Canvas::TileImageInt(const gfx::ImageSkia& image, |
447 int src_x, int src_y, | 498 int src_x, |
448 int dest_x, int dest_y, int w, int h) { | 499 int src_y, |
| 500 int dest_x, |
| 501 int dest_y, |
| 502 int w, |
| 503 int h) { |
449 TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h); | 504 TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h); |
450 } | 505 } |
451 | 506 |
452 void Canvas::TileImageInt(const gfx::ImageSkia& image, | 507 void Canvas::TileImageInt(const gfx::ImageSkia& image, |
453 int src_x, int src_y, | 508 int src_x, |
454 float tile_scale_x, float tile_scale_y, | 509 int src_y, |
455 int dest_x, int dest_y, int w, int h) { | 510 float tile_scale_x, |
| 511 float tile_scale_y, |
| 512 int dest_x, |
| 513 int dest_y, |
| 514 int w, |
| 515 int h) { |
456 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) | 516 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) |
457 return; | 517 return; |
458 | 518 |
459 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image, | 519 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image, |
460 tile_scale_x, tile_scale_y); | 520 tile_scale_x, tile_scale_y); |
461 if (image_rep.is_null()) | 521 if (image_rep.is_null()) |
462 return; | 522 return; |
463 | 523 |
464 SkMatrix shader_scale; | 524 SkMatrix shader_scale; |
465 shader_scale.setScale(SkFloatToScalar(tile_scale_x), | 525 shader_scale.setScale(SkFloatToScalar(tile_scale_x), |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 | 594 |
535 float bitmap_scale = image_rep.GetScale(); | 595 float bitmap_scale = image_rep.GetScale(); |
536 if (scale_x < bitmap_scale || scale_y < bitmap_scale) | 596 if (scale_x < bitmap_scale || scale_y < bitmap_scale) |
537 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); | 597 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); |
538 } | 598 } |
539 | 599 |
540 return image_rep; | 600 return image_rep; |
541 } | 601 } |
542 | 602 |
543 } // namespace gfx | 603 } // namespace gfx |
OLD | NEW |