From 2a827e81b3334f33b0f8ff05b6a39a11d532568f Mon Sep 17 00:00:00 2001 From: "edisonn@google.com" Date: Wed, 10 Oct 2012 15:20:34 +0000 Subject: 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 --- tools/PdfRenderer.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ tools/PdfRenderer.h | 67 ++++++++++++++++++++++++++++++++++++++++++++++ tools/test_pdfs.py | 60 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 tools/PdfRenderer.cpp create mode 100644 tools/PdfRenderer.h create mode 100644 tools/test_pdfs.py (limited to 'tools') diff --git a/tools/PdfRenderer.cpp b/tools/PdfRenderer.cpp new file mode 100644 index 0000000000..8819266a9f --- /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(); +} + +} diff --git a/tools/PdfRenderer.h b/tools/PdfRenderer.h new file mode 100644 index 0000000000..bce6197bb5 --- /dev/null +++ b/tools/PdfRenderer.h @@ -0,0 +1,67 @@ +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef PdfRenderer_DEFINED +#define PdfRenderer_DEFINED + +// +// PdfRender takes a SkPicture and writes it to a PDF file. +// An SkPicture can be built manually, or read from an SKP file. +// + +#include "SkMath.h" +#include "SkPicture.h" +#include "SkTypes.h" +#include "SkTDArray.h" +#include "SkRefCnt.h" +#include "SkString.h" + +class SkBitmap; +class SkCanvas; +class SkGLContext; +class SkPDFDevice; + +namespace sk_tools { + +class PdfRenderer : public SkRefCnt { +public: + virtual void init(SkPicture* pict); + virtual void setup() {} + virtual void render() = 0; + virtual void end(); + + PdfRenderer() + : fPicture(NULL) + , fPDFDevice(NULL) + {} + + bool write(const SkString& path) const; + +protected: + SkCanvas* setupCanvas(); + SkCanvas* setupCanvas(int width, int height); + + SkAutoTUnref fCanvas; + SkPicture* fPicture; + SkPDFDevice* fPDFDevice; + + +private: + typedef SkRefCnt INHERITED; +}; + +class SimplePdfRenderer : public PdfRenderer { +public: + virtual void render() SK_OVERRIDE; + +private: + typedef PdfRenderer INHERITED; +}; + +} + +#endif // PdfRenderer_DEFINED diff --git a/tools/test_pdfs.py b/tools/test_pdfs.py new file mode 100644 index 0000000000..ac3eab9a33 --- /dev/null +++ b/tools/test_pdfs.py @@ -0,0 +1,60 @@ +''' +Compares the rendererings of serialized SkPictures to expected images. + +Launch with --help to see more information. + + +Copyright 2012 Google Inc. + +Use of this source code is governed by a BSD-style license that can be +found in the LICENSE file. +''' +# common Python modules +import os +import optparse +import sys +import shutil +import tempfile +import test_rendering + +USAGE_STRING = 'Usage: %s input... expectedDir' +HELP_STRING = ''' + +Takes input SkPicture files and renders them as PDF files, and then compares +those resulting PDF files against PDF files found in expectedDir. + +Each instance of "input" can be either a file (name must end in .skp), or a +directory (in which case this script will process all .skp files within the +directory). +''' + + +def Main(args): + """Allow other scripts to call this script with fake command-line args. + + @param The commandline argument list + """ + parser = optparse.OptionParser(USAGE_STRING % '%prog' + HELP_STRING) + parser.add_option('--render_dir', dest='render_dir', + help = ('specify the location to output the rendered ' + 'files. Default is a temp directory.')) + parser.add_option('--diff_dir', dest='diff_dir', + help = ('specify the location to output the diff files. ' + 'Default is a temp directory.')) + + options, arguments = parser.parse_args(args) + + if (len(arguments) < 3): + print("Expected at least one input and one ouput folder.") + parser.print_help() + sys.exit(-1) + + inputs = arguments[1:-1] + expected_dir = arguments[-1] + + test_rendering.TestRenderSkps(inputs, expected_dir, options.render_dir, + options.diff_dir, 'render_pdfs', '') + +if __name__ == '__main__': + Main(sys.argv) + -- cgit v1.2.3