• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/core/SkTLazy.h"
21 
22 using SkSVGColorType     = SkColor;
23 using SkSVGIntegerType   = int;
24 using SkSVGNumberType    = SkScalar;
25 using SkSVGStringType    = SkString;
26 using SkSVGViewBoxType   = SkRect;
27 using SkSVGTransformType = SkMatrix;
28 using SkSVGPointsType    = SkTDArray<SkPoint>;
29 
30 enum class SkSVGPropertyState {
31     kUnspecified,
32     kInherit,
33     kValue,
34 };
35 
36 // https://www.w3.org/TR/SVG11/intro.html#TermProperty
37 template <typename T, bool kInheritable> class SkSVGProperty {
38 public:
39     using ValueT = T;
40 
SkSVGProperty()41     SkSVGProperty() : fState(SkSVGPropertyState::kUnspecified) {}
42 
SkSVGProperty(SkSVGPropertyState state)43     explicit SkSVGProperty(SkSVGPropertyState state) : fState(state) {}
44 
SkSVGProperty(const T & value)45     explicit SkSVGProperty(const T& value) : fState(SkSVGPropertyState::kValue) {
46         fValue.set(value);
47     }
48 
SkSVGProperty(T && value)49     explicit SkSVGProperty(T&& value) : fState(SkSVGPropertyState::kValue) {
50         fValue.set(std::move(value));
51     }
52 
53     template <typename... Args>
init(Args &&...args)54     void init(Args&&... args) {
55         fState = SkSVGPropertyState::kValue;
56         fValue.init(std::forward<Args>(args)...);
57     }
58 
isInheritable()59     constexpr bool isInheritable() const { return kInheritable; }
60 
isValue()61     bool isValue() const { return fState == SkSVGPropertyState::kValue; }
62 
getMaybeNull()63     T* getMaybeNull() const {
64         return fValue.getMaybeNull();
65     }
66 
set(SkSVGPropertyState state)67     void set(SkSVGPropertyState state) {
68         fState = state;
69         if (fState != SkSVGPropertyState::kValue) {
70             fValue.reset();
71         }
72     }
73 
set(const T & value)74     void set(const T& value) {
75         fState = SkSVGPropertyState::kValue;
76         fValue.set(value);
77     }
78 
set(T && value)79     void set(T&& value) {
80         fState = SkSVGPropertyState::kValue;
81         fValue.set(std::move(value));
82     }
83 
84     T* operator->() {
85         SkASSERT(fState == SkSVGPropertyState::kValue);
86         SkASSERT(fValue.isValid());
87         return fValue.get();
88     }
89 
90     const T* operator->() const {
91         SkASSERT(fState == SkSVGPropertyState::kValue);
92         SkASSERT(fValue.isValid());
93         return fValue.get();
94     }
95 
96     T& operator*() {
97         SkASSERT(fState == SkSVGPropertyState::kValue);
98         SkASSERT(fValue.isValid());
99         return *fValue;
100     }
101 
102     const T& operator*() const {
103         SkASSERT(fState == SkSVGPropertyState::kValue);
104         SkASSERT(fValue.isValid());
105         return *fValue;
106     }
107 
108 private:
109     SkSVGPropertyState fState;
110     SkTLazy<T> fValue;
111 };
112 
113 class SkSVGLength {
114 public:
115     enum class Unit {
116         kUnknown,
117         kNumber,
118         kPercentage,
119         kEMS,
120         kEXS,
121         kPX,
122         kCM,
123         kMM,
124         kIN,
125         kPT,
126         kPC,
127     };
128 
SkSVGLength()129     constexpr SkSVGLength()                    : fValue(0), fUnit(Unit::kUnknown) {}
130     explicit constexpr SkSVGLength(SkScalar v, Unit u = Unit::kNumber)
fValue(v)131         : fValue(v), fUnit(u) {}
132     SkSVGLength(const SkSVGLength&)            = default;
133     SkSVGLength& operator=(const SkSVGLength&) = default;
134 
135     bool operator==(const SkSVGLength& other) const {
136         return fUnit == other.fUnit && fValue == other.fValue;
137     }
138     bool operator!=(const SkSVGLength& other) const { return !(*this == other); }
139 
value()140     const SkScalar& value() const { return fValue; }
unit()141     const Unit&     unit()  const { return fUnit;  }
142 
143 private:
144     SkScalar fValue;
145     Unit     fUnit;
146 };
147 
148 // https://www.w3.org/TR/SVG11/linking.html#IRIReference
149 class SkSVGIRI {
150 public:
151     enum class Type {
152         kLocal,
153         kNonlocal,
154         kDataURI,
155     };
156 
SkSVGIRI()157     SkSVGIRI() : fType(Type::kLocal) {}
SkSVGIRI(Type t,const SkSVGStringType & iri)158     SkSVGIRI(Type t, const SkSVGStringType& iri) : fType(t), fIRI(iri) {}
159 
type()160     Type type() const { return fType; }
iri()161     const SkSVGStringType& iri() const { return fIRI; }
162 
163     bool operator==(const SkSVGIRI& other) const {
164         return fType == other.fType && fIRI == other.fIRI;
165     }
166     bool operator!=(const SkSVGIRI& other) const { return !(*this == other); }
167 
168 private:
169     Type fType;
170     SkSVGStringType fIRI;
171 };
172 
173 // https://www.w3.org/TR/SVG11/types.html#InterfaceSVGColor
174 class SkSVGColor {
175 public:
176     enum class Type {
177         kCurrentColor,
178         kColor,
179         kICCColor,
180     };
181 
SkSVGColor()182     SkSVGColor() : fType(Type::kColor), fColor(SK_ColorBLACK) {}
SkSVGColor(Type t)183     explicit SkSVGColor(Type t) : fType(t), fColor(SK_ColorBLACK) {}
SkSVGColor(const SkSVGColorType & c)184     explicit SkSVGColor(const SkSVGColorType& c) : fType(Type::kColor), fColor(c) {}
185     bool operator==(const SkSVGColor& other) const {
186         return fType == other.fType && fColor == other.fColor;
187     }
188     bool operator!=(const SkSVGColor& other) const { return !(*this == other); }
189 
type()190     Type type() const { return fType; }
color()191     const SkSVGColorType& color() const { SkASSERT(fType == Type::kColor); return fColor; }
192 
193 private:
194     Type fType;
195     SkSVGColorType fColor;
196 };
197 
198 class SkSVGPaint {
199 public:
200     enum class Type {
201         kNone,
202         kColor,
203         kIRI,
204     };
205 
SkSVGPaint()206     SkSVGPaint() : fType(Type::kNone), fColor(SK_ColorBLACK) {}
SkSVGPaint(Type t)207     explicit SkSVGPaint(Type t) : fType(t), fColor(SK_ColorBLACK) {}
SkSVGPaint(const SkSVGColor & c)208     explicit SkSVGPaint(const SkSVGColor& c) : fType(Type::kColor), fColor(c) {}
SkSVGPaint(const SkSVGIRI & iri,const SkSVGColor & fallback_color)209     SkSVGPaint(const SkSVGIRI& iri, const SkSVGColor& fallback_color)
210         : fType(Type::kIRI), fColor(fallback_color), fIRI(iri) {}
211 
212     SkSVGPaint(const SkSVGPaint&)            = default;
213     SkSVGPaint& operator=(const SkSVGPaint&) = default;
214 
215     bool operator==(const SkSVGPaint& other) const {
216         return fType == other.fType && fColor == other.fColor && fIRI == other.fIRI;
217     }
218     bool operator!=(const SkSVGPaint& other) const { return !(*this == other); }
219 
type()220     Type type() const { return fType; }
color()221     const SkSVGColor& color() const {
222         SkASSERT(fType == Type::kColor || fType == Type::kIRI);
223         return fColor;
224     }
iri()225     const SkSVGIRI& iri() const { SkASSERT(fType == Type::kIRI); return fIRI; }
226 
227 private:
228     Type fType;
229 
230     // Logical union.
231     SkSVGColor fColor;
232     SkSVGIRI   fIRI;
233 };
234 
235 // <funciri> | none (used for clip/mask/filter properties)
236 class SkSVGFuncIRI {
237 public:
238     enum class Type {
239         kNone,
240         kIRI,
241     };
242 
SkSVGFuncIRI()243     SkSVGFuncIRI() : fType(Type::kNone) {}
SkSVGFuncIRI(Type t)244     explicit SkSVGFuncIRI(Type t) : fType(t) {}
SkSVGFuncIRI(SkSVGIRI && iri)245     explicit SkSVGFuncIRI(SkSVGIRI&& iri) : fType(Type::kIRI), fIRI(std::move(iri)) {}
246 
247     bool operator==(const SkSVGFuncIRI& other) const {
248         return fType == other.fType && fIRI == other.fIRI;
249     }
250     bool operator!=(const SkSVGFuncIRI& other) const { return !(*this == other); }
251 
type()252     Type type() const { return fType; }
iri()253     const SkSVGIRI& iri() const { SkASSERT(fType == Type::kIRI); return fIRI; }
254 
255 private:
256     Type           fType;
257     SkSVGIRI       fIRI;
258 };
259 
260 enum class SkSVGLineCap {
261     kButt,
262     kRound,
263     kSquare,
264 };
265 
266 class SkSVGLineJoin {
267 public:
268     enum class Type {
269         kMiter,
270         kRound,
271         kBevel,
272         kInherit,
273     };
274 
SkSVGLineJoin()275     constexpr SkSVGLineJoin() : fType(Type::kInherit) {}
SkSVGLineJoin(Type t)276     constexpr explicit SkSVGLineJoin(Type t) : fType(t) {}
277 
278     SkSVGLineJoin(const SkSVGLineJoin&)            = default;
279     SkSVGLineJoin& operator=(const SkSVGLineJoin&) = default;
280 
281     bool operator==(const SkSVGLineJoin& other) const { return fType == other.fType; }
282     bool operator!=(const SkSVGLineJoin& other) const { return !(*this == other); }
283 
type()284     Type type() const { return fType; }
285 
286 private:
287     Type fType;
288 };
289 
290 class SkSVGSpreadMethod {
291 public:
292     // These values must match Skia's SkShader::TileMode enum.
293     enum class Type {
294         kPad,       // kClamp_TileMode
295         kRepeat,    // kRepeat_TileMode
296         kReflect,   // kMirror_TileMode
297     };
298 
SkSVGSpreadMethod()299     constexpr SkSVGSpreadMethod() : fType(Type::kPad) {}
SkSVGSpreadMethod(Type t)300     constexpr explicit SkSVGSpreadMethod(Type t) : fType(t) {}
301 
302     SkSVGSpreadMethod(const SkSVGSpreadMethod&)            = default;
303     SkSVGSpreadMethod& operator=(const SkSVGSpreadMethod&) = default;
304 
305     bool operator==(const SkSVGSpreadMethod& other) const { return fType == other.fType; }
306     bool operator!=(const SkSVGSpreadMethod& other) const { return !(*this == other); }
307 
type()308     Type type() const { return fType; }
309 
310 private:
311     Type fType;
312 };
313 
314 class SkSVGFillRule {
315 public:
316     enum class Type {
317         kNonZero,
318         kEvenOdd,
319         kInherit,
320     };
321 
SkSVGFillRule()322     constexpr SkSVGFillRule() : fType(Type::kInherit) {}
SkSVGFillRule(Type t)323     constexpr explicit SkSVGFillRule(Type t) : fType(t) {}
324 
325     SkSVGFillRule(const SkSVGFillRule&)            = default;
326     SkSVGFillRule& operator=(const SkSVGFillRule&) = default;
327 
328     bool operator==(const SkSVGFillRule& other) const { return fType == other.fType; }
329     bool operator!=(const SkSVGFillRule& other) const { return !(*this == other); }
330 
type()331     Type type() const { return fType; }
332 
asFillType()333     SkPathFillType asFillType() const {
334         SkASSERT(fType != Type::kInherit); // should never be called for unresolved values.
335         return fType == Type::kEvenOdd ? SkPathFillType::kEvenOdd : SkPathFillType::kWinding;
336     }
337 
338 private:
339     Type fType;
340 };
341 
342 class SkSVGVisibility {
343 public:
344     enum class Type {
345         kVisible,
346         kHidden,
347         kCollapse,
348         kInherit,
349     };
350 
SkSVGVisibility()351     constexpr SkSVGVisibility() : fType(Type::kVisible) {}
SkSVGVisibility(Type t)352     constexpr explicit SkSVGVisibility(Type t) : fType(t) {}
353 
354     SkSVGVisibility(const SkSVGVisibility&)            = default;
355     SkSVGVisibility& operator=(const SkSVGVisibility&) = default;
356 
357     bool operator==(const SkSVGVisibility& other) const { return fType == other.fType; }
358     bool operator!=(const SkSVGVisibility& other) const { return !(*this == other); }
359 
type()360     Type type() const { return fType; }
361 
362 private:
363     Type fType;
364 };
365 
366 class SkSVGDashArray {
367 public:
368     enum class Type {
369         kNone,
370         kDashArray,
371         kInherit,
372     };
373 
SkSVGDashArray()374     SkSVGDashArray()                : fType(Type::kNone) {}
SkSVGDashArray(Type t)375     explicit SkSVGDashArray(Type t) : fType(t) {}
SkSVGDashArray(SkTDArray<SkSVGLength> && dashArray)376     explicit SkSVGDashArray(SkTDArray<SkSVGLength>&& dashArray)
377         : fType(Type::kDashArray)
378         , fDashArray(std::move(dashArray)) {}
379 
380     SkSVGDashArray(const SkSVGDashArray&)            = default;
381     SkSVGDashArray& operator=(const SkSVGDashArray&) = default;
382 
383     bool operator==(const SkSVGDashArray& other) const {
384         return fType == other.fType && fDashArray == other.fDashArray;
385     }
386     bool operator!=(const SkSVGDashArray& other) const { return !(*this == other); }
387 
type()388     Type type() const { return fType; }
389 
dashArray()390     const SkTDArray<SkSVGLength>& dashArray() const { return fDashArray; }
391 
392 private:
393     Type fType;
394     SkTDArray<SkSVGLength> fDashArray;
395 };
396 
397 class SkSVGStopColor {
398 public:
399     enum class Type {
400         kColor,
401         kCurrentColor,
402         kICCColor,
403         kInherit,
404     };
405 
SkSVGStopColor()406     SkSVGStopColor() : fType(Type::kColor), fColor(SK_ColorBLACK) {}
SkSVGStopColor(Type t)407     explicit SkSVGStopColor(Type t) : fType(t), fColor(SK_ColorBLACK) {}
SkSVGStopColor(const SkSVGColorType & c)408     explicit SkSVGStopColor(const SkSVGColorType& c) : fType(Type::kColor), fColor(c) {}
409 
410     SkSVGStopColor(const SkSVGStopColor&)            = default;
411     SkSVGStopColor& operator=(const SkSVGStopColor&) = default;
412 
413     bool operator==(const SkSVGStopColor& other) const {
414         return fType == other.fType && fColor == other.fColor;
415     }
416     bool operator!=(const SkSVGStopColor& other) const { return !(*this == other); }
417 
type()418     Type type() const { return fType; }
color()419     const SkSVGColorType& color() const { SkASSERT(fType == Type::kColor); return fColor; }
420 
421 private:
422     Type fType;
423     SkSVGColorType fColor;
424 };
425 
426 class SkSVGObjectBoundingBoxUnits {
427 public:
428     enum class Type {
429         kUserSpaceOnUse,
430         kObjectBoundingBox,
431     };
432 
SkSVGObjectBoundingBoxUnits()433     SkSVGObjectBoundingBoxUnits() : fType(Type::kUserSpaceOnUse) {}
SkSVGObjectBoundingBoxUnits(Type t)434     explicit SkSVGObjectBoundingBoxUnits(Type t) : fType(t) {}
435 
436     bool operator==(const SkSVGObjectBoundingBoxUnits& other) const {
437         return fType == other.fType;
438     }
439     bool operator!=(const SkSVGObjectBoundingBoxUnits& other) const {
440         return !(*this == other);
441     }
442 
type()443     Type type() const { return fType; }
444 
445 private:
446     Type fType;
447 };
448 
449 class SkSVGFontFamily {
450 public:
451     enum class Type {
452         kFamily,
453         kInherit,
454     };
455 
SkSVGFontFamily()456     SkSVGFontFamily() : fType(Type::kInherit) {}
SkSVGFontFamily(const char family[])457     explicit SkSVGFontFamily(const char family[])
458         : fType(Type::kFamily)
459         , fFamily(family) {}
460 
461     bool operator==(const SkSVGFontFamily& other) const {
462         return fType == other.fType && fFamily == other.fFamily;
463     }
464     bool operator!=(const SkSVGFontFamily& other) const { return !(*this == other); }
465 
type()466     Type type() const { return fType; }
467 
family()468     const SkString& family() const { return fFamily; }
469 
470 private:
471     Type     fType;
472     SkString fFamily;
473 };
474 
475 class SkSVGFontStyle {
476 public:
477     enum class Type {
478         kNormal,
479         kItalic,
480         kOblique,
481         kInherit,
482     };
483 
SkSVGFontStyle()484     SkSVGFontStyle() : fType(Type::kInherit) {}
SkSVGFontStyle(Type t)485     explicit SkSVGFontStyle(Type t) : fType(t) {}
486 
487     bool operator==(const SkSVGFontStyle& other) const {
488         return fType == other.fType;
489     }
490     bool operator!=(const SkSVGFontStyle& other) const { return !(*this == other); }
491 
type()492     Type type() const { return fType; }
493 
494 private:
495     Type fType;
496 };
497 
498 class SkSVGFontSize {
499 public:
500     enum class Type {
501         kLength,
502         kInherit,
503     };
504 
SkSVGFontSize()505     SkSVGFontSize() : fType(Type::kInherit), fSize(0) {}
SkSVGFontSize(const SkSVGLength & s)506     explicit SkSVGFontSize(const SkSVGLength& s)
507         : fType(Type::kLength)
508         , fSize(s) {}
509 
510     bool operator==(const SkSVGFontSize& other) const {
511         return fType == other.fType && fSize == other.fSize;
512     }
513     bool operator!=(const SkSVGFontSize& other) const { return !(*this == other); }
514 
type()515     Type type() const { return fType; }
516 
size()517     const SkSVGLength& size() const { return fSize; }
518 
519 private:
520     Type        fType;
521     SkSVGLength fSize;
522 };
523 
524 class SkSVGFontWeight {
525 public:
526     enum class Type {
527         k100,
528         k200,
529         k300,
530         k400,
531         k500,
532         k600,
533         k700,
534         k800,
535         k900,
536         kNormal,
537         kBold,
538         kBolder,
539         kLighter,
540         kInherit,
541     };
542 
SkSVGFontWeight()543     SkSVGFontWeight() : fType(Type::kInherit) {}
SkSVGFontWeight(Type t)544     explicit SkSVGFontWeight(Type t) : fType(t) {}
545 
546     bool operator==(const SkSVGFontWeight& other) const {
547         return fType == other.fType;
548     }
549     bool operator!=(const SkSVGFontWeight& other) const { return !(*this == other); }
550 
type()551     Type type() const { return fType; }
552 
553 private:
554     Type fType;
555 };
556 
557 struct SkSVGPreserveAspectRatio {
558     enum Align : uint8_t {
559         // These values are chosen such that bits [0,1] encode X alignment, and
560         // bits [2,3] encode Y alignment.
561         kXMinYMin = 0x00,
562         kXMidYMin = 0x01,
563         kXMaxYMin = 0x02,
564         kXMinYMid = 0x04,
565         kXMidYMid = 0x05,
566         kXMaxYMid = 0x06,
567         kXMinYMax = 0x08,
568         kXMidYMax = 0x09,
569         kXMaxYMax = 0x0a,
570 
571         kNone     = 0x10,
572     };
573 
574     enum Scale {
575         kMeet,
576         kSlice,
577     };
578 
579     Align fAlign = kXMidYMid;
580     Scale fScale = kMeet;
581 };
582 
583 class SkSVGTextAnchor {
584 public:
585     enum class Type {
586         kStart,
587         kMiddle,
588         kEnd,
589         kInherit,
590     };
591 
SkSVGTextAnchor()592     SkSVGTextAnchor() : fType(Type::kInherit) {}
SkSVGTextAnchor(Type t)593     explicit SkSVGTextAnchor(Type t) : fType(t) {}
594 
595     bool operator==(const SkSVGTextAnchor& other) const {
596         return fType == other.fType;
597     }
598     bool operator!=(const SkSVGTextAnchor& other) const { return !(*this == other); }
599 
type()600     Type type() const { return fType; }
601 
602 private:
603     Type fType;
604 };
605 
606 // https://www.w3.org/TR/SVG11/filters.html#FilterPrimitiveInAttribute
607 class SkSVGFeInputType {
608 public:
609     enum class Type {
610         kSourceGraphic,
611         kSourceAlpha,
612         kBackgroundImage,
613         kBackgroundAlpha,
614         kFillPaint,
615         kStrokePaint,
616         kFilterPrimitiveReference,
617         kUnspecified,
618     };
619 
SkSVGFeInputType()620     SkSVGFeInputType() : fType(Type::kUnspecified) {}
SkSVGFeInputType(Type t)621     explicit SkSVGFeInputType(Type t) : fType(t) {}
SkSVGFeInputType(const SkSVGStringType & id)622     explicit SkSVGFeInputType(const SkSVGStringType& id)
623             : fType(Type::kFilterPrimitiveReference), fId(id) {}
624 
625     bool operator==(const SkSVGFeInputType& other) const {
626         return fType == other.fType && fId == other.fId;
627     }
628     bool operator!=(const SkSVGFeInputType& other) const { return !(*this == other); }
629 
id()630     const SkString& id() const {
631         SkASSERT(fType == Type::kFilterPrimitiveReference);
632         return fId;
633     }
634 
type()635     Type type() const { return fType; }
636 
637 private:
638     Type fType;
639     SkString fId;
640 };
641 
642 enum class SkSVGFeColorMatrixType {
643     kMatrix,
644     kSaturate,
645     kHueRotate,
646     kLuminanceToAlpha,
647 };
648 
649 using SkSVGFeColorMatrixValues = SkTDArray<SkSVGNumberType>;
650 
651 enum class SkSVGFeCompositeOperator {
652     kOver,
653     kIn,
654     kOut,
655     kAtop,
656     kXor,
657     kArithmetic,
658 };
659 
660 class SkSVGFeTurbulenceBaseFrequency {
661 public:
SkSVGFeTurbulenceBaseFrequency()662     SkSVGFeTurbulenceBaseFrequency() : fFreqX(0), fFreqY(0) {}
SkSVGFeTurbulenceBaseFrequency(SkSVGNumberType freqX,SkSVGNumberType freqY)663     SkSVGFeTurbulenceBaseFrequency(SkSVGNumberType freqX, SkSVGNumberType freqY)
664             : fFreqX(freqX), fFreqY(freqY) {}
665 
freqX()666     SkSVGNumberType freqX() const { return fFreqX; }
freqY()667     SkSVGNumberType freqY() const { return fFreqY; }
668 
669 private:
670     SkSVGNumberType fFreqX;
671     SkSVGNumberType fFreqY;
672 };
673 
674 struct SkSVGFeTurbulenceType {
675     enum Type {
676         kFractalNoise,
677         kTurbulence,
678     };
679 
680     Type fType;
681 
SkSVGFeTurbulenceTypeSkSVGFeTurbulenceType682     SkSVGFeTurbulenceType() : fType(kTurbulence) {}
SkSVGFeTurbulenceTypeSkSVGFeTurbulenceType683     explicit SkSVGFeTurbulenceType(Type type) : fType(type) {}
684 };
685 
686 enum class SkSVGXmlSpace {
687     kDefault,
688     kPreserve,
689 };
690 
691 enum class SkSVGColorspace {
692     kAuto,
693     kSRGB,
694     kLinearRGB,
695 };
696 
697 // https://www.w3.org/TR/SVG11/painting.html#DisplayProperty
698 enum class SkSVGDisplay {
699     kInline,
700     kNone,
701 };
702 
703 #endif // SkSVGTypes_DEFINED
704