2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkSVGAttributeParser_DEFINED
9 #define SkSVGAttributeParser_DEFINED
13 #include "include/private/SkNoncopyable.h"
14 #include "modules/svg/include/SkSVGTypes.h"
15 #include "src/core/SkTLazy.h"
17 class SkSVGAttributeParser : public SkNoncopyable {
19 SkSVGAttributeParser(const char[]);
21 bool parseInteger(SkSVGIntegerType*);
22 bool parseViewBox(SkSVGViewBoxType*);
23 bool parsePreserveAspectRatio(SkSVGPreserveAspectRatio*);
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); }
29 template <typename T> using ParseResult = SkTLazy<T>;
31 template <typename T> static ParseResult<T> parse(const char* value) {
32 ParseResult<T> result;
34 if (SkSVGAttributeParser(value).parse(&parsedValue)) {
35 result.set(std::move(parsedValue));
41 static ParseResult<T> parse(const char* expectedName,
44 if (!strcmp(name, expectedName)) {
45 return parse<T>(value);
48 return ParseResult<T>();
51 template <typename PropertyT>
52 static ParseResult<PropertyT> parseProperty(const char* expectedName,
55 if (strcmp(name, expectedName) != 0) {
56 return ParseResult<PropertyT>();
59 if (!strcmp(value, "inherit")) {
60 PropertyT result(SkSVGPropertyState::kInherit);
61 return ParseResult<PropertyT>(&result);
64 auto pr = parse<typename PropertyT::ValueT>(value);
66 PropertyT result(*pr);
67 return ParseResult<PropertyT>(&result);
70 return ParseResult<PropertyT>();
76 explicit RestoreCurPos(SkSVGAttributeParser* self)
77 : fSelf(self), fCurPos(self->fCurPos) {}
81 fSelf->fCurPos = this->fCurPos;
85 void clear() { fSelf = nullptr; }
87 SkSVGAttributeParser* fSelf;
90 RestoreCurPos( const RestoreCurPos&) = delete;
91 RestoreCurPos& operator=(const RestoreCurPos&) = delete;
95 void* operator new(size_t) = delete;
96 void* operator new(size_t, void*) = delete;
101 template <typename F>
102 bool advanceWhile(F func);
104 bool matchStringToken(const char* token, const char** newPos = nullptr) const;
105 bool matchHexToken(const char** newPos) const;
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 parseColorComponentToken(int32_t*);
120 bool parseColorToken(SkColor*);
121 bool parseRGBColorToken(SkColor*);
122 bool parseSVGColor(SkSVGColor*, SkSVGColor::Vars&&);
123 bool parseSVGColorType(SkSVGColorType*);
124 bool parseFuncIRI(SkSVGFuncIRI*);
127 bool parseMatrixToken(SkMatrix*);
128 bool parseTranslateToken(SkMatrix*);
129 bool parseScaleToken(SkMatrix*);
130 bool parseRotateToken(SkMatrix*);
131 bool parseSkewXToken(SkMatrix*);
132 bool parseSkewYToken(SkMatrix*);
134 // Parses a sequence of 'WS* <prefix> WS* (<nested>)', where the nested sequence
135 // is handled by the passed functor.
136 template <typename Func, typename T>
137 bool parseParenthesized(const char* prefix, Func, T* result);
139 template <typename T>
140 bool parseList(std::vector<T>*);
142 template <typename T, typename TArray>
143 bool parseEnumMap(const TArray& arr, T* result) {
144 for (size_t i = 0; i < SK_ARRAY_COUNT(arr); ++i) {
145 if (this->parseExpectedStringToken(std::get<0>(arr[i]))) {
146 *result = std::get<1>(arr[i]);
153 // The current position in the input string.
157 using INHERITED = SkNoncopyable;
160 #endif // SkSVGAttributeParser_DEFINED