1 /* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "SkObjectParser.h" 9 #include "SkData.h" 10 #include "SkFontDescriptor.h" 11 #include "SkImage.h" 12 #include "SkPath.h" 13 #include "SkRRect.h" 14 #include "SkShader.h" 15 #include "SkStream.h" 16 #include "SkStringUtils.h" 17 #include "SkTypeface.h" 18 #include "SkUtils.h" 19 #include "SkClipOpPriv.h" 20 21 /* TODO(chudy): Replace all std::strings with char */ 22 23 SkString* SkObjectParser::BitmapToString(const SkBitmap& bitmap) { 24 SkString* mBitmap = new SkString("SkBitmap: "); 25 mBitmap->append("W: "); 26 mBitmap->appendS32(bitmap.width()); 27 mBitmap->append(" H: "); 28 mBitmap->appendS32(bitmap.height()); 29 30 const char* ctString = "<unknown>"; 31 switch (bitmap.colorType()) { 32 case kUnknown_SkColorType: ctString = "None"; break; 33 case kAlpha_8_SkColorType: ctString = "A8"; break; 34 case kRGB_565_SkColorType: ctString = "565"; break; 35 case kARGB_4444_SkColorType: ctString = "4444"; break; 36 case kRGBA_8888_SkColorType: ctString = "RGBA"; break; 37 case kRGB_888x_SkColorType: ctString = "RGB"; break; 38 case kBGRA_8888_SkColorType: ctString = "BGRA"; break; 39 case kRGBA_1010102_SkColorType: ctString = "1010102"; break; 40 case kRGB_101010x_SkColorType: ctString = "101010x"; break; 41 case kGray_8_SkColorType: ctString = "G8"; break; 42 case kRGBA_F16_SkColorType: ctString = "RGBAf16"; break; 43 } 44 mBitmap->append(" ColorType: "); 45 mBitmap->append(ctString); 46 47 if (bitmap.isOpaque()) { 48 mBitmap->append(" opaque"); 49 } else { 50 mBitmap->append(" not-opaque"); 51 } 52 53 if (bitmap.isImmutable()) { 54 mBitmap->append(" immutable"); 55 } else { 56 mBitmap->append(" not-immutable"); 57 } 58 59 if (bitmap.isVolatile()) { 60 mBitmap->append(" volatile"); 61 } else { 62 mBitmap->append(" not-volatile"); 63 } 64 65 mBitmap->append(" genID: "); 66 mBitmap->appendS32(bitmap.getGenerationID()); 67 68 return mBitmap; 69 } 70 71 SkString* SkObjectParser::ImageToString(const SkImage* image) { 72 SkString* str = new SkString("SkImage: "); 73 if (!image) { 74 return str; 75 } 76 77 str->append("W: "); 78 str->appendS32(image->width()); 79 str->append(" H: "); 80 str->appendS32(image->height()); 81 82 if (image->isOpaque()) { 83 str->append(" opaque"); 84 } else { 85 str->append(" not-opaque"); 86 } 87 88 str->append(" uniqueID: "); 89 str->appendS32(image->uniqueID()); 90 91 return str; 92 } 93 94 SkString* SkObjectParser::BoolToString(bool doAA) { 95 SkString* mBool = new SkString("Bool doAA: "); 96 if (doAA) { 97 mBool->append("True"); 98 } else { 99 mBool->append("False"); 100 } 101 return mBool; 102 } 103 104 SkString* SkObjectParser::CustomTextToString(const char* text) { 105 SkString* mText = new SkString(text); 106 return mText; 107 } 108 109 SkString* SkObjectParser::IntToString(int x, const char* text) { 110 SkString* mInt = new SkString(text); 111 mInt->append(" "); 112 mInt->appendScalar(SkIntToScalar(x)); 113 return mInt; 114 } 115 116 SkString* SkObjectParser::IRectToString(const SkIRect& rect) { 117 SkString* mRect = new SkString("SkIRect: "); 118 mRect->append("L: "); 119 mRect->appendS32(rect.left()); 120 mRect->append(", T: "); 121 mRect->appendS32(rect.top()); 122 mRect->append(", R: "); 123 mRect->appendS32(rect.right()); 124 mRect->append(", B: "); 125 mRect->appendS32(rect.bottom()); 126 return mRect; 127 } 128 129 SkString* SkObjectParser::MatrixToString(const SkMatrix& matrix) { 130 SkString* str = new SkString("SkMatrix: "); 131 #ifndef SK_IGNORE_TO_STRING 132 matrix.toString(str); 133 #endif 134 return str; 135 } 136 137 SkString* SkObjectParser::PaintToString(const SkPaint& paint) { 138 SkString* str = new SkString; 139 #ifndef SK_IGNORE_TO_STRING 140 paint.toString(str); 141 #endif 142 return str; 143 } 144 145 SkString* SkObjectParser::PathToString(const SkPath& path) { 146 SkString* mPath = new SkString; 147 148 mPath->appendf("Path (%d) (", path.getGenerationID()); 149 150 static const char* gFillStrings[] = { 151 "Winding", "EvenOdd", "InverseWinding", "InverseEvenOdd" 152 }; 153 154 mPath->append(gFillStrings[path.getFillType()]); 155 mPath->append(", "); 156 157 static const char* gConvexityStrings[] = { 158 "Unknown", "Convex", "Concave" 159 }; 160 SkASSERT(SkPath::kConcave_Convexity == 2); 161 162 mPath->append(gConvexityStrings[path.getConvexity()]); 163 mPath->append(", "); 164 165 if (path.isRect(nullptr)) { 166 mPath->append("isRect, "); 167 } else { 168 mPath->append("isNotRect, "); 169 } 170 171 if (path.isOval(nullptr)) { 172 mPath->append("isOval, "); 173 } else { 174 mPath->append("isNotOval, "); 175 } 176 177 SkRRect rrect; 178 if (path.isRRect(&rrect)) { 179 mPath->append("isRRect, "); 180 } else { 181 mPath->append("isNotRRect, "); 182 } 183 184 mPath->appendS32(path.countVerbs()); 185 mPath->append("V, "); 186 mPath->appendS32(path.countPoints()); 187 mPath->append("P): "); 188 189 static const char* gVerbStrings[] = { 190 "Move", "Line", "Quad", "Conic", "Cubic", "Close", "Done" 191 }; 192 static const int gPtsPerVerb[] = { 1, 1, 2, 2, 3, 0, 0 }; 193 static const int gPtOffsetPerVerb[] = { 0, 1, 1, 1, 1, 0, 0 }; 194 SkASSERT(SkPath::kDone_Verb == 6); 195 196 SkPath::Iter iter(const_cast<SkPath&>(path), false); 197 SkPath::Verb verb; 198 SkPoint points[4]; 199 200 for(verb = iter.next(points, false); 201 verb != SkPath::kDone_Verb; 202 verb = iter.next(points, false)) { 203 204 mPath->append(gVerbStrings[verb]); 205 mPath->append(" "); 206 207 for (int i = 0; i < gPtsPerVerb[verb]; ++i) { 208 mPath->append("("); 209 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fX); 210 mPath->append(", "); 211 mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fY); 212 mPath->append(")"); 213 } 214 215 if (SkPath::kConic_Verb == verb) { 216 mPath->append("("); 217 mPath->appendScalar(iter.conicWeight()); 218 mPath->append(")"); 219 } 220 221 mPath->append(" "); 222 } 223 224 SkString* boundStr = SkObjectParser::RectToString(path.getBounds(), " Bound: "); 225 226 if (boundStr) { 227 mPath->append(*boundStr); 228 delete boundStr; 229 } 230 231 return mPath; 232 } 233 234 SkString* SkObjectParser::PointsToString(const SkPoint pts[], size_t count) { 235 SkString* mPoints = new SkString("SkPoints pts[]: "); 236 for (unsigned int i = 0; i < count; i++) { 237 mPoints->append("("); 238 mPoints->appendScalar(pts[i].fX); 239 mPoints->append(","); 240 mPoints->appendScalar(pts[i].fY); 241 mPoints->append(")"); 242 } 243 return mPoints; 244 } 245 246 SkString* SkObjectParser::PointModeToString(SkCanvas::PointMode mode) { 247 SkString* mMode = new SkString("SkCanvas::PointMode: "); 248 if (mode == SkCanvas::kPoints_PointMode) { 249 mMode->append("kPoints_PointMode"); 250 } else if (mode == SkCanvas::kLines_PointMode) { 251 mMode->append("kLines_Mode"); 252 } else if (mode == SkCanvas::kPolygon_PointMode) { 253 mMode->append("kPolygon_PointMode"); 254 } 255 return mMode; 256 } 257 258 SkString* SkObjectParser::RectToString(const SkRect& rect, const char* title) { 259 260 SkString* mRect = new SkString; 261 262 if (nullptr == title) { 263 mRect->append("SkRect: "); 264 } else { 265 mRect->append(title); 266 } 267 mRect->append("("); 268 mRect->appendScalar(rect.left()); 269 mRect->append(", "); 270 mRect->appendScalar(rect.top()); 271 mRect->append(", "); 272 mRect->appendScalar(rect.right()); 273 mRect->append(", "); 274 mRect->appendScalar(rect.bottom()); 275 mRect->append(")"); 276 return mRect; 277 } 278 279 SkString* SkObjectParser::RRectToString(const SkRRect& rrect, const char* title) { 280 281 SkString* mRRect = new SkString; 282 283 if (nullptr == title) { 284 mRRect->append("SkRRect ("); 285 if (rrect.isEmpty()) { 286 mRRect->append("empty"); 287 } else if (rrect.isRect()) { 288 mRRect->append("rect"); 289 } else if (rrect.isOval()) { 290 mRRect->append("oval"); 291 } else if (rrect.isSimple()) { 292 mRRect->append("simple"); 293 } else if (rrect.isNinePatch()) { 294 mRRect->append("nine-patch"); 295 } else { 296 SkASSERT(rrect.isComplex()); 297 mRRect->append("complex"); 298 } 299 mRRect->append("): "); 300 } else { 301 mRRect->append(title); 302 } 303 mRRect->append("("); 304 mRRect->appendScalar(rrect.rect().left()); 305 mRRect->append(", "); 306 mRRect->appendScalar(rrect.rect().top()); 307 mRRect->append(", "); 308 mRRect->appendScalar(rrect.rect().right()); 309 mRRect->append(", "); 310 mRRect->appendScalar(rrect.rect().bottom()); 311 mRRect->append(") radii: ("); 312 for (int i = 0; i < 4; ++i) { 313 const SkVector& radii = rrect.radii((SkRRect::Corner) i); 314 mRRect->appendScalar(radii.fX); 315 mRRect->append(", "); 316 mRRect->appendScalar(radii.fY); 317 if (i < 3) { 318 mRRect->append(", "); 319 } 320 } 321 mRRect->append(")"); 322 return mRRect; 323 } 324 325 SkString* SkObjectParser::ClipOpToString(SkClipOp op) { 326 SkString* mOp = new SkString("SkRegion::Op: "); 327 if (op == kDifference_SkClipOp) { 328 mOp->append("kDifference_Op"); 329 } else if (op == kIntersect_SkClipOp) { 330 mOp->append("kIntersect_Op"); 331 } else if (op == kUnion_SkClipOp) { 332 mOp->append("kUnion_Op"); 333 } else if (op == kXOR_SkClipOp) { 334 mOp->append("kXOR_Op"); 335 } else if (op == kReverseDifference_SkClipOp) { 336 mOp->append("kReverseDifference_Op"); 337 } else if (op == kReplace_SkClipOp) { 338 mOp->append("kReplace_Op"); 339 } else { 340 mOp->append("Unknown Type"); 341 } 342 return mOp; 343 } 344 345 SkString* SkObjectParser::RegionToString(const SkRegion& region) { 346 SkString* mRegion = new SkString("SkRegion: Data unavailable."); 347 return mRegion; 348 } 349 350 SkString* SkObjectParser::SaveLayerFlagsToString(SkCanvas::SaveLayerFlags saveLayerFlags) { 351 SkString* mFlags = new SkString("SkCanvas::SaveFlags: "); 352 if (saveLayerFlags & SkCanvas::kIsOpaque_SaveLayerFlag) { 353 mFlags->append("kIsOpaque_SaveLayerFlag "); 354 } 355 if (saveLayerFlags & SkCanvas::kPreserveLCDText_SaveLayerFlag) { 356 mFlags->append("kPreserveLCDText_SaveLayerFlag "); 357 } 358 return mFlags; 359 } 360 361 SkString* SkObjectParser::ScalarToString(SkScalar x, const char* text) { 362 SkString* mScalar = new SkString(text); 363 mScalar->append(" "); 364 mScalar->appendScalar(x); 365 return mScalar; 366 } 367 368 SkString* SkObjectParser::TextToString(const void* text, size_t byteLength, 369 SkPaint::TextEncoding encoding) { 370 371 SkString* decodedText = new SkString(); 372 switch (encoding) { 373 case SkPaint::kUTF8_TextEncoding: { 374 decodedText->append("UTF-8: "); 375 decodedText->append((const char*)text, byteLength); 376 break; 377 } 378 case SkPaint::kUTF16_TextEncoding: { 379 decodedText->append("UTF-16: "); 380 size_t sizeNeeded = SkUTF16_ToUTF8((uint16_t*)text, 381 SkToS32(byteLength / 2), 382 nullptr); 383 SkAutoSTMalloc<0x100, char> utf8(sizeNeeded); 384 SkUTF16_ToUTF8((uint16_t*)text, SkToS32(byteLength / 2), utf8); 385 decodedText->append(utf8, sizeNeeded); 386 break; 387 } 388 case SkPaint::kUTF32_TextEncoding: { 389 decodedText->append("UTF-32: "); 390 const SkUnichar* begin = (const SkUnichar*)text; 391 const SkUnichar* end = (const SkUnichar*)((const char*)text + byteLength); 392 for (const SkUnichar* unichar = begin; unichar < end; ++unichar) { 393 decodedText->appendUnichar(*unichar); 394 } 395 break; 396 } 397 case SkPaint::kGlyphID_TextEncoding: { 398 decodedText->append("GlyphID: "); 399 const uint16_t* begin = (const uint16_t*)text; 400 const uint16_t* end = (const uint16_t*)((const char*)text + byteLength); 401 for (const uint16_t* glyph = begin; glyph < end; ++glyph) { 402 decodedText->append("0x"); 403 decodedText->appendHex(*glyph); 404 decodedText->append(" "); 405 } 406 break; 407 } 408 default: 409 decodedText->append("Unknown text encoding."); 410 break; 411 } 412 413 return decodedText; 414 } 415 416 SkString* SkObjectParser::LatticeToString(const SkCanvas::Lattice& lattice) { 417 SkString* mLattice = new SkString; 418 mLattice->append("Lattice: "); 419 mLattice->append("(X: "); 420 mLattice->appendS32(lattice.fXCount); 421 mLattice->append(", Y:"); 422 mLattice->appendS32(lattice.fYCount); 423 mLattice->append(", Bounds:"); 424 if (nullptr != lattice.fBounds) { 425 mLattice->append(*IRectToString(*lattice.fBounds)); 426 } else { 427 mLattice->append("null"); 428 } 429 mLattice->append(")"); 430 return mLattice; 431 } 432