blob: a15a3207d3d5c235a980a5730073220f4cc2f0f1 [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: ");
96 matrix.toString(str);
97 return str;
chudy@google.com97cee972012-08-07 20:41:37 +000098}
chudy@google.com902ebe52012-06-29 14:21:22 +000099
robertphillips@google.com735edb02013-01-10 18:54:11 +0000100SkString* SkObjectParser::PaintToString(const SkPaint& paint) {
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000101 SkString* str = new SkString;
102 paint.toString(str);
103 return str;
chudy@google.com97cee972012-08-07 20:41:37 +0000104}
105
106SkString* SkObjectParser::PathToString(const SkPath& path) {
robertphillips@google.com87201762012-10-18 13:30:18 +0000107 SkString* mPath = new SkString("Path (");
108
robertphillips@google.com51185fe2012-12-05 19:34:33 +0000109 static const char* gFillStrings[] = {
110 "Winding", "EvenOdd", "InverseWinding", "InverseEvenOdd"
111 };
112
113 mPath->append(gFillStrings[path.getFillType()]);
114 mPath->append(", ");
115
robertphillips@google.com87201762012-10-18 13:30:18 +0000116 static const char* gConvexityStrings[] = {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000117 "Unknown", "Convex", "Concave"
robertphillips@google.com87201762012-10-18 13:30:18 +0000118 };
119 SkASSERT(SkPath::kConcave_Convexity == 2);
120
121 mPath->append(gConvexityStrings[path.getConvexity()]);
122 mPath->append(", ");
123
robertphillips@google.com195952f2012-10-23 12:13:35 +0000124 if (path.isRect(NULL)) {
125 mPath->append("isRect, ");
126 } else {
127 mPath->append("isNotRect, ");
128 }
129
robertphillips@google.com87201762012-10-18 13:30:18 +0000130 mPath->appendS32(path.countVerbs());
131 mPath->append("V, ");
132 mPath->appendS32(path.countPoints());
133 mPath->append("P): ");
134
135 static const char* gVerbStrings[] = {
136 "Move", "Line", "Quad", "Cubic", "Close", "Done"
137 };
138 static const int gPtsPerVerb[] = { 1, 1, 2, 3, 0, 0 };
139 static const int gPtOffsetPerVerb[] = { 0, 1, 1, 1, 0, 0 };
140 SkASSERT(SkPath::kDone_Verb == 5);
141
142 SkPath::Iter iter(const_cast<SkPath&>(path), false);
143 SkPath::Verb verb;
144 SkPoint points[4];
145
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000146 for(verb = iter.next(points, false);
147 verb != SkPath::kDone_Verb;
robertphillips@google.com87201762012-10-18 13:30:18 +0000148 verb = iter.next(points, false)) {
149
150 mPath->append(gVerbStrings[verb]);
151 mPath->append(" ");
152
153 for (int i = 0; i < gPtsPerVerb[verb]; ++i) {
154 mPath->append("(");
155 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fX);
156 mPath->append(", ");
157 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fY);
158 mPath->append(") ");
159 }
chudy@google.com97cee972012-08-07 20:41:37 +0000160 }
robertphillips@google.com87201762012-10-18 13:30:18 +0000161
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000162 SkString* boundStr = SkObjectParser::RectToString(path.getBounds(), " Bound: ");
163
164 if (NULL != boundStr) {
165 mPath->append(*boundStr);
166 SkDELETE(boundStr);
167 }
168
chudy@google.com97cee972012-08-07 20:41:37 +0000169 return mPath;
170}
171
172SkString* SkObjectParser::PointsToString(const SkPoint pts[], size_t count) {
173 SkString* mPoints = new SkString("SkPoints pts[]: ");
174 for (unsigned int i = 0; i < count; i++) {
175 mPoints->append("(");
176 mPoints->appendScalar(pts[i].fX);
177 mPoints->append(",");
178 mPoints->appendScalar(pts[i].fY);
179 mPoints->append(")");
180 }
181 return mPoints;
182}
183
184SkString* SkObjectParser::PointModeToString(SkCanvas::PointMode mode) {
185 SkString* mMode = new SkString("SkCanvas::PointMode: ");
186 if (mode == SkCanvas::kPoints_PointMode) {
187 mMode->append("kPoints_PointMode");
188 } else if (mode == SkCanvas::kLines_PointMode) {
189 mMode->append("kLines_Mode");
190 } else if (mode == SkCanvas::kPolygon_PointMode) {
191 mMode->append("kPolygon_PointMode");
192 }
193 return mMode;
194}
195
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000196SkString* SkObjectParser::RectToString(const SkRect& rect, const char* title) {
197
198 SkString* mRect = new SkString;
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000199
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000200 if (NULL == title) {
201 mRect->append("SkRect: ");
202 } else {
203 mRect->append(title);
204 }
chudy@google.com97cee972012-08-07 20:41:37 +0000205 mRect->append("(");
206 mRect->appendScalar(rect.left());
207 mRect->append(", ");
208 mRect->appendScalar(rect.top());
209 mRect->append(", ");
210 mRect->appendScalar(rect.right());
211 mRect->append(", ");
212 mRect->appendScalar(rect.bottom());
213 mRect->append(")");
214 return mRect;
215}
216
robertphillips@google.com67baba42013-01-02 20:20:31 +0000217SkString* SkObjectParser::RRectToString(const SkRRect& rrect, const char* title) {
218
219 SkString* mRRect = new SkString;
220
221 if (NULL == title) {
222 mRRect->append("SkRRect (");
223 if (rrect.isEmpty()) {
224 mRRect->append("empty");
225 } else if (rrect.isRect()) {
226 mRRect->append("rect");
227 } else if (rrect.isOval()) {
228 mRRect->append("oval");
229 } else if (rrect.isSimple()) {
230 mRRect->append("simple");
231 } else {
232 SkASSERT(rrect.isComplex());
233 mRRect->append("complex");
234 }
235 mRRect->append("): ");
236 } else {
237 mRRect->append(title);
238 }
239 mRRect->append("(");
240 mRRect->appendScalar(rrect.rect().left());
241 mRRect->append(", ");
242 mRRect->appendScalar(rrect.rect().top());
243 mRRect->append(", ");
244 mRRect->appendScalar(rrect.rect().right());
245 mRRect->append(", ");
246 mRRect->appendScalar(rrect.rect().bottom());
247 mRRect->append(") radii: (");
248 for (int i = 0; i < 4; ++i) {
249 const SkVector& radii = rrect.radii((SkRRect::Corner) i);
250 mRRect->appendScalar(radii.fX);
251 mRRect->append(", ");
252 mRRect->appendScalar(radii.fY);
253 if (i < 3) {
254 mRRect->append(", ");
255 }
256 }
257 mRRect->append(")");
258 return mRRect;
259}
260
chudy@google.com97cee972012-08-07 20:41:37 +0000261SkString* SkObjectParser::RegionOpToString(SkRegion::Op op) {
262 SkString* mOp = new SkString("SkRegion::Op: ");
263 if (op == SkRegion::kDifference_Op) {
264 mOp->append("kDifference_Op");
265 } else if (op == SkRegion::kIntersect_Op) {
266 mOp->append("kIntersect_Op");
267 } else if (op == SkRegion::kUnion_Op) {
268 mOp->append("kUnion_Op");
269 } else if (op == SkRegion::kXOR_Op) {
270 mOp->append("kXOR_Op");
271 } else if (op == SkRegion::kReverseDifference_Op) {
272 mOp->append("kReverseDifference_Op");
273 } else if (op == SkRegion::kReplace_Op) {
274 mOp->append("kReplace_Op");
275 } else {
276 mOp->append("Unknown Type");
277 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000278 return mOp;
279}
280
chudy@google.com97cee972012-08-07 20:41:37 +0000281SkString* SkObjectParser::RegionToString(const SkRegion& region) {
282 SkString* mRegion = new SkString("SkRegion: Data unavailable.");
283 return mRegion;
chudy@google.com902ebe52012-06-29 14:21:22 +0000284}
285
chudy@google.com97cee972012-08-07 20:41:37 +0000286SkString* SkObjectParser::SaveFlagsToString(SkCanvas::SaveFlags flags) {
287 SkString* mFlags = new SkString("SkCanvas::SaveFlags: ");
chudy@google.com902ebe52012-06-29 14:21:22 +0000288 if(flags == SkCanvas::kMatrixClip_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000289 mFlags->append("kMatrixClip_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000290 } else if (flags == SkCanvas::kClip_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000291 mFlags->append("kClip_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000292 } else if (flags == SkCanvas::kHasAlphaLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000293 mFlags->append("kHasAlphaLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000294 } else if (flags == SkCanvas::kFullColorLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000295 mFlags->append("kFullColorLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000296 } else if (flags == SkCanvas::kClipToLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000297 mFlags->append("kClipToLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000298 } else if (flags == SkCanvas::kMatrixClip_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000299 mFlags->append("kMatrixClip_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000300 } else if (flags == SkCanvas::kARGB_NoClipLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000301 mFlags->append("kARGB_NoClipLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000302 } else if (flags == SkCanvas::kARGB_ClipLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000303 mFlags->append("kARGB_ClipLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000304 } else {
chudy@google.com97cee972012-08-07 20:41:37 +0000305 mFlags->append("Data Unavailable");
chudy@google.com902ebe52012-06-29 14:21:22 +0000306 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000307 return mFlags;
308}
309
chudy@google.com97cee972012-08-07 20:41:37 +0000310SkString* SkObjectParser::ScalarToString(SkScalar x, const char* text) {
311 SkString* mScalar = new SkString(text);
312 mScalar->append(" ");
313 mScalar->appendScalar(x);
chudy@google.com902ebe52012-06-29 14:21:22 +0000314 return mScalar;
315}
316
chudy@google.com97cee972012-08-07 20:41:37 +0000317SkString* SkObjectParser::TextToString(const void* text, size_t byteLength) {
robertphillips@google.com94acc702012-09-06 18:43:21 +0000318 SkString* mText = new SkString(6+byteLength+1);
319 mText->append("Text: ");
320 mText->append((char*) text, byteLength);
chudy@google.com97cee972012-08-07 20:41:37 +0000321 return mText;
chudy@google.com902ebe52012-06-29 14:21:22 +0000322}