OLD | NEW |
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 "SkCommandLineFlags.h" |
9 #include "SkDevice.h" | 10 #include "SkDevice.h" |
10 #include "SkForceLinking.h" | 11 #include "SkForceLinking.h" |
11 #include "SkGraphics.h" | 12 #include "SkGraphics.h" |
12 #include "SkImageEncoder.h" | 13 #include "SkImageEncoder.h" |
13 #include "SkOSFile.h" | 14 #include "SkOSFile.h" |
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" |
| 19 #include "SkTSort.h" |
18 #include "PdfRenderer.h" | 20 #include "PdfRenderer.h" |
19 #include "picture_utils.h" | 21 #include "picture_utils.h" |
20 | 22 |
21 __SK_FORCE_IMAGE_DECODER_LINKING; | 23 __SK_FORCE_IMAGE_DECODER_LINKING; |
22 | 24 |
23 #ifdef SK_USE_CDB | 25 #ifdef SK_USE_CDB |
24 #include "win_dbghelp.h" | 26 #include "win_dbghelp.h" |
25 #endif | 27 #endif |
26 | 28 |
27 /** | 29 /** |
28 * render_pdfs | 30 * render_pdfs |
29 * | 31 * |
30 * Given list of directories and files to use as input, expects to find .skp | 32 * 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 | 33 * files and it will convert them to .pdf files writing them in the output |
32 * directory. | 34 * directory. |
33 * | 35 * |
34 * Returns zero exit code if all .skp files were converted successfully, | 36 * Returns zero exit code if all .skp files were converted successfully, |
35 * otherwise returns error code 1. | 37 * otherwise returns error code 1. |
36 */ | 38 */ |
37 | 39 |
38 static const char PDF_FILE_EXTENSION[] = "pdf"; | 40 static const char PDF_FILE_EXTENSION[] = "pdf"; |
39 static const char SKP_FILE_EXTENSION[] = "skp"; | 41 static const char SKP_FILE_EXTENSION[] = "skp"; |
40 | 42 |
41 static void usage(const char* argv0) { | 43 |
42 SkDebugf("SKP to PDF rendering tool\n"); | 44 DEFINE_string2(inputPaths, r, "", |
43 SkDebugf("\n" | 45 "A list of directories and files to use as input. " |
44 "Usage: \n" | 46 "Files are expected to have the .skp extension."); |
45 " %s <input>... [-w <outputDir>] [--jpegQuality N] \n" | 47 |
46 , argv0); | 48 DEFINE_string2(outputDir, w, "", |
47 SkDebugf("\n\n"); | 49 "Directory to write the rendered pdfs."); |
48 SkDebugf( | 50 |
49 " input: A list of directories and files to use as input. Files are\n" | 51 DEFINE_string2(match, m, "", |
50 " expected to have the .skp extension.\n\n"); | 52 "[~][^]substring[$] [...] of filenames to run.\n" |
51 SkDebugf( | 53 "Multiple matches may be separated by spaces.\n" |
52 " outputDir: directory to write the rendered pdfs.\n\n"); | 54 "~ causes a matching file to always be skipped\n" |
53 SkDebugf("\n"); | 55 "^ requires the start of the file to match\n" |
54 SkDebugf( | 56 "$ requires the end of the file to match\n" |
55 " jpegQuality N: encodes images in JPEG at quality level N, which can\n" | 57 "^ and $ requires an exact match\n" |
56 " be in range 0-100).\n" | 58 "If a file does not match any list entry,\n" |
57 " N = -1 will disable JPEG compression.\n" | 59 "it is skipped unless some list entry starts with ~"); |
58 " Default is N = 100, maximum quality.\n\n"); | 60 |
59 SkDebugf("\n"); | 61 DEFINE_int32(jpegQuality, 100, |
60 } | 62 "Encodes images in JPEG at quality level N, which can be in " |
| 63 "range 0-100). N = -1 will disable JPEG compression. " |
| 64 "Default is N = 100, maximum quality."); |
61 | 65 |
62 /** Replaces the extension of a file. | 66 /** Replaces the extension of a file. |
63 * @param path File name whose extension will be changed. | 67 * @param path File name whose extension will be changed. |
64 * @param old_extension The old extension. | 68 * @param old_extension The old extension. |
65 * @param new_extension The new extension. | 69 * @param new_extension The new extension. |
66 * @returns false if the file did not has the expected extension. | 70 * @returns false if the file did not has the expected extension. |
67 * if false is returned, contents of path are undefined. | 71 * if false is returned, contents of path are undefined. |
68 */ | 72 */ |
69 static bool replace_filename_extension(SkString* path, | 73 static bool replace_filename_extension(SkString* path, |
70 const char old_extension[], | 74 const char old_extension[], |
71 const char new_extension[]) { | 75 const char new_extension[]) { |
72 if (path->endsWith(old_extension)) { | 76 if (path->endsWith(old_extension)) { |
73 path->remove(path->size() - strlen(old_extension), | 77 path->remove(path->size() - strlen(old_extension), |
74 strlen(old_extension)); | 78 strlen(old_extension)); |
75 if (!path->endsWith(".")) { | 79 if (!path->endsWith(".")) { |
76 return false; | 80 return false; |
77 } | 81 } |
78 path->append(new_extension); | 82 path->append(new_extension); |
79 return true; | 83 return true; |
80 } | 84 } |
81 return false; | 85 return false; |
82 } | 86 } |
83 | 87 |
84 int gJpegQuality = 100; | |
85 // the size_t* parameter is deprecated, so we ignore it | 88 // the size_t* parameter is deprecated, so we ignore it |
86 static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) { | 89 static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) { |
87 if (gJpegQuality == -1) { | 90 if (FLAGS_jpegQuality == -1) { |
88 return NULL; | 91 return NULL; |
89 } | 92 } |
90 | 93 |
91 SkBitmap bm = bitmap; | 94 SkBitmap bm = bitmap; |
92 #if defined(SK_BUILD_FOR_MAC) | 95 #if defined(SK_BUILD_FOR_MAC) |
93 // Workaround bug #1043 where bitmaps with referenced pixels cause | 96 // Workaround bug #1043 where bitmaps with referenced pixels cause |
94 // CGImageDestinationFinalize to crash | 97 // CGImageDestinationFinalize to crash |
95 SkBitmap copy; | 98 SkBitmap copy; |
96 bitmap.deepCopyTo(©); | 99 bitmap.deepCopyTo(©); |
97 bm = copy; | 100 bm = copy; |
98 #endif | 101 #endif |
99 | 102 |
100 return SkImageEncoder::EncodeData(bm, | 103 return SkImageEncoder::EncodeData( |
101 SkImageEncoder::kJPEG_Type, | 104 bm, SkImageEncoder::kJPEG_Type, FLAGS_jpegQuality); |
102 gJpegQuality); | |
103 } | 105 } |
104 | 106 |
105 /** Builds the output filename. path = dir/name, and it replaces expected | 107 /** Builds the output filename. path = dir/name, and it replaces expected |
106 * .skp extension with .pdf extention. | 108 * .skp extension with .pdf extention. |
107 * @param path Output filename. | 109 * @param path Output filename. |
108 * @param name The name of the file. | 110 * @param name The name of the file. |
109 * @returns false if the file did not has the expected extension. | 111 * @returns false if the file did not has the expected extension. |
110 * if false is returned, contents of path are undefined. | 112 * if false is returned, contents of path are undefined. |
111 */ | 113 */ |
112 static bool make_output_filepath(SkString* path, const SkString& dir, | 114 static bool make_output_filepath(SkString* path, const SkString& dir, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 return false; | 160 return false; |
159 } | 161 } |
160 | 162 |
161 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream)); | 163 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream)); |
162 | 164 |
163 if (NULL == picture.get()) { | 165 if (NULL == picture.get()) { |
164 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); | 166 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); |
165 return false; | 167 return false; |
166 } | 168 } |
167 | 169 |
168 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(), | 170 SkDebugf("exporting... [%-4i %6i] %s\n", |
169 inputPath.c_str()); | 171 picture->width(), picture->height(), inputPath.c_str()); |
170 | 172 |
171 SkWStream* stream(open_stream(outputDir, inputFilename)); | 173 SkWStream* stream(open_stream(outputDir, inputFilename)); |
172 | 174 |
173 if (!stream) { | 175 if (!stream) { |
174 return false; | 176 return false; |
175 } | 177 } |
176 | 178 |
177 renderer.init(picture, stream); | 179 renderer.init(picture, stream); |
178 | 180 |
179 bool success = renderer.render(); | 181 bool success = renderer.render(); |
180 SkDELETE(stream); | 182 SkDELETE(stream); |
181 | 183 |
182 renderer.end(); | 184 renderer.end(); |
183 | 185 |
184 return success; | 186 return success; |
185 } | 187 } |
186 | 188 |
| 189 static bool operator<(const SkString& a, const SkString& b) { |
| 190 return strcmp(a.c_str(), b.c_str()) < 0; |
| 191 } |
| 192 |
187 /** For each file in the directory or for the file passed in input, call | 193 /** For each file in the directory or for the file passed in input, call |
188 * render_pdf. | 194 * render_pdf. |
189 * @param input A directory or an skp file. | 195 * @param input A directory or an skp file. |
190 * @param outputDir Output dir. | 196 * @param outputDir Output dir. |
191 * @param renderer The object responsible to render the skp object into pdf. | 197 * @param renderer The object responsible to render the skp object into pdf. |
192 */ | 198 */ |
193 static int process_input(const SkString& input, const SkString& outputDir, | 199 static int process_input( |
194 sk_tools::PdfRenderer& renderer) { | 200 const SkCommandLineFlags::StringArray& inputs, |
195 int failures = 0; | 201 const SkString& outputDir, |
196 if (sk_isdir(input.c_str())) { | 202 sk_tools::PdfRenderer& renderer) { |
197 SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION); | 203 SkTArray<SkString> files; |
198 SkString inputFilename; | 204 for (int i = 0; i < inputs.count(); i ++) { |
199 while (iter.next(&inputFilename)) { | 205 const char* input = inputs[i]; |
200 SkString inputPath = SkOSPath::Join(input.c_str(), inputFilename.c_s
tr()); | 206 if (sk_isdir(input)) { |
201 if (!render_pdf(inputPath, outputDir, renderer)) { | 207 SkOSFile::Iter iter(input, SKP_FILE_EXTENSION); |
202 ++failures; | 208 SkString inputFilename; |
| 209 while (iter.next(&inputFilename)) { |
| 210 if (!SkCommandLineFlags::ShouldSkip( |
| 211 FLAGS_match, inputFilename.c_str())) { |
| 212 files.push_back( |
| 213 SkOSPath::Join(input, inputFilename.c_str())); |
| 214 } |
| 215 } |
| 216 } else { |
| 217 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, input)) { |
| 218 files.push_back(SkString(input)); |
203 } | 219 } |
204 } | 220 } |
205 } else { | 221 } |
206 SkString inputPath(input); | 222 SkTQSort<SkString>(files.begin(), files.end() - 1); |
207 if (!render_pdf(inputPath, outputDir, renderer)) { | 223 int failures = 0; |
| 224 for (int i = 0; i < files.count(); i ++) { |
| 225 if (!render_pdf(files[i], outputDir, renderer)) { |
208 ++failures; | 226 ++failures; |
209 } | 227 } |
210 } | 228 } |
211 return failures; | 229 return failures; |
212 } | 230 } |
213 | 231 |
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); | 232 int tool_main_core(int argc, char** argv); |
257 int tool_main_core(int argc, char** argv) { | 233 int tool_main_core(int argc, char** argv) { |
| 234 SkCommandLineFlags::Parse(argc, argv); |
| 235 |
258 SkAutoGraphics ag; | 236 SkAutoGraphics ag; |
259 SkTArray<SkString> inputs; | |
260 | 237 |
261 SkAutoTUnref<sk_tools::PdfRenderer> | 238 SkAutoTUnref<sk_tools::PdfRenderer> |
262 renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data))); | 239 renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data))); |
263 SkASSERT(renderer.get()); | 240 SkASSERT(renderer.get()); |
264 | 241 |
265 SkString outputDir; | 242 SkString outputDir; |
266 parse_commandline(argc, argv, &inputs, &outputDir); | 243 if (FLAGS_outputDir.count() > 0) { |
| 244 outputDir = FLAGS_outputDir[0]; |
| 245 } |
267 | 246 |
268 int failures = 0; | 247 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 | 248 |
273 if (failures != 0) { | 249 if (failures != 0) { |
274 SkDebugf("Failed to render %i PDFs.\n", failures); | 250 SkDebugf("Failed to render %i PDFs.\n", failures); |
275 return 1; | 251 return 1; |
276 } | 252 } |
277 | 253 |
278 return 0; | 254 return 0; |
279 } | 255 } |
280 | 256 |
281 int tool_main(int argc, char** argv); | 257 int tool_main(int argc, char** argv); |
(...skipping 11 matching lines...) Expand all Loading... |
293 } | 269 } |
294 #endif | 270 #endif |
295 return 0; | 271 return 0; |
296 } | 272 } |
297 | 273 |
298 #if !defined SK_BUILD_FOR_IOS | 274 #if !defined SK_BUILD_FOR_IOS |
299 int main(int argc, char * const argv[]) { | 275 int main(int argc, char * const argv[]) { |
300 return tool_main(argc, (char**) argv); | 276 return tool_main(argc, (char**) argv); |
301 } | 277 } |
302 #endif | 278 #endif |
OLD | NEW |