Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2271)

Unified Diff: experimental/svg/model/SkSVGAttributeParser.cpp

Issue 2220933003: [SVGDom] Improved transform parsing (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: review Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « experimental/svg/model/SkSVGAttributeParser.h ('k') | experimental/svg/model/SkSVGDOM.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: experimental/svg/model/SkSVGAttributeParser.cpp
diff --git a/experimental/svg/model/SkSVGAttributeParser.cpp b/experimental/svg/model/SkSVGAttributeParser.cpp
index 62ca4c8ab2536f33c3b724e68e4cff1809eac95a..f973d385f5751906a40374c3b396ea4d3e97b2e2 100644
--- a/experimental/svg/model/SkSVGAttributeParser.cpp
+++ b/experimental/svg/model/SkSVGAttributeParser.cpp
@@ -211,3 +211,144 @@ bool SkSVGAttributeParser::parseViewBox(SkSVGViewBoxType* vb) {
}
return parsedValue && this->parseEOSToken();
}
+
+template <typename Func, typename T>
+bool SkSVGAttributeParser::parseParenthesized(const char* prefix, Func f, T* result) {
+ this->parseWSToken();
+ if (prefix && !this->parseExpectedStringToken(prefix)) {
+ return false;
+ }
+ this->parseWSToken();
+ if (!this->parseExpectedStringToken("(")) {
+ return false;
+ }
+ this->parseWSToken();
+
+ if (!f(result)) {
+ return false;
+ }
+ this->parseWSToken();
+
+ return this->parseExpectedStringToken(")");
+}
+
+bool SkSVGAttributeParser::parseMatrixToken(SkMatrix* matrix) {
+ return this->parseParenthesized("matrix", [this](SkMatrix* m) -> bool {
+ SkScalar scalars[6];
+ for (int i = 0; i < 6; ++i) {
+ if (!(this->parseScalarToken(scalars + i) &&
+ (i > 4 || this->parseSepToken()))) {
+ return false;
+ }
+ }
+
+ m->setAll(scalars[0], scalars[2], scalars[4], scalars[1], scalars[3], scalars[5], 0, 0, 1);
+ return true;
+ }, matrix);
+}
+
+bool SkSVGAttributeParser::parseTranslateToken(SkMatrix* matrix) {
+ return this->parseParenthesized("translate", [this](SkMatrix* m) -> bool {
+ SkScalar tx, ty;
+ this->parseWSToken();
+ if (!this->parseScalarToken(&tx)) {
+ return false;
+ }
+
+ if (!(this->parseSepToken() && this->parseScalarToken(&ty))) {
+ ty = tx;
+ }
+
+ m->setTranslate(tx, ty);
+ return true;
+ }, matrix);
+}
+
+bool SkSVGAttributeParser::parseScaleToken(SkMatrix* matrix) {
+ return this->parseParenthesized("scale", [this](SkMatrix* m) -> bool {
+ SkScalar sx, sy;
+ if (!this->parseScalarToken(&sx)) {
+ return false;
+ }
+
+ if (!(this->parseSepToken() && this->parseScalarToken(&sy))) {
+ sy = sx;
+ }
+
+ m->setScale(sx, sy);
+ return true;
+ }, matrix);
+}
+
+bool SkSVGAttributeParser::parseRotateToken(SkMatrix* matrix) {
+ return this->parseParenthesized("rotate", [this](SkMatrix* m) -> bool {
+ SkScalar angle;
+ if (!this->parseScalarToken(&angle)) {
+ return false;
+ }
+
+ SkScalar cx = 0;
+ SkScalar cy = 0;
+ // optional [<cx> <cy>]
+ if (this->parseSepToken() && this->parseScalarToken(&cx)) {
+ if (!(this->parseSepToken() && this->parseScalarToken(&cy))) {
+ return false;
+ }
+ }
+
+ m->setRotate(angle, cx, cy);
+ return true;
+ }, matrix);
+}
+
+bool SkSVGAttributeParser::parseSkewXToken(SkMatrix* matrix) {
+ return this->parseParenthesized("skewX", [this](SkMatrix* m) -> bool {
+ SkScalar angle;
+ if (!this->parseScalarToken(&angle)) {
+ return false;
+ }
+ m->setSkewX(angle);
+ return true;
+ }, matrix);
+}
+
+bool SkSVGAttributeParser::parseSkewYToken(SkMatrix* matrix) {
+ return this->parseParenthesized("skewY", [this](SkMatrix* m) -> bool {
+ SkScalar angle;
+ if (!this->parseScalarToken(&angle)) {
+ return false;
+ }
+ m->setSkewY(angle);
+ return true;
+ }, matrix);
+}
+
+// https://www.w3.org/TR/SVG/coords.html#TransformAttribute
+bool SkSVGAttributeParser::parseTransform(SkSVGTransformType* t) {
+ SkMatrix matrix = SkMatrix::I();
+
+ bool parsed = false;
+ while (true) {
+ SkMatrix m;
+
+ if (!( this->parseMatrixToken(&m)
+ || this->parseTranslateToken(&m)
+ || this->parseScaleToken(&m)
+ || this->parseRotateToken(&m)
+ || this->parseSkewXToken(&m)
+ || this->parseSkewYToken(&m))) {
+ break;
+ }
+
+ matrix.preConcat(m);
+ parsed = true;
+ }
+
+ this->parseWSToken();
+ if (!parsed || !this->parseEOSToken()) {
+ return false;
+ }
+
+ *t = SkSVGTransformType(matrix);
+ return true;
+}
« no previous file with comments | « experimental/svg/model/SkSVGAttributeParser.h ('k') | experimental/svg/model/SkSVGDOM.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698