fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [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 SkSVGTypes_DEFINED |
| 9 | #define SkSVGTypes_DEFINED |
| 10 | |
| 11 | #include "SkColor.h" |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 12 | #include "SkMatrix.h" |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 13 | #include "SkPath.h" |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 14 | #include "SkPoint.h" |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 15 | #include "SkRect.h" |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 16 | #include "SkScalar.h" |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 17 | #include "SkString.h" |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 18 | #include "SkTDArray.h" |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 19 | #include "SkTypes.h" |
| 20 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 21 | template <typename T> |
| 22 | class SkSVGPrimitiveTypeWrapper { |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 23 | public: |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 24 | SkSVGPrimitiveTypeWrapper() = default; |
| 25 | explicit constexpr SkSVGPrimitiveTypeWrapper(T v) : fValue(v) {} |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 26 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 27 | SkSVGPrimitiveTypeWrapper(const SkSVGPrimitiveTypeWrapper&) = default; |
| 28 | SkSVGPrimitiveTypeWrapper& operator=(const SkSVGPrimitiveTypeWrapper&) = default; |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 29 | SkSVGPrimitiveTypeWrapper& operator=(const T& v) { fValue = v; return *this; } |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 30 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 31 | bool operator==(const SkSVGPrimitiveTypeWrapper<T>& other) const { |
| 32 | return fValue == other.fValue; |
| 33 | } |
| 34 | bool operator!=(const SkSVGPrimitiveTypeWrapper<T>& other) const { |
| 35 | return !(*this == other); |
| 36 | } |
| 37 | |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 38 | const T& value() const { return fValue; } |
| 39 | operator const T&() const { return fValue; } |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 40 | |
| 41 | private: |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 42 | T fValue; |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 45 | using SkSVGColorType = SkSVGPrimitiveTypeWrapper<SkColor >; |
| 46 | using SkSVGNumberType = SkSVGPrimitiveTypeWrapper<SkScalar>; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 47 | using SkSVGStringType = SkSVGPrimitiveTypeWrapper<SkString>; |
fmalita | c97796b | 2016-08-08 12:58:57 -0700 | [diff] [blame] | 48 | using SkSVGViewBoxType = SkSVGPrimitiveTypeWrapper<SkRect >; |
| 49 | using SkSVGTransformType = SkSVGPrimitiveTypeWrapper<SkMatrix>; |
fmalita | 5b31f32 | 2016-08-12 12:15:33 -0700 | [diff] [blame] | 50 | using SkSVGPointsType = SkSVGPrimitiveTypeWrapper<SkTDArray<SkPoint>>; |
fmalita | 397a517 | 2016-08-08 11:38:55 -0700 | [diff] [blame] | 51 | |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 52 | class SkSVGLength { |
| 53 | public: |
| 54 | enum class Unit { |
| 55 | kUnknown, |
| 56 | kNumber, |
| 57 | kPercentage, |
| 58 | kEMS, |
| 59 | kEXS, |
| 60 | kPX, |
| 61 | kCM, |
| 62 | kMM, |
| 63 | kIN, |
| 64 | kPT, |
| 65 | kPC, |
| 66 | }; |
| 67 | |
| 68 | constexpr SkSVGLength() : fValue(0), fUnit(Unit::kUnknown) {} |
| 69 | explicit constexpr SkSVGLength(SkScalar v, Unit u = Unit::kNumber) |
| 70 | : fValue(v), fUnit(u) {} |
| 71 | SkSVGLength(const SkSVGLength&) = default; |
| 72 | SkSVGLength& operator=(const SkSVGLength&) = default; |
| 73 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 74 | bool operator==(const SkSVGLength& other) const { |
| 75 | return fUnit == other.fUnit && fValue == other.fValue; |
| 76 | } |
| 77 | bool operator!=(const SkSVGLength& other) const { return !(*this == other); } |
| 78 | |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 79 | const SkScalar& value() const { return fValue; } |
| 80 | const Unit& unit() const { return fUnit; } |
| 81 | |
| 82 | private: |
| 83 | SkScalar fValue; |
| 84 | Unit fUnit; |
| 85 | }; |
| 86 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 87 | class SkSVGPaint { |
| 88 | public: |
| 89 | enum class Type { |
| 90 | kNone, |
| 91 | kCurrentColor, |
| 92 | kColor, |
| 93 | kInherit, |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 94 | kIRI, |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 97 | SkSVGPaint() : fType(Type::kInherit), fColor(SK_ColorBLACK) {} |
| 98 | explicit SkSVGPaint(Type t) : fType(t), fColor(SK_ColorBLACK) {} |
| 99 | explicit SkSVGPaint(const SkSVGColorType& c) : fType(Type::kColor), fColor(c) {} |
| 100 | explicit SkSVGPaint(const SkString& iri) |
| 101 | : fType(Type::kIRI), fColor(SK_ColorBLACK), fIRI(iri) {} |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 102 | |
| 103 | SkSVGPaint(const SkSVGPaint&) = default; |
| 104 | SkSVGPaint& operator=(const SkSVGPaint&) = default; |
| 105 | |
| 106 | bool operator==(const SkSVGPaint& other) const { |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 107 | return fType == other.fType && fColor == other.fColor && fIRI == other.fIRI; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 108 | } |
| 109 | bool operator!=(const SkSVGPaint& other) const { return !(*this == other); } |
| 110 | |
| 111 | Type type() const { return fType; } |
| 112 | const SkSVGColorType& color() const { SkASSERT(fType == Type::kColor); return fColor; } |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 113 | const SkString& iri() const { SkASSERT(fType == Type::kIRI); return fIRI; } |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 114 | |
| 115 | private: |
| 116 | Type fType; |
| 117 | |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 118 | // Logical union. |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 119 | SkSVGColorType fColor; |
fmalita | 28d5b72 | 2016-09-12 17:06:47 -0700 | [diff] [blame] | 120 | SkString fIRI; |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Florin Malita | ce8840e | 2016-12-08 09:26:47 -0500 | [diff] [blame] | 123 | class SkSVGClip { |
| 124 | public: |
| 125 | enum class Type { |
| 126 | kNone, |
| 127 | kInherit, |
| 128 | kIRI, |
| 129 | }; |
| 130 | |
| 131 | SkSVGClip() : fType(Type::kNone) {} |
| 132 | explicit SkSVGClip(Type t) : fType(t) {} |
| 133 | explicit SkSVGClip(const SkString& iri) : fType(Type::kIRI), fIRI(iri) {} |
| 134 | |
| 135 | SkSVGClip(const SkSVGClip&) = default; |
| 136 | SkSVGClip& operator=(const SkSVGClip&) = default; |
| 137 | |
| 138 | bool operator==(const SkSVGClip& other) const { |
| 139 | return fType == other.fType && fIRI == other.fIRI; |
| 140 | } |
| 141 | bool operator!=(const SkSVGClip& other) const { return !(*this == other); } |
| 142 | |
| 143 | Type type() const { return fType; } |
| 144 | const SkString& iri() const { SkASSERT(fType == Type::kIRI); return fIRI; } |
| 145 | |
| 146 | private: |
| 147 | Type fType; |
| 148 | SkString fIRI; |
| 149 | }; |
| 150 | |
fmalita | 2d961e0 | 2016-08-11 09:16:29 -0700 | [diff] [blame] | 151 | class SkSVGLineCap { |
| 152 | public: |
| 153 | enum class Type { |
| 154 | kButt, |
| 155 | kRound, |
| 156 | kSquare, |
| 157 | kInherit, |
| 158 | }; |
| 159 | |
| 160 | constexpr SkSVGLineCap() : fType(Type::kInherit) {} |
| 161 | constexpr explicit SkSVGLineCap(Type t) : fType(t) {} |
| 162 | |
| 163 | SkSVGLineCap(const SkSVGLineCap&) = default; |
| 164 | SkSVGLineCap& operator=(const SkSVGLineCap&) = default; |
| 165 | |
| 166 | bool operator==(const SkSVGLineCap& other) const { return fType == other.fType; } |
| 167 | bool operator!=(const SkSVGLineCap& other) const { return !(*this == other); } |
| 168 | |
| 169 | Type type() const { return fType; } |
| 170 | |
| 171 | private: |
| 172 | Type fType; |
| 173 | }; |
| 174 | |
| 175 | class SkSVGLineJoin { |
| 176 | public: |
| 177 | enum class Type { |
| 178 | kMiter, |
| 179 | kRound, |
| 180 | kBevel, |
| 181 | kInherit, |
| 182 | }; |
| 183 | |
| 184 | constexpr SkSVGLineJoin() : fType(Type::kInherit) {} |
| 185 | constexpr explicit SkSVGLineJoin(Type t) : fType(t) {} |
| 186 | |
| 187 | SkSVGLineJoin(const SkSVGLineJoin&) = default; |
| 188 | SkSVGLineJoin& operator=(const SkSVGLineJoin&) = default; |
| 189 | |
| 190 | bool operator==(const SkSVGLineJoin& other) const { return fType == other.fType; } |
| 191 | bool operator!=(const SkSVGLineJoin& other) const { return !(*this == other); } |
| 192 | |
| 193 | Type type() const { return fType; } |
| 194 | |
| 195 | private: |
| 196 | Type fType; |
| 197 | }; |
| 198 | |
fmalita | cecd617 | 2016-09-13 12:56:11 -0700 | [diff] [blame] | 199 | class SkSVGSpreadMethod { |
| 200 | public: |
| 201 | // These values must match Skia's SkShader::TileMode enum. |
| 202 | enum class Type { |
| 203 | kPad, // kClamp_TileMode |
| 204 | kRepeat, // kRepeat_TileMode |
| 205 | kReflect, // kMirror_TileMode |
| 206 | }; |
| 207 | |
| 208 | constexpr SkSVGSpreadMethod() : fType(Type::kPad) {} |
| 209 | constexpr explicit SkSVGSpreadMethod(Type t) : fType(t) {} |
| 210 | |
| 211 | SkSVGSpreadMethod(const SkSVGSpreadMethod&) = default; |
| 212 | SkSVGSpreadMethod& operator=(const SkSVGSpreadMethod&) = default; |
| 213 | |
| 214 | bool operator==(const SkSVGSpreadMethod& other) const { return fType == other.fType; } |
| 215 | bool operator!=(const SkSVGSpreadMethod& other) const { return !(*this == other); } |
| 216 | |
| 217 | Type type() const { return fType; } |
| 218 | |
| 219 | private: |
| 220 | Type fType; |
| 221 | }; |
| 222 | |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 223 | class SkSVGFillRule { |
| 224 | public: |
| 225 | enum class Type { |
| 226 | kNonZero, |
| 227 | kEvenOdd, |
| 228 | kInherit, |
| 229 | }; |
| 230 | |
| 231 | constexpr SkSVGFillRule() : fType(Type::kInherit) {} |
| 232 | constexpr explicit SkSVGFillRule(Type t) : fType(t) {} |
| 233 | |
| 234 | SkSVGFillRule(const SkSVGFillRule&) = default; |
| 235 | SkSVGFillRule& operator=(const SkSVGFillRule&) = default; |
| 236 | |
| 237 | bool operator==(const SkSVGFillRule& other) const { return fType == other.fType; } |
| 238 | bool operator!=(const SkSVGFillRule& other) const { return !(*this == other); } |
| 239 | |
| 240 | Type type() const { return fType; } |
| 241 | |
Florin Malita | 57a0edf | 2017-10-10 11:22:08 -0400 | [diff] [blame] | 242 | SkPath::FillType asFillType() const { |
| 243 | SkASSERT(fType != Type::kInherit); // should never be called for unresolved values. |
| 244 | return fType == Type::kEvenOdd ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType; |
| 245 | } |
| 246 | |
Florin Malita | e932d4b | 2016-12-01 13:35:11 -0500 | [diff] [blame] | 247 | private: |
| 248 | Type fType; |
| 249 | }; |
| 250 | |
Florin Malita | ffe6ae4 | 2017-10-12 11:33:28 -0400 | [diff] [blame] | 251 | class SkSVGVisibility { |
| 252 | public: |
| 253 | enum class Type { |
| 254 | kVisible, |
| 255 | kHidden, |
| 256 | kCollapse, |
| 257 | kInherit, |
| 258 | }; |
| 259 | |
| 260 | constexpr SkSVGVisibility() : fType(Type::kVisible) {} |
| 261 | constexpr explicit SkSVGVisibility(Type t) : fType(t) {} |
| 262 | |
| 263 | SkSVGVisibility(const SkSVGVisibility&) = default; |
| 264 | SkSVGVisibility& operator=(const SkSVGVisibility&) = default; |
| 265 | |
| 266 | bool operator==(const SkSVGVisibility& other) const { return fType == other.fType; } |
| 267 | bool operator!=(const SkSVGVisibility& other) const { return !(*this == other); } |
| 268 | |
| 269 | Type type() const { return fType; } |
| 270 | |
| 271 | private: |
| 272 | Type fType; |
| 273 | }; |
| 274 | |
Florin Malita | f543a60 | 2017-10-13 14:07:44 -0400 | [diff] [blame] | 275 | class SkSVGDashArray { |
| 276 | public: |
| 277 | enum class Type { |
| 278 | kNone, |
| 279 | kDashArray, |
| 280 | kInherit, |
| 281 | }; |
| 282 | |
| 283 | SkSVGDashArray() : fType(Type::kNone) {} |
| 284 | explicit SkSVGDashArray(Type t) : fType(t) {} |
| 285 | explicit SkSVGDashArray(SkTDArray<SkSVGLength>&& dashArray) |
| 286 | : fType(Type::kDashArray) |
| 287 | , fDashArray(std::move(dashArray)) {} |
| 288 | |
| 289 | SkSVGDashArray(const SkSVGDashArray&) = default; |
| 290 | SkSVGDashArray& operator=(const SkSVGDashArray&) = default; |
| 291 | |
| 292 | bool operator==(const SkSVGDashArray& other) const { |
| 293 | return fType == other.fType && fDashArray == other.fDashArray; |
| 294 | } |
| 295 | bool operator!=(const SkSVGDashArray& other) const { return !(*this == other); } |
| 296 | |
| 297 | Type type() const { return fType; } |
| 298 | |
| 299 | const SkTDArray<SkSVGLength>& dashArray() const { return fDashArray; } |
| 300 | |
| 301 | private: |
| 302 | Type fType; |
| 303 | SkTDArray<SkSVGLength> fDashArray; |
| 304 | }; |
| 305 | |
fmalita | bffc256 | 2016-08-03 10:21:11 -0700 | [diff] [blame] | 306 | #endif // SkSVGTypes_DEFINED |