SkDocument: consolidate trimBox code
Change-Id: I56f651320964eb3bd33475d21e6977803c4ca140
Reviewed-on: https://skia-review.googlesource.com/21370
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
diff --git a/include/core/SkDocument.h b/include/core/SkDocument.h
index 9204243..0d9c23b 100644
--- a/include/core/SkDocument.h
+++ b/include/core/SkDocument.h
@@ -208,8 +208,7 @@
// cannot do this for them.
virtual ~SkDocument();
- virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height,
- const SkRect& content) = 0;
+ virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height) = 0;
virtual void onEndPage() = 0;
virtual void onClose(SkWStream*) = 0;
virtual void onAbort() = 0;
diff --git a/src/core/SkDocument.cpp b/src/core/SkDocument.cpp
index 29db7f0..006be91 100644
--- a/src/core/SkDocument.cpp
+++ b/src/core/SkDocument.cpp
@@ -5,6 +5,7 @@
* found in the LICENSE file.
*/
+#include "SkCanvas.h"
#include "SkDocument.h"
#include "SkStream.h"
@@ -37,9 +38,15 @@
for (;;) {
switch (fState) {
- case kBetweenPages_State:
+ case kBetweenPages_State: {
fState = kInPage_State;
- return this->onBeginPage(width, height, inner);
+ SkCanvas* canvas = this->onBeginPage(width, height);
+ if (content) {
+ canvas->clipRect(inner);
+ canvas->translate(inner.x(), inner.y());
+ }
+ return canvas;
+ }
case kInPage_State:
this->endPage();
break;
diff --git a/src/pdf/SkPDFDocument.cpp b/src/pdf/SkPDFDocument.cpp
index 96a79ae..ee00965 100644
--- a/src/pdf/SkPDFDocument.cpp
+++ b/src/pdf/SkPDFDocument.cpp
@@ -193,8 +193,7 @@
fObjectSerializer.serializeObjects(this->getStream());
}
-SkCanvas* SkPDFDocument::onBeginPage(SkScalar width, SkScalar height,
- const SkRect& trimBox) {
+SkCanvas* SkPDFDocument::onBeginPage(SkScalar width, SkScalar height) {
SkASSERT(!fCanvas.get()); // endPage() was called before this.
if (fPages.empty()) {
// if this is the first page if the document.
@@ -218,10 +217,6 @@
fPageDevice = sk_make_sp<SkPDFDevice>(pageSize, this);
fPageDevice->setFlip(); // Only the top-level device needs to be flipped.
fCanvas.reset(new SkPDFCanvas(fPageDevice));
- if (SkRect::MakeWH(width, height) != trimBox) {
- fCanvas->clipRect(trimBox);
- fCanvas->translate(trimBox.x(), trimBox.y());
- }
return fCanvas.get();
}
diff --git a/src/pdf/SkPDFDocument.h b/src/pdf/SkPDFDocument.h
index 7f8b6c8..3e3a50a 100644
--- a/src/pdf/SkPDFDocument.h
+++ b/src/pdf/SkPDFDocument.h
@@ -61,7 +61,7 @@
sk_sp<SkPixelSerializer>,
bool);
~SkPDFDocument() override;
- SkCanvas* onBeginPage(SkScalar, SkScalar, const SkRect&) override;
+ SkCanvas* onBeginPage(SkScalar, SkScalar) override;
void onEndPage() override;
void onClose(SkWStream*) override;
void onAbort() override;
diff --git a/src/utils/SkMultiPictureDocument.cpp b/src/utils/SkMultiPictureDocument.cpp
index 9868ca2..4ff2d07 100644
--- a/src/utils/SkMultiPictureDocument.cpp
+++ b/src/utils/SkMultiPictureDocument.cpp
@@ -44,19 +44,6 @@
return joined;
}
-static SkCanvas* trim(SkCanvas* canvas,
- SkScalar w, SkScalar h,
- const SkRect& trimBox) {
- // Only trim if necessary.
- if (trimBox != SkRect::MakeWH(w, h)) {
- // All SkDocument implementations implement trimBox using a
- // clip+translate.
- canvas->clipRect(trimBox);
- canvas->translate(trimBox.x(), trimBox.y());
- }
- return canvas;
-}
-
struct MultiPictureDocument final : public SkDocument {
SkPictureRecorder fPictureRecorder;
SkSize fCurrentPageSize;
@@ -66,9 +53,9 @@
: SkDocument(s, d) {}
~MultiPictureDocument() override { this->close(); }
- SkCanvas* onBeginPage(SkScalar w, SkScalar h, const SkRect& c) override {
+ SkCanvas* onBeginPage(SkScalar w, SkScalar h) override {
fCurrentPageSize.set(w, h);
- return trim(fPictureRecorder.beginRecording(w, h), w, h, c);
+ return fPictureRecorder.beginRecording(w, h);
}
void onEndPage() override {
fSizes.push_back(fCurrentPageSize);
diff --git a/src/xps/SkXPSDocument.cpp b/src/xps/SkXPSDocument.cpp
index 0d12e4f..ac62c62 100644
--- a/src/xps/SkXPSDocument.cpp
+++ b/src/xps/SkXPSDocument.cpp
@@ -33,13 +33,9 @@
this->close();
}
-SkCanvas* SkXPSDocument::onBeginPage(SkScalar width,
- SkScalar height,
- const SkRect& trimBox) {
+SkCanvas* SkXPSDocument::onBeginPage(SkScalar width, SkScalar height) {
fDevice.beginSheet(fUnitsPerMeter, fPixelsPerMeter, {width, height});
fCanvas.reset(new SkCanvas(&fDevice));
- fCanvas->clipRect(trimBox);
- fCanvas->translate(trimBox.x(), trimBox.y());
return fCanvas.get();
}
diff --git a/src/xps/SkXPSDocument.h b/src/xps/SkXPSDocument.h
index 726af8f..cccfc1f 100644
--- a/src/xps/SkXPSDocument.h
+++ b/src/xps/SkXPSDocument.h
@@ -24,7 +24,7 @@
virtual ~SkXPSDocument();
protected:
- SkCanvas* onBeginPage(SkScalar w, SkScalar h, const SkRect&) override;
+ SkCanvas* onBeginPage(SkScalar w, SkScalar h) override;
void onEndPage() override;
void onClose(SkWStream*) override;
void onAbort() override;