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"
16 class VectorIconSource
: public CanvasImageSource
{
18 VectorIconSource(VectorIconId id
, size_t dip_size
, SkColor color
)
20 gfx::Size(static_cast<int>(dip_size
), static_cast<int>(dip_size
)),
25 ~VectorIconSource() override
{}
28 void Draw(gfx::Canvas
* canvas
) override
{
29 PaintVectorIcon(canvas
, id_
, size_
.width(), color_
);
33 const VectorIconId id_
;
36 DISALLOW_COPY_AND_ASSIGN(VectorIconSource
);
41 void PaintVectorIcon(Canvas
* canvas
,
45 DCHECK(VectorIconId::VECTOR_ICON_NONE
!= id
);
46 const PathElement
* path_elements
= GetPathForVectorIcon(id
);
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
) {
57 SkScalar x
= path_elements
[i
++].arg
;
58 SkScalar y
= path_elements
[i
++].arg
;
64 SkScalar x
= path_elements
[i
++].arg
;
65 SkScalar y
= path_elements
[i
++].arg
;
71 SkScalar x
= path_elements
[i
++].arg
;
72 SkScalar y
= path_elements
[i
++].arg
;
78 SkScalar x
= path_elements
[i
++].arg
;
79 SkScalar y
= path_elements
[i
++].arg
;
86 path
.getLastPt(&last_point
);
87 SkScalar x
= path_elements
[i
++].arg
;
88 path
.lineTo(x
, last_point
.fY
);
93 SkScalar x
= path_elements
[i
++].arg
;
100 path
.getLastPt(&last_point
);
101 SkScalar y
= path_elements
[i
++].arg
;
102 path
.lineTo(last_point
.fX
, y
);
107 SkScalar y
= path_elements
[i
++].arg
;
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
);
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
);
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
) {
151 new VectorIconSource(id
, dip_size
, color
),
152 gfx::Size(static_cast<int>(dip_size
), static_cast<int>(dip_size
)));