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
append(const char * type,const Json::Value & value)13 void 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
writeByteArray(const void * data,size_t size)18 void 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
writeBool(bool value)28 void SkJsonWriteBuffer::writeBool(bool value) {
29 this->append("bool", value);
30 }
31
writeScalar(SkScalar value)32 void SkJsonWriteBuffer::writeScalar(SkScalar value) {
33 this->append("scalar", value);
34 }
35
writeScalarArray(const SkScalar * value,uint32_t count)36 void 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
writeInt(int32_t value)44 void SkJsonWriteBuffer::writeInt(int32_t value) {
45 this->append("int", value);
46 }
47
writeIntArray(const int32_t * value,uint32_t count)48 void 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
writeUInt(uint32_t value)56 void SkJsonWriteBuffer::writeUInt(uint32_t value) {
57 this->append("uint", value);
58 }
59
writeString(const char * value)60 void SkJsonWriteBuffer::writeString(const char* value) {
61 this->append("string", value);
62 }
63
writeFlattenable(const SkFlattenable * flattenable)64 void 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
writeColor(SkColor color)74 void SkJsonWriteBuffer::writeColor(SkColor color) {
75 this->append("color", SkDrawCommand::MakeJsonColor(color));
76 }
77
writeColorArray(const SkColor * color,uint32_t count)78 void 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
writeColor4f(const SkColor4f & color)86 void SkJsonWriteBuffer::writeColor4f(const SkColor4f& color) {
87 this->append("color", SkDrawCommand::MakeJsonColor4f(color));
88 }
89
writeColor4fArray(const SkColor4f * color,uint32_t count)90 void 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
writePoint(const SkPoint & point)98 void SkJsonWriteBuffer::writePoint(const SkPoint& point) {
99 this->append("point", SkDrawCommand::MakeJsonPoint(point));
100 }
101
writePointArray(const SkPoint * point,uint32_t count)102 void 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
writeMatrix(const SkMatrix & matrix)110 void SkJsonWriteBuffer::writeMatrix(const SkMatrix& matrix) {
111 this->append("matrix", SkDrawCommand::MakeJsonMatrix(matrix));
112 }
113
writeIRect(const SkIRect & rect)114 void SkJsonWriteBuffer::writeIRect(const SkIRect& rect) {
115 this->append("irect", SkDrawCommand::MakeJsonIRect(rect));
116 }
117
writeRect(const SkRect & rect)118 void SkJsonWriteBuffer::writeRect(const SkRect& rect) {
119 this->append("rect", SkDrawCommand::MakeJsonRect(rect));
120 }
121
writeRegion(const SkRegion & region)122 void SkJsonWriteBuffer::writeRegion(const SkRegion& region) {
123 this->append("region", SkDrawCommand::MakeJsonRegion(region));
124 }
125
writePath(const SkPath & path)126 void SkJsonWriteBuffer::writePath(const SkPath& path) {
127 this->append("path", SkDrawCommand::MakeJsonPath(path));
128 }
129
writeStream(SkStream * stream,size_t length)130 size_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
writeBitmap(const SkBitmap & bitmap)137 void SkJsonWriteBuffer::writeBitmap(const SkBitmap& bitmap) {
138 Json::Value jsonBitmap;
139 SkDrawCommand::flatten(bitmap, &jsonBitmap, *fUrlDataManager);
140 this->append("bitmap", jsonBitmap);
141 }
142
writeImage(const SkImage * image)143 void SkJsonWriteBuffer::writeImage(const SkImage* image) {
144 Json::Value jsonImage;
145 SkDrawCommand::flatten(*image, &jsonImage, *fUrlDataManager);
146 this->append("image", jsonImage);
147 }
148
writeTypeface(SkTypeface * typeface)149 void SkJsonWriteBuffer::writeTypeface(SkTypeface* typeface) {
150 // Unsupported
151 this->append("typeface", Json::Value());
152 }
153
writePaint(const SkPaint & paint)154 void SkJsonWriteBuffer::writePaint(const SkPaint& paint) {
155 this->append("paint", SkDrawCommand::MakeJsonPaint(paint, *fUrlDataManager));
156 }
157