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 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* gConfigStrings[] = { 29 "None", "A8", "Index8", "RGB565", "ARGB4444", "ARGB8888" 30 }; 31 SkASSERT(SkBitmap::kConfigCount == SK_ARRAY_COUNT(gConfigStrings)); 32 33 mBitmap->append(" Config: "); 34 mBitmap->append(gConfigStrings[bitmap.config()]); 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 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 70 SkString* SkObjectParser::CustomTextToString(const char* text) { 71 SkString* mText = new SkString(text); 72 return mText; 73 } 74 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 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 95 SkString* SkObjectParser::MatrixToString(const SkMatrix& matrix) { 96 SkString* str = new SkString("SkMatrix: "); 97 #ifdef SK_DEVELOPER 98 matrix.toString(str); 99 #endif 100 return str; 101 } 102 103 SkString* SkObjectParser::PaintToString(const SkPaint& paint) { 104 SkString* str = new SkString; 105 #ifdef SK_DEVELOPER 106 paint.toString(str); 107 #endif 108 return str; 109 } 110 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 (NULL != boundStr) { 178 mPath->append(*boundStr); 179 SkDELETE(boundStr); 180 } 181 182 return mPath; 183 } 184 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 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 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 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 { 245 SkASSERT(rrect.isComplex()); 246 mRRect->append("complex"); 247 } 248 mRRect->append("): "); 249 } else { 250 mRRect->append(title); 251 } 252 mRRect->append("("); 253 mRRect->appendScalar(rrect.rect().left()); 254 mRRect->append(", "); 255 mRRect->appendScalar(rrect.rect().top()); 256 mRRect->append(", "); 257 mRRect->appendScalar(rrect.rect().right()); 258 mRRect->append(", "); 259 mRRect->appendScalar(rrect.rect().bottom()); 260 mRRect->append(") radii: ("); 261 for (int i = 0; i < 4; ++i) { 262 const SkVector& radii = rrect.radii((SkRRect::Corner) i); 263 mRRect->appendScalar(radii.fX); 264 mRRect->append(", "); 265 mRRect->appendScalar(radii.fY); 266 if (i < 3) { 267 mRRect->append(", "); 268 } 269 } 270 mRRect->append(")"); 271 return mRRect; 272 } 273 274 SkString* SkObjectParser::RegionOpToString(SkRegion::Op op) { 275 SkString* mOp = new SkString("SkRegion::Op: "); 276 if (op == SkRegion::kDifference_Op) { 277 mOp->append("kDifference_Op"); 278 } else if (op == SkRegion::kIntersect_Op) { 279 mOp->append("kIntersect_Op"); 280 } else if (op == SkRegion::kUnion_Op) { 281 mOp->append("kUnion_Op"); 282 } else if (op == SkRegion::kXOR_Op) { 283 mOp->append("kXOR_Op"); 284 } else if (op == SkRegion::kReverseDifference_Op) { 285 mOp->append("kReverseDifference_Op"); 286 } else if (op == SkRegion::kReplace_Op) { 287 mOp->append("kReplace_Op"); 288 } else { 289 mOp->append("Unknown Type"); 290 } 291 return mOp; 292 } 293 294 SkString* SkObjectParser::RegionToString(const SkRegion& region) { 295 SkString* mRegion = new SkString("SkRegion: Data unavailable."); 296 return mRegion; 297 } 298 299 SkString* SkObjectParser::SaveFlagsToString(SkCanvas::SaveFlags flags) { 300 SkString* mFlags = new SkString("SkCanvas::SaveFlags: "); 301 if (flags & SkCanvas::kMatrix_SaveFlag) { 302 mFlags->append("kMatrix_SaveFlag "); 303 } 304 if (flags & SkCanvas::kClip_SaveFlag) { 305 mFlags->append("kClip_SaveFlag "); 306 } 307 if (flags & SkCanvas::kHasAlphaLayer_SaveFlag) { 308 mFlags->append("kHasAlphaLayer_SaveFlag "); 309 } 310 if (flags & SkCanvas::kFullColorLayer_SaveFlag) { 311 mFlags->append("kFullColorLayer_SaveFlag "); 312 } 313 if (flags & SkCanvas::kClipToLayer_SaveFlag) { 314 mFlags->append("kClipToLayer_SaveFlag "); 315 } 316 return mFlags; 317 } 318 319 SkString* SkObjectParser::ScalarToString(SkScalar x, const char* text) { 320 SkString* mScalar = new SkString(text); 321 mScalar->append(" "); 322 mScalar->appendScalar(x); 323 return mScalar; 324 } 325 326 SkString* SkObjectParser::TextToString(const void* text, size_t byteLength, 327 SkPaint::TextEncoding encoding) { 328 329 SkString* decodedText = new SkString(); 330 switch (encoding) { 331 case SkPaint::kUTF8_TextEncoding: { 332 decodedText->append("UTF-8: "); 333 decodedText->append((const char*)text, byteLength); 334 break; 335 } 336 case SkPaint::kUTF16_TextEncoding: { 337 decodedText->append("UTF-16: "); 338 size_t sizeNeeded = SkUTF16_ToUTF8((uint16_t*)text, 339 SkToS32(byteLength / 2), 340 NULL); 341 SkAutoSTMalloc<0x100, char> utf8(sizeNeeded); 342 SkUTF16_ToUTF8((uint16_t*)text, SkToS32(byteLength / 2), utf8); 343 decodedText->append(utf8, sizeNeeded); 344 break; 345 } 346 case SkPaint::kUTF32_TextEncoding: { 347 decodedText->append("UTF-32: "); 348 const SkUnichar* begin = (const SkUnichar*)text; 349 const SkUnichar* end = (const SkUnichar*)((const char*)text + byteLength); 350 for (const SkUnichar* unichar = begin; unichar < end; ++unichar) { 351 decodedText->appendUnichar(*unichar); 352 } 353 break; 354 } 355 case SkPaint::kGlyphID_TextEncoding: { 356 decodedText->append("GlyphID: "); 357 const uint16_t* begin = (const uint16_t*)text; 358 const uint16_t* end = (const uint16_t*)((const char*)text + byteLength); 359 for (const uint16_t* glyph = begin; glyph < end; ++glyph) { 360 decodedText->append("0x"); 361 decodedText->appendHex(*glyph); 362 decodedText->append(" "); 363 } 364 break; 365 } 366 default: 367 decodedText->append("Unknown text encoding."); 368 break; 369 } 370 371 return decodedText; 372 } 373