1 /*
2 * Copyright 2020 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "experimental/skrive/include/SkRive.h"
9
10 #include "experimental/skrive/src/reader/StreamReader.h"
11 #include "include/core/SkPaint.h"
12
13 namespace skrive {
14 namespace internal {
15
16 template <typename T>
17 size_t parse_node(StreamReader*, T*);
18
19 template <>
parse_node(StreamReader * sr,Shape * node)20 size_t parse_node<Shape>(StreamReader* sr, Shape* node) {
21 const auto parent_id = parse_node<Drawable>(sr, node);
22
23 node->setTransformAffectsStroke(sr->readBool("transformAffectsStroke"));
24
25 return parent_id;
26 }
27
28 } // namespace internal
29
onRevalidate()30 void Shape::onRevalidate() {
31 this->INHERITED::onRevalidate();
32
33 fFills.clear();
34 fStrokes.clear();
35 fGeometries.clear();
36
37 for (const auto& child : this->children()) {
38 if (const Paint* paint = *child) {
39 SkASSERT(paint->style() == SkPaint::kFill_Style ||
40 paint->style() == SkPaint::kStroke_Style);
41
42 auto& bucket = paint->style() == SkPaint::kFill_Style ? fFills : fStrokes;
43 bucket.push_back(paint);
44 } else if (const Geometry* geo = *child) {
45 fGeometries.push_back(geo);
46 }
47 }
48
49 SkDebugf("[Shape::onRevalidate] %zu geos %zu fill(s) %zu stroke(s)\n",
50 fGeometries.size(), fFills.size(), fStrokes.size());
51 }
52
onRender(SkCanvas * canvas) const53 void Shape::onRender(SkCanvas* canvas) const {
54 auto draw_paint = [this](SkCanvas* canvas, const Paint* paint) {
55 SkPaint p;
56 paint->apply(&p);
57
58 for (const auto& geo : fGeometries) {
59 geo->draw(canvas, p, paint->getFillRule());
60 }
61 };
62
63 TransformableComponent::ScopedTransformContext stc(this, canvas);
64
65 for (const auto* fill : fFills) {
66 draw_paint(canvas, fill);
67 }
68 for (const auto* stroke : fStrokes) {
69 draw_paint(canvas, stroke);
70 }
71 }
72
73 } // namespace skrive
74