| 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 "include/core/SkColor.h" |
| 12 |
#include "include/core/SkScalar.h" |
| 13 |
#include "include/core/SkTypes.h" |
| 14 |
#include "include/private/base/SkNoncopyable.h" |
| 15 |
#include "modules/svg/include/SkSVGTypes.h" |
| 16 |
|
| 17 |
#include <cstdint> |
| 18 |
#include <cstring> |
| 19 |
#include <optional> |
| 20 |
#include <tuple> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
class SkMatrix; |
| 24 |
class SkString; |
| 25 |
|
| 26 |
class SkSVGAttributeParser : public SkNoncopyable { |
| 27 |
public: |
| 28 |
explicit SkSVGAttributeParser(const char[]); |
| 29 |
|
| 30 |
bool parseInteger(SkSVGIntegerType*); |
| 31 |
bool parseViewBox(SkSVGViewBoxType*); |
| 32 |
bool parsePreserveAspectRatio(SkSVGPreserveAspectRatio*); |
| 33 |
|
| 34 |
// TODO: Migrate all parse*() functions to this style (and delete the old version) |
| 35 |
// so they can be used by parse<T>(): |
| 36 |
bool parse(SkSVGIntegerType* v) { return parseInteger(v); } |
| 37 |
|
| 38 |
template <typename T> using ParseResult = std::optional<T>; |
| 39 |
|
| 40 |
template <typename T> static ParseResult<T> parse(const char* value) { |
| 41 |
T parsedValue; |
| 42 |
if (SkSVGAttributeParser(value).parse(&parsedValue)) { |
| 43 |
return parsedValue; |
| 44 |
} |
| 45 |
return {}; |
| 46 |
} |
| 47 |
|
| 48 |
template <typename T> |
| 49 |
static ParseResult<T> parse(const char* expectedName, |
| 50 |
const char* name, |
| 51 |
const char* value) { |
| 52 |
if (!strcmp(name, expectedName)) { |
| 53 |
return parse<T>(value); |
| 54 |
} |
| 55 |
|
| 56 |
return ParseResult<T>(); |
| 57 |
} |
| 58 |
|
| 59 |
template <typename PropertyT> |
| 60 |
static ParseResult<PropertyT> parseProperty(const char* expectedName, |
| 61 |
const char* name, |
| 62 |
const char* value) { |
| 63 |
if (strcmp(name, expectedName) != 0) { |
| 64 |
return ParseResult<PropertyT>(); |
| 65 |
} |
| 66 |
|
| 67 |
if (!strcmp(value, "inherit")) { |
| 68 |
return PropertyT(SkSVGPropertyState::kInherit); |
| 69 |
} |
| 70 |
|
| 71 |
auto pr = parse<typename PropertyT::ValueT>(value); |
| 72 |
if (pr.has_value()) { |
| 73 |
return PropertyT(*pr); |
| 74 |
} |
| 75 |
|
| 76 |
return ParseResult<PropertyT>(); |
| 77 |
} |
| 78 |
|
| 79 |
private: |
| 80 |
class RestoreCurPos { |
| 81 |
public: |
| 82 |
explicit RestoreCurPos(SkSVGAttributeParser* self) |
| 83 |
: fSelf(self), fCurPos(self->fCurPos) {} |
| 84 |
|
| 85 |
~RestoreCurPos() { |
| 86 |
if (fSelf) { |
| 87 |
fSelf->fCurPos = this->fCurPos; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
void clear() { fSelf = nullptr; } |
| 92 |
private: |
| 93 |
SkSVGAttributeParser* fSelf; |
| 94 |
const char* fCurPos; |
| 95 |
|
| 96 |
RestoreCurPos( const RestoreCurPos&) = delete; |
| 97 |
RestoreCurPos& operator=(const RestoreCurPos&) = delete; |
| 98 |
}; |
| 99 |
|
| 100 |
// Stack-only |
| 101 |
void* operator new(size_t) = delete; |
| 102 |
void* operator new(size_t, void*) = delete; |
| 103 |
|
| 104 |
template <typename T> |
| 105 |
bool parse(T*); |
| 106 |
|
| 107 |
template <typename F> |
| 108 |
bool advanceWhile(F func); |
| 109 |
|
| 110 |
bool matchStringToken(const char* token, const char** newPos = nullptr) const; |
| 111 |
bool matchHexToken(const char** newPos) const; |
| 112 |
|
| 113 |
bool parseWSToken(); |
| 114 |
bool parseEOSToken(); |
| 115 |
bool parseSepToken(); |
| 116 |
bool parseCommaWspToken(); |
| 117 |
bool parseExpectedStringToken(const char*); |
| 118 |
bool parseScalarToken(SkScalar*); |
| 119 |
bool parseInt32Token(int32_t*); |
| 120 |
bool parseEscape(SkUnichar*); |
| 121 |
bool parseIdentToken(SkString*); |
| 122 |
bool parseLengthUnitToken(SkSVGLength::Unit*); |
| 123 |
bool parseNamedColorToken(SkColor*); |
| 124 |
bool parseHexColorToken(SkColor*); |
| 125 |
bool parseColorComponentScalarToken(int32_t*); |
| 126 |
bool parseColorComponentIntegralToken(int32_t*); |
| 127 |
bool parseColorComponentFractionalToken(int32_t*); |
| 128 |
bool parseColorComponentToken(int32_t*); |
| 129 |
bool parseColorToken(SkColor*); |
| 130 |
bool parseRGBColorToken(SkColor*); |
| 131 |
bool parseRGBAColorToken(SkColor*); |
| 132 |
bool parseSVGColor(SkSVGColor*, SkSVGColor::Vars&&); |
| 133 |
bool parseSVGColorType(SkSVGColorType*); |
| 134 |
bool parseFuncIRI(SkSVGFuncIRI*); |
| 135 |
|
| 136 |
// Transform helpers |
| 137 |
bool parseMatrixToken(SkMatrix*); |
| 138 |
bool parseTranslateToken(SkMatrix*); |
| 139 |
bool parseScaleToken(SkMatrix*); |
| 140 |
bool parseRotateToken(SkMatrix*); |
| 141 |
bool parseSkewXToken(SkMatrix*); |
| 142 |
bool parseSkewYToken(SkMatrix*); |
| 143 |
|
| 144 |
// Parses a sequence of 'WS* <prefix> WS* (<nested>)', where the nested sequence |
| 145 |
// is handled by the passed functor. |
| 146 |
template <typename Func, typename T> |
| 147 |
bool parseParenthesized(const char* prefix, Func, T* result); |
| 148 |
|
| 149 |
template <typename T> |
| 150 |
bool parseList(std::vector<T>*); |
| 151 |
|
| 152 |
template <typename T, typename TArray> |
| 153 |
bool parseEnumMap(const TArray& arr, T* result) { |
| 154 |
for (size_t i = 0; i < std::size(arr); ++i) { |
| 155 |
if (this->parseExpectedStringToken(std::get<0>(arr[i]))) { |
| 156 |
*result = std::get<1>(arr[i]); |
| 157 |
return true; |
| 158 |
} |
| 159 |
} |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
// The current position in the input string. |
| 164 |
const char* fCurPos; |
| 165 |
const char* fEndPos; |
| 166 |
|
| 167 |
using INHERITED = SkNoncopyable; |
| 168 |
}; |
| 169 |
|
| 170 |
#endif // SkSVGAttributeParser_DEFINED |