• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkStream.h"
12 
13 namespace skrive {
14 
15 namespace internal {
16 
17 extern sk_sp<Artboard> parse_artboard(StreamReader*);
18 
parse_artboards(const sk_sp<SkRive> & skrive,StreamReader * sr)19 void parse_artboards(const sk_sp<SkRive>& skrive, StreamReader* sr) {
20     const size_t artboard_count = sr->readLength16();
21     skrive->artboards().reserve(artboard_count);
22 
23     for (size_t i = 0; i < artboard_count; ++i) {
24         StreamReader::AutoBlock block(sr);
25         if (block.type() == StreamReader::BlockType::kEoB) {
26             break;
27         }
28         if (block.type() != StreamReader::BlockType::kActorArtboard) {
29             SkDebugf("!! Unexpected artboard block type: %d\n", (int)block.type());
30             continue;
31         }
32 
33         skrive->artboards().push_back(parse_artboard(sr));
34     }
35 }
36 
parse_skrive(std::unique_ptr<StreamReader> sr)37 static sk_sp<SkRive> parse_skrive(std::unique_ptr<StreamReader> sr) {
38     if (!sr) {
39         return nullptr;
40     }
41 
42     const auto version = sr->readUInt32("version");
43     SkDebugf(".. loading version %d\n", version);
44 
45     auto skrive = sk_make_sp<SkRive>();
46 
47     for (;;) {
48         StreamReader::AutoBlock block(sr);
49         if (block.type() == StreamReader::BlockType::kEoB) {
50             break;
51         }
52 
53         switch (block.type()) {
54         case StreamReader::BlockType::kArtboards:
55             parse_artboards(skrive, sr.get());
56             break;
57         default:
58             SkDebugf("!! Unsupported block type: %d\n", (int)block.type());
59             break;
60         }
61     }
62 
63     return skrive;
64 }
65 
66 } // namespace internal
67 
make(std::unique_ptr<SkStreamAsset> stream)68 sk_sp<SkRive> SkRive::Builder::make(std::unique_ptr<SkStreamAsset> stream) {
69     auto   reader = internal::StreamReader::Make(std::move(stream));
70 
71     return reader ? parse_skrive(std::move(reader))
72                   : nullptr;
73 }
74 
75 } // namespace skrive
76