blob: cba8d067e1789b84c635149e4e2aba2d43b456c4 [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"
robertphillips80af6452015-08-24 08:27:38 -070012#include "SkImage.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkPath.h"
robertphillips@google.com4991b8f2013-01-28 20:21:59 +000014#include "SkRRect.h"
15#include "SkShader.h"
16#include "SkStream.h"
17#include "SkStringUtils.h"
18#include "SkTypeface.h"
bungeman@google.com428fc4a2013-03-07 20:30:32 +000019#include "SkUtils.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000020
21/* TODO(chudy): Replace all std::strings with char */
22
chudy@google.com97cee972012-08-07 20:41:37 +000023SkString* SkObjectParser::BitmapToString(const SkBitmap& bitmap) {
robertphillips@google.com195952f2012-10-23 12:13:35 +000024 SkString* mBitmap = new SkString("SkBitmap: ");
25 mBitmap->append("W: ");
26 mBitmap->appendS32(bitmap.width());
27 mBitmap->append(" H: ");
28 mBitmap->appendS32(bitmap.height());
29
reedc77392e2014-06-02 13:07:26 -070030 const char* gColorTypeStrings[] = {
robertphillips46bffd62015-03-24 11:09:07 -070031 "None", "A8", "565", "4444", "RGBA", "BGRA", "Index8", "G8"
robertphillips@google.com195952f2012-10-23 12:13:35 +000032 };
reedc77392e2014-06-02 13:07:26 -070033 SkASSERT(kLastEnum_SkColorType + 1 == SK_ARRAY_COUNT(gColorTypeStrings));
robertphillips@google.com195952f2012-10-23 12:13:35 +000034
reedc77392e2014-06-02 13:07:26 -070035 mBitmap->append(" ColorType: ");
36 mBitmap->append(gColorTypeStrings[bitmap.colorType()]);
robertphillips@google.com195952f2012-10-23 12:13:35 +000037
38 if (bitmap.isOpaque()) {
39 mBitmap->append(" opaque");
40 } else {
41 mBitmap->append(" not-opaque");
42 }
43
44 if (bitmap.isImmutable()) {
45 mBitmap->append(" immutable");
46 } else {
47 mBitmap->append(" not-immutable");
48 }
49
50 if (bitmap.isVolatile()) {
51 mBitmap->append(" volatile");
52 } else {
53 mBitmap->append(" not-volatile");
54 }
55
56 mBitmap->append(" genID: ");
57 mBitmap->appendS32(bitmap.getGenerationID());
58
chudy@google.com902ebe52012-06-29 14:21:22 +000059 return mBitmap;
60}
61
robertphillips80af6452015-08-24 08:27:38 -070062SkString* SkObjectParser::ImageToString(const SkImage* image) {
63 SkString* str = new SkString("SkImage: ");
64 if (!image) {
65 return str;
66 }
67
68 str->append("W: ");
69 str->appendS32(image->width());
70 str->append(" H: ");
71 str->appendS32(image->height());
72
73 if (image->isOpaque()) {
74 str->append(" opaque");
75 } else {
76 str->append(" not-opaque");
77 }
78
79 str->append(" uniqueID: ");
80 str->appendS32(image->uniqueID());
81
82 return str;
83}
84
chudy@google.com97cee972012-08-07 20:41:37 +000085SkString* SkObjectParser::BoolToString(bool doAA) {
86 SkString* mBool = new SkString("Bool doAA: ");
chudy@google.com902ebe52012-06-29 14:21:22 +000087 if (doAA) {
chudy@google.com97cee972012-08-07 20:41:37 +000088 mBool->append("True");
chudy@google.com902ebe52012-06-29 14:21:22 +000089 } else {
chudy@google.com97cee972012-08-07 20:41:37 +000090 mBool->append("False");
chudy@google.com902ebe52012-06-29 14:21:22 +000091 }
chudy@google.com97cee972012-08-07 20:41:37 +000092 return mBool;
chudy@google.com902ebe52012-06-29 14:21:22 +000093}
94
chudy@google.com97cee972012-08-07 20:41:37 +000095SkString* SkObjectParser::CustomTextToString(const char* text) {
96 SkString* mText = new SkString(text);
97 return mText;
chudy@google.com902ebe52012-06-29 14:21:22 +000098}
99
chudy@google.com97cee972012-08-07 20:41:37 +0000100SkString* SkObjectParser::IntToString(int x, const char* text) {
101 SkString* mInt = new SkString(text);
102 mInt->append(" ");
103 mInt->appendScalar(SkIntToScalar(x));
104 return mInt;
chudy@google.com902ebe52012-06-29 14:21:22 +0000105}
106
chudy@google.com97cee972012-08-07 20:41:37 +0000107SkString* SkObjectParser::IRectToString(const SkIRect& rect) {
108 SkString* mRect = new SkString("SkIRect: ");
109 mRect->append("L: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +0000110 mRect->appendS32(rect.left());
chudy@google.com97cee972012-08-07 20:41:37 +0000111 mRect->append(", T: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +0000112 mRect->appendS32(rect.top());
chudy@google.com97cee972012-08-07 20:41:37 +0000113 mRect->append(", R: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +0000114 mRect->appendS32(rect.right());
chudy@google.com97cee972012-08-07 20:41:37 +0000115 mRect->append(", B: ");
robertphillips@google.com195952f2012-10-23 12:13:35 +0000116 mRect->appendS32(rect.bottom());
chudy@google.com902ebe52012-06-29 14:21:22 +0000117 return mRect;
118}
119
chudy@google.com97cee972012-08-07 20:41:37 +0000120SkString* SkObjectParser::MatrixToString(const SkMatrix& matrix) {
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000121 SkString* str = new SkString("SkMatrix: ");
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000122#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000123 matrix.toString(str);
robertphillips@google.com37a2b392013-02-14 14:40:27 +0000124#endif
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000125 return str;
chudy@google.com97cee972012-08-07 20:41:37 +0000126}
chudy@google.com902ebe52012-06-29 14:21:22 +0000127
robertphillips@google.com735edb02013-01-10 18:54:11 +0000128SkString* SkObjectParser::PaintToString(const SkPaint& paint) {
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000129 SkString* str = new SkString;
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000130#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000131 paint.toString(str);
robertphillips@google.com37a2b392013-02-14 14:40:27 +0000132#endif
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000133 return str;
chudy@google.com97cee972012-08-07 20:41:37 +0000134}
135
136SkString* SkObjectParser::PathToString(const SkPath& path) {
robertphillips@google.com87201762012-10-18 13:30:18 +0000137 SkString* mPath = new SkString("Path (");
138
robertphillips@google.com51185fe2012-12-05 19:34:33 +0000139 static const char* gFillStrings[] = {
140 "Winding", "EvenOdd", "InverseWinding", "InverseEvenOdd"
141 };
142
143 mPath->append(gFillStrings[path.getFillType()]);
144 mPath->append(", ");
145
robertphillips@google.com87201762012-10-18 13:30:18 +0000146 static const char* gConvexityStrings[] = {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000147 "Unknown", "Convex", "Concave"
robertphillips@google.com87201762012-10-18 13:30:18 +0000148 };
149 SkASSERT(SkPath::kConcave_Convexity == 2);
150
151 mPath->append(gConvexityStrings[path.getConvexity()]);
152 mPath->append(", ");
153
robertphillips@google.com195952f2012-10-23 12:13:35 +0000154 if (path.isRect(NULL)) {
155 mPath->append("isRect, ");
156 } else {
157 mPath->append("isNotRect, ");
158 }
159
robertphillips@google.com87201762012-10-18 13:30:18 +0000160 mPath->appendS32(path.countVerbs());
161 mPath->append("V, ");
162 mPath->appendS32(path.countPoints());
163 mPath->append("P): ");
164
165 static const char* gVerbStrings[] = {
commit-bot@chromium.org4a3ca942013-06-04 21:51:06 +0000166 "Move", "Line", "Quad", "Conic", "Cubic", "Close", "Done"
robertphillips@google.com87201762012-10-18 13:30:18 +0000167 };
commit-bot@chromium.org4a3ca942013-06-04 21:51:06 +0000168 static const int gPtsPerVerb[] = { 1, 1, 2, 2, 3, 0, 0 };
169 static const int gPtOffsetPerVerb[] = { 0, 1, 1, 1, 1, 0, 0 };
170 SkASSERT(SkPath::kDone_Verb == 6);
robertphillips@google.com87201762012-10-18 13:30:18 +0000171
172 SkPath::Iter iter(const_cast<SkPath&>(path), false);
173 SkPath::Verb verb;
174 SkPoint points[4];
175
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000176 for(verb = iter.next(points, false);
177 verb != SkPath::kDone_Verb;
robertphillips@google.com87201762012-10-18 13:30:18 +0000178 verb = iter.next(points, false)) {
179
180 mPath->append(gVerbStrings[verb]);
181 mPath->append(" ");
182
183 for (int i = 0; i < gPtsPerVerb[verb]; ++i) {
184 mPath->append("(");
185 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fX);
186 mPath->append(", ");
187 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fY);
commit-bot@chromium.org4a3ca942013-06-04 21:51:06 +0000188 mPath->append(")");
robertphillips@google.com87201762012-10-18 13:30:18 +0000189 }
commit-bot@chromium.org4a3ca942013-06-04 21:51:06 +0000190
191 if (SkPath::kConic_Verb == verb) {
192 mPath->append("(");
193 mPath->appendScalar(iter.conicWeight());
194 mPath->append(")");
195 }
196
197 mPath->append(" ");
chudy@google.com97cee972012-08-07 20:41:37 +0000198 }
robertphillips@google.com87201762012-10-18 13:30:18 +0000199
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000200 SkString* boundStr = SkObjectParser::RectToString(path.getBounds(), " Bound: ");
201
bsalomon49f085d2014-09-05 13:34:00 -0700202 if (boundStr) {
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000203 mPath->append(*boundStr);
halcanary385fe4d2015-08-26 13:07:48 -0700204 delete boundStr;
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000205 }
206
chudy@google.com97cee972012-08-07 20:41:37 +0000207 return mPath;
208}
209
210SkString* SkObjectParser::PointsToString(const SkPoint pts[], size_t count) {
211 SkString* mPoints = new SkString("SkPoints pts[]: ");
212 for (unsigned int i = 0; i < count; i++) {
213 mPoints->append("(");
214 mPoints->appendScalar(pts[i].fX);
215 mPoints->append(",");
216 mPoints->appendScalar(pts[i].fY);
217 mPoints->append(")");
218 }
219 return mPoints;
220}
221
222SkString* SkObjectParser::PointModeToString(SkCanvas::PointMode mode) {
223 SkString* mMode = new SkString("SkCanvas::PointMode: ");
224 if (mode == SkCanvas::kPoints_PointMode) {
225 mMode->append("kPoints_PointMode");
226 } else if (mode == SkCanvas::kLines_PointMode) {
227 mMode->append("kLines_Mode");
228 } else if (mode == SkCanvas::kPolygon_PointMode) {
229 mMode->append("kPolygon_PointMode");
230 }
231 return mMode;
232}
233
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000234SkString* SkObjectParser::RectToString(const SkRect& rect, const char* title) {
235
236 SkString* mRect = new SkString;
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000237
robertphillips@google.com30d35f22012-11-06 16:45:36 +0000238 if (NULL == title) {
239 mRect->append("SkRect: ");
240 } else {
241 mRect->append(title);
242 }
chudy@google.com97cee972012-08-07 20:41:37 +0000243 mRect->append("(");
244 mRect->appendScalar(rect.left());
245 mRect->append(", ");
246 mRect->appendScalar(rect.top());
247 mRect->append(", ");
248 mRect->appendScalar(rect.right());
249 mRect->append(", ");
250 mRect->appendScalar(rect.bottom());
251 mRect->append(")");
252 return mRect;
253}
254
robertphillips@google.com67baba42013-01-02 20:20:31 +0000255SkString* SkObjectParser::RRectToString(const SkRRect& rrect, const char* title) {
256
257 SkString* mRRect = new SkString;
258
259 if (NULL == title) {
260 mRRect->append("SkRRect (");
261 if (rrect.isEmpty()) {
262 mRRect->append("empty");
263 } else if (rrect.isRect()) {
264 mRRect->append("rect");
265 } else if (rrect.isOval()) {
266 mRRect->append("oval");
267 } else if (rrect.isSimple()) {
268 mRRect->append("simple");
commit-bot@chromium.orgf338d7c2014-03-17 21:17:30 +0000269 } else if (rrect.isNinePatch()) {
270 mRRect->append("nine-patch");
robertphillips@google.com67baba42013-01-02 20:20:31 +0000271 } else {
272 SkASSERT(rrect.isComplex());
273 mRRect->append("complex");
274 }
275 mRRect->append("): ");
276 } else {
277 mRRect->append(title);
278 }
279 mRRect->append("(");
280 mRRect->appendScalar(rrect.rect().left());
281 mRRect->append(", ");
282 mRRect->appendScalar(rrect.rect().top());
283 mRRect->append(", ");
284 mRRect->appendScalar(rrect.rect().right());
285 mRRect->append(", ");
286 mRRect->appendScalar(rrect.rect().bottom());
287 mRRect->append(") radii: (");
288 for (int i = 0; i < 4; ++i) {
289 const SkVector& radii = rrect.radii((SkRRect::Corner) i);
290 mRRect->appendScalar(radii.fX);
291 mRRect->append(", ");
292 mRRect->appendScalar(radii.fY);
293 if (i < 3) {
294 mRRect->append(", ");
295 }
296 }
297 mRRect->append(")");
298 return mRRect;
299}
300
chudy@google.com97cee972012-08-07 20:41:37 +0000301SkString* SkObjectParser::RegionOpToString(SkRegion::Op op) {
302 SkString* mOp = new SkString("SkRegion::Op: ");
303 if (op == SkRegion::kDifference_Op) {
304 mOp->append("kDifference_Op");
305 } else if (op == SkRegion::kIntersect_Op) {
306 mOp->append("kIntersect_Op");
307 } else if (op == SkRegion::kUnion_Op) {
308 mOp->append("kUnion_Op");
309 } else if (op == SkRegion::kXOR_Op) {
310 mOp->append("kXOR_Op");
311 } else if (op == SkRegion::kReverseDifference_Op) {
312 mOp->append("kReverseDifference_Op");
313 } else if (op == SkRegion::kReplace_Op) {
314 mOp->append("kReplace_Op");
315 } else {
316 mOp->append("Unknown Type");
317 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000318 return mOp;
319}
320
chudy@google.com97cee972012-08-07 20:41:37 +0000321SkString* SkObjectParser::RegionToString(const SkRegion& region) {
322 SkString* mRegion = new SkString("SkRegion: Data unavailable.");
323 return mRegion;
chudy@google.com902ebe52012-06-29 14:21:22 +0000324}
325
chudy@google.com97cee972012-08-07 20:41:37 +0000326SkString* SkObjectParser::SaveFlagsToString(SkCanvas::SaveFlags flags) {
327 SkString* mFlags = new SkString("SkCanvas::SaveFlags: ");
fmalita@google.comd3ae1d62013-07-11 16:25:55 +0000328 if (flags & SkCanvas::kHasAlphaLayer_SaveFlag) {
329 mFlags->append("kHasAlphaLayer_SaveFlag ");
330 }
331 if (flags & SkCanvas::kFullColorLayer_SaveFlag) {
332 mFlags->append("kFullColorLayer_SaveFlag ");
333 }
334 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
335 mFlags->append("kClipToLayer_SaveFlag ");
chudy@google.com902ebe52012-06-29 14:21:22 +0000336 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000337 return mFlags;
338}
339
chudy@google.com97cee972012-08-07 20:41:37 +0000340SkString* SkObjectParser::ScalarToString(SkScalar x, const char* text) {
341 SkString* mScalar = new SkString(text);
342 mScalar->append(" ");
343 mScalar->appendScalar(x);
chudy@google.com902ebe52012-06-29 14:21:22 +0000344 return mScalar;
345}
346
bungeman@google.com428fc4a2013-03-07 20:30:32 +0000347SkString* SkObjectParser::TextToString(const void* text, size_t byteLength,
348 SkPaint::TextEncoding encoding) {
349
350 SkString* decodedText = new SkString();
351 switch (encoding) {
352 case SkPaint::kUTF8_TextEncoding: {
353 decodedText->append("UTF-8: ");
354 decodedText->append((const char*)text, byteLength);
355 break;
356 }
357 case SkPaint::kUTF16_TextEncoding: {
358 decodedText->append("UTF-16: ");
skia.committer@gmail.comf61ebc02013-11-22 07:02:24 +0000359 size_t sizeNeeded = SkUTF16_ToUTF8((uint16_t*)text,
360 SkToS32(byteLength / 2),
robertphillips@google.coma4662862013-11-21 14:24:16 +0000361 NULL);
scroggo@google.comb9050d72013-08-26 21:20:04 +0000362 SkAutoSTMalloc<0x100, char> utf8(sizeNeeded);
robertphillips@google.coma4662862013-11-21 14:24:16 +0000363 SkUTF16_ToUTF8((uint16_t*)text, SkToS32(byteLength / 2), utf8);
bungeman@google.com428fc4a2013-03-07 20:30:32 +0000364 decodedText->append(utf8, sizeNeeded);
bungeman@google.com428fc4a2013-03-07 20:30:32 +0000365 break;
366 }
367 case SkPaint::kUTF32_TextEncoding: {
368 decodedText->append("UTF-32: ");
369 const SkUnichar* begin = (const SkUnichar*)text;
370 const SkUnichar* end = (const SkUnichar*)((const char*)text + byteLength);
371 for (const SkUnichar* unichar = begin; unichar < end; ++unichar) {
372 decodedText->appendUnichar(*unichar);
373 }
374 break;
375 }
376 case SkPaint::kGlyphID_TextEncoding: {
377 decodedText->append("GlyphID: ");
378 const uint16_t* begin = (const uint16_t*)text;
379 const uint16_t* end = (const uint16_t*)((const char*)text + byteLength);
380 for (const uint16_t* glyph = begin; glyph < end; ++glyph) {
381 decodedText->append("0x");
382 decodedText->appendHex(*glyph);
383 decodedText->append(" ");
384 }
385 break;
386 }
387 default:
388 decodedText->append("Unknown text encoding.");
389 break;
390 }
391
392 return decodedText;
chudy@google.com902ebe52012-06-29 14:21:22 +0000393}