blob: 1821548aa34bc901d5c8a13c11acca5322cfc949 [file] [log] [blame]
edisonn@google.comc319abe2012-11-01 19:52:38 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkCanvas.h"
9#include "SkDevice.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000010#include "SkForceLinking.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000011#include "SkGraphics.h"
scroggo@google.comf8d7d272013-02-22 21:38:35 +000012#include "SkImageDecoder.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000013#include "SkImageEncoder.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000014#include "SkOSFile.h"
15#include "SkPicture.h"
16#include "SkStream.h"
17#include "SkTArray.h"
18#include "PdfRenderer.h"
19#include "picture_utils.h"
20
scroggo@google.com7def5e12013-05-31 14:00:10 +000021__SK_FORCE_IMAGE_DECODER_LINKING;
22
edisonn@google.com184487c2013-03-08 18:00:16 +000023#ifdef SK_USE_CDB
24#include "win_dbghelp.h"
25#endif
26
edisonn@google.comc319abe2012-11-01 19:52:38 +000027/**
28 * render_pdfs
29 *
30 * 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 * directory.
33 *
34 * Returns zero exit code if all .skp files were converted successfully,
35 * otherwise returns error code 1.
36 */
37
38static const char PDF_FILE_EXTENSION[] = "pdf";
39static const char SKP_FILE_EXTENSION[] = "skp";
40
41static void usage(const char* argv0) {
42 SkDebugf("SKP to PDF rendering tool\n");
43 SkDebugf("\n"
44"Usage: \n"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000045" %s <input>... [-w <outputDir>] [--jpegQuality N] \n"
edisonn@google.comc319abe2012-11-01 19:52:38 +000046, argv0);
47 SkDebugf("\n\n");
48 SkDebugf(
49" input: A list of directories and files to use as input. Files are\n"
50" expected to have the .skp extension.\n\n");
51 SkDebugf(
52" outputDir: directory to write the rendered pdfs.\n\n");
53 SkDebugf("\n");
edisonn@google.comd9dfa182013-04-24 13:01:01 +000054 SkDebugf(
55" jpegQuality N: encodes images in JPEG at quality level N, which can\n"
56" be in range 0-100).\n"
57" N = -1 will disable JPEG compression.\n"
58" Default is N = 100, maximum quality.\n\n");
59 SkDebugf("\n");
edisonn@google.comc319abe2012-11-01 19:52:38 +000060}
61
62/** Replaces the extension of a file.
63 * @param path File name whose extension will be changed.
64 * @param old_extension The old extension.
65 * @param new_extension The new extension.
66 * @returns false if the file did not has the expected extension.
67 * if false is returned, contents of path are undefined.
68 */
69static bool replace_filename_extension(SkString* path,
70 const char old_extension[],
71 const char new_extension[]) {
72 if (path->endsWith(old_extension)) {
73 path->remove(path->size() - strlen(old_extension),
74 strlen(old_extension));
75 if (!path->endsWith(".")) {
76 return false;
77 }
78 path->append(new_extension);
79 return true;
80 }
81 return false;
82}
83
edisonn@google.comd9dfa182013-04-24 13:01:01 +000084int gJpegQuality = 100;
85static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect) {
86 if (gJpegQuality == -1) return false;
87
88 SkIRect bitmapBounds;
89 SkBitmap subset;
90 const SkBitmap* bitmapToUse = &bitmap;
91 bitmap.getBounds(&bitmapBounds);
92 if (rect != bitmapBounds) {
93 SkAssertResult(bitmap.extractSubset(&subset, rect));
94 bitmapToUse = &subset;
95 }
skia.committer@gmail.com83f0d302013-04-25 07:01:04 +000096
edisonn@google.comd9dfa182013-04-24 13:01:01 +000097#if defined(SK_BUILD_FOR_MAC)
98 // Workaround bug #1043 where bitmaps with referenced pixels cause
99 // CGImageDestinationFinalize to crash
100 SkBitmap copy;
101 bitmapToUse->deepCopyTo(&copy, bitmapToUse->config());
102 bitmapToUse = &copy;
103#endif
104
105 return SkImageEncoder::EncodeStream(stream,
106 *bitmapToUse,
107 SkImageEncoder::kJPEG_Type,
108 gJpegQuality);
109}
110
skia.committer@gmail.com760f2d92012-11-02 02:01:24 +0000111/** Builds the output filename. path = dir/name, and it replaces expected
edisonn@google.comc319abe2012-11-01 19:52:38 +0000112 * .skp extension with .pdf extention.
113 * @param path Output filename.
114 * @param name The name of the file.
115 * @returns false if the file did not has the expected extension.
116 * if false is returned, contents of path are undefined.
117 */
118static bool make_output_filepath(SkString* path, const SkString& dir,
119 const SkString& name) {
120 sk_tools::make_filepath(path, dir, name);
121 return replace_filename_extension(path,
122 SKP_FILE_EXTENSION,
123 PDF_FILE_EXTENSION);
124}
125
126/** Write the output of pdf renderer to a file.
127 * @param outputDir Output dir.
128 * @param inputFilename The skp file that was read.
129 * @param renderer The object responsible to write the pdf file.
130 */
131static bool write_output(const SkString& outputDir,
132 const SkString& inputFilename,
133 const sk_tools::PdfRenderer& renderer) {
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000134 if (outputDir.isEmpty()) {
135 SkDynamicMemoryWStream stream;
136 renderer.write(&stream);
137 return true;
138 }
139
edisonn@google.comc319abe2012-11-01 19:52:38 +0000140 SkString outputPath;
141 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
142 return false;
143 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000144
145 SkFILEWStream stream(outputPath.c_str());
146 if (!stream.isValid()) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000147 SkDebugf("Could not write to file %s\n", outputPath.c_str());
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000148 return false;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000149 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000150 renderer.write(&stream);
151
152 return true;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000153}
154
155/** Reads an skp file, renders it to pdf and writes the output to a pdf file
156 * @param inputPath The skp file to be read.
157 * @param outputDir Output dir.
158 * @param renderer The object responsible to render the skp object into pdf.
159 */
160static bool render_pdf(const SkString& inputPath, const SkString& outputDir,
161 sk_tools::PdfRenderer& renderer) {
162 SkString inputFilename;
163 sk_tools::get_basename(&inputFilename, inputPath);
164
165 SkFILEStream inputStream;
166 inputStream.setPath(inputPath.c_str());
167 if (!inputStream.isValid()) {
168 SkDebugf("Could not open file %s\n", inputPath.c_str());
169 return false;
170 }
171
172 bool success = false;
173 SkAutoTUnref<SkPicture>
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000174 picture(SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory)));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000175
176 if (!success) {
177 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
178 return false;
179 }
180
181 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(),
182 inputPath.c_str());
183
184 renderer.init(picture);
185
186 renderer.render();
187
188 success = write_output(outputDir, inputFilename, renderer);
189
190 renderer.end();
191 return success;
192}
193
194/** For each file in the directory or for the file passed in input, call
195 * render_pdf.
196 * @param input A directory or an skp file.
197 * @param outputDir Output dir.
198 * @param renderer The object responsible to render the skp object into pdf.
199 */
200static int process_input(const SkString& input, const SkString& outputDir,
201 sk_tools::PdfRenderer& renderer) {
202 int failures = 0;
203 if (sk_isdir(input.c_str())) {
204 SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION);
205 SkString inputFilename;
206 while (iter.next(&inputFilename)) {
207 SkString inputPath;
208 sk_tools::make_filepath(&inputPath, input, inputFilename);
209 if (!render_pdf(inputPath, outputDir, renderer)) {
210 ++failures;
211 }
212 }
213 } else {
214 SkString inputPath(input);
215 if (!render_pdf(inputPath, outputDir, renderer)) {
216 ++failures;
217 }
218 }
219 return failures;
220}
221
222static void parse_commandline(int argc, char* const argv[],
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000223 SkTArray<SkString>* inputs,
224 SkString* outputDir) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000225 const char* argv0 = argv[0];
226 char* const* stop = argv + argc;
227
228 for (++argv; argv < stop; ++argv) {
229 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
230 usage(argv0);
231 exit(-1);
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000232 } else if (0 == strcmp(*argv, "-w")) {
233 ++argv;
234 if (argv >= stop) {
235 SkDebugf("Missing outputDir for -w\n");
236 usage(argv0);
237 exit(-1);
238 }
239 *outputDir = SkString(*argv);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000240 } else if (0 == strcmp(*argv, "--jpegQuality")) {
241 ++argv;
242 if (argv >= stop) {
243 SkDebugf("Missing argument for --jpegQuality\n");
244 usage(argv0);
245 exit(-1);
246 }
247 gJpegQuality = atoi(*argv);
248 if (gJpegQuality < -1 || gJpegQuality > 100) {
249 SkDebugf("Invalid argument for --jpegQuality\n");
250 usage(argv0);
skia.committer@gmail.com83f0d302013-04-25 07:01:04 +0000251 exit(-1);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000252 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000253 } else {
254 inputs->push_back(SkString(*argv));
255 }
256 }
257
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000258 if (inputs->count() < 1) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000259 usage(argv0);
260 exit(-1);
261 }
262}
263
edisonn@google.com184487c2013-03-08 18:00:16 +0000264int tool_main_core(int argc, char** argv);
265int tool_main_core(int argc, char** argv) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000266 SkAutoGraphics ag;
267 SkTArray<SkString> inputs;
268
269 SkAutoTUnref<sk_tools::PdfRenderer>
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000270 renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_stream)));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000271 SkASSERT(renderer.get());
272
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000273 SkString outputDir;
274 parse_commandline(argc, argv, &inputs, &outputDir);
edisonn@google.comc319abe2012-11-01 19:52:38 +0000275
276 int failures = 0;
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000277 for (int i = 0; i < inputs.count(); i ++) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000278 failures += process_input(inputs[i], outputDir, *renderer);
279 }
280
281 if (failures != 0) {
282 SkDebugf("Failed to render %i PDFs.\n", failures);
283 return 1;
284 }
edisonn@google.com184487c2013-03-08 18:00:16 +0000285
286 return 0;
287}
288
289int tool_main(int argc, char** argv);
290int tool_main(int argc, char** argv) {
291#ifdef SK_USE_CDB
292 setUpDebuggingFromArgs(argv[0]);
293 __try {
294#endif
295 return tool_main_core(argc, argv);
296#ifdef SK_USE_CDB
297 }
298 __except(GenerateDumpAndPrintCallstack(GetExceptionInformation()))
299 {
300 return -1;
301 }
302#endif
humper@google.comf2863292013-01-07 20:08:56 +0000303 return 0;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000304}
305
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000306#if !defined SK_BUILD_FOR_IOS
307int main(int argc, char * const argv[]) {
308 return tool_main(argc, (char**) argv);
309}
310#endif