Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3426)

Unified Diff: src/doc/SkDocument_PDF.cpp

Issue 16660002: SkDocument base for pdf, xps, etc. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: add Done proc, so caller can delete the stream automatically when the doc is done with it Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3d9c46362de778661670754e856d8e1f6dfc87ae
--- /dev/null
+++ b/src/doc/SkDocument_PDF.cpp
@@ -0,0 +1,92 @@
+/*
+ * 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, void (*doneProc)(SkWStream*))
+ : SkDocument(stream, doneProc) {
+ fDoc = SkNEW(SkPDFDocument);
+ fCanvas = NULL;
+ fDevice = NULL;
+ }
+
+ virtual ~SkDocument_PDF() {
+ // subclasses must call close() in their destructors
+ 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, void (*done)(SkWStream*)) {
+ return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done)) : NULL;
+}
+
+static void delete_wstream(SkWStream* stream) {
+ SkDELETE(stream);
+}
+
+SkDocument* SkDocument::CreatePDF(const char path[]) {
+ SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path));
+ if (!stream->isValid()) {
+ SkDELETE(stream);
+ return NULL;
+ }
+ return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream));
+}
+
« src/doc/SkDocument.cpp ('K') | « src/doc/SkDocument.cpp ('k') | tools/skhello.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698