switched to SkJSONCPP for JSON output
authorethannicholas <ethannicholas@google.com>
Wed, 27 Jan 2016 17:06:35 +0000 (09:06 -0800)
committerCommit bot <commit-bot@chromium.org>
Wed, 27 Jan 2016 17:06:35 +0000 (09:06 -0800)
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1639333002

Review URL: https://codereview.chromium.org/1639333002

tools/json/SkJSONCanvas.cpp
tools/json/SkJSONCanvas.h
tools/json/SkJSONRenderer.h

index f60ee27..4bd23db 100644 (file)
 #include "SkJSONCanvas.h"
 #include "SkPath.h"
 #include "SkRRect.h"
-#include "stdio.h"
-#include "stdlib.h"
 
 SkJSONCanvas::SkJSONCanvas(int width, int height, SkWStream& out) 
-       : INHERITED(width, height)
-       , fOut(out)
-       , fFirstCommand(true) {
-       fOut.writeText("{\"" SKJSONCANVAS_VERSION "\":1, \"" SKJSONCANVAS_COMMANDS 
-                   "\":[");
+    : INHERITED(width, height)
+    , fOut(out)
+    , fRoot(Json::objectValue)
+    , fCommands(Json::arrayValue) {
+    fRoot[SKJSONCANVAS_VERSION] = Json::Value(1);
 }
 
 void SkJSONCanvas::finish() {
-       fOut.writeText("]}");
+    fRoot[SKJSONCANVAS_COMMANDS] = fCommands;
+    fOut.writeText(Json::FastWriter().write(fRoot).c_str());
 }
 
-void SkJSONCanvas::writef(const char* format, ...) {
-    va_list args;
-    va_start(args, format);
-    SkString s;
-    s.appendVAList(format, args);
-    fOut.writeText(s.c_str());
+Json::Value SkJSONCanvas::makePoint(const SkPoint& point) {
+    Json::Value result(Json::arrayValue);
+    result.append(Json::Value(point.x()));
+    result.append(Json::Value(point.y()));
+    return result;
 }
 
-void SkJSONCanvas::open(const char* name) {
-       if (fFirstCommand) {
-               fFirstCommand = false;
-       }
-       else {
-               fOut.writeText(",");
-       }
-       this->writef("{\"" SKJSONCANVAS_COMMAND "\":\"%s\"", name);
+Json::Value SkJSONCanvas::makePoint(SkScalar x, SkScalar y) {
+    Json::Value result(Json::arrayValue);
+    result.append(Json::Value(x));
+    result.append(Json::Value(y));
+    return result;
 }
 
-void SkJSONCanvas::close() {
-       fOut.writeText("}");
+Json::Value SkJSONCanvas::makeRect(const SkRect& rect) {
+    Json::Value result(Json::arrayValue);
+    result.append(Json::Value(rect.left()));
+    result.append(Json::Value(rect.top()));
+    result.append(Json::Value(rect.right()));
+    result.append(Json::Value(rect.bottom()));
+    return result;
 }
 
-void SkJSONCanvas::writeString(const char* name, const char* text) {
-    this->writeString(name, text, strlen(text));
+Json::Value SkJSONCanvas::makeRRect(const SkRRect& rrect) {
+    Json::Value result(Json::arrayValue);
+    result.append(this->makeRect(rrect.rect()));
+    result.append(this->makePoint(rrect.radii(SkRRect::kUpperLeft_Corner)));
+    result.append(this->makePoint(rrect.radii(SkRRect::kUpperRight_Corner)));
+    result.append(this->makePoint(rrect.radii(SkRRect::kLowerLeft_Corner)));
+    result.append(this->makePoint(rrect.radii(SkRRect::kLowerRight_Corner)));
+    return result;
 }
 
-void SkJSONCanvas::writeString(const char* name, const void* text, size_t length) {
-       // TODO: escaping
-       this->writef(",\"%s\":\"", name);
-       fOut.write(text, length);
-       fOut.writeText("\"");
-}
-
-void SkJSONCanvas::writePoint(const char* name, const SkPoint& point) {
-       this->writef(",\"%s\":[%f, %f]", name, point.x(), point.y());
-}
-
-void SkJSONCanvas::writeRect(const char* name, const SkRect& rect) {
-       this->writef(",\"%s\":[%f, %f, %f, %f]", name, rect.left(), rect.top(), rect.right(), 
-                                rect.bottom());
-}
-
-void SkJSONCanvas::writeRRect(const char* name, const SkRRect& rrect) {
-       SkRect rect = rrect.rect();
-       SkVector corner1 = rrect.radii(SkRRect::kUpperLeft_Corner);
-       SkVector corner2 = rrect.radii(SkRRect::kUpperRight_Corner);
-       SkVector corner3 = rrect.radii(SkRRect::kLowerLeft_Corner);
-       SkVector corner4 = rrect.radii(SkRRect::kLowerRight_Corner);
-       this->writef(",\"%s\":[[%f, %f, %f, %f],[%f, %f],[%f, %f],[%f, %f],[%f, %f]]", name, 
-                                rect.left(), rect.top(), rect.right(), rect.bottom(), corner1.x(), corner1.y(), 
-                                corner2.x(), corner2.y(), corner3.x(), corner3.y(), corner4.x(), corner4.y());
-}
-
-void SkJSONCanvas::writePath(const char* name, const SkPath& path) {
-       SkString text("[");
+Json::Value SkJSONCanvas::makePath(const SkPath& path) {
+    Json::Value result(Json::arrayValue);
     SkPath::Iter iter(path, false);
     SkPoint pts[4];
-    bool first = true;
     SkPath::Verb verb;
     while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
-       if (first) {
-               first = false;
-       }
-       else {
-                       text.append(",");
-       }
         switch (verb) {
-            case SkPath::kLine_Verb:
-                text.appendf("{\"" SKJSONCANVAS_VERB_LINE "\":[%f,%f]}", pts[1].x(), pts[1].y());
+            case SkPath::kLine_Verb: {
+                Json::Value line(Json::objectValue);
+                line[SKJSONCANVAS_VERB_LINE] = this->makePoint(pts[1]);
+                result.append(line);
                 break;
-            case SkPath::kQuad_Verb:
-                text.appendf("{\"" SKJSONCANVAS_VERB_QUAD "\":[[%f,%f],[%f,%f]]}", pts[1].x(), 
-                             pts[1].y(), pts[2].x(), pts[2].y());
+            }
+            case SkPath::kQuad_Verb: {
+                Json::Value quad(Json::objectValue);
+                Json::Value coords(Json::arrayValue);
+                coords.append(this->makePoint(pts[1]));
+                coords.append(this->makePoint(pts[2]));
+                quad[SKJSONCANVAS_VERB_QUAD] = coords;
+                result.append(quad);
                 break;
-            case SkPath::kCubic_Verb:
-                text.appendf("{\"" SKJSONCANVAS_VERB_CUBIC "\":[[%f,%f],[%f,%f],[%f,%f]]}", 
-                             pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y(), pts[3].x(), 
-                             pts[3].y());
+            }
+            case SkPath::kCubic_Verb: {
+                Json::Value cubic(Json::objectValue);
+                Json::Value coords(Json::arrayValue);
+                coords.append(this->makePoint(pts[1]));
+                coords.append(this->makePoint(pts[2]));
+                coords.append(this->makePoint(pts[3]));
+                cubic[SKJSONCANVAS_VERB_CUBIC] = coords;
+                result.append(cubic);
                 break;
-            case SkPath::kConic_Verb:
-                text.appendf("{\"" SKJSONCANVAS_VERB_CONIC "\":[[%f,%f],[%f,%f],%f]}", pts[1].x(), 
-                             pts[1].y(), pts[2].x(), pts[2].y(), iter.conicWeight());
+            }
+            case SkPath::kConic_Verb: {
+                Json::Value conic(Json::objectValue);
+                Json::Value coords(Json::arrayValue);
+                coords.append(this->makePoint(pts[1]));
+                coords.append(this->makePoint(pts[2]));
+                coords.append(Json::Value(iter.conicWeight()));
+                conic[SKJSONCANVAS_VERB_CONIC] = coords;
+                result.append(conic);
                 break;
-            case SkPath::kMove_Verb:
-                text.appendf("{\"" SKJSONCANVAS_VERB_MOVE "\":[%f,%f]}", pts[0].x(), pts[0].y());
+            }
+            case SkPath::kMove_Verb: {
+                Json::Value move(Json::objectValue);
+                move[SKJSONCANVAS_VERB_MOVE] = this->makePoint(pts[0]);
+                result.append(move);
                 break;
+            }
             case SkPath::kClose_Verb:
-                text.appendf("\"" SKJSONCANVAS_VERB_CLOSE "\"");
+                result.append(Json::Value(SKJSONCANVAS_VERB_CLOSE));
                 break;
             case SkPath::kDone_Verb:
                 break;
         }
     }
-    text.appendf("]");
-       this->writef(",\"" SKJSONCANVAS_ATTRIBUTE_PATH "\":%s", text.c_str());
+    return result;
 }
 
-void SkJSONCanvas::writeRegion(const char* name, const SkRegion& region) {
-       this->writef(",\"%s\":\"<unimplemented>\"", name);
+Json::Value SkJSONCanvas::makeRegion(const SkRegion& region) {
+    return Json::Value("<unimplemented>");
 }
 
-void SkJSONCanvas::writePaint(const SkPaint& paint) {
-       this->writef(",\"" SKJSONCANVAS_ATTRIBUTE_PAINT "\":{");
-       SkColor color = paint.getColor();
-       bool first = true;
-       if (color != SK_ColorBLACK) {
-               this->writef("\"" SKJSONCANVAS_ATTRIBUTE_COLOR "\":[%d,%d,%d,%d]", SkColorGetA(color), 
-                     SkColorGetR(color), SkColorGetG(color), SkColorGetB(color));
-               first = false;
-       }
+Json::Value SkJSONCanvas::makePaint(const SkPaint& paint) {
+    Json::Value result(Json::objectValue);
+    SkColor color = paint.getColor();
+    if (color != SK_ColorBLACK) {
+        Json::Value colorValue(Json::arrayValue);
+        colorValue.append(Json::Value(SkColorGetA(color)));
+        colorValue.append(Json::Value(SkColorGetR(color)));
+        colorValue.append(Json::Value(SkColorGetG(color)));
+        colorValue.append(Json::Value(SkColorGetB(color)));
+        result[SKJSONCANVAS_ATTRIBUTE_COLOR] = colorValue;;
+    }
     SkPaint::Style style = paint.getStyle();
     if (style != SkPaint::kFill_Style) {
-        if (first) {
-            first = false;
-        }
-        else {
-            fOut.writeText(",");
-        }
         switch (style) {
-            case SkPaint::kStroke_Style:        
-                fOut.writeText("\"" SKJSONCANVAS_ATTRIBUTE_STYLE "\":\""
-                               SKJSONCANVAS_STYLE_STROKE "\"");
+            case SkPaint::kStroke_Style: {
+                Json::Value stroke(SKJSONCANVAS_STYLE_STROKE);
+                result[SKJSONCANVAS_ATTRIBUTE_STYLE] = stroke;
                 break;
-            case SkPaint::kStrokeAndFill_Style: 
-                fOut.writeText("\"" SKJSONCANVAS_ATTRIBUTE_STYLE "\":\""
-                               SKJSONCANVAS_STYLE_STROKEANDFILL "\""); 
+            }
+            case SkPaint::kStrokeAndFill_Style: {
+                Json::Value strokeAndFill(SKJSONCANVAS_STYLE_STROKEANDFILL);
+                result[SKJSONCANVAS_ATTRIBUTE_STYLE] = strokeAndFill;
                 break;
+            }
             default: SkASSERT(false);
         }
     }
     SkScalar strokeWidth = paint.getStrokeWidth();
     if (strokeWidth != 0.0f) {
-        if (first) {
-            first = false;
-        }
-        else {
-            fOut.writeText(",");
-        }
-        this->writef("\"" SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH "\":%f", strokeWidth);
+        result[SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH] = Json::Value(strokeWidth);
     }
     if (paint.isAntiAlias()) {
-        if (first) {
-            first = false;
-        }
-        else {
-            fOut.writeText(",");
-        }
-        fOut.writeText("\"" SKJSONCANVAS_ATTRIBUTE_ANTIALIAS "\":true");
+        result[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS] = Json::Value(true);
     }
-       fOut.writeText("}");
-}
-
-void SkJSONCanvas::writeMatrix(const char* name, const SkMatrix& matrix) {
-       this->writef(",\"%s\":[[%f,%f,%f],[%f,%f,%f],[%f,%f,%f]]", name,
-                                matrix[0], matrix[1], matrix[2],
-                                matrix[3], matrix[4], matrix[5],
-                                matrix[6], matrix[7], matrix[8]);
-}
-
-void SkJSONCanvas::writeRegionOp(const char* name, SkRegion::Op op) {
-       this->writef(",\"%s\":\"", name);
-       switch (op) {
+    return result;
+}
+
+Json::Value SkJSONCanvas::makeMatrix(const SkMatrix& matrix) {
+    Json::Value result(Json::arrayValue);
+    Json::Value row1(Json::arrayValue);
+    row1.append(Json::Value(matrix[0]));
+    row1.append(Json::Value(matrix[1]));
+    row1.append(Json::Value(matrix[2]));
+    result.append(row1);
+    Json::Value row2(Json::arrayValue);
+    row2.append(Json::Value(matrix[3]));
+    row2.append(Json::Value(matrix[4]));
+    row2.append(Json::Value(matrix[5]));
+    result.append(row2);
+    Json::Value row3(Json::arrayValue);
+    row3.append(Json::Value(matrix[6]));
+    row3.append(Json::Value(matrix[7]));
+    row3.append(Json::Value(matrix[8]));
+    result.append(row3);
+    return result;
+}
+
+Json::Value SkJSONCanvas::makeRegionOp(SkRegion::Op op) {
+    switch (op) {
         case SkRegion::kDifference_Op:
-            fOut.writeText(SKJSONCANVAS_REGIONOP_DIFFERENCE);
-            break;
+            return Json::Value(SKJSONCANVAS_REGIONOP_DIFFERENCE);
         case SkRegion::kIntersect_Op:
-            fOut.writeText(SKJSONCANVAS_REGIONOP_INTERSECT);
-            break;
+            return Json::Value(SKJSONCANVAS_REGIONOP_INTERSECT);
         case SkRegion::kUnion_Op:
-            fOut.writeText(SKJSONCANVAS_REGIONOP_UNION);
-            break;
+            return Json::Value(SKJSONCANVAS_REGIONOP_UNION);
         case SkRegion::kXOR_Op:
-            fOut.writeText(SKJSONCANVAS_REGIONOP_XOR);
-            break;
+            return Json::Value(SKJSONCANVAS_REGIONOP_XOR);
         case SkRegion::kReverseDifference_Op:
-            fOut.writeText(SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE);
-            break;
+            return Json::Value(SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE);
         case SkRegion::kReplace_Op:
-            fOut.writeText(SKJSONCANVAS_REGIONOP_REPLACE);
-            break;
+            return Json::Value(SKJSONCANVAS_REGIONOP_REPLACE);
         default:
             SkASSERT(false);
+            return Json::Value("<invalid region op>");
     };
-    fOut.writeText("\"");
 }
 
-void SkJSONCanvas::writeEdgeStyle(const char* name, SkCanvas::ClipEdgeStyle edgeStyle) {
-       this->writef(",\"%s\":\"", name);
-       switch (edgeStyle) {
-        case SkCanvas::kHard_ClipEdgeStyle: fOut.writeText(SKJSONCANVAS_EDGESTYLE_HARD); break;
-        case SkCanvas::kSoft_ClipEdgeStyle: fOut.writeText(SKJSONCANVAS_EDGESTYLE_SOFT); break;
-        default:                            SkASSERT(false);
+Json::Value SkJSONCanvas::makeEdgeStyle(SkCanvas::ClipEdgeStyle edgeStyle) {
+    switch (edgeStyle) {
+        case SkCanvas::kHard_ClipEdgeStyle: 
+            return Json::Value(SKJSONCANVAS_EDGESTYLE_HARD);
+        case SkCanvas::kSoft_ClipEdgeStyle:
+            return Json::Value(SKJSONCANVAS_EDGESTYLE_SOFT);
+        default:
+            SkASSERT(false);
+            return Json::Value("<invalid edge style>");
     };
-    fOut.writeText("\"");
 }
 
-void SkJSONCanvas::writePointMode(const char* name, SkCanvas::PointMode mode) {
-    this->writef(",\"%s\":\"", name);
+Json::Value SkJSONCanvas::makePointMode(SkCanvas::PointMode mode) {
     switch (mode) {
-        case SkCanvas::kPoints_PointMode:  fOut.writeText(SKJSONCANVAS_POINTMODE_POINTS);  break;
-        case SkCanvas::kLines_PointMode:   fOut.writeText(SKJSONCANVAS_POINTMODE_LINES);   break;
-        case SkCanvas::kPolygon_PointMode: fOut.writeText(SKJSONCANVAS_POINTMODE_POLYGON); break;
-        default:                           SkASSERT(false);
+        case SkCanvas::kPoints_PointMode:
+            return Json::Value(SKJSONCANVAS_POINTMODE_POINTS);
+        case SkCanvas::kLines_PointMode:
+            return Json::Value(SKJSONCANVAS_POINTMODE_LINES);
+        case SkCanvas::kPolygon_PointMode: 
+            return Json::Value(SKJSONCANVAS_POINTMODE_POLYGON);
+        default:
+            SkASSERT(false);
+            return Json::Value("<invalid point mode>");
     };
-    fOut.writeText("\"");
 }
 
 void SkJSONCanvas::updateMatrix() {
-       const SkMatrix& matrix = this->getTotalMatrix();
-       if (matrix != fLastMatrix) {
-               this->open(SKJSONCANVAS_COMMAND_MATRIX);
-               this->writeMatrix(SKJSONCANVAS_ATTRIBUTE_MATRIX, matrix);
-               fLastMatrix = matrix;
-               this->close();
-       }
+    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] = this->makeMatrix(matrix);
+        fCommands.append(command);
+        fLastMatrix = matrix;
+    }
 }
 
 void SkJSONCanvas::onDrawPaint(const SkPaint& paint) {
-       this->open(SKJSONCANVAS_COMMAND_PAINT);
-       this->writePaint(paint);
-       this->close();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_PAINT);
+    command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
-       this->updateMatrix();
-       this->open(SKJSONCANVAS_COMMAND_RECT);
-       this->writeRect(SKJSONCANVAS_ATTRIBUTE_COORDS, rect);
-       this->writePaint(paint);
-       this->close();
+    this->updateMatrix();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RECT);
+    command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makeRect(rect);
+    command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
-       this->updateMatrix();
-       this->open(SKJSONCANVAS_COMMAND_OVAL);
-       this->writeRect(SKJSONCANVAS_ATTRIBUTE_COORDS, rect);
-       this->writePaint(paint);
-       this->close();
+    this->updateMatrix();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_OVAL);
+    command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makeRect(rect);
+    command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
-       this->updateMatrix();
-       this->open(SKJSONCANVAS_COMMAND_RRECT);
-       this->writeRRect(SKJSONCANVAS_ATTRIBUTE_COORDS, rrect);
-       this->writePaint(paint);
-       this->close();}
+    this->updateMatrix();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RRECT);
+    command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makeRRect(rrect);
+    command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint);
+    fCommands.append(command);
+}
 
 void SkJSONCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
     this->updateMatrix();
-    this->open(SKJSONCANVAS_COMMAND_DRRECT);
-    this->writeRRect(SKJSONCANVAS_ATTRIBUTE_OUTER, outer);
-    this->writeRRect(SKJSONCANVAS_ATTRIBUTE_INNER, inner);
-    this->writePaint(paint);
-    this->close();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RRECT);
+    command[SKJSONCANVAS_ATTRIBUTE_INNER] = this->makeRRect(inner);
+    command[SKJSONCANVAS_ATTRIBUTE_OUTER] = this->makeRRect(outer);
+    command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::onDrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[], 
-                                                               const SkPaint& paint) {
+                                const SkPaint& paint) {
     this->updateMatrix();
-    this->open(SKJSONCANVAS_COMMAND_POINTS);
-    this->writePointMode(SKJSONCANVAS_ATTRIBUTE_MODE, mode);
-    fOut.writeText(",\"" SKJSONCANVAS_ATTRIBUTE_POINTS "\":[");
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_POINTS);
+    command[SKJSONCANVAS_ATTRIBUTE_MODE] = this->makePointMode(mode);
+    Json::Value points(Json::arrayValue);
     for (size_t i = 0; i < count; i++) {
-        if (i != 0) {
-            fOut.writeText(",");
-        }
-        this->writef("[%f,%f]", pts[i].x(), pts[i].y());
+        points.append(this->makePoint(pts[i]));
     }
-    fOut.writeText("]");
-    this->writePaint(paint);
-    this->close();
+    command[SKJSONCANVAS_ATTRIBUTE_POINTS] = points;
+    command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::onDrawVertices(SkCanvas::VertexMode, int vertexCount, const SkPoint vertices[],
@@ -307,11 +302,13 @@ void SkJSONCanvas::onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[]
 }
 
 void SkJSONCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
-       this->updateMatrix();
-       this->open(SKJSONCANVAS_COMMAND_PATH);
-       this->writePath(SKJSONCANVAS_ATTRIBUTE_PATH, path);
-       this->writePaint(paint);
-       this->close();}
+    this->updateMatrix();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_PATH);
+    command[SKJSONCANVAS_ATTRIBUTE_PATH] = this->makePath(path);
+    command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint);
+    fCommands.append(command);
+}
 
 void SkJSONCanvas::onDrawImage(const SkImage*, SkScalar dx, SkScalar dy, const SkPaint*) {
     SkDebugf("unsupported: drawImage\n");
@@ -344,11 +341,13 @@ void SkJSONCanvas::onDrawBitmapNine(const SkBitmap&, const SkIRect& center, cons
 void SkJSONCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x,
                               SkScalar y, const SkPaint& paint) {
     this->updateMatrix();
-    this->open(SKJSONCANVAS_COMMAND_TEXT);
-    this->writeString(SKJSONCANVAS_ATTRIBUTE_TEXT, text, byteLength);
-    this->writePoint(SKJSONCANVAS_ATTRIBUTE_COORDS, { x, y });
-    this->writePaint(paint);
-    this->close();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_TEXT);
+    command[SKJSONCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) text, 
+                                                       ((const char*) text) + byteLength);
+    command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makePoint(x, y);
+    command[SKJSONCANVAS_ATTRIBUTE_PAINT] = this->makePaint(paint);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::onDrawPosText(const void* text, size_t byteLength,
@@ -384,45 +383,52 @@ void SkJSONCanvas::onDrawDrawable(SkDrawable*, const SkMatrix*) {
 }
 
 void SkJSONCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
-       this->updateMatrix();
-       this->open(SKJSONCANVAS_COMMAND_CLIPRECT);
-       this->writeRect(SKJSONCANVAS_ATTRIBUTE_COORDS, rect);
-       this->writeRegionOp(SKJSONCANVAS_ATTRIBUTE_REGIONOP, op);
-       this->writeEdgeStyle(SKJSONCANVAS_ATTRIBUTE_EDGESTYLE, edgeStyle);
-       this->close();
+    this->updateMatrix();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_CLIPRECT);
+    command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makeRect(rect);
+    command[SKJSONCANVAS_ATTRIBUTE_REGIONOP] = this->makeRegionOp(op);
+    command[SKJSONCANVAS_ATTRIBUTE_EDGESTYLE] = this->makeEdgeStyle(edgeStyle);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
-       this->updateMatrix();
-       this->open(SKJSONCANVAS_COMMAND_CLIPRRECT);
-       this->writeRRect(SKJSONCANVAS_ATTRIBUTE_COORDS, rrect);
-       this->writeRegionOp(SKJSONCANVAS_ATTRIBUTE_REGIONOP, op);
-       this->writeEdgeStyle(SKJSONCANVAS_ATTRIBUTE_EDGESTYLE, edgeStyle);
-       this->close();
+    this->updateMatrix();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_CLIPRRECT);
+    command[SKJSONCANVAS_ATTRIBUTE_COORDS] = this->makeRRect(rrect);
+    command[SKJSONCANVAS_ATTRIBUTE_REGIONOP] = this->makeRegionOp(op);
+    command[SKJSONCANVAS_ATTRIBUTE_EDGESTYLE] = this->makeEdgeStyle(edgeStyle);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
-       updateMatrix();
-       this->open(SKJSONCANVAS_COMMAND_CLIPPATH);
-       this->writePath(SKJSONCANVAS_ATTRIBUTE_PATH, path);
-       this->writeRegionOp(SKJSONCANVAS_ATTRIBUTE_REGIONOP, op);
-       this->writeEdgeStyle(SKJSONCANVAS_ATTRIBUTE_EDGESTYLE, edgeStyle);
-       this->close();
+    this->updateMatrix();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_CLIPPATH);
+    command[SKJSONCANVAS_ATTRIBUTE_PATH] = this->makePath(path);
+    command[SKJSONCANVAS_ATTRIBUTE_REGIONOP] = this->makeRegionOp(op);
+    command[SKJSONCANVAS_ATTRIBUTE_EDGESTYLE] = this->makeEdgeStyle(edgeStyle);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
-       this->open(SKJSONCANVAS_COMMAND_CLIPREGION);
-       this->writeRegion(SKJSONCANVAS_ATTRIBUTE_DEVICEREGION, deviceRgn);
-       this->writeRegionOp(SKJSONCANVAS_ATTRIBUTE_REGIONOP, op);
-       this->close();
+    this->updateMatrix();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_CLIPREGION);
+    command[SKJSONCANVAS_ATTRIBUTE_REGION] = this->makeRegion(deviceRgn);
+    command[SKJSONCANVAS_ATTRIBUTE_REGIONOP] = this->makeRegionOp(op);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::willSave() {
-       this->open(SKJSONCANVAS_COMMAND_SAVE);
-       this->close();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_SAVE);
+    fCommands.append(command);
 }
 
 void SkJSONCanvas::willRestore() {
-       this->open(SKJSONCANVAS_COMMAND_RESTORE);
-       this->close();
+    Json::Value command(Json::objectValue);
+    command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_RESTORE);
+    fCommands.append(command);
 }
