Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(758)

Side by Side Diff: tools/json/SkJSONRenderer.cpp

Issue 1644903003: added support for more features in JSON (blurs, dashing, different path fill types, etc.) (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/json/SkJSONCanvas.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkJSONRenderer.h" 8 #include "SkJSONRenderer.h"
9
10 #include "SkBlurMaskFilter.h"
11 #include "SkDashPathEffect.h"
9 #include "SkJSONCanvas.h" 12 #include "SkJSONCanvas.h"
10 #include "SkJSONCPP.h" 13 #include "SkJSONCPP.h"
11 #include "SkPath.h" 14 #include "SkPath.h"
12 15
13 namespace SkJSONRenderer { 16 namespace SkJSONRenderer {
14 17
15 class Renderer { 18 class Renderer {
16 public: 19 public:
17 void getPaint(Json::Value& command, SkPaint* paint); 20 void getPaint(Json::Value& command, SkPaint* paint);
18 21
19 void getRect(Json::Value& command, const char* name, SkRect* rect); 22 void getRect(Json::Value& command, const char* name, SkRect* rect);
20 23
21 void getRRect(Json::Value& command, const char* name, SkRRect* rrect); 24 void getRRect(Json::Value& command, const char* name, SkRRect* rrect);
22 25
26 void getPath(Json::Value& command, SkPath* path);
27
28 SkRegion::Op getRegionOp(Json::Value& command);
29
23 void processCommand(Json::Value& command, SkCanvas* target); 30 void processCommand(Json::Value& command, SkCanvas* target);
24 31
25 void processMatrix(Json::Value& command, SkCanvas* target); 32 void processMatrix(Json::Value& command, SkCanvas* target);
26 33
27 void processSave(Json::Value& command, SkCanvas* target); 34 void processSave(Json::Value& command, SkCanvas* target);
28 35
29 void processRestore(Json::Value& command, SkCanvas* target); 36 void processRestore(Json::Value& command, SkCanvas* target);
30 37
31 void processPaint(Json::Value& command, SkCanvas* target); 38 void processPaint(Json::Value& command, SkCanvas* target);
32 39
33 void processRect(Json::Value& command, SkCanvas* target); 40 void processRect(Json::Value& command, SkCanvas* target);
34 41
35 void processRRect(Json::Value& command, SkCanvas* target); 42 void processRRect(Json::Value& command, SkCanvas* target);
36 43
37 void processOval(Json::Value& command, SkCanvas* target); 44 void processOval(Json::Value& command, SkCanvas* target);
38 45
39 void processPath(Json::Value& command, SkCanvas* target); 46 void processPath(Json::Value& command, SkCanvas* target);
40 47
41 void processText(Json::Value& command, SkCanvas* target); 48 void processText(Json::Value& command, SkCanvas* target);
42 49
50 void processPosText(Json::Value& command, SkCanvas* target);
51
43 void processPoints(Json::Value& command, SkCanvas* target); 52 void processPoints(Json::Value& command, SkCanvas* target);
44 53
45 void processClipRect(Json::Value& command, SkCanvas* target); 54 void processClipRect(Json::Value& command, SkCanvas* target);
55
56 void processClipRRect(Json::Value& command, SkCanvas* target);
57
58 void processClipPath(Json::Value& command, SkCanvas* target);
46 }; 59 };
47 60
48 void Renderer::processCommand(Json::Value& command, SkCanvas* target) { 61 void Renderer::processCommand(Json::Value& command, SkCanvas* target) {
49 const char* name = command[SKJSONCANVAS_COMMAND].asCString(); 62 const char* name = command[SKJSONCANVAS_COMMAND].asCString();
50 // TODO speed this up with a hash 63 // TODO speed this up with a hash
51 if (!strcmp(name, SKJSONCANVAS_COMMAND_MATRIX)) { 64 if (!strcmp(name, SKJSONCANVAS_COMMAND_MATRIX)) {
52 this->processMatrix(command, target); 65 this->processMatrix(command, target);
53 } 66 }
54 else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVE)) { 67 else if (!strcmp(name, SKJSONCANVAS_COMMAND_SAVE)) {
55 this->processSave(command, target); 68 this->processSave(command, target);
(...skipping 12 matching lines...) Expand all
68 } 81 }
69 else if (!strcmp(name, SKJSONCANVAS_COMMAND_OVAL)) { 82 else if (!strcmp(name, SKJSONCANVAS_COMMAND_OVAL)) {
70 this->processOval(command, target); 83 this->processOval(command, target);
71 } 84 }
72 else if (!strcmp(name, SKJSONCANVAS_COMMAND_PATH)) { 85 else if (!strcmp(name, SKJSONCANVAS_COMMAND_PATH)) {
73 this->processPath(command, target); 86 this->processPath(command, target);
74 } 87 }
75 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) { 88 else if (!strcmp(name, SKJSONCANVAS_COMMAND_TEXT)) {
76 this->processText(command, target); 89 this->processText(command, target);
77 } 90 }
91 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POSTEXT)) {
92 this->processPosText(command, target);
93 }
78 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) { 94 else if (!strcmp(name, SKJSONCANVAS_COMMAND_POINTS)) {
79 this->processPoints(command, target); 95 this->processPoints(command, target);
80 } 96 }
81 else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRECT)) { 97 else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRECT)) {
82 this->processClipRect(command, target); 98 this->processClipRect(command, target);
83 } 99 }
100 else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPRRECT)) {
101 this->processClipRRect(command, target);
102 }
103 else if (!strcmp(name, SKJSONCANVAS_COMMAND_CLIPPATH)) {
104 this->processClipPath(command, target);
105 }
84 else { 106 else {
85 SkDebugf("unsupported JSON command: %s\n", name); 107 SkDebugf("unsupported JSON command: %s\n", name);
86 } 108 }
87 } 109 }
88 110
89 void Renderer::getPaint(Json::Value& command, SkPaint* result) { 111 void Renderer::getPaint(Json::Value& command, SkPaint* result) {
90 Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT]; 112 Json::Value jsonPaint = command[SKJSONCANVAS_ATTRIBUTE_PAINT];
91 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_COLOR)) { 113 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_COLOR)) {
92 Json::Value color = jsonPaint[SKJSONCANVAS_ATTRIBUTE_COLOR]; 114 Json::Value color = jsonPaint[SKJSONCANVAS_ATTRIBUTE_COLOR];
93 result->setColor(SkColorSetARGB(color[0].asInt(), color[1].asInt(), colo r[2].asInt(), 115 result->setColor(SkColorSetARGB(color[0].asInt(), color[1].asInt(), colo r[2].asInt(),
(...skipping 11 matching lines...) Expand all
105 result->setStyle(SkPaint::kStrokeAndFill_Style); 127 result->setStyle(SkPaint::kStrokeAndFill_Style);
106 } 128 }
107 } 129 }
108 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH)) { 130 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH)) {
109 float strokeWidth = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH].asFloa t(); 131 float strokeWidth = jsonPaint[SKJSONCANVAS_ATTRIBUTE_STROKEWIDTH].asFloa t();
110 result->setStrokeWidth(strokeWidth); 132 result->setStrokeWidth(strokeWidth);
111 } 133 }
112 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_ANTIALIAS)) { 134 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_ANTIALIAS)) {
113 result->setAntiAlias(jsonPaint[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool( )); 135 result->setAntiAlias(jsonPaint[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool( ));
114 } 136 }
137 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_BLUR)) {
138 Json::Value blur = jsonPaint[SKJSONCANVAS_ATTRIBUTE_BLUR];
139 SkScalar sigma = blur[SKJSONCANVAS_ATTRIBUTE_SIGMA].asFloat();
140 SkBlurStyle style;
141 const char* jsonStyle = blur[SKJSONCANVAS_ATTRIBUTE_STYLE].asCString();
142 if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_NORMAL)) {
143 style = SkBlurStyle::kNormal_SkBlurStyle;
144 }
145 else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_SOLID)) {
146 style = SkBlurStyle::kSolid_SkBlurStyle;
147 }
148 else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_OUTER)) {
149 style = SkBlurStyle::kOuter_SkBlurStyle;
150 }
151 else if (!strcmp(jsonStyle, SKJSONCANVAS_BLURSTYLE_INNER)) {
152 style = SkBlurStyle::kInner_SkBlurStyle;
153 }
154 else {
155 SkASSERT(false);
156 style = SkBlurStyle::kNormal_SkBlurStyle;
157 }
158 SkBlurMaskFilter::BlurFlags flags;
159 const char* jsonQuality = blur[SKJSONCANVAS_ATTRIBUTE_QUALITY].asCString ();
160 if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_LOW)) {
161 flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag;
162 }
163 else if (!strcmp(jsonQuality, SKJSONCANVAS_BLURQUALITY_HIGH)) {
164 flags = SkBlurMaskFilter::BlurFlags::kHighQuality_BlurFlag;
165 }
166 else {
167 SkASSERT(false);
168 flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag;
169 }
170 result->setMaskFilter(SkBlurMaskFilter::Create(style, sigma, flags));
171 }
172 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_DASHING)) {
173 Json::Value dash = jsonPaint[SKJSONCANVAS_ATTRIBUTE_DASHING];
174 Json::Value jsonIntervals = dash[SKJSONCANVAS_ATTRIBUTE_INTERVALS];
175 Json::ArrayIndex count = jsonIntervals.size();
176 SkScalar* intervals = (SkScalar*) sk_malloc_throw(count * sizeof(SkScala r));
177 for (Json::ArrayIndex i = 0; i < count; i++) {
178 intervals[i] = jsonIntervals[i].asFloat();
179 }
180 SkScalar phase = dash[SKJSONCANVAS_ATTRIBUTE_PHASE].asFloat();
181 result->setPathEffect(SkDashPathEffect::Create(intervals, count, phase)) ;
182 free(intervals);
183 }
184 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTALIGN)) {
185 SkPaint::Align textAlign;
186 const char* jsonAlign = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTALIGN].asCS tring();
187 if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_LEFT)) {
188 textAlign = SkPaint::kLeft_Align;
189 }
190 else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_CENTER)) {
191 textAlign = SkPaint::kCenter_Align;
192 }
193 else if (!strcmp(jsonAlign, SKJSONCANVAS_ALIGN_RIGHT)) {
194 textAlign = SkPaint::kRight_Align;
195 }
196 else {
197 SkASSERT(false);
198 textAlign = SkPaint::kLeft_Align;
199 }
200 }
201 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSIZE)) {
202 float textSize = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSIZE].asFloat();
203 result->setTextSize(textSize);
204 }
205 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX)) {
206 float textScaleX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSCALEX].asFloat( );
207 result->setTextScaleX(textScaleX);
208 }
209 if (jsonPaint.isMember(SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX)) {
210 float textSkewX = jsonPaint[SKJSONCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat();
211 result->setTextSkewX(textSkewX);
212 }
115 } 213 }
116 214
117 void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) { 215 void Renderer::getRect(Json::Value& command, const char* name, SkRect* result) {
118 Json::Value rect = command[name]; 216 Json::Value rect = command[name];
119 result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3] .asFloat()); 217 result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3] .asFloat());
120 } 218 }
121 219
122 void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result) { 220 void Renderer::getRRect(Json::Value& command, const char* name, SkRRect* result) {
123 Json::Value rrect = command[name]; 221 Json::Value rrect = command[name];
124 SkVector radii[4] = { 222 SkVector radii[4] = {
125 { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, 223 { rrect[1][0].asFloat(), rrect[1][1].asFloat() },
126 { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, 224 { rrect[2][0].asFloat(), rrect[2][1].asFloat() },
127 { rrect[3][0].asFloat(), rrect[3][1].asFloat() }, 225 { rrect[3][0].asFloat(), rrect[3][1].asFloat() },
128 { rrect[4][0].asFloat(), rrect[4][1].asFloat() } 226 { rrect[4][0].asFloat(), rrect[4][1].asFloat() }
129 }; 227 };
130 result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asF loat(), 228 result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asF loat(),
131 rrect[0][2].asFloat(), rrect[0][3].asF loat()), 229 rrect[0][2].asFloat(), rrect[0][3].asF loat()),
132 radii); 230 radii);
133 } 231 }
134 232
233 void Renderer::getPath(Json::Value& command, SkPath* result) {
234 Json::Value path = command[SKJSONCANVAS_ATTRIBUTE_PATH];
235 const char* fillType = path[SKJSONCANVAS_ATTRIBUTE_FILLTYPE].asCString();
236 if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_WINDING)) {
237 result->setFillType(SkPath::kWinding_FillType);
238 }
239 else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_EVENODD)) {
240 result->setFillType(SkPath::kEvenOdd_FillType);
241 }
242 else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEWINDING)) {
243 result->setFillType(SkPath::kInverseWinding_FillType);
244 }
245 else if (!strcmp(fillType, SKJSONCANVAS_FILLTYPE_INVERSEEVENODD)) {
246 result->setFillType(SkPath::kInverseEvenOdd_FillType);
247 }
248 Json::Value verbs = path[SKJSONCANVAS_ATTRIBUTE_VERBS];
249 for (Json::ArrayIndex i = 0; i < verbs.size(); i++) {
250 Json::Value verb = verbs[i];
251 if (verb.isString()) {
252 SkASSERT(!strcmp(verb.asCString(), SKJSONCANVAS_VERB_CLOSE));
253 result->close();
254 }
255 else {
256 if (verb.isMember(SKJSONCANVAS_VERB_MOVE)) {
257 Json::Value move = verb[SKJSONCANVAS_VERB_MOVE];
258 result->moveTo(move[0].asFloat(), move[1].asFloat());
259 }
260 else if (verb.isMember(SKJSONCANVAS_VERB_LINE)) {
261 Json::Value line = verb[SKJSONCANVAS_VERB_LINE];
262 result->lineTo(line[0].asFloat(), line[1].asFloat());
263 }
264 else if (verb.isMember(SKJSONCANVAS_VERB_QUAD)) {
265 Json::Value quad = verb[SKJSONCANVAS_VERB_QUAD];
266 result->quadTo(quad[0][0].asFloat(), quad[0][1].asFloat(),
267 quad[1][0].asFloat(), quad[1][1].asFloat());
268 }
269 else if (verb.isMember(SKJSONCANVAS_VERB_CUBIC)) {
270 Json::Value cubic = verb[SKJSONCANVAS_VERB_CUBIC];
271 result->cubicTo(cubic[0][0].asFloat(), cubic[0][1].asFloat(),
272 cubic[1][0].asFloat(), cubic[1][1].asFloat(),
273 cubic[2][0].asFloat(), cubic[2][1].asFloat());
274 }
275 else if (verb.isMember(SKJSONCANVAS_VERB_CONIC)) {
276 Json::Value conic = verb[SKJSONCANVAS_VERB_CONIC];
277 result->conicTo(conic[0][0].asFloat(), conic[0][1].asFloat(),
278 conic[1][0].asFloat(), conic[1][1].asFloat(),
279 conic[2].asFloat());
280 }
281 else {
282 SkASSERT(false);
283 }
284 }
285 }
286 }
287
288 SkRegion::Op Renderer::getRegionOp(Json::Value& command) {
289 const char* op = command[SKJSONCANVAS_ATTRIBUTE_REGIONOP].asCString();
290 if (!strcmp(op, SKJSONCANVAS_REGIONOP_DIFFERENCE)) {
291 return SkRegion::kDifference_Op;
292 }
293 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_INTERSECT)) {
294 return SkRegion::kIntersect_Op;
295 }
296 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_UNION)) {
297 return SkRegion::kUnion_Op;
298 }
299 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_XOR)) {
300 return SkRegion::kXOR_Op;
301 }
302 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REVERSE_DIFFERENCE)) {
303 return SkRegion::kReverseDifference_Op;
304 }
305 else if (!strcmp(op, SKJSONCANVAS_REGIONOP_REPLACE)) {
306 return SkRegion::kReplace_Op;
307 }
308 SkASSERT(false);
309 return SkRegion::kIntersect_Op;
310 }
311
135 void Renderer::processMatrix(Json::Value& command, SkCanvas* target) { 312 void Renderer::processMatrix(Json::Value& command, SkCanvas* target) {
136 Json::Value jsonMatrix = command[SKJSONCANVAS_ATTRIBUTE_MATRIX]; 313 Json::Value jsonMatrix = command[SKJSONCANVAS_ATTRIBUTE_MATRIX];
137 SkMatrix matrix; 314 SkMatrix matrix;
138 SkScalar values[] = { 315 SkScalar values[] = {
139 jsonMatrix[0][0].asFloat(), jsonMatrix[0][1].asFloat(), jsonMatrix[0][2] .asFloat(), 316 jsonMatrix[0][0].asFloat(), jsonMatrix[0][1].asFloat(), jsonMatrix[0][2] .asFloat(),
140 jsonMatrix[1][0].asFloat(), jsonMatrix[1][1].asFloat(), jsonMatrix[1][2] .asFloat(), 317 jsonMatrix[1][0].asFloat(), jsonMatrix[1][1].asFloat(), jsonMatrix[1][2] .asFloat(),
141 jsonMatrix[2][0].asFloat(), jsonMatrix[2][1].asFloat(), jsonMatrix[2][2] .asFloat() 318 jsonMatrix[2][0].asFloat(), jsonMatrix[2][1].asFloat(), jsonMatrix[2][2] .asFloat()
142 }; 319 };
143 matrix.set9(values); 320 matrix.set9(values);
144 target->setMatrix(matrix); 321 target->setMatrix(matrix);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 SkRect rect; 355 SkRect rect;
179 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); 356 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect);
180 SkPaint paint; 357 SkPaint paint;
181 this->getPaint(command, &paint); 358 this->getPaint(command, &paint);
182 target->drawOval(rect, paint); 359 target->drawOval(rect, paint);
183 } 360 }
184 361
185 void Renderer::processPath(Json::Value& command, SkCanvas* target) { 362 void Renderer::processPath(Json::Value& command, SkCanvas* target) {
186 Json::Value jsonPath = command[SKJSONCANVAS_ATTRIBUTE_PATH]; 363 Json::Value jsonPath = command[SKJSONCANVAS_ATTRIBUTE_PATH];
187 SkPath path; 364 SkPath path;
188 for (Json::ArrayIndex i = 0; i < jsonPath.size(); i++) { 365 this->getPath(command, &path);
189 Json::Value verb = jsonPath[i];
190 if (verb.isString()) {
191 SkASSERT(!strcmp(verb.asCString(), SKJSONCANVAS_VERB_CLOSE));
192 path.close();
193 }
194 else {
195 if (verb.isMember(SKJSONCANVAS_VERB_MOVE)) {
196 Json::Value move = verb[SKJSONCANVAS_VERB_MOVE];
197 path.moveTo(move[0].asFloat(), move[1].asFloat());
198 }
199 else if (verb.isMember(SKJSONCANVAS_VERB_LINE)) {
200 Json::Value line = verb[SKJSONCANVAS_VERB_LINE];
201 path.lineTo(line[0].asFloat(), line[1].asFloat());
202 }
203 else if (verb.isMember(SKJSONCANVAS_VERB_QUAD)) {
204 Json::Value quad = verb[SKJSONCANVAS_VERB_QUAD];
205 path.quadTo(quad[0][0].asFloat(), quad[0][1].asFloat(),
206 quad[1][0].asFloat(), quad[1][1].asFloat());
207 }
208 else if (verb.isMember(SKJSONCANVAS_VERB_CUBIC)) {
209 Json::Value cubic = verb[SKJSONCANVAS_VERB_CUBIC];
210 path.cubicTo(cubic[0][0].asFloat(), cubic[0][1].asFloat(),
211 cubic[1][0].asFloat(), cubic[1][1].asFloat(),
212 cubic[2][0].asFloat(), cubic[2][1].asFloat());
213 }
214 else if (verb.isMember(SKJSONCANVAS_VERB_CONIC)) {
215 Json::Value conic = verb[SKJSONCANVAS_VERB_CONIC];
216 path.conicTo(conic[0][0].asFloat(), conic[0][1].asFloat(),
217 conic[1][0].asFloat(), conic[1][1].asFloat(),
218 conic[2].asFloat());
219 }
220 else {
221 SkASSERT(false);
222 }
223 }
224 }
225 SkPaint paint; 366 SkPaint paint;
226 this->getPaint(command, &paint); 367 this->getPaint(command, &paint);
227 target->drawPath(path, paint); 368 target->drawPath(path, paint);
228 } 369 }
229 370
230 void Renderer::processText(Json::Value& command, SkCanvas* target) { 371 void Renderer::processText(Json::Value& command, SkCanvas* target) {
231 const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString(); 372 const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString();
232 SkPaint paint; 373 SkPaint paint;
233 this->getPaint(command, &paint); 374 this->getPaint(command, &paint);
234 Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS]; 375 Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS];
235 target->drawText(text, strlen(text), coords[0].asFloat(), coords[1].asFloat( ), paint); 376 target->drawText(text, strlen(text), coords[0].asFloat(), coords[1].asFloat( ), paint);
236 } 377 }
237 378
379 void Renderer::processPosText(Json::Value& command, SkCanvas* target) {
380 const char* text = command[SKJSONCANVAS_ATTRIBUTE_TEXT].asCString();
381 SkPaint paint;
382 this->getPaint(command, &paint);
383 Json::Value coords = command[SKJSONCANVAS_ATTRIBUTE_COORDS];
384 int count = (int) coords.size();
385 SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
386 for (int i = 0; i < count; i++) {
387 points[i] = SkPoint::Make(coords[i][0].asFloat(), coords[i][1].asFloat() );
388 }
389 target->drawPosText(text, strlen(text), points, paint);
390 free(points);
391 }
392
238 void Renderer::processPoints(Json::Value& command, SkCanvas* target) { 393 void Renderer::processPoints(Json::Value& command, SkCanvas* target) {
239 SkCanvas::PointMode mode; 394 SkCanvas::PointMode mode;
240 const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString(); 395 const char* jsonMode = command[SKJSONCANVAS_ATTRIBUTE_MODE].asCString();
241 if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POINTS)) { 396 if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POINTS)) {
242 mode = SkCanvas::kPoints_PointMode; 397 mode = SkCanvas::kPoints_PointMode;
243 } 398 }
244 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_LINES)) { 399 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_LINES)) {
245 mode = SkCanvas::kLines_PointMode; 400 mode = SkCanvas::kLines_PointMode;
246 } 401 }
247 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POLYGON)) { 402 else if (!strcmp(jsonMode, SKJSONCANVAS_POINTMODE_POLYGON)) {
(...skipping 11 matching lines...) Expand all
259 } 414 }
260 SkPaint paint; 415 SkPaint paint;
261 this->getPaint(command, &paint); 416 this->getPaint(command, &paint);
262 target->drawPoints(mode, count, points, paint); 417 target->drawPoints(mode, count, points, paint);
263 free(points); 418 free(points);
264 } 419 }
265 420
266 void Renderer::processClipRect(Json::Value& command, SkCanvas* target) { 421 void Renderer::processClipRect(Json::Value& command, SkCanvas* target) {
267 SkRect rect; 422 SkRect rect;
268 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect); 423 this->getRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rect);
269 target->clipRect(rect); 424 target->clipRect(rect, this->getRegionOp(command),
425 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
426 }
427
428 void Renderer::processClipRRect(Json::Value& command, SkCanvas* target) {
429 SkRRect rrect;
430 this->getRRect(command, SKJSONCANVAS_ATTRIBUTE_COORDS, &rrect);
431 target->clipRRect(rrect, this->getRegionOp(command),
432 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
433 }
434
435 void Renderer::processClipPath(Json::Value& command, SkCanvas* target) {
436 SkPath path;
437 this->getPath(command, &path);
438 target->clipPath(path, this->getRegionOp(command),
439 command[SKJSONCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
270 } 440 }
271 441
272 void render(const char* json, SkCanvas* target) { 442 void render(const char* json, SkCanvas* target) {
273 Renderer renderer; 443 Renderer renderer;
274 Json::Reader reader; 444 Json::Reader reader;
275 Json::Value root; 445 Json::Value root;
276 if (reader.parse(std::string(json), root)) { 446 if (reader.parse(std::string(json), root)) {
277 SkASSERT(root[SKJSONCANVAS_VERSION].asInt() == 1); 447 SkASSERT(root[SKJSONCANVAS_VERSION].asInt() == 1);
278 Json::Value commands = root[SKJSONCANVAS_COMMANDS]; 448 Json::Value commands = root[SKJSONCANVAS_COMMANDS];
279 for (Json::ArrayIndex i = 0; i < commands.size(); i++) { 449 for (Json::ArrayIndex i = 0; i < commands.size(); i++) {
280 renderer.processCommand(commands[i], target); 450 renderer.processCommand(commands[i], target);
281 } 451 }
282 } 452 }
283 else { 453 else {
284 SkDebugf(json); 454 SkDebugf(json);
285 SkFAIL("json parse failure"); 455 SkFAIL("json parse failure");
286 } 456 }
287 } 457 }
288 458
289 } // namespace 459 } // namespace
OLDNEW
« no previous file with comments | « tools/json/SkJSONCanvas.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698