| 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" | |
| 10 #include "SkPDFDocument.h" | 9 #include "SkPDFDocument.h" |
| 10 #include "SkPDFDeviceFlattener.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 SkPicture::EncodeBitmap encoder) | 15 SkPicture::EncodeBitmap encoder) |
| 16 : SkDocument(stream, doneProc) | 16 : SkDocument(stream, doneProc) |
| 17 , fEncoder(encoder) { | 17 , fEncoder(encoder) { |
| 18 fDoc = SkNEW(SkPDFDocument); | 18 fDoc = SkNEW(SkPDFDocument); |
| 19 fCanvas = NULL; | 19 fCanvas = NULL; |
| 20 fDevice = NULL; | 20 fDevice = NULL; |
| 21 } | 21 } |
| 22 | 22 |
| 23 virtual ~SkDocument_PDF() { | 23 virtual ~SkDocument_PDF() { |
| 24 // subclasses must call close() in their destructors | 24 // subclasses must call close() in their destructors |
| 25 this->close(); | 25 this->close(); |
| 26 } | 26 } |
| 27 | 27 |
| 28 protected: | 28 protected: |
| 29 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, | 29 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, |
| 30 const SkRect& content) SK_OVERRIDE { | 30 const SkRect& trimBox) SK_OVERRIDE { |
| 31 SkASSERT(NULL == fCanvas); | 31 SkASSERT(NULL == fCanvas); |
| 32 SkASSERT(NULL == fDevice); | 32 SkASSERT(NULL == fDevice); |
| 33 | 33 |
| 34 SkISize pageS, contentS; | 34 SkSize mediaBoxSize; |
| 35 SkMatrix matrix; | 35 mediaBoxSize.set(width, height); |
| 36 | 36 |
| 37 pageS.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height)); | 37 fDevice = SkNEW_ARGS(SkPDFDeviceFlattener, (mediaBoxSize, &trimBox)); |
| 38 contentS.set(SkScalarRoundToInt(content.width()), | |
| 39 SkScalarRoundToInt(content.height())); | |
| 40 matrix.setTranslate(content.fLeft, content.fTop); | |
| 41 | |
| 42 fDevice = SkNEW_ARGS(SkPDFDevice, (pageS, contentS, matrix)); | |
| 43 if (fEncoder) { | 38 if (fEncoder) { |
| 44 fDevice->setDCTEncoder(fEncoder); | 39 fDevice->setDCTEncoder(fEncoder); |
| 45 } | 40 } |
| 46 fCanvas = SkNEW_ARGS(SkCanvas, (fDevice)); | 41 fCanvas = SkNEW_ARGS(SkCanvas, (fDevice)); |
| 47 return fCanvas; | 42 return fCanvas; |
| 48 } | 43 } |
| 49 | 44 |
| 50 virtual void onEndPage() SK_OVERRIDE { | 45 virtual void onEndPage() SK_OVERRIDE { |
| 51 SkASSERT(fCanvas); | 46 SkASSERT(fCanvas); |
| 52 SkASSERT(fDevice); | 47 SkASSERT(fDevice); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 71 return success; | 66 return success; |
| 72 } | 67 } |
| 73 | 68 |
| 74 virtual void onAbort() SK_OVERRIDE { | 69 virtual void onAbort() SK_OVERRIDE { |
| 75 SkDELETE(fDoc); | 70 SkDELETE(fDoc); |
| 76 fDoc = NULL; | 71 fDoc = NULL; |
| 77 } | 72 } |
| 78 | 73 |
| 79 private: | 74 private: |
| 80 SkPDFDocument* fDoc; | 75 SkPDFDocument* fDoc; |
| 81 SkPDFDevice* fDevice; | 76 SkPDFDeviceFlattener* fDevice; |
| 82 SkCanvas* fCanvas; | 77 SkCanvas* fCanvas; |
| 83 SkPicture::EncodeBitmap fEncoder; | 78 SkPicture::EncodeBitmap fEncoder; |
| 84 }; | 79 }; |
| 85 | 80 |
| 86 /////////////////////////////////////////////////////////////////////////////// | 81 /////////////////////////////////////////////////////////////////////////////// |
| 87 | 82 |
| 88 SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*), | 83 SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*), |
| 89 SkPicture::EncodeBitmap enc) { | 84 SkPicture::EncodeBitmap enc) { |
| 90 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done, enc)) : NULL; | 85 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done, enc)) : NULL; |
| 91 } | 86 } |
| 92 | 87 |
| 93 static void delete_wstream(SkWStream* stream) { | 88 static void delete_wstream(SkWStream* stream) { |
| 94 SkDELETE(stream); | 89 SkDELETE(stream); |
| 95 } | 90 } |
| 96 | 91 |
| 97 SkDocument* SkDocument::CreatePDF(const char path[], SkPicture::EncodeBitmap enc
) { | 92 SkDocument* SkDocument::CreatePDF(const char path[], SkPicture::EncodeBitmap enc
) { |
| 98 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path)); | 93 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path)); |
| 99 if (!stream->isValid()) { | 94 if (!stream->isValid()) { |
| 100 SkDELETE(stream); | 95 SkDELETE(stream); |
| 101 return NULL; | 96 return NULL; |
| 102 } | 97 } |
| 103 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, enc)); | 98 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, enc)); |
| 104 } | 99 } |
| OLD | NEW |