aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/json/SkJSONRenderer.cpp
blob: a830fe0fb5f84cb79cc91c975e8bca9e9b57d59b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkJSONRenderer.h"

#include "SkBlurMaskFilter.h"
#include "SkDashPathEffect.h"
#include "SkJSONCanvas.h"
#include "SkJSONCPP.h"
#include "SkPath.h"

namespace SkJSONRenderer {

class Renderer {
public:
    void getPaint(Json::Value& command, SkPaint* paint);

    void getRect(Json::Value& command, const char* name, SkRect* rect);

    void getRRect(Json::Value& command, const char* name, SkRRect* rrect);

    void getPath(Json::Value& command, SkPath* path);

    SkRegion::Op getRegionOp(Json::Value& command);

    void processCommand(Json::Value& command, SkCanvas* target);

    void processMatrix(Json::Value& command, SkCanvas* target);

    void processSave(Json::Value& command, SkCanvas* target);

    void processRestore(Json::Value& command, SkCanvas* target);

    void processPaint(Json::Value& command, SkCanvas* target);

    void processRect(Json::Value& command, SkCanvas* target);

    void processRRect(Json::Value& command, SkCanvas* target);

    void processOval(Json::Value& command, SkCanvas* target);

    void processPath(Json::Value& command, SkCanvas* target);

    void processText(Json::Value& command, SkCanvas* target);

    void processPosText(Json::Value& command, SkCanvas* target);

    void processPoints(Json::Value& command, SkCanvas* target);

    void processClipRect(Json::Value& command, SkCanvas* target);
    
    void processClipRRect(Json::Value& command, SkCanvas* target);

