Refactor the way enhanced bookmark gets large icons
[chromium-blink-merge.git] / ui / gfx / paint_vector_icon.cc
blob358f87260d785e147e60ad67b71685ca2ff8f332
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/paint_vector_icon.h"
7 #include "ui/gfx/canvas.h"
8 #include "ui/gfx/image/canvas_image_source.h"
9 #include "ui/gfx/vector_icon_types.h"
10 #include "ui/gfx/vector_icons2.h"
12 namespace gfx {
14 namespace {
16 class VectorIconSource : public CanvasImageSource {
17 public:
18 VectorIconSource(VectorIconId id, size_t dip_size, SkColor color)
19 : CanvasImageSource(
20 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)),
21 false),
22 id_(id),
23 color_(color) {}
25 ~VectorIconSource() override {}
27 // CanvasImageSource:
28 void Draw(gfx::Canvas* canvas) override {
29 PaintVectorIcon(canvas, id_, size_.width(), color_);
32 private:
33 const VectorIconId id_;
34 const SkColor color_;
36 DISALLOW_COPY_AND_ASSIGN(VectorIconSource);
39 } // namespace
41 void PaintVectorIcon(Canvas* canvas,
42 VectorIconId id,
43 size_t dip_size,
44 SkColor color) {
45 DCHECK(VectorIconId::VECTOR_ICON_NONE != id);
46 const PathElement* path_elements = GetPathForVectorIcon(id);
47 SkPath path;
48 path.setFillType(SkPath::kEvenOdd_FillType);
49 if (dip_size != kReferenceSizeDip) {
50 SkScalar scale = SkIntToScalar(dip_size) / SkIntToScalar(kReferenceSizeDip);
51 canvas->sk_canvas()->scale(scale, scale);
54 for (size_t i = 0; path_elements[i].type != END;) {
55 switch (path_elements[i++].type) {
56 case MOVE_TO: {
57 SkScalar x = path_elements[i++].arg;
58 SkScalar y = path_elements[i++].arg;
59 path.moveTo(x, y);
60 break;
63 case R_MOVE_TO: {
64 SkScalar x = path_elements[i++].arg;
65 SkScalar y = path_elements[i++].arg;
66 path.rMoveTo(x, y);
67 break;
70 case LINE_TO: {
71 SkScalar x = path_elements[i++].arg;
72 SkScalar y = path_elements[i++].arg;
73 path.lineTo(x, y);
74 break;
77 case R_LINE_TO: {
78 SkScalar x = path_elements[i++].arg;
79 SkScalar y = path_elements[i++].arg;
80 path.rLineTo(x, y);
81 break;
84 case H_LINE_TO: {
85 SkPoint last_point;
86 path.getLastPt(&last_point);
87 SkScalar x = path_elements[i++].arg;
88 path.lineTo(x, last_point.fY);
89 break;
92 case R_H_LINE_TO: {
93 SkScalar x = path_elements[i++].arg;
94 path.rLineTo(x, 0);
95 break;
98 case V_LINE_TO: {
99 SkPoint last_point;
100 path.getLastPt(&last_point);
101 SkScalar y = path_elements[i++].arg;
102 path.lineTo(last_point.fX, y);
103 break;
106 case R_V_LINE_TO: {
107 SkScalar y = path_elements[i++].arg;
108 path.rLineTo(0, y);
109 break;
112 case R_CUBIC_TO: {
113 SkScalar x1 = path_elements[i++].arg;
114 SkScalar y1 = path_elements[i++].arg;
115 SkScalar x2 = path_elements[i++].arg;
116 SkScalar y2 = path_elements[i++].arg;
117 SkScalar x3 = path_elements[i++].arg;
118 SkScalar y3 = path_elements[i++].arg;
119 path.rCubicTo(x1, y1, x2, y2, x3, y3);
120 break;
123 case CLOSE: {
124 path.close();
125 break;
128 case CIRCLE: {
129 SkScalar x = path_elements[i++].arg;
130 SkScalar y = path_elements[i++].arg;
131 SkScalar r = path_elements[i++].arg;
132 path.addCircle(x, y, r);
133 break;
136 case END:
137 NOTREACHED();
138 break;
142 SkPaint paint;
143 paint.setStyle(SkPaint::kFill_Style);
144 paint.setAntiAlias(true);
145 paint.setColor(color);
146 canvas->DrawPath(path, paint);
149 ImageSkia CreateVectorIcon(VectorIconId id, size_t dip_size, SkColor color) {
150 return ImageSkia(
151 new VectorIconSource(id, dip_size, color),
152 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)));
155 } // namespace gfx