blob: 6c2543b87dcf140fd3635e4773f87845f8896de8 [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
19/**
20 * render_pdfs
21 *
22 * Given list of directories and files to use as input, expects to find .skp
23 * files and it will convert them to .pdf files writing them in the output
24 * directory.
25 *
26 * Returns zero exit code if all .skp files were converted successfully,
27 * otherwise returns error code 1.
28 */
29
30static const char PDF_FILE_EXTENSION[] = "pdf";
31static const char SKP_FILE_EXTENSION[] = "skp";
32
33static void usage(const char* argv0) {
34 SkDebugf("SKP to PDF rendering tool\n");
35 SkDebugf("\n"
36"Usage: \n"
edisonn@google.com4fa566b2013-01-11 20:30:41 +000037" %s <input>... -w <outputDir> \n"
edisonn@google.comc319abe2012-11-01 19:52:38 +000038, argv0);
39 SkDebugf("\n\n");
40 SkDebugf(
41" input: A list of directories and files to use as input. Files are\n"
42" expected to have the .skp extension.\n\n");
43 SkDebugf(
44" outputDir: directory to write the rendered pdfs.\n\n");
45 SkDebugf("\n");
46}
47
48/** Replaces the extension of a file.
49 * @param path File name whose extension will be changed.
50 * @param old_extension The old extension.
51 * @param new_extension The new extension.
52 * @returns false if the file did not has the expected extension.
53 * if false is returned, contents of path are undefined.
54 */
55static bool replace_filename_extension(SkString* path,
56 const char old_extension[],
57 const char new_extension[]) {
58 if (path->endsWith(old_extension)) {
59 path->remove(path->size() - strlen(old_extension),
60 strlen(old_extension));
61 if (!path->endsWith(".")) {
62 return false;
63 }
64 path->append(new_extension);
65 return true;
66 }
67 return false;
68}
69
skia.committer@gmail.com760f2d92012-11-02 02:01:24 +000070/** Builds the output filename. path = dir/name, and it replaces expected
edisonn@google.comc319abe2012-11-01 19:52:38 +000071 * .skp extension with .pdf extention.
72 * @param path Output filename.
73 * @param name The name of the file.
74 * @returns false if the file did not has the expected extension.
75 * if false is returned, contents of path are undefined.
76 */
77static bool make_output_filepath(SkString* path, const SkString& dir,
78 const SkString& name) {
79 sk_tools::make_filepath(path, dir, name);
80 return replace_filename_extension(path,
81 SKP_FILE_EXTENSION,
82 PDF_FILE_EXTENSION);
83}
84
85/** Write the output of pdf renderer to a file.
86 * @param outputDir Output dir.
87 * @param inputFilename The skp file that was read.
88 * @param renderer The object responsible to write the pdf file.
89 */
90static bool write_output(const SkString& outputDir,
91 const SkString& inputFilename,
92 const sk_tools::PdfRenderer& renderer) {
edisonn@google.com4fa566b2013-01-11 20:30:41 +000093 if (outputDir.isEmpty()) {
94 SkDynamicMemoryWStream stream;
95 renderer.write(&stream);
96 return true;
97 }
98
edisonn@google.comc319abe2012-11-01 19:52:38 +000099 SkString outputPath;
100 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
101 return false;
102 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000103
104 SkFILEWStream stream(outputPath.c_str());
105 if (!stream.isValid()) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000106 SkDebugf("Could not write to file %s\n", outputPath.c_str());
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000107 return false;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000108 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000109 renderer.write(&stream);
110
111 return true;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000112}
113
114/** Reads an skp file, renders it to pdf and writes the output to a pdf file
115 * @param inputPath The skp file to be read.
116 * @param outputDir Output dir.
117 * @param renderer The object responsible to render the skp object into pdf.
118 */
119static bool render_pdf(const SkString& inputPath, const SkString& outputDir,
120 sk_tools::PdfRenderer& renderer) {
121 SkString inputFilename;
122 sk_tools::get_basename(&inputFilename, inputPath);
123
124 SkFILEStream inputStream;
125 inputStream.setPath(inputPath.c_str());
126 if (!inputStream.isValid()) {
127 SkDebugf("Could not open file %s\n", inputPath.c_str());
128 return false;
129 }
130
131 bool success = false;
132 SkAutoTUnref<SkPicture>
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000133 picture(SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory)));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000134
135 if (!success) {
136 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
137 return false;
138 }
139
140 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(),
141 inputPath.c_str());
142
143 renderer.init(picture);
144
145 renderer.render();
146
147 success = write_output(outputDir, inputFilename, renderer);
148
149 renderer.end();
150 return success;
151}
152
153/** For each file in the directory or for the file passed in input, call
154 * render_pdf.
155 * @param input A directory or an skp file.
156 * @param outputDir Output dir.
157 * @param renderer The object responsible to render the skp object into pdf.
158 */
159static int process_input(const SkString& input, const SkString& outputDir,
160 sk_tools::PdfRenderer& renderer) {
161 int failures = 0;
162 if (sk_isdir(input.c_str())) {
163 SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION);
164 SkString inputFilename;
165 while (iter.next(&inputFilename)) {
166 SkString inputPath;
167 sk_tools::make_filepath(&inputPath, input, inputFilename);
168 if (!render_pdf(inputPath, outputDir, renderer)) {
169 ++failures;
170 }
171 }
172 } else {
173 SkString inputPath(input);
174 if (!render_pdf(inputPath, outputDir, renderer)) {
175 ++failures;
176 }
177 }
178 return failures;
179}
180
181static void parse_commandline(int argc, char* const argv[],
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000182 SkTArray<SkString>* inputs,
183 SkString* outputDir) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000184 const char* argv0 = argv[0];
185 char* const* stop = argv + argc;
186
187 for (++argv; argv < stop; ++argv) {
188 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
189 usage(argv0);
190 exit(-1);
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000191 } else if (0 == strcmp(*argv, "-w")) {
192 ++argv;
193 if (argv >= stop) {
194 SkDebugf("Missing outputDir for -w\n");
195 usage(argv0);
196 exit(-1);
197 }
198 *outputDir = SkString(*argv);
edisonn@google.comc319abe2012-11-01 19:52:38 +0000199 } else {
200 inputs->push_back(SkString(*argv));
201 }
202 }
203
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000204 if (inputs->count() < 1) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000205 usage(argv0);
206 exit(-1);
207 }
208}
209
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000210int tool_main(int argc, char** argv);
211int tool_main(int argc, char** argv) {
212
edisonn@google.comc319abe2012-11-01 19:52:38 +0000213 SkAutoGraphics ag;
214 SkTArray<SkString> inputs;
215
216 SkAutoTUnref<sk_tools::PdfRenderer>
217 renderer(SkNEW(sk_tools::SimplePdfRenderer));
218 SkASSERT(renderer.get());
219
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000220 SkString outputDir;
221 parse_commandline(argc, argv, &inputs, &outputDir);
edisonn@google.comc319abe2012-11-01 19:52:38 +0000222
223 int failures = 0;
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000224 for (int i = 0; i < inputs.count(); i ++) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000225 failures += process_input(inputs[i], outputDir, *renderer);
226 }
227
228 if (failures != 0) {
229 SkDebugf("Failed to render %i PDFs.\n", failures);
230 return 1;
231 }
humper@google.comf2863292013-01-07 20:08:56 +0000232 return 0;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000233}
234
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000235#if !defined SK_BUILD_FOR_IOS
236int main(int argc, char * const argv[]) {
237 return tool_main(argc, (char**) argv);
238}
239#endif