Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: tools/render_pdfs_main.cpp

Issue 441423002: render_pdfs - don't render all skps (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: change command line flags, --match flag. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« gyp/tools.gyp ('K') | « gyp/tools.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkCanvas.h" 8 #include "SkCanvas.h"
9 #include "SkDevice.h" 9 #include "SkDevice.h"
10 #include "SkForceLinking.h" 10 #include "SkForceLinking.h"
11 #include "SkGraphics.h" 11 #include "SkGraphics.h"
12 #include "SkImageEncoder.h" 12 #include "SkImageEncoder.h"
13 #include "SkOSFile.h" 13 #include "SkOSFile.h"
14 #include "SkCommandLineFlags.h"
mtklein 2014/08/06 18:48:36 questionable alphabetism?
hal.canary 2014/08/06 20:11:23 Done.
14 #include "SkPicture.h" 15 #include "SkPicture.h"
15 #include "SkPixelRef.h" 16 #include "SkPixelRef.h"
16 #include "SkStream.h" 17 #include "SkStream.h"
17 #include "SkTArray.h" 18 #include "SkTArray.h"
18 #include "PdfRenderer.h" 19 #include "PdfRenderer.h"
19 #include "picture_utils.h" 20 #include "picture_utils.h"
20 21
21 __SK_FORCE_IMAGE_DECODER_LINKING; 22 __SK_FORCE_IMAGE_DECODER_LINKING;
22 23
23 #ifdef SK_USE_CDB 24 #ifdef SK_USE_CDB
24 #include "win_dbghelp.h" 25 #include "win_dbghelp.h"
25 #endif 26 #endif
26 27
27 /** 28 /**
28 * render_pdfs 29 * render_pdfs
29 * 30 *
30 * Given list of directories and files to use as input, expects to find .skp 31 * Given list of directories and files to use as input, expects to find .skp
31 * files and it will convert them to .pdf files writing them in the output 32 * files and it will convert them to .pdf files writing them in the output
32 * directory. 33 * directory.
33 * 34 *
34 * Returns zero exit code if all .skp files were converted successfully, 35 * Returns zero exit code if all .skp files were converted successfully,
35 * otherwise returns error code 1. 36 * otherwise returns error code 1.
36 */ 37 */
37 38
38 static const char PDF_FILE_EXTENSION[] = "pdf"; 39 static const char PDF_FILE_EXTENSION[] = "pdf";
39 static const char SKP_FILE_EXTENSION[] = "skp"; 40 static const char SKP_FILE_EXTENSION[] = "skp";
40 41
41 static void usage(const char* argv0) { 42
42 SkDebugf("SKP to PDF rendering tool\n"); 43 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.
43 SkDebugf("\n" 44 "A list of directories and files to use as input. "
44 "Usage: \n" 45 "Files are expected to have the .skp extension.");
45 " %s <input>... [-w <outputDir>] [--jpegQuality N] \n" 46
46 , argv0); 47 DEFINE_string2(outputDir, w, NULL,
47 SkDebugf("\n\n"); 48 "Directory to write the rendered pdfs.");
48 SkDebugf( 49
49 " input: A list of directories and files to use as input. Files are\n" 50 DEFINE_string2(match, m, NULL,
50 " expected to have the .skp extension.\n\n"); 51 "[~][^]substring[$] [...] of filenames to run.\n"
51 SkDebugf( 52 "Multiple matches may be separated by spaces.\n"
52 " outputDir: directory to write the rendered pdfs.\n\n"); 53 "~ causes a matching file to always be skipped\n"
53 SkDebugf("\n"); 54 "^ requires the start of the file to match\n"
54 SkDebugf( 55 "$ requires the end of the file to match\n"
55 " jpegQuality N: encodes images in JPEG at quality level N, which can\n" 56 "^ and $ requires an exact match\n"
56 " be in range 0-100).\n" 57 "If a file does not match any list entry,\n"
57 " N = -1 will disable JPEG compression.\n" 58 "it is skipped unless some list entry starts with ~");
58 " Default is N = 100, maximum quality.\n\n"); 59
59 SkDebugf("\n"); 60 DEFINE_int32(jpegQuality, 100,
60 } 61 "Encodes images in JPEG at quality level N, which can be in "
62 "range 0-100). N = -1 will disable JPEG compression. "
63 "Default is N = 100, maximum quality.");
61 64
62 /** Replaces the extension of a file. 65 /** Replaces the extension of a file.
63 * @param path File name whose extension will be changed. 66 * @param path File name whose extension will be changed.
64 * @param old_extension The old extension. 67 * @param old_extension The old extension.
65 * @param new_extension The new extension. 68 * @param new_extension The new extension.
66 * @returns false if the file did not has the expected extension. 69 * @returns false if the file did not has the expected extension.
67 * if false is returned, contents of path are undefined. 70 * if false is returned, contents of path are undefined.
68 */ 71 */
69 static bool replace_filename_extension(SkString* path, 72 static bool replace_filename_extension(SkString* path,
70 const char old_extension[], 73 const char old_extension[],
71 const char new_extension[]) { 74 const char new_extension[]) {
72 if (path->endsWith(old_extension)) { 75 if (path->endsWith(old_extension)) {
73 path->remove(path->size() - strlen(old_extension), 76 path->remove(path->size() - strlen(old_extension),
74 strlen(old_extension)); 77 strlen(old_extension));
75 if (!path->endsWith(".")) { 78 if (!path->endsWith(".")) {
76 return false; 79 return false;
77 } 80 }
78 path->append(new_extension); 81 path->append(new_extension);
79 return true; 82 return true;
80 } 83 }
81 return false; 84 return false;
82 } 85 }
83 86
84 int gJpegQuality = 100;
85 // the size_t* parameter is deprecated, so we ignore it 87 // the size_t* parameter is deprecated, so we ignore it
86 static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) { 88 static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) {
87 if (gJpegQuality == -1) { 89 if (FLAGS_jpegQuality == -1) {
88 return NULL; 90 return NULL;
89 } 91 }
90 92
91 SkBitmap bm = bitmap; 93 SkBitmap bm = bitmap;
92 #if defined(SK_BUILD_FOR_MAC) 94 #if defined(SK_BUILD_FOR_MAC)
93 // Workaround bug #1043 where bitmaps with referenced pixels cause 95 // Workaround bug #1043 where bitmaps with referenced pixels cause
94 // CGImageDestinationFinalize to crash 96 // CGImageDestinationFinalize to crash
95 SkBitmap copy; 97 SkBitmap copy;
96 bitmap.deepCopyTo(&copy); 98 bitmap.deepCopyTo(&copy);
97 bm = copy; 99 bm = copy;
98 #endif 100 #endif
99 101
100 return SkImageEncoder::EncodeData(bm, 102 return SkImageEncoder::EncodeData(
101 SkImageEncoder::kJPEG_Type, 103 bm, SkImageEncoder::kJPEG_Type, FLAGS_jpegQuality);
102 gJpegQuality);
103 } 104 }
104 105
105 /** Builds the output filename. path = dir/name, and it replaces expected 106 /** Builds the output filename. path = dir/name, and it replaces expected
106 * .skp extension with .pdf extention. 107 * .skp extension with .pdf extention.
107 * @param path Output filename. 108 * @param path Output filename.
108 * @param name The name of the file. 109 * @param name The name of the file.
109 * @returns false if the file did not has the expected extension. 110 * @returns false if the file did not has the expected extension.
110 * if false is returned, contents of path are undefined. 111 * if false is returned, contents of path are undefined.
111 */ 112 */
112 static bool make_output_filepath(SkString* path, const SkString& dir, 113 static bool make_output_filepath(SkString* path, const SkString& dir,
(...skipping 27 matching lines...) Expand all
140 } 141 }
141 142
142 return stream; 143 return stream;
143 } 144 }
144 145
145 /** Reads an skp file, renders it to pdf and writes the output to a pdf file 146 /** Reads an skp file, renders it to pdf and writes the output to a pdf file
146 * @param inputPath The skp file to be read. 147 * @param inputPath The skp file to be read.
147 * @param outputDir Output dir. 148 * @param outputDir Output dir.
148 * @param renderer The object responsible to render the skp object into pdf. 149 * @param renderer The object responsible to render the skp object into pdf.
149 */ 150 */
150 static bool render_pdf(const SkString& inputPath, const SkString& outputDir, 151 static bool render_pdf(const char* inputPath, const SkString& outputDir,
151 sk_tools::PdfRenderer& renderer) { 152 sk_tools::PdfRenderer& renderer) {
152 SkString inputFilename = SkOSPath::Basename(inputPath.c_str()); 153 SkString inputFilename = SkOSPath::Basename(inputPath);
153 154
154 SkFILEStream inputStream; 155 SkFILEStream inputStream;
155 inputStream.setPath(inputPath.c_str()); 156 inputStream.setPath(inputPath);
156 if (!inputStream.isValid()) { 157 if (!inputStream.isValid()) {
157 SkDebugf("Could not open file %s\n", inputPath.c_str()); 158 SkDebugf("Could not open file %s\n", inputPath);
158 return false; 159 return false;
159 } 160 }
160 161
161 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream)); 162 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream));
162 163
163 if (NULL == picture.get()) { 164 if (NULL == picture.get()) {
164 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); 165 SkDebugf("Could not read an SkPicture from %s\n", inputPath);
165 return false; 166 return false;
166 } 167 }
167 168
168 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(), 169 SkDebugf("exporting... [%-4i %6i] %s\n",
169 inputPath.c_str()); 170 picture->width(), picture->height(), inputPath);
170 171
171 SkWStream* stream(open_stream(outputDir, inputFilename)); 172 SkWStream* stream(open_stream(outputDir, inputFilename));
172 173
173 if (!stream) { 174 if (!stream) {
174 return false; 175 return false;
175 } 176 }
176 177
177 renderer.init(picture, stream); 178 renderer.init(picture, stream);
178 179
179 bool success = renderer.render(); 180 bool success = renderer.render();
180 SkDELETE(stream); 181 SkDELETE(stream);
181 182
182 renderer.end(); 183 renderer.end();
183 184
184 return success; 185 return success;
185 } 186 }
186 187
188 #include <stdlib.h>
189 static int compare_strings(const SkString& a, const SkString& b) {
190 size_t len = (a.size() < b.size()) ? a.size() : b.size();
191 int cmp = memcmp(a.c_str(), b.c_str(), len);
192 if (0 == cmp) {
193 return b.size() - a.size();
194 } else {
195 return cmp;
196 }
197 }
198 static int compare_void_strings(const void* a,const void* b) {
199 return compare_strings(*(const SkString*) a,
200 *(const SkString*) b);
201 }
202
203 static void sort_string_array(SkTArray<SkString>* array) {
204 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.
205 compare_void_strings);
206 }
207
187 /** For each file in the directory or for the file passed in input, call 208 /** For each file in the directory or for the file passed in input, call
188 * render_pdf. 209 * render_pdf.
189 * @param input A directory or an skp file. 210 * @param input A directory or an skp file.
190 * @param outputDir Output dir. 211 * @param outputDir Output dir.
191 * @param renderer The object responsible to render the skp object into pdf. 212 * @param renderer The object responsible to render the skp object into pdf.
192 */ 213 */
193 static int process_input(const SkString& input, const SkString& outputDir, 214 static int process_input(
194 sk_tools::PdfRenderer& renderer) { 215 const SkCommandLineFlags::StringArray& inputs,
195 int failures = 0; 216 const SkString& outputDir,
196 if (sk_isdir(input.c_str())) { 217 sk_tools::PdfRenderer& renderer) {
197 SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION); 218 SkTArray<SkString> files;
198 SkString inputFilename; 219 for (int i = 0; i < inputs.count(); i ++) {
199 while (iter.next(&inputFilename)) { 220 const char* input = inputs[i];
200 SkString inputPath = SkOSPath::Join(input.c_str(), inputFilename.c_s tr()); 221 if (sk_isdir(input)) {
201 if (!render_pdf(inputPath, outputDir, renderer)) { 222 SkOSFile::Iter iter(input, SKP_FILE_EXTENSION);
202 ++failures; 223 SkString inputFilename;
224 while (iter.next(&inputFilename)) {
225 if (!SkCommandLineFlags::ShouldSkip(
226 FLAGS_match, inputFilename.c_str())) {
227 files.push_back(
228 SkOSPath::Join(input, inputFilename.c_str()));
229 }
230 }
231 } else {
232 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, input)) {
233 files.push_back(SkString(input));
203 } 234 }
204 } 235 }
205 } else { 236 }
206 SkString inputPath(input); 237 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
207 if (!render_pdf(inputPath, outputDir, renderer)) { 238 int failures = 0;
239 for (int i = 0; i < files.count(); i ++) {
240 if (!render_pdf(files[i].c_str(), outputDir, renderer)) {
208 ++failures; 241 ++failures;
209 } 242 }
210 } 243 }
211 return failures; 244 return failures;
212 } 245 }
213 246
214 static void parse_commandline(int argc, char* const argv[],
215 SkTArray<SkString>* inputs,
216 SkString* outputDir) {
217 const char* argv0 = argv[0];
218 char* const* stop = argv + argc;
219
220 for (++argv; argv < stop; ++argv) {
221 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
222 usage(argv0);
223 exit(-1);
224 } else if (0 == strcmp(*argv, "-w")) {
225 ++argv;
226 if (argv >= stop) {
227 SkDebugf("Missing outputDir for -w\n");
228 usage(argv0);
229 exit(-1);
230 }
231 *outputDir = SkString(*argv);
232 } else if (0 == strcmp(*argv, "--jpegQuality")) {
233 ++argv;
234 if (argv >= stop) {
235 SkDebugf("Missing argument for --jpegQuality\n");
236 usage(argv0);
237 exit(-1);
238 }
239 gJpegQuality = atoi(*argv);
240 if (gJpegQuality < -1 || gJpegQuality > 100) {
241 SkDebugf("Invalid argument for --jpegQuality\n");
242 usage(argv0);
243 exit(-1);
244 }
245 } else {
246 inputs->push_back(SkString(*argv));
247 }
248 }
249
250 if (inputs->count() < 1) {
251 usage(argv0);
252 exit(-1);
253 }
254 }
255
256 int tool_main_core(int argc, char** argv); 247 int tool_main_core(int argc, char** argv);
257 int tool_main_core(int argc, char** argv) { 248 int tool_main_core(int argc, char** argv) {
249 SkCommandLineFlags::Parse(argc, argv);
250
258 SkAutoGraphics ag; 251 SkAutoGraphics ag;
259 SkTArray<SkString> inputs;
260 252
261 SkAutoTUnref<sk_tools::PdfRenderer> 253 SkAutoTUnref<sk_tools::PdfRenderer>
262 renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data))); 254 renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data)));
263 SkASSERT(renderer.get()); 255 SkASSERT(renderer.get());
264 256
265 SkString outputDir; 257 SkString outputDir;
266 parse_commandline(argc, argv, &inputs, &outputDir); 258 if (FLAGS_outputDir.count() > 0) {
259 outputDir = FLAGS_outputDir[0];
260 }
267 261
268 int failures = 0; 262 int failures = process_input(FLAGS_inputPaths, outputDir, *renderer);
269 for (int i = 0; i < inputs.count(); i ++) {
270 failures += process_input(inputs[i], outputDir, *renderer);
271 }
272 263
273 if (failures != 0) { 264 if (failures != 0) {
274 SkDebugf("Failed to render %i PDFs.\n", failures); 265 SkDebugf("Failed to render %i PDFs.\n", failures);
275 return 1; 266 return 1;
276 } 267 }
277 268
278 return 0; 269 return 0;
279 } 270 }
280 271
281 int tool_main(int argc, char** argv); 272 int tool_main(int argc, char** argv);
(...skipping 11 matching lines...) Expand all
293 } 284 }
294 #endif 285 #endif
295 return 0; 286 return 0;
296 } 287 }
297 288
298 #if !defined SK_BUILD_FOR_IOS 289 #if !defined SK_BUILD_FOR_IOS
299 int main(int argc, char * const argv[]) { 290 int main(int argc, char * const argv[]) {
300 return tool_main(argc, (char**) argv); 291 return tool_main(argc, (char**) argv);
301 } 292 }
302 #endif 293 #endif
OLDNEW
« gyp/tools.gyp ('K') | « gyp/tools.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698