    void processClipPath(Json::Value& command, SkCanvas* target);
};

void Renderer::processCommand(Json::Value& command, SkCanvas* target) {
    const char* name = command[SKJSONCANVAS_COMMAND].asCString();
    // TODO speed this up with a hash
    if (!strcmp(name, SKJSONCANVAS_COMMAND_MATRIX)) {
        this->processMatrix(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVE)) {
        this->processSave(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_RESTORE)) {
        this->processRestore(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_PAINT)) {
        this->processPaint(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_RECT)) {
        this->processRect(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_RRECT)) {
        this->processRRect(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_OVAL)) {
        this->processOval(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_PATH)) {
        this->processPath(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) {
        this->processText(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) {
        this->processPosText(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) {
        this->processPoints(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRECT)) {
        this->processClipRect(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRRECT)) {
        this->processClipRRect(command, target);
    }
    else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPPATH)) {
        this->processClipPath(command, target);
    }
    else {
        SkDebugf("unsupported JSON command: %s\n", name);
    }
}

void Renderer::getPaint(Json::Value& command, SkPaint* result) {
    Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT];
    if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_COLOR)) {
        Json::Value color = jsonPaint[SKJSONCANVAS_ATTRIBUTE_COLOR];
        result->setColor(SkColorSetARGB(color[0].asInt(), color[1].asInt(), color[2].asInt(),
                         color[3].asInt()));
    }
    if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STYLE)) {
        const char* style = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString();
        if (!strcmp(style, SKJSONCANVAS_STYLE_FILL)) {
            result->setStyle(SkPaint::kFill_Style);
        }
        else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKE)) {
            result->setStyle(SkPaint::kStroke_Style);
        }
        else if (!strcmp(style, SKJSONCANVAS_STYLE_STROKEANDFILL)) {
            result->setStyle(SkPaint::kStrokeAndFill_Style);
        }
    }
    if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH)) {
        float strokeWidth = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH].asFloat();
        result->setStrokeWidth(strokeWidth);
    }
    if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_ANTIALIAS)) {
        result->setAntiAlias(jsonPaint[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
    }
    if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_BLUR)) {
        Json::Value blur = jsonPaint[SKJSONCANVAS_ATTRIBUTE_BLUR];
        SkScalar sigma = blur[SKJSONCANVAS_ATTRIBUTE_SIGMA].asFloat();
        SkBlurStyle style;
        const char* jsonStyle = blur[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString();
        if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_NORMAL)) {
            style = SkBlurStyle::kNormal_SkBlurStyle;
        }
        else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_SOLID)) {
            style = SkBlurStyle::kSolid_SkBlurStyle;
        }
        else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_OUTER)) {
            style = SkBlurStyle::kOuter_SkBlurStyle;
        }
        else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_INNER)) {
            style = SkBlurStyle::kInner_SkBlurStyle;
        }
        else {
            SkASSERT(false);
            style = SkBlurStyle::kNormal_SkBlurStyle;
        }
        SkBlurMaskFilter::BlurFlags flags;
        const char* jsonQuality = blur[SKJSONCANVAS_ATTRIBUTE_QUALITY].asCString();
        if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_LOW)) {
            flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag;
        }
        else if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_HIGH)) {
            flags = SkBlurMaskFilter::BlurFlags::kHighQuality_BlurFlag;
        }
        else {
            SkASSERT(false);
            flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag;
        }
        result->setMaskFilter(SkBlurMaskFilter::Create(style, sigma, flags));
    }
    if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_DASHING)) {
        Json::Value dash = jsonPaint[SKJSONCANVAS_ATTRIBUTE_DASHING];
        Json::Value jsonIntervals = dash[SKJSONCANVAS_ATTRIBUTE_INTERVALS];
        Json::ArrayIndex count = jsonIntervals.size();
        SkScalar* intervals = (SkScalar*) sk_malloc_throw(count * sizeof(SkScalar));
        for (Json::ArrayIndex i = 0; i < count; i++) {
            intervals[i] = jsonIntervals[i].asFloat();
        }
        SkScalar phase = dash[SKJSONCANVAS_ATTRIBUTE_PHASE].asFloat();
        result->setPathEffect(SkDashPathEffect::Create(intervals, count, phase));
        free(intervals);
    }
    if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTALIGN)) {
        SkPaint::Align textAlign;
        const char* jsonAlign = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN].asCString();
        if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_LEFT)) {
            textAlign = SkPaint::kLeft_Align;
        }
        else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_CENTER)) {
            textAlign = SkPaint::kCenter_Align;
        }
        else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_RIGHT)) {
            textAlign = SkPaint::kRight_Align;
        }
        else {
            SkASSERT(false);
            textAlign = SkPaint::kLeft_Align;
        }
        result->setTextAlign(textAlign);
    }
    if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSIZE)) {
        float textSize = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSIZE].asFloat();
        result->setTextSize(textSize);
    }
    if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX)) {
        float textScaleX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX].asFloat();
        result->setTextScaleX(textScaleX);
    }
    if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) {
        float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat();
        result->setTextSkewX(textSkewX);
    }
}

void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) {
    Json::Value rect = command[name];
    result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3].asFloat());
}

void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result) {
    Json::Value rrect = command[name];
    SkVector radii[4] = {
                            { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, 
                            { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, 
                            { rrect[3][0].asFloat(), rrect[3][1].asFloat() }, 
                            { rrect[4][0].asFloat(), rrect[4][1].asFloat() }
                        };
    result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asFloat(), 
                                          rrect[0][2].asFloat(), rrect[0][3].asFloat()), 
                         radii);
}

