[SVGDom] Initial <use> support
https://www.w3.org/TR/SVG/struct.html#UseElement Change-Id: Id85c2648255dcd4763bf11c7bdb8b1490286f1e4 Reviewed-on: https://skia-review.googlesource.com/58260 Commit-Queue: Florin Malita <fmalita@chromium.org> Reviewed-by: Robert Phillips <robertphillips@google.com>
This commit is contained in:
parent
c9472af858
commit
6a69c053ff
1
BUILD.gn
1
BUILD.gn
@ -1185,6 +1185,7 @@ if (skia_enable_tools) {
|
|||||||
"experimental/svg/model/SkSVGShape.cpp",
|
"experimental/svg/model/SkSVGShape.cpp",
|
||||||
"experimental/svg/model/SkSVGStop.cpp",
|
"experimental/svg/model/SkSVGStop.cpp",
|
||||||
"experimental/svg/model/SkSVGTransformableNode.cpp",
|
"experimental/svg/model/SkSVGTransformableNode.cpp",
|
||||||
|
"experimental/svg/model/SkSVGUse.cpp",
|
||||||
"experimental/svg/model/SkSVGValue.cpp",
|
"experimental/svg/model/SkSVGValue.cpp",
|
||||||
]
|
]
|
||||||
deps = [
|
deps = [
|
||||||
|
@ -33,4 +33,4 @@ private:
|
|||||||
typedef SkSVGTransformableNode INHERITED;
|
typedef SkSVGTransformableNode INHERITED;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SkSVGSVG_DEFINED
|
#endif // SkSVGContainer_DEFINED
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#include "SkSVGStop.h"
|
#include "SkSVGStop.h"
|
||||||
#include "SkSVGSVG.h"
|
#include "SkSVGSVG.h"
|
||||||
#include "SkSVGTypes.h"
|
#include "SkSVGTypes.h"
|
||||||
|
#include "SkSVGUse.h"
|
||||||
#include "SkSVGValue.h"
|
#include "SkSVGValue.h"
|
||||||
#include "SkTSearch.h"
|
#include "SkTSearch.h"
|
||||||
|
|
||||||
@ -335,6 +336,7 @@ SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
|
|||||||
{ "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
|
{ "rect" , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }},
|
||||||
{ "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
|
{ "stop" , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make(); }},
|
||||||
{ "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
|
{ "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }},
|
||||||
|
{ "use" , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make(); }},
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ConstructionContext {
|
struct ConstructionContext {
|
||||||
|
@ -32,7 +32,8 @@ enum class SkSVGTag {
|
|||||||
kRadialGradient,
|
kRadialGradient,
|
||||||
kRect,
|
kRect,
|
||||||
kStop,
|
kStop,
|
||||||
kSvg
|
kSvg,
|
||||||
|
kUse
|
||||||
};
|
};
|
||||||
|
|
||||||
class SkSVGNode : public SkRefCnt {
|
class SkSVGNode : public SkRefCnt {
|
||||||
|
86
experimental/svg/model/SkSVGUse.cpp
Normal file
86
experimental/svg/model/SkSVGUse.cpp
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017 Google Inc.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license that can be
|
||||||
|
* found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "SkSVGUse.h"
|
||||||
|
|
||||||
|
#include "SkCanvas.h"
|
||||||
|
#include "SkSVGRenderContext.h"
|
||||||
|
#include "SkSVGValue.h"
|
||||||
|
|
||||||
|
SkSVGUse::SkSVGUse() : INHERITED(SkSVGTag::kUse) {}
|
||||||
|
|
||||||
|
void SkSVGUse::appendChild(sk_sp<SkSVGNode>) {
|
||||||
|
SkDebugf("cannot append child nodes to this element.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void SkSVGUse::setHref(const SkSVGStringType& href) {
|
||||||
|
fHref = href;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SkSVGUse::setX(const SkSVGLength& x) {
|
||||||
|
fX = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SkSVGUse::setY(const SkSVGLength& y) {
|
||||||
|
fY = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SkSVGUse::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
|
||||||
|
switch (attr) {
|
||||||
|
case SkSVGAttribute::kHref:
|
||||||
|
if (const auto* href = v.as<SkSVGStringValue>()) {
|
||||||
|
this->setHref(*href);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SkSVGAttribute::kX:
|
||||||
|
if (const auto* x = v.as<SkSVGLengthValue>()) {
|
||||||
|
this->setX(*x);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SkSVGAttribute::kY:
|
||||||
|
if (const auto* y = v.as<SkSVGLengthValue>()) {
|
||||||
|
this->setY(*y);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
this->INHERITED::onSetAttribute(attr, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SkSVGUse::onPrepareToRender(SkSVGRenderContext* ctx) const {
|
||||||
|
if (fHref.value().isEmpty() || !INHERITED::onPrepareToRender(ctx)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fX.value() || fY.value()) {
|
||||||
|
// Restored when the local SkSVGRenderContext leaves scope.
|
||||||
|
ctx->canvas()->save();
|
||||||
|
ctx->canvas()->translate(fX.value(), fY.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: width/height override for <svg> targets.
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SkSVGUse::onRender(const SkSVGRenderContext& ctx) const {
|
||||||
|
const auto* ref = ctx.findNodeById(fHref);
|
||||||
|
if (!ref) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ref->render(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
SkPath SkSVGUse::onAsPath(const SkSVGRenderContext& ctx) const {
|
||||||
|
const auto* ref = ctx.findNodeById(fHref);
|
||||||
|
if (!ref) {
|
||||||
|
return SkPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ref->asPath(ctx);
|
||||||
|
}
|
47
experimental/svg/model/SkSVGUse.h
Normal file
47
experimental/svg/model/SkSVGUse.h
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017 Google Inc.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license that can be
|
||||||
|
* found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SkSVGUse_DEFINED
|
||||||
|
#define SkSVGUse_DEFINED
|
||||||
|
|
||||||
|
#include "SkSVGTransformableNode.h"
|
||||||
|
#include "SkSVGTypes.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements support for <use> (reference) elements.
|
||||||
|
* (https://www.w3.org/TR/SVG/struct.html#UseElement)
|
||||||
|
*/
|
||||||
|
class SkSVGUse final : public SkSVGTransformableNode {
|
||||||
|
public:
|
||||||
|
~SkSVGUse() override = default;
|
||||||
|
|
||||||
|
static sk_sp<SkSVGUse> Make() { return sk_sp<SkSVGUse>(new SkSVGUse()); }
|
||||||
|
|
||||||
|
void appendChild(sk_sp<SkSVGNode>) override;
|
||||||
|
|
||||||
|
void setHref(const SkSVGStringType&);
|
||||||
|
void setX(const SkSVGLength&);
|
||||||
|
void setY(const SkSVGLength&);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void onSetAttribute(SkSVGAttribute, const SkSVGValue&) override;
|
||||||
|
|
||||||
|
bool onPrepareToRender(SkSVGRenderContext*) const override;
|
||||||
|
void onRender(const SkSVGRenderContext&) const override;
|
||||||
|
SkPath onAsPath(const SkSVGRenderContext&) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
SkSVGUse();
|
||||||
|
|
||||||
|
SkSVGStringType fHref;
|
||||||
|
SkSVGLength fX = SkSVGLength(0);
|
||||||
|
SkSVGLength fY = SkSVGLength(0);
|
||||||
|
|
||||||
|
typedef SkSVGTransformableNode INHERITED;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SkSVGUse_DEFINED
|
Loading…
Reference in New Issue
Block a user