index fcf7c37..66b413e 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "SkCanvas.h"
 #include "SkStream.h"
+#include "SkJSONCPP.h"
 
 #define SKJSONCANVAS_VERSION                     "version"
 #define SKJSONCANVAS_COMMANDS                    "commands"
@@ -58,6 +59,7 @@
 #define SKJSONCANVAS_ATTRIBUTE_STYLE             "style"
 #define SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH       "strokeWidth"
 #define SKJSONCANVAS_ATTRIBUTE_ANTIALIAS         "antiAlias"
+#define SKJSONCANVAS_ATTRIBUTE_REGION            "region"
 #define SKJSONCANVAS_ATTRIBUTE_REGIONOP          "op"
 #define SKJSONCANVAS_ATTRIBUTE_EDGESTYLE         "edgeStyle"
 #define SKJSONCANVAS_ATTRIBUTE_DEVICEREGION      "deviceRegion"
@@ -175,41 +177,34 @@ public:
     void willRestore() override;
 
 private:
-    void writef(const char* fmt, ...);
+    Json::Value makePoint(const SkPoint& point);
 
-    void open(const char* name);
+    Json::Value makePoint(SkScalar x, SkScalar y);
 
-    void close();
+    Json::Value makeRect(const SkRect& rect);
 
-    void writeString(const char* name, const char* text);
+    Json::Value makeRRect(const SkRRect& rrect);
 
