Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 12 #include "third_party/skia/include/core/SkPath.h" | 12 #include "third_party/skia/include/core/SkPath.h" |
| 13 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/image/canvas_image_source.h" | 14 #include "ui/gfx/image/canvas_image_source.h" |
| 15 #include "ui/gfx/vector_icon_types.h" | 15 #include "ui/gfx/vector_icon_types.h" |
| 16 #include "ui/gfx/vector_icons2.h" | 16 #include "ui/gfx/vector_icons2.h" |
| 17 | 17 |
| 18 namespace gfx { | 18 namespace gfx { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // Translates a string such as "MOVE_TO" into a command such as MOVE_TO. | 22 // Translates a string such as "MOVE_TO" into a command such as MOVE_TO. |
| 23 CommandType CommandFromString(const std::string& source) { | 23 CommandType CommandFromString(const std::string& source) { |
| 24 #define RETURN_IF_IS(command) \ | 24 #define RETURN_IF_IS(command) \ |
| 25 if (source == #command) \ | 25 if (source == #command) \ |
| 26 return command; | 26 return command; |
| 27 | 27 |
| 28 RETURN_IF_IS(NEW_PATH); | 28 RETURN_IF_IS(NEW_PATH); |
|
sky
2015/08/05 00:06:01
Don't you need to update this?
Evan Stade
2015/08/05 19:04:25
only if I want it to keep working!
| |
| 29 RETURN_IF_IS(PATH_COLOR_ARGB); | 29 RETURN_IF_IS(PATH_COLOR_ARGB); |
| 30 RETURN_IF_IS(MOVE_TO); | 30 RETURN_IF_IS(MOVE_TO); |
| 31 RETURN_IF_IS(R_MOVE_TO); | 31 RETURN_IF_IS(R_MOVE_TO); |
| 32 RETURN_IF_IS(LINE_TO); | 32 RETURN_IF_IS(LINE_TO); |
| 33 RETURN_IF_IS(R_LINE_TO); | 33 RETURN_IF_IS(R_LINE_TO); |
| 34 RETURN_IF_IS(H_LINE_TO); | 34 RETURN_IF_IS(H_LINE_TO); |
| 35 RETURN_IF_IS(R_H_LINE_TO); | 35 RETURN_IF_IS(R_H_LINE_TO); |
| 36 RETURN_IF_IS(V_LINE_TO); | 36 RETURN_IF_IS(V_LINE_TO); |
| 37 RETURN_IF_IS(R_V_LINE_TO); | 37 RETURN_IF_IS(R_V_LINE_TO); |
| 38 RETURN_IF_IS(CUBIC_TO); | 38 RETURN_IF_IS(CUBIC_TO); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 | 70 |
| 71 std::vector<SkPath> paths; | 71 std::vector<SkPath> paths; |
| 72 paths.push_back(SkPath()); | 72 paths.push_back(SkPath()); |
| 73 paths.back().setFillType(SkPath::kEvenOdd_FillType); | 73 paths.back().setFillType(SkPath::kEvenOdd_FillType); |
| 74 size_t canvas_size = kReferenceSizeDip; | 74 size_t canvas_size = kReferenceSizeDip; |
| 75 | 75 |
| 76 std::vector<SkPaint> paints; | 76 std::vector<SkPaint> paints; |
| 77 paints.push_back(SkPaint()); | 77 paints.push_back(SkPaint()); |
| 78 paints.back().setColor(color); | 78 paints.back().setColor(color); |
| 79 | 79 |
| 80 SkRect clip_rect = SkRect::MakeEmpty(); | |
| 81 | |
| 80 for (size_t i = 0; path_elements[i].type != END; i++) { | 82 for (size_t i = 0; path_elements[i].type != END; i++) { |
| 81 SkPath& path = paths.back(); | 83 SkPath& path = paths.back(); |
| 84 SkPaint& paint = paints.back(); | |
| 82 switch (path_elements[i].type) { | 85 switch (path_elements[i].type) { |
| 83 case NEW_PATH: { | 86 case NEW_PATH: { |
| 84 paths.push_back(SkPath()); | 87 paths.push_back(SkPath()); |
| 85 paints.push_back(SkPaint()); | 88 paints.push_back(SkPaint()); |
| 86 paints.back().setColor(color); | 89 paints.back().setColor(color); |
| 87 break; | 90 break; |
| 88 } | 91 } |
| 89 | 92 |
| 90 case PATH_COLOR_ARGB: { | 93 case PATH_COLOR_ARGB: { |
| 91 int a = SkScalarFloorToInt(path_elements[++i].arg); | 94 int a = SkScalarFloorToInt(path_elements[++i].arg); |
| 92 int r = SkScalarFloorToInt(path_elements[++i].arg); | 95 int r = SkScalarFloorToInt(path_elements[++i].arg); |
| 93 int g = SkScalarFloorToInt(path_elements[++i].arg); | 96 int g = SkScalarFloorToInt(path_elements[++i].arg); |
| 94 int b = SkScalarFloorToInt(path_elements[++i].arg); | 97 int b = SkScalarFloorToInt(path_elements[++i].arg); |
| 95 paints.back().setColor(SkColorSetARGB(a, r, g, b)); | 98 paint.setColor(SkColorSetARGB(a, r, g, b)); |
| 96 break; | 99 break; |
| 97 } | 100 } |
| 98 | 101 |
| 102 case STROKE: { | |
| 103 paint.setStyle(SkPaint::kStroke_Style); | |
| 104 SkScalar width = path_elements[++i].arg; | |
| 105 paint.setStrokeWidth(width); | |
| 106 break; | |
| 107 } | |
| 108 | |
| 99 case MOVE_TO: { | 109 case MOVE_TO: { |
| 100 SkScalar x = path_elements[++i].arg; | 110 SkScalar x = path_elements[++i].arg; |
| 101 SkScalar y = path_elements[++i].arg; | 111 SkScalar y = path_elements[++i].arg; |
| 102 path.moveTo(x, y); | 112 path.moveTo(x, y); |
| 103 break; | 113 break; |
| 104 } | 114 } |
| 105 | 115 |
| 106 case R_MOVE_TO: { | 116 case R_MOVE_TO: { |
| 107 SkScalar x = path_elements[++i].arg; | 117 SkScalar x = path_elements[++i].arg; |
| 108 SkScalar y = path_elements[++i].arg; | 118 SkScalar y = path_elements[++i].arg; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 path.close(); | 196 path.close(); |
| 187 break; | 197 break; |
| 188 } | 198 } |
| 189 | 199 |
| 190 case CANVAS_DIMENSIONS: { | 200 case CANVAS_DIMENSIONS: { |
| 191 SkScalar width = path_elements[++i].arg; | 201 SkScalar width = path_elements[++i].arg; |
| 192 canvas_size = SkScalarTruncToInt(width); | 202 canvas_size = SkScalarTruncToInt(width); |
| 193 break; | 203 break; |
| 194 } | 204 } |
| 195 | 205 |
| 206 case CLIP: { | |
| 207 SkScalar x = path_elements[++i].arg; | |
| 208 SkScalar y = path_elements[++i].arg; | |
| 209 SkScalar w = path_elements[++i].arg; | |
| 210 SkScalar h = path_elements[++i].arg; | |
| 211 clip_rect = SkRect::MakeXYWH(x, y, w, h); | |
| 212 break; | |
| 213 } | |
| 214 | |
| 196 case END: | 215 case END: |
| 197 NOTREACHED(); | 216 NOTREACHED(); |
| 198 break; | 217 break; |
| 199 } | 218 } |
| 200 } | 219 } |
| 201 | 220 |
| 202 if (dip_size != canvas_size) { | 221 if (dip_size != canvas_size) { |
| 203 SkScalar scale = SkIntToScalar(dip_size) / SkIntToScalar(canvas_size); | 222 SkScalar scale = SkIntToScalar(dip_size) / SkIntToScalar(canvas_size); |
| 204 canvas->sk_canvas()->scale(scale, scale); | 223 canvas->sk_canvas()->scale(scale, scale); |
| 205 } | 224 } |
| 206 | 225 |
| 226 if (!clip_rect.isEmpty()) | |
| 227 canvas->sk_canvas()->clipRect(clip_rect); | |
| 228 | |
| 207 DCHECK_EQ(paints.size(), paths.size()); | 229 DCHECK_EQ(paints.size(), paths.size()); |
| 208 for (size_t i = 0; i < paths.size(); ++i) { | 230 for (size_t i = 0; i < paths.size(); ++i) { |
| 209 paints[i].setStyle(SkPaint::kFill_Style); | |
| 210 paints[i].setAntiAlias(true); | 231 paints[i].setAntiAlias(true); |
| 232 paints[i].setStrokeCap(SkPaint::kRound_Cap); | |
| 211 paths[i].setFillType(SkPath::kEvenOdd_FillType); | 233 paths[i].setFillType(SkPath::kEvenOdd_FillType); |
| 212 canvas->DrawPath(paths[i], paints[i]); | 234 canvas->DrawPath(paths[i], paints[i]); |
| 213 } | 235 } |
| 214 } | 236 } |
| 215 | 237 |
| 216 class VectorIconSource : public CanvasImageSource { | 238 class VectorIconSource : public CanvasImageSource { |
| 217 public: | 239 public: |
| 218 VectorIconSource(VectorIconId id, size_t dip_size, SkColor color) | 240 VectorIconSource(VectorIconId id, size_t dip_size, SkColor color) |
| 219 : CanvasImageSource( | 241 : CanvasImageSource( |
| 220 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)), | 242 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)), |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 static base::LazyInstance<VectorIconCache> g_icon_cache = | 319 static base::LazyInstance<VectorIconCache> g_icon_cache = |
| 298 LAZY_INSTANCE_INITIALIZER; | 320 LAZY_INSTANCE_INITIALIZER; |
| 299 | 321 |
| 300 } // namespace | 322 } // namespace |
| 301 | 323 |
| 302 void PaintVectorIcon(Canvas* canvas, | 324 void PaintVectorIcon(Canvas* canvas, |
| 303 VectorIconId id, | 325 VectorIconId id, |
| 304 size_t dip_size, | 326 size_t dip_size, |
| 305 SkColor color) { | 327 SkColor color) { |
| 306 DCHECK(VectorIconId::VECTOR_ICON_NONE != id); | 328 DCHECK(VectorIconId::VECTOR_ICON_NONE != id); |
| 307 PaintPath(canvas, GetPathForVectorIcon(id), dip_size, color); | 329 const PathElement* path = canvas->image_scale() == 1.f |
| 330 ? GetPathForVectorIconAt1xScale(id) | |
| 331 : GetPathForVectorIcon(id); | |
| 332 PaintPath(canvas, path, dip_size, color); | |
| 308 } | 333 } |
| 309 | 334 |
| 310 ImageSkia CreateVectorIcon(VectorIconId id, size_t dip_size, SkColor color) { | 335 ImageSkia CreateVectorIcon(VectorIconId id, size_t dip_size, SkColor color) { |
| 311 return g_icon_cache.Get().GetOrCreateIcon(id, dip_size, color); | 336 return g_icon_cache.Get().GetOrCreateIcon(id, dip_size, color); |
| 312 } | 337 } |
| 313 | 338 |
| 314 ImageSkia CreateVectorIconFromSource(const std::string& source, | 339 ImageSkia CreateVectorIconFromSource(const std::string& source, |
| 315 size_t dip_size, | 340 size_t dip_size, |
| 316 SkColor color) { | 341 SkColor color) { |
| 317 return ImageSkia( | 342 return ImageSkia( |
| 318 new VectorIconSource(source, dip_size, color), | 343 new VectorIconSource(source, dip_size, color), |
| 319 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size))); | 344 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size))); |
| 320 } | 345 } |
| 321 | 346 |
| 322 } // namespace gfx | 347 } // namespace gfx |
| OLD | NEW |