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 #ifndef SkRive_Streamreader_DEFINED
9 #define SkRive_Streamreader_DEFINED
10 
11 #include <memory>
12 
13 #include "include/core/SkColor.h"
14 #include "include/core/SkM44.h"
15 #include "include/core/SkRefCnt.h"
16 
17 class SkStreamAsset;
18 class SkString;
19 
20 namespace skrive::internal {
21 
22 class StreamReader {
23 public:
24     virtual ~StreamReader() = default;
25 
26     static std::unique_ptr<StreamReader> Make(std::unique_ptr<SkStreamAsset>);
27     static std::unique_ptr<StreamReader> Make(const sk_sp<SkData>&);
28 
29     enum class BlockType : uint8_t {
30         kUnknown              =   0,
31         kComponents           =   1,
32         kActorNode            =   2,
33 
34         kActorShape           = 100,
35         kActorPath            = 101,
36         kColorFill            = 102,
37         kColorStroke          = 103,
38         kGradientFill         = 104,
39         kGradientStroke       = 105,
40         kRadialGradientFill   = 106,
41         kRadialGradientStroke = 107,
42         kActorEllipse         = 108,
43         kActorRectangle       = 109,
44         kActorTriangle        = 110,
45         kActorStar            = 111,
46         kActorPolygon         = 112,
47         kActorArtboard        = 114,
48         kArtboards            = 115,
49 
50         // End-of-block marker
51         kEoB           = 0xff,
52     };
53 
54     // Sequential block API
55     virtual BlockType openBlock() = 0;
56     virtual void     closeBlock() = 0;
57 
58     // Keyed API
59     virtual bool   openArray(const char label[]) = 0;
60     virtual void  closeArray()                   = 0;
61     virtual bool  openObject(const char label[]) = 0;
62     virtual void closeObject()                   = 0;
63 
64 
65     virtual uint16_t readId    (const char label[]) = 0;
66     virtual bool     readBool  (const char label[]) = 0;
67     virtual float    readFloat (const char label[]) = 0;
68     virtual uint8_t  readUInt8 (const char label[]) = 0;
69     virtual uint16_t readUInt16(const char label[]) = 0;
70     virtual uint32_t readUInt32(const char label[]) = 0;
71     virtual SkString readString(const char label[]) = 0;
72 
73     virtual uint8_t  readLength8 () = 0;
74     virtual uint16_t readLength16() = 0;
75 
76     SkColor4f readColor(const char label[]);
77     SkV2      readV2(const char label[]);
78 
79     class AutoBlock final {
80     public:
AutoBlock(StreamReader * reader)81         explicit AutoBlock(StreamReader* reader)
82             : fReader(reader)
83             , fType(reader->openBlock()) {}
84 
AutoBlock(const std::unique_ptr<StreamReader> & reader)85         explicit AutoBlock(const std::unique_ptr<StreamReader>& reader)
86             : fReader(reader.get())
87             , fType(reader->openBlock()) {}
88 
~AutoBlock()89         ~AutoBlock() {
90             if (fType != BlockType::kEoB) {
91                 fReader->closeBlock();
92             }
93         }
94 
type()95         BlockType type() const { return fType; }
96 
97     private:
98         StreamReader* fReader;
99         BlockType     fType;
100     };
101 
102 protected:
103     virtual size_t readFloatArray(const char label[], float dst[], size_t count) = 0;
104 };
105 
106 }  // namespace skrive::internal
107 
108 #endif // SkRiveStreamreader_DEFINED
109