blob: 2d1f140f67c0ae96ae3eb1fc7f53fdc73da5a769 [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"
bungeman@google.com428fc4a2013-03-07 20:30:32 +000017#include "SkUtils.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000018
19/* TODO(chudy): Replace all std::strings with char */
20
chudy@google.com97cee972012-08-07 20:41:37 +000021SkString* SkObjectParser::BitmapToString(const SkBitmap& bitmap) {
robertphillips@google.com195952f2012-10-23 12:13:35 +000022 SkString* mBitmap = new SkString("SkBitmap: ");
23 mBitmap->append("W: ");
24 mBitmap->appendS32(bitmap.width());
25 mBitmap->append(" H: ");
26 mBitmap->appendS32(bitmap.height());
27
28 const char* gConfigStrings[] = {
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +000029 "None", "A1", "A8", "Index8", "RGB565", "ARGB4444", "ARGB8888", "RLE8"
robertphillips@google.com195952f2012-10-23 12:13:35 +000030 };
31 SkASSERT(SkBitmap::kConfigCount == 8);
32
33 mBitmap->append(" Config: ");
34 mBitmap->append(gConfigStrings[bitmap.getConfig()]);
35
36 if (bitmap.isOpaque()) {
37 mBitmap->append(" opaque");
38 } else {
39 mBitmap->append(" not-opaque");
40 }
41
42 if (bitmap.isImmutable()) {
43 mBitmap->append(" immutable");
44 } else {
45 mBitmap->append(" not-immutable");
46 }
47
48 if (bitmap.isVolatile()) {
49 mBitmap->append(" volatile");
50 } else {
51 mBitmap->append(" not-volatile");
52 }
53
54 mBitmap->append(" genID: ");
55 mBitmap->appendS32(bitmap.getGenerationID());
56
chudy@google.com902ebe52012-06-29 14:21:22 +000057 return mBitmap;
58}
59
chudy@google.com97cee972012-08-07 20:41:37 +000060SkString* SkObjectParser::BoolToString(bool doAA) {
61 SkString* mBool = new SkString("Bool doAA: ");
chudy@google.com902ebe52012-06-29 14:21:22 +000062 if (doAA) {
chudy@google.com97cee972012-08-07 20:41:37 +000063 mBool->append("True");
chudy@google.com902ebe52012-06-29 14:21:22 +000064 } else {
chudy@google.com97cee972012-08-07 20:41:37 +000065 mBool->append("False");
chudy@google.com902ebe52012-06-29 14:21:22 +000066 }
chudy@google.com97cee972012-08-07 20:41:37 +000067 return mBool;
chudy@google.com902ebe52012-06-29 14:21:22 +000068}
69
chudy@google.com97cee972012-08-07 20:41:37 +000070SkString* SkObjectParser::CustomTextToString(const char* text) {
71 SkString* mText = new SkString(text);
72 return mText;
chudy@google.com902ebe52012-06-29 14:21:22 +000073}
74
chudy@google.com97cee972012-08-07 20:41:37 +000075SkString* SkObjectParser::IntToString(int x, const char* text) {
76 SkString* mInt = new SkString(text);
77 mInt->append(" ");
78 mInt->appendScalar(SkIntToScalar(x));
79 return mInt;
chudy@google.com902ebe52012-06-29 14:21:22 +000080}
81
chudy@google.com97cee972012-08-07 20:41:37 +000082SkString* SkObjectParser::IRectToString(const SkIRect& rect) {
83 SkString* mRect = new SkString("SkIRect: ");
84 mRect->append("L: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +000085 mRect->appendS32(rect.left());
chudy@google.com97cee972012-08-07 20:41:37 +000086 mRect->append(", T: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +000087 mRect->appendS32(rect.top());
chudy@google.com97cee972012-08-07 20:41:37 +000088 mRect->append(", R: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +000089 mRect->appendS32(rect.right());
chudy@google.com97cee972012-08-07 20:41:37 +000090 mRect->append(", B: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +000091 mRect->appendS32(rect.bottom());
chudy@google.com902ebe52012-06-29 14:21:22 +000092 return mRect;
93}
94
chudy@google.com97cee972012-08-07 20:41:37 +000095SkString* SkObjectParser::MatrixToString(const SkMatrix& matrix) {
robertphillips@google.com791f12e2013-02-14 13:53:53 +000096 SkString* str = new SkString("SkMatrix: ");
robertphillips@google.com37a2b392013-02-14 14:40:27 +000097#ifdef SK_DEVELOPER
robertphillips@google.com791f12e2013-02-14 13:53:53 +000098 matrix.toString(str);
robertphillips@google.com37a2b392013-02-14 14:40:27 +000099#endif
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000100 return str;
chudy@google.com97cee972012-08-07 20:41:37 +0000101}
chudy@google.com902ebe52012-06-29 14:21:22 +0000102
robertphillips@google.com735edb02013-01-10 18:54:11 +0000103SkString* SkObjectParser::PaintToString(const SkPaint& paint) {
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000104 SkString* str = new SkString;
robertphillips@google.com37a2b392013-02-14 14:40:27 +0000105#ifdef SK_DEVELOPER
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000106 paint.toString(str);
robertphillips@google.com37a2b392013-02-14 14:40:27 +0000107#endif
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000108 return str;
chudy@google.com97cee972012-08-07 20:41:37 +0000109}
110
111SkString* SkObjectParser::PathToString(const SkPath& path) {
robertphillips@google.com87201762012-10-18 13:30:18 +0000112 SkString* mPath = new SkString("Path (");
113
robertphillips@google.com51185fe2012-12-05 19:34:33 +0000114 static const char* gFillStrings[] = {
115 "Winding", "EvenOdd", "InverseWinding", "InverseEvenOdd"
116 };
117
118 mPath->append(gFillStrings[path.getFillType()]);
119 mPath->append(", ");
120
robertphillips@google.com87201762012-10-18 13:30:18 +0000121 static const char* gConvexityStrings[] = {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000122 "Unknown", "Convex", "Concave"
robertphillips@google.com87201762012-10-18 13:30:18 +0000123 };
124 SkASSERT(SkPath::kConcave_Convexity == 2);
125
126 mPath->append(gConvexityStrings[path.getConvexity()]);
127 mPath->append(", ");
128
robertphillips@google.com195952f2012-10-23 12:13:35 +0000129 if (path.isRect(NULL)) {
130 mPath->append("isRect, ");
131 } else {
132 mPath->append("isNotRect, ");
133 }
134
robertphillips@google.com87201762012-10-18 13:30:18 +0000135 mPath->appendS32(path.countVerbs());
136 mPath->append("V, ");
137 mPath->appendS32(path.countPoints());
138 mPath->append("P): ");
139
140 static const char* gVerbStrings[] = {
141 "Move", "Line", "Quad", "Cubic", "Close", "Done"
142 };
143 static const int gPtsPerVerb[] = { 1, 1, 2, 3, 0, 0 };
144 static const int gPtOffsetPerVerb[] = { 0, 1, 1, 1, 0, 0 };
145 SkASSERT(SkPath::kDone_Verb == 5);
146
147 SkPath::Iter iter(const_cast<SkPath&>(path), false);
148 SkPath::Verb verb;
149 SkPoint points[4];
150
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000151 for(verb = iter.next(points, false);
152 verb != SkPath::kDone_Verb;
robertphillips@google.com87201762012-10-18 13:30:18 +0000153 verb = iter.next(points, false)) {
154
155 mPath->append(gVerbStrings[verb]);
156 mPath->append(" ");
157
158 for (int i = 0; i < gPtsPerVerb[verb]; ++i) {
159 mPath->append("(");
160 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fX);
161 mPath->append(", ");
162 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fY);
163 mPath->append(") ");
164 }
chudy@google.com97cee972012-08-07 20:41:37 +0000165 }
robertphillips@google.com87201762012-10-18 13:30:18 +0000166
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000167 SkString* boundStr = SkObjectParser::RectToString(path.getBounds(), " Bound: ");
168
169 if (NULL != boundStr) {
170 mPath->append(*boundStr);
171 SkDELETE(boundStr);
172 }
173
chudy@google.com97cee972012-08-07 20:41:37 +0000174 return mPath;
175}
176
177SkString* SkObjectParser::PointsToString(const SkPoint pts[], size_t count) {
178 SkString* mPoints = new SkString("SkPoints pts[]: ");
179 for (unsigned int i = 0; i < count; i++) {
180 mPoints->append("(");
181 mPoints->appendScalar(pts[i].fX);
182 mPoints->append(",");
183 mPoints->appendScalar(pts[i].fY);
184 mPoints->append(")");
185 }
186 return mPoints;
187}
188
189SkString* SkObjectParser::PointModeToString(SkCanvas::PointMode mode) {
190 SkString* mMode = new SkString("SkCanvas::PointMode: ");
191 if (mode == SkCanvas::kPoints_PointMode) {
192 mMode->append("kPoints_PointMode");
193 } else if (mode == SkCanvas::kLines_PointMode) {
194 mMode->append("kLines_Mode");
195 } else if (mode == SkCanvas::kPolygon_PointMode) {
196 mMode->append("kPolygon_PointMode");
197 }
198 return mMode;
199}
200
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000201SkString* SkObjectParser::RectToString(const SkRect& rect, const char* title) {
202
203 SkString* mRect = new SkString;
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000204
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000205 if (NULL == title) {
206 mRect->append("SkRect: ");
207 } else {
208 mRect->append(title);
209 }
chudy@google.com97cee972012-08-07 20:41:37 +0000210 mRect->append("(");
211 mRect->appendScalar(rect.left());
212 mRect->append(", ");
213 mRect->appendScalar(rect.top());
214 mRect->append(", ");
215 mRect->appendScalar(rect.right());
216 mRect->append(", ");
217 mRect->appendScalar(rect.bottom());
218 mRect->append(")");
219 return mRect;
220}
221
robertphillips@google.com67baba42013-01-02 20:20:31 +0000222SkString* SkObjectParser::RRectToString(const SkRRect& rrect, const char* title) {
223
224 SkString* mRRect = new SkString;
225
226 if (NULL == title) {
227 mRRect->append("SkRRect (");
228 if (rrect.isEmpty()) {
229 mRRect->append("empty");
230 } else if (rrect.isRect()) {
231 mRRect->append("rect");
232 } else if (rrect.isOval()) {
233 mRRect->append("oval");
234 } else if (rrect.isSimple()) {
235 mRRect->append("simple");
236 } else {
237 SkASSERT(rrect.isComplex());
238 mRRect->append("complex");
239 }
240 mRRect->append("): ");
241 } else {
242 mRRect->append(title);
243 }
244 mRRect->append("(");
245 mRRect->appendScalar(rrect.rect().left());
246 mRRect->append(", ");
247 mRRect->appendScalar(rrect.rect().top());
248 mRRect->append(", ");
249 mRRect->appendScalar(rrect.rect().right());
250 mRRect->append(", ");
251 mRRect->appendScalar(rrect.rect().bottom());
252 mRRect->append(") radii: (");
253 for (int i = 0; i < 4; ++i) {
254 const SkVector& radii = rrect.radii((SkRRect::Corner) i);
255 mRRect->appendScalar(radii.fX);
256 mRRect->append(", ");
257 mRRect->appendScalar(radii.fY);
258 if (i < 3) {
259 mRRect->append(", ");
260 }
261 }
262 mRRect->append(")");
263 return mRRect;
264}
265
chudy@google.com97cee972012-08-07 20:41:37 +0000266SkString* SkObjectParser::RegionOpToString(SkRegion::Op op) {
267 SkString* mOp = new SkString("SkRegion::Op: ");
268 if (op == SkRegion::kDifference_Op) {
269 mOp->append("kDifference_Op");
270 } else if (op == SkRegion::kIntersect_Op) {
271 mOp->append("kIntersect_Op");
272 } else if (op == SkRegion::kUnion_Op) {
273 mOp->append("kUnion_Op");
274 } else if (op == SkRegion::kXOR_Op) {
275 mOp->append("kXOR_Op");
276 } else if (op == SkRegion::kReverseDifference_Op) {
277 mOp->append("kReverseDifference_Op");
278 } else if (op == SkRegion::kReplace_Op) {
279 mOp->append("kReplace_Op");
280 } else {
281 mOp->append("Unknown Type");
282 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000283 return mOp;
284}
285
chudy@google.com97cee972012-08-07 20:41:37 +0000286SkString* SkObjectParser::RegionToString(const SkRegion& region) {
287 SkString* mRegion = new SkString("SkRegion: Data unavailable.");
288 return mRegion;
chudy@google.com902ebe52012-06-29 14:21:22 +0000289}
290
chudy@google.com97cee972012-08-07 20:41:37 +0000291SkString* SkObjectParser::SaveFlagsToString(SkCanvas::SaveFlags flags) {
292 SkString* mFlags = new SkString("SkCanvas::SaveFlags: ");
chudy@google.com902ebe52012-06-29 14:21:22 +0000293 if(flags == SkCanvas::kMatrixClip_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000294 mFlags->append("kMatrixClip_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000295 } else if (flags == SkCanvas::kClip_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000296 mFlags->append("kClip_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000297 } else if (flags == SkCanvas::kHasAlphaLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000298 mFlags->append("kHasAlphaLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000299 } else if (flags == SkCanvas::kFullColorLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000300 mFlags->append("kFullColorLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000301 } else if (flags == SkCanvas::kClipToLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000302 mFlags->append("kClipToLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000303 } else if (flags == SkCanvas::kMatrixClip_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000304 mFlags->append("kMatrixClip_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000305 } else if (flags == SkCanvas::kARGB_NoClipLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000306 mFlags->append("kARGB_NoClipLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000307 } else if (flags == SkCanvas::kARGB_ClipLayer_SaveFlag) {
chudy@google.com97cee972012-08-07 20:41:37 +0000308 mFlags->append("kARGB_ClipLayer_SaveFlag");
chudy@google.com902ebe52012-06-29 14:21:22 +0000309 } else {
chudy@google.com97cee972012-08-07 20:41:37 +0000310 mFlags->append("Data Unavailable");
chudy@google.com902ebe52012-06-29 14:21:22 +0000311 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000312 return mFlags;
313}
314
chudy@google.com97cee972012-08-07 20:41:37 +0000315SkString* SkObjectParser::ScalarToString(SkScalar x, const char* text) {
316 SkString* mScalar = new SkString(text);
317 mScalar->append(" ");
318 mScalar->appendScalar(x);
chudy@google.com902ebe52012-06-29 14:21:22 +0000319 return mScalar;
320}
321
bungeman@google.com428fc4a2013-03-07 20:30:32 +0000322SkString* SkObjectParser::TextToString(const void* text, size_t byteLength,
323 SkPaint::TextEncoding encoding) {
324
325 SkString* decodedText = new SkString();
326 switch (encoding) {
327 case SkPaint::kUTF8_TextEncoding: {
328 decodedText->append("UTF-8: ");
329 decodedText->append((const char*)text, byteLength);
330 break;
331 }
332 case SkPaint::kUTF16_TextEncoding: {
333 decodedText->append("UTF-16: ");
334 size_t sizeNeeded = SkUTF16_ToUTF8((uint16_t*)text, byteLength / 2, NULL);
335 char* utf8 = new char[sizeNeeded];
336 SkUTF16_ToUTF8((uint16_t*)text, byteLength / 2, utf8);
337 decodedText->append(utf8, sizeNeeded);
338 delete utf8;
339 break;
340 }
341 case SkPaint::kUTF32_TextEncoding: {
342 decodedText->append("UTF-32: ");
343 const SkUnichar* begin = (const SkUnichar*)text;
344 const SkUnichar* end = (const SkUnichar*)((const char*)text + byteLength);
345 for (const SkUnichar* unichar = begin; unichar < end; ++unichar) {
346 decodedText->appendUnichar(*unichar);
347 }
348 break;
349 }
350 case SkPaint::kGlyphID_TextEncoding: {
351 decodedText->append("GlyphID: ");
352 const uint16_t* begin = (const uint16_t*)text;
353 const uint16_t* end = (const uint16_t*)((const char*)text + byteLength);
354 for (const uint16_t* glyph = begin; glyph < end; ++glyph) {
355 decodedText->append("0x");
356 decodedText->appendHex(*glyph);
357 decodedText->append(" ");
358 }
359 break;
360 }
361 default:
362 decodedText->append("Unknown text encoding.");
363 break;
364 }
365
366 return decodedText;
chudy@google.com902ebe52012-06-29 14:21:22 +0000367}