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

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

Powered by Google App Engine
This is Rietveld 408576698