blob: a830fe0fb5f84cb79cc91c975e8bca9e9b57d59b [file] [log] [blame]
ethannicholas978d08a2016-01-26 07:47:57 -08001/*
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"
ethannicholas78fc22a2016-01-29 07:15:08 -08009
10#include "SkBlurMaskFilter.h"
11#include "SkDashPathEffect.h"
ethannicholas978d08a2016-01-26 07:47:57 -080012#include "SkJSONCanvas.h"
13#include "SkJSONCPP.h"
14#include "SkPath.h"
15
16namespace SkJSONRenderer {
17
18class Renderer {
19public:
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
ethannicholas78fc22a2016-01-29 07:15:08 -080026 void getPath(Json::Value& command, SkPath* path);
27
28 SkRegion::Op getRegionOp(Json::Value& command);
29
ethannicholas978d08a2016-01-26 07:47:57 -080030 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
ethannicholas78fc22a2016-01-29 07:15:08 -080050 void processPosText(Json::Value& command, SkCanvas* target);
51
ethannicholas978d08a2016-01-26 07:47:57 -080052 void processPoints(Json::Value& command, SkCanvas* target);
53
54 void processClipRect(Json::Value& command, SkCanvas* target);
ethannicholas78fc22a2016-01-29 07:15:08 -080055
56 void processClipRRect(Json::Value& command, SkCanvas* target);
57
58 void processClipPath(Json::Value& command, SkCanvas* target);
ethannicholas978d08a2016-01-26 07:47:57 -080059};
60
61void 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 }
ethannicholas78fc22a2016-01-29 07:15:08 -080091 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) {
92 this->processPosText(command, target);
93 }
ethannicholas978d08a2016-01-26 07:47:57 -080094 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 }
ethannicholas78fc22a2016-01-29 07:15:08 -0800100 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 }
ethannicholas978d08a2016-01-26 07:47:57 -0800106 else {
107 SkDebugf("unsupported JSON command: %s\n", name);
108 }
109}
110
111void 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 }
ethannicholas78fc22a2016-01-29 07:15:08 -0800137 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 }
ethannicholasc05f7c32016-01-29 08:02:34 -0800200 result->setTextAlign(textAlign);
ethannicholas78fc22a2016-01-29 07:15:08 -0800201 }
202 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSIZE)) {
203 float textSize = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSIZE].asFloat();
204 result->setTextSize(textSize);
205 }
206 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX)) {
207 float textScaleX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX].asFloat();
208 result->setTextScaleX(textScaleX);
209 }
210 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) {
211 float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat();
212 result->setTextSkewX(textSkewX);
213 }
ethannicholas978d08a2016-01-26 07:47:57 -0800214}
215
216void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) {
217 Json::Value rect = command[name];
218 result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3].asFloat());
219}
220
221void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result) {
222 Json::Value rrect = command[name];
223 SkVector radii[4] = {
224 { rrect[1][0].asFloat(), rrect[1][1].asFloat() },
225 { rrect[2][0].asFloat(), rrect[2][1].asFloat() },
226 { rrect[3][0].asFloat(), rrect[3][1].asFloat() },
227 { rrect[4][0].asFloat(), rrect[4][1].asFloat() }
228 };
229 result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asFloat(),
230 rrect[0][2].asFloat(), rrect[0][3].asFloat()),
231 radii);
232}
233
ethannicholas78fc22a2016-01-29 07:15:08 -0800234void Renderer::getPath(Json::Value& command, SkPath* result) {
235 Json::Value path = command[SKJSONCANVAS_ATTRIBUTE_PATH];
236 const char* fillType = path[SKJSONCANVAS_ATTRIBUTE_FILLTYPE].asCString();
237 if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_WINDING)) {
238 result->setFillType(SkPath::kWinding_FillType);
239 }
240 else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_EVENODD)) {
241 result->setFillType(SkPath::kEvenOdd_FillType);
242 }
243 else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEWINDING)) {
244 result->setFillType(SkPath::kInverseWinding_FillType);
245 }
246 else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEEVENODD)) {
247 result->setFillType(SkPath::kInverseEvenOdd_FillType);
248 }
249 Json::Value verbs = path[SKJSONCANVAS_ATTRIBUTE_VERBS];
250 for (Json::ArrayIndex i = 0; i < verbs.size(); i++) {
251 Json::Value verb = verbs[i];
252 if (verb.isString()) {
253 SkASSERT(!strcmp(verb.asCString(), SKJSONCANVAS_VERB_CLOSE));
254 result->close();
255 }
256 else {
257 if (verb.isMember(SKJSONCANVAS_VERB_MOVE)) {
258 Json::Value move = verb[SKJSONCANVAS_VERB_MOVE];
259 result->moveTo(move[0].asFloat(), move[1].asFloat());
260 }
261 else if (verb.isMember(SKJSONCANVAS_VERB_LINE)) {
262 Json::Value line = verb[SKJSONCANVAS_VERB_LINE];
263 result->lineTo(line[0].asFloat(), line[1].asFloat());
264 }
265 else if (verb.isMember(SKJSONCANVAS_VERB_QUAD)) {
266 Json::Value quad = verb[SKJSONCANVAS_VERB_QUAD];
267 result->quadTo(quad[0][0].asFloat(), quad[0][1].asFloat(),
268 quad[1][0].asFloat(), quad[1][1].asFloat());
269 }
270 else if (verb.isMember(SKJSONCANVAS_VERB_CUBIC)) {
271 Json::Value cubic = verb[SKJSONCANVAS_VERB_CUBIC];
272 result->cubicTo(cubic[0][0].asFloat(), cubic[0][1].asFloat(),
273 cubic[1][0].asFloat(), cubic[1][1].asFloat(),
274 cubic[2][0].asFloat(), cubic[2][1].asFloat());
275 }
276 else if (verb.isMember(SKJSONCANVAS_VERB_CONIC)) {
277 Json::Value conic = verb[SKJSONCANVAS_VERB_CONIC];
278 result->conicTo(conic[0][0].asFloat(), conic[0][1].asFloat(),
279 conic[1][0].asFloat(), conic[1][1].asFloat(),
280 conic[2].asFloat());
281 }
282 else {
283 SkASSERT(false);
284 }
285 }
286 }
287}
288
289SkRegion::Op Renderer::getRegionOp(Json::Value& command) {
290 const char* op = command[SKJSONCANVAS_ATTRIBUTE_REGIONOP].asCString();
291 if (!strcmp(op, SKJSONCANVAS_REGIONOP_DIFFERENCE)) {
292 return SkRegion::kDifference_Op;
293 }
294 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_INTERSECT)) {
295 return SkRegion::kIntersect_Op;
296 }
297 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_UNION)) {
298 return SkRegion::kUnion_Op;
299 }
300 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_XOR)) {
301 return SkRegion::kXOR_Op;
302 }
303 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE)) {
304 return SkRegion::kReverseDifference_Op;
305 }
306 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REPLACE)) {
307 return SkRegion::kReplace_Op;
308 }
309 SkASSERT(false);
310 return SkRegion::kIntersect_Op;
311}
312
ethannicholas978d08a2016-01-26 07:47:57 -0800313void Renderer::processMatrix(Json::Value& command, SkCanvas* target) {
314 Json::Value jsonMatrix = command[SKJSONCANVAS_ATTRIBUTE_MATRIX];
315 SkMatrix matrix;
316 SkScalar values[] = {
317 jsonMatrix[0][0].asFloat(), jsonMatrix[0][1].asFloat(), jsonMatrix[0][2].asFloat(),
318 jsonMatrix[1][0].asFloat(), jsonMatrix[1][1].asFloat(), jsonMatrix[1][2].asFloat(),
319 jsonMatrix[2][0].asFloat(), jsonMatrix[2][1].asFloat(), jsonMatrix[2][2].asFloat()
320 };
321 matrix.set9(values);
322 target->setMatrix(matrix);
323}
324
325void Renderer::processSave(Json::Value& command, SkCanvas* target) {
326 target->save();
327}
328
329void Renderer::processRestore(Json::Value& command, SkCanvas* target) {
330 target->restore();
331}
332
333void Renderer::processPaint(Json::Value& command, SkCanvas* target) {
334 SkPaint paint;
335 this->getPaint(command, &paint);
336 target->drawPaint(paint);
337}
338
339void Renderer::processRect(Json::Value& command, SkCanvas* target) {
340 SkRect rect;
341 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect);
342 SkPaint paint;
343 this->getPaint(command, &paint);
344 target->drawRect(rect, paint);
345}
346
347void Renderer::processRRect(Json::Value& command, SkCanvas* target) {
348 SkRRect rrect;
349 this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect);
350 SkPaint paint;
351 this->getPaint(command, &paint);
352 target->drawRRect(rrect, paint);
353}
354
355void Renderer::processOval(Json::Value& command, SkCanvas* target) {
356 SkRect rect;
357 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect);
358 SkPaint paint;
359 this->getPaint(command, &paint);
360 target->drawOval(rect, paint);
361}
362
363void Renderer::processPath(Json::Value& command, SkCanvas* target) {
364 Json::Value jsonPath = command[SKJSONCANVAS_ATTRIBUTE_PATH];
365 SkPath path;
ethannicholas78fc22a2016-01-29 07:15:08 -0800366 this->getPath(command, &path);
ethannicholas978d08a2016-01-26 07:47:57 -0800367 SkPaint paint;
368 this->getPaint(command, &paint);
369 target->drawPath(path, paint);
370}
371
372void Renderer::processText(Json::Value& command, SkCanvas* target) {
373 const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString();
374 SkPaint paint;
375 this->getPaint(command, &paint);
376 Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS];
377 target->drawText(text, strlen(text), coords[0].asFloat(), coords[1].asFloat(), paint);
378}
379
ethannicholas78fc22a2016-01-29 07:15:08 -0800380void Renderer::processPosText(Json::Value& command, SkCanvas* target) {
381 const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString();
382 SkPaint paint;
383 this->getPaint(command, &paint);
384 Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS];
385 int count = (int) coords.size();
386 SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
387 for (int i = 0; i < count; i++) {
388 points[i] = SkPoint::Make(coords[i][0].asFloat(), coords[i][1].asFloat());
389 }
390 target->drawPosText(text, strlen(text), points, paint);
391 free(points);
392}
393
ethannicholas978d08a2016-01-26 07:47:57 -0800394void Renderer::processPoints(Json::Value& command, SkCanvas* target) {
395 SkCanvas::PointMode mode;
396 const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString();
397 if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POINTS)) {
398 mode = SkCanvas::kPoints_PointMode;
399 }
400 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_LINES)) {
401 mode = SkCanvas::kLines_PointMode;
402 }
403 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POLYGON)) {
404 mode = SkCanvas::kPolygon_PointMode;
405 }
406 else {
407 SkASSERT(false);
408 return;
409 }
410 Json::Value jsonPoints = command[SKJSONCANVAS_ATTRIBUTE_POINTS];
411 int count = (int) jsonPoints.size();
412 SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
413 for (int i = 0; i < count; i++) {
414 points[i] = SkPoint::Make(jsonPoints[i][0].asFloat(), jsonPoints[i][1].asFloat());
415 }
416 SkPaint paint;
417 this->getPaint(command, &paint);
418 target->drawPoints(mode, count, points, paint);
419 free(points);
420}
421
422void Renderer::processClipRect(Json::Value& command, SkCanvas* target) {
423 SkRect rect;
424 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect);
ethannicholas78fc22a2016-01-29 07:15:08 -0800425 target->clipRect(rect, this->getRegionOp(command),
426 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
427}
428
429void Renderer::processClipRRect(Json::Value& command, SkCanvas* target) {
430 SkRRect rrect;
431 this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect);
432 target->clipRRect(rrect, this->getRegionOp(command),
433 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
434}
435
436void Renderer::processClipPath(Json::Value& command, SkCanvas* target) {
437 SkPath path;
438 this->getPath(command, &path);
439 target->clipPath(path, this->getRegionOp(command),
440 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
ethannicholas978d08a2016-01-26 07:47:57 -0800441}
442
443void render(const char* json, SkCanvas* target) {
444 Renderer renderer;
445 Json::Reader reader;
446 Json::Value root;
447 if (reader.parse(std::string(json), root)) {
448 SkASSERT(root[SKJSONCANVAS_VERSION].asInt() == 1);
449 Json::Value commands = root[SKJSONCANVAS_COMMANDS];
450 for (Json::ArrayIndex i = 0; i < commands.size(); i++) {
451 renderer.processCommand(commands[i], target);
452 }
453 }
454 else {
455 SkDebugf(json);
456 SkFAIL("json parse failure");
457 }
458}
459
460} // namespace