OLD | NEW |
(Empty) | |
| 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 |
| 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 |
| 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 |
| 28 void SkJsonWriteBuffer::writeBool(bool value) { |
| 29 this->append("bool", value); |
| 30 } |
| 31 |
| 32 void SkJsonWriteBuffer::writeScalar(SkScalar value) { |
| 33 this->append("scalar", value); |
| 34 } |
| 35 |
| 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 |
| 44 void SkJsonWriteBuffer::writeInt(int32_t value) { |
| 45 this->append("int", value); |
| 46 } |
| 47 |
| 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 |
| 56 void SkJsonWriteBuffer::writeUInt(uint32_t value) { |
| 57 this->append("uint", value); |
| 58 } |
| 59 |
| 60 void SkJsonWriteBuffer::writeString(const char* value) { |
| 61 this->append("string", value); |
| 62 } |
| 63 |
| 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 |
| 74 void SkJsonWriteBuffer::writeColor(SkColor color) { |
| 75 this->append("color", SkDrawCommand::MakeJsonColor(color)); |
| 76 } |
| 77 |
| 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 |
| 86 void SkJsonWriteBuffer::writePoint(const SkPoint& point) { |
| 87 this->append("point", SkDrawCommand::MakeJsonPoint(point)); |
| 88 } |
| 89 |
| 90 void SkJsonWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) { |
| 91 Json::Value jsonArray(Json::arrayValue); |
| 92 for (uint32_t i = 0; i < count; ++i) { |
| 93 jsonArray.append(SkDrawCommand::MakeJsonPoint(point[i])); |
| 94 } |
| 95 this->append("pointArray", jsonArray); |
| 96 } |
| 97 |
| 98 void SkJsonWriteBuffer::writeMatrix(const SkMatrix& matrix) { |
| 99 this->append("matrix", SkDrawCommand::MakeJsonMatrix(matrix)); |
| 100 } |
| 101 |
| 102 void SkJsonWriteBuffer::writeIRect(const SkIRect& rect) { |
| 103 this->append("irect", SkDrawCommand::MakeJsonIRect(rect)); |
| 104 } |
| 105 |
| 106 void SkJsonWriteBuffer::writeRect(const SkRect& rect) { |
| 107 this->append("rect", SkDrawCommand::MakeJsonRect(rect)); |
| 108 } |
| 109 |
| 110 void SkJsonWriteBuffer::writeRegion(const SkRegion& region) { |
| 111 this->append("region", SkDrawCommand::MakeJsonRegion(region)); |
| 112 } |
| 113 |
| 114 void SkJsonWriteBuffer::writePath(const SkPath& path) { |
| 115 this->append("path", SkDrawCommand::MakeJsonPath(path)); |
| 116 } |
| 117 |
| 118 size_t SkJsonWriteBuffer::writeStream(SkStream* stream, size_t length) { |
| 119 // Contents not supported |
| 120 SkASSERT(length < Json::Value::maxUInt); |
| 121 this->append("stream", static_cast<Json::UInt>(length)); |
| 122 return 0; |
| 123 } |
| 124 |
| 125 void SkJsonWriteBuffer::writeBitmap(const SkBitmap& bitmap) { |
| 126 Json::Value jsonBitmap; |
| 127 SkDrawCommand::flatten(bitmap, &jsonBitmap, *fUrlDataManager); |
| 128 this->append("bitmap", jsonBitmap); |
| 129 } |
| 130 |
| 131 void SkJsonWriteBuffer::writeImage(const SkImage* image) { |
| 132 Json::Value jsonImage; |
| 133 SkDrawCommand::flatten(*image, &jsonImage, *fUrlDataManager); |
| 134 this->append("image", jsonImage); |
| 135 } |
| 136 |
| 137 void SkJsonWriteBuffer::writeTypeface(SkTypeface* typeface) { |
| 138 // Unsupported |
| 139 this->append("typeface", Json::Value()); |
| 140 } |
| 141 |
| 142 void SkJsonWriteBuffer::writePaint(const SkPaint& paint) { |
| 143 this->append("paint", SkDrawCommand::MakeJsonPaint(paint, *fUrlDataManager))
; |
| 144 } |
OLD | NEW |