Chromium Code Reviews| Index: src/doc/SkDocument_PDF.cpp |
| diff --git a/src/doc/SkDocument_PDF.cpp b/src/doc/SkDocument_PDF.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..61f04e97e99cb18976748a4ccfb68dedabea576a |
| --- /dev/null |
| +++ b/src/doc/SkDocument_PDF.cpp |
| @@ -0,0 +1,86 @@ |
| +/* |
| + * Copyright 2013 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkDocument.h" |
| +#include "SkPDFDocument.h" |
| +#include "SkPDFDevice.h" |
| + |
| +class SkDocument_PDF : public SkDocument { |
| +public: |
| + SkDocument_PDF(SkWStream* stream) : SkDocument(stream) { |
| + fDoc = SkNEW(SkPDFDocument); |
| + fCanvas = NULL; |
| + fDevice = NULL; |
| + } |
| + |
| + virtual ~SkDocument_PDF() { |
| + this->close(); |
| + } |
| + |
| +protected: |
| + virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, |
| + const SkRect& content) SK_OVERRIDE { |
| + SkASSERT(NULL == fCanvas); |
| + SkASSERT(NULL == fDevice); |
| + |
| + SkISize pageS, contentS; |
| + SkMatrix matrix; |
| + |
| + pageS.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height)); |
| + contentS.set(SkScalarRoundToInt(content.width()), |
| + SkScalarRoundToInt(content.height())); |
| + matrix.setTranslate(content.fLeft, content.fTop); |
| + |
| + fDevice = SkNEW_ARGS(SkPDFDevice, (pageS, contentS, matrix)); |
| + fCanvas = SkNEW_ARGS(SkCanvas, (fDevice)); |
| + return fCanvas; |
| + } |
| + |
| + virtual void onEndPage() SK_OVERRIDE { |
| + SkASSERT(fCanvas); |
| + SkASSERT(fDevice); |
| + |
| + fCanvas->flush(); |
| + fDoc->appendPage(fDevice); |
| + |
| + fCanvas->unref(); |
| + fDevice->unref(); |
| + |
| + fCanvas = NULL; |
| + fDevice = NULL; |
| + } |
| + |
| + virtual void onClose(SkWStream* stream) SK_OVERRIDE { |
| + SkASSERT(NULL == fCanvas); |
| + SkASSERT(NULL == fDevice); |
| + |
| + fDoc->emitPDF(stream); |
| + SkDELETE(fDoc); |
| + fDoc = NULL; |
| + } |
| + |
| +private: |
| + SkPDFDocument* fDoc; |
| + SkPDFDevice* fDevice; |
| + SkCanvas* fCanvas; |
| +}; |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| + |
| +SkDocument* SkDocument::CreatePDF(SkWStream* stream) { |
| + return stream ? SkNEW_ARGS(SkDocument_PDF, (stream)) : NULL; |
| +} |
| + |
| +SkDocument* SkDocument::CreatePDF(const char path[]) { |
| + SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path)); |
|
scroggo
2013/06/07 20:12:32
This one does need to be deleted.
|
| + if (!stream->isValid()) { |
| + SkDELETE(stream); |
| + return NULL; |
| + } |
| + return SkNEW_ARGS(SkDocument_PDF, (stream)); |
| +} |
| + |