ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [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 "SkJSONRenderer.h" |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 9 | |
| 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkDashPathEffect.h" |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 12 | #include "SkJSONCanvas.h" |
| 13 | #include "SkJSONCPP.h" |
| 14 | #include "SkPath.h" |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 15 | #include "SkValidatingReadBuffer.h" |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 16 | |
| 17 | namespace SkJSONRenderer { |
| 18 | |
| 19 | class Renderer { |
| 20 | public: |
| 21 | void getPaint(Json::Value& command, SkPaint* paint); |
| 22 | |
| 23 | void getRect(Json::Value& command, const char* name, SkRect* rect); |
| 24 | |
| 25 | void getRRect(Json::Value& command, const char* name, SkRRect* rrect); |
| 26 | |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 27 | void getPath(Json::Value& command, SkPath* path); |
| 28 | |
| 29 | SkRegion::Op getRegionOp(Json::Value& command); |
| 30 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 31 | void processCommand(Json::Value& command, SkCanvas* target); |
| 32 | |
| 33 | void processMatrix(Json::Value& command, SkCanvas* target); |
| 34 | |
| 35 | void processSave(Json::Value& command, SkCanvas* target); |
| 36 | |
| 37 | void processRestore(Json::Value& command, SkCanvas* target); |
| 38 | |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 39 | void processSaveLayer(Json::Value& command, SkCanvas* target); |
| 40 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 41 | void processPaint(Json::Value& command, SkCanvas* target); |
| 42 | |
| 43 | void processRect(Json::Value& command, SkCanvas* target); |
| 44 | |
| 45 | void processRRect(Json::Value& command, SkCanvas* target); |
| 46 | |
| 47 | void processOval(Json::Value& command, SkCanvas* target); |
| 48 | |
| 49 | void processPath(Json::Value& command, SkCanvas* target); |
| 50 | |
| 51 | void processText(Json::Value& command, SkCanvas* target); |
| 52 | |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 53 | void processPosText(Json::Value& command, SkCanvas* target); |
| 54 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 55 | void processPoints(Json::Value& command, SkCanvas* target); |
| 56 | |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 57 | void processImage(Json::Value& command, SkCanvas* target); |
| 58 | |
| 59 | void processImageRect(Json::Value& command, SkCanvas* target); |
| 60 | |
| 61 | void processBitmap(Json::Value& command, SkCanvas* target); |
| 62 | |
| 63 | void processBitmapRect(Json::Value& command, SkCanvas* target); |
| 64 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 65 | void processClipRect(Json::Value& command, SkCanvas* target); |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 66 | |
| 67 | void processClipRRect(Json::Value& command, SkCanvas* target); |
| 68 | |
| 69 | void processClipPath(Json::Value& command, SkCanvas* target); |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | void Renderer::processCommand(Json::Value& command, SkCanvas* target) { |
| 73 | const char* name = command[SKJSONCANVAS_COMMAND].asCString(); |
| 74 | // TODO speed this up with a hash |
| 75 | if (!strcmp(name, SKJSONCANVAS_COMMAND_MATRIX)) { |
| 76 | this->processMatrix(command, target); |
| 77 | } |
| 78 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVE)) { |
| 79 | this->processSave(command, target); |
| 80 | } |
| 81 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_RESTORE)) { |
| 82 | this->processRestore(command, target); |
| 83 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 84 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVELAYER)) { |
| 85 | this->processSaveLayer(command, target); |
| 86 | } |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 87 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_PAINT)) { |
| 88 | this->processPaint(command, target); |
| 89 | } |
| 90 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_RECT)) { |
| 91 | this->processRect(command, target); |
| 92 | } |
| 93 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_RRECT)) { |
| 94 | this->processRRect(command, target); |
| 95 | } |
| 96 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_OVAL)) { |
| 97 | this->processOval(command, target); |
| 98 | } |
| 99 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_PATH)) { |
| 100 | this->processPath(command, target); |
| 101 | } |
| 102 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) { |
| 103 | this->processText(command, target); |
| 104 | } |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 105 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) { |
| 106 | this->processPosText(command, target); |
| 107 | } |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 108 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) { |
| 109 | this->processPoints(command, target); |
| 110 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 111 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGE)) { |
| 112 | this->processImage(command, target); |
| 113 | } |
| 114 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGERECT)) { |
| 115 | this->processImageRect(command, target); |
| 116 | } |
| 117 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_BITMAP)) { |
| 118 | this->processBitmap(command, target); |
| 119 | } |
| 120 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_BITMAPRECT)) { |
| 121 | this->processBitmapRect(command, target); |
| 122 | } |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 123 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRECT)) { |
| 124 | this->processClipRect(command, target); |
| 125 | } |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 126 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRRECT)) { |
| 127 | this->processClipRRect(command, target); |
| 128 | } |
| 129 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPPATH)) { |
| 130 | this->processClipPath(command, target); |
| 131 | } |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 132 | else { |
| 133 | SkDebugf("unsupported JSON command: %s\n", name); |
| 134 | } |
| 135 | } |
| 136 | |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 137 | static void apply_paint_color(Json::Value& jsonPaint, SkPaint* target) { |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 138 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_COLOR)) { |
| 139 | Json::Value color = jsonPaint[SKJSONCANVAS_ATTRIBUTE_COLOR]; |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 140 | target->setColor(SkColorSetARGB(color[0].asInt(), color[1].asInt(), color[2].asInt(), |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 141 | color[3].asInt())); |
| 142 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // note that the caller is responsible for freeing the pointer |
| 146 | static Json::ArrayIndex decode_data(Json::Value bytes, void** target) { |
| 147 | Json::ArrayIndex size = bytes.size(); |
| 148 | *target = sk_malloc_throw(size); |
| 149 | for (Json::ArrayIndex i = 0; i < size; i++) { |
| 150 | ((uint8_t*) *target)[i] = bytes[i].asInt(); |
| 151 | } |
| 152 | return size; |
| 153 | } |
| 154 | |
| 155 | static SkFlattenable* load_flattenable(Json::Value jsonFlattenable) { |
| 156 | if (!jsonFlattenable.isMember(SKJSONCANVAS_ATTRIBUTE_NAME)) { |
| 157 | return nullptr; |
| 158 | } |
| 159 | const char* name = jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_NAME].asCString(); |
| 160 | SkFlattenable::Factory factory = SkFlattenable::NameToFactory(name); |
| 161 | if (factory == nullptr) { |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 162 | SkDebugf("no factory for loading '%s'\n", name); |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 163 | return nullptr; |
| 164 | } |
| 165 | void* data; |
| 166 | int size = decode_data(jsonFlattenable[SKJSONCANVAS_ATTRIBUTE_BYTES], &data); |
| 167 | SkValidatingReadBuffer buffer(data, size); |
| 168 | SkFlattenable* result = factory(buffer); |
| 169 | free(data); |
| 170 | if (!buffer.isValid()) { |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 171 | SkDebugf("invalid buffer loading flattenable\n"); |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 172 | return nullptr; |
| 173 | } |
| 174 | return result; |
| 175 | } |
| 176 | |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 177 | static SkColorType colortype_from_name(const char* name) { |
| 178 | if (!strcmp(name, SKJSONCANVAS_COLORTYPE_ARGB4444)) { |
| 179 | return kARGB_4444_SkColorType; |
| 180 | } |
| 181 | else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_RGBA8888)) { |
| 182 | return kRGBA_8888_SkColorType; |
| 183 | } |
| 184 | else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_BGRA8888)) { |
| 185 | return kBGRA_8888_SkColorType; |
| 186 | } |
| 187 | else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_565)) { |
| 188 | return kRGB_565_SkColorType; |
| 189 | } |
| 190 | else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_GRAY8)) { |
| 191 | return kGray_8_SkColorType; |
| 192 | } |
| 193 | else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_INDEX8)) { |
| 194 | return kIndex_8_SkColorType; |
| 195 | } |
| 196 | else if (!strcmp(name, SKJSONCANVAS_COLORTYPE_ALPHA8)) { |
| 197 | return kAlpha_8_SkColorType; |
| 198 | } |
| 199 | SkASSERT(false); |
| 200 | return kN32_SkColorType; |
| 201 | } |
| 202 | |
| 203 | static SkBitmap* convert_colortype(SkBitmap* bitmap, SkColorType colorType) { |
| 204 | if (bitmap->colorType() == colorType ) { |
| 205 | return bitmap; |
| 206 | } |
| 207 | SkBitmap* dst = new SkBitmap(); |
| 208 | if (bitmap->copyTo(dst, colorType)) { |
| 209 | delete bitmap; |
| 210 | return dst; |
| 211 | } |
| 212 | SkASSERT(false); |
| 213 | delete dst; |
| 214 | return bitmap; |
| 215 | } |
| 216 | |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 217 | // caller is responsible for freeing return value |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 218 | static SkBitmap* load_bitmap(const Json::Value& jsonBitmap) { |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 219 | if (!jsonBitmap.isMember(SKJSONCANVAS_ATTRIBUTE_BYTES)) { |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 220 | SkDebugf("invalid bitmap\n"); |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 221 | return nullptr; |
| 222 | } |
| 223 | void* data; |
| 224 | int size = decode_data(jsonBitmap[SKJSONCANVAS_ATTRIBUTE_BYTES], &data); |
| 225 | SkMemoryStream stream(data, size); |
| 226 | SkImageDecoder* decoder = SkImageDecoder::Factory(&stream); |
| 227 | SkBitmap* bitmap = new SkBitmap(); |
| 228 | SkImageDecoder::Result result = decoder->decode(&stream, bitmap, |
| 229 | SkImageDecoder::kDecodePixels_Mode); |
| 230 | free(decoder); |
| 231 | if (result != SkImageDecoder::kFailure) { |
| 232 | free(data); |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 233 | if (jsonBitmap.isMember(SKJSONCANVAS_ATTRIBUTE_COLOR)) { |
| 234 | const char* ctName = jsonBitmap[SKJSONCANVAS_ATTRIBUTE_COLOR].asCString(); |
| 235 | SkColorType ct = colortype_from_name(ctName); |
| 236 | if (ct != kIndex_8_SkColorType) { |
| 237 | bitmap = convert_colortype(bitmap, ct); |
| 238 | } |
| 239 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 240 | return bitmap; |
| 241 | } |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 242 | SkDebugf("image decode failed\n"); |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 243 | free(data); |
| 244 | return nullptr; |
| 245 | } |
| 246 | |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 247 | static SkImage* load_image(const Json::Value& jsonImage) { |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 248 | SkBitmap* bitmap = load_bitmap(jsonImage); |
| 249 | if (bitmap == nullptr) { |
| 250 | return nullptr; |
| 251 | } |
| 252 | SkImage* result = SkImage::NewFromBitmap(*bitmap); |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 253 | delete bitmap; |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 254 | return result; |
| 255 | } |
| 256 | |
| 257 | static void apply_paint_shader(Json::Value& jsonPaint, SkPaint* target) { |
| 258 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_SHADER)) { |
| 259 | Json::Value jsonShader = jsonPaint[SKJSONCANVAS_ATTRIBUTE_SHADER]; |
| 260 | SkShader* shader = (SkShader*) load_flattenable(jsonShader); |
| 261 | if (shader != nullptr) { |
| 262 | target->setShader(shader); |
| 263 | shader->unref(); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static void apply_paint_patheffect(Json::Value& jsonPaint, SkPaint* target) { |
| 269 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_PATHEFFECT)) { |
| 270 | Json::Value jsonPathEffect = jsonPaint[SKJSONCANVAS_ATTRIBUTE_PATHEFFECT]; |
| 271 | SkPathEffect* pathEffect = (SkPathEffect*) load_flattenable(jsonPathEffect); |
| 272 | if (pathEffect != nullptr) { |
| 273 | target->setPathEffect(pathEffect); |
| 274 | pathEffect->unref(); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | static void apply_paint_maskfilter(Json::Value& jsonPaint, SkPaint* target) { |
| 280 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_MASKFILTER)) { |
| 281 | Json::Value jsonMaskFilter = jsonPaint[SKJSONCANVAS_ATTRIBUTE_MASKFILTER]; |
| 282 | SkMaskFilter* maskFilter = (SkMaskFilter*) load_flattenable(jsonMaskFilter); |
| 283 | if (maskFilter != nullptr) { |
| 284 | target->setMaskFilter(maskFilter); |
| 285 | maskFilter->unref(); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static void apply_paint_xfermode(Json::Value& jsonPaint, SkPaint* target) { |
| 291 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_XFERMODE)) { |
| 292 | Json::Value jsonXfermode = jsonPaint[SKJSONCANVAS_ATTRIBUTE_XFERMODE]; |
| 293 | SkXfermode* xfermode = (SkXfermode*) load_flattenable(jsonXfermode); |
| 294 | if (xfermode != nullptr) { |
| 295 | target->setXfermode(xfermode); |
| 296 | xfermode->unref(); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 301 | static void apply_paint_imagefilter(Json::Value& jsonPaint, SkPaint* target) { |
| 302 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_IMAGEFILTER)) { |
| 303 | Json::Value jsonImageFilter = jsonPaint[SKJSONCANVAS_ATTRIBUTE_IMAGEFILTER]; |
| 304 | SkImageFilter* imageFilter = (SkImageFilter*) load_flattenable(jsonImageFilter); |
| 305 | if (imageFilter != nullptr) { |
| 306 | target->setImageFilter(imageFilter); |
| 307 | imageFilter->unref(); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 312 | static void apply_paint_style(Json::Value& jsonPaint, SkPaint* target) { |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 313 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STYLE)) { |
| 314 | const char* style = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); |
| 315 | if (!strcmp(style, SKJSONCANVAS_STYLE_FILL)) { |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 316 | target->setStyle(SkPaint::kFill_Style); |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 317 | } |
| 318 | else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKE)) { |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 319 | target->setStyle(SkPaint::kStroke_Style); |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 320 | } |
| 321 | else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKEANDFILL)) { |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 322 | target->setStyle(SkPaint::kStrokeAndFill_Style); |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static void apply_paint_strokewidth(Json::Value& jsonPaint, SkPaint* target) { |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 328 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH)) { |
| 329 | float strokeWidth = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH].asFloat(); |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 330 | target->setStrokeWidth(strokeWidth); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static void apply_paint_strokemiter(Json::Value& jsonPaint, SkPaint* target) { |
| 335 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STROKEMITER)) { |
| 336 | float strokeMiter = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STROKEMITER].asFloat(); |
| 337 | target->setStrokeMiter(strokeMiter); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static void apply_paint_cap(Json::Value& jsonPaint, SkPaint* target) { |
| 342 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_CAP)) { |
| 343 | const char* cap = jsonPaint[SKJSONCANVAS_ATTRIBUTE_CAP].asCString(); |
| 344 | if (!strcmp(cap, SKJSONCANVAS_CAP_BUTT)) { |
| 345 | target->setStrokeCap(SkPaint::kButt_Cap); |
| 346 | } |
| 347 | else if (!strcmp(cap, SKJSONCANVAS_CAP_ROUND)) { |
| 348 | target->setStrokeCap(SkPaint::kRound_Cap); |
| 349 | } |
| 350 | else if (!strcmp(cap, SKJSONCANVAS_CAP_SQUARE)) { |
| 351 | target->setStrokeCap(SkPaint::kSquare_Cap); |
| 352 | } |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 353 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static void apply_paint_antialias(Json::Value& jsonPaint, SkPaint* target) { |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 357 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_ANTIALIAS)) { |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 358 | target->setAntiAlias(jsonPaint[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 359 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static void apply_paint_blur(Json::Value& jsonPaint, SkPaint* target) { |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 363 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_BLUR)) { |
| 364 | Json::Value blur = jsonPaint[SKJSONCANVAS_ATTRIBUTE_BLUR]; |
| 365 | SkScalar sigma = blur[SKJSONCANVAS_ATTRIBUTE_SIGMA].asFloat(); |
| 366 | SkBlurStyle style; |
| 367 | const char* jsonStyle = blur[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); |
| 368 | if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_NORMAL)) { |
| 369 | style = SkBlurStyle::kNormal_SkBlurStyle; |
| 370 | } |
| 371 | else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_SOLID)) { |
| 372 | style = SkBlurStyle::kSolid_SkBlurStyle; |
| 373 | } |
| 374 | else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_OUTER)) { |
| 375 | style = SkBlurStyle::kOuter_SkBlurStyle; |
| 376 | } |
| 377 | else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_INNER)) { |
| 378 | style = SkBlurStyle::kInner_SkBlurStyle; |
| 379 | } |
| 380 | else { |
| 381 | SkASSERT(false); |
| 382 | style = SkBlurStyle::kNormal_SkBlurStyle; |
| 383 | } |
| 384 | SkBlurMaskFilter::BlurFlags flags; |
| 385 | const char* jsonQuality = blur[SKJSONCANVAS_ATTRIBUTE_QUALITY].asCString(); |
| 386 | if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_LOW)) { |
| 387 | flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag; |
| 388 | } |
| 389 | else if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_HIGH)) { |
| 390 | flags = SkBlurMaskFilter::BlurFlags::kHighQuality_BlurFlag; |
| 391 | } |
| 392 | else { |
| 393 | SkASSERT(false); |
| 394 | flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag; |
| 395 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 396 | target->setMaskFilter(SkBlurMaskFilter::Create(style, sigma, flags)); |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 397 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static void apply_paint_dashing(Json::Value& jsonPaint, SkPaint* target) { |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 401 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_DASHING)) { |
| 402 | Json::Value dash = jsonPaint[SKJSONCANVAS_ATTRIBUTE_DASHING]; |
| 403 | Json::Value jsonIntervals = dash[SKJSONCANVAS_ATTRIBUTE_INTERVALS]; |
| 404 | Json::ArrayIndex count = jsonIntervals.size(); |
| 405 | SkScalar* intervals = (SkScalar*) sk_malloc_throw(count * sizeof(SkScalar)); |
| 406 | for (Json::ArrayIndex i = 0; i < count; i++) { |
| 407 | intervals[i] = jsonIntervals[i].asFloat(); |
| 408 | } |
| 409 | SkScalar phase = dash[SKJSONCANVAS_ATTRIBUTE_PHASE].asFloat(); |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 410 | target->setPathEffect(SkDashPathEffect::Create(intervals, count, phase)); |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 411 | free(intervals); |
| 412 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static void apply_paint_textalign(Json::Value& jsonPaint, SkPaint* target) { |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 416 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTALIGN)) { |
| 417 | SkPaint::Align textAlign; |
| 418 | const char* jsonAlign = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN].asCString(); |
| 419 | if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_LEFT)) { |
| 420 | textAlign = SkPaint::kLeft_Align; |
| 421 | } |
| 422 | else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_CENTER)) { |
| 423 | textAlign = SkPaint::kCenter_Align; |
| 424 | } |
| 425 | else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_RIGHT)) { |
| 426 | textAlign = SkPaint::kRight_Align; |
| 427 | } |
| 428 | else { |
| 429 | SkASSERT(false); |
| 430 | textAlign = SkPaint::kLeft_Align; |
| 431 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 432 | target->setTextAlign(textAlign); |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 433 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static void apply_paint_textsize(Json::Value& jsonPaint, SkPaint* target) { |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 437 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSIZE)) { |
| 438 | float textSize = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSIZE].asFloat(); |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 439 | target->setTextSize(textSize); |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 440 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static void apply_paint_textscalex(Json::Value& jsonPaint, SkPaint* target) { |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 444 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX)) { |
| 445 | float textScaleX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX].asFloat(); |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 446 | target->setTextScaleX(textScaleX); |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 447 | } |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static void apply_paint_textskewx(Json::Value& jsonPaint, SkPaint* target) { |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 451 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) { |
| 452 | float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat(); |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 453 | target->setTextSkewX(textSkewX); |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 454 | } |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 455 | } |
| 456 | |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 457 | void Renderer::getPaint(Json::Value& command, SkPaint* result) { |
| 458 | Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT]; |
| 459 | apply_paint_color(jsonPaint, result); |
| 460 | apply_paint_shader(jsonPaint, result); |
| 461 | apply_paint_patheffect(jsonPaint, result); |
| 462 | apply_paint_maskfilter(jsonPaint, result); |
| 463 | apply_paint_xfermode(jsonPaint, result); |
ethannicholas | 0bd1034 | 2016-02-04 06:45:25 -0800 | [diff] [blame^] | 464 | apply_paint_imagefilter(jsonPaint, result); |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 465 | apply_paint_style(jsonPaint, result); |
| 466 | apply_paint_strokewidth(jsonPaint, result); |
| 467 | apply_paint_strokemiter(jsonPaint, result); |
| 468 | apply_paint_cap(jsonPaint, result); |
| 469 | apply_paint_antialias(jsonPaint, result); |
| 470 | apply_paint_blur(jsonPaint, result); |
| 471 | apply_paint_dashing(jsonPaint, result); |
| 472 | apply_paint_textalign(jsonPaint, result); |
| 473 | apply_paint_textsize(jsonPaint, result); |
| 474 | apply_paint_textscalex(jsonPaint, result); |
| 475 | apply_paint_textskewx(jsonPaint, result); |
| 476 | } |
| 477 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 478 | void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) { |
| 479 | Json::Value rect = command[name]; |
| 480 | result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3].asFloat()); |
| 481 | } |
| 482 | |
| 483 | void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result) { |
| 484 | Json::Value rrect = command[name]; |
| 485 | SkVector radii[4] = { |
| 486 | { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, |
| 487 | { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, |
| 488 | { rrect[3][0].asFloat(), rrect[3][1].asFloat() }, |
| 489 | { rrect[4][0].asFloat(), rrect[4][1].asFloat() } |
| 490 | }; |
| 491 | result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asFloat(), |
| 492 | rrect[0][2].asFloat(), rrect[0][3].asFloat()), |
| 493 | radii); |
| 494 | } |
| 495 | |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 496 | void Renderer::getPath(Json::Value& command, SkPath* result) { |
| 497 | Json::Value path = command[SKJSONCANVAS_ATTRIBUTE_PATH]; |
| 498 | const char* fillType = path[SKJSONCANVAS_ATTRIBUTE_FILLTYPE].asCString(); |
| 499 | if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_WINDING)) { |
| 500 | result->setFillType(SkPath::kWinding_FillType); |
| 501 | } |
| 502 | else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_EVENODD)) { |
| 503 | result->setFillType(SkPath::kEvenOdd_FillType); |
| 504 | } |
| 505 | else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEWINDING)) { |
| 506 | result->setFillType(SkPath::kInverseWinding_FillType); |
| 507 | } |
| 508 | else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEEVENODD)) { |
| 509 | result->setFillType(SkPath::kInverseEvenOdd_FillType); |
| 510 | } |
| 511 | Json::Value verbs = path[SKJSONCANVAS_ATTRIBUTE_VERBS]; |
| 512 | for (Json::ArrayIndex i = 0; i < verbs.size(); i++) { |
| 513 | Json::Value verb = verbs[i]; |
| 514 | if (verb.isString()) { |
| 515 | SkASSERT(!strcmp(verb.asCString(), SKJSONCANVAS_VERB_CLOSE)); |
| 516 | result->close(); |
| 517 | } |
| 518 | else { |
| 519 | if (verb.isMember(SKJSONCANVAS_VERB_MOVE)) { |
| 520 | Json::Value move = verb[SKJSONCANVAS_VERB_MOVE]; |
| 521 | result->moveTo(move[0].asFloat(), move[1].asFloat()); |
| 522 | } |
| 523 | else if (verb.isMember(SKJSONCANVAS_VERB_LINE)) { |
| 524 | Json::Value line = verb[SKJSONCANVAS_VERB_LINE]; |
| 525 | result->lineTo(line[0].asFloat(), line[1].asFloat()); |
| 526 | } |
| 527 | else if (verb.isMember(SKJSONCANVAS_VERB_QUAD)) { |
| 528 | Json::Value quad = verb[SKJSONCANVAS_VERB_QUAD]; |
| 529 | result->quadTo(quad[0][0].asFloat(), quad[0][1].asFloat(), |
| 530 | quad[1][0].asFloat(), quad[1][1].asFloat()); |
| 531 | } |
| 532 | else if (verb.isMember(SKJSONCANVAS_VERB_CUBIC)) { |
| 533 | Json::Value cubic = verb[SKJSONCANVAS_VERB_CUBIC]; |
| 534 | result->cubicTo(cubic[0][0].asFloat(), cubic[0][1].asFloat(), |
| 535 | cubic[1][0].asFloat(), cubic[1][1].asFloat(), |
| 536 | cubic[2][0].asFloat(), cubic[2][1].asFloat()); |
| 537 | } |
| 538 | else if (verb.isMember(SKJSONCANVAS_VERB_CONIC)) { |
| 539 | Json::Value conic = verb[SKJSONCANVAS_VERB_CONIC]; |
| 540 | result->conicTo(conic[0][0].asFloat(), conic[0][1].asFloat(), |
| 541 | conic[1][0].asFloat(), conic[1][1].asFloat(), |
| 542 | conic[2].asFloat()); |
| 543 | } |
| 544 | else { |
| 545 | SkASSERT(false); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | SkRegion::Op Renderer::getRegionOp(Json::Value& command) { |
| 552 | const char* op = command[SKJSONCANVAS_ATTRIBUTE_REGIONOP].asCString(); |
| 553 | if (!strcmp(op, SKJSONCANVAS_REGIONOP_DIFFERENCE)) { |
| 554 | return SkRegion::kDifference_Op; |
| 555 | } |
| 556 | else if (!strcmp(op, SKJSONCANVAS_REGIONOP_INTERSECT)) { |
| 557 | return SkRegion::kIntersect_Op; |
| 558 | } |
| 559 | else if (!strcmp(op, SKJSONCANVAS_REGIONOP_UNION)) { |
| 560 | return SkRegion::kUnion_Op; |
| 561 | } |
| 562 | else if (!strcmp(op, SKJSONCANVAS_REGIONOP_XOR)) { |
| 563 | return SkRegion::kXOR_Op; |
| 564 | } |
| 565 | else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE)) { |
| 566 | return SkRegion::kReverseDifference_Op; |
| 567 | } |
| 568 | else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REPLACE)) { |
| 569 | return SkRegion::kReplace_Op; |
| 570 | } |
| 571 | SkASSERT(false); |
| 572 | return SkRegion::kIntersect_Op; |
| 573 | } |
| 574 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 575 | void Renderer::processMatrix(Json::Value& command, SkCanvas* target) { |
| 576 | Json::Value jsonMatrix = command[SKJSONCANVAS_ATTRIBUTE_MATRIX]; |
| 577 | SkMatrix matrix; |
| 578 | SkScalar values[] = { |
| 579 | jsonMatrix[0][0].asFloat(), jsonMatrix[0][1].asFloat(), jsonMatrix[0][2].asFloat(), |
| 580 | jsonMatrix[1][0].asFloat(), jsonMatrix[1][1].asFloat(), jsonMatrix[1][2].asFloat(), |
| 581 | jsonMatrix[2][0].asFloat(), jsonMatrix[2][1].asFloat(), jsonMatrix[2][2].asFloat() |
| 582 | }; |
| 583 | matrix.set9(values); |
| 584 | target->setMatrix(matrix); |
| 585 | } |
| 586 | |
| 587 | void Renderer::processSave(Json::Value& command, SkCanvas* target) { |
| 588 | target->save(); |
| 589 | } |
| 590 | |
| 591 | void Renderer::processRestore(Json::Value& command, SkCanvas* target) { |
| 592 | target->restore(); |
| 593 | } |
| 594 | |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 595 | void Renderer::processSaveLayer(Json::Value& command, SkCanvas* target) { |
| 596 | SkCanvas::SaveLayerRec rec; |
| 597 | SkRect bounds; |
| 598 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_BOUNDS)) { |
| 599 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_BOUNDS, &bounds); |
| 600 | rec.fBounds = &bounds; |
| 601 | } |
| 602 | SkPaint paint; |
| 603 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { |
| 604 | this->getPaint(command, &paint); |
| 605 | rec.fPaint = &paint; |
| 606 | } |
| 607 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_BACKDROP)) { |
| 608 | rec.fBackdrop = (SkImageFilter*) load_flattenable(command[SKJSONCANVAS_ATTRIBUTE_BACKDROP]); |
| 609 | } |
| 610 | target->saveLayer(rec); |
| 611 | if (rec.fBackdrop != nullptr) { |
| 612 | rec.fBackdrop->unref(); |
| 613 | } |
| 614 | } |
| 615 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 616 | void Renderer::processPaint(Json::Value& command, SkCanvas* target) { |
| 617 | SkPaint paint; |
| 618 | this->getPaint(command, &paint); |
| 619 | target->drawPaint(paint); |
| 620 | } |
| 621 | |
| 622 | void Renderer::processRect(Json::Value& command, SkCanvas* target) { |
| 623 | SkRect rect; |
| 624 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); |
| 625 | SkPaint paint; |
| 626 | this->getPaint(command, &paint); |
| 627 | target->drawRect(rect, paint); |
| 628 | } |
| 629 | |
| 630 | void Renderer::processRRect(Json::Value& command, SkCanvas* target) { |
| 631 | SkRRect rrect; |
| 632 | this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect); |
| 633 | SkPaint paint; |
| 634 | this->getPaint(command, &paint); |
| 635 | target->drawRRect(rrect, paint); |
| 636 | } |
| 637 | |
| 638 | void Renderer::processOval(Json::Value& command, SkCanvas* target) { |
| 639 | SkRect rect; |
| 640 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); |
| 641 | SkPaint paint; |
| 642 | this->getPaint(command, &paint); |
| 643 | target->drawOval(rect, paint); |
| 644 | } |
| 645 | |
| 646 | void Renderer::processPath(Json::Value& command, SkCanvas* target) { |
| 647 | Json::Value jsonPath = command[SKJSONCANVAS_ATTRIBUTE_PATH]; |
| 648 | SkPath path; |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 649 | this->getPath(command, &path); |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 650 | SkPaint paint; |
| 651 | this->getPaint(command, &paint); |
| 652 | target->drawPath(path, paint); |
| 653 | } |
| 654 | |
| 655 | void Renderer::processText(Json::Value& command, SkCanvas* target) { |
| 656 | const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString(); |
| 657 | SkPaint paint; |
| 658 | this->getPaint(command, &paint); |
| 659 | Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; |
| 660 | target->drawText(text, strlen(text), coords[0].asFloat(), coords[1].asFloat(), paint); |
| 661 | } |
| 662 | |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 663 | void Renderer::processPosText(Json::Value& command, SkCanvas* target) { |
| 664 | const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString(); |
| 665 | SkPaint paint; |
| 666 | this->getPaint(command, &paint); |
| 667 | Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; |
| 668 | int count = (int) coords.size(); |
| 669 | SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint)); |
| 670 | for (int i = 0; i < count; i++) { |
| 671 | points[i] = SkPoint::Make(coords[i][0].asFloat(), coords[i][1].asFloat()); |
| 672 | } |
| 673 | target->drawPosText(text, strlen(text), points, paint); |
| 674 | free(points); |
| 675 | } |
| 676 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 677 | void Renderer::processPoints(Json::Value& command, SkCanvas* target) { |
| 678 | SkCanvas::PointMode mode; |
| 679 | const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString(); |
| 680 | if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POINTS)) { |
| 681 | mode = SkCanvas::kPoints_PointMode; |
| 682 | } |
| 683 | else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_LINES)) { |
| 684 | mode = SkCanvas::kLines_PointMode; |
| 685 | } |
| 686 | else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POLYGON)) { |
| 687 | mode = SkCanvas::kPolygon_PointMode; |
| 688 | } |
| 689 | else { |
| 690 | SkASSERT(false); |
| 691 | return; |
| 692 | } |
| 693 | Json::Value jsonPoints = command[SKJSONCANVAS_ATTRIBUTE_POINTS]; |
| 694 | int count = (int) jsonPoints.size(); |
| 695 | SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint)); |
| 696 | for (int i = 0; i < count; i++) { |
| 697 | points[i] = SkPoint::Make(jsonPoints[i][0].asFloat(), jsonPoints[i][1].asFloat()); |
| 698 | } |
| 699 | SkPaint paint; |
| 700 | this->getPaint(command, &paint); |
| 701 | target->drawPoints(mode, count, points, paint); |
| 702 | free(points); |
| 703 | } |
| 704 | |
| 705 | void Renderer::processClipRect(Json::Value& command, SkCanvas* target) { |
| 706 | SkRect rect; |
| 707 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame] | 708 | target->clipRect(rect, this->getRegionOp(command), |
| 709 | command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
| 710 | } |
| 711 | |
| 712 | void Renderer::processClipRRect(Json::Value& command, SkCanvas* target) { |
| 713 | SkRRect rrect; |
| 714 | this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect); |
| 715 | target->clipRRect(rrect, this->getRegionOp(command), |
| 716 | command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
| 717 | } |
| 718 | |
| 719 | void Renderer::processClipPath(Json::Value& command, SkCanvas* target) { |
| 720 | SkPath path; |
| 721 | this->getPath(command, &path); |
| 722 | target->clipPath(path, this->getRegionOp(command), |
| 723 | command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 724 | } |
| 725 | |
ethannicholas | 30c5dde | 2016-02-02 08:36:58 -0800 | [diff] [blame] | 726 | void Renderer::processImage(Json::Value& command, SkCanvas* target) { |
| 727 | SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_IMAGE]); |
| 728 | if (image == nullptr) { |
| 729 | return; |
| 730 | } |
| 731 | Json::Value point = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; |
| 732 | SkPaint* paintPtr; |
| 733 | SkPaint paint; |
| 734 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { |
| 735 | this->getPaint(command, &paint); |
| 736 | paintPtr = &paint; |
| 737 | } |
| 738 | else { |
| 739 | paintPtr = nullptr; |
| 740 | } |
| 741 | target->drawImage(image, point[0].asFloat(), point[1].asFloat(), paintPtr); |
| 742 | image->unref(); |
| 743 | } |
| 744 | |
| 745 | void Renderer::processImageRect(Json::Value& command, SkCanvas* target) { |
| 746 | SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_IMAGE]); |
| 747 | if (image == nullptr) { |
| 748 | return; |
| 749 | } |
| 750 | SkRect dst; |
| 751 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_DST, &dst); |
| 752 | SkPaint* paintPtr; |
| 753 | SkPaint paint; |
| 754 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { |
| 755 | this->getPaint(command, &paint); |
| 756 | paintPtr = &paint; |
| 757 | } |
| 758 | else { |
| 759 | paintPtr = nullptr; |
| 760 | } |
| 761 | SkCanvas::SrcRectConstraint constraint; |
| 762 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_STRICT) && |
| 763 | command[SKJSONCANVAS_ATTRIBUTE_STRICT].asBool()) { |
| 764 | constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 765 | } |
| 766 | else { |
| 767 | constraint = SkCanvas::kFast_SrcRectConstraint; |
| 768 | } |
| 769 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_SRC)) { |
| 770 | SkRect src; |
| 771 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_SRC, &src); |
| 772 | target->drawImageRect(image, src, dst, paintPtr, constraint); |
| 773 | } |
| 774 | else { |
| 775 | target->drawImageRect(image, dst, paintPtr, constraint); |
| 776 | } |
| 777 | image->unref(); |
| 778 | } |
| 779 | |
| 780 | void Renderer::processBitmap(Json::Value& command, SkCanvas* target) { |
| 781 | SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_BITMAP]); |
| 782 | if (image == nullptr) { |
| 783 | return; |
| 784 | } |
| 785 | Json::Value point = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; |
| 786 | SkPaint* paintPtr; |
| 787 | SkPaint paint; |
| 788 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { |
| 789 | this->getPaint(command, &paint); |
| 790 | paintPtr = &paint; |
| 791 | } |
| 792 | else { |
| 793 | paintPtr = nullptr; |
| 794 | } |
| 795 | target->drawImage(image, point[0].asFloat(), point[1].asFloat(), paintPtr); |
| 796 | image->unref(); |
| 797 | } |
| 798 | |
| 799 | void Renderer::processBitmapRect(Json::Value& command, SkCanvas* target) { |
| 800 | SkBitmap* bitmap = load_bitmap(command[SKJSONCANVAS_ATTRIBUTE_BITMAP]); |
| 801 | if (bitmap == nullptr) { |
| 802 | return; |
| 803 | } |
| 804 | SkRect dst; |
| 805 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_DST, &dst); |
| 806 | SkPaint* paintPtr; |
| 807 | SkPaint paint; |
| 808 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { |
| 809 | this->getPaint(command, &paint); |
| 810 | paintPtr = &paint; |
| 811 | } |
| 812 | else { |
| 813 | paintPtr = nullptr; |
| 814 | } |
| 815 | SkCanvas::SrcRectConstraint constraint; |
| 816 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_STRICT) && |
| 817 | command[SKJSONCANVAS_ATTRIBUTE_STRICT].asBool()) { |
| 818 | constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 819 | } |
| 820 | else { |
| 821 | constraint = SkCanvas::kFast_SrcRectConstraint; |
| 822 | } |
| 823 | if (command.isMember(SKJSONCANVAS_ATTRIBUTE_SRC)) { |
| 824 | SkRect src; |
| 825 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_SRC, &src); |
| 826 | target->drawBitmapRect(*bitmap, src, dst, paintPtr, constraint); |
| 827 | } |
| 828 | else { |
| 829 | target->drawBitmapRect(*bitmap, dst, paintPtr, constraint); |
| 830 | } |
| 831 | free(bitmap); |
| 832 | } |
| 833 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 834 | void render(const char* json, SkCanvas* target) { |
| 835 | Renderer renderer; |
| 836 | Json::Reader reader; |
| 837 | Json::Value root; |
| 838 | if (reader.parse(std::string(json), root)) { |
| 839 | SkASSERT(root[SKJSONCANVAS_VERSION].asInt() == 1); |
| 840 | Json::Value commands = root[SKJSONCANVAS_COMMANDS]; |
| 841 | for (Json::ArrayIndex i = 0; i < commands.size(); i++) { |
| 842 | renderer.processCommand(commands[i], target); |
| 843 | } |
| 844 | } |
| 845 | else { |
| 846 | SkDebugf(json); |
| 847 | SkFAIL("json parse failure"); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | } // namespace |