2016-07-27 01:46:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDOM.h"
|
|
|
|
#include "SkParsePath.h"
|
2016-08-03 17:21:11 +00:00
|
|
|
#include "SkSVGAttributeParser.h"
|
2016-08-16 22:38:51 +00:00
|
|
|
#include "SkSVGCircle.h"
|
2016-12-08 14:26:47 +00:00
|
|
|
#include "SkSVGClipPath.h"
|
2018-06-13 13:42:32 +00:00
|
|
|
#include "SkSVGDOM.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkSVGDefs.h"
|
2016-08-16 22:38:51 +00:00
|
|
|
#include "SkSVGEllipse.h"
|
2016-07-27 01:46:34 +00:00
|
|
|
#include "SkSVGG.h"
|
2016-08-17 15:38:15 +00:00
|
|
|
#include "SkSVGLine.h"
|
2016-09-13 00:06:47 +00:00
|
|
|
#include "SkSVGLinearGradient.h"
|
2016-07-27 01:46:34 +00:00
|
|
|
#include "SkSVGNode.h"
|
|
|
|
#include "SkSVGPath.h"
|
2017-10-11 18:34:33 +00:00
|
|
|
#include "SkSVGPattern.h"
|
2016-08-12 19:15:33 +00:00
|
|
|
#include "SkSVGPoly.h"
|
2017-10-09 20:05:30 +00:00
|
|
|
#include "SkSVGRadialGradient.h"
|
2016-08-03 17:21:11 +00:00
|
|
|
#include "SkSVGRect.h"
|
|
|
|
#include "SkSVGRenderContext.h"
|
2018-06-13 13:42:32 +00:00
|
|
|
#include "SkSVGSVG.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkSVGStop.h"
|
2016-08-03 17:21:11 +00:00
|
|
|
#include "SkSVGTypes.h"
|
2017-10-11 18:02:11 +00:00
|
|
|
#include "SkSVGUse.h"
|
2016-07-27 01:46:34 +00:00
|
|
|
#include "SkSVGValue.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkString.h"
|
2016-07-27 01:46:34 +00:00
|
|
|
#include "SkTSearch.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkTo.h"
|
2016-07-27 01:46:34 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool SetPaintAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
2016-08-11 16:16:29 +00:00
|
|
|
SkSVGPaint paint;
|
2016-08-03 17:21:11 +00:00
|
|
|
SkSVGAttributeParser parser(stringValue);
|
2016-08-11 16:16:29 +00:00
|
|
|
if (!parser.parsePaint(&paint)) {
|
2016-09-13 00:06:47 +00:00
|
|
|
return false;
|
2016-08-03 17:21:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
node->setAttribute(attr, SkSVGPaintValue(paint));
|
2016-07-27 01:46:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-13 00:06:47 +00:00
|
|
|
bool SetColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGColorType color;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseColor(&color)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGColorValue(color));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGStringType iri;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseIRI(&iri)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGStringValue(iri));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 14:26:47 +00:00
|
|
|
bool SetClipPathAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGClip clip;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseClipPath(&clip)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGClipValue(clip));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-27 01:46:34 +00:00
|
|
|
bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkPath path;
|
|
|
|
if (!SkParsePath::FromSVGString(stringValue, &path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGPathValue(path));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
2016-08-08 19:58:57 +00:00
|
|
|
SkSVGTransformType transform;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseTransform(&transform)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGTransformValue(transform));
|
2016-07-27 01:46:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-03 17:21:11 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
bool SetNumberAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGNumberType number;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseNumber(&number)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGNumberValue(number));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:38:55 +00:00
|
|
|
bool SetViewBoxAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGViewBoxType viewBox;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseViewBox(&viewBox)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGViewBoxValue(viewBox));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
bool SetLineCapAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGLineCap lineCap;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseLineCap(&lineCap)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGLineCapValue(lineCap));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetLineJoinAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGLineJoin lineJoin;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseLineJoin(&lineJoin)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGLineJoinValue(lineJoin));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:56:11 +00:00
|
|
|
bool SetSpreadMethodAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGSpreadMethod spread;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseSpreadMethod(&spread)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGSpreadMethodValue(spread));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:15:33 +00:00
|
|
|
bool SetPointsAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGPointsType points;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parsePoints(&points)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGPointsValue(points));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:35:11 +00:00
|
|
|
bool SetFillRuleAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGFillRule fillRule;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseFillRule(&fillRule)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGFillRuleValue(fillRule));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:33:28 +00:00
|
|
|
bool SetVisibilityAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGVisibility visibility;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseVisibility(&visibility)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGVisibilityValue(visibility));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-13 18:07:44 +00:00
|
|
|
bool SetDashArrayAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
|
|
|
|
const char* stringValue) {
|
|
|
|
SkSVGDashArray dashArray;
|
|
|
|
SkSVGAttributeParser parser(stringValue);
|
|
|
|
if (!parser.parseDashArray(&dashArray)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setAttribute(attr, SkSVGDashArrayValue(dashArray));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-08 20:58:50 +00:00
|
|
|
SkString TrimmedString(const char* first, const char* last) {
|
|
|
|
SkASSERT(first);
|
|
|
|
SkASSERT(last);
|
|
|
|
SkASSERT(first <= last);
|
|
|
|
|
|
|
|
while (first <= last && *first <= ' ') { first++; }
|
|
|
|
while (first <= last && *last <= ' ') { last--; }
|
|
|
|
|
|
|
|
SkASSERT(last - first + 1 >= 0);
|
|
|
|
return SkString(first, SkTo<size_t>(last - first + 1));
|
|
|
|
}
|
|
|
|
|
2016-07-29 15:52:03 +00:00
|
|
|
// Breaks a "foo: bar; baz: ..." string into key:value pairs.
|
|
|
|
class StyleIterator {
|
|
|
|
public:
|
|
|
|
StyleIterator(const char* str) : fPos(str) { }
|
|
|
|
|
|
|
|
std::tuple<SkString, SkString> next() {
|
|
|
|
SkString name, value;
|
|
|
|
|
|
|
|
if (fPos) {
|
|
|
|
const char* sep = this->nextSeparator();
|
|
|
|
SkASSERT(*sep == ';' || *sep == '\0');
|
|
|
|
|
|
|
|
const char* valueSep = strchr(fPos, ':');
|
|
|
|
if (valueSep && valueSep < sep) {
|
2016-08-08 20:58:50 +00:00
|
|
|
name = TrimmedString(fPos, valueSep - 1);
|
|
|
|
value = TrimmedString(valueSep + 1, sep - 1);
|
2016-07-29 15:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fPos = *sep ? sep + 1 : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_tuple(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* nextSeparator() const {
|
|
|
|
const char* sep = fPos;
|
|
|
|
while (*sep != ';' && *sep != '\0') {
|
|
|
|
sep++;
|
|
|
|
}
|
|
|
|
return sep;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* fPos;
|
|
|
|
};
|
|
|
|
|
|
|
|
void set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value);
|
|
|
|
|
|
|
|
bool SetStyleAttributes(const sk_sp<SkSVGNode>& node, SkSVGAttribute,
|
|
|
|
const char* stringValue) {
|
|
|
|
|
|
|
|
SkString name, value;
|
|
|
|
StyleIterator iter(stringValue);
|
|
|
|
for (;;) {
|
|
|
|
std::tie(name, value) = iter.next();
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
set_string_attribute(node, name.c_str(), value.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-27 01:46:34 +00:00
|
|
|
template<typename T>
|
|
|
|
struct SortedDictionaryEntry {
|
|
|
|
const char* fKey;
|
|
|
|
const T fValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AttrParseInfo {
|
|
|
|
SkSVGAttribute fAttr;
|
|
|
|
bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const char* stringValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
|
2016-12-08 14:26:47 +00:00
|
|
|
{ "clip-path" , { SkSVGAttribute::kClipPath , SetClipPathAttribute }},
|
2017-10-10 15:22:08 +00:00
|
|
|
{ "clip-rule" , { SkSVGAttribute::kClipRule , SetFillRuleAttribute }},
|
2016-09-13 20:59:05 +00:00
|
|
|
{ "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
|
|
|
|
{ "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
|
|
|
|
{ "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
|
|
|
|
{ "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
|
|
|
|
{ "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
|
2016-12-01 18:35:11 +00:00
|
|
|
{ "fill-rule" , { SkSVGAttribute::kFillRule , SetFillRuleAttribute }},
|
2017-10-09 20:05:30 +00:00
|
|
|
// focal point x & y
|
|
|
|
{ "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
|
|
|
|
{ "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
|
2016-09-13 20:59:05 +00:00
|
|
|
{ "gradientTransform", { SkSVGAttribute::kGradientTransform, SetTransformAttribute }},
|
|
|
|
{ "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
|
|
|
|
{ "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
|
|
|
|
{ "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
|
2017-10-11 18:34:33 +00:00
|
|
|
{ "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
|
2016-09-13 20:59:05 +00:00
|
|
|
{ "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
|
|
|
|
{ "r" , { SkSVGAttribute::kR , SetLengthAttribute }},
|
|
|
|
{ "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
|
|
|
|
{ "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
|
|
|
|
{ "spreadMethod" , { SkSVGAttribute::kSpreadMethod , SetSpreadMethodAttribute }},
|
|
|
|
{ "stop-color" , { SkSVGAttribute::kStopColor , SetColorAttribute }},
|
|
|
|
{ "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
|
|
|
|
{ "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
|
2017-10-13 18:07:44 +00:00
|
|
|
{ "stroke-dasharray" , { SkSVGAttribute::kStrokeDashArray , SetDashArrayAttribute }},
|
2017-10-13 22:18:32 +00:00
|
|
|
{ "stroke-dashoffset", { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
|
2016-09-13 20:59:05 +00:00
|
|
|
{ "stroke-linecap" , { SkSVGAttribute::kStrokeLineCap , SetLineCapAttribute }},
|
|
|
|
{ "stroke-linejoin" , { SkSVGAttribute::kStrokeLineJoin , SetLineJoinAttribute }},
|
2017-10-09 16:57:41 +00:00
|
|
|
{ "stroke-miterlimit", { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
|
2016-09-13 20:59:05 +00:00
|
|
|
{ "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
|
|
|
|
{ "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
|
|
|
|
{ "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
|
|
|
|
{ "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},
|
|
|
|
{ "viewBox" , { SkSVGAttribute::kViewBox , SetViewBoxAttribute }},
|
2017-10-12 15:33:28 +00:00
|
|
|
{ "visibility" , { SkSVGAttribute::kVisibility , SetVisibilityAttribute }},
|
2016-09-13 20:59:05 +00:00
|
|
|
{ "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }},
|
|
|
|
{ "x" , { SkSVGAttribute::kX , SetLengthAttribute }},
|
|
|
|
{ "x1" , { SkSVGAttribute::kX1 , SetLengthAttribute }},
|
|
|
|
{ "x2" , { SkSVGAttribute::kX2 , SetLengthAttribute }},
|
|
|
|
{ "xlink:href" , { SkSVGAttribute::kHref , SetIRIAttribute }},
|
|
|
|
{ "y" , { SkSVGAttribute::kY , SetLengthAttribute }},
|
|
|
|
{ "y1" , { SkSVGAttribute::kY1 , SetLengthAttribute }},
|
|
|
|
{ "y2" , { SkSVGAttribute::kY2 , SetLengthAttribute }},
|
2016-07-27 01:46:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
|
2017-10-10 13:16:52 +00:00
|
|
|
{ "a" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
|
2016-09-13 00:06:47 +00:00
|
|
|
{ "circle" , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make(); }},
|
2016-12-08 14:26:47 +00:00
|
|
|
{ "clipPath" , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make(); }},
|
2016-09-13 00:06:47 +00:00
|
|
|
{ "defs" , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make(); }},
|
|
|
|
{ "ellipse" , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make(); }},
|
|
|
|
{ "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},
|
|
|
|
{ "line" , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make(); }},
|
|
|
|
{ "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
|
|
|
|
{ "path" , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }},
|
2017-10-11 18:34:33 +00:00
|
|
|
{ "pattern" , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make(); }},
|
2016-09-13 00:06:47 +00:00
|
|
|
{ "polygon" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon(); }},
|
|
|
|
{ "polyline" , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline(); }},
|
2017-10-09 20:05:30 +00:00
|
|
|
{ "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
|
2016-09-13 00:06:47 +00:00
|
|
|
{ "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
|
|
|
|
{ "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
|
|
|
|
{ "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
|
2017-10-11 18:02:11 +00:00
|
|
|
{ "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
|
2016-07-27 01:46:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstructionContext {
|
2016-09-13 00:06:47 +00:00
|
|
|
ConstructionContext(SkSVGIDMapper* mapper) : fParent(nullptr), fIDMapper(mapper) {}
|
2016-07-27 01:46:34 +00:00
|
|
|
ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>& newParent)
|
2016-09-13 00:06:47 +00:00
|
|
|
: fParent(newParent.get()), fIDMapper(other.fIDMapper) {}
|
2016-07-27 01:46:34 +00:00
|
|
|
|
|
|
|
const SkSVGNode* fParent;
|
2016-09-13 00:06:47 +00:00
|
|
|
SkSVGIDMapper* fIDMapper;
|
2016-07-27 01:46:34 +00:00
|
|
|
};
|
|
|
|
|
2016-07-29 15:52:03 +00:00
|
|
|
void set_string_attribute(const sk_sp<SkSVGNode>& node, const char* name, const char* value) {
|
|
|
|
const int attrIndex = SkStrSearch(&gAttributeParseInfo[0].fKey,
|
|
|
|
SkTo<int>(SK_ARRAY_COUNT(gAttributeParseInfo)),
|
|
|
|
name, sizeof(gAttributeParseInfo[0]));
|
|
|
|
if (attrIndex < 0) {
|
2016-08-10 23:25:32 +00:00
|
|
|
#if defined(SK_VERBOSE_SVG_PARSING)
|
2016-07-29 15:52:03 +00:00
|
|
|
SkDebugf("unhandled attribute: %s\n", name);
|
2016-08-10 23:25:32 +00:00
|
|
|
#endif
|
2016-07-29 15:52:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(SkTo<size_t>(attrIndex) < SK_ARRAY_COUNT(gAttributeParseInfo));
|
|
|
|
const auto& attrInfo = gAttributeParseInfo[attrIndex].fValue;
|
|
|
|
if (!attrInfo.fSetter(node, attrInfo.fAttr, value)) {
|
2016-08-10 23:25:32 +00:00
|
|
|
#if defined(SK_VERBOSE_SVG_PARSING)
|
2016-07-29 15:52:03 +00:00
|
|
|
SkDebugf("could not parse attribute: '%s=\"%s\"'\n", name, value);
|
2016-08-10 23:25:32 +00:00
|
|
|
#endif
|
2016-07-29 15:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 01:46:34 +00:00
|
|
|
void parse_node_attributes(const SkDOM& xmlDom, const SkDOM::Node* xmlNode,
|
2016-09-13 00:06:47 +00:00
|
|
|
const sk_sp<SkSVGNode>& svgNode, SkSVGIDMapper* mapper) {
|
2016-07-27 01:46:34 +00:00
|
|
|
const char* name, *value;
|
|
|
|
SkDOM::AttrIter attrIter(xmlDom, xmlNode);
|
|
|
|
while ((name = attrIter.next(&value))) {
|
2016-09-13 00:06:47 +00:00
|
|
|
// We're handling id attributes out of band for now.
|
|
|
|
if (!strcmp(name, "id")) {
|
|
|
|
mapper->set(SkString(value), svgNode);
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-29 15:52:03 +00:00
|
|
|
set_string_attribute(svgNode, name, value);
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkSVGNode> construct_svg_node(const SkDOM& dom, const ConstructionContext& ctx,
|
|
|
|
const SkDOM::Node* xmlNode) {
|
|
|
|
const char* elem = dom.getName(xmlNode);
|
|
|
|
const SkDOM::Type elemType = dom.getType(xmlNode);
|
|
|
|
|
|
|
|
if (elemType == SkDOM::kText_Type) {
|
|
|
|
SkASSERT(dom.countChildren(xmlNode) == 0);
|
|
|
|
// TODO: text handling
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(elemType == SkDOM::kElement_Type);
|
|
|
|
|
|
|
|
const int tagIndex = SkStrSearch(&gTagFactories[0].fKey,
|
|
|
|
SkTo<int>(SK_ARRAY_COUNT(gTagFactories)),
|
|
|
|
elem, sizeof(gTagFactories[0]));
|
|
|
|
if (tagIndex < 0) {
|
2016-08-10 23:25:32 +00:00
|
|
|
#if defined(SK_VERBOSE_SVG_PARSING)
|
2016-07-27 01:46:34 +00:00
|
|
|
SkDebugf("unhandled element: <%s>\n", elem);
|
2016-08-10 23:25:32 +00:00
|
|
|
#endif
|
2016-07-27 01:46:34 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(SkTo<size_t>(tagIndex) < SK_ARRAY_COUNT(gTagFactories));
|
|
|
|
sk_sp<SkSVGNode> node = gTagFactories[tagIndex].fValue();
|
2016-09-13 00:06:47 +00:00
|
|
|
parse_node_attributes(dom, xmlNode, node, ctx.fIDMapper);
|
2016-07-27 01:46:34 +00:00
|
|
|
|
|
|
|
ConstructionContext localCtx(ctx, node);
|
|
|
|
for (auto* child = dom.getFirstChild(xmlNode, nullptr); child;
|
|
|
|
child = dom.getNextSibling(child)) {
|
|
|
|
sk_sp<SkSVGNode> childNode = construct_svg_node(dom, localCtx, child);
|
|
|
|
if (childNode) {
|
|
|
|
node->appendChild(std::move(childNode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-09-14 19:04:30 +00:00
|
|
|
SkSVGDOM::SkSVGDOM()
|
|
|
|
: fContainerSize(SkSize::Make(0, 0)) {
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 19:04:30 +00:00
|
|
|
sk_sp<SkSVGDOM> SkSVGDOM::MakeFromDOM(const SkDOM& xmlDom) {
|
|
|
|
sk_sp<SkSVGDOM> dom = sk_make_sp<SkSVGDOM>();
|
2016-07-27 01:46:34 +00:00
|
|
|
|
2016-09-13 00:06:47 +00:00
|
|
|
ConstructionContext ctx(&dom->fIDMapper);
|
2016-07-27 01:46:34 +00:00
|
|
|
dom->fRoot = construct_svg_node(xmlDom, ctx, xmlDom.getRootNode());
|
|
|
|
|
2016-09-14 19:04:30 +00:00
|
|
|
// Reset the default container size to match the intrinsic SVG size.
|
|
|
|
dom->setContainerSize(dom->intrinsicSize());
|
|
|
|
|
2016-07-27 01:46:34 +00:00
|
|
|
return dom;
|
|
|
|
}
|
|
|
|
|
2016-09-14 19:04:30 +00:00
|
|
|
sk_sp<SkSVGDOM> SkSVGDOM::MakeFromStream(SkStream& svgStream) {
|
2016-07-27 01:46:34 +00:00
|
|
|
SkDOM xmlDom;
|
|
|
|
if (!xmlDom.build(svgStream)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-14 19:04:30 +00:00
|
|
|
return MakeFromDOM(xmlDom);
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkSVGDOM::render(SkCanvas* canvas) const {
|
|
|
|
if (fRoot) {
|
2017-09-09 13:39:07 +00:00
|
|
|
SkSVGLengthContext lctx(fContainerSize);
|
|
|
|
SkSVGPresentationContext pctx;
|
|
|
|
fRoot->render(SkSVGRenderContext(canvas, fIDMapper, lctx, pctx));
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 19:04:30 +00:00
|
|
|
SkSize SkSVGDOM::intrinsicSize() const {
|
|
|
|
if (!fRoot || fRoot->tag() != SkSVGTag::kSvg) {
|
|
|
|
return SkSize::Make(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intrinsic sizes are never relative, so the viewport size is irrelevant.
|
|
|
|
const SkSVGLengthContext lctx(SkSize::Make(0, 0));
|
|
|
|
return static_cast<const SkSVGSVG*>(fRoot.get())->intrinsicSize(lctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkSize& SkSVGDOM::containerSize() const {
|
|
|
|
return fContainerSize;
|
|
|
|
}
|
|
|
|
|
2016-07-27 01:46:34 +00:00
|
|
|
void SkSVGDOM::setContainerSize(const SkSize& containerSize) {
|
|
|
|
// TODO: inval
|
|
|
|
fContainerSize = containerSize;
|
|
|
|
}
|
2016-08-12 20:17:11 +00:00
|
|
|
|
|
|
|
void SkSVGDOM::setRoot(sk_sp<SkSVGNode> root) {
|
|
|
|
fRoot = std::move(root);
|
|
|
|
}
|