Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkottieJson.cpp
Go to the documentation of this file.
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
9
10#include "include/core/SkM44.h"
16#include "src/utils/SkJSON.h"
17
18#include <cstddef>
19#include <limits>
20
21namespace skottie {
22
23using namespace skjson;
24
25template <>
26bool Parse<SkScalar>(const Value& v, SkScalar* s) {
27 // Some versions wrap values as single-element arrays.
28 if (const skjson::ArrayValue* array = v) {
29 if (array->size() > 0) {
30 return Parse((*array)[0], s);
31 }
32 }
33
34 if (const skjson::NumberValue* num = v) {
35 *s = static_cast<SkScalar>(**num);
36 return true;
37 }
38
39 return false;
40}
41
42template <>
43bool Parse<bool>(const Value& v, bool* b) {
44 switch(v.getType()) {
45 case Value::Type::kNumber:
46 *b = SkToBool(*v.as<NumberValue>());
47 return true;
48 case Value::Type::kBool:
49 *b = *v.as<BoolValue>();
50 return true;
51 default:
52 break;
53 }
54
55 return false;
56}
57
58template <typename T>
59bool ParseIntegral(const Value& v, T* result) {
60 if (const skjson::NumberValue* num = v) {
61 const auto dbl = **num;
62 if (dbl > static_cast<double>(std::numeric_limits<T>::max()) ||
63 dbl < static_cast<double>(std::numeric_limits<T>::min())) {
64 return false;
65 }
66
67 *result = static_cast<T>(dbl);
68 return true;
69 }
70
71 return false;
72}
73
74template <>
75bool Parse<int>(const Value& v, int* i) {
76 return ParseIntegral(v, i);
77}
78
79template <>
80bool Parse<size_t>(const Value& v, size_t* sz) {
81 return ParseIntegral(v, sz);
82}
83
84template <>
85bool Parse<SkString>(const Value& v, SkString* s) {
86 if (const skjson::StringValue* sv = v) {
87 s->set(sv->begin(), sv->size());
88 return true;
89 }
90
91 return false;
92}
93
94template <>
95bool Parse<SkV2>(const Value& v, SkV2* v2) {
96 if (!v.is<ArrayValue>())
97 return false;
98 const auto& av = v.as<ArrayValue>();
99
100 // We need at least two scalars (BM sometimes exports a third value == 0).
101 return av.size() >= 2
102 && Parse<SkScalar>(av[0], &v2->x)
103 && Parse<SkScalar>(av[1], &v2->y);
104}
105
106template <>
107bool Parse<SkPoint>(const Value& v, SkPoint* pt) {
108 if (!v.is<ObjectValue>())
109 return false;
110 const auto& ov = v.as<ObjectValue>();
111
112 return Parse<SkScalar>(ov["x"], &pt->fX)
113 && Parse<SkScalar>(ov["y"], &pt->fY);
114}
115
116template <>
118 if (!v.is<ArrayValue>())
119 return false;
120 const auto& av = v.as<ArrayValue>();
121
122 vec->resize(av.size());
123 for (size_t i = 0; i < av.size(); ++i) {
124 if (!Parse(av[i], vec->data() + i)) {
125 return false;
126 }
127 }
128
129 return true;
130}
131
133 if (jobj) {
134 if (const skjson::StringValue* sid = (*jobj)["sid"]) {
135 return sid;
136 }
137 }
138 return nullptr;
139}
140
141} // namespace skottie
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
Vec2Value v2
const T & as() const
Definition SkJSON.h:85
bool is() const
Definition SkJSON.h:77
Type getType() const
Definition SkJSON.h:371
size_t size() const
Definition SkJSON.h:262
float SkScalar
Definition extension.cpp:12
static bool b
struct MyStruct s
GAsyncResult * result
bool ParseIntegral(const Value &v, T *result)
bool Parse< int >(const Value &v, int *i)
bool Parse< SkScalar >(const Value &v, SkScalar *s)
bool Parse< SkString >(const Value &v, SkString *s)
bool Parse< bool >(const Value &v, bool *b)
bool Parse< size_t >(const Value &v, size_t *sz)
bool Parse< SkPoint >(const Value &v, SkPoint *pt)
bool Parse< VectorValue >(const Value &v, VectorValue *vec)
bool Parse< SkV2 >(const Value &v, SkV2 *v2)
bool Parse(const skjson::Value &, T *)
const skjson::StringValue * ParseSlotID(const skjson::ObjectValue *jobj)
#define T
float fX
x-axis value
float fY
y-axis value
Definition SkM44.h:19
float x
Definition SkM44.h:20
float y
Definition SkM44.h:20