Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
15#include "src/base/SkTLazy.h"
16
18public:
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) {
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) {
57 }
58
59 if (!strcmp(value, "inherit")) {
62 }
63
64 auto pr = parse<typename PropertyT::ValueT>(value);
65 if (pr.isValid()) {
66 PropertyT result(*pr);
68 }
69
71 }
72
73private:
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:
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
Definition SkColor.h:37
SkColor SkSVGColorType
Definition SkSVGTypes.h:25
int SkSVGIntegerType
Definition SkSVGTypes.h:26
int32_t SkUnichar
Definition SkTypes.h:175
SkNoncopyable()=default
bool parsePreserveAspectRatio(SkSVGPreserveAspectRatio *)
static ParseResult< PropertyT > parseProperty(const char *expectedName, const char *name, const char *value)
static ParseResult< T > parse(const char *expectedName, const char *name, const char *value)
bool parseInteger(SkSVGIntegerType *)
static ParseResult< T > parse(const char *value)
bool parseViewBox(SkSVGViewBoxType *)
bool parse(SkSVGIntegerType *v)
std::vector< SkString > Vars
Definition SkSVGTypes.h:184
T * set(const T &src)
Definition SkTLazy.h:56
float SkScalar
Definition extension.cpp:12
uint8_t value
GAsyncResult * result
const char * name
Definition fuchsia.cc:50
#define T
Definition SkMD5.cpp:120