• 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 SkSVGValue_DEFINED
9 #define SkSVGValue_DEFINED
10 
11 #include "experimental/svg/model/SkSVGTypes.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkTypes.h"
16 #include "include/private/SkNoncopyable.h"
17 
18 class SkSVGValue : public SkNoncopyable {
19 public:
20     enum class Type {
21         kClip,
22         kColor,
23         kDashArray,
24         kFillRule,
25         kLength,
26         kLineCap,
27         kLineJoin,
28         kNumber,
29         kPaint,
30         kPath,
31         kPoints,
32         kSpreadMethod,
33         kString,
34         kTransform,
35         kViewBox,
36         kVisibility,
37     };
38 
type()39     Type type() const { return fType; }
40 
41     template <typename T>
as()42     const T* as() const {
43         return fType == T::TYPE ? static_cast<const T*>(this) : nullptr;
44     }
45 
46 protected:
SkSVGValue(Type t)47     SkSVGValue(Type t) : fType(t) { }
48 
49 private:
50     Type fType;
51 
52     typedef SkNoncopyable INHERITED;
53 };
54 
55 template <typename T, SkSVGValue::Type ValueType>
56 class SkSVGWrapperValue final : public SkSVGValue {
57 public:
58     static constexpr Type TYPE = ValueType;
59 
SkSVGWrapperValue(const T & v)60     explicit SkSVGWrapperValue(const T& v)
61         : INHERITED(ValueType)
62         , fWrappedValue(v) { }
63 
64     operator const T&() const { return fWrappedValue; }
65     const T* operator->() const { return &fWrappedValue; }
66 
67 private:
68     // Stack-only
69     void* operator new(size_t) = delete;
70     void* operator new(size_t, void*) = delete;
71 
72     const T& fWrappedValue;
73 
74     typedef SkSVGValue INHERITED;
75 };
76 
77 using SkSVGClipValue         = SkSVGWrapperValue<SkSVGClip         , SkSVGValue::Type::kClip      >;
78 using SkSVGColorValue        = SkSVGWrapperValue<SkSVGColorType    , SkSVGValue::Type::kColor     >;
79 using SkSVGFillRuleValue     = SkSVGWrapperValue<SkSVGFillRule     , SkSVGValue::Type::kFillRule  >;
80 using SkSVGLengthValue       = SkSVGWrapperValue<SkSVGLength       , SkSVGValue::Type::kLength    >;
81 using SkSVGPathValue         = SkSVGWrapperValue<SkPath            , SkSVGValue::Type::kPath      >;
82 using SkSVGTransformValue    = SkSVGWrapperValue<SkSVGTransformType, SkSVGValue::Type::kTransform >;
83 using SkSVGViewBoxValue      = SkSVGWrapperValue<SkSVGViewBoxType  , SkSVGValue::Type::kViewBox   >;
84 using SkSVGPaintValue        = SkSVGWrapperValue<SkSVGPaint        , SkSVGValue::Type::kPaint     >;
85 using SkSVGLineCapValue      = SkSVGWrapperValue<SkSVGLineCap      , SkSVGValue::Type::kLineCap   >;
86 using SkSVGLineJoinValue     = SkSVGWrapperValue<SkSVGLineJoin     , SkSVGValue::Type::kLineJoin  >;
87 using SkSVGNumberValue       = SkSVGWrapperValue<SkSVGNumberType   , SkSVGValue::Type::kNumber    >;
88 using SkSVGPointsValue       = SkSVGWrapperValue<SkSVGPointsType   , SkSVGValue::Type::kPoints    >;
89 using SkSVGStringValue       = SkSVGWrapperValue<SkSVGStringType   , SkSVGValue::Type::kString    >;
90 using SkSVGSpreadMethodValue = SkSVGWrapperValue<SkSVGSpreadMethod ,
91                                                  SkSVGValue::Type::kSpreadMethod>;
92 using SkSVGVisibilityValue   = SkSVGWrapperValue<SkSVGVisibility   , SkSVGValue::Type::kVisibility>;
93 using SkSVGDashArrayValue    = SkSVGWrapperValue<SkSVGDashArray    , SkSVGValue::Type::kDashArray >;
94 
95 #endif // SkSVGValue_DEFINED
96