Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkSVGValue_DEFINED |
| 9 | #define SkSVGValue_DEFINED |
| 10 | |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkMatrix.h" |
| 13 | #include "include/core/SkPath.h" |
| 14 | #include "include/core/SkTypes.h" |
| 15 | #include "include/private/SkNoncopyable.h" |
| 16 | #include "modules/svg/include/SkSVGTypes.h" |
| 17 | |
| 18 | class SkSVGValue : public SkNoncopyable { |
| 19 | public: |
| 20 | enum class Type { |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 21 | kColor, |
Tyler Denniston | b3cafbc | 2020-10-30 15:00:48 -0400 | [diff] [blame] | 22 | kFilter, |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 23 | kLength, |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 24 | kNumber, |
Tyler Denniston | 30e327e | 2020-10-29 16:29:22 -0400 | [diff] [blame] | 25 | kObjectBoundingBoxUnits, |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 26 | kPreserveAspectRatio, |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 27 | kStopColor, |
| 28 | kString, |
| 29 | kTransform, |
| 30 | kViewBox, |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | Type type() const { return fType; } |
| 34 | |
| 35 | template <typename T> |
| 36 | const T* as() const { |
| 37 | return fType == T::TYPE ? static_cast<const T*>(this) : nullptr; |
| 38 | } |
| 39 | |
| 40 | protected: |
| 41 | SkSVGValue(Type t) : fType(t) { } |
| 42 | |
| 43 | private: |
| 44 | Type fType; |
| 45 | |
| 46 | using INHERITED = SkNoncopyable; |
| 47 | }; |
| 48 | |
| 49 | template <typename T, SkSVGValue::Type ValueType> |
| 50 | class SkSVGWrapperValue final : public SkSVGValue { |
| 51 | public: |
| 52 | static constexpr Type TYPE = ValueType; |
| 53 | |
| 54 | explicit SkSVGWrapperValue(const T& v) |
| 55 | : INHERITED(ValueType) |
| 56 | , fWrappedValue(v) { } |
| 57 | |
| 58 | operator const T&() const { return fWrappedValue; } |
| 59 | const T* operator->() const { return &fWrappedValue; } |
| 60 | |
| 61 | private: |
| 62 | // Stack-only |
| 63 | void* operator new(size_t) = delete; |
| 64 | void* operator new(size_t, void*) = delete; |
| 65 | |
| 66 | const T& fWrappedValue; |
| 67 | |
| 68 | using INHERITED = SkSVGValue; |
| 69 | }; |
| 70 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 71 | using SkSVGColorValue = SkSVGWrapperValue<SkSVGColorType , SkSVGValue::Type::kColor >; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 72 | using SkSVGLengthValue = SkSVGWrapperValue<SkSVGLength , SkSVGValue::Type::kLength >; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 73 | using SkSVGTransformValue = SkSVGWrapperValue<SkSVGTransformType, SkSVGValue::Type::kTransform >; |
| 74 | using SkSVGViewBoxValue = SkSVGWrapperValue<SkSVGViewBoxType , SkSVGValue::Type::kViewBox >; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 75 | using SkSVGNumberValue = SkSVGWrapperValue<SkSVGNumberType , SkSVGValue::Type::kNumber >; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 76 | using SkSVGStringValue = SkSVGWrapperValue<SkSVGStringType , SkSVGValue::Type::kString >; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 77 | using SkSVGStopColorValue = SkSVGWrapperValue<SkSVGStopColor , SkSVGValue::Type::kStopColor >; |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 78 | |
Tyler Denniston | 30e327e | 2020-10-29 16:29:22 -0400 | [diff] [blame] | 79 | using SkSVGPreserveAspectRatioValue = SkSVGWrapperValue<SkSVGPreserveAspectRatio, |
| 80 | SkSVGValue::Type::kPreserveAspectRatio>; |
| 81 | |
| 82 | using SkSVGObjectBoundingBoxUnitsValue = SkSVGWrapperValue<SkSVGObjectBoundingBoxUnits, |
| 83 | SkSVGValue::Type::kObjectBoundingBoxUnits>; |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 84 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 85 | #endif // SkSVGValue_DEFINED |