diff options
author | Hal Canary <halcanary@google.com> | 2018-05-09 14:20:12 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-05-11 17:36:57 +0000 |
commit | 825911444082ddd26e9f8bef349e44e048009f5d (patch) | |
tree | 321dda176993cef91a0bacae3cd7713fd619857c | |
parent | 9bcfcb32de5fcf2f14daa7572455f798c9004025 (diff) |
SkPDF: scale entire canvas for non-standard rasterDpi
TODO: add better testing around this.
BUG: chromium:481429
Change-Id: I1296fae3fc9775febf17bba75722c30f48d67a95
Reviewed-on: https://skia-review.googlesource.com/127051
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
-rw-r--r-- | src/pdf/SkPDFDevice.cpp | 12 | ||||
-rw-r--r-- | src/pdf/SkPDFDocument.cpp | 15 | ||||
-rw-r--r-- | src/pdf/SkPDFShader.cpp | 4 |
3 files changed, 17 insertions, 14 deletions
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp index c18219ab0d..ced94ce9a9 100644 --- a/src/pdf/SkPDFDevice.cpp +++ b/src/pdf/SkPDFDevice.cpp @@ -508,8 +508,9 @@ void SkPDFDevice::setFlip() { // natively has the origin at the bottom left. This matrix // corrects for that. But that only needs to be done once, we // don't do it when layering. - fInitialTransform.setTranslate(0, SkIntToScalar(fPageSize.fHeight)); - fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1); + SkScalar rasterScale = SkPDFUtils::kDpiForRasterScaleOne / fDocument->rasterDpi(); + fInitialTransform.setConcat(SkMatrix::MakeScale(rasterScale, -rasterScale), + SkMatrix::MakeTrans(0, -fPageSize.fHeight)); } SkPDFDevice::~SkPDFDevice() { @@ -797,9 +798,6 @@ void SkPDFDevice::internalDrawPathWithFilter(const SkClipStack& clipStack, : SkStrokeRec::kHairline_InitStyle; path.transform(ctm, &path); - // TODO(halcanary): respect fDocument->rasterDpi(). - // SkScalar rasterScale = (float)rasterDpi / SkPDFUtils::kDpiForRasterScaleOne; - // Would it be easier to just change the device size (and pre-scale the canvas)? SkIRect bounds = clipStack.bounds(this->bounds()).roundOut(); SkMask sourceMask; if (!SkDraw::DrawToMask(path, &bounds, paint->getMaskFilter(), &SkMatrix::I(), @@ -2157,7 +2155,6 @@ void SkPDFDevice::internalDrawImageRect(SkKeyedImage imageSubset, // Rasterize the bitmap using perspective in a new bitmap. if (transform.hasPerspective()) { - SkASSERT(fDocument->rasterDpi() > 0); // Transform the bitmap in the new space, without taking into // account the initial transform. SkPath perspectiveOutline; @@ -2173,9 +2170,6 @@ void SkPDFDevice::internalDrawImageRect(SkKeyedImage imageSubset, // account the initial transform. SkMatrix total = transform; total.postConcat(fInitialTransform); - SkScalar dpiScale = SkIntToScalar(fDocument->rasterDpi()) / - SkIntToScalar(SkPDFUtils::kDpiForRasterScaleOne); - total.postScale(dpiScale, dpiScale); SkPath physicalPerspectiveOutline; physicalPerspectiveOutline.addRect(imageBounds); diff --git a/src/pdf/SkPDFDocument.cpp b/src/pdf/SkPDFDocument.cpp index b06b8b1a96..8219ad385e 100644 --- a/src/pdf/SkPDFDocument.cpp +++ b/src/pdf/SkPDFDocument.cpp @@ -207,11 +207,14 @@ SkCanvas* SkPDFDocument::onBeginPage(SkScalar width, SkScalar height) { fObjectSerializer.serializeObjects(this->getStream()); } } - SkISize pageSize = SkISize::Make( - SkScalarRoundToInt(width), SkScalarRoundToInt(height)); + SkScalar rasterScale = this->rasterDpi() / SkPDFUtils::kDpiForRasterScaleOne; + SkISize pageSize = {SkScalarRoundToInt(width * rasterScale), + SkScalarRoundToInt(height * rasterScale)}; + fPageDevice = sk_make_sp<SkPDFDevice>(pageSize, this); fPageDevice->setFlip(); // Only the top-level device needs to be flipped. fCanvas.reset(new SkCanvas(fPageDevice)); + fCanvas->scale(rasterScale, rasterScale); return fCanvas.get(); } @@ -222,7 +225,13 @@ void SkPDFDocument::onEndPage() { SkASSERT(fPageDevice); auto page = sk_make_sp<SkPDFDict>("Page"); page->insertObject("Resources", fPageDevice->makeResourceDict()); - page->insertObject("MediaBox", fPageDevice->copyMediaBox()); + + SkScalar rasterScale = SkPDFUtils::kDpiForRasterScaleOne / this->rasterDpi(); + + SkISize pageSize = fPageDevice->imageInfo().dimensions(); + page->insertObject("MediaBox", SkPDFUtils::RectToArray( + {0, 0, pageSize.width() * rasterScale, pageSize.height() * rasterScale})); + auto annotations = sk_make_sp<SkPDFArray>(); fPageDevice->appendAnnotations(annotations.get()); if (annotations->size() > 0) { diff --git a/src/pdf/SkPDFShader.cpp b/src/pdf/SkPDFShader.cpp index 63611d43d5..0f1fa4da15 100644 --- a/src/pdf/SkPDFShader.cpp +++ b/src/pdf/SkPDFShader.cpp @@ -283,8 +283,8 @@ static sk_sp<SkPDFObject> make_fallback_shader(SkPDFDocument* doc, } // Clamp the bitmap size to about 1M pixels static const SkScalar kMaxBitmapArea = 1024 * 1024; - SkScalar rasterScale = SkIntToScalar(doc->rasterDpi()) / SkPDFUtils::kDpiForRasterScaleOne; - SkScalar bitmapArea = rasterScale * surfaceBBox.width() * rasterScale * surfaceBBox.height(); + SkScalar bitmapArea = surfaceBBox.width() * surfaceBBox.height(); + SkScalar rasterScale = 1.0f; if (bitmapArea > kMaxBitmapArea) { rasterScale *= SkScalarSqrt(kMaxBitmapArea / bitmapArea); } |