aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/svg/model/SkSVGDOM.cpp
diff options
context:
space:
mode:
authorGravatar fmalita <fmalita@chromium.org>2016-08-03 10:21:11 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-08-03 10:21:11 -0700
commitbffc2566872f99d378a1113d0a49ec9ee0d60b7a (patch)
tree3a7fab8b6206e99201b1ea817c95454f340260c8 /experimental/svg/model/SkSVGDOM.cpp
parent9e3d3d8b363edcdd81a13b2be046152c6bb839cd (diff)
[SVGDom] Initial SVGLength support
Mostly plumb new length types, but also introduce a stateful parser, rect shape and named color support. R=reed@google.com,robertphillips@google.com,stephana@google.com GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2202053002 Review-Url: https://codereview.chromium.org/2202053002
Diffstat (limited to 'experimental/svg/model/SkSVGDOM.cpp')
-rw-r--r--experimental/svg/model/SkSVGDOM.cpp69
1 files changed, 34 insertions, 35 deletions
diff --git a/experimental/svg/model/SkSVGDOM.cpp b/experimental/svg/model/SkSVGDOM.cpp
index 4e2f898942..df589230e0 100644
--- a/experimental/svg/model/SkSVGDOM.cpp
+++ b/experimental/svg/model/SkSVGDOM.cpp
@@ -10,45 +10,20 @@
#include "SkParse.h"
#include "SkParsePath.h"
#include "SkString.h"
+#include "SkSVGAttributeParser.h"
#include "SkSVGDOM.h"
#include "SkSVGG.h"
#include "SkSVGNode.h"
#include "SkSVGPath.h"
+#include "SkSVGRect.h"
+#include "SkSVGRenderContext.h"
#include "SkSVGSVG.h"
+#include "SkSVGTypes.h"
#include "SkSVGValue.h"
#include "SkTSearch.h"
namespace {
-SkColor ParseColor(const char* str) {
- // FIXME: real parser
- if (*str++ != '#') {
- return SK_ColorBLACK;
- }
-
- uint32_t v;
- const char* consumed = SkParse::FindHex(str, &v);
-
- switch(consumed - str) {
- case 6:
- // matched '#xxxxxx'
- break;
- case 3:
- // matched '#xxx;
- v = ((v << 12) & 0x00f00000) |
- ((v << 8) & 0x000ff000) |
- ((v << 4) & 0x00000ff0) |
- ((v << 0) & 0x0000000f);
- break;
- default:
- // failed
- v = 0;
- break;
- }
-
- return v | 0xff000000;
-}
-
const char* ParseScalarPair(const char* str, SkScalar v[2]) {
str = SkParse::FindScalar(str, v);
if (str) {
@@ -98,7 +73,13 @@ SkMatrix ParseTransform(const char* str) {
bool SetPaintAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
const char* stringValue) {
- node->setAttribute(attr, SkSVGColorValue(ParseColor(stringValue)));
+ SkSVGColor color;
+ SkSVGAttributeParser parser(stringValue);
+ if (!parser.parseColor(&color)) {
+ return false;
+ }
+
+ node->setAttribute(attr, SkSVGColorValue(color));
return true;
}
@@ -119,6 +100,18 @@ bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
return true;
}
+bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
+ const char* stringValue) {
+ SkSVGLength length;
+ SkSVGAttributeParser parser(stringValue);
+ if (!parser.parseLength(&length)) {
+ return false;
+ }
+
+ node->setAttribute(attr, SkSVGLengthValue(length));
+ return true;
+}
+
// Breaks a "foo: bar; baz: ..." string into key:value pairs.
class StyleIterator {
public:
@@ -185,16 +178,21 @@ struct AttrParseInfo {
};
SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
- { "d", { SkSVGAttribute::kD, SetPathDataAttribute }},
- { "fill", { SkSVGAttribute::kFill, SetPaintAttribute }},
- { "stroke", { SkSVGAttribute::kStroke, SetPaintAttribute }},
- { "style", { SkSVGAttribute::kUnknown, SetStyleAttributes }},
+ { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
+ { "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
+ { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
+ { "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
+ { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
{ "transform", { SkSVGAttribute::kTransform, SetTransformAttribute }},
+ { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
+ { "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
+ { "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
};
SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
{ "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
{ "path", []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
+ { "rect", []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
{ "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
};
@@ -294,7 +292,8 @@ sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream, const SkSize& cont
void SkSVGDOM::render(SkCanvas* canvas) const {
if (fRoot) {
- fRoot->render(canvas);
+ SkSVGRenderContext ctx(fContainerSize);
+ fRoot->render(canvas, ctx);
}
}