OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2012 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 "SkObjectParser.h" |
| 11 |
| 12 void SkJsonWriteBuffer::append(const char* name, const Json::Value& value) { |
| 13 SkString arrayName = SkStringPrintf("%s_array", name); |
| 14 bool rawNamePresent = fJson.isMember(name); |
| 15 bool arrayPresent = fJson.isMember(arrayName.c_str()); |
| 16 |
| 17 if (arrayPresent) { |
| 18 SkASSERT(!rawNamePresent); |
| 19 // We've already seen this name more than once, append to the array |
| 20 fJson[arrayName.c_str()].append(value); |
| 21 } else if (rawNamePresent) { |
| 22 SkASSERT(!arrayPresent); |
| 23 // We've seen this name once. Convert to an array and see with the old a
nd new values |
| 24 Json::Value jsonArray; |
| 25 jsonArray.append(fJson[name]); |
| 26 jsonArray.append(value); |
| 27 fJson[arrayName.c_str()] = jsonArray; |
| 28 fJson.removeMember(name); |
| 29 } else { |
| 30 // First (and likely only) time to see this name. Just insert it |
| 31 fJson[name] = value; |
| 32 } |
| 33 } |
| 34 |
| 35 void SkJsonWriteBuffer::writeByteArray(const char* name, const void* data, size_
t size) { |
| 36 Json::Value jsonArray; |
| 37 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
| 38 for (size_t i = 0; i < size; ++i) { |
| 39 SkString hexByte = SkStringPrintf("%02x", bytes[i]); |
| 40 jsonArray.append(hexByte.c_str()); |
| 41 } |
| 42 this->append(name, jsonArray); |
| 43 } |
| 44 |
| 45 void SkJsonWriteBuffer::writeBool(const char* name, bool value) { |
| 46 this->append(name, value); |
| 47 } |
| 48 |
| 49 void SkJsonWriteBuffer::writeScalar(const char* name, SkScalar value) { |
| 50 this->append(name, value); |
| 51 } |
| 52 |
| 53 void SkJsonWriteBuffer::writeScalarArray(const char* name, const SkScalar* value
, uint32_t count) { |
| 54 Json::Value jsonArray; |
| 55 for (uint32_t i = 0; i < count; ++i) { |
| 56 jsonArray.append(value[i]); |
| 57 } |
| 58 this->append(name, jsonArray); |
| 59 } |
| 60 |
| 61 void SkJsonWriteBuffer::writeInt(const char* name, int32_t value) { |
| 62 this->append(name, value); |
| 63 } |
| 64 |
| 65 void SkJsonWriteBuffer::writeIntArray(const char* name, const int32_t* value, ui
nt32_t count) { |
| 66 Json::Value jsonArray; |
| 67 for (uint32_t i = 0; i < count; ++i) { |
| 68 jsonArray.append(value[i]); |
| 69 } |
| 70 this->append(name, jsonArray); |
| 71 } |
| 72 |
| 73 void SkJsonWriteBuffer::writeUInt(const char* name, uint32_t value) { |
| 74 this->append(name, value); |
| 75 } |
| 76 |
| 77 void SkJsonWriteBuffer::write32(const char* name, int32_t value) { |
| 78 this->append(name, value); |
| 79 } |
| 80 |
| 81 void SkJsonWriteBuffer::writeString(const char* name, const char* value) { |
| 82 this->append(name, value); |
| 83 } |
| 84 |
| 85 void SkJsonWriteBuffer::writeEncodedString(const char* name, const void* value,
size_t byteLength, |
| 86 SkPaint::TextEncoding encoding) { |
| 87 SkAutoTDelete<SkString> string = SkObjectParser::TextToString(value, byteLen
gth, encoding); |
| 88 this->append(name, string->c_str()); |
| 89 } |
| 90 |
| 91 void SkJsonWriteBuffer::writeFunctionPtr(const char* name, void* ptr) { |
| 92 this->append(name, reinterpret_cast<uintptr_t>(ptr)); |
| 93 } |
| 94 |
| 95 void SkJsonWriteBuffer::writeFlattenable(const char* name, const SkFlattenable*
flattenable) { |
| 96 SkJsonWriteBuffer flattenableBuffer; |
| 97 flattenable->flatten(flattenableBuffer); |
| 98 this->append(name, flattenableBuffer.getValue()); |
| 99 } |
| 100 |
| 101 void SkJsonWriteBuffer::writeColor(const char* name, const SkColor& color) { |
| 102 SkString hexColor = SkStringPrintf("#%08x", color); |
| 103 this->append(name, hexColor.c_str()); |
| 104 } |
| 105 |
| 106 void SkJsonWriteBuffer::writeColorArray(const char* name, const SkColor* color,
uint32_t count) { |
| 107 Json::Value jsonArray; |
| 108 for (uint32_t i = 0; i < count; ++i) { |
| 109 SkString hexColor = SkStringPrintf("#%08x", color[i]); |
| 110 jsonArray.append(hexColor.c_str()); |
| 111 } |
| 112 this->append(name, jsonArray); |
| 113 } |
| 114 |
| 115 void SkJsonWriteBuffer::writePoint(const char* name, const SkPoint& point) { |
| 116 Json::Value jsonPoint; |
| 117 jsonPoint["x"] = point.fX; |
| 118 jsonPoint["y"] = point.fY; |
| 119 this->append(name, jsonPoint); |
| 120 } |
| 121 |
| 122 void SkJsonWriteBuffer::writePointArray(const char* name, const SkPoint* point,
uint32_t count) { |
| 123 Json::Value jsonArray; |
| 124 for (uint32_t i = 0; i < count; ++i) { |
| 125 Json::Value jsonPoint; |
| 126 jsonPoint["x"] = point[i].fX; |
| 127 jsonPoint["y"] = point[i].fY; |
| 128 jsonArray.append(jsonPoint); |
| 129 } |
| 130 this->append(name, jsonArray); |
| 131 } |
| 132 |
| 133 void SkJsonWriteBuffer::writeMatrix(const char* name, const SkMatrix& matrix) { |
| 134 SkString matrixString; |
| 135 #ifndef SK_IGNORE_TO_STRING |
| 136 matrix.toString(&matrixString); |
| 137 #endif |
| 138 this->append(name, matrixString.c_str()); |
| 139 } |
| 140 |
| 141 void SkJsonWriteBuffer::writeIRect(const char* name, const SkIRect& rect) { |
| 142 SkAutoTDelete<SkString> rectString = SkObjectParser::IRectToString(rect); |
| 143 this->append(name, rectString->c_str()); |
| 144 } |
| 145 |
| 146 void SkJsonWriteBuffer::writeRect(const char* name, const SkRect& rect) { |
| 147 SkAutoTDelete<SkString> rectString = SkObjectParser::RectToString(rect); |
| 148 this->append(name, rectString->c_str()); |
| 149 } |
| 150 |
| 151 void SkJsonWriteBuffer::writeRegion(const char* name, const SkRegion& region) { |
| 152 // Unsupported |
| 153 this->append(name, Json::Value()); |
| 154 } |
| 155 |
| 156 void SkJsonWriteBuffer::writePath(const char* name, const SkPath& path) { |
| 157 SkAutoTDelete<SkString> pathString = SkObjectParser::PathToString(path); |
| 158 this->append(name, pathString->c_str()); |
| 159 } |
| 160 |
| 161 size_t SkJsonWriteBuffer::writeStream(const char* name, SkStream* stream, size_t
length) { |
| 162 // Contents not supported |
| 163 this->append(name, Json::Value(length)); |
| 164 return 0; |
| 165 } |
| 166 |
| 167 void SkJsonWriteBuffer::writeBitmap(const char* name, const SkBitmap& bitmap) { |
| 168 SkAutoTDelete<SkString> bitmapString = SkObjectParser::BitmapToString(bitmap
); |
| 169 this->append(name, bitmapString->c_str()); |
| 170 } |
| 171 |
| 172 void SkJsonWriteBuffer::writeImage(const char* name, const SkImage* image) { |
| 173 SkAutoTDelete<SkString> imageString = SkObjectParser::ImageToString(image); |
| 174 this->append(name, imageString->c_str()); |
| 175 } |
| 176 |
| 177 void SkJsonWriteBuffer::writeTypeface(const char* name, SkTypeface* typeface) { |
| 178 // Unsupported |
| 179 this->append(name, Json::Value()); |
| 180 } |
| 181 |
| 182 void SkJsonWriteBuffer::writePaint(const char* name, const SkPaint& paint) { |
| 183 SkAutoTDelete<SkString> paintString = SkObjectParser::PaintToString(paint); |
| 184 this->append(name, paintString->c_str()); |
| 185 } |
OLD | NEW |