blob: a7100645281045ace38a2d6745baaa1985a40473 [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"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000012#include "SkImageEncoder.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000013#include "SkOSFile.h"
14#include "SkPicture.h"
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000015#include "SkPixelRef.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000016#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;
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000085static SkData* encode_to_dct_data(size_t* pixelRefOffset, const SkBitmap& bitmap) {
86 if (gJpegQuality == -1) {
87 return NULL;
88 }
edisonn@google.comd9dfa182013-04-24 13:01:01 +000089
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000090 SkBitmap bm = bitmap;
edisonn@google.comd9dfa182013-04-24 13:01:01 +000091#if defined(SK_BUILD_FOR_MAC)
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000092 // Workaround bug #1043 where bitmaps with referenced pixels cause
93 // CGImageDestinationFinalize to crash
94 SkBitmap copy;
95 bitmap.deepCopyTo(&copy, bitmap.config());
96 bm = copy;
edisonn@google.comd9dfa182013-04-24 13:01:01 +000097#endif
98
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000099 SkPixelRef* pr = bm.pixelRef();
100 if (pr != NULL) {
101 SkData* data = pr->refEncodedData();
102 if (data != NULL) {
103 *pixelRefOffset = bm.pixelRefOffset();
104 return data;
105 }
106 }
107
108 *pixelRefOffset = 0;
109 return SkImageEncoder::EncodeData(bm,
110 SkImageEncoder::kJPEG_Type,
111 gJpegQuality);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000112}
113
skia.committer@gmail.com760f2d92012-11-02 02:01:24 +0000114/** Builds the output filename. path = dir/name, and it replaces expected
edisonn@google.comc319abe2012-11-01 19:52:38 +0000115 * .skp extension with .pdf extention.
116 * @param path Output filename.
117 * @param name The name of the file.
118 * @returns false if the file did not has the expected extension.
119 * if false is returned, contents of path are undefined.
120 */
121static bool make_output_filepath(SkString* path, const SkString& dir,
122 const SkString& name) {
123 sk_tools::make_filepath(path, dir, name);
124 return replace_filename_extension(path,
125 SKP_FILE_EXTENSION,
126 PDF_FILE_EXTENSION);
127}
128
129/** Write the output of pdf renderer to a file.
130 * @param outputDir Output dir.
131 * @param inputFilename The skp file that was read.
132 * @param renderer The object responsible to write the pdf file.
133 */
134static bool write_output(const SkString& outputDir,
135 const SkString& inputFilename,
136 const sk_tools::PdfRenderer& renderer) {
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000137 if (outputDir.isEmpty()) {
138 SkDynamicMemoryWStream stream;
139 renderer.write(&stream);
140 return true;
141 }
142
edisonn@google.comc319abe2012-11-01 19:52:38 +0000143 SkString outputPath;
144 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
145 return false;
146 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000147
148 SkFILEWStream stream(outputPath.c_str());
149 if (!stream.isValid()) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000150 SkDebugf("Could not write to file %s\n", outputPath.c_str());
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000151 return false;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000152 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000153 renderer.write(&stream);
154
155 return true;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000156}
157
158/** Reads an skp file, renders it to pdf and writes the output to a pdf file
159 * @param inputPath The skp file to be read.
160 * @param outputDir Output dir.
161 * @param renderer The object responsible to render the skp object into pdf.
162 */
163static bool render_pdf(const SkString& inputPath, const SkString& outputDir,
164 sk_tools::PdfRenderer& renderer) {
165 SkString inputFilename;
166 sk_tools::get_basename(&inputFilename, inputPath);
167
168 SkFILEStream inputStream;
169 inputStream.setPath(inputPath.c_str());
170 if (!inputStream.isValid()) {
171 SkDebugf("Could not open file %s\n", inputPath.c_str());
172 return false;
173 }
174
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000175 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000176
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000177 if (NULL == picture.get()) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000178 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
179 return false;
180 }
181
182 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(),
183 inputPath.c_str());
184
185 renderer.init(picture);
186
187 renderer.render();
188
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000189 bool success = write_output(outputDir, inputFilename, renderer);
edisonn@google.comc319abe2012-11-01 19:52:38 +0000190
191 renderer.end();
192 return success;
193}
194
195/** For each file in the directory or for the file passed in input, call
196 * render_pdf.
197 * @param input A directory or an skp file.
198 * @param outputDir Output dir.
199 * @param renderer The object responsible to render the skp object into pdf.
200 */
201static int process_input(const SkString& input, const SkString& outputDir,
202 sk_tools::PdfRenderer& renderer) {
203 int failures = 0;
204 if (sk_isdir(input.c_str())) {
205 SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION);
206 SkString inputFilename;
207 while (iter.next(&inputFilename)) {
208 SkString inputPath;
209 sk_tools::make_filepath(&inputPath, input, inputFilename);
210 if (!render_pdf(inputPath, outputDir, renderer)) {
211 ++failures;
212 }
213 }
214 } else {
215 SkString inputPath(input);
216 if (!render_pdf(inputPath, outputDir, renderer)) {
217 ++failures;
218 }
219 }
220 return failures;
221}
222
223static void parse_commandline(int argc, char* const argv[],
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000224 SkTArray<SkString>* inputs,
225 SkString* outputDir) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000226 const char* argv0 = argv[0];
227 char* const* stop = argv + argc;
228
229 for (++argv; argv < stop; ++argv) {
230 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
231 usage(argv0);
232 exit(-1);
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000233 } else if (0 == strcmp(*argv, "-w")) {
234 ++argv;
235 if (argv >= stop) {
236 SkDebugf("Missing outputDir for -w\n");
237 usage(argv0);
238 exit(-1);
239 }
240 *outputDir = SkString(*argv);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000241 } else if (0 == strcmp(*argv, "--jpegQuality")) {
242 ++argv;
243 if (argv >= stop) {
244 SkDebugf("Missing argument for --jpegQuality\n");
245 usage(argv0);
246 exit(-1);
247 }
248 gJpegQuality = atoi(*argv);
249 if (gJpegQuality < -1 || gJpegQuality > 100) {
250 SkDebugf("Invalid argument for --jpegQuality\n");
251 usage(argv0);
skia.committer@gmail.com83f0d302013-04-25 07:01:04 +0000252 exit(-1);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000253 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000254 } else {
255 inputs->push_back(SkString(*argv));
256 }
257 }
258
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000259 if (inputs->count() < 1) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000260 usage(argv0);
261 exit(-1);
262 }
263}
264
edisonn@google.com184487c2013-03-08 18:00:16 +0000265int tool_main_core(int argc, char** argv);
266int tool_main_core(int argc, char** argv) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000267 SkAutoGraphics ag;
268 SkTArray<SkString> inputs;
269
270 SkAutoTUnref<sk_tools::PdfRenderer>
commit-bot@chromium.org608ea652013-10-03 19:29:21 +0000271 renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data)));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000272 SkASSERT(renderer.get());
273
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000274 SkString outputDir;
275 parse_commandline(argc, argv, &inputs, &outputDir);
edisonn@google.comc319abe2012-11-01 19:52:38 +0000276
277 int failures = 0;
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000278 for (int i = 0; i < inputs.count(); i ++) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000279 failures += process_input(inputs[i], outputDir, *renderer);
280 }
281
282 if (failures != 0) {
283 SkDebugf("Failed to render %i PDFs.\n", failures);
284 return 1;
285 }
edisonn@google.com184487c2013-03-08 18:00:16 +0000286
287 return 0;
288}
289
290int tool_main(int argc, char** argv);
291int tool_main(int argc, char** argv) {
292#ifdef SK_USE_CDB
293 setUpDebuggingFromArgs(argv[0]);
294 __try {
295#endif
296 return tool_main_core(argc, argv);
297#ifdef SK_USE_CDB
298 }
299 __except(GenerateDumpAndPrintCallstack(GetExceptionInformation()))
300 {
301 return -1;
302 }
303#endif
humper@google.comf2863292013-01-07 20:08:56 +0000304 return 0;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000305}
306
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000307#if !defined SK_BUILD_FOR_IOS
308int main(int argc, char * const argv[]) {
309 return tool_main(argc, (char**) argv);
310}
311#endif