blob: 834cbc3a96ded17bc16c2ae2a21b4258ca9fcff8 [file] [log] [blame]
reed@google.com99ac02b2013-06-07 20:30:16 +00001/*
2 * Copyright 2013 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 "SkDocument.h"
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +00009#include "SkPDFDocument.h"
reed58677362014-10-09 05:30:10 -070010#include "SkPDFDevice.h"
reed@google.com99ac02b2013-06-07 20:30:16 +000011
12class SkDocument_PDF : public SkDocument {
13public:
robertphillips@google.com701b4052013-11-18 16:26:25 +000014 SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*,bool),
commit-bot@chromium.org8c294902013-10-21 17:14:37 +000015 SkPicture::EncodeBitmap encoder,
16 SkScalar rasterDpi)
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000017 : SkDocument(stream, doneProc)
commit-bot@chromium.org8c294902013-10-21 17:14:37 +000018 , fEncoder(encoder)
19 , fRasterDpi(rasterDpi) {
reed@google.com99ac02b2013-06-07 20:30:16 +000020 fDoc = SkNEW(SkPDFDocument);
21 fCanvas = NULL;
22 fDevice = NULL;
23 }
skia.committer@gmail.com63193672013-06-08 07:01:13 +000024
reed@google.com99ac02b2013-06-07 20:30:16 +000025 virtual ~SkDocument_PDF() {
26 // subclasses must call close() in their destructors
27 this->close();
28 }
29
30protected:
31 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height,
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000032 const SkRect& trimBox) SK_OVERRIDE {
reed@google.com99ac02b2013-06-07 20:30:16 +000033 SkASSERT(NULL == fCanvas);
34 SkASSERT(NULL == fDevice);
35
reed58677362014-10-09 05:30:10 -070036 SkISize mediaBoxSize;
37 mediaBoxSize.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height));
reed@google.com99ac02b2013-06-07 20:30:16 +000038
reed58677362014-10-09 05:30:10 -070039 fDevice = SkNEW_ARGS(SkPDFDevice, (mediaBoxSize, mediaBoxSize, SkMatrix::I()));
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000040 if (fEncoder) {
41 fDevice->setDCTEncoder(fEncoder);
42 }
commit-bot@chromium.org8c294902013-10-21 17:14:37 +000043 if (fRasterDpi != 0) {
44 fDevice->setRasterDpi(fRasterDpi);
45 }
reed@google.com99ac02b2013-06-07 20:30:16 +000046 fCanvas = SkNEW_ARGS(SkCanvas, (fDevice));
halcanarybe519ad2014-11-10 14:22:14 -080047 fCanvas->clipRect(trimBox);
halcanary93f81612014-11-10 14:01:57 -080048 fCanvas->translate(trimBox.x(), trimBox.y());
reed@google.com99ac02b2013-06-07 20:30:16 +000049 return fCanvas;
50 }
51
52 virtual void onEndPage() SK_OVERRIDE {
53 SkASSERT(fCanvas);
54 SkASSERT(fDevice);
55
56 fCanvas->flush();
57 fDoc->appendPage(fDevice);
58
59 fCanvas->unref();
60 fDevice->unref();
61
62 fCanvas = NULL;
63 fDevice = NULL;
64 }
65
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000066 virtual bool onClose(SkWStream* stream) SK_OVERRIDE {
reed@google.com99ac02b2013-06-07 20:30:16 +000067 SkASSERT(NULL == fCanvas);
68 SkASSERT(NULL == fDevice);
69
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000070 bool success = fDoc->emitPDF(stream);
71 SkDELETE(fDoc);
72 fDoc = NULL;
73 return success;
74 }
75
76 virtual void onAbort() SK_OVERRIDE {
reed@google.com99ac02b2013-06-07 20:30:16 +000077 SkDELETE(fDoc);
78 fDoc = NULL;
79 }
80
81private:
82 SkPDFDocument* fDoc;
reed58677362014-10-09 05:30:10 -070083 SkPDFDevice* fDevice;
reed@google.com99ac02b2013-06-07 20:30:16 +000084 SkCanvas* fCanvas;
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000085 SkPicture::EncodeBitmap fEncoder;
commit-bot@chromium.org8c294902013-10-21 17:14:37 +000086 SkScalar fRasterDpi;
reed@google.com99ac02b2013-06-07 20:30:16 +000087};
88
89///////////////////////////////////////////////////////////////////////////////
90
robertphillips@google.com701b4052013-11-18 16:26:25 +000091SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*,bool),
commit-bot@chromium.org8c294902013-10-21 17:14:37 +000092 SkPicture::EncodeBitmap enc,
93 SkScalar dpi) {
94 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done, enc, dpi)) : NULL;
reed@google.com99ac02b2013-06-07 20:30:16 +000095}
96
robertphillips@google.com701b4052013-11-18 16:26:25 +000097static void delete_wstream(SkWStream* stream, bool aborted) {
reed@google.com99ac02b2013-06-07 20:30:16 +000098 SkDELETE(stream);
99}
100
commit-bot@chromium.org8c294902013-10-21 17:14:37 +0000101SkDocument* SkDocument::CreatePDF(const char path[],
102 SkPicture::EncodeBitmap enc,
103 SkScalar dpi) {
reed@google.com99ac02b2013-06-07 20:30:16 +0000104 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path));
105 if (!stream->isValid()) {
106 SkDELETE(stream);
107 return NULL;
108 }
commit-bot@chromium.org8c294902013-10-21 17:14:37 +0000109 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, enc, dpi));
reed@google.com99ac02b2013-06-07 20:30:16 +0000110}