[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>
This commit is contained in:
Florin Malita 2018-01-04 11:27:09 -05:00 committed by Skia Commit-Bot
parent 6dbff086f0
commit 02a32b0fa4
3 changed files with 99 additions and 1 deletions

View File

@ -194,6 +194,41 @@ sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, Att
return rect_node; 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) { sk_sp<sksg::Color> AttachColorPaint(const Json::Value& obj, AttachContext* ctx) {
SkASSERT(obj.isObject()); SkASSERT(obj.isObject());
@ -278,6 +313,7 @@ static constexpr GeometryAttacherT gGeometryAttachers[] = {
AttachPathGeometry, AttachPathGeometry,
AttachRRectGeometry, AttachRRectGeometry,
AttachEllipseGeometry, AttachEllipseGeometry,
AttachPolystarGeometry,
}; };
using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*); 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 { "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint
{ "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup { "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup
{ "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
{ "rc", ShapeType::kGeometry , 1 }, // shape -> AttachRRectGeometry { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
{ "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
{ "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
{ "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint { "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint
{ "tr", ShapeType::kTransform , 0 }, // transform -> AttachTransform { "tr", ShapeType::kTransform , 0 }, // transform -> AttachTransform
}; };

View File

@ -10,9 +10,12 @@
#include "SkColor.h" #include "SkColor.h"
#include "SkottyPriv.h" #include "SkottyPriv.h"
#include "SkPath.h" #include "SkPath.h"
#include "SkSGPath.h"
#include "SkSGRect.h" #include "SkSGRect.h"
#include "SkSGTransform.h" #include "SkSGTransform.h"
#include <cmath>
namespace skotty { namespace skotty {
namespace { namespace {
@ -185,4 +188,36 @@ void CompositeTransform::apply() {
fTransformNode->setMatrix(t); 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 } // namespace skotty

View File

@ -21,6 +21,7 @@
class SkPath; class SkPath;
namespace sksg { namespace sksg {
class Path;
class RRect; class RRect;
class RenderNode; class RenderNode;
class Transform; class Transform;
@ -111,6 +112,31 @@ private:
using INHERITED = SkRefCnt; 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 { class CompositeTransform final : public SkRefCnt {
public: public:
explicit CompositeTransform(sk_sp<sksg::RenderNode>); explicit CompositeTransform(sk_sp<sksg::RenderNode>);