| 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 #include <tuple> | 8 #include <tuple> |
| 9 | 9 |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 15 #include "third_party/skia/include/core/SkPaint.h" | 15 #include "third_party/skia/include/core/SkPaint.h" |
| 16 #include "third_party/skia/include/core/SkPath.h" | 16 #include "third_party/skia/include/core/SkPath.h" |
| 17 #include "third_party/skia/include/core/SkXfermode.h" | 17 #include "third_party/skia/include/core/SkXfermode.h" |
| 18 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 19 #include "ui/gfx/image/canvas_image_source.h" | 19 #include "ui/gfx/image/canvas_image_source.h" |
| 20 #include "ui/gfx/scoped_canvas.h" |
| 20 #include "ui/gfx/vector_icon_types.h" | 21 #include "ui/gfx/vector_icon_types.h" |
| 21 #include "ui/gfx/vector_icons.h" | 22 #include "ui/gfx/vector_icons.h" |
| 22 | 23 |
| 23 namespace gfx { | 24 namespace gfx { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 // Translates a string such as "MOVE_TO" into a command such as MOVE_TO. | 28 // Translates a string such as "MOVE_TO" into a command such as MOVE_TO. |
| 28 CommandType CommandFromString(const std::string& source) { | 29 CommandType CommandFromString(const std::string& source) { |
| 29 #define RETURN_IF_IS(command) \ | 30 #define RETURN_IF_IS(command) \ |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 else | 76 else |
| 76 path.push_back(PathElement(CommandFromString(piece))); | 77 path.push_back(PathElement(CommandFromString(piece))); |
| 77 } | 78 } |
| 78 return path; | 79 return path; |
| 79 } | 80 } |
| 80 | 81 |
| 81 void PaintPath(Canvas* canvas, | 82 void PaintPath(Canvas* canvas, |
| 82 const PathElement* path_elements, | 83 const PathElement* path_elements, |
| 83 size_t dip_size, | 84 size_t dip_size, |
| 84 SkColor color) { | 85 SkColor color) { |
| 85 canvas->Save(); | |
| 86 SkPath path; | 86 SkPath path; |
| 87 path.setFillType(SkPath::kEvenOdd_FillType); | 87 path.setFillType(SkPath::kEvenOdd_FillType); |
| 88 | 88 |
| 89 size_t canvas_size = kReferenceSizeDip; | 89 size_t canvas_size = kReferenceSizeDip; |
| 90 std::vector<SkPath> paths; | 90 std::vector<SkPath> paths; |
| 91 std::vector<SkPaint> paints; | 91 std::vector<SkPaint> paints; |
| 92 SkRect clip_rect = SkRect::MakeEmpty(); | 92 SkRect clip_rect = SkRect::MakeEmpty(); |
| 93 bool flips_in_rtl = false; | 93 bool flips_in_rtl = false; |
| 94 | 94 |
| 95 for (size_t i = 0; path_elements[i].type != END; i++) { | 95 for (size_t i = 0; path_elements[i].type != END; i++) { |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 flips_in_rtl = true; | 286 flips_in_rtl = true; |
| 287 break; | 287 break; |
| 288 } | 288 } |
| 289 | 289 |
| 290 case END: | 290 case END: |
| 291 NOTREACHED(); | 291 NOTREACHED(); |
| 292 break; | 292 break; |
| 293 } | 293 } |
| 294 } | 294 } |
| 295 | 295 |
| 296 if (flips_in_rtl && base::i18n::IsRTL()) { | 296 gfx::ScopedRTLFlipCanvas scoped_rtl_flip_canvas( |
| 297 canvas->Scale(-1, 1); | 297 canvas, static_cast<int>(canvas_size), flips_in_rtl); |
| 298 canvas->Translate(gfx::Vector2d(-static_cast<int>(canvas_size), 0)); | |
| 299 } | |
| 300 | 298 |
| 301 if (dip_size != canvas_size) { | 299 if (dip_size != canvas_size) { |
| 302 SkScalar scale = SkIntToScalar(dip_size) / SkIntToScalar(canvas_size); | 300 SkScalar scale = SkIntToScalar(dip_size) / SkIntToScalar(canvas_size); |
| 303 canvas->sk_canvas()->scale(scale, scale); | 301 canvas->sk_canvas()->scale(scale, scale); |
| 304 } | 302 } |
| 305 | 303 |
| 306 if (!clip_rect.isEmpty()) | 304 if (!clip_rect.isEmpty()) |
| 307 canvas->sk_canvas()->clipRect(clip_rect); | 305 canvas->sk_canvas()->clipRect(clip_rect); |
| 308 | 306 |
| 309 DCHECK_EQ(paints.size(), paths.size()); | 307 DCHECK_EQ(paints.size(), paths.size()); |
| 310 for (size_t i = 0; i < paths.size(); ++i) | 308 for (size_t i = 0; i < paths.size(); ++i) |
| 311 canvas->DrawPath(paths[i], paints[i]); | 309 canvas->DrawPath(paths[i], paints[i]); |
| 312 canvas->Restore(); | |
| 313 } | 310 } |
| 314 | 311 |
| 315 class VectorIconSource : public CanvasImageSource { | 312 class VectorIconSource : public CanvasImageSource { |
| 316 public: | 313 public: |
| 317 VectorIconSource(VectorIconId id, | 314 VectorIconSource(VectorIconId id, |
| 318 size_t dip_size, | 315 size_t dip_size, |
| 319 SkColor color, | 316 SkColor color, |
| 320 VectorIconId badge_id) | 317 VectorIconId badge_id) |
| 321 : CanvasImageSource( | 318 : CanvasImageSource( |
| 322 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)), | 319 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)), |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 | 447 |
| 451 ImageSkia CreateVectorIconFromSource(const std::string& source, | 448 ImageSkia CreateVectorIconFromSource(const std::string& source, |
| 452 size_t dip_size, | 449 size_t dip_size, |
| 453 SkColor color) { | 450 SkColor color) { |
| 454 return ImageSkia( | 451 return ImageSkia( |
| 455 new VectorIconSource(source, dip_size, color), | 452 new VectorIconSource(source, dip_size, color), |
| 456 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size))); | 453 gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size))); |
| 457 } | 454 } |
| 458 | 455 |
| 459 } // namespace gfx | 456 } // namespace gfx |
| OLD | NEW |