1/*
2 * Copyright 2016 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 "SkJsonWriteBuffer.h"
9
10#include "SkDrawCommand.h"
11#include "SkObjectParser.h"
12
13void SkJsonWriteBuffer::append(const char* type, const Json::Value& value) {
14    SkString fullName = SkStringPrintf("%02d_%s", fJson.size(), type);
15    fJson[fullName.c_str()] = value;
16}
17
18void SkJsonWriteBuffer::writePad32(const void* data, size_t size) {
19    Json::Value jsonArray(Json::arrayValue);
20    const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
21    for (size_t i = 0; i < size; ++i) {
22        SkString hexByte = SkStringPrintf("%02x", bytes[i]);
23        jsonArray.append(hexByte.c_str());
24    }
25    this->append("rawBytes", jsonArray);
26}
27
28void SkJsonWriteBuffer::writeByteArray(const void* data, size_t size) {
29    Json::Value jsonArray(Json::arrayValue);
30    const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
31    for (size_t i = 0; i < size; ++i) {
32        SkString hexByte = SkStringPrintf("%02x", bytes[i]);
33        jsonArray.append(hexByte.c_str());
34    }
35    this->append("byteArray", jsonArray);
36}
37
38void SkJsonWriteBuffer::writeBool(bool value) {
39    this->append("bool", value);
40}
41
42void SkJsonWriteBuffer::writeScalar(SkScalar value) {
43    this->append("scalar", value);
44}
45
46void SkJsonWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) {
47    Json::Value jsonArray(Json::arrayValue);
48    for (uint32_t i = 0; i < count; ++i) {
49        jsonArray.append(value[i]);
50    }
51    this->append("scalarArray", jsonArray);
52}
53
54void SkJsonWriteBuffer::writeInt(int32_t value) {
55    this->append("int", value);
56}
57
58void SkJsonWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) {
59    Json::Value jsonArray(Json::arrayValue);
60    for (uint32_t i = 0; i < count; ++i) {
61        jsonArray.append(value[i]);
62    }
63    this->append("intArray", jsonArray);
64}
65
66void SkJsonWriteBuffer::writeUInt(uint32_t value) {
67    this->append("uint", value);
68}
69
70void SkJsonWriteBuffer::writeString(const char* value) {
71    this->append("string", value);
72}
73
74void SkJsonWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
75    if (flattenable) {
76        SkJsonWriteBuffer flattenableBuffer(fUrlDataManager);
77        flattenable->flatten(flattenableBuffer);
78        this->append(flattenable->getTypeName(), flattenableBuffer.getValue());
79    } else {
80        this->append("flattenable", Json::Value());
81    }
82}
83
84void SkJsonWriteBuffer::writeColor(SkColor color) {
85    this->append("color", SkDrawCommand::MakeJsonColor(color));
86}
87
88void SkJsonWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) {
89    Json::Value jsonArray(Json::arrayValue);
90    for (uint32_t i = 0; i < count; ++i) {
91        jsonArray.append(SkDrawCommand::MakeJsonColor(color[i]));
92    }
93    this->append("colorArray", jsonArray);
94}
95
96void SkJsonWriteBuffer::writeColor4f(const SkColor4f& color) {
97    this->append("color", SkDrawCommand::MakeJsonColor4f(color));
98}
99
100void SkJsonWriteBuffer::writeColor4fArray(const SkColor4f* color, uint32_t count) {
101    Json::Value jsonArray(Json::arrayValue);
102    for (uint32_t i = 0; i < count; ++i) {
103        jsonArray.append(SkDrawCommand::MakeJsonColor4f(color[i]));
104    }
105    this->append("colorArray", jsonArray);
106}
107
108void SkJsonWriteBuffer::writePoint(const SkPoint& point) {
109    this->append("point", SkDrawCommand::MakeJsonPoint(point));
110}
111
112void SkJsonWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) {
113    Json::Value jsonArray(Json::arrayValue);
114    for (uint32_t i = 0; i < count; ++i) {
115        jsonArray.append(SkDrawCommand::MakeJsonPoint(point[i]));
116    }
117    this->append("pointArray", jsonArray);
118}
119
120void SkJsonWriteBuffer::writeMatrix(const SkMatrix& matrix) {
121    this->append("matrix", SkDrawCommand::MakeJsonMatrix(matrix));
122}
123
124void SkJsonWriteBuffer::writeIRect(const SkIRect& rect) {
125    this->append("irect", SkDrawCommand::MakeJsonIRect(rect));
126}
127
128void SkJsonWriteBuffer::writeRect(const SkRect& rect) {
129    this->append("rect", SkDrawCommand::MakeJsonRect(rect));
130}
131
132void SkJsonWriteBuffer::writeRegion(const SkRegion& region) {
133    this->append("region", SkDrawCommand::MakeJsonRegion(region));
134}
135
136void SkJsonWriteBuffer::writePath(const SkPath& path) {
137    this->append("path", SkDrawCommand::MakeJsonPath(path));
138}
139
140size_t SkJsonWriteBuffer::writeStream(SkStream* stream, size_t length) {
141    // Contents not supported
142    SkASSERT(length < Json::Value::maxUInt);
143    this->append("stream", static_cast<Json::UInt>(length));
144    return 0;
145}
146
147void SkJsonWriteBuffer::writeImage(const SkImage* image) {
148    Json::Value jsonImage;
149    SkDrawCommand::flatten(*image, &jsonImage, *fUrlDataManager);
150    this->append("image", jsonImage);
151}
152
153void SkJsonWriteBuffer::writeTypeface(SkTypeface* typeface) {
154    // Unsupported
155    this->append("typeface", Json::Value());
156}
157
158void SkJsonWriteBuffer::writePaint(const SkPaint& paint) {
159    this->append("paint", SkDrawCommand::MakeJsonPaint(paint, *fUrlDataManager));
160}
161