Skia
2D Graphics Library
SkSVGAttributeParser.h
Go to the documentation of this file.
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 SkSVGAttributeParser_DEFINED
9 #define SkSVGAttributeParser_DEFINED
10 
11 #include <vector>
12 
13 #include "include/private/base/SkNoncopyable.h"
15 #include "src/base/SkTLazy.h"
16 
17 class SkSVGAttributeParser : public SkNoncopyable {
18 public:
19  SkSVGAttributeParser(const char[]);
20 
24 
25  // TODO: Migrate all parse*() functions to this style (and delete the old version)
26  // so they can be used by parse<T>():
27  bool parse(SkSVGIntegerType* v) { return parseInteger(v); }
28 
29  template <typename T> using ParseResult = SkTLazy<T>;
30 
31  template <typename T> static ParseResult<T> parse(const char* value) {
32  ParseResult<T> result;
33  T parsedValue;
34  if (SkSVGAttributeParser(value).parse(&parsedValue)) {
35  result.set(std::move(parsedValue));
36  }
37  return result;
38  }
39 
40  template <typename T>
41  static ParseResult<T> parse(const char* expectedName,
42  const char* name,
43  const char* value) {
44  if (!strcmp(name, expectedName)) {
45  return parse<T>(value);
46  }
47 
48  return ParseResult<T>();
49  }
50 
51  template <typename PropertyT>
52  static ParseResult<PropertyT> parseProperty(const char* expectedName,
53  const char* name,
54  const char* value) {
55  if (strcmp(name, expectedName) != 0) {
56  return ParseResult<PropertyT>();
57  }
58 
59  if (!strcmp(value, "inherit")) {
60  PropertyT result(SkSVGPropertyState::kInherit);
61  return ParseResult<PropertyT>(&result);
62  }
63 
64  auto pr = parse<typename PropertyT::ValueT>(value);
65  if (pr.isValid()) {
66  PropertyT result(*pr);
67  return ParseResult<PropertyT>(&result);
68  }
69 
70  return ParseResult<PropertyT>();
71  }
72 
73 private:
74  class RestoreCurPos {
75  public:
76  explicit RestoreCurPos(SkSVGAttributeParser* self)
77  : fSelf(self), fCurPos(self->fCurPos) {}
78 
79  ~RestoreCurPos() {
80  if (fSelf) {
81  fSelf->fCurPos = this->fCurPos;
82  }
83  }
84 
85  void clear() { fSelf = nullptr; }
86  private:
87  SkSVGAttributeParser* fSelf;
88  const char* fCurPos;
89 
90  RestoreCurPos( const RestoreCurPos&) = delete;
91  RestoreCurPos& operator=(const RestoreCurPos&) = delete;
92  };
93 
94  // Stack-only
95  void* operator new(size_t) = delete;
96  void* operator new(size_t, void*) = delete;
97 
98  template <typename T>
99  bool parse(T*);
100 
101  template <typename F>
102  bool advanceWhile(F func);
103 
104  bool matchStringToken(const char* token, const char** newPos = nullptr) const;
105  bool matchHexToken(const char** newPos) const;
106 
107  bool parseWSToken();
108  bool parseEOSToken();
109  bool parseSepToken();
110  bool parseCommaWspToken();
111  bool parseExpectedStringToken(const char*);
112  bool parseScalarToken(SkScalar*);
113  bool parseInt32Token(int32_t*);
114  bool parseEscape(SkUnichar*);
115  bool parseIdentToken(SkString*);
116  bool parseLengthUnitToken(SkSVGLength::Unit*);
117  bool parseNamedColorToken(SkColor*);
118  bool parseHexColorToken(SkColor*);
119  bool parseColorComponentScalarToken(int32_t*);
120  bool parseColorComponentIntegralToken(int32_t*);
121  bool parseColorComponentFractionalToken(int32_t*);
122  bool parseColorComponentToken(int32_t*);
123  bool parseColorToken(SkColor*);
124  bool parseRGBColorToken(SkColor*);
125  bool parseRGBAColorToken(SkColor*);
126  bool parseSVGColor(SkSVGColor*, SkSVGColor::Vars&&);
127  bool parseSVGColorType(SkSVGColorType*);
128  bool parseFuncIRI(SkSVGFuncIRI*);
129 
130  // Transform helpers
131  bool parseMatrixToken(SkMatrix*);
132  bool parseTranslateToken(SkMatrix*);
133  bool parseScaleToken(SkMatrix*);
134  bool parseRotateToken(SkMatrix*);
135  bool parseSkewXToken(SkMatrix*);
136  bool parseSkewYToken(SkMatrix*);
137 
138  // Parses a sequence of 'WS* <prefix> WS* (<nested>)', where the nested sequence
139  // is handled by the passed functor.
140  template <typename Func, typename T>
141  bool parseParenthesized(const char* prefix, Func, T* result);
142 
143  template <typename T>
144  bool parseList(std::vector<T>*);
145 
146  template <typename T, typename TArray>
147  bool parseEnumMap(const TArray& arr, T* result) {
148  for (size_t i = 0; i < std::size(arr); ++i) {
149  if (this->parseExpectedStringToken(std::get<0>(arr[i]))) {
150  *result = std::get<1>(arr[i]);
151  return true;
152  }
153  }
154  return false;
155  }
156 
157  // The current position in the input string.
158  const char* fCurPos;
159  const char* fEndPos;
160 
161  using INHERITED = SkNoncopyable;
162 };
163 
164 #endif // SkSVGAttributeParser_DEFINED
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:37
SkColor SkSVGColorType
Definition: SkSVGTypes.h:25
int SkSVGIntegerType
Definition: SkSVGTypes.h:26
float SkScalar
Definition: SkScalar.h:14
int32_t SkUnichar
32 bit integer to hold a unicode value
Definition: SkTypes.h:167
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
Definition: SkSVGAttributeParser.h:17
bool parsePreserveAspectRatio(SkSVGPreserveAspectRatio *)
static ParseResult< T > parse(const char *value)
Definition: SkSVGAttributeParser.h:31
SkTLazy< T > ParseResult
Definition: SkSVGAttributeParser.h:29
bool parseInteger(SkSVGIntegerType *)
bool parseViewBox(SkSVGViewBoxType *)
static ParseResult< PropertyT > parseProperty(const char *expectedName, const char *name, const char *value)
Definition: SkSVGAttributeParser.h:52
static ParseResult< T > parse(const char *expectedName, const char *name, const char *value)
Definition: SkSVGAttributeParser.h:41
SkSVGAttributeParser(const char[])
bool parse(SkSVGIntegerType *v)
Definition: SkSVGAttributeParser.h:27
Definition: SkSVGTypes.h:177
std::vector< SkString > Vars
Definition: SkSVGTypes.h:184
Definition: SkSVGTypes.h:264
Unit
Definition: SkSVGTypes.h:118
Light weight class for managing strings.
Definition: SkString.h:118
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582
Definition: SkSVGTypes.h:585