Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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" | |
| 9 #include "SkPDFDocument.h" | |
| 10 #include "SkPDFDevice.h" | |
| 11 | |
| 12 class SkDocument_PDF : public SkDocument { | |
| 13 public: | |
| 14 SkDocument_PDF(SkWStream* stream) : SkDocument(stream) { | |
| 15 fDoc = SkNEW(SkPDFDocument); | |
| 16 fCanvas = NULL; | |
| 17 fDevice = NULL; | |
| 18 } | |
| 19 | |
| 20 virtual ~SkDocument_PDF() { | |
| 21 this->close(); | |
| 22 } | |
| 23 | |
| 24 protected: | |
| 25 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, | |
| 26 const SkRect& content) SK_OVERRIDE { | |
| 27 SkASSERT(NULL == fCanvas); | |
| 28 SkASSERT(NULL == fDevice); | |
| 29 | |
| 30 SkISize pageS, contentS; | |
| 31 SkMatrix matrix; | |
| 32 | |
| 33 pageS.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height)); | |
| 34 contentS.set(SkScalarRoundToInt(content.width()), | |
| 35 SkScalarRoundToInt(content.height())); | |
| 36 matrix.setTranslate(content.fLeft, content.fTop); | |
| 37 | |
| 38 fDevice = SkNEW_ARGS(SkPDFDevice, (pageS, contentS, matrix)); | |
| 39 fCanvas = SkNEW_ARGS(SkCanvas, (fDevice)); | |
| 40 return fCanvas; | |
| 41 } | |
| 42 | |
| 43 virtual void onEndPage() SK_OVERRIDE { | |
| 44 SkASSERT(fCanvas); | |
| 45 SkASSERT(fDevice); | |
| 46 | |
| 47 fCanvas->flush(); | |
| 48 fDoc->appendPage(fDevice); | |
| 49 | |
| 50 fCanvas->unref(); | |
| 51 fDevice->unref(); | |
| 52 | |
| 53 fCanvas = NULL; | |
| 54 fDevice = NULL; | |
| 55 } | |
| 56 | |
| 57 virtual void onClose(SkWStream* stream) SK_OVERRIDE { | |
| 58 SkASSERT(NULL == fCanvas); | |
| 59 SkASSERT(NULL == fDevice); | |
| 60 | |
| 61 fDoc->emitPDF(stream); | |
| 62 SkDELETE(fDoc); | |
| 63 fDoc = NULL; | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 SkPDFDocument* fDoc; | |
| 68 SkPDFDevice* fDevice; | |
| 69 SkCanvas* fCanvas; | |
| 70 }; | |
| 71 | |
| 72 /////////////////////////////////////////////////////////////////////////////// | |
| 73 | |
| 74 SkDocument* SkDocument::CreatePDF(SkWStream* stream) { | |
| 75 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream)) : NULL; | |
| 76 } | |
| 77 | |
| 78 SkDocument* SkDocument::CreatePDF(const char path[]) { | |
| 79 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path)); | |
|
scroggo
2013/06/07 20:12:32
This one does need to be deleted.
| |
| 80 if (!stream->isValid()) { | |
| 81 SkDELETE(stream); | |
| 82 return NULL; | |
| 83 } | |
| 84 return SkNEW_ARGS(SkDocument_PDF, (stream)); | |
| 85 } | |
| 86 | |
| OLD | NEW |