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 "include/core/SkColor.h" 12 #include "include/core/SkMatrix.h" 13 #include "include/core/SkPath.h" 14 #include "include/core/SkPoint.h" 15 #include "include/core/SkRect.h" 16 #include "include/core/SkScalar.h" 17 #include "include/core/SkString.h" 18 #include "include/core/SkTypes.h" 19 #include "include/private/SkTDArray.h" 20 21 template <typename T> 22 class SkSVGPrimitiveTypeWrapper { 23 public: 24 SkSVGPrimitiveTypeWrapper() = default; SkSVGPrimitiveTypeWrapper(T v)25 explicit constexpr SkSVGPrimitiveTypeWrapper(T v) : fValue(v) {} 26 27 SkSVGPrimitiveTypeWrapper(const SkSVGPrimitiveTypeWrapper&) = default; 28 SkSVGPrimitiveTypeWrapper& operator=(const SkSVGPrimitiveTypeWrapper&) = default; 29 SkSVGPrimitiveTypeWrapper& operator=(const T& v) { fValue = v; return *this; } 30 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 value()38 const T& value() const { return fValue; } 39 operator const T&() const { return fValue; } 40 41 private: 42 T fValue; 43 }; 44 45 using SkSVGColorType = SkSVGPrimitiveTypeWrapper<SkColor >; 46 using SkSVGNumberType = SkSVGPrimitiveTypeWrapper<SkScalar>; 47 using SkSVGStringType = SkSVGPrimitiveTypeWrapper<SkString>; 48 using SkSVGViewBoxType = SkSVGPrimitiveTypeWrapper<SkRect >; 49 using SkSVGTransformType = SkSVGPrimitiveTypeWrapper<SkMatrix>; 50 using SkSVGPointsType = SkSVGPrimitiveTypeWrapper<SkTDArray<SkPoint>>; 51 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 SkSVGLength()68 constexpr SkSVGLength() : fValue(0), fUnit(Unit::kUnknown) {} 69 explicit constexpr SkSVGLength(SkScalar v, Unit u = Unit::kNumber) fValue(v)70 : fValue(v), fUnit(u) {} 71 SkSVGLength(const SkSVGLength&) = default; 72 SkSVGLength& operator=(const SkSVGLength&) = default; 73 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 value()79 const SkScalar& value() const { return fValue; } unit()80 const Unit& unit() const { return fUnit; } 81 82 private: 83 SkScalar fValue; 84 Unit fUnit; 85 }; 86 87 class SkSVGPaint { 88 public: 89 enum class Type { 90 kNone, 91 kCurrentColor, 92 kColor, 93 kInherit, 94 kIRI, 95 }; 96 SkSVGPaint()97 SkSVGPaint() : fType(Type::kInherit), fColor(SK_ColorBLACK) {} SkSVGPaint(Type t)98 explicit SkSVGPaint(Type t) : fType(t), fColor(SK_ColorBLACK) {} SkSVGPaint(const SkSVGColorType & c)99 explicit SkSVGPaint(const SkSVGColorType& c) : fType(Type::kColor), fColor(c) {} SkSVGPaint(const SkString & iri)100 explicit SkSVGPaint(const SkString& iri) 101 : fType(Type::kIRI), fColor(SK_ColorBLACK), fIRI(iri) {} 102 103 SkSVGPaint(const SkSVGPaint&) = default; 104 SkSVGPaint& operator=(const SkSVGPaint&) = default; 105 106 bool operator==(const SkSVGPaint& other) const { 107 return fType == other.fType && fColor == other.fColor && fIRI == other.fIRI; 108 } 109 bool operator!=(const SkSVGPaint& other) const { return !(*this == other); } 110 type()111 Type type() const { return fType; } color()112 const SkSVGColorType& color() const { SkASSERT(fType == Type::kColor); return fColor; } iri()113 const SkString& iri() const { SkASSERT(fType == Type::kIRI); return fIRI; } 114 115 private: 116 Type fType; 117 118 // Logical union. 119 SkSVGColorType fColor; 120 SkString fIRI; 121 }; 122 123 class SkSVGClip { 124 public: 125 enum class Type { 126 kNone, 127 kInherit, 128 kIRI, 129 }; 130 SkSVGClip()131 SkSVGClip() : fType(Type::kNone) {} SkSVGClip(Type t)132 explicit SkSVGClip(Type t) : fType(t) {} SkSVGClip(const SkString & iri)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 type()143 Type type() const { return fType; } iri()144 const SkString& iri() const { SkASSERT(fType == Type::kIRI); return fIRI; } 145 146 private: 147 Type fType; 148 SkString fIRI; 149 }; 150 151 class SkSVGLineCap { 152 public: 153 enum class Type { 154 kButt, 155 kRound, 156 kSquare, 157 kInherit, 158 }; 159 SkSVGLineCap()160 constexpr SkSVGLineCap() : fType(Type::kInherit) {} SkSVGLineCap(Type t)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 type()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 SkSVGLineJoin()184 constexpr SkSVGLineJoin() : fType(Type::kInherit) {} SkSVGLineJoin(Type t)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 type()193 Type type() const { return fType; } 194 195 private: 196 Type fType; 197 }; 198 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 SkSVGSpreadMethod()208 constexpr SkSVGSpreadMethod() : fType(Type::kPad) {} SkSVGSpreadMethod(Type t)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 type()217 Type type() const { return fType; } 218 219 private: 220 Type fType; 221 }; 222 223 class SkSVGFillRule { 224 public: 225 enum class Type { 226 kNonZero, 227 kEvenOdd, 228 kInherit, 229 }; 230 SkSVGFillRule()231 constexpr SkSVGFillRule() : fType(Type::kInherit) {} SkSVGFillRule(Type t)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 type()240 Type type() const { return fType; } 241 asFillType()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 247 private: 248 Type fType; 249 }; 250 251 class SkSVGVisibility { 252 public: 253 enum class Type { 254 kVisible, 255 kHidden, 256 kCollapse, 257 kInherit, 258 }; 259 SkSVGVisibility()260 constexpr SkSVGVisibility() : fType(Type::kVisible) {} SkSVGVisibility(Type t)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 type()269 Type type() const { return fType; } 270 271 private: 272 Type fType; 273 }; 274 275 class SkSVGDashArray { 276 public: 277 enum class Type { 278 kNone, 279 kDashArray, 280 kInherit, 281 }; 282 SkSVGDashArray()283 SkSVGDashArray() : fType(Type::kNone) {} SkSVGDashArray(Type t)284 explicit SkSVGDashArray(Type t) : fType(t) {} SkSVGDashArray(SkTDArray<SkSVGLength> && dashArray)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 type()297 Type type() const { return fType; } 298 dashArray()299 const SkTDArray<SkSVGLength>& dashArray() const { return fDashArray; } 300 301 private: 302 Type fType; 303 SkTDArray<SkSVGLength> fDashArray; 304 }; 305 306 #endif // SkSVGTypes_DEFINED 307