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

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: sky review 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 SkScalar reference_size = 48.0;
tfarina 2015/07/01 22:57:52 kReferenceSize?
Evan Stade 2015/07/02 00:08:05 Done.
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 type) { this->type = type; }
32 PathElement(SkScalar arg) { this->arg = arg; }
Peter Kasting 2015/07/01 22:26:57 Nit: explicit? Or does that break the code below?
Evan Stade 2015/07/02 00:08:06 breaks it
33
34 union {
35 CommandType type;
36 SkScalar arg;
37 };
38 };
39
40 const PathElement* GetPathForVectorIcon(VectorIconId id) {
41 switch (id) {
42 case VectorIconId::CHECK_CIRCLE: {
43 static PathElement path[] = {
44 CIRCLE, 24, 24, 20,
Peter Kasting 2015/07/01 22:26:57 I don't love inlining these instructions here. It
Evan Stade 2015/07/02 00:08:05 Can you elaborate on why that is better? Do you ju
Peter Kasting 2015/07/02 00:23:53 I want updating icons to not have to touch code, s
Evan Stade 2015/07/02 00:58:24 Currently image assets are separate from code, but
Peter Kasting 2015/07/02 08:06:11 Designers frequently have me point them to the app
Evan Stade 2015/07/06 18:35:59 You bring up a good point, which is how to visuali
45 MOVE_TO, 20, 34,
46 LINE_TO, 10, 24,
47 R_LINE_TO, 2.83, -2.83,
48 LINE_TO, 20, 28.34,
49 R_LINE_TO, 15.17, -15.17,
50 LINE_TO, 38, 16,
51 LINE_TO, 20, 34,
52 END
53 };
54 return path;
55 }
56
57 case VectorIconId::PHOTO_CAMERA: {
58 static PathElement path[] = {
59 CIRCLE, 24, 24, 6.4,
60 MOVE_TO, 18, 4,
61 R_LINE_TO, -3.66, 4,
62 H_LINE_TO, 8,
63 R_CUBIC_TO, -2.21, 0, -4, 1.79, -4, 4,
64 R_V_LINE_TO, 24,
65 R_CUBIC_TO, 0, 2.21, 1.79, 4, 4, 4,
66 R_H_LINE_TO, 32,
67 R_CUBIC_TO, 2.21, 0, 4, -1.79, 4, -4,
68 V_LINE_TO, 12,
69 R_CUBIC_TO, 0, -2.21, -1.79, -4, -4, -4,
70 R_H_LINE_TO, -6.34,
71 LINE_TO, 30, 4,
72 H_LINE_TO, 18,
73 CLOSE,
74 CIRCLE, 24, 24, 10,
75 END
76 };
77 return path;
78 }
79
80 case VectorIconId::VECTOR_ICON_NONE:
Peter Kasting 2015/07/01 22:26:58 Nit: Put in same order that the enum values were d
Evan Stade 2015/07/02 00:08:06 Done.
81 NOTREACHED();
82 return nullptr;
83 }
84 }
85
86 } // namespace
87
88 void PaintVectorIcon(Canvas* canvas, VectorIconId id, size_t dp_size,
89 SkColor color) {
Peter Kasting 2015/07/01 22:26:58 Nit: I think it's easier to read function definiti
Evan Stade 2015/07/02 00:08:06 Done.
90 if (id == VectorIconId::VECTOR_ICON_NONE) {
91 NOTREACHED();
Peter Kasting 2015/07/01 22:26:58 Don't handle DCHECK failure. This block should ju
Evan Stade 2015/07/02 00:08:05 Done.
92 return;
93 }
94
95 const PathElement* path_elements = GetPathForVectorIcon(id);
96 SkPath path;
97 path.setFillType(SkPath::kEvenOdd_FillType);
98 SkScalar scale = dp_size / reference_size;
Peter Kasting 2015/07/01 22:26:58 You compute this and then multiply every coordinat
Evan Stade 2015/07/02 00:08:06 good idea, done
99
100 for (size_t i = 0; path_elements[i].type != END;) {
101 switch (path_elements[i++].type) {
102 case CIRCLE: {
Peter Kasting 2015/07/01 22:26:57 Nit: Put in same order that the enum values were d
Evan Stade 2015/07/02 00:08:05 Done.
103 SkScalar x = scale * path_elements[i++].arg;
104 SkScalar y = scale * path_elements[i++].arg;
105 SkScalar r = scale * path_elements[i++].arg;
106 path.addCircle(x, y, r);
107 break;
108 }
109
110 case MOVE_TO: {
111 SkScalar x = scale * path_elements[i++].arg;
112 SkScalar y = scale * path_elements[i++].arg;
113 path.moveTo(x, y);
114 break;
115 }
116
117 case R_MOVE_TO: {
118 SkScalar x = scale * path_elements[i++].arg;
119 SkScalar y = scale * path_elements[i++].arg;
120 path.rMoveTo(x, y);
121 break;
122 }
123
124 case LINE_TO: {
125 SkScalar x = scale * path_elements[i++].arg;
126 SkScalar y = scale * path_elements[i++].arg;
127 path.lineTo(x, y);
128 break;
129 }
130
131 case R_LINE_TO: {
132 SkScalar x = scale * path_elements[i++].arg;
133 SkScalar y = scale * path_elements[i++].arg;
134 path.rLineTo(x, y);
135 break;
136 }
137
138 case H_LINE_TO: {
139 SkPoint last_point;
140 path.getLastPt(&last_point);
141 SkScalar x = scale * path_elements[i++].arg;
142 path.lineTo(x, last_point.fY);
143 break;
144 }
145
146 case V_LINE_TO: {
147 SkPoint last_point;
148 path.getLastPt(&last_point);
149 SkScalar y = scale * path_elements[i++].arg;
150 path.lineTo(last_point.fX, y);
151 break;
152 }
153
154 case R_H_LINE_TO: {
155 SkScalar x = scale * path_elements[i++].arg;
156 path.rLineTo(x, 0);
157 break;
158 }
159
160 case R_V_LINE_TO: {
161 SkScalar y = scale * path_elements[i++].arg;
162 path.rLineTo(0, y);
163 break;
164 }
165
166 case R_CUBIC_TO: {
167 SkScalar x1 = scale * path_elements[i++].arg;
168 SkScalar y1 = scale * path_elements[i++].arg;
169 SkScalar x2 = scale * path_elements[i++].arg;
170 SkScalar y2 = scale * path_elements[i++].arg;
171 SkScalar x3 = scale * path_elements[i++].arg;
172 SkScalar y3 = scale * path_elements[i++].arg;
173 path.rCubicTo(x1, y1, x2, y2, x3, y3);
174 break;
175 }
176
177 case CLOSE: {
178 path.close();
179 break;
180 }
181
182 case END:
183 NOTREACHED();
184 break;
185 }
186 }
187
188 SkPaint paint;
189 paint.setStyle(SkPaint::kFill_Style);
190 paint.setAntiAlias(true);
191 paint.setColor(color);
192 canvas->DrawPath(path, paint);
193 }
194
195 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698