Debian Codegraph @

modules/svg/include/SkSVGTypes.h

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