-    void writeString(const char* name, const void* text, size_t length);
+    Json::Value makePath(const SkPath& path);
 
-    void writePoint(const char* name, const SkPoint& point);
-
-    void writeRect(const char* name, const SkRect& rect);
-
-    void writeRRect(const char* name, const SkRRect& rrect);
-
-    void writePath(const char* name, const SkPath& path);
-
-    void writeRegion(const char* name, const SkRegion& region);
+    Json::Value makeRegion(const SkRegion& region);
   
-    void writePaint(const SkPaint& paint);
+    Json::Value makePaint(const SkPaint& paint);
   
-    void writeRegionOp(const char* name, SkRegion::Op op);
+    Json::Value makeRegionOp(SkRegion::Op op);
   
-    void writeEdgeStyle(const char* name, SkCanvas::ClipEdgeStyle edgeStyle);
+    Json::Value makeEdgeStyle(SkCanvas::ClipEdgeStyle edgeStyle);
   
-    void writePointMode(const char* name, SkCanvas::PointMode mode);
+    Json::Value makePointMode(SkCanvas::PointMode mode);
   
-    void writeMatrix(const char* name, const SkMatrix& matrix);
+    Json::Value makeMatrix(const SkMatrix& matrix);
   
     void updateMatrix();
 
-    SkWStream& fOut;
-    SkMatrix   fLastMatrix;
-    bool       fFirstCommand;
+    SkWStream&  fOut;
+    SkMatrix    fLastMatrix;
+    Json::Value fRoot;
+    Json::Value fCommands;
 
     typedef SkCanvas INHERITED;
 };
index d92edf2..2df2f50 100644 (file)
@@ -8,7 +8,6 @@
 #ifndef SkJSONRenderer_DEFINED
 #define SkJSONRenderer_DEFINED
 
-
 #include "SkCanvas.h"
 
 namespace SkJSONRenderer {