Chromium Code Reviews| Index: tools/render_pdfs_main.cpp |
| diff --git a/tools/render_pdfs_main.cpp b/tools/render_pdfs_main.cpp |
| index 6e1d609cde7391b8a1a52fe793ee0b499d45ea6f..367d256d9b2ba8ec62f682461e0bed9f011b9abe 100644 |
| --- a/tools/render_pdfs_main.cpp |
| +++ b/tools/render_pdfs_main.cpp |
| @@ -11,6 +11,7 @@ |
| #include "SkGraphics.h" |
| #include "SkImageEncoder.h" |
| #include "SkOSFile.h" |
| +#include "SkCommandLineFlags.h" |
|
mtklein
2014/08/06 18:48:36
questionable alphabetism?
hal.canary
2014/08/06 20:11:23
Done.
|
| #include "SkPicture.h" |
| #include "SkPixelRef.h" |
| #include "SkStream.h" |
| @@ -38,26 +39,28 @@ __SK_FORCE_IMAGE_DECODER_LINKING; |
| static const char PDF_FILE_EXTENSION[] = "pdf"; |
| static const char SKP_FILE_EXTENSION[] = "skp"; |
| -static void usage(const char* argv0) { |
| - SkDebugf("SKP to PDF rendering tool\n"); |
| - SkDebugf("\n" |
| -"Usage: \n" |
| -" %s <input>... [-w <outputDir>] [--jpegQuality N] \n" |
| -, argv0); |
| - SkDebugf("\n\n"); |
| - SkDebugf( |
| -" input: A list of directories and files to use as input. Files are\n" |
| -" expected to have the .skp extension.\n\n"); |
| - SkDebugf( |
| -" outputDir: directory to write the rendered pdfs.\n\n"); |
| - SkDebugf("\n"); |
| - SkDebugf( |
| -" jpegQuality N: encodes images in JPEG at quality level N, which can\n" |
| -" be in range 0-100).\n" |
| -" N = -1 will disable JPEG compression.\n" |
| -" Default is N = 100, maximum quality.\n\n"); |
| - SkDebugf("\n"); |
| -} |
| + |
| +DEFINE_string2(inputPaths, r, NULL, |
|
mtklein
2014/08/06 18:48:36
Usually we default strings to "".
hal.canary
2014/08/06 20:11:24
Done.
|
| + "A list of directories and files to use as input. " |
| + "Files are expected to have the .skp extension."); |
| + |
| +DEFINE_string2(outputDir, w, NULL, |
| + "Directory to write the rendered pdfs."); |
| + |
| +DEFINE_string2(match, m, NULL, |
| + "[~][^]substring[$] [...] of filenames to run.\n" |
| + "Multiple matches may be separated by spaces.\n" |
| + "~ causes a matching file to always be skipped\n" |
| + "^ requires the start of the file to match\n" |
| + "$ requires the end of the file to match\n" |
| + "^ and $ requires an exact match\n" |
| + "If a file does not match any list entry,\n" |
| + "it is skipped unless some list entry starts with ~"); |
| + |
| +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. |
| * @param path File name whose extension will be changed. |
| @@ -81,10 +84,9 @@ static bool replace_filename_extension(SkString* path, |
| return false; |
| } |
| -int gJpegQuality = 100; |
| // the size_t* parameter is deprecated, so we ignore it |
| static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) { |
| - if (gJpegQuality == -1) { |
| + if (FLAGS_jpegQuality == -1) { |
| return NULL; |
| } |
| @@ -97,9 +99,8 @@ static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) { |
| bm = copy; |
| #endif |
| - return SkImageEncoder::EncodeData(bm, |
| - SkImageEncoder::kJPEG_Type, |
| - gJpegQuality); |
| + return SkImageEncoder::EncodeData( |
| + bm, SkImageEncoder::kJPEG_Type, FLAGS_jpegQuality); |
| } |
| /** Builds the output filename. path = dir/name, and it replaces expected |
| @@ -147,26 +148,26 @@ static SkWStream* open_stream(const SkString& outputDir, |
| * @param outputDir Output dir. |
| * @param renderer The object responsible to render the skp object into pdf. |
| */ |
| -static bool render_pdf(const SkString& inputPath, const SkString& outputDir, |
| +static bool render_pdf(const char* inputPath, const SkString& outputDir, |
| sk_tools::PdfRenderer& renderer) { |
| - SkString inputFilename = SkOSPath::Basename(inputPath.c_str()); |
| + SkString inputFilename = SkOSPath::Basename(inputPath); |
| SkFILEStream inputStream; |
| - inputStream.setPath(inputPath.c_str()); |
| + inputStream.setPath(inputPath); |
| if (!inputStream.isValid()) { |
| - SkDebugf("Could not open file %s\n", inputPath.c_str()); |
| + SkDebugf("Could not open file %s\n", inputPath); |
| return false; |
| } |
| SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream)); |
| if (NULL == picture.get()) { |
| - SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); |
| + SkDebugf("Could not read an SkPicture from %s\n", inputPath); |
| return false; |
| } |
| - SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(), |
| - inputPath.c_str()); |
| + SkDebugf("exporting... [%-4i %6i] %s\n", |
| + picture->width(), picture->height(), inputPath); |
| SkWStream* stream(open_stream(outputDir, inputFilename)); |
| @@ -184,92 +185,82 @@ static bool render_pdf(const SkString& inputPath, const SkString& outputDir, |
| return success; |
| } |
| +#include <stdlib.h> |
| +static int compare_strings(const SkString& a, const SkString& b) { |
| + size_t len = (a.size() < b.size()) ? a.size() : b.size(); |
| + int cmp = memcmp(a.c_str(), b.c_str(), len); |
| + if (0 == cmp) { |
| + return b.size() - a.size(); |
| + } else { |
| + return cmp; |
| + } |
| +} |
| +static int compare_void_strings(const void* a,const void* b) { |
| + return compare_strings(*(const SkString*) a, |
| + *(const SkString*) b); |
| +} |
| + |
| +static void sort_string_array(SkTArray<SkString>* array) { |
| + qsort(array->begin(), array->count(), sizeof(SkString), |
|
mtklein
2014/08/06 18:48:36
Using qsort with non-POD data seems error prone.
hal.canary
2014/08/06 20:11:23
Done.
|
| + compare_void_strings); |
| +} |
| + |
| /** For each file in the directory or for the file passed in input, call |
| * render_pdf. |
| * @param input A directory or an skp file. |
| * @param outputDir Output dir. |
| * @param renderer The object responsible to render the skp object into pdf. |
| */ |
| -static int process_input(const SkString& input, const SkString& outputDir, |
| - sk_tools::PdfRenderer& renderer) { |
| - int failures = 0; |
| - if (sk_isdir(input.c_str())) { |
| - SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION); |
| - SkString inputFilename; |
| - while (iter.next(&inputFilename)) { |
| - SkString inputPath = SkOSPath::Join(input.c_str(), inputFilename.c_str()); |
| - if (!render_pdf(inputPath, outputDir, renderer)) { |
| - ++failures; |
| +static int process_input( |
| + const SkCommandLineFlags::StringArray& inputs, |
| + const SkString& outputDir, |
| + sk_tools::PdfRenderer& renderer) { |
| + SkTArray<SkString> files; |
| + for (int i = 0; i < inputs.count(); i ++) { |
| + const char* input = inputs[i]; |
| + if (sk_isdir(input)) { |
| + SkOSFile::Iter iter(input, SKP_FILE_EXTENSION); |
| + 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); |
| - if (!render_pdf(inputPath, outputDir, renderer)) { |
| + } |
| + sort_string_array(&files); |
|
mtklein
2014/08/06 18:48:36
Or maybe just don't sort?
hal.canary
2014/08/06 20:11:24
I am sorting so that I can easily verify that the
|
| + int failures = 0; |
| + for (int i = 0; i < files.count(); i ++) { |
| + if (!render_pdf(files[i].c_str(), outputDir, renderer)) { |
| ++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) { |
| + SkCommandLineFlags::Parse(argc, argv); |
| + |
| SkAutoGraphics ag; |
| - SkTArray<SkString> inputs; |
| SkAutoTUnref<sk_tools::PdfRenderer> |
| renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data))); |
| SkASSERT(renderer.get()); |
| SkString outputDir; |
| - parse_commandline(argc, argv, &inputs, &outputDir); |
| - |
| - int failures = 0; |
| - for (int i = 0; i < inputs.count(); i ++) { |
| - failures += process_input(inputs[i], outputDir, *renderer); |
| + if (FLAGS_outputDir.count() > 0) { |
| + outputDir = FLAGS_outputDir[0]; |
| } |
| + int failures = process_input(FLAGS_inputPaths, outputDir, *renderer); |
| + |
| if (failures != 0) { |
| SkDebugf("Failed to render %i PDFs.\n", failures); |
| return 1; |