Index: tools/json/SkJSONRenderer.cpp |
diff --git a/tools/json/SkJSONRenderer.cpp b/tools/json/SkJSONRenderer.cpp |
index f9bb6ee40e26af65b10bcafb7684d9dea107be00..e84a1ed69200be82397c266c9900a66dfef57e50 100644 |
--- a/tools/json/SkJSONRenderer.cpp |
+++ b/tools/json/SkJSONRenderer.cpp |
@@ -6,6 +6,9 @@ |
*/ |
#include "SkJSONRenderer.h" |
+ |
+#include "SkBlurMaskFilter.h" |
+#include "SkDashPathEffect.h" |
#include "SkJSONCanvas.h" |
#include "SkJSONCPP.h" |
#include "SkPath.h" |
@@ -20,6 +23,10 @@ public: |
void getRRect(Json::Value& command, const char* name, SkRRect* rrect); |
+ void getPath(Json::Value& command, SkPath* path); |
+ |
+ SkRegion::Op getRegionOp(Json::Value& command); |
+ |
void processCommand(Json::Value& command, SkCanvas* target); |
void processMatrix(Json::Value& command, SkCanvas* target); |
@@ -40,9 +47,15 @@ public: |
void processText(Json::Value& command, SkCanvas* target); |
+ void processPosText(Json::Value& command, SkCanvas* target); |
+ |
void processPoints(Json::Value& command, SkCanvas* target); |
void processClipRect(Json::Value& command, SkCanvas* target); |
+ |
+ void processClipRRect(Json::Value& command, SkCanvas* target); |
+ |
+ void processClipPath(Json::Value& command, SkCanvas* target); |
}; |
void Renderer::processCommand(Json::Value& command, SkCanvas* target) { |
@@ -75,12 +88,21 @@ void Renderer::processCommand(Json::Value& command, SkCanvas* target) { |
else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) { |
this->processText(command, target); |
} |
+ else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) { |
+ this->processPosText(command, target); |
+ } |
else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) { |
this->processPoints(command, target); |
} |
else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRECT)) { |
this->processClipRect(command, target); |
} |
+ else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRRECT)) { |
+ this->processClipRRect(command, target); |
+ } |
+ else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPPATH)) { |
+ this->processClipPath(command, target); |
+ } |
else { |
SkDebugf("unsupported JSON command: %s\n", name); |
} |
@@ -112,6 +134,82 @@ void Renderer::getPaint(Json::Value& command, SkPaint* result) { |
if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_ANTIALIAS)) { |
result->setAntiAlias(jsonPaint[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
} |
+ if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_BLUR)) { |
+ Json::Value blur = jsonPaint[SKJSONCANVAS_ATTRIBUTE_BLUR]; |
+ SkScalar sigma = blur[SKJSONCANVAS_ATTRIBUTE_SIGMA].asFloat(); |
+ SkBlurStyle style; |
+ const char* jsonStyle = blur[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString(); |
+ if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_NORMAL)) { |
+ style = SkBlurStyle::kNormal_SkBlurStyle; |
+ } |
+ else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_SOLID)) { |
+ style = SkBlurStyle::kSolid_SkBlurStyle; |
+ } |
+ else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_OUTER)) { |
+ style = SkBlurStyle::kOuter_SkBlurStyle; |
+ } |
+ else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_INNER)) { |
+ style = SkBlurStyle::kInner_SkBlurStyle; |
+ } |
+ else { |
+ SkASSERT(false); |
+ style = SkBlurStyle::kNormal_SkBlurStyle; |
+ } |
+ SkBlurMaskFilter::BlurFlags flags; |
+ const char* jsonQuality = blur[SKJSONCANVAS_ATTRIBUTE_QUALITY].asCString(); |
+ if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_LOW)) { |
+ flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag; |
+ } |
+ else if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_HIGH)) { |
+ flags = SkBlurMaskFilter::BlurFlags::kHighQuality_BlurFlag; |
+ } |
+ else { |
+ SkASSERT(false); |
+ flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag; |
+ } |
+ result->setMaskFilter(SkBlurMaskFilter::Create(style, sigma, flags)); |
+ } |
+ if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_DASHING)) { |
+ Json::Value dash = jsonPaint[SKJSONCANVAS_ATTRIBUTE_DASHING]; |
+ Json::Value jsonIntervals = dash[SKJSONCANVAS_ATTRIBUTE_INTERVALS]; |
+ Json::ArrayIndex count = jsonIntervals.size(); |
+ SkScalar* intervals = (SkScalar*) sk_malloc_throw(count * sizeof(SkScalar)); |
+ for (Json::ArrayIndex i = 0; i < count; i++) { |
+ intervals[i] = jsonIntervals[i].asFloat(); |
+ } |
+ SkScalar phase = dash[SKJSONCANVAS_ATTRIBUTE_PHASE].asFloat(); |
+ result->setPathEffect(SkDashPathEffect::Create(intervals, count, phase)); |
+ free(intervals); |
+ } |
+ if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTALIGN)) { |
+ SkPaint::Align textAlign; |
+ const char* jsonAlign = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN].asCString(); |
+ if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_LEFT)) { |
+ textAlign = SkPaint::kLeft_Align; |
+ } |
+ else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_CENTER)) { |
+ textAlign = SkPaint::kCenter_Align; |
+ } |
+ else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_RIGHT)) { |
+ textAlign = SkPaint::kRight_Align; |
+ } |
+ else { |
+ SkASSERT(false); |
+ textAlign = SkPaint::kLeft_Align; |
+ } |
+ } |
+ if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSIZE)) { |
+ float textSize = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSIZE].asFloat(); |
+ result->setTextSize(textSize); |
+ } |
+ if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX)) { |
+ float textScaleX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX].asFloat(); |
+ result->setTextScaleX(textScaleX); |
+ } |
+ if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) { |
+ float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat(); |
+ result->setTextSkewX(textSkewX); |
+ } |
} |
void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) { |
@@ -132,6 +230,85 @@ void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result) |
radii); |
} |
+void Renderer::getPath(Json::Value& command, SkPath* result) { |
+ Json::Value path = command[SKJSONCANVAS_ATTRIBUTE_PATH]; |
+ const char* fillType = path[SKJSONCANVAS_ATTRIBUTE_FILLTYPE].asCString(); |
+ if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_WINDING)) { |
+ result->setFillType(SkPath::kWinding_FillType); |
+ } |
+ else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_EVENODD)) { |
+ result->setFillType(SkPath::kEvenOdd_FillType); |
+ } |
+ else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEWINDING)) { |
+ result->setFillType(SkPath::kInverseWinding_FillType); |
+ } |
+ else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEEVENODD)) { |
+ result->setFillType(SkPath::kInverseEvenOdd_FillType); |
+ } |
+ Json::Value verbs = path[SKJSONCANVAS_ATTRIBUTE_VERBS]; |
+ for (Json::ArrayIndex i = 0; i < verbs.size(); i++) { |
+ Json::Value verb = verbs[i]; |
+ if (verb.isString()) { |
+ SkASSERT(!strcmp(verb.asCString(), SKJSONCANVAS_VERB_CLOSE)); |
+ result->close(); |
+ } |
+ else { |
+ if (verb.isMember(SKJSONCANVAS_VERB_MOVE)) { |
+ Json::Value move = verb[SKJSONCANVAS_VERB_MOVE]; |
+ result->moveTo(move[0].asFloat(), move[1].asFloat()); |
+ } |
+ else if (verb.isMember(SKJSONCANVAS_VERB_LINE)) { |
+ Json::Value line = verb[SKJSONCANVAS_VERB_LINE]; |
+ result->lineTo(line[0].asFloat(), line[1].asFloat()); |
+ } |
+ else if (verb.isMember(SKJSONCANVAS_VERB_QUAD)) { |
+ Json::Value quad = verb[SKJSONCANVAS_VERB_QUAD]; |
+ result->quadTo(quad[0][0].asFloat(), quad[0][1].asFloat(), |
+ quad[1][0].asFloat(), quad[1][1].asFloat()); |
+ } |
+ else if (verb.isMember(SKJSONCANVAS_VERB_CUBIC)) { |
+ Json::Value cubic = verb[SKJSONCANVAS_VERB_CUBIC]; |
+ result->cubicTo(cubic[0][0].asFloat(), cubic[0][1].asFloat(), |
+ cubic[1][0].asFloat(), cubic[1][1].asFloat(), |
+ cubic[2][0].asFloat(), cubic[2][1].asFloat()); |
+ } |
+ else if (verb.isMember(SKJSONCANVAS_VERB_CONIC)) { |
+ Json::Value conic = verb[SKJSONCANVAS_VERB_CONIC]; |
+ result->conicTo(conic[0][0].asFloat(), conic[0][1].asFloat(), |
+ conic[1][0].asFloat(), conic[1][1].asFloat(), |
+ conic[2].asFloat()); |
+ } |
+ else { |
+ SkASSERT(false); |
+ } |
+ } |
+ } |
+} |
+ |
+SkRegion::Op Renderer::getRegionOp(Json::Value& command) { |
+ const char* op = command[SKJSONCANVAS_ATTRIBUTE_REGIONOP].asCString(); |
+ if (!strcmp(op, SKJSONCANVAS_REGIONOP_DIFFERENCE)) { |
+ return SkRegion::kDifference_Op; |
+ } |
+ else if (!strcmp(op, SKJSONCANVAS_REGIONOP_INTERSECT)) { |
+ return SkRegion::kIntersect_Op; |
+ } |
+ else if (!strcmp(op, SKJSONCANVAS_REGIONOP_UNION)) { |
+ return SkRegion::kUnion_Op; |
+ } |
+ else if (!strcmp(op, SKJSONCANVAS_REGIONOP_XOR)) { |
+ return SkRegion::kXOR_Op; |
+ } |
+ else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE)) { |
+ return SkRegion::kReverseDifference_Op; |
+ } |
+ else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REPLACE)) { |
+ return SkRegion::kReplace_Op; |
+ } |
+ SkASSERT(false); |
+ return SkRegion::kIntersect_Op; |
+} |
+ |
void Renderer::processMatrix(Json::Value& command, SkCanvas* target) { |
Json::Value jsonMatrix = command[SKJSONCANVAS_ATTRIBUTE_MATRIX]; |
SkMatrix matrix; |
@@ -185,43 +362,7 @@ void Renderer::processOval(Json::Value& command, SkCanvas* target) { |
void Renderer::processPath(Json::Value& command, SkCanvas* target) { |
Json::Value jsonPath = command[SKJSONCANVAS_ATTRIBUTE_PATH]; |
SkPath path; |
- for (Json::ArrayIndex i = 0; i < jsonPath.size(); i++) { |
- Json::Value verb = jsonPath[i]; |
- if (verb.isString()) { |
- SkASSERT(!strcmp(verb.asCString(), SKJSONCANVAS_VERB_CLOSE)); |
- path.close(); |
- } |
- else { |
- if (verb.isMember(SKJSONCANVAS_VERB_MOVE)) { |
- Json::Value move = verb[SKJSONCANVAS_VERB_MOVE]; |
- path.moveTo(move[0].asFloat(), move[1].asFloat()); |
- } |
- else if (verb.isMember(SKJSONCANVAS_VERB_LINE)) { |
- Json::Value line = verb[SKJSONCANVAS_VERB_LINE]; |
- path.lineTo(line[0].asFloat(), line[1].asFloat()); |
- } |
- else if (verb.isMember(SKJSONCANVAS_VERB_QUAD)) { |
- Json::Value quad = verb[SKJSONCANVAS_VERB_QUAD]; |
- path.quadTo(quad[0][0].asFloat(), quad[0][1].asFloat(), |
- quad[1][0].asFloat(), quad[1][1].asFloat()); |
- } |
- else if (verb.isMember(SKJSONCANVAS_VERB_CUBIC)) { |
- Json::Value cubic = verb[SKJSONCANVAS_VERB_CUBIC]; |
- path.cubicTo(cubic[0][0].asFloat(), cubic[0][1].asFloat(), |
- cubic[1][0].asFloat(), cubic[1][1].asFloat(), |
- cubic[2][0].asFloat(), cubic[2][1].asFloat()); |
- } |
- else if (verb.isMember(SKJSONCANVAS_VERB_CONIC)) { |
- Json::Value conic = verb[SKJSONCANVAS_VERB_CONIC]; |
- path.conicTo(conic[0][0].asFloat(), conic[0][1].asFloat(), |
- conic[1][0].asFloat(), conic[1][1].asFloat(), |
- conic[2].asFloat()); |
- } |
- else { |
- SkASSERT(false); |
- } |
- } |
- } |
+ this->getPath(command, &path); |
SkPaint paint; |
this->getPaint(command, &paint); |
target->drawPath(path, paint); |
@@ -235,6 +376,20 @@ void Renderer::processText(Json::Value& command, SkCanvas* target) { |
target->drawText(text, strlen(text), coords[0].asFloat(), coords[1].asFloat(), paint); |
} |
+void Renderer::processPosText(Json::Value& command, SkCanvas* target) { |
+ const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString(); |
+ SkPaint paint; |
+ this->getPaint(command, &paint); |
+ Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; |
+ int count = (int) coords.size(); |
+ SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint)); |
+ for (int i = 0; i < count; i++) { |
+ points[i] = SkPoint::Make(coords[i][0].asFloat(), coords[i][1].asFloat()); |
+ } |
+ target->drawPosText(text, strlen(text), points, paint); |
+ free(points); |
+} |
+ |
void Renderer::processPoints(Json::Value& command, SkCanvas* target) { |
SkCanvas::PointMode mode; |
const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString(); |
@@ -266,7 +421,22 @@ void Renderer::processPoints(Json::Value& command, SkCanvas* target) { |
void Renderer::processClipRect(Json::Value& command, SkCanvas* target) { |
SkRect rect; |
this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); |
- target->clipRect(rect); |
+ target->clipRect(rect, this->getRegionOp(command), |
+ 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), |
+ 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), |
+ command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool()); |
} |
void render(const char* json, SkCanvas* target) { |