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
12namespace skrive {
13
14namespace internal {
15
16template <typename T>
17size_t parse_node(StreamReader*, T*);
18
19template <>
20size_t parse_node<Paint>(StreamReader* sr, Paint* node) {
21    const auto parent_id = parse_node<Component>(sr, node);
22
23    node->setOpacity(sr->readFloat("opacity"));
24
25    return parent_id;
26}
27
28void parse_fill_stroke(StreamReader* sr, Paint* node) {
29    if (node->style() == SkPaint::kFill_Style) {
30        static constexpr SkPathFillType gFillTypeMap[] = {
31            SkPathFillType::kWinding,  // 0
32            SkPathFillType::kEvenOdd,  // 1
33        };
34        node->setFillRule(gFillTypeMap[std::min<size_t>(sr->readUInt8("fillRule"),
35                                                        SK_ARRAY_COUNT(gFillTypeMap) - 1)]);
36    } else {
37        node->setStrokeWidth(sr->readFloat("width"));
38
39        static constexpr SkPaint::Cap gCapMap[] = {
40            SkPaint::kButt_Cap,   // 0
41            SkPaint::kRound_Cap,  // 1
42            SkPaint::kSquare_Cap, // 2
43        };
44        node->setStrokeCap(gCapMap[std::min<size_t>(sr->readUInt8("cap"),
45                                                    SK_ARRAY_COUNT(gCapMap) - 1)]);
46
47        static constexpr SkPaint::Join gJoinMap[] = {
48            SkPaint::kMiter_Join,  // 0
49            SkPaint::kRound_Join,  // 1
50            SkPaint::kBevel_Join,  // 2
51        };
52        node->setStrokeJoin(gJoinMap[std::min<size_t>(sr->readUInt8("join"),
53                                                      SK_ARRAY_COUNT(gJoinMap) - 1)]);
54
55        static constexpr Paint::StrokeTrim gTrimMap[] = {
56            Paint::StrokeTrim::kOff,         // 0
57            Paint::StrokeTrim::kSequential,  // 1
58            Paint::StrokeTrim::kSynced,      // 2
59        };
60        node->setStrokeTrim(gTrimMap[std::min<size_t>(sr->readUInt8("trim"),
61                                                      SK_ARRAY_COUNT(gTrimMap) - 1)]);
62
63        if (node->getStrokeTrim() != Paint::StrokeTrim::kOff) {
64            node->setStrokeTrimStart (sr->readFloat("start" ));
65            node->setStrokeTrimEnd   (sr->readFloat("end"   ));
66            node->setStrokeTrimOffset(sr->readFloat("offset"));
67        }
68    }
69}
70
71} // namespace internal
72
73void Paint::onApply(SkPaint* paint) const {
74    paint->setAntiAlias(true);
75    paint->setStyle(this->style());
76
77    paint->setStrokeWidth(fStrokeWidth);
78    paint->setStrokeCap  (fStrokeCap  );
79    paint->setStrokeJoin (fStrokeJoin );
80}
81
82} // namespace skrive
83