blob: 06624a0941acb74fa24cce6ca9336e8a7f811d8f [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
11#include "include/private/SkNoncopyable.h"
12#include "modules/svg/include/SkSVGTypes.h"
Tyler Denniston57154992020-11-04 16:08:30 -050013#include "src/core/SkTLazy.h"
Florin Malitab3418102020-10-15 18:10:29 -040014
15class SkSVGAttributeParser : public SkNoncopyable {
16public:
17 SkSVGAttributeParser(const char[]);
18
19 bool parseColor(SkSVGColorType*);
20 bool parseClipPath(SkSVGClip*);
21 bool parseFillRule(SkSVGFillRule*);
Tyler Dennistonb3cafbc2020-10-30 15:00:48 -040022 bool parseFilter(SkSVGFilterType*);
Florin Malitab3418102020-10-15 18:10:29 -040023 bool parseNumber(SkSVGNumberType*);
Tyler Dennistondada9602020-11-03 10:04:25 -050024 bool parseInteger(SkSVGIntegerType*);
Florin Malitab3418102020-10-15 18:10:29 -040025 bool parseLength(SkSVGLength*);
26 bool parseViewBox(SkSVGViewBoxType*);
27 bool parseTransform(SkSVGTransformType*);
28 bool parsePaint(SkSVGPaint*);
29 bool parseLineCap(SkSVGLineCap*);
30 bool parseLineJoin(SkSVGLineJoin*);
31 bool parsePoints(SkSVGPointsType*);
32 bool parseIRI(SkSVGStringType*);
33 bool parseSpreadMethod(SkSVGSpreadMethod*);
34 bool parseStopColor(SkSVGStopColor*);
Tyler Denniston30e327e2020-10-29 16:29:22 -040035 bool parseObjectBoundingBoxUnits(SkSVGObjectBoundingBoxUnits*);
Florin Malitab3418102020-10-15 18:10:29 -040036 bool parseVisibility(SkSVGVisibility*);
37 bool parseDashArray(SkSVGDashArray*);
Florin Malita385e7442020-10-21 16:55:46 -040038 bool parsePreserveAspectRatio(SkSVGPreserveAspectRatio*);
Florin Malitab3418102020-10-15 18:10:29 -040039
Florin Malita39fe8c82020-10-20 10:43:03 -040040 bool parseFontFamily(SkSVGFontFamily*);
41 bool parseFontSize(SkSVGFontSize*);
42 bool parseFontStyle(SkSVGFontStyle*);
43 bool parseFontWeight(SkSVGFontWeight*);
Florin Malita056385b2020-10-27 22:57:56 -040044 bool parseTextAnchor(SkSVGTextAnchor*);
Florin Malita39fe8c82020-10-20 10:43:03 -040045
Tyler Denniston57154992020-11-04 16:08:30 -050046 bool parseEOSToken();
47 bool parseCommaWspToken();
48 bool parseExpectedStringToken(const char*);
49
50 // TODO: Migrate all parse*() functions to this style (and delete the old version)
51 // so they can be used by parse<T>():
52 bool parse(SkSVGNumberType* v) { return parseNumber(v); }
53 bool parse(SkSVGIntegerType* v) { return parseInteger(v); }
54
55 template <typename T> using ParseResult = SkTLazy<T>;
56
57 template <typename T>
58 static ParseResult<T> parse(const char* expectedName,
59 const char* name,
60 const char* value,
61 bool (*parseFnc)(const char*, T*)) {
62 if (strcmp(name, expectedName) != 0) {
63 return ParseResult<T>();
64 }
65
66 T parsedValue;
67 if (parseFnc(value, &parsedValue)) {
68 return ParseResult<T>(&parsedValue);
69 }
70
71 return ParseResult<T>();
72 }
73
74 template <typename T>
75 static ParseResult<T> parse(const char* expectedName, const char* name, const char* value) {
76 const auto parseFnc = +[](const char* str, T* v) {
77 SkSVGAttributeParser parser(str);
78 return parser.parse(v);
79 };
80 return parse(expectedName, name, value, parseFnc);
81 }
82
Florin Malitab3418102020-10-15 18:10:29 -040083private:
84 // Stack-only
85 void* operator new(size_t) = delete;
86 void* operator new(size_t, void*) = delete;
87
88 template <typename F>
89 bool advanceWhile(F func);
90
91 bool parseWSToken();
Florin Malitab3418102020-10-15 18:10:29 -040092 bool parseSepToken();
Florin Malitab3418102020-10-15 18:10:29 -040093 bool parseScalarToken(SkScalar*);
Tyler Dennistondada9602020-11-03 10:04:25 -050094 bool parseInt32Token(int32_t*);
Florin Malitab3418102020-10-15 18:10:29 -040095 bool parseHexToken(uint32_t*);
96 bool parseLengthUnitToken(SkSVGLength::Unit*);
97 bool parseNamedColorToken(SkColor*);
98 bool parseHexColorToken(SkColor*);
99 bool parseColorComponentToken(int32_t*);
100 bool parseRGBColorToken(SkColor*);
101 bool parseFuncIRI(SkSVGStringType*);
102
103 // Transform helpers
104 bool parseMatrixToken(SkMatrix*);
105 bool parseTranslateToken(SkMatrix*);
106 bool parseScaleToken(SkMatrix*);
107 bool parseRotateToken(SkMatrix*);
108 bool parseSkewXToken(SkMatrix*);
109 bool parseSkewYToken(SkMatrix*);
110
111 // Parses a sequence of 'WS* <prefix> WS* (<nested>)', where the nested sequence
112 // is handled by the passed functor.
113 template <typename Func, typename T>
114 bool parseParenthesized(const char* prefix, Func, T* result);
115
Florin Malita39fe8c82020-10-20 10:43:03 -0400116 template <typename T, typename TArray>
117 bool parseEnumMap(const TArray& arr, T* result);
118
Florin Malitab3418102020-10-15 18:10:29 -0400119 // The current position in the input string.
120 const char* fCurPos;
121
122 using INHERITED = SkNoncopyable;
123};
124
125#endif // SkSVGAttributeParser_DEFINED