Skip to content

Commit

Permalink
Add an SKP to PDF rendered. test_pdfs.py will be hooked up in buildbo…
Browse files Browse the repository at this point in the history
…t testing later.

Review URL: https://codereview.appspot.com/6610056

git-svn-id: http://skia.googlecode.com/svn/trunk@5880 2bbb7eff-a529-9590-31e7-b0007b416f81
  • Loading branch information
[email protected] committed Oct 10, 2012
1 parent 1b6c73d commit 2a827e8
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 0 deletions.
33 changes: 33 additions & 0 deletions gyp/tools.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,39 @@
}],
],
},
{
'target_name': 'render_pdfs',
'type': 'executable',
'sources': [
'../tools/render_pdfs_main.cpp',
],
'include_dirs': [
'../src/pipe/utils/',
],
'dependencies': [
'core.gyp:core',
'effects.gyp:effects',
'images.gyp:images',
'ports.gyp:ports',
'tools.gyp:pdf_renderer',
'tools.gyp:picture_utils',
],
},
{
'target_name': 'pdf_renderer',
'type': 'static_library',
'sources': [
'../tools/PdfRenderer.cpp',
'../tools/PdfRenderer.h',
],
'include_dirs': [
'../src/utils/',
],
'dependencies': [
'core.gyp:core',
'pdf.gyp:pdf',
],
},
{
'target_name': 'picture_utils',
'type': 'static_library',
Expand Down
73 changes: 73 additions & 0 deletions tools/PdfRenderer.cpp
Original file line number Diff line number Diff line change
@@ -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();
}

}
67 changes: 67 additions & 0 deletions tools/PdfRenderer.h
Original file line number Diff line number Diff line change
@@ -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<SkCanvas> 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
60 changes: 60 additions & 0 deletions tools/test_pdfs.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 2a827e8

Please sign in to comment.