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"
12#include "SkMatrix.h"
13#include "SkPoint.h"
14#include "SkRect.h"
15#include "SkScalar.h"
16#include "SkString.h"
17#include "SkTDArray.h"
18#include "SkTypes.h"
19
20template <typename T>
21class SkSVGPrimitiveTypeWrapper {
22public:
23    SkSVGPrimitiveTypeWrapper() = default;
24    explicit constexpr SkSVGPrimitiveTypeWrapper(T v) : fValue(v) {}
25
26    SkSVGPrimitiveTypeWrapper(const SkSVGPrimitiveTypeWrapper&)            = default;
27    SkSVGPrimitiveTypeWrapper& operator=(const SkSVGPrimitiveTypeWrapper&) = default;
28    SkSVGPrimitiveTypeWrapper& operator=(const T& v) { fValue = v; return *this; }
29
30    bool operator==(const SkSVGPrimitiveTypeWrapper<T>& other) const {
31        return fValue == other.fValue;
32    }
33    bool operator!=(const SkSVGPrimitiveTypeWrapper<T>& other) const {
34        return !(*this == other);
35    }
36
37    const T& value() const { return fValue; }
38    operator const T&() const { return fValue; }
39
40private:
41    T fValue;
42};
43
44using SkSVGColorType      = SkSVGPrimitiveTypeWrapper<SkColor >;
45using SkSVGNumberType     = SkSVGPrimitiveTypeWrapper<SkScalar>;
46using SkSVGStringType     = SkSVGPrimitiveTypeWrapper<SkString>;
47using SkSVGViewBoxType    = SkSVGPrimitiveTypeWrapper<SkRect  >;
48using SkSVGTransformType  = SkSVGPrimitiveTypeWrapper<SkMatrix>;
49using SkSVGPointsType     = SkSVGPrimitiveTypeWrapper<SkTDArray<SkPoint>>;
50
51class SkSVGLength {
52public:
53    enum class Unit {
54        kUnknown,
55        kNumber,
56        kPercentage,
57        kEMS,
58        kEXS,
59        kPX,
60        kCM,
61        kMM,
62        kIN,
63        kPT,
64        kPC,
65    };
66
67    constexpr SkSVGLength()                    : fValue(0), fUnit(Unit::kUnknown) {}
68    explicit constexpr SkSVGLength(SkScalar v, Unit u = Unit::kNumber)
69        : fValue(v), fUnit(u) {}
70    SkSVGLength(const SkSVGLength&)            = default;
71    SkSVGLength& operator=(const SkSVGLength&) = default;
72
73    bool operator==(const SkSVGLength& other) const {
74        return fUnit == other.fUnit && fValue == other.fValue;
75    }
76    bool operator!=(const SkSVGLength& other) const { return !(*this == other); }
77
78    const SkScalar& value() const { return fValue; }
79    const Unit&     unit()  const { return fUnit;  }
80
81private:
82    SkScalar fValue;
83    Unit     fUnit;
84};
85
86class SkSVGPaint {
87public:
88    enum class Type {
89        kNone,
90        kCurrentColor,
91        kColor,
92        kInherit,
93        kIRI,
94    };
95
96    SkSVGPaint() : fType(Type::kInherit), fColor(SK_ColorBLACK) {}
97    explicit SkSVGPaint(Type t) : fType(t), fColor(SK_ColorBLACK) {}
98    explicit SkSVGPaint(const SkSVGColorType& c) : fType(Type::kColor), fColor(c) {}
99    explicit SkSVGPaint(const SkString& iri)
100        : fType(Type::kIRI), fColor(SK_ColorBLACK), fIRI(iri) {}
101
102    SkSVGPaint(const SkSVGPaint&)            = default;
103    SkSVGPaint& operator=(const SkSVGPaint&) = default;
104
105    bool operator==(const SkSVGPaint& other) const {
106        return fType == other.fType && fColor == other.fColor && fIRI == other.fIRI;
107    }
108    bool operator!=(const SkSVGPaint& other) const { return !(*this == other); }
109
110    Type type() const { return fType; }
111    const SkSVGColorType& color() const { SkASSERT(fType == Type::kColor); return fColor; }
112    const SkString& iri() const { SkASSERT(fType == Type::kIRI); return fIRI; }
113
114private:
115    Type fType;
116
117    // Logical union.
118    SkSVGColorType fColor;
119    SkString       fIRI;
120};
121
122class SkSVGClip {
123public:
124    enum class Type {
125        kNone,
126        kInherit,
127        kIRI,
128    };
129
130    SkSVGClip() : fType(Type::kNone) {}
131    explicit SkSVGClip(Type t) : fType(t)           {}
132    explicit SkSVGClip(const SkString& iri) : fType(Type::kIRI), fIRI(iri) {}
133
134    SkSVGClip(const SkSVGClip&)            = default;
135    SkSVGClip& operator=(const SkSVGClip&) = default;
136
137    bool operator==(const SkSVGClip& other) const {
138        return fType == other.fType && fIRI == other.fIRI;
139    }
140    bool operator!=(const SkSVGClip& other) const { return !(*this == other); }
141
142    Type type() const { return fType; }
143    const SkString& iri() const { SkASSERT(fType == Type::kIRI); return fIRI; }
144
145private:
146    Type           fType;
147    SkString       fIRI;
148};
149
150class SkSVGLineCap {
151public:
152    enum class Type {
153        kButt,
154        kRound,
155        kSquare,
156        kInherit,
157    };
158
159    constexpr SkSVGLineCap() : fType(Type::kInherit) {}
160    constexpr explicit SkSVGLineCap(Type t) : fType(t) {}
161
162    SkSVGLineCap(const SkSVGLineCap&)            = default;
163    SkSVGLineCap& operator=(const SkSVGLineCap&) = default;
164
165    bool operator==(const SkSVGLineCap& other) const { return fType == other.fType; }
166    bool operator!=(const SkSVGLineCap& other) const { return !(*this == other); }
167
168    Type type() const { return fType; }
169
170private:
171    Type fType;
172};
173
174class SkSVGLineJoin {
175public:
176    enum class Type {
177        kMiter,
178        kRound,
179        kBevel,
180        kInherit,
181    };
182
183    constexpr SkSVGLineJoin() : fType(Type::kInherit) {}
184    constexpr explicit SkSVGLineJoin(Type t) : fType(t) {}
185
186    SkSVGLineJoin(const SkSVGLineJoin&)            = default;
187    SkSVGLineJoin& operator=(const SkSVGLineJoin&) = default;
188
189    bool operator==(const SkSVGLineJoin& other) const { return fType == other.fType; }
190    bool operator!=(const SkSVGLineJoin& other) const { return !(*this == other); }
191
192    Type type() const { return fType; }
193
194private:
195    Type fType;
196};
197
198class SkSVGSpreadMethod {
199public:
200    // These values must match Skia's SkShader::TileMode enum.
201    enum class Type {
202        kPad,       // kClamp_TileMode
203        kRepeat,    // kRepeat_TileMode
204        kReflect,   // kMirror_TileMode
205    };
206
207    constexpr SkSVGSpreadMethod() : fType(Type::kPad) {}
208    constexpr explicit SkSVGSpreadMethod(Type t) : fType(t) {}
209
210    SkSVGSpreadMethod(const SkSVGSpreadMethod&)            = default;
211    SkSVGSpreadMethod& operator=(const SkSVGSpreadMethod&) = default;
212
213    bool operator==(const SkSVGSpreadMethod& other) const { return fType == other.fType; }
214    bool operator!=(const SkSVGSpreadMethod& other) const { return !(*this == other); }
215
216    Type type() const { return fType; }
217
218private:
219    Type fType;
220};
221
222class SkSVGFillRule {
223public:
224    enum class Type {
225        kNonZero,
226        kEvenOdd,
227        kInherit,
228    };
229
230    constexpr SkSVGFillRule() : fType(Type::kInherit) {}
231    constexpr explicit SkSVGFillRule(Type t) : fType(t) {}
232
233    SkSVGFillRule(const SkSVGFillRule&)            = default;
234    SkSVGFillRule& operator=(const SkSVGFillRule&) = default;
235
236    bool operator==(const SkSVGFillRule& other) const { return fType == other.fType; }
237    bool operator!=(const SkSVGFillRule& other) const { return !(*this == other); }
238
239    Type type() const { return fType; }
240
241private:
242    Type fType;
243};
244
245#endif // SkSVGTypes_DEFINED
246