blob: 4b075dd2f2404bed70de464c639ed401970b47f2 [file] [log] [blame]
brianosmanfad98562016-05-04 11:06:28 -07001/*
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::writeByteArray(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("byteArray", jsonArray);
26}
27
28void SkJsonWriteBuffer::writeBool(bool value) {
29 this->append("bool", value);
30}
31
32void SkJsonWriteBuffer::writeScalar(SkScalar value) {
33 this->append("scalar", value);
34}
35
36void SkJsonWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) {
37 Json::Value jsonArray(Json::arrayValue);
38 for (uint32_t i = 0; i < count; ++i) {
39 jsonArray.append(value[i]);
40 }
41 this->append("scalarArray", jsonArray);
42}
43
44void SkJsonWriteBuffer::writeInt(int32_t value) {
45 this->append("int", value);
46}
47
48void SkJsonWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) {
49 Json::Value jsonArray(Json::arrayValue);
50 for (uint32_t i = 0; i < count; ++i) {
51 jsonArray.append(value[i]);
52 }
53 this->append("intArray", jsonArray);
54}
55
56void SkJsonWriteBuffer::writeUInt(uint32_t value) {
57 this->append("uint", value);
58}
59
60void SkJsonWriteBuffer::writeString(const char* value) {
61 this->append("string", value);
62}
63
64void SkJsonWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
65 if (flattenable) {
66 SkJsonWriteBuffer flattenableBuffer(fUrlDataManager);
67 flattenable->flatten(flattenableBuffer);
68 this->append(flattenable->getTypeName(), flattenableBuffer.getValue());
69 } else {
70 this->append("flattenable", Json::Value());
71 }
72}
73
74void SkJsonWriteBuffer::writeColor(SkColor color) {
75 this->append("color", SkDrawCommand::MakeJsonColor(color));
76}
77
78void SkJsonWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) {
79 Json::Value jsonArray(Json::arrayValue);
80 for (uint32_t i = 0; i < count; ++i) {
81 jsonArray.append(SkDrawCommand::MakeJsonColor(color[i]));
82 }
83 this->append("colorArray", jsonArray);
84}
85
brianosman97bbf822016-09-25 13:15:58 -070086void SkJsonWriteBuffer::writeColor4f(const SkColor4f& color) {
87 this->append("color", SkDrawCommand::MakeJsonColor4f(color));
88}
89
90void SkJsonWriteBuffer::writeColor4fArray(const SkColor4f* color, uint32_t count) {
91 Json::Value jsonArray(Json::arrayValue);
92 for (uint32_t i = 0; i < count; ++i) {
93 jsonArray.append(SkDrawCommand::MakeJsonColor4f(color[i]));
94 }
95 this->append("colorArray", jsonArray);
96}
97
brianosmanfad98562016-05-04 11:06:28 -070098void SkJsonWriteBuffer::writePoint(const SkPoint& point) {
99 this->append("point", SkDrawCommand::MakeJsonPoint(point));
100}
101
102void SkJsonWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) {
103 Json::Value jsonArray(Json::arrayValue);
104 for (uint32_t i = 0; i < count; ++i) {
105 jsonArray.append(SkDrawCommand::MakeJsonPoint(point[i]));
106 }
107 this->append("pointArray", jsonArray);
108}
109
110void SkJsonWriteBuffer::writeMatrix(const SkMatrix& matrix) {
111 this->append("matrix", SkDrawCommand::MakeJsonMatrix(matrix));
112}
113
114void SkJsonWriteBuffer::writeIRect(const SkIRect& rect) {
115 this->append("irect", SkDrawCommand::MakeJsonIRect(rect));
116}
117
118void SkJsonWriteBuffer::writeRect(const SkRect& rect) {
119 this->append("rect", SkDrawCommand::MakeJsonRect(rect));
120}
121
122void SkJsonWriteBuffer::writeRegion(const SkRegion& region) {
123 this->append("region", SkDrawCommand::MakeJsonRegion(region));
124}
125
126void SkJsonWriteBuffer::writePath(const SkPath& path) {
127 this->append("path", SkDrawCommand::MakeJsonPath(path));
128}
129
130size_t SkJsonWriteBuffer::writeStream(SkStream* stream, size_t length) {
131 // Contents not supported
132 SkASSERT(length < Json::Value::maxUInt);
133 this->append("stream", static_cast<Json::UInt>(length));
134 return 0;
135}
136
137void SkJsonWriteBuffer::writeBitmap(const SkBitmap& bitmap) {
138 Json::Value jsonBitmap;
139 SkDrawCommand::flatten(bitmap, &jsonBitmap, *fUrlDataManager);
140 this->append("bitmap", jsonBitmap);
141}
142
143void SkJsonWriteBuffer::writeImage(const SkImage* image) {
144 Json::Value jsonImage;
145 SkDrawCommand::flatten(*image, &jsonImage, *fUrlDataManager);
146 this->append("image", jsonImage);
147}
148
149void SkJsonWriteBuffer::writeTypeface(SkTypeface* typeface) {
150 // Unsupported
151 this->append("typeface", Json::Value());
152}
153
154void SkJsonWriteBuffer::writePaint(const SkPaint& paint) {
155 this->append("paint", SkDrawCommand::MakeJsonPaint(paint, *fUrlDataManager));
156}