| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkDocument.h" | 8 #include "SkDocument.h" |
| 9 #include "SkPDFDevice.h" |
| 9 #include "SkPDFDocument.h" | 10 #include "SkPDFDocument.h" |
| 10 #include "SkPDFDevice.h" | |
| 11 | 11 |
| 12 class SkDocument_PDF : public SkDocument { | 12 class SkDocument_PDF : public SkDocument { |
| 13 public: | 13 public: |
| 14 SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*)) | 14 SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*), |
| 15 : SkDocument(stream, doneProc) { | 15 SkPicture::EncodeBitmap encoder) |
| 16 : SkDocument(stream, doneProc) |
| 17 , fEncoder(encoder) { |
| 16 fDoc = SkNEW(SkPDFDocument); | 18 fDoc = SkNEW(SkPDFDocument); |
| 17 fCanvas = NULL; | 19 fCanvas = NULL; |
| 18 fDevice = NULL; | 20 fDevice = NULL; |
| 19 } | 21 } |
| 20 | 22 |
| 21 virtual ~SkDocument_PDF() { | 23 virtual ~SkDocument_PDF() { |
| 22 // subclasses must call close() in their destructors | 24 // subclasses must call close() in their destructors |
| 23 this->close(); | 25 this->close(); |
| 24 } | 26 } |
| 25 | 27 |
| 26 protected: | 28 protected: |
| 27 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, | 29 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, |
| 28 const SkRect& content) SK_OVERRIDE { | 30 const SkRect& content) SK_OVERRIDE { |
| 29 SkASSERT(NULL == fCanvas); | 31 SkASSERT(NULL == fCanvas); |
| 30 SkASSERT(NULL == fDevice); | 32 SkASSERT(NULL == fDevice); |
| 31 | 33 |
| 32 SkISize pageS, contentS; | 34 SkISize pageS, contentS; |
| 33 SkMatrix matrix; | 35 SkMatrix matrix; |
| 34 | 36 |
| 35 pageS.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height)); | 37 pageS.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height)); |
| 36 contentS.set(SkScalarRoundToInt(content.width()), | 38 contentS.set(SkScalarRoundToInt(content.width()), |
| 37 SkScalarRoundToInt(content.height())); | 39 SkScalarRoundToInt(content.height())); |
| 38 matrix.setTranslate(content.fLeft, content.fTop); | 40 matrix.setTranslate(content.fLeft, content.fTop); |
| 39 | 41 |
| 40 fDevice = SkNEW_ARGS(SkPDFDevice, (pageS, contentS, matrix)); | 42 fDevice = SkNEW_ARGS(SkPDFDevice, (pageS, contentS, matrix)); |
| 43 if (fEncoder) { |
| 44 fDevice->setDCTEncoder(fEncoder); |
| 45 } |
| 41 fCanvas = SkNEW_ARGS(SkCanvas, (fDevice)); | 46 fCanvas = SkNEW_ARGS(SkCanvas, (fDevice)); |
| 42 return fCanvas; | 47 return fCanvas; |
| 43 } | 48 } |
| 44 | 49 |
| 45 virtual void onEndPage() SK_OVERRIDE { | 50 virtual void onEndPage() SK_OVERRIDE { |
| 46 SkASSERT(fCanvas); | 51 SkASSERT(fCanvas); |
| 47 SkASSERT(fDevice); | 52 SkASSERT(fDevice); |
| 48 | 53 |
| 49 fCanvas->flush(); | 54 fCanvas->flush(); |
| 50 fDoc->appendPage(fDevice); | 55 fDoc->appendPage(fDevice); |
| 51 | 56 |
| 52 fCanvas->unref(); | 57 fCanvas->unref(); |
| 53 fDevice->unref(); | 58 fDevice->unref(); |
| 54 | 59 |
| 55 fCanvas = NULL; | 60 fCanvas = NULL; |
| 56 fDevice = NULL; | 61 fDevice = NULL; |
| 57 } | 62 } |
| 58 | 63 |
| 59 virtual void onClose(SkWStream* stream) SK_OVERRIDE { | 64 virtual bool onClose(SkWStream* stream) SK_OVERRIDE { |
| 60 SkASSERT(NULL == fCanvas); | 65 SkASSERT(NULL == fCanvas); |
| 61 SkASSERT(NULL == fDevice); | 66 SkASSERT(NULL == fDevice); |
| 62 | 67 |
| 63 fDoc->emitPDF(stream); | 68 bool success = fDoc->emitPDF(stream); |
| 69 SkDELETE(fDoc); |
| 70 fDoc = NULL; |
| 71 return success; |
| 72 } |
| 73 |
| 74 virtual void onAbort() SK_OVERRIDE { |
| 64 SkDELETE(fDoc); | 75 SkDELETE(fDoc); |
| 65 fDoc = NULL; | 76 fDoc = NULL; |
| 66 } | 77 } |
| 67 | 78 |
| 68 private: | 79 private: |
| 69 SkPDFDocument* fDoc; | 80 SkPDFDocument* fDoc; |
| 70 SkPDFDevice* fDevice; | 81 SkPDFDevice* fDevice; |
| 71 SkCanvas* fCanvas; | 82 SkCanvas* fCanvas; |
| 83 SkPicture::EncodeBitmap fEncoder; |
| 72 }; | 84 }; |
| 73 | 85 |
| 74 /////////////////////////////////////////////////////////////////////////////// | 86 /////////////////////////////////////////////////////////////////////////////// |
| 75 | 87 |
| 76 SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*)) { | 88 SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*), |
| 77 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done)) : NULL; | 89 SkPicture::EncodeBitmap enc) { |
| 90 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done, enc)) : NULL; |
| 78 } | 91 } |
| 79 | 92 |
| 80 static void delete_wstream(SkWStream* stream) { | 93 static void delete_wstream(SkWStream* stream) { |
| 81 SkDELETE(stream); | 94 SkDELETE(stream); |
| 82 } | 95 } |
| 83 | 96 |
| 84 SkDocument* SkDocument::CreatePDF(const char path[]) { | 97 SkDocument* SkDocument::CreatePDF(const char path[], SkPicture::EncodeBitmap enc
) { |
| 85 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path)); | 98 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path)); |
| 86 if (!stream->isValid()) { | 99 if (!stream->isValid()) { |
| 87 SkDELETE(stream); | 100 SkDELETE(stream); |
| 88 return NULL; | 101 return NULL; |
| 89 } | 102 } |
| 90 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream)); | 103 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, enc)); |
| 91 } | 104 } |
| OLD | NEW |