void Renderer::getPath(Json::Value& command, SkPath* result) {
    Json::Value path = command[SKJSONCANVAS_ATTRIBUTE_PATH];
    const char* fillType = path[SKJSONCANVAS_ATTRIBUTE_FILLTYPE].asCString();
    if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_WINDING)) {
        result->setFillType(SkPath::kWinding_FillType);
    }
    else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_EVENODD)) {
        result->setFillType(SkPath::kEvenOdd_FillType);
    }
    else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEWINDING)) {
        result->setFillType(SkPath::kInverseWinding_FillType);
    }
    else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEEVENODD)) {
        result->setFillType(SkPath::kInverseEvenOdd_FillType);
    }
    Json::Value verbs = path[SKJSONCANVAS_ATTRIBUTE_VERBS];
    for (Json::ArrayIndex i = 0; i < verbs.size(); i++) {
        Json::Value verb = verbs[i];
        if (verb.isString()) {
            SkASSERT(!strcmp(verb.asCString(), SKJSONCANVAS_VERB_CLOSE));
            result->close();
        }
        else {
            if (verb.isMember(SKJSONCANVAS_VERB_MOVE)) {
                Json::Value move = verb[SKJSONCANVAS_VERB_MOVE];
                result->moveTo(move[0].asFloat(), move[1].asFloat());
            }
            else if (verb.isMember(SKJSONCANVAS_VERB_LINE)) {
                Json::Value line = verb[SKJSONCANVAS_VERB_LINE];
                result->lineTo(line[0].asFloat(), line[1].asFloat());
            }
            else if (verb.isMember(SKJSONCANVAS_VERB_QUAD)) {
                Json::Value quad = verb[SKJSONCANVAS_VERB_QUAD];
                result->quadTo(quad[0][0].asFloat(), quad[0][1].asFloat(),
                               quad[1][0].asFloat(), quad[1][1].asFloat());
            }
            else if (verb.isMember(SKJSONCANVAS_VERB_CUBIC)) {
                Json::Value cubic = verb[SKJSONCANVAS_VERB_CUBIC];
                result->cubicTo(cubic[0][0].asFloat(), cubic[0][1].asFloat(),
                                cubic[1][0].asFloat(), cubic[1][1].asFloat(),
                                cubic[2][0].asFloat(), cubic[2][1].asFloat());
            }
            else if (verb.isMember(SKJSONCANVAS_VERB_CONIC)) {
                Json::Value conic = verb[SKJSONCANVAS_VERB_CONIC];
                result->conicTo(conic[0][0].asFloat(), conic[0][1].asFloat(),
                                conic[1][0].asFloat(), conic[1][1].asFloat(),
                                conic[2].asFloat());
            }
            else {
                SkASSERT(false);
            }
        }
    }
}

SkRegion::Op Renderer::getRegionOp(Json::Value& command) {
    const char* op = command[SKJSONCANVAS_ATTRIBUTE_REGIONOP].asCString();
    if (!strcmp(op, SKJSONCANVAS_REGIONOP_DIFFERENCE)) {
        return SkRegion::kDifference_Op;
    }
    else if (!strcmp(op, SKJSONCANVAS_REGIONOP_INTERSECT)) {
        return SkRegion::kIntersect_Op;
    }
    else if (!strcmp(op, SKJSONCANVAS_REGIONOP_UNION)) {
        return SkRegion::kUnion_Op;
    }
    else if (!strcmp(op, SKJSONCANVAS_REGIONOP_XOR)) {
        return SkRegion::kXOR_Op;
    }
    else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE)) {
        return SkRegion::kReverseDifference_Op;
    }
    else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REPLACE)) {
        return SkRegion::kReplace_Op;
    }
    SkASSERT(false);
    return SkRegion::kIntersect_Op;
}

void Renderer::processMatrix(Json::Value& command, SkCanvas* target) {
    Json::Value jsonMatrix = command[SKJSONCANVAS_ATTRIBUTE_MATRIX];
    SkMatrix matrix;
    SkScalar values[] = { 
        jsonMatrix[0][0].asFloat(), jsonMatrix[0][1].asFloat(), jsonMatrix[0][2].asFloat(),
        jsonMatrix[1][0].asFloat(), jsonMatrix[1][1].asFloat(), jsonMatrix[1][2].asFloat(),
        jsonMatrix[2][0].asFloat(), jsonMatrix[2][1].asFloat(), jsonMatrix[2][2].asFloat() 
    };
    matrix.set9(values);
    target->setMatrix(matrix);
}

void Renderer::processSave(Json::Value& command, SkCanvas* target) {
    target->save();
}

void Renderer::processRestore(Json::Value& command, SkCanvas* target) {
    target->restore();
}

