OLD | NEW |
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 "SkTextBlob.h" |
| 16 #include "SkTypeface.h" |
15 #include "SkValidatingReadBuffer.h" | 17 #include "SkValidatingReadBuffer.h" |
16 | 18 |
17 namespace SkJSONRenderer { | 19 namespace SkJSONRenderer { |
18 | 20 |
19 class Renderer { | 21 class Renderer { |
20 public: | 22 public: |
21 void getPaint(Json::Value& paint, SkPaint* result); | 23 void getPaint(Json::Value& paint, SkPaint* result); |
22 | 24 |
23 void getRect(Json::Value& rect, SkRect* result); | 25 void getRect(Json::Value& rect, SkRect* result); |
24 | 26 |
(...skipping 28 matching lines...) Expand all Loading... |
53 void processOval(Json::Value& command, SkCanvas* target); | 55 void processOval(Json::Value& command, SkCanvas* target); |
54 | 56 |
55 void processPath(Json::Value& command, SkCanvas* target); | 57 void processPath(Json::Value& command, SkCanvas* target); |
56 | 58 |
57 void processText(Json::Value& command, SkCanvas* target); | 59 void processText(Json::Value& command, SkCanvas* target); |
58 | 60 |
59 void processPosText(Json::Value& command, SkCanvas* target); | 61 void processPosText(Json::Value& command, SkCanvas* target); |
60 | 62 |
61 void processTextOnPath(Json::Value& command, SkCanvas* target); | 63 void processTextOnPath(Json::Value& command, SkCanvas* target); |
62 | 64 |
| 65 void processTextBlob(Json::Value& command, SkCanvas* target); |
| 66 |
63 void processPoints(Json::Value& command, SkCanvas* target); | 67 void processPoints(Json::Value& command, SkCanvas* target); |
64 | 68 |
65 void processImage(Json::Value& command, SkCanvas* target); | 69 void processImage(Json::Value& command, SkCanvas* target); |
66 | 70 |
67 void processImageRect(Json::Value& command, SkCanvas* target); | 71 void processImageRect(Json::Value& command, SkCanvas* target); |
68 | 72 |
69 void processBitmap(Json::Value& command, SkCanvas* target); | 73 void processBitmap(Json::Value& command, SkCanvas* target); |
70 | 74 |
71 void processBitmapRect(Json::Value& command, SkCanvas* target); | 75 void processBitmapRect(Json::Value& command, SkCanvas* target); |
72 | 76 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 } | 119 } |
116 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) { | 120 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) { |
117 this->processText(command, target); | 121 this->processText(command, target); |
118 } | 122 } |
119 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) { | 123 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) { |
120 this->processPosText(command, target); | 124 this->processPosText(command, target); |
121 } | 125 } |
122 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXTONPATH)) { | 126 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXTONPATH)) { |
123 this->processTextOnPath(command, target); | 127 this->processTextOnPath(command, target); |
124 } | 128 } |
| 129 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXTBLOB)) { |
| 130 this->processTextBlob(command, target); |
| 131 } |
125 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) { | 132 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) { |
126 this->processPoints(command, target); | 133 this->processPoints(command, target); |
127 } | 134 } |
128 else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGE)) { | 135 else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGE)) { |
129 this->processImage(command, target); | 136 this->processImage(command, target); |
130 } | 137 } |
131 else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGERECT)) { | 138 else if (!strcmp(name, SKJSONCANVAS_COMMAND_IMAGERECT)) { |
132 this->processImageRect(command, target); | 139 this->processImageRect(command, target); |
133 } | 140 } |
134 else if (!strcmp(name, SKJSONCANVAS_COMMAND_BITMAP)) { | 141 else if (!strcmp(name, SKJSONCANVAS_COMMAND_BITMAP)) { |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_MASKFILTER)) { | 304 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_MASKFILTER)) { |
298 Json::Value jsonMaskFilter = jsonPaint[SKJSONCANVAS_ATTRIBUTE_MASKFILTER
]; | 305 Json::Value jsonMaskFilter = jsonPaint[SKJSONCANVAS_ATTRIBUTE_MASKFILTER
]; |
299 SkMaskFilter* maskFilter = (SkMaskFilter*) load_flattenable(jsonMaskFilt
er); | 306 SkMaskFilter* maskFilter = (SkMaskFilter*) load_flattenable(jsonMaskFilt
er); |
300 if (maskFilter != nullptr) { | 307 if (maskFilter != nullptr) { |
301 target->setMaskFilter(maskFilter); | 308 target->setMaskFilter(maskFilter); |
302 maskFilter->unref(); | 309 maskFilter->unref(); |
303 } | 310 } |
304 } | 311 } |
305 } | 312 } |
306 | 313 |
| 314 static void apply_paint_colorfilter(Json::Value& jsonPaint, SkPaint* target) { |
| 315 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_COLORFILTER)) { |
| 316 Json::Value jsonColorFilter = jsonPaint[SKJSONCANVAS_ATTRIBUTE_COLORFILT
ER]; |
| 317 SkColorFilter* colorFilter = (SkColorFilter*) load_flattenable(jsonColor
Filter); |
| 318 if (colorFilter != nullptr) { |
| 319 target->setColorFilter(colorFilter); |
| 320 colorFilter->unref(); |
| 321 } |
| 322 } |
| 323 } |
| 324 |
307 static void apply_paint_xfermode(Json::Value& jsonPaint, SkPaint* target) { | 325 static void apply_paint_xfermode(Json::Value& jsonPaint, SkPaint* target) { |
308 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_XFERMODE)) { | 326 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_XFERMODE)) { |
309 Json::Value jsonXfermode = jsonPaint[SKJSONCANVAS_ATTRIBUTE_XFERMODE]; | 327 Json::Value jsonXfermode = jsonPaint[SKJSONCANVAS_ATTRIBUTE_XFERMODE]; |
310 SkXfermode* xfermode = (SkXfermode*) load_flattenable(jsonXfermode); | 328 SkXfermode* xfermode = (SkXfermode*) load_flattenable(jsonXfermode); |
311 if (xfermode != nullptr) { | 329 if (xfermode != nullptr) { |
312 target->setXfermode(xfermode); | 330 target->setXfermode(xfermode); |
313 xfermode->unref(); | 331 xfermode->unref(); |
314 } | 332 } |
315 } | 333 } |
316 } | 334 } |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 } | 482 } |
465 } | 483 } |
466 | 484 |
467 static void apply_paint_textskewx(Json::Value& jsonPaint, SkPaint* target) { | 485 static void apply_paint_textskewx(Json::Value& jsonPaint, SkPaint* target) { |
468 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) { | 486 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) { |
469 float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat(); | 487 float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat(); |
470 target->setTextSkewX(textSkewX); | 488 target->setTextSkewX(textSkewX); |
471 } | 489 } |
472 } | 490 } |
473 | 491 |
| 492 static void apply_paint_typeface(Json::Value& jsonPaint, SkPaint* target) { |
| 493 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TYPEFACE)) { |
| 494 Json::Value jsonTypeface = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TYPEFACE]; |
| 495 Json::Value bytes = jsonTypeface[SKJSONCANVAS_ATTRIBUTE_BYTES]; |
| 496 void* data; |
| 497 Json::ArrayIndex length = decode_data(bytes, &data); |
| 498 SkMemoryStream buffer(data, length); |
| 499 SkTypeface* typeface = SkTypeface::Deserialize(&buffer); |
| 500 free(data); |
| 501 target->setTypeface(typeface); |
| 502 } |
| 503 } |
| 504 |
474 void Renderer::getPaint(Json::Value& paint, SkPaint* result) { | 505 void Renderer::getPaint(Json::Value& paint, SkPaint* result) { |
475 apply_paint_color(paint, result); | 506 apply_paint_color(paint, result); |
476 apply_paint_shader(paint, result); | 507 apply_paint_shader(paint, result); |
477 apply_paint_patheffect(paint, result); | 508 apply_paint_patheffect(paint, result); |
478 apply_paint_maskfilter(paint, result); | 509 apply_paint_maskfilter(paint, result); |
| 510 apply_paint_colorfilter(paint, result); |
479 apply_paint_xfermode(paint, result); | 511 apply_paint_xfermode(paint, result); |
480 apply_paint_imagefilter(paint, result); | 512 apply_paint_imagefilter(paint, result); |
481 apply_paint_style(paint, result); | 513 apply_paint_style(paint, result); |
482 apply_paint_strokewidth(paint, result); | 514 apply_paint_strokewidth(paint, result); |
483 apply_paint_strokemiter(paint, result); | 515 apply_paint_strokemiter(paint, result); |
484 apply_paint_cap(paint, result); | 516 apply_paint_cap(paint, result); |
485 apply_paint_antialias(paint, result); | 517 apply_paint_antialias(paint, result); |
486 apply_paint_blur(paint, result); | 518 apply_paint_blur(paint, result); |
487 apply_paint_dashing(paint, result); | 519 apply_paint_dashing(paint, result); |
488 apply_paint_textalign(paint, result); | 520 apply_paint_textalign(paint, result); |
489 apply_paint_textsize(paint, result); | 521 apply_paint_textsize(paint, result); |
490 apply_paint_textscalex(paint, result); | 522 apply_paint_textscalex(paint, result); |
491 apply_paint_textskewx(paint, result); | 523 apply_paint_textskewx(paint, result); |
| 524 apply_paint_typeface(paint, result); |
492 } | 525 } |
493 | 526 |
494 void Renderer::getRect(Json::Value& rect, SkRect* result) { | 527 void Renderer::getRect(Json::Value& rect, SkRect* result) { |
495 result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3]
.asFloat()); | 528 result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3]
.asFloat()); |
496 } | 529 } |
497 | 530 |
498 void Renderer::getRRect(Json::Value& rrect, SkRRect* result) { | 531 void Renderer::getRRect(Json::Value& rrect, SkRRect* result) { |
499 SkVector radii[4] = { | 532 SkVector radii[4] = { |
500 { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, | 533 { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, |
501 { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, | 534 { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 matrixPtr = &matrix; | 743 matrixPtr = &matrix; |
711 } | 744 } |
712 else { | 745 else { |
713 matrixPtr = nullptr; | 746 matrixPtr = nullptr; |
714 } | 747 } |
715 SkPaint paint; | 748 SkPaint paint; |
716 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); | 749 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); |
717 target->drawTextOnPath(text, strlen(text), path, matrixPtr, paint); | 750 target->drawTextOnPath(text, strlen(text), path, matrixPtr, paint); |
718 } | 751 } |
719 | 752 |
| 753 void Renderer::processTextBlob(Json::Value& command, SkCanvas* target) { |
| 754 SkTextBlobBuilder builder; |
| 755 Json::Value runs = command[SKJSONCANVAS_ATTRIBUTE_RUNS]; |
| 756 for (Json::ArrayIndex i = 0 ; i < runs.size(); i++) { |
| 757 Json::Value run = runs[i]; |
| 758 SkPaint font; |
| 759 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 760 this->getPaint(run[SKJSONCANVAS_ATTRIBUTE_FONT], &font); |
| 761 Json::Value glyphs = run[SKJSONCANVAS_ATTRIBUTE_GLYPHS]; |
| 762 int count = glyphs.size(); |
| 763 Json::Value coords = run[SKJSONCANVAS_ATTRIBUTE_COORDS]; |
| 764 SkScalar x = coords[0].asFloat(); |
| 765 SkScalar y = coords[1].asFloat(); |
| 766 if (run.isMember(SKJSONCANVAS_ATTRIBUTE_POSITIONS)) { |
| 767 Json::Value positions = run[SKJSONCANVAS_ATTRIBUTE_POSITIONS]; |
| 768 if (positions.size() > 0 && positions[0].isNumeric()) { |
| 769 SkTextBlobBuilder::RunBuffer buffer = builder.allocRunPosH(font,
count, y); |
| 770 for (int j = 0; j < count; j++) { |
| 771 buffer.glyphs[j] = glyphs[j].asUInt(); |
| 772 buffer.pos[j] = positions[j].asFloat(); |
| 773 } |
| 774 } |
| 775 else { |
| 776 SkTextBlobBuilder::RunBuffer buffer = builder.allocRunPos(font,
count); |
| 777 for (int j = 0; j < count; j++) { |
| 778 buffer.glyphs[j] = glyphs[j].asUInt(); |
| 779 buffer.pos[j * 2] = positions[j][0].asFloat(); |
| 780 buffer.pos[j * 2 + 1] = positions[j][1].asFloat(); |
| 781 } |
| 782 } |
| 783 } |
| 784 else { |
| 785 SkTextBlobBuilder::RunBuffer buffer = builder.allocRun(font, count,
x, y); |
| 786 for (int j = 0; j < count; j++) { |
| 787 buffer.glyphs[j] = glyphs[j].asUInt(); |
| 788 } |
| 789 } |
| 790 } |
| 791 SkScalar x = command[SKJSONCANVAS_ATTRIBUTE_X].asFloat(); |
| 792 SkScalar y = command[SKJSONCANVAS_ATTRIBUTE_Y].asFloat(); |
| 793 SkPaint paint; |
| 794 this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); |
| 795 target->drawTextBlob(builder.build(), x, y, paint); |
| 796 } |
| 797 |
720 void Renderer::processPoints(Json::Value& command, SkCanvas* target) { | 798 void Renderer::processPoints(Json::Value& command, SkCanvas* target) { |
721 SkCanvas::PointMode mode; | 799 SkCanvas::PointMode mode; |
722 const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString(); | 800 const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString(); |
723 if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POINTS)) { | 801 if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POINTS)) { |
724 mode = SkCanvas::kPoints_PointMode; | 802 mode = SkCanvas::kPoints_PointMode; |
725 } | 803 } |
726 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_LINES)) { | 804 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_LINES)) { |
727 mode = SkCanvas::kLines_PointMode; | 805 mode = SkCanvas::kLines_PointMode; |
728 } | 806 } |
729 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POLYGON)) { | 807 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POLYGON)) { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
885 renderer.processCommand(commands[i], target); | 963 renderer.processCommand(commands[i], target); |
886 } | 964 } |
887 } | 965 } |
888 else { | 966 else { |
889 SkDebugf(json); | 967 SkDebugf(json); |
890 SkFAIL("json parse failure"); | 968 SkFAIL("json parse failure"); |
891 } | 969 } |
892 } | 970 } |
893 | 971 |
894 } // namespace | 972 } // namespace |
OLD | NEW |