render_pdfs implements --match

BUG=skia:2743
NOTRY=true
R=djsollen@google.com, mtklein@google.com, borenet@google.com

Author: halcanary@google.com

Review URL: https://codereview.chromium.org/441423002
This commit is contained in:
halcanary 2014-08-07 07:24:47 -07:00 committed by Commit bot
parent c72e870a35
commit 0bef17a678
2 changed files with 66 additions and 88 deletions

View File

@ -521,10 +521,12 @@
'../tools/PdfRenderer.h', '../tools/PdfRenderer.h',
], ],
'include_dirs': [ 'include_dirs': [
'../src/core',
'../src/pipe/utils/', '../src/pipe/utils/',
'../src/utils/', '../src/utils/',
], ],
'dependencies': [ 'dependencies': [
'flags.gyp:flags',
'pdf.gyp:pdf', 'pdf.gyp:pdf',
'skia_lib.gyp:skia_lib', 'skia_lib.gyp:skia_lib',
'tools.gyp:picture_utils', 'tools.gyp:picture_utils',

View File

@ -6,6 +6,7 @@
*/ */
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkCommandLineFlags.h"
#include "SkDevice.h" #include "SkDevice.h"
#include "SkForceLinking.h" #include "SkForceLinking.h"
#include "SkGraphics.h" #include "SkGraphics.h"
@ -15,6 +16,7 @@
#include "SkPixelRef.h" #include "SkPixelRef.h"
#include "SkStream.h" #include "SkStream.h"
#include "SkTArray.h" #include "SkTArray.h"
#include "SkTSort.h"
#include "PdfRenderer.h" #include "PdfRenderer.h"
#include "picture_utils.h" #include "picture_utils.h"
@ -38,26 +40,28 @@ __SK_FORCE_IMAGE_DECODER_LINKING;
static const char PDF_FILE_EXTENSION[] = "pdf"; static const char PDF_FILE_EXTENSION[] = "pdf";
static const char SKP_FILE_EXTENSION[] = "skp"; static const char SKP_FILE_EXTENSION[] = "skp";
static void usage(const char* argv0) {
SkDebugf("SKP to PDF rendering tool\n"); DEFINE_string2(inputPaths, r, "",
SkDebugf("\n" "A list of directories and files to use as input. "
"Usage: \n" "Files are expected to have the .skp extension.");
" %s <input>... [-w <outputDir>] [--jpegQuality N] \n"
, argv0); DEFINE_string2(outputDir, w, "",
SkDebugf("\n\n"); "Directory to write the rendered pdfs.");
SkDebugf(
" input: A list of directories and files to use as input. Files are\n" DEFINE_string2(match, m, "",
" expected to have the .skp extension.\n\n"); "[~][^]substring[$] [...] of filenames to run.\n"
SkDebugf( "Multiple matches may be separated by spaces.\n"
" outputDir: directory to write the rendered pdfs.\n\n"); "~ causes a matching file to always be skipped\n"
SkDebugf("\n"); "^ requires the start of the file to match\n"
SkDebugf( "$ requires the end of the file to match\n"
" jpegQuality N: encodes images in JPEG at quality level N, which can\n" "^ and $ requires an exact match\n"
" be in range 0-100).\n" "If a file does not match any list entry,\n"
" N = -1 will disable JPEG compression.\n" "it is skipped unless some list entry starts with ~");
" Default is N = 100, maximum quality.\n\n");
SkDebugf("\n"); DEFINE_int32(jpegQuality, 100,
} "Encodes images in JPEG at quality level N, which can be in "
"range 0-100). N = -1 will disable JPEG compression. "
"Default is N = 100, maximum quality.");
/** Replaces the extension of a file. /** Replaces the extension of a file.
* @param path File name whose extension will be changed. * @param path File name whose extension will be changed.
@ -81,10 +85,9 @@ static bool replace_filename_extension(SkString* path,
return false; return false;
} }
int gJpegQuality = 100;
// the size_t* parameter is deprecated, so we ignore it // the size_t* parameter is deprecated, so we ignore it
static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) { static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) {
if (gJpegQuality == -1) { if (FLAGS_jpegQuality == -1) {
return NULL; return NULL;
} }
@ -97,9 +100,8 @@ static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) {
bm = copy; bm = copy;
#endif #endif
return SkImageEncoder::EncodeData(bm, return SkImageEncoder::EncodeData(
SkImageEncoder::kJPEG_Type, bm, SkImageEncoder::kJPEG_Type, FLAGS_jpegQuality);
gJpegQuality);
} }
/** Builds the output filename. path = dir/name, and it replaces expected /** Builds the output filename. path = dir/name, and it replaces expected
@ -165,8 +167,8 @@ static bool render_pdf(const SkString& inputPath, const SkString& outputDir,
return false; return false;
} }
SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(), SkDebugf("exporting... [%-4i %6i] %s\n",
inputPath.c_str()); picture->width(), picture->height(), inputPath.c_str());
SkWStream* stream(open_stream(outputDir, inputFilename)); SkWStream* stream(open_stream(outputDir, inputFilename));
@ -184,92 +186,66 @@ static bool render_pdf(const SkString& inputPath, const SkString& outputDir,
return success; return success;
} }
static bool operator<(const SkString& a, const SkString& b) {
return strcmp(a.c_str(), b.c_str()) < 0;
}
/** For each file in the directory or for the file passed in input, call /** For each file in the directory or for the file passed in input, call
* render_pdf. * render_pdf.
* @param input A directory or an skp file. * @param input A directory or an skp file.
* @param outputDir Output dir. * @param outputDir Output dir.
* @param renderer The object responsible to render the skp object into pdf. * @param renderer The object responsible to render the skp object into pdf.
*/ */
static int process_input(const SkString& input, const SkString& outputDir, static int process_input(
sk_tools::PdfRenderer& renderer) { const SkCommandLineFlags::StringArray& inputs,
int failures = 0; const SkString& outputDir,
if (sk_isdir(input.c_str())) { sk_tools::PdfRenderer& renderer) {
SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION); SkTArray<SkString> files;
SkString inputFilename; for (int i = 0; i < inputs.count(); i ++) {
while (iter.next(&inputFilename)) { const char* input = inputs[i];
SkString inputPath = SkOSPath::Join(input.c_str(), inputFilename.c_str()); if (sk_isdir(input)) {
if (!render_pdf(inputPath, outputDir, renderer)) { SkOSFile::Iter iter(input, SKP_FILE_EXTENSION);
++failures; SkString inputFilename;
while (iter.next(&inputFilename)) {
if (!SkCommandLineFlags::ShouldSkip(
FLAGS_match, inputFilename.c_str())) {
files.push_back(
SkOSPath::Join(input, inputFilename.c_str()));
}
}
} else {
if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, input)) {
files.push_back(SkString(input));
} }
} }
} else { }
SkString inputPath(input); SkTQSort<SkString>(files.begin(), files.end() - 1);
if (!render_pdf(inputPath, outputDir, renderer)) { int failures = 0;
for (int i = 0; i < files.count(); i ++) {
if (!render_pdf(files[i], outputDir, renderer)) {
++failures; ++failures;
} }
} }
return failures; return failures;
} }
static void parse_commandline(int argc, char* const argv[],
SkTArray<SkString>* inputs,
SkString* outputDir) {
const char* argv0 = argv[0];
char* const* stop = argv + argc;
for (++argv; argv < stop; ++argv) {
if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
usage(argv0);
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 if (0 == strcmp(*argv, "--jpegQuality")) {
++argv;
if (argv >= stop) {
SkDebugf("Missing argument for --jpegQuality\n");
usage(argv0);
exit(-1);
}
gJpegQuality = atoi(*argv);
if (gJpegQuality < -1 || gJpegQuality > 100) {
SkDebugf("Invalid argument for --jpegQuality\n");
usage(argv0);
exit(-1);
}
} else {
inputs->push_back(SkString(*argv));
}
}
if (inputs->count() < 1) {
usage(argv0);
exit(-1);
}
}
int tool_main_core(int argc, char** argv); int tool_main_core(int argc, char** argv);
int tool_main_core(int argc, char** argv) { int tool_main_core(int argc, char** argv) {
SkCommandLineFlags::Parse(argc, argv);
SkAutoGraphics ag; SkAutoGraphics ag;
SkTArray<SkString> inputs;
SkAutoTUnref<sk_tools::PdfRenderer> SkAutoTUnref<sk_tools::PdfRenderer>
renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data))); renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data)));
SkASSERT(renderer.get()); SkASSERT(renderer.get());
SkString outputDir; SkString outputDir;
parse_commandline(argc, argv, &inputs, &outputDir); if (FLAGS_outputDir.count() > 0) {
outputDir = FLAGS_outputDir[0];
int failures = 0;
for (int i = 0; i < inputs.count(); i ++) {
failures += process_input(inputs[i], outputDir, *renderer);
} }
int failures = process_input(FLAGS_inputPaths, outputDir, *renderer);
if (failures != 0) { if (failures != 0) {
SkDebugf("Failed to render %i PDFs.\n", failures); SkDebugf("Failed to render %i PDFs.\n", failures);
return 1; return 1;