blob: a3738dbeeecd53be8cd1fe3a26984d86a6d5a5a0 [file] [log] [blame]
chudy@google.com902ebe52012-06-29 14:21:22 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkObjectParser.h"
humper@google.comf515ffc2013-01-07 15:48:19 +000010#include "SkData.h"
11#include "SkFontDescriptor.h"
robertphillips@google.com4991b8f2013-01-28 20:21:59 +000012#include "SkRRect.h"
13#include "SkShader.h"
14#include "SkStream.h"
15#include "SkStringUtils.h"
16#include "SkTypeface.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000017
18/* TODO(chudy): Replace all std::strings with char */
19
chudy@google.com97cee972012-08-07 20:41:37 +000020SkString* SkObjectParser::BitmapToString(const SkBitmap& bitmap) {
robertphillips@google.com195952f2012-10-23 12:13:35 +000021 SkString* mBitmap = new SkString("SkBitmap: ");
22 mBitmap->append("W: ");
23 mBitmap->appendS32(bitmap.width());
24 mBitmap->append(" H: ");
25 mBitmap->appendS32(bitmap.height());
26
27 const char* gConfigStrings[] = {
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +000028 "None", "A1", "A8", "Index8", "RGB565", "ARGB4444", "ARGB8888", "RLE8"
robertphillips@google.com195952f2012-10-23 12:13:35 +000029 };
30 SkASSERT(SkBitmap::kConfigCount == 8);
31
32 mBitmap->append(" Config: ");
33 mBitmap->append(gConfigStrings[bitmap.getConfig()]);
34
35 if (bitmap.isOpaque()) {
36 mBitmap->append(" opaque");
37 } else {
38 mBitmap->append(" not-opaque");
39 }
40
41 if (bitmap.isImmutable()) {
42 mBitmap->append(" immutable");
43 } else {
44 mBitmap->append(" not-immutable");
45 }
46
47 if (bitmap.isVolatile()) {
48 mBitmap->append(" volatile");
49 } else {
50 mBitmap->append(" not-volatile");
51 }
52
53 mBitmap->append(" genID: ");
54 mBitmap->appendS32(bitmap.getGenerationID());
55
chudy@google.com902ebe52012-06-29 14:21:22 +000056 return mBitmap;
57}
58
chudy@google.com97cee972012-08-07 20:41:37 +000059SkString* SkObjectParser::BoolToString(bool doAA) {
60 SkString* mBool = new SkString("Bool doAA: ");
chudy@google.com902ebe52012-06-29 14:21:22 +000061 if (doAA) {
chudy@google.com97cee972012-08-07 20:41:37 +000062 mBool->append("True");
chudy@google.com902ebe52012-06-29 14:21:22 +000063 } else {
chudy@google.com97cee972012-08-07 20:41:37 +000064 mBool->append("False");
chudy@google.com902ebe52012-06-29 14:21:22 +000065 }
chudy@google.com97cee972012-08-07 20:41:37 +000066 return mBool;
chudy@google.com902ebe52012-06-29 14:21:22 +000067}
68
chudy@google.com97cee972012-08-07 20:41:37 +000069SkString* SkObjectParser::CustomTextToString(const char* text) {
70 SkString* mText = new SkString(text);
71 return mText;
chudy@google.com902ebe52012-06-29 14:21:22 +000072}
73
chudy@google.com97cee972012-08-07 20:41:37 +000074SkString* SkObjectParser::IntToString(int x, const char* text) {
75 SkString* mInt = new SkString(text);
76 mInt->append(" ");
77 mInt->appendScalar(SkIntToScalar(x));
78 return mInt;
chudy@google.com902ebe52012-06-29 14:21:22 +000079}
80
chudy@google.com97cee972012-08-07 20:41:37 +000081SkString* SkObjectParser::IRectToString(const SkIRect& rect) {
82 SkString* mRect = new SkString("SkIRect: ");
83 mRect->append("L: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +000084 mRect->appendS32(rect.left());
chudy@google.com97cee972012-08-07 20:41:37 +000085 mRect->append(", T: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +000086 mRect->appendS32(rect.top());
chudy@google.com97cee972012-08-07 20:41:37 +000087 mRect->append(", R: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +000088 mRect->appendS32(rect.right());
chudy@google.com97cee972012-08-07 20:41:37 +000089 mRect->append(", B: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +000090 mRect->appendS32(rect.bottom());
chudy@google.com902ebe52012-06-29 14:21:22 +000091 return mRect;
92}
93
chudy@google.com97cee972012-08-07 20:41:37 +000094SkString* SkObjectParser::MatrixToString(const SkMatrix& matrix) {
robertphillips@google.com791f12e2013-02-14 13:53:53 +000095 SkString* str = new SkString("SkMatrix: ");
robertphillips@google.com37a2b392013-02-14 14:40:27 +000096#ifdef SK_DEVELOPER
robertphillips@google.com791f12e2013-02-14 13:53:53 +000097 matrix.toString(str);
robertphillips@google.com37a2b392013-02-14 14:40:27 +000098#endif
robertphillips@google.com791f12e2013-02-14 13:53:53 +000099 return str;
chudy@google.com97cee972012-08-07 20:41:37 +0000100}
chudy@google.com902ebe52012-06-29 14:21:22 +0000101
robertphillips@google.com735edb02013-01-10 18:54:11 +0000102SkString* SkObjectParser::PaintToString(const SkPaint& paint) {
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000103 SkString* str = new SkString;
robertphillips@google.com37a2b392013-02-14 14:40:27 +0000104#ifdef SK_DEVELOPER
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000105 paint.toString(str);
robertphillips@google.com37a2b392013-02-14 14:40:27 +0000106#endif
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000107 return str;
chudy@google.com97cee972012-08-07 20:41:37 +0000108}
109
110SkString* SkObjectParser::PathToString(const SkPath& path) {
robertphillips@google.com87201762012-10-18 13:30:18 +0000111 SkString* mPath = new SkString("Path (");
112
robertphillips@google.com51185fe2012-12-05 19:34:33 +0000113 static const char* gFillStrings[] = {
114 "Winding", "EvenOdd", "InverseWinding", "InverseEvenOdd"
115 };
116
117 mPath->append(gFillStrings[path.getFillType()]);
118 mPath->append(", ");
119
robertphillips@google.com87201762012-10-18 13:30:18 +0000120 static const char* gConvexityStrings[] = {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000121 "Unknown", "Convex", "Concave"
robertphillips@google.com87201762012-10-18 13:30:18 +0000122 };
123 SkASSERT(SkPath::kConcave_Convexity == 2);
124
125 mPath->append(gConvexityStrings[path.getConvexity()]);
126 mPath->append(", ");
127
robertphillips@google.com195952f2012-10-23 12:13:35 +0000128 if (path.isRect(NULL)) {
129 mPath->append("isRect, ");
130 } else {
131 mPath->append("isNotRect, ");
132 }
133
robertphillips@google.com87201762012-10-18 13:30:18 +0000134 mPath->appendS32(path.countVerbs());
135 mPath->append("V, ");
136 mPath->appendS32(path.countPoints());
137 mPath->append("P): ");
138
139 static const char* gVerbStrings[] = {
140 "Move", "Line", "Quad", "Cubic", "Close", "Done"
141 };
142 static const int gPtsPerVerb[] = { 1, 1, 2, 3, 0, 0 };
143 static const int gPtOffsetPerVerb[] = { 0, 1, 1, 1, 0, 0 };
144 SkASSERT(SkPath::kDone_Verb == 5);
145
146 SkPath::Iter iter(const_cast<SkPath&>(path), false);
147 SkPath::Verb verb;
148 SkPoint points[4];
149
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000150 for(verb = iter.next(points, false);
151 verb != SkPath::kDone_Verb;
robertphillips@google.com87201762012-10-18 13:30:18 +0000152 verb = iter.next(points, false)) {
153
154 mPath->append(gVerbStrings[verb]);
155 mPath->append(" ");
156
157 for (int i = 0; i < gPtsPerVerb[verb]; ++i) {
158 mPath->append("(");
159 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fX);
160 mPath->append(", ");
161 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fY);
162 mPath->append(") ");
163 }
chudy@google.com97cee972012-08-07 20:41:37 +0000164 }
robertphillips@google.com87201762012-10-18 13:30:18 +0000165
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000166 SkString* boundStr = SkObjectParser::RectToString(path.getBounds(), " Bound: ");
167
168 if (NULL != boundStr) {
169 mPath->append(*boundStr);
170 SkDELETE(boundStr);
171 }
172
chudy@google.com97cee972012-08-07 20:41:37 +0000173 return mPath;
174}
175
176SkString* SkObjectParser::PointsToString(const SkPoint pts[], size_t count) {
177 SkString* mPoints = new SkString("SkPoints pts[]: ");
178 for (unsigned int i = 0; i < count; i++) {
179 mPoints->append("(");
180 mPoints->appendScalar(pts[i].fX);
181 mPoints->append(",");
182 mPoints->appendScalar(pts[i].fY);
183 mPoints->append(")");
184 }
185 return mPoints;
186}
187
188SkString* SkObjectParser::PointModeToString(SkCanvas::PointMode mode) {
189 SkString* mMode = new SkString("SkCanvas::PointMode: ");
190 if (mode == SkCanvas::kPoints_PointMode) {
191 mMode->append("kPoints_PointMode");
192 } else if (mode == SkCanvas::kLines_PointMode) {
193 mMode->append("kLines_Mode");
194 } else if (mode == SkCanvas::kPolygon_PointMode) {
195 mMode->append("kPolygon_PointMode");
196 }
197 return mMode;
198}
199
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000200SkString* SkObjectParser::RectToString(const SkRect& rect, const char* title) {
201
202 SkString* mRect = new SkString;
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000203
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000204 if (NULL == title) {
205 mRect->append("SkRect: ");
206 } else {
207 mRect->append(title);
208 }
chudy@google.com97cee972012-08-07 20:41:37 +0000209 mRect->append("(");
210 mRect->appendScalar(rect.left());
211 mRect->append(", ");
212 mRect->appendScalar(rect.top());
213 mRect->append(", ");
214 mRect->appendScalar(rect.right());
215 mRect->append(", ");
216 mRect->appendScalar(rect.bottom());
217 mRect->append(")");
218 return mRect;
219}
220
robertphillips@google.com67baba42013-01-02 20:20:31 +0000221SkString* SkObjectParser::RRectToString(const SkRRect& rrect, const char* title) {
222
223 SkString* mRRect = new SkString;
224
225 if (NULL == title) {
226 mRRect->append("SkRRect (");
227 if (rrect.isEmpty()) {
228 mRRect->append("empty");
229 } else if (rrect.isRect()) {
230 mRRect->append("rect");
231 } else if (rrect.isOval()) {
232 mRRect->append("oval");
233 } else if (rrect.isSimple()) {
234 mRRect->append("simple");
235 } else {
236 SkASSERT(rrect.isComplex());
237 mRRect->append("complex");
238 }
239 mRRect->append("): ");
240 } else {
241 mRRect->append(title);
242 }
243 mRRect->append("(");
244 mRRect->appendScalar(rrect.rect().left());
245 mRRect->append(", ");
246 mRRect->appendScalar(rrect.rect().top());
247 mRRect->append(", ");
248 mRRect->appendScalar(rrect.rect().right());
249 mRRect->append(", ");
250 mRRect->appendScalar(rrect.rect().bottom());
251 mRRect->append(") radii: (");
252 for (int i = 0; i < 4; ++i) {
253 const SkVector& radii = rrect.radii((SkRRect::Corner) i);
254 mRRect->appendScalar(radii.fX);
255 mRRect->append(", ");
256 mRRect->appendScalar(radii.fY);
257 if (i < 3) {
258 mRRect->append(", ");
259 }
260 }
261 mRRect->append(")");
262 return mRRect;
263}
264
chudy@google.com97cee972012-08-07 20:41:37 +0000265SkString* SkObjectParser::RegionOpToString(SkRegion::Op op) {
266 SkString* mOp = new SkString("SkRegion::Op: ");
267 if (op == SkRegion::kDifference_Op) {
268 mOp->append("kDifference_Op");
269 } else if (op == SkRegion::kIntersect_Op) {
270 mOp->append("kIntersect_Op");
271 } else if (op == SkRegion::kUnion_Op) {
272 mOp->append("kUnion_Op");
273 } else if (op == SkRegion::kXOR_Op) {
274 mOp->append("kXOR_Op");
275 } else if (op == SkRegion::kReverseDifference_Op) {
276 mOp->append("kReverseDifference_Op");
277 } else if (op == SkRegion::kReplace_Op) {
278 mOp->append("kReplace_Op");
279 } else {
280 mOp->append("Unknown Type");
281 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000282 return mOp;
283}
284
chudy@google.com97cee972012-08-07 20:41:37 +0000285SkString* SkObjectParser::RegionToString(const SkRegion& region) {
286 SkString* mRegion = new SkString("SkRegion: Data unavailable.");
287 return mRegion;
chudy@google.com902ebe52012-06-29 14:21:22 +0000288}
289
chudy@google.com97cee972012-08-07 20:41:37 +0000290SkString* SkObjectParser::SaveFlagsToString(SkCanvas::SaveFlags flags) {
291 SkString* mFlags = new SkString("SkCanvas::SaveFlags: ");
chudy@google.com902ebe52012-06-29 14:21:22 +0000292 if(flags == SkCanvas::kMatrixClip_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000293 mFlags->append("kMatrixClip_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000294 } else if (flags == SkCanvas::kClip_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000295 mFlags->append("kClip_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000296 } else if (flags == SkCanvas::kHasAlphaLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000297 mFlags->append("kHasAlphaLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000298 } else if (flags == SkCanvas::kFullColorLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000299 mFlags->append("kFullColorLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000300 } else if (flags == SkCanvas::kClipToLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000301 mFlags->append("kClipToLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000302 } else if (flags == SkCanvas::kMatrixClip_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000303 mFlags->append("kMatrixClip_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000304 } else if (flags == SkCanvas::kARGB_NoClipLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000305 mFlags->append("kARGB_NoClipLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000306 } else if (flags == SkCanvas::kARGB_ClipLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000307 mFlags->append("kARGB_ClipLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000308 } else {
chudy@google.com97cee972012-08-07 20:41:37 +0000309 mFlags->append("Data Unavailable");
chudy@google.com902ebe52012-06-29 14:21:22 +0000310 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000311 return mFlags;
312}
313
chudy@google.com97cee972012-08-07 20:41:37 +0000314SkString* SkObjectParser::ScalarToString(SkScalar x, const char* text) {
315 SkString* mScalar = new SkString(text);
316 mScalar->append(" ");
317 mScalar->appendScalar(x);
chudy@google.com902ebe52012-06-29 14:21:22 +0000318 return mScalar;
319}
320
chudy@google.com97cee972012-08-07 20:41:37 +0000321SkString* SkObjectParser::TextToString(const void* text, size_t byteLength) {
robertphillips@google.com94acc702012-09-06 18:43:21 +0000322 SkString* mText = new SkString(6+byteLength+1);
323 mText->append("Text: ");
324 mText->append((char*) text, byteLength);
chudy@google.com97cee972012-08-07 20:41:37 +0000325 return mText;
chudy@google.com902ebe52012-06-29 14:21:22 +0000326}