Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 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 | |
Florin Malita | 2d059fc | 2021-01-05 11:53:15 -0500 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 13 | #include "include/private/SkNoncopyable.h" |
| 14 | #include "modules/svg/include/SkSVGTypes.h" |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 15 | #include "src/core/SkTLazy.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 16 | |
| 17 | class SkSVGAttributeParser : public SkNoncopyable { |
| 18 | public: |
| 19 | SkSVGAttributeParser(const char[]); |
| 20 | |
Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 21 | bool parseInteger(SkSVGIntegerType*); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 22 | bool parseViewBox(SkSVGViewBoxType*); |
Florin Malita | 385e744 | 2020-10-21 16:55:46 -0400 | [diff] [blame] | 23 | bool parsePreserveAspectRatio(SkSVGPreserveAspectRatio*); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 24 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 25 | // TODO: Migrate all parse*() functions to this style (and delete the old version) |
| 26 | // so they can be used by parse<T>(): |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 27 | bool parse(SkSVGIntegerType* v) { return parseInteger(v); } |
| 28 | |
| 29 | template <typename T> using ParseResult = SkTLazy<T>; |
| 30 | |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 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 | |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 40 | template <typename T> |
| 41 | static ParseResult<T> parse(const char* expectedName, |
| 42 | const char* name, |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 43 | const char* value) { |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 44 | if (!strcmp(name, expectedName)) { |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 45 | return parse<T>(value); |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 48 | return ParseResult<T>(); |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Tyler Denniston | 79832e3 | 2020-11-18 09:34:07 -0500 | [diff] [blame] | 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 | } |
Tyler Denniston | a0a5146 | 2020-11-10 13:13:28 -0500 | [diff] [blame] | 72 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 73 | private: |
| 74 | // Stack-only |
| 75 | void* operator new(size_t) = delete; |
| 76 | void* operator new(size_t, void*) = delete; |
| 77 | |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 78 | template <typename T> |
| 79 | bool parse(T*); |
| 80 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 81 | template <typename F> |
| 82 | bool advanceWhile(F func); |
| 83 | |
Tyler Denniston | 209857c | 2021-01-27 14:22:19 -0500 | [diff] [blame] | 84 | bool matchStringToken(const char* token, const char** newPos = nullptr) const; |
| 85 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 86 | bool parseWSToken(); |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 87 | bool parseEOSToken(); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 88 | bool parseSepToken(); |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 89 | bool parseCommaWspToken(); |
| 90 | bool parseExpectedStringToken(const char*); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 91 | bool parseScalarToken(SkScalar*); |
Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 92 | bool parseInt32Token(int32_t*); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 93 | 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 Denniston | e71f547 | 2021-01-27 13:30:59 -0500 | [diff] [blame] | 99 | bool parseFuncIRI(SkSVGFuncIRI*); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 100 | |
| 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 Malita | 2d059fc | 2021-01-05 11:53:15 -0500 | [diff] [blame] | 114 | template <typename T> |
| 115 | bool parseList(std::vector<T>*); |
| 116 | |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 117 | template <typename T, typename TArray> |
Tyler Denniston | 70bb18d | 2020-11-06 12:07:53 -0500 | [diff] [blame] | 118 | 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 Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 127 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 128 | // The current position in the input string. |
| 129 | const char* fCurPos; |
| 130 | |
| 131 | using INHERITED = SkNoncopyable; |
| 132 | }; |
| 133 | |
| 134 | #endif // SkSVGAttributeParser_DEFINED |