aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/skottie/SkottieParser.cpp
blob: 24603bf828e6f9def4e49c6c990a0d269038289a (plain)
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
/*
 * 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 "SkottieParser.h"

#include "SkJSONCPP.h"
#include "SkScalar.h"
#include "SkPath.h"
#include "SkPoint.h"
#include "SkString.h"

#include <vector>

namespace skottie {

template <>
bool Parse<SkScalar>(const Json::Value& jv, SkScalar* v) {
    // Some versions wrap values as single-element arrays.
    if (jv.isArray() && jv.size() == 1) {
        return Parse(jv[0], v);
    }

    if (jv.isNull() || !jv.isConvertibleTo(Json::realValue))
        return false;

    *v = jv.asFloat();

    return true;
}

template <>
bool Parse<bool>(const Json::Value& jv, bool* v) {
    if (jv.isNull() || !jv.isConvertibleTo(Json::booleanValue))
        return false;

    *v = jv.asBool();

    return true;
}

template <>
bool Parse<int>(const Json::Value& jv, int* v) {
    if (jv.isNull() || !jv.isConvertibleTo(Json::intValue))
        return false;

    *v = jv.asInt();

    return true;
}

template <>
bool Parse<SkString>(const Json::Value& jv, SkString* v) {
    if (jv.isNull() || !jv.isConvertibleTo(Json::stringValue))
        return false;

    v->set(jv.isString() ? jv.asCString() : jv.asString().c_str());

    return true;
}

template <>
bool Parse<SkPoint>(const Json::Value& jv, SkPoint* v) {
    if (!jv.isObject())
        return false;

    const auto& jvx = jv["x"],
                jvy = jv["y"];

    // Some BM versions seem to store x/y as single-element arrays.
    return Parse(jvx.isArray() ? jvx[0] : jvx, &v->fX)
        && Parse(jvy.isArray() ? jvy[0] : jvy, &v->fY);
}

template <>
bool Parse<std::vector<float>>(const Json::Value& jv, std::vector<float>* v) {
    if (!jv.isArray())
        return false;

    v->resize(jv.size());

    for (Json::ArrayIndex i = 0; i < jv.size(); ++i) {
        if (!Parse(jv[i], v->data() + i)) {
            return false;
        }
    }

    return true;
}

namespace {

bool ParsePointVec(const Json::Value& jv, std::vector<SkPoint>* pts) {
    if (!jv.isArray())
        return false;

    pts->clear();
    pts->reserve(jv.size());

    std::vector<float> vec;
    for (Json::ArrayIndex i = 0; i < jv.size(); ++i) {
        if (!Parse(jv[i], &vec) || vec.size() != 2)
            return false;
        pts->push_back(SkPoint::Make(vec[0], vec[1]));
    }

    return true;
}

} // namespace

template <>
bool Parse<SkPath>(const Json::Value& jv, SkPath* v) {
    SkASSERT(v->isEmpty());

    // Some versions wrap values as single-element arrays.
    if (jv.isArray() && jv.size() == 1) {
        return Parse(jv[0], 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 (!jv.isObject() ||
        !ParsePointVec(jv["i"], &inPts) ||
        !ParsePointVec(jv["o"], &outPts) ||
        !ParsePointVec(jv["v"], &verts) ||
        inPts.size() != outPts.size() ||
        inPts.size() != verts.size()) {

        return false;
    }

    if (!verts.empty()) {
        v->moveTo(verts.front());
    }

    const auto& addCubic = [&](size_t from, size_t to) {
        v->cubicTo(verts[from] + outPts[from],
                   verts[to]   + inPts[to],
                   verts[to]);
    };

    for (size_t i = 1; i < verts.size(); ++i) {
        addCubic(i - 1, i);
    }

    if (!verts.empty() && ParseDefault(jv["c"], false)) {
        addCubic(verts.size() - 1, 0);
        v->close();
    }

    return true;
}

} // nasmespace skottie