1
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"
10 #include "SkData.h"
11 #include "SkFontDescriptor.h"
12 #include "SkRRect.h"
13 #include "SkShader.h"
14 #include "SkStream.h"
15 #include "SkStringUtils.h"
16 #include "SkTypeface.h"
17 #include "SkUtils.h"
18
19 /* TODO(chudy): Replace all std::strings with char */
20
BitmapToString(const SkBitmap & bitmap)21 SkString* SkObjectParser::BitmapToString(const SkBitmap& bitmap) {
22 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* gColorTypeStrings[] = {
29 "None", "A8", "565", "4444", "RGBA", "BGRA", "Index8", "G8"
30 };
31 SkASSERT(kLastEnum_SkColorType + 1 == SK_ARRAY_COUNT(gColorTypeStrings));
32
33 mBitmap->append(" ColorType: ");
34 mBitmap->append(gColorTypeStrings[bitmap.colorType()]);
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
57 return mBitmap;
58 }
59
BoolToString(bool doAA)60 SkString* SkObjectParser::BoolToString(bool doAA) {
61 SkString* mBool = new SkString("Bool doAA: ");
62 if (doAA) {
63 mBool->append("True");
64 } else {
65 mBool->append("False");
66 }
67 return mBool;
68 }
69
CustomTextToString(const char * text)70 SkString* SkObjectParser::CustomTextToString(const char* text) {
71 SkString* mText = new SkString(text);
72 return mText;
73 }
74
IntToString(int x,const char * text)75 SkString* SkObjectParser::IntToString(int x, const char* text) {
76 SkString* mInt = new SkString(text);
77 mInt->append(" ");
78 mInt->appendScalar(SkIntToScalar(x));
79 return mInt;
80 }
81
IRectToString(const SkIRect & rect)82 SkString* SkObjectParser::IRectToString(const SkIRect& rect) {
83 SkString* mRect = new SkString("SkIRect: ");
84 mRect->append("L: ");
85 mRect->appendS32(rect.left());
86 mRect->append(", T: ");
87 mRect->appendS32(rect.top());
88 mRect->append(", R: ");
89 mRect->appendS32(rect.right());
90 mRect->append(", B: ");
91 mRect->appendS32(rect.bottom());
92 return mRect;
93 }
94
MatrixToString(const SkMatrix & matrix)95 SkString* SkObjectParser::MatrixToString(const SkMatrix& matrix) {
96 SkString* str = new SkString("SkMatrix: ");
97 #ifndef SK_IGNORE_TO_STRING
98 matrix.toString(str);
99 #endif
100 return str;
101 }
102
PaintToString(const SkPaint & paint)103 SkString* SkObjectParser::PaintToString(const SkPaint& paint) {
104 SkString* str = new SkString;
105 #ifndef SK_IGNORE_TO_STRING
106 paint.toString(str);
107 #endif
108 return str;
109 }
110
PathToString(const SkPath & path)111 SkString* SkObjectParser::PathToString(const SkPath& path) {
112 SkString* mPath = new SkString("Path (");
113
114 static const char* gFillStrings[] = {
115 "Winding", "EvenOdd", "InverseWinding", "InverseEvenOdd"
116 };
117
118 mPath->append(gFillStrings[path.getFillType()]);
119 mPath->append(", ");
120
121 static const char* gConvexityStrings[] = {
122 "Unknown", "Convex", "Concave"
123 };
124 SkASSERT(SkPath::kConcave_Convexity == 2);
125
126 mPath->append(gConvexityStrings[path.getConvexity()]);
127 mPath->append(", ");
128
129 if (path.isRect(NULL)) {
130 mPath->append("isRect, ");
131 } else {
132 mPath->append("isNotRect, ");
133 }
134
135 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", "Conic", "Cubic", "Close", "Done"
142 };
143 static const int gPtsPerVerb[] = { 1, 1, 2, 2, 3, 0, 0 };
144 static const int gPtOffsetPerVerb[] = { 0, 1, 1, 1, 1, 0, 0 };
145 SkASSERT(SkPath::kDone_Verb == 6);
146
147 SkPath::Iter iter(const_cast<SkPath&>(path), false);
148 SkPath::Verb verb;
149 SkPoint points[4];
150
151 for(verb = iter.next(points, false);
152 verb != SkPath::kDone_Verb;
153 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 }
165
166 if (SkPath::kConic_Verb == verb) {
167 mPath->append("(");
168 mPath->appendScalar(iter.conicWeight());
169 mPath->append(")");
170 }
171
172 mPath->append(" ");
173 }
174
175 SkString* boundStr = SkObjectParser::RectToString(path.getBounds(), " Bound: ");
176
177 if (boundStr) {
178 mPath->append(*boundStr);
179 SkDELETE(boundStr);
180 }
181
182 return mPath;
183 }
184
PointsToString(const SkPoint pts[],size_t count)185 SkString* SkObjectParser::PointsToString(const SkPoint pts[], size_t count) {
186 SkString* mPoints = new SkString("SkPoints pts[]: ");
187 for (unsigned int i = 0; i < count; i++) {
188 mPoints->append("(");
189 mPoints->appendScalar(pts[i].fX);
190 mPoints->append(",");
191 mPoints->appendScalar(pts[i].fY);
192 mPoints->append(")");
193 }
194 return mPoints;
195 }
196
PointModeToString(SkCanvas::PointMode mode)197 SkString* SkObjectParser::PointModeToString(SkCanvas::PointMode mode) {
198 SkString* mMode = new SkString("SkCanvas::PointMode: ");
199 if (mode == SkCanvas::kPoints_PointMode) {
200 mMode->append("kPoints_PointMode");
201 } else if (mode == SkCanvas::kLines_PointMode) {
202 mMode->append("kLines_Mode");
203 } else if (mode == SkCanvas::kPolygon_PointMode) {
204 mMode->append("kPolygon_PointMode");
205 }
206 return mMode;
207 }
208
RectToString(const SkRect & rect,const char * title)209 SkString* SkObjectParser::RectToString(const SkRect& rect, const char* title) {
210
211 SkString* mRect = new SkString;
212
213 if (NULL == title) {
214 mRect->append("SkRect: ");
215 } else {
216 mRect->append(title);
217 }
218 mRect->append("(");
219 mRect->appendScalar(rect.left());
220 mRect->append(", ");
221 mRect->appendScalar(rect.top());
222 mRect->append(", ");
223 mRect->appendScalar(rect.right());
224 mRect->append(", ");
225 mRect->appendScalar(rect.bottom());
226 mRect->append(")");
227 return mRect;
228 }
229
RRectToString(const SkRRect & rrect,const char * title)230 SkString* SkObjectParser::RRectToString(const SkRRect& rrect, const char* title) {
231
232 SkString* mRRect = new SkString;
233
234 if (NULL == title) {
235 mRRect->append("SkRRect (");
236 if (rrect.isEmpty()) {
237 mRRect->append("empty");
238 } else if (rrect.isRect()) {
239 mRRect->append("rect");
240 } else if (rrect.isOval()) {
241 mRRect->append("oval");
242 } else if (rrect.isSimple()) {
243 mRRect->append("simple");
244 } else if (rrect.isNinePatch()) {
245 mRRect->append("nine-patch");
246 } else {
247 SkASSERT(rrect.isComplex());
248 mRRect->append("complex");
249 }
250 mRRect->append("): ");
251 } else {
252 mRRect->append(title);
253 }
254 mRRect->append("(");
255 mRRect->appendScalar(rrect.rect().left());
256 mRRect->append(", ");
257 mRRect->appendScalar(rrect.rect().top());
258 mRRect->append(", ");
259 mRRect->appendScalar(rrect.rect().right());
260 mRRect->append(", ");
261 mRRect->appendScalar(rrect.rect().bottom());
262 mRRect->append(") radii: (");
263 for (int i = 0; i < 4; ++i) {
264 const SkVector& radii = rrect.radii((SkRRect::Corner) i);
265 mRRect->appendScalar(radii.fX);
266 mRRect->append(", ");
267 mRRect->appendScalar(radii.fY);
268 if (i < 3) {
269 mRRect->append(", ");
270 }
271 }
272 mRRect->append(")");
273 return mRRect;
274 }
275
RegionOpToString(SkRegion::Op op)276 SkString* SkObjectParser::RegionOpToString(SkRegion::Op op) {
277 SkString* mOp = new SkString("SkRegion::Op: ");
278 if (op == SkRegion::kDifference_Op) {
279 mOp->append("kDifference_Op");
280 } else if (op == SkRegion::kIntersect_Op) {
281 mOp->append("kIntersect_Op");
282 } else if (op == SkRegion::kUnion_Op) {
283 mOp->append("kUnion_Op");
284 } else if (op == SkRegion::kXOR_Op) {
285 mOp->append("kXOR_Op");
286 } else if (op == SkRegion::kReverseDifference_Op) {
287 mOp->append("kReverseDifference_Op");
288 } else if (op == SkRegion::kReplace_Op) {
289 mOp->append("kReplace_Op");
290 } else {
291 mOp->append("Unknown Type");
292 }
293 return mOp;
294 }
295
RegionToString(const SkRegion & region)296 SkString* SkObjectParser::RegionToString(const SkRegion& region) {
297 SkString* mRegion = new SkString("SkRegion: Data unavailable.");
298 return mRegion;
299 }
300
SaveFlagsToString(SkCanvas::SaveFlags flags)301 SkString* SkObjectParser::SaveFlagsToString(SkCanvas::SaveFlags flags) {
302 SkString* mFlags = new SkString("SkCanvas::SaveFlags: ");
303 if (flags & SkCanvas::kHasAlphaLayer_SaveFlag) {
304 mFlags->append("kHasAlphaLayer_SaveFlag ");
305 }
306 if (flags & SkCanvas::kFullColorLayer_SaveFlag) {
307 mFlags->append("kFullColorLayer_SaveFlag ");
308 }
309 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
310 mFlags->append("kClipToLayer_SaveFlag ");
311 }
312 return mFlags;
313 }
314
ScalarToString(SkScalar x,const char * text)315 SkString* SkObjectParser::ScalarToString(SkScalar x, const char* text) {
316 SkString* mScalar = new SkString(text);
317 mScalar->append(" ");
318 mScalar->appendScalar(x);
319 return mScalar;
320 }
321
TextToString(const void * text,size_t byteLength,SkPaint::TextEncoding encoding)322 SkString* 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,
335 SkToS32(byteLength / 2),
336 NULL);
337 SkAutoSTMalloc<0x100, char> utf8(sizeNeeded);
338 SkUTF16_ToUTF8((uint16_t*)text, SkToS32(byteLength / 2), utf8);
339 decodedText->append(utf8, sizeNeeded);
340 break;
341 }
342 case SkPaint::kUTF32_TextEncoding: {
343 decodedText->append("UTF-32: ");
344 const SkUnichar* begin = (const SkUnichar*)text;
345 const SkUnichar* end = (const SkUnichar*)((const char*)text + byteLength);
346 for (const SkUnichar* unichar = begin; unichar < end; ++unichar) {
347 decodedText->appendUnichar(*unichar);
348 }
349 break;
350 }
351 case SkPaint::kGlyphID_TextEncoding: {
352 decodedText->append("GlyphID: ");
353 const uint16_t* begin = (const uint16_t*)text;
354 const uint16_t* end = (const uint16_t*)((const char*)text + byteLength);
355 for (const uint16_t* glyph = begin; glyph < end; ++glyph) {
356 decodedText->append("0x");
357 decodedText->appendHex(*glyph);
358 decodedText->append(" ");
359 }
360 break;
361 }
362 default:
363 decodedText->append("Unknown text encoding.");
364 break;
365 }
366
367 return decodedText;
368 }
369