Add option to render PDFs to memory only.
Review URL: https://codereview.appspot.com/7097045 git-svn-id: http://skia.googlecode.com/svn/trunk@7140 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
parent
c18143e89b
commit
4fa566b34a
@ -48,15 +48,10 @@ void PdfRenderer::end() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PdfRenderer::write(const SkString& path) const {
|
void PdfRenderer::write(SkWStream* stream) const {
|
||||||
SkPDFDocument doc;
|
SkPDFDocument doc;
|
||||||
doc.appendPage(fPDFDevice);
|
doc.appendPage(fPDFDevice);
|
||||||
SkFILEWStream stream(path.c_str());
|
doc.emitPDF(stream);
|
||||||
if (stream.isValid()) {
|
|
||||||
doc.emitPDF(&stream);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimplePdfRenderer::render() {
|
void SimplePdfRenderer::render() {
|
||||||
|
@ -39,7 +39,7 @@ public:
|
|||||||
, fPDFDevice(NULL)
|
, fPDFDevice(NULL)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
bool write(const SkString& path) const;
|
void write(SkWStream* stream) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SkCanvas* setupCanvas();
|
SkCanvas* setupCanvas();
|
||||||
|
@ -33,7 +33,7 @@ static void usage(const char* argv0) {
|
|||||||
SkDebugf("SKP to PDF rendering tool\n");
|
SkDebugf("SKP to PDF rendering tool\n");
|
||||||
SkDebugf("\n"
|
SkDebugf("\n"
|
||||||
"Usage: \n"
|
"Usage: \n"
|
||||||
" %s <input>... <outputDir> \n"
|
" %s <input>... -w <outputDir> \n"
|
||||||
, argv0);
|
, argv0);
|
||||||
SkDebugf("\n\n");
|
SkDebugf("\n\n");
|
||||||
SkDebugf(
|
SkDebugf(
|
||||||
@ -89,15 +89,25 @@ static bool make_output_filepath(SkString* path, const SkString& dir,
|
|||||||
static bool write_output(const SkString& outputDir,
|
static bool write_output(const SkString& outputDir,
|
||||||
const SkString& inputFilename,
|
const SkString& inputFilename,
|
||||||
const sk_tools::PdfRenderer& renderer) {
|
const sk_tools::PdfRenderer& renderer) {
|
||||||
|
if (outputDir.isEmpty()) {
|
||||||
|
SkDynamicMemoryWStream stream;
|
||||||
|
renderer.write(&stream);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
SkString outputPath;
|
SkString outputPath;
|
||||||
if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
|
if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool isWritten = renderer.write(outputPath);
|
|
||||||
if (!isWritten) {
|
SkFILEWStream stream(outputPath.c_str());
|
||||||
|
if (!stream.isValid()) {
|
||||||
SkDebugf("Could not write to file %s\n", outputPath.c_str());
|
SkDebugf("Could not write to file %s\n", outputPath.c_str());
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return isWritten;
|
renderer.write(&stream);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Reads an skp file, renders it to pdf and writes the output to a pdf file
|
/** Reads an skp file, renders it to pdf and writes the output to a pdf file
|
||||||
@ -168,7 +178,8 @@ static int process_input(const SkString& input, const SkString& outputDir,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void parse_commandline(int argc, char* const argv[],
|
static void parse_commandline(int argc, char* const argv[],
|
||||||
SkTArray<SkString>* inputs) {
|
SkTArray<SkString>* inputs,
|
||||||
|
SkString* outputDir) {
|
||||||
const char* argv0 = argv[0];
|
const char* argv0 = argv[0];
|
||||||
char* const* stop = argv + argc;
|
char* const* stop = argv + argc;
|
||||||
|
|
||||||
@ -176,12 +187,20 @@ static void parse_commandline(int argc, char* const argv[],
|
|||||||
if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
|
if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
|
||||||
usage(argv0);
|
usage(argv0);
|
||||||
exit(-1);
|
exit(-1);
|
||||||
|
} else if (0 == strcmp(*argv, "-w")) {
|
||||||
|
++argv;
|
||||||
|
if (argv >= stop) {
|
||||||
|
SkDebugf("Missing outputDir for -w\n");
|
||||||
|
usage(argv0);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
*outputDir = SkString(*argv);
|
||||||
} else {
|
} else {
|
||||||
inputs->push_back(SkString(*argv));
|
inputs->push_back(SkString(*argv));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inputs->count() < 2) {
|
if (inputs->count() < 1) {
|
||||||
usage(argv0);
|
usage(argv0);
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
@ -197,11 +216,11 @@ int tool_main(int argc, char** argv) {
|
|||||||
renderer(SkNEW(sk_tools::SimplePdfRenderer));
|
renderer(SkNEW(sk_tools::SimplePdfRenderer));
|
||||||
SkASSERT(renderer.get());
|
SkASSERT(renderer.get());
|
||||||
|
|
||||||
parse_commandline(argc, argv, &inputs);
|
SkString outputDir;
|
||||||
SkString outputDir = inputs[inputs.count() - 1];
|
parse_commandline(argc, argv, &inputs, &outputDir);
|
||||||
|
|
||||||
int failures = 0;
|
int failures = 0;
|
||||||
for (int i = 0; i < inputs.count() - 1; i ++) {
|
for (int i = 0; i < inputs.count(); i ++) {
|
||||||
failures += process_input(inputs[i], outputDir, *renderer);
|
failures += process_input(inputs[i], outputDir, *renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user