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" |
| 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 | |
brianosman | 97bbf82 | 2016-09-25 13:15:58 -0700 | [diff] [blame] | 86 | void SkJsonWriteBuffer::writeColor4f(const SkColor4f& color) { |
| 87 | this->append("color", SkDrawCommand::MakeJsonColor4f(color)); |
| 88 | } |
| 89 | |
| 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 | |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 98 | void SkJsonWriteBuffer::writePoint(const SkPoint& point) { |
| 99 | this->append("point", SkDrawCommand::MakeJsonPoint(point)); |
| 100 | } |
| 101 | |
| 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 | |
| 110 | void SkJsonWriteBuffer::writeMatrix(const SkMatrix& matrix) { |
| 111 | this->append("matrix", SkDrawCommand::MakeJsonMatrix(matrix)); |
| 112 | } |
| 113 | |
| 114 | void SkJsonWriteBuffer::writeIRect(const SkIRect& rect) { |
| 115 | this->append("irect", SkDrawCommand::MakeJsonIRect(rect)); |
| 116 | } |
| 117 | |
| 118 | void SkJsonWriteBuffer::writeRect(const SkRect& rect) { |
| 119 | this->append("rect", SkDrawCommand::MakeJsonRect(rect)); |
| 120 | } |
| 121 | |
| 122 | void SkJsonWriteBuffer::writeRegion(const SkRegion& region) { |
| 123 | this->append("region", SkDrawCommand::MakeJsonRegion(region)); |
| 124 | } |
| 125 | |
| 126 | void SkJsonWriteBuffer::writePath(const SkPath& path) { |
| 127 | this->append("path", SkDrawCommand::MakeJsonPath(path)); |
| 128 | } |
| 129 | |
| 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 | |
| 137 | void SkJsonWriteBuffer::writeBitmap(const SkBitmap& bitmap) { |
| 138 | Json::Value jsonBitmap; |
| 139 | SkDrawCommand::flatten(bitmap, &jsonBitmap, *fUrlDataManager); |
| 140 | this->append("bitmap", jsonBitmap); |
| 141 | } |
| 142 | |
| 143 | void SkJsonWriteBuffer::writeImage(const SkImage* image) { |
| 144 | Json::Value jsonImage; |
| 145 | SkDrawCommand::flatten(*image, &jsonImage, *fUrlDataManager); |
| 146 | this->append("image", jsonImage); |
| 147 | } |
| 148 | |
| 149 | void SkJsonWriteBuffer::writeTypeface(SkTypeface* typeface) { |
| 150 | // Unsupported |
| 151 | this->append("typeface", Json::Value()); |
| 152 | } |
| 153 | |
| 154 | void SkJsonWriteBuffer::writePaint(const SkPaint& paint) { |
| 155 | this->append("paint", SkDrawCommand::MakeJsonPaint(paint, *fUrlDataManager)); |
| 156 | } |