diff options
author | ethannicholas <ethannicholas@google.com> | 2016-02-04 10:37:50 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-04 10:37:50 -0800 |
commit | 7471c780d48afd4dc02ed45c60a2fd2efa9e5a84 (patch) | |
tree | 350243a8d390608be0758606776ca05d4b42b0cf | |
parent | 6b48984333029f2ad94a06339b2329bda8ee09ba (diff) |
JSON API cleanups
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1664263002
Review URL: https://codereview.chromium.org/1664263002
-rw-r--r-- | tools/json/SkJSONCanvas.cpp | 85 | ||||
-rw-r--r-- | tools/json/SkJSONCanvas.h | 9 | ||||
-rw-r--r-- | tools/json/SkJSONRenderer.cpp | 187 |
3 files changed, 181 insertions, 100 deletions
diff --git a/tools/json/SkJSONCanvas.cpp b/tools/json/SkJSONCanvas.cpp index 9361191c0a..3c6fa05bb6 100644 --- a/tools/json/SkJSONCanvas.cpp +++ b/tools/json/SkJSONCanvas.cpp @@ -510,15 +510,31 @@ Json::Value SkJSONCanvas::makePointMode(SkCanvas::PointMode mode) { }; } -void SkJSONCanvas::updateMatrix() { - const SkMatrix& matrix = this->getTotalMatrix(); - if (matrix != fLastMatrix) { - Json::Value command(Json::objectValue); - command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_MATRIX); - command[SKJSONCANVAS_ATTRIBUTE_MATRIX] = MakeMatrix(matrix); - fCommands.append(command); - fLastMatrix = matrix; +void SkJSONCanvas::didConcat(const SkMatrix& matrix) { + Json::Value command(Json::objectValue); + switch (matrix.getType()) { + case SkMatrix::kTranslate_Mask: + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_TRANSLATE); + command[SKJSONCANVAS_ATTRIBUTE_X] = Json::Value(matrix.get(SkMatrix::kMTransX)); + command[SKJSONCANVAS_ATTRIBUTE_Y] = Json::Value(matrix.get(SkMatrix::kMTransY)); + break; + case SkMatrix::kScale_Mask: + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_SCALE); + command[SKJSONCANVAS_ATTRIBUTE_X] = Json::Value(matrix.get(SkMatrix::kMScaleX)); + command[SKJSONCANVAS_ATTRIBUTE_Y] = Json::Value(matrix.get(SkMatrix::kMScaleY)); + break; + default: + this->didSetMatrix(this->getTotalMatrix()); + return; } + fCommands.append(command); +} + +void SkJSONCanvas::didSetMatrix(const SkMatrix& matrix) { + Json::Value command(Json::objectValue); + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_MATRIX); + command[SKJSONCANVAS_ATTRIBUTE_MATRIX] = this->makeMatrix(matrix); + fCommands.append(command); } void SkJSONCanvas::onDrawPaint(const SkPaint& paint) { @@ -529,7 +545,6 @@ void SkJSONCanvas::onDrawPaint(const SkPaint& paint) { } void SkJSONCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RECT); command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makeRect(rect); @@ -538,7 +553,6 @@ void SkJSONCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) { } void SkJSONCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_OVAL); command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makeRect(rect); @@ -547,7 +561,6 @@ void SkJSONCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) { } void SkJSONCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RRECT); command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makeRRect(rrect); @@ -556,7 +569,6 @@ void SkJSONCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { } void SkJSONCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RRECT); command[SKJSONCANVAS_ATTRIBUTE_INNER] = this->makeRRect(inner); @@ -567,7 +579,6 @@ void SkJSONCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, cons void SkJSONCanvas::onDrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_POINTS); command[SKJSONCANVAS_ATTRIBUTE_MODE] = this->makePointMode(mode); @@ -584,15 +595,20 @@ void SkJSONCanvas::onDrawVertices(SkCanvas::VertexMode, int vertexCount, const S const SkPoint texs[], const SkColor colors[], SkXfermode*, const uint16_t indices[], int indexCount, const SkPaint&) { SkDebugf("unsupported: drawVertices\n"); + Json::Value command(Json::objectValue); + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_VERTICES); + fCommands.append(command); } void SkJSONCanvas::onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int count, SkXfermode::Mode, const SkRect* cull, const SkPaint*) { SkDebugf("unsupported: drawAtlas\n"); + Json::Value command(Json::objectValue); + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_ATLAS); + fCommands.append(command); } void SkJSONCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_PATH); command[SKJSONCANVAS_ATTRIBUTE_PATH] = this->makePath(path); @@ -604,7 +620,6 @@ void SkJSONCanvas::onDrawImage(const SkImage* image, SkScalar dx, SkScalar dy, const SkPaint* paint) { Json::Value encoded; if (flatten(*image, &encoded, fSendBinaries)) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_IMAGE); command[SKJSONCANVAS_ATTRIBUTE_IMAGE] = encoded; @@ -620,7 +635,6 @@ void SkJSONCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, cons const SkPaint* paint, SkCanvas::SrcRectConstraint constraint) { Json::Value encoded; if (flatten(*image, &encoded, fSendBinaries)) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_IMAGERECT); command[SKJSONCANVAS_ATTRIBUTE_IMAGE] = encoded; @@ -641,13 +655,15 @@ void SkJSONCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, cons void SkJSONCanvas::onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst, const SkPaint*) { SkDebugf("unsupported: drawImageNine\n"); + Json::Value command(Json::objectValue); + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_IMAGENINE); + fCommands.append(command); } void SkJSONCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar dx, SkScalar dy, const SkPaint* paint) { Json::Value encoded; if (flatten(bitmap, &encoded, fSendBinaries)) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_BITMAP); command[SKJSONCANVAS_ATTRIBUTE_BITMAP] = encoded; @@ -663,7 +679,6 @@ void SkJSONCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, c const SkPaint* paint, SkCanvas::SrcRectConstraint constraint) { Json::Value encoded; if (flatten(bitmap, &encoded, fSendBinaries)) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_BITMAPRECT); command[SKJSONCANVAS_ATTRIBUTE_BITMAP] = encoded; @@ -684,11 +699,13 @@ void SkJSONCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, c void SkJSONCanvas::onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst, const SkPaint*) { SkDebugf("unsupported: drawBitmapNine\n"); + Json::Value command(Json::objectValue); + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_BITMAPNINE); + fCommands.append(command); } void SkJSONCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, const SkPaint& paint) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_TEXT); command[SKJSONCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) text, @@ -700,7 +717,6 @@ void SkJSONCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, void SkJSONCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], const SkPaint& paint) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_POSTEXT); command[SKJSONCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) text, @@ -718,31 +734,51 @@ void SkJSONCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY, const SkPaint& paint) { SkDebugf("unsupported: drawPosTextH\n"); + Json::Value command(Json::objectValue); + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_POSTEXTH); + fCommands.append(command); } void SkJSONCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) { - SkDebugf("unsupported: drawTextOnPath\n"); + Json::Value command(Json::objectValue); + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_TEXTONPATH); + command[SKJSONCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) text, + ((const char*) text) + byteLength); + command[SKJSONCANVAS_ATTRIBUTE_PATH] = this->makePath(path); + if (matrix != nullptr) { + command[SKJSONCANVAS_ATTRIBUTE_MATRIX] = this->makeMatrix(*matrix); + } + command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint); + fCommands.append(command); } void SkJSONCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) { SkDebugf("unsupported: drawTextBlob\n"); + Json::Value command(Json::objectValue); + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_TEXTBLOB); + fCommands.append(command); } void SkJSONCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) { SkDebugf("unsupported: drawPatch\n"); + Json::Value command(Json::objectValue); + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_PATCH); + fCommands.append(command); } void SkJSONCanvas::onDrawDrawable(SkDrawable*, const SkMatrix*) { SkDebugf("unsupported: drawDrawable\n"); + Json::Value command(Json::objectValue); + command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_DRAWABLE); + fCommands.append(command); } void SkJSONCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_CLIPRECT); command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makeRect(rect); @@ -752,7 +788,6 @@ void SkJSONCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle } void SkJSONCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_CLIPRRECT); command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makeRRect(rrect); @@ -762,7 +797,6 @@ void SkJSONCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeSt } void SkJSONCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_CLIPPATH); command[SKJSONCANVAS_ATTRIBUTE_PATH] = this->makePath(path); @@ -772,7 +806,6 @@ void SkJSONCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle } void SkJSONCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_CLIPREGION); command[SKJSONCANVAS_ATTRIBUTE_REGION] = this->makeRegion(deviceRgn); @@ -781,7 +814,6 @@ void SkJSONCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { } void SkJSONCanvas::willSave() { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_SAVE); fCommands.append(command); @@ -794,7 +826,6 @@ void SkJSONCanvas::willRestore() { } SkCanvas::SaveLayerStrategy SkJSONCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) { - this->updateMatrix(); Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_SAVELAYER); if (rec.fBounds != nullptr) { diff --git a/tools/json/SkJSONCanvas.h b/tools/json/SkJSONCanvas.h index 2e016df937..b43606447b 100644 --- a/tools/json/SkJSONCanvas.h +++ b/tools/json/SkJSONCanvas.h @@ -16,6 +16,8 @@ #define SKJSONCANVAS_COMMANDS "commands" #define SKJSONCANVAS_COMMAND "command" +#define SKJSONCANVAS_COMMAND_TRANSLATE "Translate" +#define SKJSONCANVAS_COMMAND_SCALE "Scale" #define SKJSONCANVAS_COMMAND_MATRIX "Matrix" #define SKJSONCANVAS_COMMAND_PAINT "Paint" #define SKJSONCANVAS_COMMAND_RECT "Rect" @@ -94,6 +96,8 @@ #define SKJSONCANVAS_ATTRIBUTE_DST "dst" #define SKJSONCANVAS_ATTRIBUTE_STRICT "strict" #define SKJSONCANVAS_ATTRIBUTE_DESCRIPTION "description" +#define SKJSONCANVAS_ATTRIBUTE_X "x" +#define SKJSONCANVAS_ATTRIBUTE_Y "y" #define SKJSONCANVAS_VERB_MOVE "move" #define SKJSONCANVAS_VERB_LINE "line" @@ -169,6 +173,10 @@ public: // overridden SkCanvas API + void didConcat(const SkMatrix&) override; + + void didSetMatrix(const SkMatrix&) override; + void onDrawPaint(const SkPaint&) override; void onDrawRect(const SkRect&, const SkPaint&) override; @@ -268,7 +276,6 @@ private: void updateMatrix(); SkWStream& fOut; - SkMatrix fLastMatrix; Json::Value fRoot; Json::Value fCommands; bool fSendBinaries; diff --git a/tools/json/SkJSONRenderer.cpp b/tools/json/SkJSONRenderer.cpp index 4049770c6e..a44503af5e 100644 --- a/tools/json/SkJSONRenderer.cpp +++ b/tools/json/SkJSONRenderer.cpp @@ -18,18 +18,24 @@ namespace SkJSONRenderer { class Renderer { public: - void getPaint(Json::Value& command, SkPaint* paint); + void getPaint(Json::Value& paint, SkPaint* result); - void getRect(Json::Value& command, const char* name, SkRect* rect); + void getRect(Json::Value& rect, SkRect* result); - void getRRect(Json::Value& command, const char* name, SkRRect* rrect); + void getRRect(Json::Value& rrect, SkRRect* result); - void getPath(Json::Value& command, SkPath* path); + void getPath(Json::Value& path, SkPath* result); - SkRegion::Op getRegionOp(Json::Value& command); + void getMatrix(Json::Value& matrix, SkMatrix* result); + + SkRegion::Op getRegionOp(Json::Value& op); void processCommand(Json::Value& command, SkCanvas* target); + void processTranslate(Json::Value& command, SkCanvas* target); + + void processScale(Json::Value& command, SkCanvas* target); + void processMatrix(Json::Value& command, SkCanvas* target); void processSave(Json::Value& command, SkCanvas* target); @@ -52,6 +58,8 @@ public: void processPosText(Json::Value& command, SkCanvas* target); + void processTextOnPath(Json::Value& command, SkCanvas* target); + void processPoints(Json::Value& command, SkCanvas* target); void processImage(Json::Value& command, SkCanvas* target); @@ -72,7 +80,13 @@ public: 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)) { + if (!strcmp(name, SKJSONCANVAS_COMMAND_TRANSLATE)) { + this->processTranslate(command, target); + } + else if (!strcmp(name, SKJSONCANVAS_COMMAND_SCALE)) { + this->processScale(command, target); + } + else if (!strcmp(name, SKJSONCANVAS_COMMAND_MATRIX)) { this->processMatrix(command, target); } else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVE)) { @@ -105,6 +119,9 @@ void Renderer::processCommand(Json::Value& command, SkCanvas* target) { else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) { this->processPosText(command, target); } + else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXTONPATH)) { + this->processTextOnPath(command, target); + } else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) { this->processPoints(command, target); } @@ -454,34 +471,31 @@ static void apply_paint_textskewx(Json::Value& jsonPaint, SkPaint* target) { } } -void Renderer::getPaint(Json::Value& command, SkPaint* result) { - Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT]; - apply_paint_color(jsonPaint, result); - apply_paint_shader(jsonPaint, result); - apply_paint_patheffect(jsonPaint, result); - apply_paint_maskfilter(jsonPaint, result); - apply_paint_xfermode(jsonPaint, result); - apply_paint_imagefilter(jsonPaint, result); - apply_paint_style(jsonPaint, result); - apply_paint_strokewidth(jsonPaint, result); - apply_paint_strokemiter(jsonPaint, result); - apply_paint_cap(jsonPaint, result); - apply_paint_antialias(jsonPaint, result); - apply_paint_blur(jsonPaint, result); - apply_paint_dashing(jsonPaint, result); - apply_paint_textalign(jsonPaint, result); - apply_paint_textsize(jsonPaint, result); - apply_paint_textscalex(jsonPaint, result); - apply_paint_textskewx(jsonPaint, result); -} - -void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) { - Json::Value rect = command[name]; +void Renderer::getPaint(Json::Value& paint, SkPaint* result) { + apply_paint_color(paint, result); + apply_paint_shader(paint, result); + apply_paint_patheffect(paint, result); + apply_paint_maskfilter(paint, result); + apply_paint_xfermode(paint, result); + apply_paint_imagefilter(paint, result); + apply_paint_style(paint, result); + apply_paint_strokewidth(paint, result); + apply_paint_strokemiter(paint, result); + apply_paint_cap(paint, result); + apply_paint_antialias(paint, result); + apply_paint_blur(paint, result); + apply_paint_dashing(paint, result); + apply_paint_textalign(paint, result); + apply_paint_textsize(paint, result); + apply_paint_textscalex(paint, result); + apply_paint_textskewx(paint, result); +} + +void Renderer::getRect(Json::Value& rect, SkRect* result) { 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]; +void Renderer::getRRect(Json::Value& rrect, SkRRect* result) { SkVector radii[4] = { { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, @@ -490,11 +504,19 @@ void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result) }; result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asFloat(), rrect[0][2].asFloat(), rrect[0][3].asFloat()), - radii); + radii); } -void Renderer::getPath(Json::Value& command, SkPath* result) { - Json::Value path = command[SKJSONCANVAS_ATTRIBUTE_PATH]; +void Renderer::getMatrix(Json::Value& matrix, SkMatrix* result) { + SkScalar values[] = { + matrix[0][0].asFloat(), matrix[0][1].asFloat(), matrix[0][2].asFloat(), + matrix[1][0].asFloat(), matrix[1][1].asFloat(), matrix[1][2].asFloat(), + matrix[2][0].asFloat(), matrix[2][1].asFloat(), matrix[2][2].asFloat() + }; + result->set9(values); +} + +void Renderer::getPath(Json::Value& path, SkPath* result) { const char* fillType = path[SKJSONCANVAS_ATTRIBUTE_FILLTYPE].asCString(); if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_WINDING)) { result->setFillType(SkPath::kWinding_FillType); @@ -548,8 +570,8 @@ void Renderer::getPath(Json::Value& command, SkPath* result) { } } -SkRegion::Op Renderer::getRegionOp(Json::Value& command) { - const char* op = command[SKJSONCANVAS_ATTRIBUTE_REGIONOP].asCString(); +SkRegion::Op Renderer::getRegionOp(Json::Value& jsonOp) { + const char* op = jsonOp.asCString(); if (!strcmp(op, SKJSONCANVAS_REGIONOP_DIFFERENCE)) { return SkRegion::kDifference_Op; } @@ -572,15 +594,19 @@ SkRegion::Op Renderer::getRegionOp(Json::Value& command) { return SkRegion::kIntersect_Op; } +void Renderer::processTranslate(Json::Value& command, SkCanvas* target) { + target->translate(command[SKJSONCANVAS_ATTRIBUTE_X].asFloat(), + command[SKJSONCANVAS_ATTRIBUTE_Y].asFloat()); +} + +void Renderer::processScale(Json::Value& command, SkCanvas* target) { + target->scale(command[SKJSONCANVAS_ATTRIBUTE_X].asFloat(), + command[SKJSONCANVAS_ATTRIBUTE_Y].asFloat()); +} + 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); + this->getMatrix(command[SKJSONCANVAS_ATTRIBUTE_MATRIX], &matrix); target->setMatrix(matrix); } @@ -596,12 +622,12 @@ void Renderer::processSaveLayer(Json::Value& command, SkCanvas* target) { SkCanvas::SaveLayerRec rec; SkRect bounds; if (command.isMember(SKJSONCANVAS_ATTRIBUTE_BOUNDS)) { - this->getRect(command, SKJSONCANVAS_ATTRIBUTE_BOUNDS, &bounds); + this->getRect(command[SKJSONCANVAS_ATTRIBUTE_BOUNDS], &bounds); rec.fBounds = &bounds; } SkPaint paint; if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); rec.fPaint = &paint; } if (command.isMember(SKJSONCANVAS_ATTRIBUTE_BACKDROP)) { @@ -615,47 +641,46 @@ void Renderer::processSaveLayer(Json::Value& command, SkCanvas* target) { void Renderer::processPaint(Json::Value& command, SkCanvas* target) { SkPaint paint; - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); target->drawPaint(paint); } void Renderer::processRect(Json::Value& command, SkCanvas* target) { SkRect rect; - this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); + this->getRect(command[SKJSONCANVAS_ATTRIBUTE_COORDS], &rect); SkPaint paint; - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); target->drawRect(rect, paint); } void Renderer::processRRect(Json::Value& command, SkCanvas* target) { SkRRect rrect; - this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect); + this->getRRect(command[SKJSONCANVAS_ATTRIBUTE_COORDS], &rrect); SkPaint paint; - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); target->drawRRect(rrect, paint); } void Renderer::processOval(Json::Value& command, SkCanvas* target) { SkRect rect; - this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); + this->getRect(command[SKJSONCANVAS_ATTRIBUTE_COORDS], &rect); SkPaint paint; - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &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); + this->getPath(command[SKJSONCANVAS_ATTRIBUTE_PATH], &path); SkPaint paint; - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &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); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; target->drawText(text, strlen(text), coords[0].asFloat(), coords[1].asFloat(), paint); } @@ -663,7 +688,7 @@ void Renderer::processText(Json::Value& command, SkCanvas* target) { void Renderer::processPosText(Json::Value& command, SkCanvas* target) { const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString(); SkPaint paint; - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; int count = (int) coords.size(); SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint)); @@ -674,6 +699,24 @@ void Renderer::processPosText(Json::Value& command, SkCanvas* target) { free(points); } +void Renderer::processTextOnPath(Json::Value& command, SkCanvas* target) { + const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString(); + SkPath path; + this->getPath(command[SKJSONCANVAS_ATTRIBUTE_PATH], &path); + SkMatrix* matrixPtr; + SkMatrix matrix; + if (command.isMember(SKJSONCANVAS_ATTRIBUTE_MATRIX)) { + this->getMatrix(command[SKJSONCANVAS_ATTRIBUTE_MATRIX], &matrix); + matrixPtr = &matrix; + } + else { + matrixPtr = nullptr; + } + SkPaint paint; + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); + target->drawTextOnPath(text, strlen(text), path, matrixPtr, paint); +} + void Renderer::processPoints(Json::Value& command, SkCanvas* target) { SkCanvas::PointMode mode; const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString(); @@ -697,29 +740,29 @@ void Renderer::processPoints(Json::Value& command, SkCanvas* target) { points[i] = SkPoint::Make(jsonPoints[i][0].asFloat(), jsonPoints[i][1].asFloat()); } SkPaint paint; - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &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), + this->getRect(command[SKJSONCANVAS_ATTRIBUTE_COORDS], &rect); + target->clipRect(rect, this->getRegionOp(command[SKJSONCANVAS_ATTRIBUTE_REGIONOP]), 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), + this->getRRect(command[SKJSONCANVAS_ATTRIBUTE_COORDS], &rrect); + target->clipRRect(rrect, this->getRegionOp(command[SKJSONCANVAS_ATTRIBUTE_REGIONOP]), 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), + this->getPath(command[SKJSONCANVAS_ATTRIBUTE_PATH], &path); + target->clipPath(path, this->getRegionOp(command[SKJSONCANVAS_ATTRIBUTE_REGIONOP]), command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); } @@ -732,7 +775,7 @@ void Renderer::processImage(Json::Value& command, SkCanvas* target) { SkPaint* paintPtr; SkPaint paint; if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); paintPtr = &paint; } else { @@ -748,11 +791,11 @@ void Renderer::processImageRect(Json::Value& command, SkCanvas* target) { return; } SkRect dst; - this->getRect(command, SKJSONCANVAS_ATTRIBUTE_DST, &dst); + this->getRect(command[SKJSONCANVAS_ATTRIBUTE_DST], &dst); SkPaint* paintPtr; SkPaint paint; if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); paintPtr = &paint; } else { @@ -768,7 +811,7 @@ void Renderer::processImageRect(Json::Value& command, SkCanvas* target) { } if (command.isMember(SKJSONCANVAS_ATTRIBUTE_SRC)) { SkRect src; - this->getRect(command, SKJSONCANVAS_ATTRIBUTE_SRC, &src); + this->getRect(command[SKJSONCANVAS_ATTRIBUTE_SRC], &src); target->drawImageRect(image, src, dst, paintPtr, constraint); } else { @@ -786,7 +829,7 @@ void Renderer::processBitmap(Json::Value& command, SkCanvas* target) { SkPaint* paintPtr; SkPaint paint; if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); paintPtr = &paint; } else { @@ -802,11 +845,11 @@ void Renderer::processBitmapRect(Json::Value& command, SkCanvas* target) { return; } SkRect dst; - this->getRect(command, SKJSONCANVAS_ATTRIBUTE_DST, &dst); + this->getRect(command[SKJSONCANVAS_ATTRIBUTE_DST], &dst); SkPaint* paintPtr; SkPaint paint; if (command.isMember(SKJSONCANVAS_ATTRIBUTE_PAINT)) { - this->getPaint(command, &paint); + this->getPaint(command[SKJSONCANVAS_ATTRIBUTE_PAINT], &paint); paintPtr = &paint; } else { @@ -822,7 +865,7 @@ void Renderer::processBitmapRect(Json::Value& command, SkCanvas* target) { } if (command.isMember(SKJSONCANVAS_ATTRIBUTE_SRC)) { SkRect src; - this->getRect(command, SKJSONCANVAS_ATTRIBUTE_SRC, &src); + this->getRect(command[SKJSONCANVAS_ATTRIBUTE_SRC], &src); target->drawBitmapRect(*bitmap, src, dst, paintPtr, constraint); } else { |