blob: 1ac02d3fc3feeec09fb34dc3798f2d3b4957383a [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"
10#include "SkGraphics.h"
scroggo@google.comf8d7d272013-02-22 21:38:35 +000011#include "SkImageDecoder.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000012#include "SkOSFile.h"
13#include "SkPicture.h"
14#include "SkStream.h"
15#include "SkTArray.h"
16#include "PdfRenderer.h"
17#include "picture_utils.h"
18
edisonn@google.com184487c2013-03-08 18:00:16 +000019#ifdef SK_USE_CDB
20#include "win_dbghelp.h"
21#endif
22
edisonn@google.comc319abe2012-11-01 19:52:38 +000023/**
24 * render_pdfs
25 *
26 * Given list of directories and files to use as input, expects to find .skp
27 * files and it will convert them to .pdf files writing them in the output
28 * directory.
29 *
30 * Returns zero exit code if all .skp files were converted successfully,
31 * otherwise returns error code 1.
32 */
33
34static const char PDF_FILE_EXTENSION[] = "pdf";
35static const char SKP_FILE_EXTENSION[] = "skp";
36
37static void usage(const char* argv0) {
38 SkDebugf("SKP to PDF rendering tool\n");
39 SkDebugf("\n"
40"Usage: \n"
edisonn@google.com4fa566b2013-01-11 20:30:41 +000041" %s <input>... -w <outputDir> \n"
edisonn@google.comc319abe2012-11-01 19:52:38 +000042, argv0);
43 SkDebugf("\n\n");
44 SkDebugf(
45" input: A list of directories and files to use as input. Files are\n"
46" expected to have the .skp extension.\n\n");
47 SkDebugf(
48" outputDir: directory to write the rendered pdfs.\n\n");
49 SkDebugf("\n");
50}
51
52/** Replaces the extension of a file.
53 * @param path File name whose extension will be changed.
54 * @param old_extension The old extension.
55 * @param new_extension The new extension.
56 * @returns false if the file did not has the expected extension.
57 * if false is returned, contents of path are undefined.
58 */
59static bool replace_filename_extension(SkString* path,
60 const char old_extension[],
61 const char new_extension[]) {
62 if (path->endsWith(old_extension)) {
63 path->remove(path->size() - strlen(old_extension),
64 strlen(old_extension));
65 if (!path->endsWith(".")) {
66 return false;
67 }
68 path->append(new_extension);
69 return true;
70 }
71 return false;
72}
73
skia.committer@gmail.com760f2d92012-11-02 02:01:24 +000074/** Builds the output filename. path = dir/name, and it replaces expected
edisonn@google.comc319abe2012-11-01 19:52:38 +000075 * .skp extension with .pdf extention.
76 * @param path Output filename.
77 * @param name The name of the file.
78 * @returns false if the file did not has the expected extension.
79 * if false is returned, contents of path are undefined.
80 */
81static bool make_output_filepath(SkString* path, const SkString& dir,
82 const SkString& name) {
83 sk_tools::make_filepath(path, dir, name);
84 return replace_filename_extension(path,
85 SKP_FILE_EXTENSION,
86 PDF_FILE_EXTENSION);
87}
88
89/** Write the output of pdf renderer to a file.
90 * @param outputDir Output dir.
91 * @param inputFilename The skp file that was read.
92 * @param renderer The object responsible to write the pdf file.
93 */
94static bool write_output(const SkString& outputDir,
95 const SkString& inputFilename,
96 const sk_tools::PdfRenderer& renderer) {
edisonn@google.com4fa566b2013-01-11 20:30:41 +000097 if (outputDir.isEmpty()) {
98 SkDynamicMemoryWStream stream;
99 renderer.write(&stream);
100 return true;
101 }
102
edisonn@google.comc319abe2012-11-01 19:52:38 +0000103 SkString outputPath;
104 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
105 return false;
106 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000107
108 SkFILEWStream stream(outputPath.c_str());
109 if (!stream.isValid()) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000110 SkDebugf("Could not write to file %s\n", outputPath.c_str());
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000111 return false;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000112 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000113 renderer.write(&stream);
114
115 return true;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000116}
117
118/** Reads an skp file, renders it to pdf and writes the output to a pdf file
119 * @param inputPath The skp file to be read.
120 * @param outputDir Output dir.
121 * @param renderer The object responsible to render the skp object into pdf.
122 */
123static bool render_pdf(const SkString& inputPath, const SkString& outputDir,
124 sk_tools::PdfRenderer& renderer) {
125 SkString inputFilename;
126 sk_tools::get_basename(&inputFilename, inputPath);
127
128 SkFILEStream inputStream;
129 inputStream.setPath(inputPath.c_str());
130 if (!inputStream.isValid()) {
131 SkDebugf("Could not open file %s\n", inputPath.c_str());
132 return false;
133 }
134
135 bool success = false;
136 SkAutoTUnref<SkPicture>
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000137 picture(SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory)));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000138
139 if (!success) {
140 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
141 return false;
142 }
143
144 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(),
145 inputPath.c_str());
146
147 renderer.init(picture);
148
149 renderer.render();
150
151 success = write_output(outputDir, inputFilename, renderer);
152
153 renderer.end();
154 return success;
155}
156
157/** For each file in the directory or for the file passed in input, call
158 * render_pdf.
159 * @param input A directory or an skp file.
160 * @param outputDir Output dir.
161 * @param renderer The object responsible to render the skp object into pdf.
162 */
163static int process_input(const SkString& input, const SkString& outputDir,
164 sk_tools::PdfRenderer& renderer) {
165 int failures = 0;
166 if (sk_isdir(input.c_str())) {
167 SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION);
168 SkString inputFilename;
169 while (iter.next(&inputFilename)) {
170 SkString inputPath;
171 sk_tools::make_filepath(&inputPath, input, inputFilename);
172 if (!render_pdf(inputPath, outputDir, renderer)) {
173 ++failures;
174 }
175 }
176 } else {
177 SkString inputPath(input);
178 if (!render_pdf(inputPath, outputDir, renderer)) {
179 ++failures;
180 }
181 }
182 return failures;
183}
184
185static void parse_commandline(int argc, char* const argv[],
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000186 SkTArray<SkString>* inputs,
187 SkString* outputDir) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000188 const char* argv0 = argv[0];
189 char* const* stop = argv + argc;
190
191 for (++argv; argv < stop; ++argv) {
192 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
193 usage(argv0);
194 exit(-1);
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000195 } else if (0 == strcmp(*argv, "-w")) {
196 ++argv;
197 if (argv >= stop) {
198 SkDebugf("Missing outputDir for -w\n");
199 usage(argv0);
200 exit(-1);
201 }
202 *outputDir = SkString(*argv);
edisonn@google.comc319abe2012-11-01 19:52:38 +0000203 } else {
204 inputs->push_back(SkString(*argv));
205 }
206 }
207
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000208 if (inputs->count() < 1) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000209 usage(argv0);
210 exit(-1);
211 }
212}
213
edisonn@google.com184487c2013-03-08 18:00:16 +0000214int tool_main_core(int argc, char** argv);
215int tool_main_core(int argc, char** argv) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000216 SkAutoGraphics ag;
217 SkTArray<SkString> inputs;
218
219 SkAutoTUnref<sk_tools::PdfRenderer>
220 renderer(SkNEW(sk_tools::SimplePdfRenderer));
221 SkASSERT(renderer.get());
222
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000223 SkString outputDir;
224 parse_commandline(argc, argv, &inputs, &outputDir);
edisonn@google.comc319abe2012-11-01 19:52:38 +0000225
226 int failures = 0;
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000227 for (int i = 0; i < inputs.count(); i ++) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000228 failures += process_input(inputs[i], outputDir, *renderer);
229 }
230
231 if (failures != 0) {
232 SkDebugf("Failed to render %i PDFs.\n", failures);
233 return 1;
234 }
edisonn@google.com184487c2013-03-08 18:00:16 +0000235
236 return 0;
237}
238
239int tool_main(int argc, char** argv);
240int tool_main(int argc, char** argv) {
241#ifdef SK_USE_CDB
242 setUpDebuggingFromArgs(argv[0]);
243 __try {
244#endif
245 return tool_main_core(argc, argv);
246#ifdef SK_USE_CDB
247 }
248 __except(GenerateDumpAndPrintCallstack(GetExceptionInformation()))
249 {
250 return -1;
251 }
252#endif
humper@google.comf2863292013-01-07 20:08:56 +0000253 return 0;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000254}
255
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000256#if !defined SK_BUILD_FOR_IOS
257int main(int argc, char * const argv[]) {
258 return tool_main(argc, (char**) argv);
259}
260#endif