Add an SKP to PDF rendered. test_pdfs.py will be hooked up in buildbot testing later.
Review URL: https://codereview.appspot.com/6610056

git-svn-id: http://skia.googlecode.com/svn/trunk@5880 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tools/PdfRenderer.cpp b/tools/PdfRenderer.cpp
new file mode 100644
index 0000000..8819266
--- /dev/null
+++ b/tools/PdfRenderer.cpp
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "PdfRenderer.h"
+#include "SkCanvas.h"
+#include "SkDevice.h"
+#include "SkPDFDevice.h"
+#include "SkPDFDocument.h"
+
+namespace sk_tools {
+
+void PdfRenderer::init(SkPicture* pict) {
+    SkASSERT(NULL == fPicture);
+    SkASSERT(NULL == fCanvas.get());
+    if (fPicture != NULL || NULL != fCanvas.get()) {
+        return;
+    }
+
+    SkASSERT(pict != NULL);
+    if (NULL == pict) {
+        return;
+    }
+
+    fPicture = pict;
+    fCanvas.reset(this->setupCanvas());
+}
+
+SkCanvas* PdfRenderer::setupCanvas() {
+    return this->setupCanvas(fPicture->width(), fPicture->height());
+}
+
+SkCanvas* PdfRenderer::setupCanvas(int width, int height) {
+    SkISize pageSize = SkISize::Make(width, height);
+    fPDFDevice = SkNEW_ARGS(SkPDFDevice, (pageSize, pageSize, SkMatrix::I()));
+    return SkNEW_ARGS(SkCanvas, (fPDFDevice));
+}
+
+void PdfRenderer::end() {
+    fPicture = NULL;
+    fCanvas.reset(NULL);
+    if (fPDFDevice) {
+        SkDELETE(fPDFDevice);
+        fPDFDevice = NULL;
+    }
+}
+
+bool PdfRenderer::write(const SkString& path) const {
+    SkPDFDocument doc;
+    doc.appendPage(fPDFDevice);
+    SkFILEWStream stream(path.c_str());
+    if (stream.isValid()) {
+        doc.emitPDF(&stream);
+        return true;
+    }
+    return false;
+}
+
+void SimplePdfRenderer::render() {
+    SkASSERT(fCanvas.get() != NULL);
+    SkASSERT(fPicture != NULL);
+    if (NULL == fCanvas.get() || NULL == fPicture) {
+        return;
+    }
+
+    fCanvas->drawPicture(*fPicture);
+    fCanvas->flush();
+}
+
+}