aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental
diff options
context:
space:
mode:
authorGravatar Florin Malita <fmalita@chromium.org>2018-01-04 11:27:09 -0500
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2018-01-04 17:55:04 +0000
commit02a32b0fa432a720d7b41c263756a7bac53b020b (patch)
treef9ee98954979600cc56327702de3f939f22854b0 /experimental
parent6dbff086f0a20e433e2d687359bdb54219b538f7 (diff)
[skotty] Add polystar support
TBR= Change-Id: Ifcf6beb75eaf08a150785b72e322bb30ab84b779 Reviewed-on: https://skia-review.googlesource.com/90902 Reviewed-by: Florin Malita <fmalita@chromium.org> Commit-Queue: Florin Malita <fmalita@chromium.org>
Diffstat (limited to 'experimental')
-rw-r--r--experimental/skotty/Skotty.cpp39
-rw-r--r--experimental/skotty/SkottyProperties.cpp35
-rw-r--r--experimental/skotty/SkottyProperties.h26
3 files changed, 99 insertions, 1 deletions
diff --git a/experimental/skotty/Skotty.cpp b/experimental/skotty/Skotty.cpp
index 093d7f68e4..e84abcd4a8 100644
--- a/experimental/skotty/Skotty.cpp
+++ b/experimental/skotty/Skotty.cpp
@@ -194,6 +194,41 @@ sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, Att
return rect_node;
}
+sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
+ SkASSERT(jstar.isObject());
+
+ static constexpr CompositePolyStar::Type gTypes[] = {
+ CompositePolyStar::Type::kStar, // "sy": 1
+ CompositePolyStar::Type::kPoly, // "sy": 2
+ };
+
+ const auto type = ParseInt(jstar["sy"], 0) - 1;
+ if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
+ LogFail(jstar, "Unknown polystar type");
+ return nullptr;
+ }
+
+ auto path_node = sksg::Path::Make();
+ auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
+
+ AttachProperty<VectorValue, SkPoint>(jstar["p"], ctx, composite,
+ [](const sk_sp<CompositePolyStar>& node, const SkPoint& p) { node->setPosition(p); });
+ AttachProperty<ScalarValue, SkScalar>(jstar["pt"], ctx, composite,
+ [](const sk_sp<CompositePolyStar>& node, SkScalar pt) { node->setPointCount(pt); });
+ AttachProperty<ScalarValue, SkScalar>(jstar["ir"], ctx, composite,
+ [](const sk_sp<CompositePolyStar>& node, SkScalar ir) { node->setInnerRadius(ir); });
+ AttachProperty<ScalarValue, SkScalar>(jstar["or"], ctx, composite,
+ [](const sk_sp<CompositePolyStar>& node, SkScalar otr) { node->setOuterRadius(otr); });
+ AttachProperty<ScalarValue, SkScalar>(jstar["is"], ctx, composite,
+ [](const sk_sp<CompositePolyStar>& node, SkScalar is) { node->setInnerRoundness(is); });
+ AttachProperty<ScalarValue, SkScalar>(jstar["os"], ctx, composite,
+ [](const sk_sp<CompositePolyStar>& node, SkScalar os) { node->setOuterRoundness(os); });
+ AttachProperty<ScalarValue, SkScalar>(jstar["r"], ctx, composite,
+ [](const sk_sp<CompositePolyStar>& node, SkScalar r) { node->setRotation(r); });
+
+ return path_node;
+}
+
sk_sp<sksg::Color> AttachColorPaint(const Json::Value& obj, AttachContext* ctx) {
SkASSERT(obj.isObject());
@@ -278,6 +313,7 @@ static constexpr GeometryAttacherT gGeometryAttachers[] = {
AttachPathGeometry,
AttachRRectGeometry,
AttachEllipseGeometry,
+ AttachPolystarGeometry,
};
using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
@@ -325,8 +361,9 @@ const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
{ "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint
{ "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup
{ "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
- { "rc", ShapeType::kGeometry , 1 }, // shape -> AttachRRectGeometry
+ { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
{ "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
+ { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
{ "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint
{ "tr", ShapeType::kTransform , 0 }, // transform -> AttachTransform
};
diff --git a/experimental/skotty/SkottyProperties.cpp b/experimental/skotty/SkottyProperties.cpp
index 241b659c9d..3029409f6c 100644
--- a/experimental/skotty/SkottyProperties.cpp
+++ b/experimental/skotty/SkottyProperties.cpp
@@ -10,9 +10,12 @@
#include "SkColor.h"
#include "SkottyPriv.h"
#include "SkPath.h"
+#include "SkSGPath.h"
#include "SkSGRect.h"
#include "SkSGTransform.h"
+#include <cmath>
+
namespace skotty {
namespace {
@@ -185,4 +188,36 @@ void CompositeTransform::apply() {
fTransformNode->setMatrix(t);
}
+CompositePolyStar::CompositePolyStar(sk_sp<sksg::Path> wrapped_node, Type t)
+ : fPathNode(std::move(wrapped_node))
+ , fType(t) {}
+
+void CompositePolyStar::apply() {
+ const auto count = SkScalarTruncToInt(fPointCount);
+ const auto arc = SK_ScalarPI * 2 / count;
+
+ const auto pt_on_circle = [](const SkPoint& c, SkScalar r, SkScalar a) {
+ return SkPoint::Make(c.x() + r * std::cos(a),
+ c.y() + r * std::sin(a));
+ };
+
+ // TODO: inner/outer "roundness"?
+
+ SkPath poly;
+
+ auto angle = SkDegreesToRadians(fRotation);
+ poly.moveTo(pt_on_circle(fPosition, fOuterRadius, angle));
+
+ for (int i = 0; i < count; ++i) {
+ if (fType == Type::kStar) {
+ poly.lineTo(pt_on_circle(fPosition, fInnerRadius, angle + arc * 0.5f));
+ }
+ angle += arc;
+ poly.lineTo(pt_on_circle(fPosition, fOuterRadius, angle));
+ }
+
+ poly.close();
+ fPathNode->setPath(poly);
+}
+
} // namespace skotty
diff --git a/experimental/skotty/SkottyProperties.h b/experimental/skotty/SkottyProperties.h
index 6147dd4d63..8730f61352 100644
--- a/experimental/skotty/SkottyProperties.h
+++ b/experimental/skotty/SkottyProperties.h
@@ -21,6 +21,7 @@
class SkPath;
namespace sksg {
+class Path;
class RRect;
class RenderNode;
class Transform;
@@ -111,6 +112,31 @@ private:
using INHERITED = SkRefCnt;
};
+class CompositePolyStar final : public SkRefCnt {
+public:
+ enum class Type {
+ kStar, kPoly,
+ };
+
+ CompositePolyStar(sk_sp<sksg::Path>, Type);
+
+ COMPOSITE_PROPERTY(Position , SkPoint , SkPoint::Make(0, 0))
+ COMPOSITE_PROPERTY(PointCount , SkScalar, 0)
+ COMPOSITE_PROPERTY(InnerRadius , SkScalar, 0)
+ COMPOSITE_PROPERTY(OuterRadius , SkScalar, 0)
+ COMPOSITE_PROPERTY(InnerRoundness, SkScalar, 0)
+ COMPOSITE_PROPERTY(OuterRoundness, SkScalar, 0)
+ COMPOSITE_PROPERTY(Rotation , SkScalar, 0)
+
+private:
+ void apply();
+
+ sk_sp<sksg::Path> fPathNode;
+ Type fType;
+
+ using INHERITED = SkRefCnt;
+};
+
class CompositeTransform final : public SkRefCnt {
public:
explicit CompositeTransform(sk_sp<sksg::RenderNode>);