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 uint32_t* SkJsonWriteBuffer::reserve(size_t size) { |
| 13 // This code path should never be encountered! reserve() is part of the SkWr
iteBuffer |
| 14 // interface, but that's only because SkPaint uses it during flatten() to sa
ve a bit |
| 15 // of time & space. It's a leaky abstraction, though - assuming that data ca
n be written |
| 16 // through a raw pointer to the buffer. |
| 17 SkASSERT(false); |
| 18 |
| 19 // Regardless - we make a best effort to not crash if we somehow get here, b
y giving |
| 20 // the caller a valid pointer to memory that we then drop on the floor: |
| 21 SkASSERT(SkAlign4(size) == size); |
| 22 if (size > fReserveDataSize) { |
| 23 fReserveData.realloc(size / sizeof(uint32_t)); |
| 24 } |
| 25 |
| 26 return fReserveData.get(); |
| 27 } |
| 28 |
| 29 void SkJsonWriteBuffer::append(const char* type, const Json::Value& value) { |
| 30 SkString fullName = SkStringPrintf("%02d_%s", fFieldCount++, type); |
| 31 fJson[fullName.c_str()] = value; |
| 32 } |
| 33 |
| 34 void SkJsonWriteBuffer::writeByteArray(const void* data, size_t size) { |
| 35 Json::Value jsonArray; |
| 36 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
| 37 for (size_t i = 0; i < size; ++i) { |
| 38 SkString hexByte = SkStringPrintf("%02x", bytes[i]); |
| 39 jsonArray.append(hexByte.c_str()); |
| 40 } |
| 41 this->append("byteArray", jsonArray); |
| 42 } |
| 43 |
| 44 void SkJsonWriteBuffer::writeDataAsByteArray(SkData* data) { |
| 45 Json::Value jsonArray; |
| 46 const uint8_t* bytes = data->bytes(); |
| 47 for (size_t i = 0; i < data->size(); ++i) { |
| 48 SkString hexByte = SkStringPrintf("%02x", bytes[i]); |
| 49 jsonArray.append(hexByte.c_str()); |
| 50 } |
| 51 this->append("data", jsonArray); |
| 52 } |
| 53 |
| 54 void SkJsonWriteBuffer::writeBool(bool value) { |
| 55 this->append("bool", value); |
| 56 } |
| 57 |
| 58 void SkJsonWriteBuffer::writeScalar(SkScalar value) { |
| 59 this->append("scalar", value); |
| 60 } |
| 61 |
| 62 void SkJsonWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count)
{ |
| 63 Json::Value jsonArray; |
| 64 for (uint32_t i = 0; i < count; ++i) { |
| 65 jsonArray.append(value[i]); |
| 66 } |
| 67 this->append("scalarArray", jsonArray); |
| 68 } |
| 69 |
| 70 void SkJsonWriteBuffer::writeInt(int32_t value) { |
| 71 this->append("int", value); |
| 72 } |
| 73 |
| 74 void SkJsonWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) { |
| 75 Json::Value jsonArray; |
| 76 for (uint32_t i = 0; i < count; ++i) { |
| 77 jsonArray.append(value[i]); |
| 78 } |
| 79 this->append("intArray", jsonArray); |
| 80 } |
| 81 |
| 82 void SkJsonWriteBuffer::writeUInt(uint32_t value) { |
| 83 this->append("uint", value); |
| 84 } |
| 85 |
| 86 void SkJsonWriteBuffer::write32(int32_t value) { |
| 87 this->append("int32", value); |
| 88 } |
| 89 |
| 90 void SkJsonWriteBuffer::writeString(const char* value) { |
| 91 this->append("string", value); |
| 92 } |
| 93 |
| 94 void SkJsonWriteBuffer::writeEncodedString(const void* value, size_t byteLength, |
| 95 SkPaint::TextEncoding encoding) { |
| 96 SkAutoTDelete<SkString> string = SkObjectParser::TextToString(value, byteLen
gth, encoding); |
| 97 this->append("encodedString", string->c_str()); |
| 98 } |
| 99 |
| 100 void SkJsonWriteBuffer::writeFunctionPtr(void* ptr) { |
| 101 this->append("functionPtr", reinterpret_cast<uintptr_t>(ptr)); |
| 102 } |
| 103 |
| 104 void SkJsonWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { |
| 105 if (flattenable) { |
| 106 SkJsonWriteBuffer flattenableBuffer(fUrlDataManager); |
| 107 flattenable->flatten(flattenableBuffer); |
| 108 this->append(flattenable->getTypeName(), flattenableBuffer.getValue()); |
| 109 } else { |
| 110 this->append("flattenable", "<null>"); |
| 111 } |
| 112 } |
| 113 |
| 114 void SkJsonWriteBuffer::writeColor(const SkColor& color) { |
| 115 SkString hexColor = SkStringPrintf("#%08x", color); |
| 116 this->append("color", hexColor.c_str()); |
| 117 } |
| 118 |
| 119 void SkJsonWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) { |
| 120 Json::Value jsonArray; |
| 121 for (uint32_t i = 0; i < count; ++i) { |
| 122 SkString hexColor = SkStringPrintf("#%08x", color[i]); |
| 123 jsonArray.append(hexColor.c_str()); |
| 124 } |
| 125 this->append("colorArray", jsonArray); |
| 126 } |
| 127 |
| 128 void SkJsonWriteBuffer::writePoint(const SkPoint& point) { |
| 129 Json::Value jsonPoint; |
| 130 jsonPoint.append(point.fX); |
| 131 jsonPoint.append(point.fY); |
| 132 this->append("point", jsonPoint); |
| 133 } |
| 134 |
| 135 void SkJsonWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) { |
| 136 Json::Value jsonArray; |
| 137 for (uint32_t i = 0; i < count; ++i) { |
| 138 Json::Value jsonPoint; |
| 139 jsonPoint.append(point[i].fX); |
| 140 jsonPoint.append(point[i].fY); |
| 141 jsonArray.append(jsonPoint); |
| 142 } |
| 143 this->append("pointArray", jsonArray); |
| 144 } |
| 145 |
| 146 void SkJsonWriteBuffer::writeMatrix(const SkMatrix& matrix) { |
| 147 SkString matrixString; |
| 148 #ifndef SK_IGNORE_TO_STRING |
| 149 matrix.toString(&matrixString); |
| 150 #endif |
| 151 this->append("matrix", matrixString.c_str()); |
| 152 } |
| 153 |
| 154 void SkJsonWriteBuffer::writeIRect(const SkIRect& rect) { |
| 155 Json::Value jsonRect; |
| 156 jsonRect.append(rect.fLeft); |
| 157 jsonRect.append(rect.fTop); |
| 158 jsonRect.append(rect.fRight); |
| 159 jsonRect.append(rect.fBottom); |
| 160 this->append("irect", jsonRect); |
| 161 } |
| 162 |
| 163 void SkJsonWriteBuffer::writeRect(const SkRect& rect) { |
| 164 Json::Value jsonRect; |
| 165 jsonRect.append(rect.fLeft); |
| 166 jsonRect.append(rect.fTop); |
| 167 jsonRect.append(rect.fRight); |
| 168 jsonRect.append(rect.fBottom); |
| 169 this->append("rect", jsonRect); |
| 170 } |
| 171 |
| 172 void SkJsonWriteBuffer::writeRegion(const SkRegion& region) { |
| 173 // Unsupported |
| 174 this->append("region", Json::Value()); |
| 175 } |
| 176 |
| 177 void SkJsonWriteBuffer::writePath(const SkPath& path) { |
| 178 SkAutoTDelete<SkString> pathString = SkObjectParser::PathToString(path); |
| 179 this->append("path", pathString->c_str()); |
| 180 } |
| 181 |
| 182 size_t SkJsonWriteBuffer::writeStream(SkStream* stream, size_t length) { |
| 183 // Contents not supported |
| 184 this->append("stream", length); |
| 185 return 0; |
| 186 } |
| 187 |
| 188 void SkJsonWriteBuffer::writeBitmap(const SkBitmap& bitmap) { |
| 189 SkAutoTDelete<SkString> bitmapString = SkObjectParser::BitmapToString(bitmap
); |
| 190 this->append("bitmap", bitmapString->c_str()); |
| 191 } |
| 192 |
| 193 void SkJsonWriteBuffer::writeImage(const SkImage* image) { |
| 194 SkAutoTDelete<SkString> imageString = SkObjectParser::ImageToString(image); |
| 195 this->append("image", imageString->c_str()); |
| 196 } |
| 197 |
| 198 void SkJsonWriteBuffer::writeTypeface(SkTypeface* typeface) { |
| 199 // Unsupported |
| 200 this->append("typeface", Json::Value()); |
| 201 } |
| 202 |
| 203 void SkJsonWriteBuffer::writePaint(const SkPaint& paint) { |
| 204 // TODO: Store a reference to the UrlDataManager in this class, then call ba
ck into |
| 205 // make_json_paint in this situation, to get the richer decoding? |
| 206 extern Json::Value make_json_paint(const SkPaint& paint, UrlDataManager& url
DataManager); |
| 207 this->append("paint", make_json_paint(paint, *fUrlDataManager)); |
| 208 } |
OLD | NEW |