OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #ifndef SkJSONCanvas_DEFINED |
| 9 #define SkJSONCanvas_DEFINED |
| 10 |
| 11 #include "SkCanvas.h" |
| 12 #include "SkStream.h" |
| 13 |
| 14 #define SKJSONCANVAS_VERSION "version" |
| 15 #define SKJSONCANVAS_COMMANDS "commands" |
| 16 #define SKJSONCANVAS_COMMAND "command" |
| 17 |
| 18 #define SKJSONCANVAS_COMMAND_MATRIX "Matrix" |
| 19 #define SKJSONCANVAS_COMMAND_PAINT "Paint" |
| 20 #define SKJSONCANVAS_COMMAND_RECT "Rect" |
| 21 #define SKJSONCANVAS_COMMAND_OVAL "Oval" |
| 22 #define SKJSONCANVAS_COMMAND_RRECT "RRect" |
| 23 #define SKJSONCANVAS_COMMAND_DRRECT "DRRect" |
| 24 #define SKJSONCANVAS_COMMAND_POINTS "Points" |
| 25 #define SKJSONCANVAS_COMMAND_VERTICES "Vertices" |
| 26 #define SKJSONCANVAS_COMMAND_ATLAS "Atlas" |
| 27 #define SKJSONCANVAS_COMMAND_PATH "Path" |
| 28 #define SKJSONCANVAS_COMMAND_IMAGE "Image" |
| 29 #define SKJSONCANVAS_COMMAND_IMAGERECT "ImageRect" |
| 30 #define SKJSONCANVAS_COMMAND_IMAGENINE "ImageNine" |
| 31 #define SKJSONCANVAS_COMMAND_BITMAP "Bitmap" |
| 32 #define SKJSONCANVAS_COMMAND_BITMAPRECT "BitmapRect" |
| 33 #define SKJSONCANVAS_COMMAND_BITMAPNINE "BitmapNine" |
| 34 #define SKJSONCANVAS_COMMAND_TEXT "Text" |
| 35 #define SKJSONCANVAS_COMMAND_POSTEXT "PosText" |
| 36 #define SKJSONCANVAS_COMMAND_POSTEXTH "PosTextH" |
| 37 #define SKJSONCANVAS_COMMAND_TEXTONPATH "TextOnPath" |
| 38 #define SKJSONCANVAS_COMMAND_TEXTBLOB "TextBlob" |
| 39 #define SKJSONCANVAS_COMMAND_PATCH "Patch" |
| 40 #define SKJSONCANVAS_COMMAND_DRAWABLE "Drawable" |
| 41 #define SKJSONCANVAS_COMMAND_CLIPRECT "ClipRect" |
| 42 #define SKJSONCANVAS_COMMAND_CLIPRRECT "ClipRRect" |
| 43 #define SKJSONCANVAS_COMMAND_CLIPPATH "ClipPath" |
| 44 #define SKJSONCANVAS_COMMAND_CLIPREGION "ClipRegion" |
| 45 #define SKJSONCANVAS_COMMAND_SAVE "Save" |
| 46 #define SKJSONCANVAS_COMMAND_RESTORE "Restore" |
| 47 |
| 48 #define SKJSONCANVAS_ATTRIBUTE_MATRIX "matrix" |
| 49 #define SKJSONCANVAS_ATTRIBUTE_COORDS "coords" |
| 50 #define SKJSONCANVAS_ATTRIBUTE_PAINT "paint" |
| 51 #define SKJSONCANVAS_ATTRIBUTE_OUTER "outer" |
| 52 #define SKJSONCANVAS_ATTRIBUTE_INNER "inner" |
| 53 #define SKJSONCANVAS_ATTRIBUTE_MODE "mode" |
| 54 #define SKJSONCANVAS_ATTRIBUTE_POINTS "points" |
| 55 #define SKJSONCANVAS_ATTRIBUTE_PATH "path" |
| 56 #define SKJSONCANVAS_ATTRIBUTE_TEXT "text" |
| 57 #define SKJSONCANVAS_ATTRIBUTE_COLOR "color" |
| 58 #define SKJSONCANVAS_ATTRIBUTE_STYLE "style" |
| 59 #define SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH "strokeWidth" |
| 60 #define SKJSONCANVAS_ATTRIBUTE_ANTIALIAS "antiAlias" |
| 61 #define SKJSONCANVAS_ATTRIBUTE_REGIONOP "op" |
| 62 #define SKJSONCANVAS_ATTRIBUTE_EDGESTYLE "edgeStyle" |
| 63 #define SKJSONCANVAS_ATTRIBUTE_DEVICEREGION "deviceRegion" |
| 64 |
| 65 #define SKJSONCANVAS_VERB_MOVE "move" |
| 66 #define SKJSONCANVAS_VERB_LINE "line" |
| 67 #define SKJSONCANVAS_VERB_QUAD "quad" |
| 68 #define SKJSONCANVAS_VERB_CUBIC "cubic" |
| 69 #define SKJSONCANVAS_VERB_CONIC "conic" |
| 70 #define SKJSONCANVAS_VERB_CLOSE "close" |
| 71 |
| 72 #define SKJSONCANVAS_STYLE_FILL "fill" |
| 73 #define SKJSONCANVAS_STYLE_STROKE "stroke" |
| 74 #define SKJSONCANVAS_STYLE_STROKEANDFILL "strokeAndFill" |
| 75 |
| 76 #define SKJSONCANVAS_EDGESTYLE_HARD "hard" |
| 77 #define SKJSONCANVAS_EDGESTYLE_SOFT "soft" |
| 78 |
| 79 #define SKJSONCANVAS_POINTMODE_POINTS "points" |
| 80 #define SKJSONCANVAS_POINTMODE_LINES "lines" |
| 81 #define SKJSONCANVAS_POINTMODE_POLYGON "polygon" |
| 82 |
| 83 #define SKJSONCANVAS_REGIONOP_DIFFERENCE "difference" |
| 84 #define SKJSONCANVAS_REGIONOP_INTERSECT "intersect" |
| 85 #define SKJSONCANVAS_REGIONOP_UNION "union" |
| 86 #define SKJSONCANVAS_REGIONOP_XOR "xor" |
| 87 #define SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE "reverseDifference" |
| 88 #define SKJSONCANVAS_REGIONOP_REPLACE "replace" |
| 89 |
| 90 /* |
| 91 * Implementation of SkCanvas which writes JSON when drawn to. The JSON describe
s all of the draw |
| 92 * commands issued to the canvas, and can later be turned back into draw command
s using |
| 93 * SkJSONRenderer. Be sure to call finish() when you are done drawing. |
| 94 */ |
| 95 class SkJSONCanvas : public SkCanvas { |
| 96 public: |
| 97 /* Create a canvas which writes to the specified output stream. */ |
| 98 SkJSONCanvas(int width, int height, SkWStream& out); |
| 99 |
| 100 /* Complete the JSON document. */ |
| 101 void finish(); |
| 102 |
| 103 // overridden SkCanvas API |
| 104 |
| 105 void onDrawPaint(const SkPaint&) override; |
| 106 |
| 107 void onDrawRect(const SkRect&, const SkPaint&) override; |
| 108 |
| 109 void onDrawOval(const SkRect&, const SkPaint&) override; |
| 110 |
| 111 void onDrawRRect(const SkRRect&, const SkPaint&) override; |
| 112 |
| 113 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override; |
| 114 |
| 115 void onDrawPoints(SkCanvas::PointMode, size_t count, const SkPoint pts[], |
| 116 const SkPaint&) override; |
| 117 |
| 118 void onDrawVertices(SkCanvas::VertexMode, int vertexCount, const SkPoint ver
tices[], |
| 119 const SkPoint texs[], const SkColor colors[], SkXfermode
*, |
| 120 const uint16_t indices[], int indexCount, const SkPaint&
) override; |
| 121 |
| 122 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const Sk
Color[], |
| 123 int count, SkXfermode::Mode, const SkRect* cull, const SkPa
int*) override; |
| 124 |
| 125 void onDrawPath(const SkPath&, const SkPaint&) override; |
| 126 |
| 127 void onDrawImage(const SkImage*, SkScalar dx, SkScalar dy, const SkPaint*) o
verride; |
| 128 |
| 129 void onDrawImageRect(const SkImage*, const SkRect*, const SkRect&, const SkP
aint*, |
| 130 SrcRectConstraint) override; |
| 131 |
| 132 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& ds
t, |
| 133 const SkPaint*) override; |
| 134 |
| 135 void onDrawBitmap(const SkBitmap&, SkScalar dx, SkScalar dy, const SkPaint*)
override; |
| 136 |
| 137 void onDrawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&, const S
kPaint*, |
| 138 SkCanvas::SrcRectConstraint) override; |
| 139 |
| 140 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect&
dst, |
| 141 const SkPaint*) override; |
| 142 |
| 143 void onDrawText(const void* text, size_t byteLength, SkScalar x, |
| 144 SkScalar y, const SkPaint& paint) override; |
| 145 |
| 146 void onDrawPosText(const void* text, size_t byteLength, |
| 147 const SkPoint pos[], const SkPaint& paint) override; |
| 148 |
| 149 void onDrawPosTextH(const void* text, size_t byteLength, |
| 150 const SkScalar xpos[], SkScalar constY, |
| 151 const SkPaint& paint) override; |
| 152 |
| 153 void onDrawTextOnPath(const void* text, size_t byteLength, |
| 154 const SkPath& path, const SkMatrix* matrix, |
| 155 const SkPaint& paint) override; |
| 156 |
| 157 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 158 const SkPaint& paint) override; |
| 159 |
| 160 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 161 const SkPoint texCoords[4], SkXfermode* xmode, const SkPain
t& paint) override; |
| 162 |
| 163 void onDrawDrawable(SkDrawable*, const SkMatrix*) override; |
| 164 |
| 165 void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle
) override; |
| 166 |
| 167 void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeSt
yle) override; |
| 168 |
| 169 void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle
) override; |
| 170 |
| 171 void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) override; |
| 172 |
| 173 void willSave() override; |
| 174 |
| 175 void willRestore() override; |
| 176 |
| 177 private: |
| 178 void writef(const char* fmt, ...); |
| 179 |
| 180 void open(const char* name); |
| 181 |
| 182 void close(); |
| 183 |
| 184 void writeString(const char* name, const char* text); |
| 185 |
| 186 void writeString(const char* name, const void* text, size_t length); |
| 187 |
| 188 void writePoint(const char* name, const SkPoint& point); |
| 189 |
| 190 void writeRect(const char* name, const SkRect& rect); |
| 191 |
| 192 void writeRRect(const char* name, const SkRRect& rrect); |
| 193 |
| 194 void writePath(const char* name, const SkPath& path); |
| 195 |
| 196 void writeRegion(const char* name, const SkRegion& region); |
| 197 |
| 198 void writePaint(const SkPaint& paint); |
| 199 |
| 200 void writeRegionOp(const char* name, SkRegion::Op op); |
| 201 |
| 202 void writeEdgeStyle(const char* name, SkCanvas::ClipEdgeStyle edgeStyle); |
| 203 |
| 204 void writePointMode(const char* name, SkCanvas::PointMode mode); |
| 205 |
| 206 void writeMatrix(const char* name, const SkMatrix& matrix); |
| 207 |
| 208 void updateMatrix(); |
| 209 |
| 210 SkWStream& fOut; |
| 211 SkMatrix fLastMatrix; |
| 212 bool fFirstCommand; |
| 213 |
| 214 typedef SkCanvas INHERITED; |
| 215 }; |
| 216 |
| 217 #endif |
OLD | NEW |