void Renderer::processPaint(Json::Value& command, SkCanvas* target) {
    SkPaint paint;
    this->getPaint(command, &paint);
    target->drawPaint(paint);
}

void Renderer::processRect(Json::Value& command, SkCanvas* target) {
    SkRect rect;
    this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect);
    SkPaint paint;
    this->getPaint(command, &paint);
    target->drawRect(rect, paint);
}

void Renderer::processRRect(Json::Value& command, SkCanvas* target) {
    SkRRect rrect;
    this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect);
    SkPaint paint;
    this->getPaint(command, &paint);
    target->drawRRect(rrect, paint);
}

void Renderer::processOval(Json::Value& command, SkCanvas* target) {
    SkRect rect;
    this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect);
    SkPaint paint;
    this->getPaint(command, &paint);
    target->drawOval(rect, paint);
}

void Renderer::processPath(Json::Value& command, SkCanvas* target) {
    Json::Value jsonPath = command[SKJSONCANVAS_ATTRIBUTE_PATH];
    SkPath path;
    this->getPath(command, &path);
    SkPaint paint;
    this->getPaint(command, &paint);
    target->drawPath(path, paint);
}

void Renderer::processText(Json::Value& command, SkCanvas* target) {
    const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString();
    SkPaint paint;
    this->getPaint(command, &paint);
    Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS];
    target->drawText(text, strlen(text), coords[0].asFloat(), coords[1].asFloat(), paint);
}

void Renderer::processPosText(Json::Value& command, SkCanvas* target) {
    const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString();
    SkPaint paint;
    this->getPaint(command, &paint);
    Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS];
    int count = (int) coords.size();
    SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
    for (int i = 0; i < count; i++) {
        points[i] = SkPoint::Make(coords[i][0].asFloat(), coords[i][1].asFloat());
    }
    target->drawPosText(text, strlen(text), points, paint);
    free(points);
}

void Renderer::processPoints(Json::Value& command, SkCanvas* target) {
    SkCanvas::PointMode mode;
    const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString();
    if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POINTS)) {
        mode = SkCanvas::kPoints_PointMode;
    }
    else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_LINES)) {
        mode = SkCanvas::kLines_PointMode;
    }
    else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POLYGON)) {
        mode = SkCanvas::kPolygon_PointMode;
    }
    else {
        SkASSERT(false);
        return;
    }
    Json::Value jsonPoints = command[SKJSONCANVAS_ATTRIBUTE_POINTS];
    int count = (int) jsonPoints.size();
    SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
    for (int i = 0; i < count; i++) {
        points[i] = SkPoint::Make(jsonPoints[i][0].asFloat(), jsonPoints[i][1].asFloat());
    }
    SkPaint paint;
    this->getPaint(command, &paint);
    target->drawPoints(mode, count, points, paint);
    free(points);
}

void Renderer::processClipRect(Json::Value& command, SkCanvas* target) {
    SkRect rect;
    this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect);
    target->clipRect(rect, this->getRegionOp(command), 
                     command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
}

void Renderer::processClipRRect(Json::Value& command, SkCanvas* target) {
    SkRRect rrect;
    this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect);
    target->clipRRect(rrect, this->getRegionOp(command), 
                      command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
}

void Renderer::processClipPath(Json::Value& command, SkCanvas* target) {
    SkPath path;
    this->getPath(command, &path);
    target->clipPath(path, this->getRegionOp(command), 
                     command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
}

void render(const char* json, SkCanvas* target) {
    Renderer renderer;
    Json::Reader reader;
    Json::Value root;
    if (reader.parse(std::string(json), root)) {
        SkASSERT(root[SKJSONCANVAS_VERSION].asInt() == 1);
        Json::Value commands = root[SKJSONCANVAS_COMMANDS];
        for (Json::ArrayIndex i = 0; i < commands.size(); i++) {
            renderer.processCommand(commands[i], target);
        }
    }
    else {
        SkDebugf(json);
        SkFAIL("json parse failure");
    }
}

} // namespace