1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkottieJson.h"
#include "SkData.h"
#include "SkScalar.h"
#include "SkPath.h"
#include "SkPoint.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkottieValue.h"
#include "rapidjson/error/en.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
#include <vector>
namespace skottie {
namespace json {
template <>
bool ValueRef::to<SkScalar>(SkScalar* v) const {
if (!fValue) return false;
// Some versions wrap values as single-element arrays.
if (fValue->IsArray() && fValue->Size() == 1) {
return ValueRef(fValue->operator[](0)).to(v);
}
if (!fValue->IsNumber())
return false;
*v = static_cast<SkScalar>(fValue->GetDouble());
return true;
}
template <>
bool ValueRef::to<bool>(bool* v) const {
if (!fValue) return false;
switch(fValue->GetType()) {
case rapidjson::kNumberType:
*v = SkToBool(fValue->GetDouble());
return true;
case rapidjson::kFalseType:
case rapidjson::kTrueType:
*v = fValue->GetBool();
return true;
default:
break;
}
return false;
}
template <>
bool ValueRef::to<int>(int* v) const {
if (!fValue || !fValue->IsInt())
return false;
*v = fValue->GetInt();
return true;
}
template <>
bool ValueRef::to<SkString>(SkString* v) const {
if (!fValue || !fValue->IsString())
return false;
v->set(fValue->GetString());
return true;
}
template <>
bool ValueRef::to<SkPoint>(SkPoint* v) const {
if (!fValue || !fValue->IsObject())
return false;
const auto jvx = ValueRef(this->operator[]("x")),
jvy = ValueRef(this->operator[]("y"));
// Some BM versions seem to store x/y as single-element arrays.
return ValueRef(jvx.isArray() ? jvx.operator[](size_t(0)) : jvx).to(&v->fX)
&& ValueRef(jvy.isArray() ? jvy.operator[](size_t(0)) : jvy).to(&v->fY);
}
template <>
bool ValueRef::to<std::vector<float>>(std::vector<float>* v) const {
if (!fValue || !fValue->IsArray())
return false;
v->resize(fValue->Size());
for (size_t i = 0; i < fValue->Size(); ++i) {
if (!ValueRef(fValue->operator[](i)).to(v->data() + i)) {
return false;
}
}
return true;
}
namespace {
bool ParsePointVec(const ValueRef& jv, std::vector<SkPoint>* pts) {
if (!jv.isArray())
return false;
pts->clear();
pts->reserve(jv.size());
std::vector<float> vec;
for (size_t i = 0; i < jv.size(); ++i) {
if (!jv[i].to(&vec) || vec.size() != 2)
return false;
pts->push_back(SkPoint::Make(vec[0], vec[1]));
}
return true;
}
} // namespace
template <>
bool ValueRef::to<ShapeValue>(ShapeValue* v) const {
SkASSERT(v->fVertices.empty());
if (!fValue)
return false;
// Some versions wrap values as single-element arrays.
if (fValue->IsArray() && fValue->Size() == 1) {
return ValueRef(fValue->operator[](0)).to(v);
}
std::vector<SkPoint> inPts, // Cubic Bezier "in" control points, relative to vertices.
outPts, // Cubic Bezier "out" control points, relative to vertices.
verts; // Cubic Bezier vertices.
if (!fValue->IsObject() ||
!ParsePointVec(this->operator[]("i"), &inPts) ||
!ParsePointVec(this->operator[]("o"), &outPts) ||
!ParsePointVec(this->operator[]("v"), &verts) ||
inPts.size() != outPts.size() ||
inPts.size() != verts.size()) {
return false;
}
v->fVertices.reserve(inPts.size());
for (size_t i = 0; i < inPts.size(); ++i) {
v->fVertices.push_back(BezierVertex({inPts[i], outPts[i], verts[i]}));
}
v->fClosed = this->operator[]("c").toDefault<bool>(false);
return true;
}
size_t ValueRef::size() const {
return this->isArray() ? fValue->Size() : 0;
}
ValueRef ValueRef::operator[](size_t i) const {
return i < this->size() ? ValueRef(fValue->operator[](i)) : ValueRef();
}
ValueRef ValueRef::operator[](const char* key) const {
if (!this->isObject())
return ValueRef();
const auto m = fValue->FindMember(key);
return m == fValue->MemberEnd() ? ValueRef() : ValueRef(m->value);
}
const rapidjson::Value* ValueRef::begin() const {
return this->isArray() ? fValue->Begin() : nullptr;
}
const rapidjson::Value* ValueRef::end() const {
return this->isArray() ? fValue->End() : nullptr;
}
SkString ValueRef::toString() const {
#ifdef SK_DEBUG
rapidjson::StringBuffer buf;
if (fValue) {
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
fValue->Accept(writer);
}
return SkString(buf.GetString());
#else
return SkString();
#endif // SK_DEBUG
}
Document::Document(SkStream* stream) {
if (!stream->hasLength()) {
SkDebugf("!! unsupported unseekable json stream\n");
return;
}
// RapidJSON provides three DOM-builder approaches:
//
// 1) in-place : all data buffered, constructs the DOM in-place -- this is the fastest
// 2) from buffer: all data buffered, copies to DOM -- this is slightly slower
// 3) from stream: streamed data, reads/copies to DOM -- this is *significantly* slower
//
// We like fast, so #1 it is.
// The buffer needs to be C-string.
const auto size = stream->getLength();
fData = SkData::MakeUninitialized(size + 1);
if (stream->read(fData->writable_data(), size) < size) {
SkDebugf("!! could not read JSON stream\n");
return;
}
auto data = static_cast<char*>(fData->writable_data());
data[size] = '\0';
fDocument.ParseInsitu(data);
#ifdef SK_DEBUG
if (fDocument.HasParseError()) {
SkDebugf("!! failed to parse json: %s\n",
rapidjson::GetParseError_En(fDocument.GetParseError()));
}
#endif
}
} // namespace json
} // namespace skottie
|