1 /*
2 * Copyright 2018 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 "SkottieJson.h"
9
10 #include "SkData.h"
11 #include "SkScalar.h"
12 #include "SkPath.h"
13 #include "SkPoint.h"
14 #include "SkStream.h"
15 #include "SkString.h"
16 #include "SkottieValue.h"
17 #include <vector>
18
19 namespace skottie {
20
21 using namespace skjson;
22
23 template <>
Parse(const Value & v,SkScalar * s)24 bool Parse<SkScalar>(const Value& v, SkScalar* s) {
25 // Some versions wrap values as single-element arrays.
26 if (const skjson::ArrayValue* array = v) {
27 if (array->size() > 0) {
28 return Parse((*array)[0], s);
29 }
30 }
31
32 if (const skjson::NumberValue* num = v) {
33 *s = static_cast<SkScalar>(**num);
34 return true;
35 }
36
37 return false;
38 }
39
40 template <>
Parse(const Value & v,bool * b)41 bool Parse<bool>(const Value& v, bool* b) {
42 switch(v.getType()) {
43 case Value::Type::kNumber:
44 *b = SkToBool(*v.as<NumberValue>());
45 return true;
46 case Value::Type::kBool:
47 *b = *v.as<BoolValue>();
48 return true;
49 default:
50 break;
51 }
52
53 return false;
54 }
55
56 template <typename T>
ParseIntegral(const Value & v,T * result)57 bool ParseIntegral(const Value& v, T* result) {
58 if (const skjson::NumberValue* num = v) {
59 const auto dbl = **num;
60 *result = static_cast<T>(dbl);
61 return static_cast<double>(*result) == dbl;
62 }
63
64 return false;
65 }
66
67 template <>
Parse(const Value & v,int * i)68 bool Parse<int>(const Value& v, int* i) {
69 return ParseIntegral(v, i);
70 }
71
72 template <>
Parse(const Value & v,size_t * sz)73 bool Parse<size_t>(const Value& v, size_t* sz) {
74 return ParseIntegral(v, sz);
75 }
76
77 template <>
Parse(const Value & v,SkString * s)78 bool Parse<SkString>(const Value& v, SkString* s) {
79 if (const skjson::StringValue* sv = v) {
80 s->set(sv->begin(), sv->size());
81 return true;
82 }
83
84 return false;
85 }
86
87 template <>
Parse(const Value & v,SkPoint * pt)88 bool Parse<SkPoint>(const Value& v, SkPoint* pt) {
89 if (!v.is<ObjectValue>())
90 return false;
91 const auto& ov = v.as<ObjectValue>();
92
93 return Parse<SkScalar>(ov["x"], &pt->fX)
94 && Parse<SkScalar>(ov["y"], &pt->fY);
95 }
96
97 template <>
Parse(const Value & v,std::vector<float> * vec)98 bool Parse<std::vector<float>>(const Value& v, std::vector<float>* vec) {
99 if (!v.is<ArrayValue>())
100 return false;
101 const auto& av = v.as<ArrayValue>();
102
103 vec->resize(av.size());
104 for (size_t i = 0; i < av.size(); ++i) {
105 if (!Parse(av[i], vec->data() + i)) {
106 return false;
107 }
108 }
109
110 return true;
111 }
112
113 } // namespace skottie
114