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