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

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: compile? 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
« no previous file with comments | « ui/gfx/vector_icons.h ('k') | ui/resources/default_100_percent/common/checkmark.png » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 kReferenceSizeDip = 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 };
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 NOTREACHED();
86 return nullptr;
87 }
88
89 } // namespace
90
91 void PaintVectorIcon(Canvas* canvas,
92 VectorIconId id,
93 size_t dip_size,
94 SkColor color) {
95 DCHECK(VectorIconId::VECTOR_ICON_NONE != id);
96 const PathElement* path_elements = GetPathForVectorIcon(id);
97 SkPath path;
98 path.setFillType(SkPath::kEvenOdd_FillType);
99 if (dip_size != kReferenceSizeDip) {
100 SkScalar scale = SkIntToScalar(dip_size) / SkIntToScalar(kReferenceSizeDip);
101 canvas->sk_canvas()->scale(scale, scale);
102 }
103
104 for (size_t i = 0; path_elements[i].type != END;) {
105 switch (path_elements[i++].type) {
106 case MOVE_TO: {
107 SkScalar x = path_elements[i++].arg;
108 SkScalar y = path_elements[i++].arg;
109 path.moveTo(x, y);
110 break;
111 }
112
113 case R_MOVE_TO: {
114 SkScalar x = path_elements[i++].arg;
115 SkScalar y = path_elements[i++].arg;
116 path.rMoveTo(x, y);
117 break;
118 }
119
120 case LINE_TO: {
121 SkScalar x = path_elements[i++].arg;
122 SkScalar y = path_elements[i++].arg;
123 path.lineTo(x, y);
124 break;
125 }
126
127 case R_LINE_TO: {
128 SkScalar x = path_elements[i++].arg;
129 SkScalar y = path_elements[i++].arg;
130 path.rLineTo(x, y);
131 break;
132 }
133
134 case H_LINE_TO: {
135 SkPoint last_point;
136 path.getLastPt(&last_point);
137 SkScalar x = path_elements[i++].arg;
138 path.lineTo(x, last_point.fY);
139 break;
140 }
141
142 case R_H_LINE_TO: {
143 SkScalar x = path_elements[i++].arg;
144 path.rLineTo(x, 0);
145 break;
146 }
147
148 case V_LINE_TO: {
149 SkPoint last_point;
150 path.getLastPt(&last_point);
151 SkScalar y = path_elements[i++].arg;
152 path.lineTo(last_point.fX, y);
153 break;
154 }
155
156 case R_V_LINE_TO: {
157 SkScalar y = path_elements[i++].arg;
158 path.rLineTo(0, y);
159 break;
160 }
161
162 case R_CUBIC_TO: {
163 SkScalar x1 = path_elements[i++].arg;
164 SkScalar y1 = path_elements[i++].arg;
165 SkScalar x2 = path_elements[i++].arg;
166 SkScalar y2 = path_elements[i++].arg;
167 SkScalar x3 = path_elements[i++].arg;
168 SkScalar y3 = path_elements[i++].arg;
169 path.rCubicTo(x1, y1, x2, y2, x3, y3);
170 break;
171 }
172
173 case CLOSE: {
174 path.close();
175 break;
176 }
177
178 case CIRCLE: {
179 SkScalar x = path_elements[i++].arg;
180 SkScalar y = path_elements[i++].arg;
181 SkScalar r = path_elements[i++].arg;
182 path.addCircle(x, y, r);
183 break;
184 }
185
186 case END:
187 NOTREACHED();
188 break;
189 }
190 }
191
192 SkPaint paint;
193 paint.setStyle(SkPaint::kFill_Style);
194 paint.setAntiAlias(true);
195 paint.setColor(color);
196 canvas->DrawPath(path, paint);
197 }
198
199 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/vector_icons.h ('k') | ui/resources/default_100_percent/common/checkmark.png » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698