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 void Canvas::SizeStringInt(const base::string16& text, |
| 91 const gfx::Font& font, |
| 92 int* width, |
| 93 int* height, |
| 94 int line_height, |
| 95 int flags) { |
| 96 SizeStringInt(text, gfx::FontList(font), width, height, line_height, flags); |
| 97 } |
| 98 |
| 99 // static |
| 100 int Canvas::GetStringWidth(const base::string16& text, |
| 101 const gfx::FontList& font_list) { |
| 102 int width = 0, height = 0; |
| 103 SizeStringInt(text, font_list, &width, &height, 0, NO_ELLIPSIS); |
| 104 return width; |
| 105 } |
| 106 |
| 107 // static |
90 int Canvas::GetStringWidth(const base::string16& text, const gfx::Font& font) { | 108 int Canvas::GetStringWidth(const base::string16& text, const gfx::Font& font) { |
91 int width = 0, height = 0; | 109 int width = 0, height = 0; |
92 Canvas::SizeStringInt(text, font, &width, &height, 0, NO_ELLIPSIS); | 110 SizeStringInt(text, gfx::FontList(font), &width, &height, 0, NO_ELLIPSIS); |
93 return width; | 111 return width; |
94 } | 112 } |
95 | 113 |
96 // static | 114 // static |
97 int Canvas::DefaultCanvasTextAlignment() { | 115 int Canvas::DefaultCanvasTextAlignment() { |
98 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; | 116 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; |
99 } | 117 } |
100 | 118 |
| 119 void Canvas::DrawStringWithHalo(const base::string16& text, |
| 120 const gfx::Font& font, |
| 121 SkColor text_color, |
| 122 SkColor halo_color_in, |
| 123 int x, |
| 124 int y, |
| 125 int w, |
| 126 int h, |
| 127 int flags) { |
| 128 DrawStringWithHalo(text, gfx::FontList(font), text_color, halo_color_in, |
| 129 gfx::Rect(x, y, w, h), flags); |
| 130 } |
| 131 |
101 gfx::ImageSkiaRep Canvas::ExtractImageRep() const { | 132 gfx::ImageSkiaRep Canvas::ExtractImageRep() const { |
102 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false); | 133 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false); |
103 | 134 |
104 // Make a bitmap to return, and a canvas to draw into it. We don't just want | 135 // 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 | 136 // to call extractSubset or the copy constructor, since we want an actual copy |
106 // of the bitmap. | 137 // of the bitmap. |
107 SkBitmap result; | 138 SkBitmap result; |
108 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); | 139 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); |
109 | 140 |
110 return gfx::ImageSkiaRep(result, scale_factor_); | 141 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); | 321 DrawImageInt(image, x, y, paint); |
291 } | 322 } |
292 | 323 |
293 void Canvas::DrawImageInt(const gfx::ImageSkia& image, int x, int y, uint8 a) { | 324 void Canvas::DrawImageInt(const gfx::ImageSkia& image, int x, int y, uint8 a) { |
294 SkPaint paint; | 325 SkPaint paint; |
295 paint.setAlpha(a); | 326 paint.setAlpha(a); |
296 DrawImageInt(image, x, y, paint); | 327 DrawImageInt(image, x, y, paint); |
297 } | 328 } |
298 | 329 |
299 void Canvas::DrawImageInt(const gfx::ImageSkia& image, | 330 void Canvas::DrawImageInt(const gfx::ImageSkia& image, |
300 int x, int y, | 331 int x, |
| 332 int y, |
301 const SkPaint& paint) { | 333 const SkPaint& paint) { |
302 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image); | 334 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image); |
303 if (image_rep.is_null()) | 335 if (image_rep.is_null()) |
304 return; | 336 return; |
305 const SkBitmap& bitmap = image_rep.sk_bitmap(); | 337 const SkBitmap& bitmap = image_rep.sk_bitmap(); |
306 float bitmap_scale = image_rep.GetScale(); | 338 float bitmap_scale = image_rep.GetScale(); |
307 | 339 |
308 canvas_->save(); | 340 canvas_->save(); |
309 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), | 341 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), |
310 SkFloatToScalar(1.0f / bitmap_scale)); | 342 SkFloatToScalar(1.0f / bitmap_scale)); |
311 canvas_->drawBitmap(bitmap, | 343 canvas_->drawBitmap(bitmap, |
312 SkFloatToScalar(x * bitmap_scale), | 344 SkFloatToScalar(x * bitmap_scale), |
313 SkFloatToScalar(y * bitmap_scale), | 345 SkFloatToScalar(y * bitmap_scale), |
314 &paint); | 346 &paint); |
315 canvas_->restore(); | 347 canvas_->restore(); |
316 } | 348 } |
317 | 349 |
318 void Canvas::DrawImageInt(const gfx::ImageSkia& image, | 350 void Canvas::DrawImageInt(const gfx::ImageSkia& image, |
319 int src_x, int src_y, int src_w, int src_h, | 351 int src_x, |
320 int dest_x, int dest_y, int dest_w, int dest_h, | 352 int src_y, |
| 353 int src_w, |
| 354 int src_h, |
| 355 int dest_x, |
| 356 int dest_y, |
| 357 int dest_w, |
| 358 int dest_h, |
321 bool filter) { | 359 bool filter) { |
322 SkPaint p; | 360 SkPaint p; |
323 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, | 361 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, |
324 dest_w, dest_h, filter, p); | 362 dest_w, dest_h, filter, p); |
325 } | 363 } |
326 | 364 |
327 void Canvas::DrawImageInt(const gfx::ImageSkia& image, | 365 void Canvas::DrawImageInt(const gfx::ImageSkia& image, |
328 int src_x, int src_y, int src_w, int src_h, | 366 int src_x, |
329 int dest_x, int dest_y, int dest_w, int dest_h, | 367 int src_y, |
| 368 int src_w, |
| 369 int src_h, |
| 370 int dest_x, |
| 371 int dest_y, |
| 372 int dest_w, |
| 373 int dest_h, |
330 bool filter, | 374 bool filter, |
331 const SkPaint& paint) { | 375 const SkPaint& paint) { |
332 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && | 376 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
333 src_y + src_h < std::numeric_limits<int16_t>::max()); | 377 src_y + src_h < std::numeric_limits<int16_t>::max()); |
334 if (src_w <= 0 || src_h <= 0) { | 378 if (src_w <= 0 || src_h <= 0) { |
335 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; | 379 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; |
336 return; | 380 return; |
337 } | 381 } |
338 | 382 |
339 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) | 383 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( | 446 skia::RefPtr<SkShader> shader = gfx::CreateImageRepShader( |
403 image_rep, | 447 image_rep, |
404 SkShader::kRepeat_TileMode, | 448 SkShader::kRepeat_TileMode, |
405 matrix); | 449 matrix); |
406 | 450 |
407 SkPaint p(paint); | 451 SkPaint p(paint); |
408 p.setShader(shader.get()); | 452 p.setShader(shader.get()); |
409 canvas_->drawPath(path, p); | 453 canvas_->drawPath(path, p); |
410 } | 454 } |
411 | 455 |
| 456 void Canvas::DrawString(const base::string16& text, |
| 457 const gfx::FontList& font_list, |
| 458 SkColor color, |
| 459 const gfx::Rect& display_rect) { |
| 460 DrawStringWithFlags(text, font_list, color, display_rect, |
| 461 DefaultCanvasTextAlignment()); |
| 462 } |
| 463 |
| 464 void Canvas::DrawStringWithFlags(const base::string16& text, |
| 465 const gfx::FontList& font_list, |
| 466 SkColor color, |
| 467 const gfx::Rect& display_rect, |
| 468 int flags) { |
| 469 DrawStringWithShadows(text, font_list, color, display_rect, 0, flags, |
| 470 ShadowValues()); |
| 471 } |
| 472 |
412 void Canvas::DrawStringInt(const base::string16& text, | 473 void Canvas::DrawStringInt(const base::string16& text, |
413 const gfx::Font& font, | 474 const gfx::Font& font, |
414 SkColor color, | 475 SkColor color, |
415 int x, int y, int w, int h) { | 476 int x, |
| 477 int y, |
| 478 int w, |
| 479 int h) { |
416 DrawStringInt(text, font, color, x, y, w, h, DefaultCanvasTextAlignment()); | 480 DrawStringInt(text, font, color, x, y, w, h, DefaultCanvasTextAlignment()); |
417 } | 481 } |
418 | 482 |
419 void Canvas::DrawStringInt(const base::string16& text, | 483 void Canvas::DrawStringInt(const base::string16& text, |
420 const gfx::Font& font, | 484 const gfx::Font& font, |
421 SkColor color, | 485 SkColor color, |
422 const gfx::Rect& display_rect) { | 486 const gfx::Rect& display_rect) { |
423 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), | 487 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), |
424 display_rect.width(), display_rect.height()); | 488 display_rect.width(), display_rect.height()); |
425 } | 489 } |
426 | 490 |
427 void Canvas::DrawStringInt(const base::string16& text, | 491 void Canvas::DrawStringInt(const base::string16& text, |
428 const gfx::Font& font, | 492 const gfx::Font& font, |
429 SkColor color, | 493 SkColor color, |
430 int x, int y, int w, int h, | 494 int x, |
| 495 int y, |
| 496 int w, |
| 497 int h, |
431 int flags) { | 498 int flags) { |
432 DrawStringWithShadows(text, | 499 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()); | 500 ShadowValues()); |
439 } | 501 } |
440 | 502 |
| 503 void Canvas::DrawStringWithShadows(const base::string16& text, |
| 504 const gfx::Font& font, |
| 505 SkColor color, |
| 506 const gfx::Rect& text_bounds, |
| 507 int line_height, |
| 508 int flags, |
| 509 const ShadowValues& shadows) { |
| 510 DrawStringWithShadows(text, gfx::FontList(font), color, text_bounds, |
| 511 line_height, flags, shadows); |
| 512 } |
| 513 |
441 void Canvas::TileImageInt(const gfx::ImageSkia& image, | 514 void Canvas::TileImageInt(const gfx::ImageSkia& image, |
442 int x, int y, int w, int h) { | 515 int x, |
| 516 int y, |
| 517 int w, |
| 518 int h) { |
443 TileImageInt(image, 0, 0, x, y, w, h); | 519 TileImageInt(image, 0, 0, x, y, w, h); |
444 } | 520 } |
445 | 521 |
446 void Canvas::TileImageInt(const gfx::ImageSkia& image, | 522 void Canvas::TileImageInt(const gfx::ImageSkia& image, |
447 int src_x, int src_y, | 523 int src_x, |
448 int dest_x, int dest_y, int w, int h) { | 524 int src_y, |
| 525 int dest_x, |
| 526 int dest_y, |
| 527 int w, |
| 528 int h) { |
449 TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h); | 529 TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h); |
450 } | 530 } |
451 | 531 |
452 void Canvas::TileImageInt(const gfx::ImageSkia& image, | 532 void Canvas::TileImageInt(const gfx::ImageSkia& image, |
453 int src_x, int src_y, | 533 int src_x, |
454 float tile_scale_x, float tile_scale_y, | 534 int src_y, |
455 int dest_x, int dest_y, int w, int h) { | 535 float tile_scale_x, |
| 536 float tile_scale_y, |
| 537 int dest_x, |
| 538 int dest_y, |
| 539 int w, |
| 540 int h) { |
456 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) | 541 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) |
457 return; | 542 return; |
458 | 543 |
459 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image, | 544 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image, |
460 tile_scale_x, tile_scale_y); | 545 tile_scale_x, tile_scale_y); |
461 if (image_rep.is_null()) | 546 if (image_rep.is_null()) |
462 return; | 547 return; |
463 | 548 |
464 SkMatrix shader_scale; | 549 SkMatrix shader_scale; |
465 shader_scale.setScale(SkFloatToScalar(tile_scale_x), | 550 shader_scale.setScale(SkFloatToScalar(tile_scale_x), |
(...skipping 22 matching lines...) Expand all Loading... |
488 } | 573 } |
489 | 574 |
490 void Canvas::EndPlatformPaint() { | 575 void Canvas::EndPlatformPaint() { |
491 skia::EndPlatformPaint(canvas_); | 576 skia::EndPlatformPaint(canvas_); |
492 } | 577 } |
493 | 578 |
494 void Canvas::Transform(const gfx::Transform& transform) { | 579 void Canvas::Transform(const gfx::Transform& transform) { |
495 canvas_->concat(transform.matrix()); | 580 canvas_->concat(transform.matrix()); |
496 } | 581 } |
497 | 582 |
| 583 void Canvas::DrawFadeTruncatingString( |
| 584 const base::string16& text, |
| 585 TruncateFadeMode truncate_mode, |
| 586 size_t desired_characters_to_truncate_from_head, |
| 587 const gfx::Font& font, |
| 588 SkColor color, |
| 589 const gfx::Rect& display_rect) { |
| 590 DrawFadeTruncatingString(text, truncate_mode, |
| 591 desired_characters_to_truncate_from_head, |
| 592 gfx::FontList(font), color, display_rect); |
| 593 } |
| 594 |
498 Canvas::Canvas(SkCanvas* canvas, ui::ScaleFactor scale_factor) | 595 Canvas::Canvas(SkCanvas* canvas, ui::ScaleFactor scale_factor) |
499 : scale_factor_(scale_factor), | 596 : scale_factor_(scale_factor), |
500 owned_canvas_(), | 597 owned_canvas_(), |
501 canvas_(canvas) { | 598 canvas_(canvas) { |
502 DCHECK(canvas); | 599 DCHECK(canvas); |
503 } | 600 } |
504 | 601 |
505 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) { | 602 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) { |
506 SkRect clip; | 603 SkRect clip; |
507 return canvas_->getClipBounds(&clip) && | 604 return canvas_->getClipBounds(&clip) && |
(...skipping 26 matching lines...) Expand all Loading... |
534 | 631 |
535 float bitmap_scale = image_rep.GetScale(); | 632 float bitmap_scale = image_rep.GetScale(); |
536 if (scale_x < bitmap_scale || scale_y < bitmap_scale) | 633 if (scale_x < bitmap_scale || scale_y < bitmap_scale) |
537 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); | 634 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); |
538 } | 635 } |
539 | 636 |
540 return image_rep; | 637 return image_rep; |
541 } | 638 } |
542 | 639 |
543 } // namespace gfx | 640 } // namespace gfx |
OLD | NEW |