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