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 { |
| 21 | kClip, |
| 22 | kColor, |
| 23 | kDashArray, |
| 24 | kFillRule, |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame^] | 25 | kFontFamily, |
| 26 | kFontSize, |
| 27 | kFontStyle, |
| 28 | kFontWeight, |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 29 | kLength, |
| 30 | kLineCap, |
| 31 | kLineJoin, |
| 32 | kNumber, |
| 33 | kPaint, |
| 34 | kPath, |
| 35 | kPoints, |
| 36 | kSpreadMethod, |
| 37 | kStopColor, |
| 38 | kString, |
| 39 | kTransform, |
| 40 | kViewBox, |
| 41 | kVisibility, |
| 42 | }; |
| 43 | |
| 44 | Type type() const { return fType; } |
| 45 | |
| 46 | template <typename T> |
| 47 | const T* as() const { |
| 48 | return fType == T::TYPE ? static_cast<const T*>(this) : nullptr; |
| 49 | } |
| 50 | |
| 51 | protected: |
| 52 | SkSVGValue(Type t) : fType(t) { } |
| 53 | |
| 54 | private: |
| 55 | Type fType; |
| 56 | |
| 57 | using INHERITED = SkNoncopyable; |
| 58 | }; |
| 59 | |
| 60 | template <typename T, SkSVGValue::Type ValueType> |
| 61 | class SkSVGWrapperValue final : public SkSVGValue { |
| 62 | public: |
| 63 | static constexpr Type TYPE = ValueType; |
| 64 | |
| 65 | explicit SkSVGWrapperValue(const T& v) |
| 66 | : INHERITED(ValueType) |
| 67 | , fWrappedValue(v) { } |
| 68 | |
| 69 | operator const T&() const { return fWrappedValue; } |
| 70 | const T* operator->() const { return &fWrappedValue; } |
| 71 | |
| 72 | private: |
| 73 | // Stack-only |
| 74 | void* operator new(size_t) = delete; |
| 75 | void* operator new(size_t, void*) = delete; |
| 76 | |
| 77 | const T& fWrappedValue; |
| 78 | |
| 79 | using INHERITED = SkSVGValue; |
| 80 | }; |
| 81 | |
| 82 | using SkSVGClipValue = SkSVGWrapperValue<SkSVGClip , SkSVGValue::Type::kClip >; |
| 83 | using SkSVGColorValue = SkSVGWrapperValue<SkSVGColorType , SkSVGValue::Type::kColor >; |
| 84 | using SkSVGFillRuleValue = SkSVGWrapperValue<SkSVGFillRule , SkSVGValue::Type::kFillRule >; |
| 85 | using SkSVGLengthValue = SkSVGWrapperValue<SkSVGLength , SkSVGValue::Type::kLength >; |
| 86 | using SkSVGPathValue = SkSVGWrapperValue<SkPath , SkSVGValue::Type::kPath >; |
| 87 | using SkSVGTransformValue = SkSVGWrapperValue<SkSVGTransformType, SkSVGValue::Type::kTransform >; |
| 88 | using SkSVGViewBoxValue = SkSVGWrapperValue<SkSVGViewBoxType , SkSVGValue::Type::kViewBox >; |
| 89 | using SkSVGPaintValue = SkSVGWrapperValue<SkSVGPaint , SkSVGValue::Type::kPaint >; |
| 90 | using SkSVGLineCapValue = SkSVGWrapperValue<SkSVGLineCap , SkSVGValue::Type::kLineCap >; |
| 91 | using SkSVGLineJoinValue = SkSVGWrapperValue<SkSVGLineJoin , SkSVGValue::Type::kLineJoin >; |
| 92 | using SkSVGNumberValue = SkSVGWrapperValue<SkSVGNumberType , SkSVGValue::Type::kNumber >; |
| 93 | using SkSVGPointsValue = SkSVGWrapperValue<SkSVGPointsType , SkSVGValue::Type::kPoints >; |
| 94 | using SkSVGStringValue = SkSVGWrapperValue<SkSVGStringType , SkSVGValue::Type::kString >; |
| 95 | using SkSVGSpreadMethodValue = SkSVGWrapperValue<SkSVGSpreadMethod , |
| 96 | SkSVGValue::Type::kSpreadMethod>; |
| 97 | using SkSVGStopColorValue = SkSVGWrapperValue<SkSVGStopColor , SkSVGValue::Type::kStopColor >; |
| 98 | using SkSVGVisibilityValue = SkSVGWrapperValue<SkSVGVisibility , SkSVGValue::Type::kVisibility>; |
| 99 | using SkSVGDashArrayValue = SkSVGWrapperValue<SkSVGDashArray , SkSVGValue::Type::kDashArray >; |
| 100 | |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame^] | 101 | using SkSVGFontFamilyValue = SkSVGWrapperValue<SkSVGFontFamily , SkSVGValue::Type::kFontFamily>; |
| 102 | using SkSVGFontSizeValue = SkSVGWrapperValue<SkSVGFontSize , SkSVGValue::Type::kFontSize >; |
| 103 | using SkSVGFontStyleValue = SkSVGWrapperValue<SkSVGFontStyle , SkSVGValue::Type::kFontStyle >; |
| 104 | using SkSVGFontWeightValue = SkSVGWrapperValue<SkSVGFontWeight , SkSVGValue::Type::kFontWeight>; |
| 105 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 106 | #endif // SkSVGValue_DEFINED |