void onDraw(SkCanvas* canvas) override { static const struct { SkFilterQuality fQuality; const char* fName; } kQualities[] = { {kNone_SkFilterQuality, "none"}, {kLow_SkFilterQuality, "low"}, {kMedium_SkFilterQuality, "medium"}, {kHigh_SkFilterQuality, "high"}, }; for (size_t q = 0; q < SK_ARRAY_COUNT(kQualities); ++q) { SkPaint paint; sk_tool_utils::set_portable_typeface(&paint); paint.setFilterQuality(kQualities[q].fQuality); SkPaint bmpPaint(paint); SkMatrix lm = SkMatrix::I(); lm.setScaleX(2.5); lm.setTranslateX(423); lm.setTranslateY(330); static const SkShader::TileMode kTM = SkShader::kRepeat_TileMode; bmpPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTM, kTM, &lm)); SkRect rect = SkRect::MakeLTRB(20, 60, 220, 210); canvas->drawRect(rect, bmpPaint); paint.setAntiAlias(true); canvas->drawText(kQualities[q].fName, strlen(kQualities[q].fName), 20, 40, paint); canvas->translate(250, 0); } }
static Sink* create_via(const char* tag, Sink* wrapped) { #define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); } VIA("twice", ViaTwice, wrapped); VIA("pipe", ViaPipe, wrapped); VIA("serialize", ViaSerialization, wrapped); VIA("2ndpic", ViaSecondPicture, wrapped); VIA("sp", ViaSingletonPictures, wrapped); VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped); VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped); if (FLAGS_matrix.count() == 4) { SkMatrix m; m.reset(); m.setScaleX((SkScalar)atof(FLAGS_matrix[0])); m.setSkewX ((SkScalar)atof(FLAGS_matrix[1])); m.setSkewY ((SkScalar)atof(FLAGS_matrix[2])); m.setScaleY((SkScalar)atof(FLAGS_matrix[3])); VIA("matrix", ViaMatrix, m, wrapped); VIA("upright", ViaUpright, m, wrapped); } #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK VIA("androidsdk", ViaAndroidSDK, wrapped); #endif #undef VIA return nullptr; }
void onOnceBeforeDraw() override { fPath.moveTo(69.7030518991886f, 0); fPath.cubicTo( 69.7030518991886f, 21.831149999999997f, 58.08369508178456f, 43.66448333333333f, 34.8449814469765f, 65.5f); fPath.cubicTo( 11.608591683531916f, 87.33115f, -0.010765133872116195f, 109.16448333333332f, -0.013089005235602302f, 131); fPath.close(); fFlipped = fPath; SkMatrix matrix; matrix.reset(); matrix.setScaleX(0); matrix.setScaleY(0); matrix.setSkewX(1); matrix.setSkewY(1); fFlipped.transform(matrix); }
AffineTransform::operator SkMatrix() const { SkMatrix result; result.setScaleX(WebCoreDoubleToSkScalar(a())); result.setSkewX(WebCoreDoubleToSkScalar(c())); result.setTranslateX(WebCoreDoubleToSkScalar(e())); result.setScaleY(WebCoreDoubleToSkScalar(d())); result.setSkewY(WebCoreDoubleToSkScalar(b())); result.setTranslateY(WebCoreDoubleToSkScalar(f())); // FIXME: Set perspective properly. result.setPerspX(0); result.setPerspY(0); result.set(SkMatrix::kMPersp2, SK_Scalar1); return result; }
SkMatrix affineTransformToSkMatrix(const AffineTransform& source) { SkMatrix result; result.setScaleX(WebCoreDoubleToSkScalar(source.a())); result.setSkewX(WebCoreDoubleToSkScalar(source.c())); result.setTranslateX(WebCoreDoubleToSkScalar(source.e())); result.setScaleY(WebCoreDoubleToSkScalar(source.d())); result.setSkewY(WebCoreDoubleToSkScalar(source.b())); result.setTranslateY(WebCoreDoubleToSkScalar(source.f())); // FIXME: Set perspective properly. result.setPerspX(0); result.setPerspY(0); result.set(SkMatrix::kMPersp2, SK_Scalar1); return result; }
void drawAll(SkCanvas* canvas, SkScalar scaleX) const { constexpr struct { SkFilterQuality fQuality; const char* fName; } kQualities[] = { {kNone_SkFilterQuality, "none"}, {kLow_SkFilterQuality, "low"}, {kMedium_SkFilterQuality, "medium"}, {kHigh_SkFilterQuality, "high"}, }; SkRect rect = SkRect::MakeLTRB(20, 60, 220, 210); SkMatrix lm = SkMatrix::I(); lm.setScaleX(scaleX); lm.setTranslateX(423); lm.setTranslateY(330); SkPaint textPaint; textPaint.setAntiAlias(true); SkPaint bmpPaint(textPaint); SkFont font(ToolUtils::create_portable_typeface()); SkAutoCanvasRestore acr(canvas, true); for (size_t q = 0; q < SK_ARRAY_COUNT(kQualities); ++q) { constexpr SkTileMode kTM = SkTileMode::kRepeat; bmpPaint.setShader(fBmp.makeShader(kTM, kTM, &lm)); bmpPaint.setFilterQuality(kQualities[q].fQuality); canvas->drawRect(rect, bmpPaint); canvas->drawString(kQualities[q].fName, 20, 40, font, textPaint); canvas->translate(250, 0); } }
SkMatrix makeMatrix() { SkMatrix matrix; matrix.reset(); RandomSetMatrix setMatrix = (RandomSetMatrix) fRand.nextRangeU(0, kRandomSetMatrix_Last); if (fPrintName) { SkDebugf("%.*s%s\n", fPathDepth * 3, fTab, gRandomSetMatrixNames[setMatrix]); } switch (setMatrix) { case kSetIdentity: break; case kSetTranslateX: matrix.setTranslateX(makeScalar()); break; case kSetTranslateY: matrix.setTranslateY(makeScalar()); break; case kSetTranslate: matrix.setTranslate(makeScalar(), makeScalar()); break; case kSetScaleX: matrix.setScaleX(makeScalar()); break; case kSetScaleY: matrix.setScaleY(makeScalar()); break; case kSetScale: matrix.setScale(makeScalar(), makeScalar()); break; case kSetScaleTranslate: matrix.setScale(makeScalar(), makeScalar(), makeScalar(), makeScalar()); break; case kSetSkewX: matrix.setSkewX(makeScalar()); break; case kSetSkewY: matrix.setSkewY(makeScalar()); break; case kSetSkew: matrix.setSkew(makeScalar(), makeScalar()); break; case kSetSkewTranslate: matrix.setSkew(makeScalar(), makeScalar(), makeScalar(), makeScalar()); break; case kSetRotate: matrix.setRotate(makeScalar()); break; case kSetRotateTranslate: matrix.setRotate(makeScalar(), makeScalar(), makeScalar()); break; case kSetPerspectiveX: matrix.setPerspX(makeScalar()); break; case kSetPerspectiveY: matrix.setPerspY(makeScalar()); break; case kSetAll: matrix.setAll(makeScalar(), makeScalar(), makeScalar(), makeScalar(), makeScalar(), makeScalar(), makeScalar(), makeScalar(), makeScalar()); break; } return matrix; }
// Called to test various transforms on a single SkRRect. static void test_transform_helper(skiatest::Reporter* reporter, const SkRRect& orig) { SkRRect dst; dst.setEmpty(); // The identity matrix will duplicate the rrect. bool success = orig.transform(SkMatrix::I(), &dst); REPORTER_ASSERT(reporter, success); REPORTER_ASSERT(reporter, orig == dst); // Skew and Perspective make transform fail. SkMatrix matrix; matrix.reset(); matrix.setSkewX(SkIntToScalar(2)); assert_transform_failure(reporter, orig, matrix); matrix.reset(); matrix.setSkewY(SkIntToScalar(3)); assert_transform_failure(reporter, orig, matrix); matrix.reset(); matrix.setPerspX(4); assert_transform_failure(reporter, orig, matrix); matrix.reset(); matrix.setPerspY(5); assert_transform_failure(reporter, orig, matrix); // Rotation fails. matrix.reset(); matrix.setRotate(SkIntToScalar(90)); assert_transform_failure(reporter, orig, matrix); matrix.setRotate(SkIntToScalar(37)); assert_transform_failure(reporter, orig, matrix); // Translate will keep the rect moved, but otherwise the same. matrix.reset(); SkScalar translateX = SkIntToScalar(32); SkScalar translateY = SkIntToScalar(15); matrix.setTranslateX(translateX); matrix.setTranslateY(translateY); dst.setEmpty(); success = orig.transform(matrix, &dst); REPORTER_ASSERT(reporter, success); for (int i = 0; i < 4; ++i) { REPORTER_ASSERT(reporter, orig.radii((SkRRect::Corner) i) == dst.radii((SkRRect::Corner) i)); } REPORTER_ASSERT(reporter, orig.rect().width() == dst.rect().width()); REPORTER_ASSERT(reporter, orig.rect().height() == dst.rect().height()); REPORTER_ASSERT(reporter, dst.rect().left() == orig.rect().left() + translateX); REPORTER_ASSERT(reporter, dst.rect().top() == orig.rect().top() + translateY); // Keeping the translation, but adding skew will make transform fail. matrix.setSkewY(SkIntToScalar(7)); assert_transform_failure(reporter, orig, matrix); // Scaling in -x will flip the round rect horizontally. matrix.reset(); matrix.setScaleX(SkIntToScalar(-1)); dst.setEmpty(); success = orig.transform(matrix, &dst); REPORTER_ASSERT(reporter, success); { GET_RADII; // Radii have swapped in x. REPORTER_ASSERT(reporter, origUL == dstUR); REPORTER_ASSERT(reporter, origUR == dstUL); REPORTER_ASSERT(reporter, origLR == dstLL); REPORTER_ASSERT(reporter, origLL == dstLR); } // Width and height remain the same. REPORTER_ASSERT(reporter, orig.rect().width() == dst.rect().width()); REPORTER_ASSERT(reporter, orig.rect().height() == dst.rect().height()); // Right and left have swapped (sort of) REPORTER_ASSERT(reporter, orig.rect().right() == -dst.rect().left()); // Top has stayed the same. REPORTER_ASSERT(reporter, orig.rect().top() == dst.rect().top()); // Keeping the scale, but adding a persp will make transform fail. matrix.setPerspX(7); assert_transform_failure(reporter, orig, matrix); // Scaling in -y will flip the round rect vertically. matrix.reset(); matrix.setScaleY(SkIntToScalar(-1)); dst.setEmpty(); success = orig.transform(matrix, &dst); REPORTER_ASSERT(reporter, success); { GET_RADII; // Radii have swapped in y. REPORTER_ASSERT(reporter, origUL == dstLL); REPORTER_ASSERT(reporter, origUR == dstLR); REPORTER_ASSERT(reporter, origLR == dstUR); REPORTER_ASSERT(reporter, origLL == dstUL); } // Width and height remain the same. REPORTER_ASSERT(reporter, orig.rect().width() == dst.rect().width()); REPORTER_ASSERT(reporter, orig.rect().height() == dst.rect().height()); // Top and bottom have swapped (sort of) REPORTER_ASSERT(reporter, orig.rect().top() == -dst.rect().bottom()); // Left has stayed the same. REPORTER_ASSERT(reporter, orig.rect().left() == dst.rect().left()); // Scaling in -x and -y will swap in both directions. matrix.reset(); matrix.setScaleY(SkIntToScalar(-1)); matrix.setScaleX(SkIntToScalar(-1)); dst.setEmpty(); success = orig.transform(matrix, &dst); REPORTER_ASSERT(reporter, success); { GET_RADII; REPORTER_ASSERT(reporter, origUL == dstLR); REPORTER_ASSERT(reporter, origUR == dstLL); REPORTER_ASSERT(reporter, origLR == dstUL); REPORTER_ASSERT(reporter, origLL == dstUR); } // Width and height remain the same. REPORTER_ASSERT(reporter, orig.rect().width() == dst.rect().width()); REPORTER_ASSERT(reporter, orig.rect().height() == dst.rect().height()); REPORTER_ASSERT(reporter, orig.rect().top() == -dst.rect().bottom()); REPORTER_ASSERT(reporter, orig.rect().right() == -dst.rect().left()); // Scale in both directions. SkScalar xScale = SkIntToScalar(3); SkScalar yScale = 3.2f; matrix.reset(); matrix.setScaleX(xScale); matrix.setScaleY(yScale); dst.setEmpty(); success = orig.transform(matrix, &dst); REPORTER_ASSERT(reporter, success); // Radii are scaled. for (int i = 0; i < 4; ++i) { REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.radii((SkRRect::Corner) i).fX, SkScalarMul(orig.radii((SkRRect::Corner) i).fX, xScale))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.radii((SkRRect::Corner) i).fY, SkScalarMul(orig.radii((SkRRect::Corner) i).fY, yScale))); } REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.rect().width(), SkScalarMul(orig.rect().width(), xScale))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.rect().height(), SkScalarMul(orig.rect().height(), yScale))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.rect().left(), SkScalarMul(orig.rect().left(), xScale))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.rect().top(), SkScalarMul(orig.rect().top(), yScale))); }
void SkSVGPaint::setSave(SkSVGParser& parser) { SkTDArray<SkString*> clips; SkSVGPaint* walking = parser.fHead; int index; SkMatrix sum; sum.reset(); while (walking != NULL) { for (index = kInitial + 1; index < kTerminal; index++) { SkString* lastAttr = (*walking)[index]; if (lastAttr->size() == 0) continue; if (index == kTransform) { const char* str = lastAttr->c_str(); SkASSERT(strncmp(str, "matrix(", 7) == 0); str += 6; const char* strEnd = strrchr(str, ')'); SkASSERT(strEnd != NULL); SkString mat(str, strEnd - str); SkSVGParser::ConvertToArray(mat); SkScalar values[6]; SkParse::FindScalars(mat.c_str() + 1, values, 6); SkMatrix matrix; matrix.reset(); matrix.setScaleX(values[0]); matrix.setSkewY(values[1]); matrix.setSkewX(values[2]); matrix.setScaleY(values[3]); matrix.setTranslateX(values[4]); matrix.setTranslateY(values[5]); sum.setConcat(matrix, sum); continue; } if ( index == kClipPath) *clips.insert(0) = lastAttr; } walking = walking->fNext; } if ((sum == parser.fLastTransform) == false) { SkMatrix inverse; bool success = parser.fLastTransform.invert(&inverse); SkASSERT(success == true); SkMatrix output; output.setConcat(inverse, sum); parser.fLastTransform = sum; SkString outputStr; outputStr.appendUnichar('['); outputStr.appendScalar(output.getScaleX()); outputStr.appendUnichar(','); outputStr.appendScalar(output.getSkewX()); outputStr.appendUnichar(','); outputStr.appendScalar(output.getTranslateX()); outputStr.appendUnichar(','); outputStr.appendScalar(output.getSkewY()); outputStr.appendUnichar(','); outputStr.appendScalar(output.getScaleY()); outputStr.appendUnichar(','); outputStr.appendScalar(output.getTranslateY()); outputStr.appendUnichar(','); outputStr.appendScalar(output.getPerspX()); outputStr.appendUnichar(','); outputStr.appendScalar(output.getPerspY()); outputStr.append(",1]"); parser._startElement("matrix"); parser._addAttributeLen("matrix", outputStr.c_str(), outputStr.size()); parser._endElement(); } #if 0 // incomplete if (parser.fTransformClips.size() > 0) { // need to reset the clip when the 'g' scope is ended parser._startElement("add"); const char* start = strchr(current->f_clipPath.c_str(), '#') + 1; SkASSERT(start); parser._addAttributeLen("use", start, strlen(start) - 1); parser._endElement(); // clip } #endif }