forked from google/skia
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an SKP to PDF rendered. test_pdfs.py will be hooked up in buildbo…
…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
1 parent
1b6c73d
commit 2a827e8
Showing
4 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|