[skrive] Parse ellipse data

TBR=
Change-Id: I76b4cd1639bee648630a841164045f8b9693af49
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/298560
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Florin Malita 2020-06-23 21:42:25 -04:00 committed by Skia Commit-Bot
parent 22f246f5ad
commit a3b0b30a78
7 changed files with 134 additions and 32 deletions

View File

@ -75,6 +75,7 @@ protected:
kNode,
kShape,
kColorPaint,
kEllipse,
};
explicit Component(Type t) : fType(t) {}
@ -97,14 +98,25 @@ private:
bool fDirty = true;
};
class Node : public Component {
class TransformableComponent : public Component {
public:
Node() : INHERITED(Type::kNode) {}
ACTOR_ATTR(Translation , SkV2 , SkV2({0, 0}))
ACTOR_ATTR(Scale , SkV2 , SkV2({1, 1}))
ACTOR_ATTR(Rotation , float, 0 )
ACTOR_ATTR(Opacity , float, 1 )
protected:
explicit TransformableComponent(Type t) : INHERITED(t) {}
private:
using INHERITED = Component;
};
class Node : public TransformableComponent {
public:
Node() : INHERITED(Type::kNode) {}
ACTOR_ATTR(CollapsedVisibility, bool , false )
void addChild(sk_sp<Component>);
@ -117,7 +129,7 @@ private:
std::vector<sk_sp<Component>> fChildren;
using INHERITED = Component;
using INHERITED = TransformableComponent;
};
class Drawable : public Node {
@ -188,23 +200,46 @@ private:
using INHERITED = Paint;
};
class Geometry : public TransformableComponent {
protected:
explicit Geometry(Type t) : INHERITED(t) {}
private:
using INHERITED = TransformableComponent;
};
class Ellipse final : public Geometry {
public:
Ellipse() : INHERITED(Type::kEllipse) {}
ACTOR_ATTR(Width , float, 0)
ACTOR_ATTR(Height, float, 0)
private:
void onRevalidate() override;
using INHERITED = Geometry;
};
template <typename T>
constexpr bool Component::is_base_of(Type t) {
if (t == Type::kNode ) return std::is_base_of<T, Node >::value;
if (t == Type::kShape ) return std::is_base_of<T, Shape >::value;
if (t == Type::kColorPaint) return std::is_base_of<T, ColorPaint>::value;
if (t == Type::kEllipse ) return std::is_base_of<T, Ellipse >::value;
return false;
}
class Artboard final : public SkRefCnt {
public:
ACTOR_ATTR(Name , SkString , SkString() )
ACTOR_ATTR(Color , SkColor4f, SkColors::kBlack)
ACTOR_ATTR(Size , SkV2 , SkV2({0,0}) )
ACTOR_ATTR(Origin , SkV2 , SkV2({0,0}) )
ACTOR_ATTR(Translation , SkV2 , SkV2({0,0}) )
ACTOR_ATTR(ClipContents, bool , false )
ACTOR_ATTR(Root , sk_sp<Node>, nullptr )
ACTOR_ATTR(Name , SkString , SkString() )
ACTOR_ATTR(Color , SkColor4f , SkColors::kBlack)
ACTOR_ATTR(Size , SkV2 , SkV2({0,0}) )
ACTOR_ATTR(Origin , SkV2 , SkV2({0,0}) )
ACTOR_ATTR(Translation , SkV2 , SkV2({0,0}) )
ACTOR_ATTR(ClipContents, bool , false )
void render(SkCanvas*) const;

View File

@ -14,6 +14,7 @@ skia_skrive_sources = [
"$_src/Color.cpp",
"$_src/Component.cpp",
"$_src/Drawable.cpp",
"$_src/Ellipse.cpp",
"$_src/Node.cpp",
"$_src/Paint.cpp",
"$_src/Shape.cpp",

View File

@ -40,6 +40,8 @@ std::tuple<sk_sp<Component>, size_t> parse_component(StreamReader* sr) {
return make_from_stream<ColorPaint>(sr, SkPaint::kFill_Style);
case StreamReader::BlockType::kColorStroke:
return make_from_stream<ColorPaint>(sr, SkPaint::kStroke_Style);
case StreamReader::BlockType::kActorEllipse:
return make_from_stream<Ellipse>(sr);
default:
break;
}
@ -97,7 +99,7 @@ sk_sp<Artboard> parse_artboard(StreamReader* sr) {
switch (block.type()) {
case StreamReader::BlockType::kComponents:
parse_components(sr);
ab->setRoot(parse_components(sr));
break;
default:
SkDebugf("!! Unsupported block type: %d\n", block.type());
@ -114,11 +116,18 @@ sk_sp<Artboard> parse_artboard(StreamReader* sr) {
} // namespace internal
void Artboard::render(SkCanvas* canvas) const {
SkAutoCanvasRestore acr(canvas, true);
canvas->translate(fTranslation.x, fTranslation.y);
SkPaint paint;
paint.setColor4f(fColor);
const auto rect = SkRect::MakeXYWH(fTranslation.x, fTranslation.y, fSize.x, fSize.y);
canvas->drawRect(rect, paint);
canvas->drawRect(SkRect::MakeWH(fSize.x, fSize.y), paint);
if (fRoot) {
fRoot->revalidate();
}
}
} // namespace skrive

View File

@ -26,6 +26,18 @@ size_t parse_node<Component>(StreamReader* sr, Component* node) {
return parent_index;
}
template <>
size_t parse_node<TransformableComponent>(StreamReader* sr, TransformableComponent* node) {
const auto parent_index = parse_node<Component>(sr, node);
node->setTranslation(sr->readV2("translation"));
node->setRotation(sr->readFloat("rotation"));
node->setScale(sr->readV2("scale"));
node->setOpacity(sr->readFloat("opacity"));
return parent_index;
}
} // namespace internal
void Component::invalidate() {

View File

@ -0,0 +1,32 @@
/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "experimental/skrive/include/SkRive.h"
#include "experimental/skrive/src/reader/StreamReader.h"
namespace skrive {
namespace internal {
template <typename T>
size_t parse_node(StreamReader*, T*);
template <>
size_t parse_node<Ellipse>(StreamReader* sr, Ellipse* node) {
const auto parent_index = parse_node<TransformableComponent>(sr, node);
node->setWidth(sr->readFloat("width"));
node->setHeight(sr->readFloat("height"));
return parent_index;
}
} // namespace internal
void Ellipse::onRevalidate() {}
} // namespace skrive

View File

@ -19,12 +19,8 @@ size_t parse_node(StreamReader*, T*);
template <>
size_t parse_node<Node>(StreamReader* sr, Node* node) {
const auto parent_index = parse_node<Component>(sr, node);
const auto parent_index = parse_node<TransformableComponent>(sr, node);
node->setTranslation(sr->readV2("translation"));
node->setRotation(sr->readFloat("rotation"));
node->setScale(sr->readV2("scale"));
node->setOpacity(sr->readFloat("opacity"));
node->setCollapsedVisibility(sr->readBool("isCollapsed"));
if (sr->openArray("clips")) {

View File

@ -18,29 +18,46 @@ DEF_TEST(SkRive_DomTypes, reporter) {
{
auto node = sk_make_sp<skrive::Node>();
check_type<skrive::Component>(reporter, node, true);
check_type<skrive::Node >(reporter, node, true);
check_type<skrive::Drawable >(reporter, node, false);
check_type<skrive::Shape >(reporter, node, false);
check_type<skrive::Component >(reporter, node, true);
check_type<skrive::TransformableComponent>(reporter, node, true);
check_type<skrive::Node >(reporter, node, true);
check_type<skrive::Drawable >(reporter, node, false);
check_type<skrive::Shape >(reporter, node, false);
}
{
auto node = sk_make_sp<skrive::Shape>();
check_type<skrive::Component>(reporter, node, true);
check_type<skrive::Node >(reporter, node, true);
check_type<skrive::Drawable >(reporter, node, true);
check_type<skrive::Shape >(reporter, node, true);
check_type<skrive::Component >(reporter, node, true);
check_type<skrive::TransformableComponent>(reporter, node, true);
check_type<skrive::Node >(reporter, node, true);
check_type<skrive::Drawable >(reporter, node, true);
check_type<skrive::Shape >(reporter, node, true);
}
{
auto node = sk_make_sp<skrive::ColorPaint>(SkPaint::Style::kFill_Style);
check_type<skrive::Component >(reporter, node, true);
check_type<skrive::Node >(reporter, node, false);
check_type<skrive::Drawable >(reporter, node, false);
check_type<skrive::Shape >(reporter, node, false);
check_type<skrive::Paint >(reporter, node, true );
check_type<skrive::ColorPaint>(reporter, node, true );
check_type<skrive::Component >(reporter, node, true);
check_type<skrive::TransformableComponent>(reporter, node, false);
check_type<skrive::Node >(reporter, node, false);
check_type<skrive::Drawable >(reporter, node, false);
check_type<skrive::Shape >(reporter, node, false);
check_type<skrive::Paint >(reporter, node, true );
check_type<skrive::ColorPaint >(reporter, node, true );
}
{
auto node = sk_make_sp<skrive::Ellipse>();
check_type<skrive::Component >(reporter, node, true);
check_type<skrive::TransformableComponent>(reporter, node, true);
check_type<skrive::Node >(reporter, node, false);
check_type<skrive::Drawable >(reporter, node, false);
check_type<skrive::Shape >(reporter, node, false);
check_type<skrive::Paint >(reporter, node, false );
check_type<skrive::ColorPaint >(reporter, node, false );
check_type<skrive::Geometry >(reporter, node, true);
check_type<skrive::Ellipse >(reporter, node, true);
}
}