public:
/** Create a PDF drawing context with the given width and height.
* 72 points/in means letter paper is 612x792.
- * @param width Page width in points.
- * @param height Page height in points.
+ * @param pageSize Page size in points.
+ * @param contentSize The content size of the page in points. This will be
+ * combined with the initial transform to determine the drawing area
+ * (as reported by the width and height methods). Anything outside
+ * of the drawing area will be clipped.
* @param initialTransform The initial transform to apply to the page.
* This may be useful to, for example, move the origin in and
* over a bit to account for a margin, scale the canvas,
* inverse scale+translate to accommodate the one that SkPDFDevice
* always does.
*/
- SkPDFDevice(int width, int height, const SkMatrix& initialTransform);
- virtual ~SkPDFDevice();
+ SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
+ const SkMatrix& initialTransform);
+ SK_API virtual ~SkPDFDevice();
virtual uint32_t getDeviceCapabilities() { return kVector_Capability; }
- virtual int width() const { return fWidth; };
-
- virtual int height() const { return fHeight; };
-
virtual void clear(SkColor color);
/** Called with the correct matrix and clip before this device is drawn
virtual SkDeviceFactory* onNewDeviceFactory();
private:
- int fWidth;
- int fHeight;
+ SkISize fPageSize;
SkMatrix fInitialTransform;
SkRefPtr<SkPDFDict> fResourceDict;
initialTransform.setTranslate(0, height);
initialTransform.preScale(1, -1);
}
- return SkNEW_ARGS(SkPDFDevice, (width, height, initialTransform));
+ SkISize size = SkISize::Make(width, height);
+ return SkNEW_ARGS(SkPDFDevice, (size, size, initialTransform));
}
-static inline SkBitmap makeABitmap(int width, int height) {
+static inline SkBitmap makeContentBitmap(const SkISize& contentSize,
+ const SkMatrix& initialTransform) {
+ // Compute the size of the drawing area.
+ SkVector drawingSize;
+ SkMatrix inverse;
+ drawingSize.set(contentSize.fWidth, contentSize.fHeight);
+ initialTransform.invert(&inverse);
+ inverse.mapVectors(&drawingSize, 1);
+ SkISize size = SkSize::Make(drawingSize.fX, drawingSize.fY).toRound();
+
SkBitmap bitmap;
- bitmap.setConfig(SkBitmap::kNo_Config, width, height);
+ bitmap.setConfig(SkBitmap::kNo_Config, size.fWidth, size.fHeight);
return bitmap;
}
-SkPDFDevice::SkPDFDevice(int width, int height,
+SkPDFDevice::SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
const SkMatrix& initialTransform)
- : SkDevice(NULL, makeABitmap(width, height), false),
- fWidth(width),
- fHeight(height),
+ : SkDevice(NULL, makeContentBitmap(contentSize, initialTransform), false),
+ fPageSize(pageSize),
fGraphicStackIndex(0) {
// Skia generally uses the top left as the origin but PDF natively has the
// origin at the bottom left. This matrix corrects for that. When layering,
// we specify an inverse correction to cancel this out.
- fInitialTransform.setTranslate(0, height);
+ fInitialTransform.setTranslate(0, pageSize.fHeight);
fInitialTransform.preScale(1, -1);
fInitialTransform.preConcat(initialTransform);
fGraphicStack[0].fFont = NULL;
fGraphicStack[0].fShader = NULL;
fGraphicStack[0].fGraphicState = NULL;
- fGraphicStack[0].fClip.setRect(0,0, fWidth, fHeight);
+ fGraphicStack[0].fClip.setRect(0, 0, this->width(), this->height());
fGraphicStack[0].fTransform.reset();
fGraphicStackIndex = 0;
fResourceDict = NULL;
mediaBox->reserve(4);
mediaBox->append(zero.get());
mediaBox->append(zero.get());
- mediaBox->append(new SkPDFInt(fWidth))->unref();
- mediaBox->append(new SkPDFInt(fHeight))->unref();
+ mediaBox->append(new SkPDFInt(fPageSize.fWidth))->unref();
+ mediaBox->append(new SkPDFInt(fPageSize.fHeight))->unref();
return mediaBox;
}