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