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

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

Issue 1268633002: Make it easier to test changes to a .icon file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix win Created 5 years, 4 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/paint_vector_icon.h" 5 #include "ui/gfx/paint_vector_icon.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
10 #include "third_party/skia/include/core/SkPath.h" 12 #include "third_party/skia/include/core/SkPath.h"
11 #include "ui/gfx/canvas.h" 13 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/image/canvas_image_source.h" 14 #include "ui/gfx/image/canvas_image_source.h"
13 #include "ui/gfx/vector_icon_types.h" 15 #include "ui/gfx/vector_icon_types.h"
14 #include "ui/gfx/vector_icons2.h" 16 #include "ui/gfx/vector_icons2.h"
15 17
16 namespace gfx { 18 namespace gfx {
17 19
18 namespace { 20 namespace {
19 21
20 class VectorIconSource : public CanvasImageSource { 22 // Translates a string such as "MOVE_TO" into a command such as MOVE_TO.
21 public: 23 CommandType CommandFromString(const std::string& source) {
22 VectorIconSource(VectorIconId id, size_t dip_size, SkColor color) 24 #define RETURN_IF_IS(command) \
23 : CanvasImageSource( 25 if (source == #command) \
24 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)), 26 return command;
25 false),
26 id_(id),
27 color_(color) {}
28 27
29 ~VectorIconSource() override {} 28 RETURN_IF_IS(MOVE_TO);
29 RETURN_IF_IS(R_MOVE_TO);
30 RETURN_IF_IS(R_LINE_TO);
31 RETURN_IF_IS(H_LINE_TO);
32 RETURN_IF_IS(R_H_LINE_TO);
33 RETURN_IF_IS(V_LINE_TO);
34 RETURN_IF_IS(R_V_LINE_TO);
35 RETURN_IF_IS(CUBIC_TO);
36 RETURN_IF_IS(R_CUBIC_TO);
37 RETURN_IF_IS(CIRCLE);
38 RETURN_IF_IS(CLOSE);
39 RETURN_IF_IS(END);
40 #undef RETURN_IF_IS
30 41
31 // CanvasImageSource: 42 NOTREACHED();
32 void Draw(gfx::Canvas* canvas) override { 43 return CLOSE;
33 PaintVectorIcon(canvas, id_, size_.width(), color_); 44 }
45
46 std::vector<PathElement> PathFromSource(const std::string& source) {
47 std::vector<PathElement> path;
48 std::vector<std::string> pieces = base::SplitString(
49 source, "\n ,f", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
50 for (const auto& piece : pieces) {
51 double value;
52 if (base::StringToDouble(piece, &value))
53 path.push_back(PathElement(SkDoubleToScalar(value)));
54 else
55 path.push_back(PathElement(CommandFromString(piece)));
34 } 56 }
57 return path;
58 }
35 59
36 private: 60 void PaintPath(Canvas* canvas,
37 const VectorIconId id_; 61 const PathElement* path_elements,
38 const SkColor color_; 62 size_t dip_size,
39 63 SkColor color) {
40 DISALLOW_COPY_AND_ASSIGN(VectorIconSource); 64 SkPath path;
41 }; 65 path.setFillType(SkPath::kEvenOdd_FillType);
42 66 if (dip_size != kReferenceSizeDip) {
43 // This class caches vector icons (as ImageSkia) so they don't have to be drawn 67 SkScalar scale = SkIntToScalar(dip_size) / SkIntToScalar(kReferenceSizeDip);
44 // more than once. This also guarantees the backing data for the images returned 68 canvas->sk_canvas()->scale(scale, scale);
45 // by CreateVectorIcon will persist in memory until program termination.
46 class VectorIconCache {
47 public:
48 VectorIconCache() {}
49 ~VectorIconCache() {}
50
51 ImageSkia GetOrCreateIcon(VectorIconId id, size_t dip_size, SkColor color) {
52 IconDescription description(id, dip_size, color);
53 auto iter = images_.find(description);
54 if (iter != images_.end())
55 return iter->second;
56
57 ImageSkia icon(
58 new VectorIconSource(id, dip_size, color),
59 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)));
60 images_.insert(std::make_pair(description, icon));
61 return icon;
62 } 69 }
63 70
64 private:
65 struct IconDescription {
66 IconDescription(VectorIconId id, size_t dip_size, SkColor color)
67 : id(id), dip_size(dip_size), color(color) {}
68
69 bool operator<(const IconDescription& other) const {
70 if (id != other.id)
71 return id < other.id;
72 if (dip_size != other.dip_size)
73 return dip_size < other.dip_size;
74 return color < other.color;
75 }
76
77 VectorIconId id;
78 size_t dip_size;
79 SkColor color;
80 };
81
82 std::map<IconDescription, ImageSkia> images_;
83
84 DISALLOW_COPY_AND_ASSIGN(VectorIconCache);
85 };
86
87 static base::LazyInstance<VectorIconCache> g_icon_cache =
88 LAZY_INSTANCE_INITIALIZER;
89
90 } // namespace
91
92 void PaintVectorIcon(Canvas* canvas,
93 VectorIconId id,
94 size_t dip_size,
95 SkColor color) {
96 DCHECK(VectorIconId::VECTOR_ICON_NONE != id);
97 const PathElement* path_elements = GetPathForVectorIcon(id);
98
99 std::vector<SkPath> paths;
100 paths.push_back(SkPath());
101 paths.back().setFillType(SkPath::kEvenOdd_FillType);
102 size_t canvas_size = kReferenceSizeDip;
103
104 for (size_t i = 0; path_elements[i].type != END; i++) { 71 for (size_t i = 0; path_elements[i].type != END; i++) {
105 SkPath& path = paths.back(); 72 SkPath& path = paths.back();
106 switch (path_elements[i].type) { 73 switch (path_elements[i].type) {
107 case NEW_PATH: { 74 case NEW_PATH: {
108 paths.push_back(SkPath()); 75 paths.push_back(SkPath());
109 paths.back().setFillType(SkPath::kEvenOdd_FillType); 76 paths.back().setFillType(SkPath::kEvenOdd_FillType);
110 break; 77 break;
111 } 78 }
112 79
113 case MOVE_TO: { 80 case MOVE_TO: {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 187
221 if (dip_size != canvas_size) { 188 if (dip_size != canvas_size) {
222 SkScalar scale = SkIntToScalar(dip_size) / SkIntToScalar(canvas_size); 189 SkScalar scale = SkIntToScalar(dip_size) / SkIntToScalar(canvas_size);
223 canvas->sk_canvas()->scale(scale, scale); 190 canvas->sk_canvas()->scale(scale, scale);
224 } 191 }
225 192
226 for (const auto& path : paths) 193 for (const auto& path : paths)
227 canvas->DrawPath(path, paint); 194 canvas->DrawPath(path, paint);
228 } 195 }
229 196
197 class VectorIconSource : public CanvasImageSource {
198 public:
199 VectorIconSource(VectorIconId id, size_t dip_size, SkColor color)
200 : CanvasImageSource(
201 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)),
202 false),
203 id_(id),
204 color_(color) {}
205
206 VectorIconSource(const std::string& definition,
207 size_t dip_size,
208 SkColor color)
209 : CanvasImageSource(
210 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)),
211 false),
212 id_(VectorIconId::VECTOR_ICON_NONE),
213 path_(PathFromSource(definition)),
214 color_(color) {}
215
216 ~VectorIconSource() override {}
217
218 // CanvasImageSource:
219 void Draw(gfx::Canvas* canvas) override {
220 if (path_.empty())
221 PaintVectorIcon(canvas, id_, size_.width(), color_);
222 else
223 PaintPath(canvas, path_.data(), size_.width(), color_);
224 }
225
226 private:
227 const VectorIconId id_;
228 const std::vector<PathElement> path_;
229 const SkColor color_;
230
231 DISALLOW_COPY_AND_ASSIGN(VectorIconSource);
232 };
233
234 // This class caches vector icons (as ImageSkia) so they don't have to be drawn
235 // more than once. This also guarantees the backing data for the images returned
236 // by CreateVectorIcon will persist in memory until program termination.
237 class VectorIconCache {
238 public:
239 VectorIconCache() {}
240 ~VectorIconCache() {}
241
242 ImageSkia GetOrCreateIcon(VectorIconId id, size_t dip_size, SkColor color) {
243 IconDescription description(id, dip_size, color);
244 auto iter = images_.find(description);
245 if (iter != images_.end())
246 return iter->second;
247
248 ImageSkia icon(
249 new VectorIconSource(id, dip_size, color),
250 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)));
251 images_.insert(std::make_pair(description, icon));
252 return icon;
253 }
254
255 private:
256 struct IconDescription {
257 IconDescription(VectorIconId id, size_t dip_size, SkColor color)
258 : id(id), dip_size(dip_size), color(color) {}
259
260 bool operator<(const IconDescription& other) const {
261 if (id != other.id)
262 return id < other.id;
263 if (dip_size != other.dip_size)
264 return dip_size < other.dip_size;
265 return color < other.color;
266 }
267
268 VectorIconId id;
269 size_t dip_size;
270 SkColor color;
271 };
272
273 std::map<IconDescription, ImageSkia> images_;
274
275 DISALLOW_COPY_AND_ASSIGN(VectorIconCache);
276 };
277
278 static base::LazyInstance<VectorIconCache> g_icon_cache =
279 LAZY_INSTANCE_INITIALIZER;
280
281 } // namespace
282
283 void PaintVectorIcon(Canvas* canvas,
284 VectorIconId id,
285 size_t dip_size,
286 SkColor color) {
287 DCHECK(VectorIconId::VECTOR_ICON_NONE != id);
288 PaintPath(canvas, GetPathForVectorIcon(id), dip_size, color);
289 }
290
230 ImageSkia CreateVectorIcon(VectorIconId id, size_t dip_size, SkColor color) { 291 ImageSkia CreateVectorIcon(VectorIconId id, size_t dip_size, SkColor color) {
231 return g_icon_cache.Get().GetOrCreateIcon(id, dip_size, color); 292 return g_icon_cache.Get().GetOrCreateIcon(id, dip_size, color);
232 } 293 }
233 294
295 ImageSkia CreateVectorIconFromSource(const std::string& source,
296 size_t dip_size,
297 SkColor color) {
298 return ImageSkia(
299 new VectorIconSource(source, dip_size, color),
300 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)));
301 }
302
234 } // namespace gfx 303 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698