Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: tools/json/SkJSONRenderer.cpp

Issue 1664263002: JSON API cleanups (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: reverted unintentional revert of Josh's changes :-) Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/json/SkJSONCanvas.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkJSONRenderer.h" 8 #include "SkJSONRenderer.h"
9 9
10 #include "SkBlurMaskFilter.h" 10 #include "SkBlurMaskFilter.h"
11 #include "SkDashPathEffect.h" 11 #include "SkDashPathEffect.h"
12 #include "SkJSONCanvas.h" 12 #include "SkJSONCanvas.h"
13 #include "SkJSONCPP.h" 13 #include "SkJSONCPP.h"
14 #include "SkPath.h" 14 #include "SkPath.h"
15 #include "SkValidatingReadBuffer.h" 15 #include "SkValidatingReadBuffer.h"
16 16
17 namespace SkJSONRenderer { 17 namespace SkJSONRenderer {
18 18
19 class Renderer { 19 class Renderer {
20 public: 20 public:
21 void getPaint(Json::Value& command, SkPaint* paint); 21 void getPaint(Json::Value& paint, SkPaint* result);
22 22
23 void getRect(Json::Value& command, const char* name, SkRect* rect); 23 void getRect(Json::Value& rect, SkRect* result);
24 24
25 void getRRect(Json::Value& command, const char* name, SkRRect* rrect); 25 void getRRect(Json::Value& rrect, SkRRect* result);
26 26
27 void getPath(Json::Value& command, SkPath* path); 27 void getPath(Json::Value& path, SkPath* result);
28 28
29 SkRegion::Op getRegionOp(Json::Value& command); 29 void getMatrix(Json::Value& matrix, SkMatrix* result);
30
31 SkRegion::Op getRegionOp(Json::Value& op);
30 32
31 void processCommand(Json::Value& command, SkCanvas* target); 33 void processCommand(Json::Value& command, SkCanvas* target);
32 34
35 void processTranslate(Json::Value& command, SkCanvas* target);
36
37 void processScale(Json::Value& command, SkCanvas* target);
38
33 void processMatrix(Json::Value& command, SkCanvas* target); 39 void processMatrix(Json::Value& command, SkCanvas* target);
34 40
35 void processSave(Json::Value& command, SkCanvas* target); 41 void processSave(Json::Value& command, SkCanvas* target);
36 42
37 void processRestore(Json::Value& command, SkCanvas* target); 43 void processRestore(Json::Value& command, SkCanvas* target);
38 44
39 void processSaveLayer(Json::Value& command, SkCanvas* target); 45 void processSaveLayer(Json::Value& command, SkCanvas* target);
40 46
41 void processPaint(Json::Value& command, SkCanvas* target); 47 void processPaint(Json::Value& command, SkCanvas* target);
42 48
43 void processRect(Json::Value& command, SkCanvas* target); 49 void processRect(Json::Value& command, SkCanvas* target);
44 50
45 void processRRect(Json::Value& command, SkCanvas* target); 51 void processRRect(Json::Value& command, SkCanvas* target);
46 52
47 void processOval(Json::Value& command, SkCanvas* target); 53 void processOval(Json::Value& command, SkCanvas* target);
48 54
49 void processPath(Json::Value& command, SkCanvas* target); 55 void processPath(Json::Value& command, SkCanvas* target);
50 56
51 void processText(Json::Value& command, SkCanvas* target); 57 void processText(Json::Value& command, SkCanvas* target);
52 58
53 void processPosText(Json::Value& command, SkCanvas* target); 59 void processPosText(Json::Value& command, SkCanvas* target);
54 60
61 void processTextOnPath(Json::Value& command, SkCanvas* target);
62
55 void processPoints(Json::Value& command, SkCanvas* target); 63 void processPoints(Json::Value& command, SkCanvas* target);
56 64
57 void processImage(Json::Value& command, SkCanvas* target); 65 void processImage(Json::Value& command, SkCanvas* target);
58 66
59 void processImageRect(Json::Value& command, SkCanvas* target); 67 void processImageRect(Json::Value& command, SkCanvas* target);
60 68
61 void processBitmap(Json::Value& command, SkCanvas* target); 69 void processBitmap(Json::Value& command, SkCanvas* target);
62 70
63 void processBitmapRect(Json::Value& command, SkCanvas* target); 71 void processBitmapRect(Json::Value& command, SkCanvas* target);
64 72
65 void processClipRect(Json::Value& command, SkCanvas* target); 73 void processClipRect(Json::Value& command, SkCanvas* target);
66 74
67 void processClipRRect(Json::Value& command, SkCanvas* target); 75 void processClipRRect(Json::Value& command, SkCanvas* target);
68 76
69 void processClipPath(Json::Value& command, SkCanvas* target); 77 void processClipPath(Json::Value& command, SkCanvas* target);
70 }; 78 };
71 79
72 void Renderer::processCommand(Json::Value& command, SkCanvas* target) { 80 void Renderer::processCommand(Json::Value& command, SkCanvas* target) {
73 const char* name = command[SKJSONCANVAS_COMMAND].asCString(); 81 const char* name = command[SKJSONCANVAS_COMMAND].asCString();
74 // TODO speed this up with a hash 82 // TODO speed this up with a hash
75 if (!strcmp(name, SKJSONCANVAS_COMMAND_MATRIX)) { 83 if (!strcmp(name, SKJSONCANVAS_COMMAND_TRANSLATE)) {
84 this->processTranslate(command, target);
85 }
86 else if (!strcmp(name, SKJSONCANVAS_COMMAND_SCALE)) {
87 this->processScale(command, target);
88 }
89 else if (!strcmp(name, SKJSONCANVAS_COMMAND_MATRIX)) {
76 this->processMatrix(command, target); 90 this->processMatrix(command, target);
77 } 91 }
78 else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVE)) { 92 else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVE)) {
79 this->processSave(command, target); 93 this->processSave(command, target);
80 } 94 }
81 else if (!strcmp(name, SKJSONCANVAS_COMMAND_RESTORE)) { 95 else if (!strcmp(name, SKJSONCANVAS_COMMAND_RESTORE)) {
82 this->processRestore(command, target); 96 this->processRestore(command, target);
83 } 97 }
84 else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVELAYER)) { 98 else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVELAYER)) {
85 this->processSaveLayer(command, target); 99 this->processSaveLayer(command, target);
(...skipping 12 matching lines...) Expand all
98 } 112 }
99 else if (!strcmp(name, SKJSONCANVAS_COMMAND_PATH)) { 113 else if (!strcmp(name, SKJSONCANVAS_COMMAND_PATH)) {
100 this->processPath(command, target); 114 this->processPath(command, target);
101 } 115 }
102 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) { 116 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) {
103 this->processText(command, target); 117 this->processText(command, target);
104 } 118 }
105 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) { 119 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) {
106 this->processPosText(command, target); 120 this->processPosText(command, target);
107 } 121 }
122 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXTONPATH)) {
123 this->processTextOnPath(command, target);
124 }
108 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) { 125 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) {
109 this->processPoints(command, target); 126 this->processPoints(command, target);
110 } 127 }
111 else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGE)) { 128 else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGE)) {
112 this->processImage(command, target); 129 this->processImage(command, target);
113 } 130 }
114 else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGERECT)) { 131 else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGERECT)) {
115 this->processImageRect(command, target); 132 this->processImageRect(command, target);
116 } 133 }
117 else if (!strcmp(name, SKJSONCANVAS_COMMAND_BITMAP)) { 134 else if (!strcmp(name, SKJSONCANVAS_COMMAND_BITMAP)) {
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 } 464 }
448 } 465 }
449 466
450 static void apply_paint_textskewx(Json::Value& jsonPaint, SkPaint* target) { 467 static void apply_paint_textskewx(Json::Value& jsonPaint, SkPaint* target) {
451 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) { 468 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) {
452 float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat(); 469 float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat();
453 target->setTextSkewX(textSkewX); 470 target->setTextSkewX(textSkewX);
454 } 471 }
455 } 472 }
456 473
457 void Renderer::getPaint(Json::Value& command, SkPaint* result) { 474 void Renderer::getPaint(Json::Value& paint, SkPaint* result) {
458 Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT]; 475 apply_paint_color(paint, result);
459 apply_paint_color(jsonPaint, result); 476 apply_paint_shader(paint, result);
460 apply_paint_shader(jsonPaint, result); 477 apply_paint_patheffect(paint, result);
461 apply_paint_patheffect(jsonPaint, result); 478 apply_paint_maskfilter(paint, result);
462 apply_paint_maskfilter(jsonPaint, result); 479 apply_paint_xfermode(paint, result);
463 apply_paint_xfermode(jsonPaint, result); 480 apply_paint_imagefilter(paint, result);
464 apply_paint_imagefilter(jsonPaint, result); 481 apply_paint_style(paint, result);
465 apply_paint_style(jsonPaint, result); 482 apply_paint_strokewidth(paint, result);
466 apply_paint_strokewidth(jsonPaint, result); 483 apply_paint_strokemiter(paint, result);
467 apply_paint_strokemiter(jsonPaint, result); 484 apply_paint_cap(paint, result);
468 apply_paint_cap(jsonPaint, result); 485 apply_paint_antialias(paint, result);
469 apply_paint_antialias(jsonPaint, result); 486 apply_paint_blur(paint, result);
470 apply_paint_blur(jsonPaint, result); 487 apply_paint_dashing(paint, result);
471 apply_paint_dashing(jsonPaint, result); 488 apply_paint_textalign(paint, result);
472 apply_paint_textalign(jsonPaint, result); 489 apply_paint_textsize(paint, result);
473 apply_paint_textsize(jsonPaint, result); 490 apply_paint_textscalex(paint, result);
474 apply_paint_textscalex(jsonPaint, result); 491 apply_paint_textskewx(paint, result);
475 apply_paint_textskewx(jsonPaint, result);
476 } 492 }
477 493
478 void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) { 494 void Renderer::getRect(Json::Value& rect, SkRect* result) {
479 Json::Value rect = command[name];
480 result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3] .asFloat()); 495 result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3] .asFloat());
481 } 496 }
482 497
483 void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result) { 498 void Renderer::getRRect(Json::Value& rrect, SkRRect* result) {
484 Json::Value rrect = command[name];
485 SkVector radii[4] = { 499 SkVector radii[4] = {
486 { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, 500 { rrect[1][0].asFloat(), rrect[1][1].asFloat() },
487 { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, 501 { rrect[2][0].asFloat(), rrect[2][1].asFloat() },
488 { rrect[3][0].asFloat(), rrect[3][1].asFloat() }, 502 { rrect[3][0].asFloat(), rrect[3][1].asFloat() },
489 { rrect[4][0].asFloat(), rrect[4][1].asFloat() } 503 { rrect[4][0].asFloat(), rrect[4][1].asFloat() }
490 }; 504 };
491 result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asF loat(), 505 result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asF loat(),
492 rrect[0][2].asFloat(), rrect[0][3].asF loat()), 506 rrect[0][2].asFloat(), rrect[0][3].asF loat()),
493 radii); 507 radii);
494 } 508 }
495 509
496 void Renderer::getPath(Json::Value& command, SkPath* result) { 510 void Renderer::getMatrix(Json::Value& matrix, SkMatrix* result) {
497 Json::Value path = command[SKJSONCANVAS_ATTRIBUTE_PATH]; 511 SkScalar values[] = {
512 matrix[0][0].asFloat(), matrix[0][1].asFloat(), matrix[0][2].asFloat(),
513 matrix[1][0].asFloat(), matrix[1][1].asFloat(), matrix[1][2].asFloat(),
514 matrix[2][0].asFloat(), matrix[2][1].asFloat(), matrix[2][2].asFloat()
515 };
516 result->set9(values);
517 }
518
519 void Renderer::getPath(Json::Value& path, SkPath* result) {
498 const char* fillType = path[SKJSONCANVAS_ATTRIBUTE_FILLTYPE].asCString(); 520 const char* fillType = path[SKJSONCANVAS_ATTRIBUTE_FILLTYPE].asCString();
499 if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_WINDING)) { 521 if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_WINDING)) {
500 result->setFillType(SkPath::kWinding_FillType); 522 result->setFillType(SkPath::kWinding_FillType);
501 } 523 }
502 else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_EVENODD)) { 524 else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_EVENODD)) {
503 result->setFillType(SkPath::kEvenOdd_FillType); 525 result->setFillType(SkPath::kEvenOdd_FillType);
504 } 526 }
505 else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEWINDING)) { 527 else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEWINDING)) {
506 result->setFillType(SkPath::kInverseWinding_FillType); 528 result->setFillType(SkPath::kInverseWinding_FillType);
507 } 529 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 conic[1][0].asFloat(), conic[1][1].asFloat(), 563 conic[1][0].asFloat(), conic[1][1].asFloat(),
542 conic[2].asFloat()); 564 conic[2].asFloat());
543 } 565 }
544 else { 566 else {
545 SkASSERT(false); 567 SkASSERT(false);
546 } 568 }
547 } 569 }
548 } 570 }
549 } 571 }
550 572
551 SkRegion::Op Renderer::getRegionOp(Json::Value& command) { 573 SkRegion::Op Renderer::getRegionOp(Json::Value& jsonOp) {
552 const char* op = command[SKJSONCANVAS_ATTRIBUTE_REGIONOP].asCString(); 574 const char* op = jsonOp.asCString();
553 if (!strcmp(op, SKJSONCANVAS_REGIONOP_DIFFERENCE)) { 575 if (!strcmp(op, SKJSONCANVAS_REGIONOP_DIFFERENCE)) {
554 return SkRegion::kDifference_Op; 576 return SkRegion::kDifference_Op;
555 } 577 }
556 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_INTERSECT)) { 578 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_INTERSECT)) {
557 return SkRegion::kIntersect_Op; 579 return SkRegion::kIntersect_Op;
558 } 580 }
559 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_UNION)) { 581 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_UNION)) {
560 return SkRegion::kUnion_Op; 582 return SkRegion::kUnion_Op;
561 } 583 }
562 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_XOR)) { 584 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_XOR)) {
563 return SkRegion::kXOR_Op; 585 return SkRegion::kXOR_Op;
564 } 586 }
565 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE)) { 587 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE)) {
566 return SkRegion::kReverseDifference_Op; 588 return SkRegion::kReverseDifference_Op;
567 } 589 }
568 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REPLACE)) { 590 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REPLACE)) {
569 return SkRegion::kReplace_Op; 591 return SkRegion::kReplace_Op;
570 } 592 }
571 SkASSERT(false); 593 SkASSERT(false);
572 return SkRegion::kIntersect_Op; 594 return SkRegion::kIntersect_Op;
573 } 595 }
574 596
597 void Renderer::processTranslate(Json::Value& command, SkCanvas* target) {
598 target->translate(command[SKJSONCANVAS_ATTRIBUTE_X].asFloat(),
599 command[SKJSONCANVAS_ATTRIBUTE_Y].asFloat());
600 }
601
602 void Renderer::processScale(Json::Value& command, SkCanvas* target) {
603 target->scale(command[SKJSONCANVAS_ATTRIBUTE_X].asFloat(),
604 command[SKJSONCANVAS_ATTRIBUTE_Y].asFloat());
605 }
606
575 void Renderer::processMatrix(Json::Value& command, SkCanvas* target) { 607 void Renderer::processMatrix(Json::Value& command, SkCanvas* target) {
576 Json::Value jsonMatrix = command[SKJSONCANVAS_ATTRIBUTE_MATRIX];
577 SkMatrix matrix; 608 SkMatrix matrix;
578 SkScalar values[] = { 609 this->getMatrix(command[SKJSONCANVAS_ATTRIBUTE_MATRIX], &matrix);
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); 610 target->setMatrix(matrix);
585 } 611 }
586 612
587 void Renderer::processSave(Json::Value& command, SkCanvas* target) { 613 void Renderer::processSave(Json::Value& command, SkCanvas* target) {
588 target->save(); 614 target->save();
589 } 615 }
590 616
591 void Renderer::processRestore(Json::Value& command, SkCanvas* target) { 617 void Renderer::processRestore(Json::Value& command, SkCanvas* target) {
592 target->restore(); 618 target->restore();
593 } 619 }
594 620
595 void Renderer::processSaveLayer(Json::Value& command, SkCanvas* target) { 621 void Renderer::processSaveLayer(Json::Value& command, SkCanvas* target) {
596 SkCanvas::SaveLayerRec rec; 622 SkCanvas::SaveLayerRec rec;
597 SkRect bounds; 623 SkRect bounds;
598 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_BOUNDS)) { 624 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_BOUNDS)) {
599 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_BOUNDS, &bounds); 625 this->getRect(command[SKJSONCANVAS_ATTRIBUTE_BOUNDS], &bounds);
600 rec.fBounds = &bounds; 626 rec.fBounds = &bounds;
601 } 627 }
602 SkPaint paint; 628 SkPaint paint;
603 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { 629 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) {
604 this->getPaint(command, &paint); 630 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
605 rec.fPaint = &paint; 631 rec.fPaint = &paint;
606 } 632 }
607 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_BACKDROP)) { 633 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_BACKDROP)) {
608 rec.fBackdrop = (SkImageFilter*) load_flattenable(command[SKJSONCANVAS_A TTRIBUTE_BACKDROP]); 634 rec.fBackdrop = (SkImageFilter*) load_flattenable(command[SKJSONCANVAS_A TTRIBUTE_BACKDROP]);
609 } 635 }
610 target->saveLayer(rec); 636 target->saveLayer(rec);
611 if (rec.fBackdrop != nullptr) { 637 if (rec.fBackdrop != nullptr) {
612 rec.fBackdrop->unref(); 638 rec.fBackdrop->unref();
613 } 639 }
614 } 640 }
615 641
616 void Renderer::processPaint(Json::Value& command, SkCanvas* target) { 642 void Renderer::processPaint(Json::Value& command, SkCanvas* target) {
617 SkPaint paint; 643 SkPaint paint;
618 this->getPaint(command, &paint); 644 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
619 target->drawPaint(paint); 645 target->drawPaint(paint);
620 } 646 }
621 647
622 void Renderer::processRect(Json::Value& command, SkCanvas* target) { 648 void Renderer::processRect(Json::Value& command, SkCanvas* target) {
623 SkRect rect; 649 SkRect rect;
624 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); 650 this->getRect(command[SKJSONCANVAS_ATTRIBUTE_COORDS], &rect);
625 SkPaint paint; 651 SkPaint paint;
626 this->getPaint(command, &paint); 652 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
627 target->drawRect(rect, paint); 653 target->drawRect(rect, paint);
628 } 654 }
629 655
630 void Renderer::processRRect(Json::Value& command, SkCanvas* target) { 656 void Renderer::processRRect(Json::Value& command, SkCanvas* target) {
631 SkRRect rrect; 657 SkRRect rrect;
632 this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect); 658 this->getRRect(command[SKJSONCANVAS_ATTRIBUTE_COORDS], &rrect);
633 SkPaint paint; 659 SkPaint paint;
634 this->getPaint(command, &paint); 660 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
635 target->drawRRect(rrect, paint); 661 target->drawRRect(rrect, paint);
636 } 662 }
637 663
638 void Renderer::processOval(Json::Value& command, SkCanvas* target) { 664 void Renderer::processOval(Json::Value& command, SkCanvas* target) {
639 SkRect rect; 665 SkRect rect;
640 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); 666 this->getRect(command[SKJSONCANVAS_ATTRIBUTE_COORDS], &rect);
641 SkPaint paint; 667 SkPaint paint;
642 this->getPaint(command, &paint); 668 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
643 target->drawOval(rect, paint); 669 target->drawOval(rect, paint);
644 } 670 }
645 671
646 void Renderer::processPath(Json::Value& command, SkCanvas* target) { 672 void Renderer::processPath(Json::Value& command, SkCanvas* target) {
647 Json::Value jsonPath = command[SKJSONCANVAS_ATTRIBUTE_PATH];
648 SkPath path; 673 SkPath path;
649 this->getPath(command, &path); 674 this->getPath(command[SKJSONCANVAS_ATTRIBUTE_PATH], &path);
650 SkPaint paint; 675 SkPaint paint;
651 this->getPaint(command, &paint); 676 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
652 target->drawPath(path, paint); 677 target->drawPath(path, paint);
653 } 678 }
654 679
655 void Renderer::processText(Json::Value& command, SkCanvas* target) { 680 void Renderer::processText(Json::Value& command, SkCanvas* target) {
656 const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString(); 681 const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString();
657 SkPaint paint; 682 SkPaint paint;
658 this->getPaint(command, &paint); 683 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
659 Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; 684 Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS];
660 target->drawText(text, strlen(text), coords[0].asFloat(), coords[1].asFloat( ), paint); 685 target->drawText(text, strlen(text), coords[0].asFloat(), coords[1].asFloat( ), paint);
661 } 686 }
662 687
663 void Renderer::processPosText(Json::Value& command, SkCanvas* target) { 688 void Renderer::processPosText(Json::Value& command, SkCanvas* target) {
664 const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString(); 689 const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString();
665 SkPaint paint; 690 SkPaint paint;
666 this->getPaint(command, &paint); 691 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
667 Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; 692 Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS];
668 int count = (int) coords.size(); 693 int count = (int) coords.size();
669 SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint)); 694 SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
670 for (int i = 0; i < count; i++) { 695 for (int i = 0; i < count; i++) {
671 points[i] = SkPoint::Make(coords[i][0].asFloat(), coords[i][1].asFloat() ); 696 points[i] = SkPoint::Make(coords[i][0].asFloat(), coords[i][1].asFloat() );
672 } 697 }
673 target->drawPosText(text, strlen(text), points, paint); 698 target->drawPosText(text, strlen(text), points, paint);
674 free(points); 699 free(points);
675 } 700 }
676 701
702 void Renderer::processTextOnPath(Json::Value& command, SkCanvas* target) {
703 const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString();
704 SkPath path;
705 this->getPath(command[SKJSONCANVAS_ATTRIBUTE_PATH], &path);
706 SkMatrix* matrixPtr;
707 SkMatrix matrix;
708 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_MATRIX)) {
709 this->getMatrix(command[SKJSONCANVAS_ATTRIBUTE_MATRIX], &matrix);
710 matrixPtr = &matrix;
711 }
712 else {
713 matrixPtr = nullptr;
714 }
715 SkPaint paint;
716 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
717 target->drawTextOnPath(text, strlen(text), path, matrixPtr, paint);
718 }
719
677 void Renderer::processPoints(Json::Value& command, SkCanvas* target) { 720 void Renderer::processPoints(Json::Value& command, SkCanvas* target) {
678 SkCanvas::PointMode mode; 721 SkCanvas::PointMode mode;
679 const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString(); 722 const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString();
680 if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POINTS)) { 723 if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POINTS)) {
681 mode = SkCanvas::kPoints_PointMode; 724 mode = SkCanvas::kPoints_PointMode;
682 } 725 }
683 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_LINES)) { 726 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_LINES)) {
684 mode = SkCanvas::kLines_PointMode; 727 mode = SkCanvas::kLines_PointMode;
685 } 728 }
686 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POLYGON)) { 729 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POLYGON)) {
687 mode = SkCanvas::kPolygon_PointMode; 730 mode = SkCanvas::kPolygon_PointMode;
688 } 731 }
689 else { 732 else {
690 SkASSERT(false); 733 SkASSERT(false);
691 return; 734 return;
692 } 735 }
693 Json::Value jsonPoints = command[SKJSONCANVAS_ATTRIBUTE_POINTS]; 736 Json::Value jsonPoints = command[SKJSONCANVAS_ATTRIBUTE_POINTS];
694 int count = (int) jsonPoints.size(); 737 int count = (int) jsonPoints.size();
695 SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint)); 738 SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
696 for (int i = 0; i < count; i++) { 739 for (int i = 0; i < count; i++) {
697 points[i] = SkPoint::Make(jsonPoints[i][0].asFloat(), jsonPoints[i][1].a sFloat()); 740 points[i] = SkPoint::Make(jsonPoints[i][0].asFloat(), jsonPoints[i][1].a sFloat());
698 } 741 }
699 SkPaint paint; 742 SkPaint paint;
700 this->getPaint(command, &paint); 743 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
701 target->drawPoints(mode, count, points, paint); 744 target->drawPoints(mode, count, points, paint);
702 free(points); 745 free(points);
703 } 746 }
704 747
705 void Renderer::processClipRect(Json::Value& command, SkCanvas* target) { 748 void Renderer::processClipRect(Json::Value& command, SkCanvas* target) {
706 SkRect rect; 749 SkRect rect;
707 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); 750 this->getRect(command[SKJSONCANVAS_ATTRIBUTE_COORDS], &rect);
708 target->clipRect(rect, this->getRegionOp(command), 751 target->clipRect(rect, this->getRegionOp(command[SKJSONCANVAS_ATTRIBUTE_REGI ONOP]),
709 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); 752 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
710 } 753 }
711 754
712 void Renderer::processClipRRect(Json::Value& command, SkCanvas* target) { 755 void Renderer::processClipRRect(Json::Value& command, SkCanvas* target) {
713 SkRRect rrect; 756 SkRRect rrect;
714 this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect); 757 this->getRRect(command[SKJSONCANVAS_ATTRIBUTE_COORDS], &rrect);
715 target->clipRRect(rrect, this->getRegionOp(command), 758 target->clipRRect(rrect, this->getRegionOp(command[SKJSONCANVAS_ATTRIBUTE_RE GIONOP]),
716 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); 759 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
717 } 760 }
718 761
719 void Renderer::processClipPath(Json::Value& command, SkCanvas* target) { 762 void Renderer::processClipPath(Json::Value& command, SkCanvas* target) {
720 SkPath path; 763 SkPath path;
721 this->getPath(command, &path); 764 this->getPath(command[SKJSONCANVAS_ATTRIBUTE_PATH], &path);
722 target->clipPath(path, this->getRegionOp(command), 765 target->clipPath(path, this->getRegionOp(command[SKJSONCANVAS_ATTRIBUTE_REGI ONOP]),
723 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); 766 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
724 } 767 }
725 768
726 void Renderer::processImage(Json::Value& command, SkCanvas* target) { 769 void Renderer::processImage(Json::Value& command, SkCanvas* target) {
727 SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_IMAGE]); 770 SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_IMAGE]);
728 if (image == nullptr) { 771 if (image == nullptr) {
729 return; 772 return;
730 } 773 }
731 Json::Value point = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; 774 Json::Value point = command[SKJSONCANVAS_ATTRIBUTE_COORDS];
732 SkPaint* paintPtr; 775 SkPaint* paintPtr;
733 SkPaint paint; 776 SkPaint paint;
734 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { 777 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) {
735 this->getPaint(command, &paint); 778 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
736 paintPtr = &paint; 779 paintPtr = &paint;
737 } 780 }
738 else { 781 else {
739 paintPtr = nullptr; 782 paintPtr = nullptr;
740 } 783 }
741 target->drawImage(image, point[0].asFloat(), point[1].asFloat(), paintPtr); 784 target->drawImage(image, point[0].asFloat(), point[1].asFloat(), paintPtr);
742 image->unref(); 785 image->unref();
743 } 786 }
744 787
745 void Renderer::processImageRect(Json::Value& command, SkCanvas* target) { 788 void Renderer::processImageRect(Json::Value& command, SkCanvas* target) {
746 SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_IMAGE]); 789 SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_IMAGE]);
747 if (image == nullptr) { 790 if (image == nullptr) {
748 return; 791 return;
749 } 792 }
750 SkRect dst; 793 SkRect dst;
751 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_DST, &dst); 794 this->getRect(command[SKJSONCANVAS_ATTRIBUTE_DST], &dst);
752 SkPaint* paintPtr; 795 SkPaint* paintPtr;
753 SkPaint paint; 796 SkPaint paint;
754 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { 797 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) {
755 this->getPaint(command, &paint); 798 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
756 paintPtr = &paint; 799 paintPtr = &paint;
757 } 800 }
758 else { 801 else {
759 paintPtr = nullptr; 802 paintPtr = nullptr;
760 } 803 }
761 SkCanvas::SrcRectConstraint constraint; 804 SkCanvas::SrcRectConstraint constraint;
762 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_STRICT) && 805 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_STRICT) &&
763 command[SKJSONCANVAS_ATTRIBUTE_STRICT].asBool()) { 806 command[SKJSONCANVAS_ATTRIBUTE_STRICT].asBool()) {
764 constraint = SkCanvas::kStrict_SrcRectConstraint; 807 constraint = SkCanvas::kStrict_SrcRectConstraint;
765 } 808 }
766 else { 809 else {
767 constraint = SkCanvas::kFast_SrcRectConstraint; 810 constraint = SkCanvas::kFast_SrcRectConstraint;
768 } 811 }
769 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_SRC)) { 812 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_SRC)) {
770 SkRect src; 813 SkRect src;
771 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_SRC, &src); 814 this->getRect(command[SKJSONCANVAS_ATTRIBUTE_SRC], &src);
772 target->drawImageRect(image, src, dst, paintPtr, constraint); 815 target->drawImageRect(image, src, dst, paintPtr, constraint);
773 } 816 }
774 else { 817 else {
775 target->drawImageRect(image, dst, paintPtr, constraint); 818 target->drawImageRect(image, dst, paintPtr, constraint);
776 } 819 }
777 image->unref(); 820 image->unref();
778 } 821 }
779 822
780 void Renderer::processBitmap(Json::Value& command, SkCanvas* target) { 823 void Renderer::processBitmap(Json::Value& command, SkCanvas* target) {
781 SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_BITMAP]); 824 SkImage* image = load_image(command[SKJSONCANVAS_ATTRIBUTE_BITMAP]);
782 if (image == nullptr) { 825 if (image == nullptr) {
783 return; 826 return;
784 } 827 }
785 Json::Value point = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; 828 Json::Value point = command[SKJSONCANVAS_ATTRIBUTE_COORDS];
786 SkPaint* paintPtr; 829 SkPaint* paintPtr;
787 SkPaint paint; 830 SkPaint paint;
788 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { 831 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) {
789 this->getPaint(command, &paint); 832 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
790 paintPtr = &paint; 833 paintPtr = &paint;
791 } 834 }
792 else { 835 else {
793 paintPtr = nullptr; 836 paintPtr = nullptr;
794 } 837 }
795 target->drawImage(image, point[0].asFloat(), point[1].asFloat(), paintPtr); 838 target->drawImage(image, point[0].asFloat(), point[1].asFloat(), paintPtr);
796 image->unref(); 839 image->unref();
797 } 840 }
798 841
799 void Renderer::processBitmapRect(Json::Value& command, SkCanvas* target) { 842 void Renderer::processBitmapRect(Json::Value& command, SkCanvas* target) {
800 SkBitmap* bitmap = load_bitmap(command[SKJSONCANVAS_ATTRIBUTE_BITMAP]); 843 SkBitmap* bitmap = load_bitmap(command[SKJSONCANVAS_ATTRIBUTE_BITMAP]);
801 if (bitmap == nullptr) { 844 if (bitmap == nullptr) {
802 return; 845 return;
803 } 846 }
804 SkRect dst; 847 SkRect dst;
805 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_DST, &dst); 848 this->getRect(command[SKJSONCANVAS_ATTRIBUTE_DST], &dst);
806 SkPaint* paintPtr; 849 SkPaint* paintPtr;
807 SkPaint paint; 850 SkPaint paint;
808 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { 851 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) {
809 this->getPaint(command, &paint); 852 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint);
810 paintPtr = &paint; 853 paintPtr = &paint;
811 } 854 }
812 else { 855 else {
813 paintPtr = nullptr; 856 paintPtr = nullptr;
814 } 857 }
815 SkCanvas::SrcRectConstraint constraint; 858 SkCanvas::SrcRectConstraint constraint;
816 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_STRICT) && 859 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_STRICT) &&
817 command[SKJSONCANVAS_ATTRIBUTE_STRICT].asBool()) { 860 command[SKJSONCANVAS_ATTRIBUTE_STRICT].asBool()) {
818 constraint = SkCanvas::kStrict_SrcRectConstraint; 861 constraint = SkCanvas::kStrict_SrcRectConstraint;
819 } 862 }
820 else { 863 else {
821 constraint = SkCanvas::kFast_SrcRectConstraint; 864 constraint = SkCanvas::kFast_SrcRectConstraint;
822 } 865 }
823 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_SRC)) { 866 if (command.isMember(SKJSONCANVAS_ATTRIBUTE_SRC)) {
824 SkRect src; 867 SkRect src;
825 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_SRC, &src); 868 this->getRect(command[SKJSONCANVAS_ATTRIBUTE_SRC], &src);
826 target->drawBitmapRect(*bitmap, src, dst, paintPtr, constraint); 869 target->drawBitmapRect(*bitmap, src, dst, paintPtr, constraint);
827 } 870 }
828 else { 871 else {
829 target->drawBitmapRect(*bitmap, dst, paintPtr, constraint); 872 target->drawBitmapRect(*bitmap, dst, paintPtr, constraint);
830 } 873 }
831 free(bitmap); 874 free(bitmap);
832 } 875 }
833 876
834 void render(const char* json, SkCanvas* target) { 877 void render(const char* json, SkCanvas* target) {
835 Renderer renderer; 878 Renderer renderer;
836 Json::Reader reader; 879 Json::Reader reader;
837 Json::Value root; 880 Json::Value root;
838 if (reader.parse(std::string(json), root)) { 881 if (reader.parse(std::string(json), root)) {
839 SkASSERT(root[SKJSONCANVAS_VERSION].asInt() == 1); 882 SkASSERT(root[SKJSONCANVAS_VERSION].asInt() == 1);
840 Json::Value commands = root[SKJSONCANVAS_COMMANDS]; 883 Json::Value commands = root[SKJSONCANVAS_COMMANDS];
841 for (Json::ArrayIndex i = 0; i < commands.size(); i++) { 884 for (Json::ArrayIndex i = 0; i < commands.size(); i++) {
842 renderer.processCommand(commands[i], target); 885 renderer.processCommand(commands[i], target);
843 } 886 }
844 } 887 }
845 else { 888 else {
846 SkDebugf(json); 889 SkDebugf(json);
847 SkFAIL("json parse failure"); 890 SkFAIL("json parse failure");
848 } 891 }
849 } 892 }
850 893
851 } // namespace 894 } // namespace
OLDNEW
« no previous file with comments | « tools/json/SkJSONCanvas.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698