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" |
| 15 | |
| 16 | namespace SkJSONRenderer { |
| 17 | |
| 18 | class Renderer { |
| 19 | public: |
| 20 | void getPaint(Json::Value& command, SkPaint* paint); |
| 21 | |
| 22 | void getRect(Json::Value& command, const char* name, SkRect* rect); |
| 23 | |
| 24 | void getRRect(Json::Value& command, const char* name, SkRRect* rrect); |
| 25 | |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame^] | 26 | void getPath(Json::Value& command, SkPath* path); |
| 27 | |
| 28 | SkRegion::Op getRegionOp(Json::Value& command); |
| 29 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 30 | void processCommand(Json::Value& command, SkCanvas* target); |
| 31 | |
| 32 | void processMatrix(Json::Value& command, SkCanvas* target); |
| 33 | |
| 34 | void processSave(Json::Value& command, SkCanvas* target); |
| 35 | |
| 36 | void processRestore(Json::Value& command, SkCanvas* target); |
| 37 | |
| 38 | void processPaint(Json::Value& command, SkCanvas* target); |
| 39 | |
| 40 | void processRect(Json::Value& command, SkCanvas* target); |
| 41 | |
| 42 | void processRRect(Json::Value& command, SkCanvas* target); |
| 43 | |
| 44 | void processOval(Json::Value& command, SkCanvas* target); |
| 45 | |
| 46 | void processPath(Json::Value& command, SkCanvas* target); |
| 47 | |
| 48 | void processText(Json::Value& command, SkCanvas* target); |
| 49 | |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame^] | 50 | void processPosText(Json::Value& command, SkCanvas* target); |
| 51 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 52 | void processPoints(Json::Value& command, SkCanvas* target); |
| 53 | |
| 54 | void processClipRect(Json::Value& command, SkCanvas* target); |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame^] | 55 | |
| 56 | void processClipRRect(Json::Value& command, SkCanvas* target); |
| 57 | |
| 58 | void processClipPath(Json::Value& command, SkCanvas* target); |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | void Renderer::processCommand(Json::Value& command, SkCanvas* target) { |
| 62 | const char* name = command[SKJSONCANVAS_COMMAND].asCString(); |
| 63 | // TODO speed this up with a hash |
| 64 | if (!strcmp(name, SKJSONCANVAS_COMMAND_MATRIX)) { |
| 65 | this->processMatrix(command, target); |
| 66 | } |
| 67 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVE)) { |
| 68 | this->processSave(command, target); |
| 69 | } |
| 70 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_RESTORE)) { |
| 71 | this->processRestore(command, target); |
| 72 | } |
| 73 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_PAINT)) { |
| 74 | this->processPaint(command, target); |
| 75 | } |
| 76 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_RECT)) { |
| 77 | this->processRect(command, target); |
| 78 | } |
| 79 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_RRECT)) { |
| 80 | this->processRRect(command, target); |
| 81 | } |
| 82 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_OVAL)) { |
| 83 | this->processOval(command, target); |
| 84 | } |
| 85 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_PATH)) { |
| 86 | this->processPath(command, target); |
| 87 | } |
| 88 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) { |
| 89 | this->processText(command, target); |
| 90 | } |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame^] | 91 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) { |
| 92 | this->processPosText(command, target); |
| 93 | } |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 94 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) { |
| 95 | this->processPoints(command, target); |
| 96 | } |
| 97 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRECT)) { |
| 98 | this->processClipRect(command, target); |
| 99 | } |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame^] | 100 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRRECT)) { |
| 101 | this->processClipRRect(command, target); |
| 102 | } |
| 103 | else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPPATH)) { |
| 104 | this->processClipPath(command, target); |
| 105 | } |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 106 | else { |
| 107 | SkDebugf("unsupported JSON command: %s\n", name); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void Renderer::getPaint(Json::Value& command, SkPaint* result) { |
| 112 | Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT]; |
| 113 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_COLOR)) { |
| 114 | Json::Value color = jsonPaint[SKJSONCANVAS_ATTRIBUTE_COLOR]; |
| 115 | result->setColor(SkColorSetARGB(color[0].asInt(), color[1].asInt(), color[2].asInt(), |
| 116 | color[3].asInt())); |
| 117 | } |
| 118 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STYLE)) { |
| 119 | const char* style = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); |
| 120 | if (!strcmp(style, SKJSONCANVAS_STYLE_FILL)) { |
| 121 | result->setStyle(SkPaint::kFill_Style); |
| 122 | } |
| 123 | else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKE)) { |
| 124 | result->setStyle(SkPaint::kStroke_Style); |
| 125 | } |
| 126 | else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKEANDFILL)) { |
| 127 | result->setStyle(SkPaint::kStrokeAndFill_Style); |
| 128 | } |
| 129 | } |
| 130 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH)) { |
| 131 | float strokeWidth = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH].asFloat(); |
| 132 | result->setStrokeWidth(strokeWidth); |
| 133 | } |
| 134 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_ANTIALIAS)) { |
| 135 | result->setAntiAlias(jsonPaint[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
| 136 | } |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame^] | 137 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_BLUR)) { |
| 138 | Json::Value blur = jsonPaint[SKJSONCANVAS_ATTRIBUTE_BLUR]; |
| 139 | SkScalar sigma = blur[SKJSONCANVAS_ATTRIBUTE_SIGMA].asFloat(); |
| 140 | SkBlurStyle style; |
| 141 | const char* jsonStyle = blur[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); |
| 142 | if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_NORMAL)) { |
| 143 | style = SkBlurStyle::kNormal_SkBlurStyle; |
| 144 | } |
| 145 | else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_SOLID)) { |
| 146 | style = SkBlurStyle::kSolid_SkBlurStyle; |
| 147 | } |
| 148 | else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_OUTER)) { |
| 149 | style = SkBlurStyle::kOuter_SkBlurStyle; |
| 150 | } |
| 151 | else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_INNER)) { |
| 152 | style = SkBlurStyle::kInner_SkBlurStyle; |
| 153 | } |
| 154 | else { |
| 155 | SkASSERT(false); |
| 156 | style = SkBlurStyle::kNormal_SkBlurStyle; |
| 157 | } |
| 158 | SkBlurMaskFilter::BlurFlags flags; |
| 159 | const char* jsonQuality = blur[SKJSONCANVAS_ATTRIBUTE_QUALITY].asCString(); |
| 160 | if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_LOW)) { |
| 161 | flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag; |
| 162 | } |
| 163 | else if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_HIGH)) { |
| 164 | flags = SkBlurMaskFilter::BlurFlags::kHighQuality_BlurFlag; |
| 165 | } |
| 166 | else { |
| 167 | SkASSERT(false); |
| 168 | flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag; |
| 169 | } |
| 170 | result->setMaskFilter(SkBlurMaskFilter::Create(style, sigma, flags)); |
| 171 | } |
| 172 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_DASHING)) { |
| 173 | Json::Value dash = jsonPaint[SKJSONCANVAS_ATTRIBUTE_DASHING]; |
| 174 | Json::Value jsonIntervals = dash[SKJSONCANVAS_ATTRIBUTE_INTERVALS]; |
| 175 | Json::ArrayIndex count = jsonIntervals.size(); |
| 176 | SkScalar* intervals = (SkScalar*) sk_malloc_throw(count * sizeof(SkScalar)); |
| 177 | for (Json::ArrayIndex i = 0; i < count; i++) { |
| 178 | intervals[i] = jsonIntervals[i].asFloat(); |
| 179 | } |
| 180 | SkScalar phase = dash[SKJSONCANVAS_ATTRIBUTE_PHASE].asFloat(); |
| 181 | result->setPathEffect(SkDashPathEffect::Create(intervals, count, phase)); |
| 182 | free(intervals); |
| 183 | } |
| 184 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTALIGN)) { |
| 185 | SkPaint::Align textAlign; |
| 186 | const char* jsonAlign = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN].asCString(); |
| 187 | if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_LEFT)) { |
| 188 | textAlign = SkPaint::kLeft_Align; |
| 189 | } |
| 190 | else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_CENTER)) { |
| 191 | textAlign = SkPaint::kCenter_Align; |
| 192 | } |
| 193 | else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_RIGHT)) { |
| 194 | textAlign = SkPaint::kRight_Align; |
| 195 | } |
| 196 | else { |
| 197 | SkASSERT(false); |
| 198 | textAlign = SkPaint::kLeft_Align; |
| 199 | } |
| 200 | } |
| 201 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSIZE)) { |
| 202 | float textSize = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSIZE].asFloat(); |
| 203 | result->setTextSize(textSize); |
| 204 | } |
| 205 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX)) { |
| 206 | float textScaleX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX].asFloat(); |
| 207 | result->setTextScaleX(textScaleX); |
| 208 | } |
| 209 | if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) { |
| 210 | float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat(); |
| 211 | result->setTextSkewX(textSkewX); |
| 212 | } |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) { |
| 216 | Json::Value rect = command[name]; |
| 217 | result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3].asFloat()); |
| 218 | } |
| 219 | |
| 220 | void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result) { |
| 221 | Json::Value rrect = command[name]; |
| 222 | SkVector radii[4] = { |
| 223 | { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, |
| 224 | { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, |
| 225 | { rrect[3][0].asFloat(), rrect[3][1].asFloat() }, |
| 226 | { rrect[4][0].asFloat(), rrect[4][1].asFloat() } |
| 227 | }; |
| 228 | result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asFloat(), |
| 229 | rrect[0][2].asFloat(), rrect[0][3].asFloat()), |
| 230 | radii); |
| 231 | } |
| 232 | |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame^] | 233 | void Renderer::getPath(Json::Value& command, SkPath* result) { |
| 234 | Json::Value path = command[SKJSONCANVAS_ATTRIBUTE_PATH]; |
| 235 | const char* fillType = path[SKJSONCANVAS_ATTRIBUTE_FILLTYPE].asCString(); |
| 236 | if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_WINDING)) { |
| 237 | result->setFillType(SkPath::kWinding_FillType); |
| 238 | } |
| 239 | else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_EVENODD)) { |
| 240 | result->setFillType(SkPath::kEvenOdd_FillType); |
| 241 | } |
| 242 | else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEWINDING)) { |
| 243 | result->setFillType(SkPath::kInverseWinding_FillType); |
| 244 | } |
| 245 | else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEEVENODD)) { |
| 246 | result->setFillType(SkPath::kInverseEvenOdd_FillType); |
| 247 | } |
| 248 | Json::Value verbs = path[SKJSONCANVAS_ATTRIBUTE_VERBS]; |
| 249 | for (Json::ArrayIndex i = 0; i < verbs.size(); i++) { |
| 250 | Json::Value verb = verbs[i]; |
| 251 | if (verb.isString()) { |
| 252 | SkASSERT(!strcmp(verb.asCString(), SKJSONCANVAS_VERB_CLOSE)); |
| 253 | result->close(); |
| 254 | } |
| 255 | else { |
| 256 | if (verb.isMember(SKJSONCANVAS_VERB_MOVE)) { |
| 257 | Json::Value move = verb[SKJSONCANVAS_VERB_MOVE]; |
| 258 | result->moveTo(move[0].asFloat(), move[1].asFloat()); |
| 259 | } |
| 260 | else if (verb.isMember(SKJSONCANVAS_VERB_LINE)) { |
| 261 | Json::Value line = verb[SKJSONCANVAS_VERB_LINE]; |
| 262 | result->lineTo(line[0].asFloat(), line[1].asFloat()); |
| 263 | } |
| 264 | else if (verb.isMember(SKJSONCANVAS_VERB_QUAD)) { |
| 265 | Json::Value quad = verb[SKJSONCANVAS_VERB_QUAD]; |
| 266 | result->quadTo(quad[0][0].asFloat(), quad[0][1].asFloat(), |
| 267 | quad[1][0].asFloat(), quad[1][1].asFloat()); |
| 268 | } |
| 269 | else if (verb.isMember(SKJSONCANVAS_VERB_CUBIC)) { |
| 270 | Json::Value cubic = verb[SKJSONCANVAS_VERB_CUBIC]; |
| 271 | result->cubicTo(cubic[0][0].asFloat(), cubic[0][1].asFloat(), |
| 272 | cubic[1][0].asFloat(), cubic[1][1].asFloat(), |
| 273 | cubic[2][0].asFloat(), cubic[2][1].asFloat()); |
| 274 | } |
| 275 | else if (verb.isMember(SKJSONCANVAS_VERB_CONIC)) { |
| 276 | Json::Value conic = verb[SKJSONCANVAS_VERB_CONIC]; |
| 277 | result->conicTo(conic[0][0].asFloat(), conic[0][1].asFloat(), |
| 278 | conic[1][0].asFloat(), conic[1][1].asFloat(), |
| 279 | conic[2].asFloat()); |
| 280 | } |
| 281 | else { |
| 282 | SkASSERT(false); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | SkRegion::Op Renderer::getRegionOp(Json::Value& command) { |
| 289 | const char* op = command[SKJSONCANVAS_ATTRIBUTE_REGIONOP].asCString(); |
| 290 | if (!strcmp(op, SKJSONCANVAS_REGIONOP_DIFFERENCE)) { |
| 291 | return SkRegion::kDifference_Op; |
| 292 | } |
| 293 | else if (!strcmp(op, SKJSONCANVAS_REGIONOP_INTERSECT)) { |
| 294 | return SkRegion::kIntersect_Op; |
| 295 | } |
| 296 | else if (!strcmp(op, SKJSONCANVAS_REGIONOP_UNION)) { |
| 297 | return SkRegion::kUnion_Op; |
| 298 | } |
| 299 | else if (!strcmp(op, SKJSONCANVAS_REGIONOP_XOR)) { |
| 300 | return SkRegion::kXOR_Op; |
| 301 | } |
| 302 | else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE)) { |
| 303 | return SkRegion::kReverseDifference_Op; |
| 304 | } |
| 305 | else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REPLACE)) { |
| 306 | return SkRegion::kReplace_Op; |
| 307 | } |
| 308 | SkASSERT(false); |
| 309 | return SkRegion::kIntersect_Op; |
| 310 | } |
| 311 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 312 | void Renderer::processMatrix(Json::Value& command, SkCanvas* target) { |
| 313 | Json::Value jsonMatrix = command[SKJSONCANVAS_ATTRIBUTE_MATRIX]; |
| 314 | SkMatrix matrix; |
| 315 | SkScalar values[] = { |
| 316 | jsonMatrix[0][0].asFloat(), jsonMatrix[0][1].asFloat(), jsonMatrix[0][2].asFloat(), |
| 317 | jsonMatrix[1][0].asFloat(), jsonMatrix[1][1].asFloat(), jsonMatrix[1][2].asFloat(), |
| 318 | jsonMatrix[2][0].asFloat(), jsonMatrix[2][1].asFloat(), jsonMatrix[2][2].asFloat() |
| 319 | }; |
| 320 | matrix.set9(values); |
| 321 | target->setMatrix(matrix); |
| 322 | } |
| 323 | |
| 324 | void Renderer::processSave(Json::Value& command, SkCanvas* target) { |
| 325 | target->save(); |
| 326 | } |
| 327 | |
| 328 | void Renderer::processRestore(Json::Value& command, SkCanvas* target) { |
| 329 | target->restore(); |
| 330 | } |
| 331 | |
| 332 | void Renderer::processPaint(Json::Value& command, SkCanvas* target) { |
| 333 | SkPaint paint; |
| 334 | this->getPaint(command, &paint); |
| 335 | target->drawPaint(paint); |
| 336 | } |
| 337 | |
| 338 | void Renderer::processRect(Json::Value& command, SkCanvas* target) { |
| 339 | SkRect rect; |
| 340 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); |
| 341 | SkPaint paint; |
| 342 | this->getPaint(command, &paint); |
| 343 | target->drawRect(rect, paint); |
| 344 | } |
| 345 | |
| 346 | void Renderer::processRRect(Json::Value& command, SkCanvas* target) { |
| 347 | SkRRect rrect; |
| 348 | this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect); |
| 349 | SkPaint paint; |
| 350 | this->getPaint(command, &paint); |
| 351 | target->drawRRect(rrect, paint); |
| 352 | } |
| 353 | |
| 354 | void Renderer::processOval(Json::Value& command, SkCanvas* target) { |
| 355 | SkRect rect; |
| 356 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); |
| 357 | SkPaint paint; |
| 358 | this->getPaint(command, &paint); |
| 359 | target->drawOval(rect, paint); |
| 360 | } |
| 361 | |
| 362 | void Renderer::processPath(Json::Value& command, SkCanvas* target) { |
| 363 | Json::Value jsonPath = command[SKJSONCANVAS_ATTRIBUTE_PATH]; |
| 364 | SkPath path; |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame^] | 365 | this->getPath(command, &path); |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 366 | SkPaint paint; |
| 367 | this->getPaint(command, &paint); |
| 368 | target->drawPath(path, paint); |
| 369 | } |
| 370 | |
| 371 | void Renderer::processText(Json::Value& command, SkCanvas* target) { |
| 372 | const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString(); |
| 373 | SkPaint paint; |
| 374 | this->getPaint(command, &paint); |
| 375 | Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; |
| 376 | target->drawText(text, strlen(text), coords[0].asFloat(), coords[1].asFloat(), paint); |
| 377 | } |
| 378 | |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame^] | 379 | void Renderer::processPosText(Json::Value& command, SkCanvas* target) { |
| 380 | const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString(); |
| 381 | SkPaint paint; |
| 382 | this->getPaint(command, &paint); |
| 383 | Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; |
| 384 | int count = (int) coords.size(); |
| 385 | SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint)); |
| 386 | for (int i = 0; i < count; i++) { |
| 387 | points[i] = SkPoint::Make(coords[i][0].asFloat(), coords[i][1].asFloat()); |
| 388 | } |
| 389 | target->drawPosText(text, strlen(text), points, paint); |
| 390 | free(points); |
| 391 | } |
| 392 | |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 393 | void Renderer::processPoints(Json::Value& command, SkCanvas* target) { |
| 394 | SkCanvas::PointMode mode; |
| 395 | const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString(); |
| 396 | if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POINTS)) { |
| 397 | mode = SkCanvas::kPoints_PointMode; |
| 398 | } |
| 399 | else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_LINES)) { |
| 400 | mode = SkCanvas::kLines_PointMode; |
| 401 | } |
| 402 | else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POLYGON)) { |
| 403 | mode = SkCanvas::kPolygon_PointMode; |
| 404 | } |
| 405 | else { |
| 406 | SkASSERT(false); |
| 407 | return; |
| 408 | } |
| 409 | Json::Value jsonPoints = command[SKJSONCANVAS_ATTRIBUTE_POINTS]; |
| 410 | int count = (int) jsonPoints.size(); |
| 411 | SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint)); |
| 412 | for (int i = 0; i < count; i++) { |
| 413 | points[i] = SkPoint::Make(jsonPoints[i][0].asFloat(), jsonPoints[i][1].asFloat()); |
| 414 | } |
| 415 | SkPaint paint; |
| 416 | this->getPaint(command, &paint); |
| 417 | target->drawPoints(mode, count, points, paint); |
| 418 | free(points); |
| 419 | } |
| 420 | |
| 421 | void Renderer::processClipRect(Json::Value& command, SkCanvas* target) { |
| 422 | SkRect rect; |
| 423 | this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); |
ethannicholas | 78fc22a | 2016-01-29 07:15:08 -0800 | [diff] [blame^] | 424 | target->clipRect(rect, this->getRegionOp(command), |
| 425 | command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
| 426 | } |
| 427 | |
| 428 | void Renderer::processClipRRect(Json::Value& command, SkCanvas* target) { |
| 429 | SkRRect rrect; |
| 430 | this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect); |
| 431 | target->clipRRect(rrect, this->getRegionOp(command), |
| 432 | command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
| 433 | } |
| 434 | |
| 435 | void Renderer::processClipPath(Json::Value& command, SkCanvas* target) { |
| 436 | SkPath path; |
| 437 | this->getPath(command, &path); |
| 438 | target->clipPath(path, this->getRegionOp(command), |
| 439 | command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
ethannicholas | 978d08a | 2016-01-26 07:47:57 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | void render(const char* json, SkCanvas* target) { |
| 443 | Renderer renderer; |
| 444 | Json::Reader reader; |
| 445 | Json::Value root; |
| 446 | if (reader.parse(std::string(json), root)) { |
| 447 | SkASSERT(root[SKJSONCANVAS_VERSION].asInt() == 1); |
| 448 | Json::Value commands = root[SKJSONCANVAS_COMMANDS]; |
| 449 | for (Json::ArrayIndex i = 0; i < commands.size(); i++) { |
| 450 | renderer.processCommand(commands[i], target); |
| 451 | } |
| 452 | } |
| 453 | else { |
| 454 | SkDebugf(json); |
| 455 | SkFAIL("json parse failure"); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | } // namespace |