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 "SkJSONCanvas.h" | 8 #include "SkJSONCanvas.h" |
| 9 #include "SkColorFilter.h" |
9 #include "SkImageFilter.h" | 10 #include "SkImageFilter.h" |
10 #include "SkMaskFilter.h" | 11 #include "SkMaskFilter.h" |
11 #include "SkPaintDefaults.h" | 12 #include "SkPaintDefaults.h" |
12 #include "SkPath.h" | 13 #include "SkPath.h" |
13 #include "SkPathEffect.h" | 14 #include "SkPathEffect.h" |
14 #include "SkRRect.h" | 15 #include "SkRRect.h" |
| 16 #include "SkTextBlob.h" |
| 17 #include "SkTextBlobRunIterator.h" |
| 18 #include "SkTypeface.h" |
15 #include "SkWriteBuffer.h" | 19 #include "SkWriteBuffer.h" |
16 | 20 |
17 SkJSONCanvas::SkJSONCanvas(int width, int height, SkWStream& out, bool sendBinar
ies) | 21 SkJSONCanvas::SkJSONCanvas(int width, int height, SkWStream& out, bool sendBinar
ies) |
18 : INHERITED(width, height) | 22 : INHERITED(width, height) |
19 , fOut(out) | 23 , fOut(out) |
20 , fRoot(Json::objectValue) | 24 , fRoot(Json::objectValue) |
21 , fCommands(Json::arrayValue) | 25 , fCommands(Json::arrayValue) |
22 , fSendBinaries(sendBinaries) { | 26 , fSendBinaries(sendBinaries) { |
23 fRoot[SKJSONCANVAS_VERSION] = Json::Value(1); | 27 fRoot[SKJSONCANVAS_VERSION] = Json::Value(1); |
24 } | 28 } |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 } | 395 } |
392 case SkPaint::kRight_Align: { | 396 case SkPaint::kRight_Align: { |
393 (*target)[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN] = SKJSONCANVAS_ALIGN
_RIGHT; | 397 (*target)[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN] = SKJSONCANVAS_ALIGN
_RIGHT; |
394 break; | 398 break; |
395 } | 399 } |
396 default: SkASSERT(false); | 400 default: SkASSERT(false); |
397 } | 401 } |
398 } | 402 } |
399 } | 403 } |
400 | 404 |
| 405 static void apply_paint_typeface(const SkPaint& paint, Json::Value* target, |
| 406 bool sendBinaries) { |
| 407 SkTypeface* typeface = paint.getTypeface(); |
| 408 if (typeface != nullptr) { |
| 409 if (sendBinaries) { |
| 410 Json::Value jsonTypeface; |
| 411 SkDynamicMemoryWStream buffer; |
| 412 typeface->serialize(&buffer); |
| 413 void* data = sk_malloc_throw(buffer.bytesWritten()); |
| 414 buffer.copyTo(data); |
| 415 Json::Value bytes; |
| 416 encode_data(data, buffer.bytesWritten(), &bytes); |
| 417 jsonTypeface[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes; |
| 418 free(data); |
| 419 (*target)[SKJSONCANVAS_ATTRIBUTE_TYPEFACE] = jsonTypeface; |
| 420 } |
| 421 } |
| 422 } |
| 423 |
401 static void apply_paint_shader(const SkPaint& paint, Json::Value* target, bool s
endBinaries) { | 424 static void apply_paint_shader(const SkPaint& paint, Json::Value* target, bool s
endBinaries) { |
402 SkFlattenable* shader = paint.getShader(); | 425 SkFlattenable* shader = paint.getShader(); |
403 if (shader != nullptr) { | 426 if (shader != nullptr) { |
404 Json::Value jsonShader; | 427 Json::Value jsonShader; |
405 flatten(shader, &jsonShader, sendBinaries); | 428 flatten(shader, &jsonShader, sendBinaries); |
406 (*target)[SKJSONCANVAS_ATTRIBUTE_SHADER] = jsonShader; | 429 (*target)[SKJSONCANVAS_ATTRIBUTE_SHADER] = jsonShader; |
407 } | 430 } |
408 } | 431 } |
409 | 432 |
410 static void apply_paint_xfermode(const SkPaint& paint, Json::Value* target, bool
sendBinaries) { | 433 static void apply_paint_xfermode(const SkPaint& paint, Json::Value* target, bool
sendBinaries) { |
411 SkFlattenable* xfermode = paint.getXfermode(); | 434 SkFlattenable* xfermode = paint.getXfermode(); |
412 if (xfermode != nullptr) { | 435 if (xfermode != nullptr) { |
413 Json::Value jsonXfermode; | 436 Json::Value jsonXfermode; |
414 flatten(xfermode, &jsonXfermode, sendBinaries); | 437 flatten(xfermode, &jsonXfermode, sendBinaries); |
415 (*target)[SKJSONCANVAS_ATTRIBUTE_XFERMODE] = jsonXfermode; | 438 (*target)[SKJSONCANVAS_ATTRIBUTE_XFERMODE] = jsonXfermode; |
416 } | 439 } |
417 } | 440 } |
418 | 441 |
419 static void apply_paint_imagefilter(const SkPaint& paint, Json::Value* target, b
ool sendBinaries) { | 442 static void apply_paint_imagefilter(const SkPaint& paint, Json::Value* target, b
ool sendBinaries) { |
420 SkFlattenable* imageFilter = paint.getImageFilter(); | 443 SkFlattenable* imageFilter = paint.getImageFilter(); |
421 if (imageFilter != nullptr) { | 444 if (imageFilter != nullptr) { |
422 Json::Value jsonImageFilter; | 445 Json::Value jsonImageFilter; |
423 flatten(imageFilter, &jsonImageFilter, sendBinaries); | 446 flatten(imageFilter, &jsonImageFilter, sendBinaries); |
424 (*target)[SKJSONCANVAS_ATTRIBUTE_IMAGEFILTER] = jsonImageFilter; | 447 (*target)[SKJSONCANVAS_ATTRIBUTE_IMAGEFILTER] = jsonImageFilter; |
425 } | 448 } |
426 } | 449 } |
427 | 450 |
| 451 static void apply_paint_colorfilter(const SkPaint& paint, Json::Value* target, b
ool sendBinaries) { |
| 452 SkFlattenable* colorFilter = paint.getColorFilter(); |
| 453 if (colorFilter != nullptr) { |
| 454 Json::Value jsonColorFilter; |
| 455 flatten(colorFilter, &jsonColorFilter, sendBinaries); |
| 456 (*target)[SKJSONCANVAS_ATTRIBUTE_COLORFILTER] = jsonColorFilter; |
| 457 } |
| 458 } |
| 459 |
428 Json::Value SkJSONCanvas::makePaint(const SkPaint& paint) { | 460 Json::Value SkJSONCanvas::makePaint(const SkPaint& paint) { |
429 Json::Value result(Json::objectValue); | 461 Json::Value result(Json::objectValue); |
430 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH, paint.getStrokeWid
th(), 0.0f); | 462 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH, paint.getStrokeWid
th(), 0.0f); |
431 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEMITER, paint.getStrokeMit
er(), | 463 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_STROKEMITER, paint.getStrokeMit
er(), |
432 SkPaintDefaults_MiterLimit); | 464 SkPaintDefaults_MiterLimit); |
433 store_bool(&result, SKJSONCANVAS_ATTRIBUTE_ANTIALIAS, paint.isAntiAlias(), f
alse); | 465 store_bool(&result, SKJSONCANVAS_ATTRIBUTE_ANTIALIAS, paint.isAntiAlias(), f
alse); |
434 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSIZE, paint.getTextSize(), | 466 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSIZE, paint.getTextSize(), |
435 SkPaintDefaults_TextSize); | 467 SkPaintDefaults_TextSize); |
436 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextScaleX
(), SK_Scalar1); | 468 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextScaleX
(), SK_Scalar1); |
437 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextSkewX(
), 0.0f); | 469 store_scalar(&result, SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextSkewX(
), 0.0f); |
438 apply_paint_color(paint, &result); | 470 apply_paint_color(paint, &result); |
439 apply_paint_style(paint, &result); | 471 apply_paint_style(paint, &result); |
440 apply_paint_cap(paint, &result); | 472 apply_paint_cap(paint, &result); |
441 apply_paint_textalign(paint, &result); | 473 apply_paint_textalign(paint, &result); |
442 apply_paint_patheffect(paint, &result, fSendBinaries); | 474 apply_paint_patheffect(paint, &result, fSendBinaries); |
443 apply_paint_maskfilter(paint, &result, fSendBinaries); | 475 apply_paint_maskfilter(paint, &result, fSendBinaries); |
444 apply_paint_shader(paint, &result, fSendBinaries); | 476 apply_paint_shader(paint, &result, fSendBinaries); |
445 apply_paint_xfermode(paint, &result, fSendBinaries); | 477 apply_paint_xfermode(paint, &result, fSendBinaries); |
446 apply_paint_imagefilter(paint, &result, fSendBinaries); | 478 apply_paint_imagefilter(paint, &result, fSendBinaries); |
| 479 apply_paint_colorfilter(paint, &result, fSendBinaries); |
| 480 apply_paint_typeface(paint, &result, fSendBinaries); |
447 return result; | 481 return result; |
448 } | 482 } |
449 | 483 |
450 Json::Value SkJSONCanvas::MakeIRect(const SkIRect& rect) { | 484 Json::Value SkJSONCanvas::MakeIRect(const SkIRect& rect) { |
451 Json::Value result(Json::arrayValue); | 485 Json::Value result(Json::arrayValue); |
452 result.append(Json::Value(rect.left())); | 486 result.append(Json::Value(rect.left())); |
453 result.append(Json::Value(rect.top())); | 487 result.append(Json::Value(rect.top())); |
454 result.append(Json::Value(rect.right())); | 488 result.append(Json::Value(rect.right())); |
455 result.append(Json::Value(rect.bottom())); | 489 result.append(Json::Value(rect.bottom())); |
456 return result; | 490 return result; |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 command[SKJSONCANVAS_ATTRIBUTE_PATH] = this->makePath(path); | 783 command[SKJSONCANVAS_ATTRIBUTE_PATH] = this->makePath(path); |
750 if (matrix != nullptr) { | 784 if (matrix != nullptr) { |
751 command[SKJSONCANVAS_ATTRIBUTE_MATRIX] = this->MakeMatrix(*matrix); | 785 command[SKJSONCANVAS_ATTRIBUTE_MATRIX] = this->MakeMatrix(*matrix); |
752 } | 786 } |
753 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint); | 787 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint); |
754 fCommands.append(command); | 788 fCommands.append(command); |
755 } | 789 } |
756 | 790 |
757 void SkJSONCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y
, | 791 void SkJSONCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y
, |
758 const SkPaint& paint) { | 792 const SkPaint& paint) { |
759 SkDebugf("unsupported: drawTextBlob\n"); | |
760 Json::Value command(Json::objectValue); | 793 Json::Value command(Json::objectValue); |
761 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_TEXTBLOB); | 794 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_TEXTBLOB); |
| 795 Json::Value runs(Json::arrayValue); |
| 796 SkTextBlobRunIterator iter(blob); |
| 797 while (!iter.done()) { |
| 798 Json::Value run(Json::objectValue); |
| 799 Json::Value jsonPositions(Json::arrayValue); |
| 800 Json::Value jsonGlyphs(Json::arrayValue); |
| 801 const SkScalar* iterPositions = iter.pos(); |
| 802 const uint16_t* iterGlyphs = iter.glyphs(); |
| 803 for (uint32_t i = 0; i < iter.glyphCount(); i++) { |
| 804 switch (iter.positioning()) { |
| 805 case SkTextBlob::kFull_Positioning: |
| 806 jsonPositions.append(this->makePoint(iterPositions[i * 2], |
| 807 iterPositions[i * 2 + 1
])); |
| 808 break; |
| 809 case SkTextBlob::kHorizontal_Positioning: |
| 810 jsonPositions.append(Json::Value(iterPositions[i])); |
| 811 break; |
| 812 case SkTextBlob::kDefault_Positioning: |
| 813 break; |
| 814 } |
| 815 jsonGlyphs.append(Json::Value(iterGlyphs[i])); |
| 816 } |
| 817 if (iter.positioning() != SkTextBlob::kDefault_Positioning) { |
| 818 run[SKJSONCANVAS_ATTRIBUTE_POSITIONS] = jsonPositions; |
| 819 } |
| 820 run[SKJSONCANVAS_ATTRIBUTE_GLYPHS] = jsonGlyphs; |
| 821 SkPaint fontPaint; |
| 822 iter.applyFontToPaint(&fontPaint); |
| 823 run[SKJSONCANVAS_ATTRIBUTE_FONT] = this->makePaint(fontPaint); |
| 824 run[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makePoint(iter.offset()); |
| 825 runs.append(run); |
| 826 iter.next(); |
| 827 } |
| 828 command[SKJSONCANVAS_ATTRIBUTE_RUNS] = runs; |
| 829 command[SKJSONCANVAS_ATTRIBUTE_X] = Json::Value(x); |
| 830 command[SKJSONCANVAS_ATTRIBUTE_Y] = Json::Value(y); |
| 831 command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint); |
762 fCommands.append(command); | 832 fCommands.append(command); |
763 } | 833 } |
764 | 834 |
765 void SkJSONCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4]
, | 835 void SkJSONCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4]
, |
766 const SkPoint texCoords[4], SkXfermode* xmode, | 836 const SkPoint texCoords[4], SkXfermode* xmode, |
767 const SkPaint& paint) { | 837 const SkPaint& paint) { |
768 SkDebugf("unsupported: drawPatch\n"); | 838 SkDebugf("unsupported: drawPatch\n"); |
769 Json::Value command(Json::objectValue); | 839 Json::Value command(Json::objectValue); |
770 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_PATCH); | 840 command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_PATCH); |
771 fCommands.append(command); | 841 fCommands.append(command); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 Json::Value backdrop; | 908 Json::Value backdrop; |
839 flatten(rec.fBackdrop, &backdrop, fSendBinaries); | 909 flatten(rec.fBackdrop, &backdrop, fSendBinaries); |
840 command[SKJSONCANVAS_ATTRIBUTE_BACKDROP] = backdrop; | 910 command[SKJSONCANVAS_ATTRIBUTE_BACKDROP] = backdrop; |
841 } | 911 } |
842 if (rec.fSaveLayerFlags != 0) { | 912 if (rec.fSaveLayerFlags != 0) { |
843 SkDebugf("unsupported: saveLayer flags\n"); | 913 SkDebugf("unsupported: saveLayer flags\n"); |
844 } | 914 } |
845 fCommands.append(command); | 915 fCommands.append(command); |
846 return this->INHERITED::getSaveLayerStrategy(rec); | 916 return this->INHERITED::getSaveLayerStrategy(rec); |
847 } | 917 } |
OLD | NEW |