blob: 0878068982256f3205e6300df87607d9b4b56604 [file] [log] [blame]
fmalitabffc2562016-08-03 10:21:11 -07001/*
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"
fmalitac97796b2016-08-08 12:58:57 -070012#include "SkMatrix.h"
Florin Malita57a0edf2017-10-10 11:22:08 -040013#include "SkPath.h"
fmalita5b31f322016-08-12 12:15:33 -070014#include "SkPoint.h"
fmalita397a5172016-08-08 11:38:55 -070015#include "SkRect.h"
fmalitabffc2562016-08-03 10:21:11 -070016#include "SkScalar.h"
fmalita28d5b722016-09-12 17:06:47 -070017#include "SkString.h"
fmalita5b31f322016-08-12 12:15:33 -070018#include "SkTDArray.h"
fmalitabffc2562016-08-03 10:21:11 -070019#include "SkTypes.h"
20
fmalita397a5172016-08-08 11:38:55 -070021template <typename T>
22class SkSVGPrimitiveTypeWrapper {
fmalitabffc2562016-08-03 10:21:11 -070023public:
fmalita397a5172016-08-08 11:38:55 -070024 SkSVGPrimitiveTypeWrapper() = default;
25 explicit constexpr SkSVGPrimitiveTypeWrapper(T v) : fValue(v) {}
fmalitabffc2562016-08-03 10:21:11 -070026
fmalita397a5172016-08-08 11:38:55 -070027 SkSVGPrimitiveTypeWrapper(const SkSVGPrimitiveTypeWrapper&) = default;
28 SkSVGPrimitiveTypeWrapper& operator=(const SkSVGPrimitiveTypeWrapper&) = default;
fmalita5b31f322016-08-12 12:15:33 -070029 SkSVGPrimitiveTypeWrapper& operator=(const T& v) { fValue = v; return *this; }
fmalitabffc2562016-08-03 10:21:11 -070030
fmalita2d961e02016-08-11 09:16:29 -070031 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
fmalita397a5172016-08-08 11:38:55 -070038 const T& value() const { return fValue; }
39 operator const T&() const { return fValue; }
fmalitabffc2562016-08-03 10:21:11 -070040
41private:
fmalita397a5172016-08-08 11:38:55 -070042 T fValue;
fmalitabffc2562016-08-03 10:21:11 -070043};
44
fmalitac97796b2016-08-08 12:58:57 -070045using SkSVGColorType = SkSVGPrimitiveTypeWrapper<SkColor >;
46using SkSVGNumberType = SkSVGPrimitiveTypeWrapper<SkScalar>;
fmalita28d5b722016-09-12 17:06:47 -070047using SkSVGStringType = SkSVGPrimitiveTypeWrapper<SkString>;
fmalitac97796b2016-08-08 12:58:57 -070048using SkSVGViewBoxType = SkSVGPrimitiveTypeWrapper<SkRect >;
49using SkSVGTransformType = SkSVGPrimitiveTypeWrapper<SkMatrix>;
fmalita5b31f322016-08-12 12:15:33 -070050using SkSVGPointsType = SkSVGPrimitiveTypeWrapper<SkTDArray<SkPoint>>;
fmalita397a5172016-08-08 11:38:55 -070051
fmalitabffc2562016-08-03 10:21:11 -070052class SkSVGLength {
53public:
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
fmalita2d961e02016-08-11 09:16:29 -070074 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
fmalitabffc2562016-08-03 10:21:11 -070079 const SkScalar& value() const { return fValue; }
80 const Unit& unit() const { return fUnit; }
81
82private:
83 SkScalar fValue;
84 Unit fUnit;
85};
86
fmalita2d961e02016-08-11 09:16:29 -070087class SkSVGPaint {
88public:
89 enum class Type {
90 kNone,
91 kCurrentColor,
92 kColor,
93 kInherit,
fmalita28d5b722016-09-12 17:06:47 -070094 kIRI,
fmalita2d961e02016-08-11 09:16:29 -070095 };
96
fmalita28d5b722016-09-12 17:06:47 -070097 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) {}
fmalita2d961e02016-08-11 09:16:29 -0700102
103 SkSVGPaint(const SkSVGPaint&) = default;
104 SkSVGPaint& operator=(const SkSVGPaint&) = default;
105
106 bool operator==(const SkSVGPaint& other) const {
fmalita28d5b722016-09-12 17:06:47 -0700107 return fType == other.fType && fColor == other.fColor && fIRI == other.fIRI;
fmalita2d961e02016-08-11 09:16:29 -0700108 }
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; }
fmalita28d5b722016-09-12 17:06:47 -0700113 const SkString& iri() const { SkASSERT(fType == Type::kIRI); return fIRI; }
fmalita2d961e02016-08-11 09:16:29 -0700114
115private:
116 Type fType;
117
fmalita28d5b722016-09-12 17:06:47 -0700118 // Logical union.
fmalita2d961e02016-08-11 09:16:29 -0700119 SkSVGColorType fColor;
fmalita28d5b722016-09-12 17:06:47 -0700120 SkString fIRI;
fmalita2d961e02016-08-11 09:16:29 -0700121};
122
Florin Malitace8840e2016-12-08 09:26:47 -0500123class SkSVGClip {
124public:
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
146private:
147 Type fType;
148 SkString fIRI;
149};
150
fmalita2d961e02016-08-11 09:16:29 -0700151class SkSVGLineCap {
152public:
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
171private:
172 Type fType;
173};
174
175class SkSVGLineJoin {
176public:
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
195private:
196 Type fType;
197};
198
fmalitacecd6172016-09-13 12:56:11 -0700199class SkSVGSpreadMethod {
200public:
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
219private:
220 Type fType;
221};
222
Florin Malitae932d4b2016-12-01 13:35:11 -0500223class SkSVGFillRule {
224public:
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 Malita57a0edf2017-10-10 11:22:08 -0400242 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 Malitae932d4b2016-12-01 13:35:11 -0500247private:
248 Type fType;
249};
250
Florin Malitaffe6ae42017-10-12 11:33:28 -0400251class SkSVGVisibility {
252public:
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
271private:
272 Type fType;
273};
274
Florin Malitaf543a602017-10-13 14:07:44 -0400275class SkSVGDashArray {
276public:
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
301private:
302 Type fType;
303 SkTDArray<SkSVGLength> fDashArray;
304};
305
fmalitabffc2562016-08-03 10:21:11 -0700306#endif // SkSVGTypes_DEFINED