reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 1 | /* |
| 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.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 9 | #include "SkPDFDocument.h" |
reed | 5867736 | 2014-10-09 05:30:10 -0700 | [diff] [blame] | 10 | #include "SkPDFDevice.h" |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 11 | |
| 12 | class SkDocument_PDF : public SkDocument { |
| 13 | public: |
robertphillips@google.com | 701b405 | 2013-11-18 16:26:25 +0000 | [diff] [blame] | 14 | SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*,bool), |
commit-bot@chromium.org | 8c29490 | 2013-10-21 17:14:37 +0000 | [diff] [blame] | 15 | SkPicture::EncodeBitmap encoder, |
| 16 | SkScalar rasterDpi) |
commit-bot@chromium.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 17 | : SkDocument(stream, doneProc) |
commit-bot@chromium.org | 8c29490 | 2013-10-21 17:14:37 +0000 | [diff] [blame] | 18 | , fEncoder(encoder) |
| 19 | , fRasterDpi(rasterDpi) { |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 20 | fDoc = SkNEW(SkPDFDocument); |
| 21 | fCanvas = NULL; |
| 22 | fDevice = NULL; |
| 23 | } |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 24 | |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 25 | virtual ~SkDocument_PDF() { |
| 26 | // subclasses must call close() in their destructors |
| 27 | this->close(); |
| 28 | } |
| 29 | |
| 30 | protected: |
| 31 | virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 32 | const SkRect& trimBox) SK_OVERRIDE { |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 33 | SkASSERT(NULL == fCanvas); |
| 34 | SkASSERT(NULL == fDevice); |
| 35 | |
reed | 5867736 | 2014-10-09 05:30:10 -0700 | [diff] [blame] | 36 | SkISize mediaBoxSize; |
| 37 | mediaBoxSize.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height)); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 38 | |
reed | 5867736 | 2014-10-09 05:30:10 -0700 | [diff] [blame] | 39 | fDevice = SkNEW_ARGS(SkPDFDevice, (mediaBoxSize, mediaBoxSize, SkMatrix::I())); |
commit-bot@chromium.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 40 | if (fEncoder) { |
| 41 | fDevice->setDCTEncoder(fEncoder); |
| 42 | } |
commit-bot@chromium.org | 8c29490 | 2013-10-21 17:14:37 +0000 | [diff] [blame] | 43 | if (fRasterDpi != 0) { |
| 44 | fDevice->setRasterDpi(fRasterDpi); |
| 45 | } |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 46 | fCanvas = SkNEW_ARGS(SkCanvas, (fDevice)); |
halcanary | be519ad | 2014-11-10 14:22:14 -0800 | [diff] [blame^] | 47 | fCanvas->clipRect(trimBox); |
halcanary | 93f8161 | 2014-11-10 14:01:57 -0800 | [diff] [blame] | 48 | fCanvas->translate(trimBox.x(), trimBox.y()); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 49 | 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.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 66 | virtual bool onClose(SkWStream* stream) SK_OVERRIDE { |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 67 | SkASSERT(NULL == fCanvas); |
| 68 | SkASSERT(NULL == fDevice); |
| 69 | |
commit-bot@chromium.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 70 | bool success = fDoc->emitPDF(stream); |
| 71 | SkDELETE(fDoc); |
| 72 | fDoc = NULL; |
| 73 | return success; |
| 74 | } |
| 75 | |
| 76 | virtual void onAbort() SK_OVERRIDE { |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 77 | SkDELETE(fDoc); |
| 78 | fDoc = NULL; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | SkPDFDocument* fDoc; |
reed | 5867736 | 2014-10-09 05:30:10 -0700 | [diff] [blame] | 83 | SkPDFDevice* fDevice; |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 84 | SkCanvas* fCanvas; |
commit-bot@chromium.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 85 | SkPicture::EncodeBitmap fEncoder; |
commit-bot@chromium.org | 8c29490 | 2013-10-21 17:14:37 +0000 | [diff] [blame] | 86 | SkScalar fRasterDpi; |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | |
robertphillips@google.com | 701b405 | 2013-11-18 16:26:25 +0000 | [diff] [blame] | 91 | SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*,bool), |
commit-bot@chromium.org | 8c29490 | 2013-10-21 17:14:37 +0000 | [diff] [blame] | 92 | SkPicture::EncodeBitmap enc, |
| 93 | SkScalar dpi) { |
| 94 | return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done, enc, dpi)) : NULL; |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 95 | } |
| 96 | |
robertphillips@google.com | 701b405 | 2013-11-18 16:26:25 +0000 | [diff] [blame] | 97 | static void delete_wstream(SkWStream* stream, bool aborted) { |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 98 | SkDELETE(stream); |
| 99 | } |
| 100 | |
commit-bot@chromium.org | 8c29490 | 2013-10-21 17:14:37 +0000 | [diff] [blame] | 101 | SkDocument* SkDocument::CreatePDF(const char path[], |
| 102 | SkPicture::EncodeBitmap enc, |
| 103 | SkScalar dpi) { |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 104 | SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path)); |
| 105 | if (!stream->isValid()) { |
| 106 | SkDELETE(stream); |
| 107 | return NULL; |
| 108 | } |
commit-bot@chromium.org | 8c29490 | 2013-10-21 17:14:37 +0000 | [diff] [blame] | 109 | return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, enc, dpi)); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 110 | } |