blob: e325f2e56b2f5a72595172805d652ba09aabc709 [file] [log] [blame]
Florin Malitab3418102020-10-15 18:10:29 -04001/*
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
Florin Malita2d059fc2021-01-05 11:53:15 -050011#include <vector>
12
Florin Malitab3418102020-10-15 18:10:29 -040013#include "include/private/SkNoncopyable.h"
14#include "modules/svg/include/SkSVGTypes.h"
Tyler Denniston57154992020-11-04 16:08:30 -050015#include "src/core/SkTLazy.h"
Florin Malitab3418102020-10-15 18:10:29 -040016
17class SkSVGAttributeParser : public SkNoncopyable {
18public:
19 SkSVGAttributeParser(const char[]);
20
Tyler Dennistondada9602020-11-03 10:04:25 -050021 bool parseInteger(SkSVGIntegerType*);
Florin Malitab3418102020-10-15 18:10:29 -040022 bool parseViewBox(SkSVGViewBoxType*);
Florin Malita385e7442020-10-21 16:55:46 -040023 bool parsePreserveAspectRatio(SkSVGPreserveAspectRatio*);
Florin Malitab3418102020-10-15 18:10:29 -040024
Tyler Denniston57154992020-11-04 16:08:30 -050025 // TODO: Migrate all parse*() functions to this style (and delete the old version)
26 // so they can be used by parse<T>():
Tyler Denniston57154992020-11-04 16:08:30 -050027 bool parse(SkSVGIntegerType* v) { return parseInteger(v); }
28
29 template <typename T> using ParseResult = SkTLazy<T>;
30
Tyler Dennistona0a51462020-11-10 13:13:28 -050031 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
Tyler Denniston57154992020-11-04 16:08:30 -050040 template <typename T>
41 static ParseResult<T> parse(const char* expectedName,
42 const char* name,
Florin Malita401321d2020-11-05 11:27:24 -050043 const char* value) {
Florin Malita401321d2020-11-05 11:27:24 -050044 if (!strcmp(name, expectedName)) {
Tyler Dennistona0a51462020-11-10 13:13:28 -050045 return parse<T>(value);
Tyler Denniston57154992020-11-04 16:08:30 -050046 }
47
Tyler Dennistona0a51462020-11-10 13:13:28 -050048 return ParseResult<T>();
Tyler Denniston57154992020-11-04 16:08:30 -050049 }
50
Tyler Denniston79832e32020-11-18 09:34:07 -050051 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 }
Tyler Dennistona0a51462020-11-10 13:13:28 -050072
Florin Malitab3418102020-10-15 18:10:29 -040073private:
74 // Stack-only
75 void* operator new(size_t) = delete;
76 void* operator new(size_t, void*) = delete;
77
Florin Malita401321d2020-11-05 11:27:24 -050078 template <typename T>
79 bool parse(T*);
80
Florin Malitab3418102020-10-15 18:10:29 -040081 template <typename F>
82 bool advanceWhile(F func);
83
Tyler Denniston209857c2021-01-27 14:22:19 -050084 bool matchStringToken(const char* token, const char** newPos = nullptr) const;
85
Florin Malitab3418102020-10-15 18:10:29 -040086 bool parseWSToken();
Florin Malita401321d2020-11-05 11:27:24 -050087 bool parseEOSToken();
Florin Malitab3418102020-10-15 18:10:29 -040088 bool parseSepToken();
Florin Malita401321d2020-11-05 11:27:24 -050089 bool parseCommaWspToken();
90 bool parseExpectedStringToken(const char*);
Florin Malitab3418102020-10-15 18:10:29 -040091 bool parseScalarToken(SkScalar*);
Tyler Dennistondada9602020-11-03 10:04:25 -050092 bool parseInt32Token(int32_t*);
Florin Malitab3418102020-10-15 18:10:29 -040093 bool parseHexToken(uint32_t*);
94 bool parseLengthUnitToken(SkSVGLength::Unit*);
95 bool parseNamedColorToken(SkColor*);
96 bool parseHexColorToken(SkColor*);
97 bool parseColorComponentToken(int32_t*);
98 bool parseRGBColorToken(SkColor*);
Tyler Dennistone71f5472021-01-27 13:30:59 -050099 bool parseFuncIRI(SkSVGFuncIRI*);
Florin Malitab3418102020-10-15 18:10:29 -0400100
101 // Transform helpers
102 bool parseMatrixToken(SkMatrix*);
103 bool parseTranslateToken(SkMatrix*);
104 bool parseScaleToken(SkMatrix*);
105 bool parseRotateToken(SkMatrix*);
106 bool parseSkewXToken(SkMatrix*);
107 bool parseSkewYToken(SkMatrix*);
108
109 // Parses a sequence of 'WS* <prefix> WS* (<nested>)', where the nested sequence
110 // is handled by the passed functor.
111 template <typename Func, typename T>
112 bool parseParenthesized(const char* prefix, Func, T* result);
113
Florin Malita2d059fc2021-01-05 11:53:15 -0500114 template <typename T>
115 bool parseList(std::vector<T>*);
116
Florin Malita39fe8c82020-10-20 10:43:03 -0400117 template <typename T, typename TArray>
Tyler Denniston70bb18d2020-11-06 12:07:53 -0500118 bool parseEnumMap(const TArray& arr, T* result) {
119 for (size_t i = 0; i < SK_ARRAY_COUNT(arr); ++i) {
120 if (this->parseExpectedStringToken(std::get<0>(arr[i]))) {
121 *result = std::get<1>(arr[i]);
122 return true;
123 }
124 }
125 return false;
126 }
Florin Malita39fe8c82020-10-20 10:43:03 -0400127
Florin Malitab3418102020-10-15 18:10:29 -0400128 // The current position in the input string.
129 const char* fCurPos;
130
131 using INHERITED = SkNoncopyable;
132};
133
134#endif // SkSVGAttributeParser_DEFINED