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

Side by Side Diff: ui/gfx/vector_icons.cc

Issue 1214693005: Introduce some util code for drawing vector assets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix compile and rebase Created 5 years, 5 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
OLDNEW
(Empty)
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.
4
5 #include "ui/gfx/vector_icons.h"
6
7 #include "ui/gfx/canvas.h"
8
9 namespace gfx {
10
11 namespace {
12
13 const int kReferenceSizeDp = 48;
14
15 enum CommandType {
16 MOVE_TO,
17 R_MOVE_TO,
18 LINE_TO,
19 R_LINE_TO,
20 H_LINE_TO,
21 R_H_LINE_TO,
22 V_LINE_TO,
23 R_V_LINE_TO,
24 R_CUBIC_TO,
25 CIRCLE,
26 CLOSE,
27 END
28 };
29
30 struct PathElement {
31 PathElement(CommandType value) : type(value) {}
32 PathElement(SkScalar value) : arg(value) {}
33
34 union {
35 CommandType type;
36 SkScalar arg;
37 };
38 };
sadrul 2015/07/07 04:18:11 Would it make sense for PathElement to have a bool
Peter Kasting 2015/07/07 04:55:17 I think this adds too much boilerplate to the clas
39
40 const PathElement* GetPathForVectorIcon(VectorIconId id) {
41 switch (id) {
42 case VectorIconId::VECTOR_ICON_NONE:
43 NOTREACHED();
44 return nullptr;
45
46 case VectorIconId::CHECK_CIRCLE: {
47 static PathElement path[] = {
48 CIRCLE, 24, 24, 20,
49 MOVE_TO, 20, 34,
50 LINE_TO, 10, 24,
51 R_LINE_TO, 2.83f, -2.83f,
52 LINE_TO, 20, 28.34f,
53 R_LINE_TO, 15.17f, -15.17f,
54 LINE_TO, 38, 16,
55 LINE_TO, 20, 34,
56 END
57 };
58 return path;
59 }
60
61 case VectorIconId::PHOTO_CAMERA: {
62 static PathElement path[] = {
63 CIRCLE, 24, 24, 6.4f,
64 MOVE_TO, 18, 4,
65 R_LINE_TO, -3.66f, 4,
66 H_LINE_TO, 8,
67 R_CUBIC_TO, -2.21f, 0, -4, 1.79f, -4, 4,
68 R_V_LINE_TO, 24,
69 R_CUBIC_TO, 0, 2.21f, 1.79f, 4, 4, 4,
70 R_H_LINE_TO, 32,
71 R_CUBIC_TO, 2.21f, 0, 4, -1.79f, 4, -4,
72 V_LINE_TO, 12,
73 R_CUBIC_TO, 0, -2.21f, -1.79f, -4, -4, -4,
74 R_H_LINE_TO, -6.34f,
75 LINE_TO, 30, 4,
76 H_LINE_TO, 18,
77 CLOSE,
78 CIRCLE, 24, 24, 10,
79 END
80 };
81 return path;
82 }
83 }
84 }
85
86 } // namespace
87
88 void PaintVectorIcon(Canvas* canvas,
89 VectorIconId id,
90 size_t dp_size,
91 SkColor color) {
92 DCHECK(VectorIconId::VECTOR_ICON_NONE != id);
93 const PathElement* path_elements = GetPathForVectorIcon(id);
94 SkPath path;
95 path.setFillType(SkPath::kEvenOdd_FillType);
96 if (dp_size != kReferenceSizeDp) {
97 SkScalar scale = SkIntToScalar(dp_size) / SkIntToScalar(kReferenceSizeDp);
98 canvas->sk_canvas()->scale(scale, scale);
99 }
100
101 for (size_t i = 0; path_elements[i].type != END;) {
102 switch (path_elements[i++].type) {
103 case MOVE_TO: {
104 SkScalar x = path_elements[i++].arg;
105 SkScalar y = path_elements[i++].arg;
106 path.moveTo(x, y);
107 break;
108 }
109
110 case R_MOVE_TO: {
111 SkScalar x = path_elements[i++].arg;
112 SkScalar y = path_elements[i++].arg;
113 path.rMoveTo(x, y);
114 break;
115 }
116
117 case LINE_TO: {
118 SkScalar x = path_elements[i++].arg;
119 SkScalar y = path_elements[i++].arg;
120 path.lineTo(x, y);
121 break;
122 }
123
124 case R_LINE_TO: {
125 SkScalar x = path_elements[i++].arg;
126 SkScalar y = path_elements[i++].arg;
127 path.rLineTo(x, y);
128 break;
129 }
130
131 case H_LINE_TO: {
132 SkPoint last_point;
133 path.getLastPt(&last_point);
sadrul 2015/07/07 04:18:11 DCHECK that this returns true?
Evan Stade 2015/07/07 16:47:50 It needn't return true. You could start a path wit
134 SkScalar x = path_elements[i++].arg;
135 path.lineTo(x, last_point.fY);
136 break;
137 }
138
139 case R_H_LINE_TO: {
140 SkScalar x = path_elements[i++].arg;
141 path.rLineTo(x, 0);
142 break;
143 }
144
145 case V_LINE_TO: {
146 SkPoint last_point;
147 path.getLastPt(&last_point);
sadrul 2015/07/07 04:18:11 ditto
148 SkScalar y = path_elements[i++].arg;
149 path.lineTo(last_point.fX, y);
150 break;
151 }
152
153 case R_V_LINE_TO: {
154 SkScalar y = path_elements[i++].arg;
155 path.rLineTo(0, y);
156 break;
157 }
158
159 case R_CUBIC_TO: {
160 SkScalar x1 = path_elements[i++].arg;
161 SkScalar y1 = path_elements[i++].arg;
162 SkScalar x2 = path_elements[i++].arg;
163 SkScalar y2 = path_elements[i++].arg;
164 SkScalar x3 = path_elements[i++].arg;
165 SkScalar y3 = path_elements[i++].arg;
166 path.rCubicTo(x1, y1, x2, y2, x3, y3);
167 break;
168 }
169
170 case CLOSE: {
171 path.close();
172 break;
173 }
174
175 case CIRCLE: {
176 SkScalar x = path_elements[i++].arg;
177 SkScalar y = path_elements[i++].arg;
178 SkScalar r = path_elements[i++].arg;
179 path.addCircle(x, y, r);
180 break;
181 }
182
183 case END:
184 NOTREACHED();
185 break;
186 }
187 }
188
189 SkPaint paint;
190 paint.setStyle(SkPaint::kFill_Style);
191 paint.setAntiAlias(true);
192 paint.setColor(color);
193 canvas->DrawPath(path, paint);
194 }
195
196 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698