blob: 20a6caefdccecc2a72c45edd362094d1fc2d2475 [file] [log] [blame]
edisonn@google.com01cd4d52013-06-10 20:44:45 +00001#include "SkCanvas.h"
2#include "SkDevice.h"
edisonn@google.comafe5e9e2013-06-19 17:42:17 +00003#include "SkForceLinking.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +00004#include "SkGraphics.h"
5#include "SkImageDecoder.h"
6#include "SkImageEncoder.h"
7#include "SkOSFile.h"
8#include "SkPicture.h"
9#include "SkStream.h"
10#include "SkTypeface.h"
11#include "SkTArray.h"
12#include "picture_utils.h"
13
edisonn@google.com59543d32013-06-18 22:00:40 +000014#include "SkPdfParser.h"
edisonn@google.comb857a0c2013-06-25 20:45:40 +000015
edisonn@google.com01cd4d52013-06-10 20:44:45 +000016/**
17 * Given list of directories and files to use as input, expects to find .pdf
18 * files and it will convert them to .png files writing them in the same directory
19 * one file for each page.
20 *
21 * Returns zero exit code if all .pdf files were converted successfully,
22 * otherwise returns error code 1.
23 */
24
25static const char PDF_FILE_EXTENSION[] = "pdf";
26static const char PNG_FILE_EXTENSION[] = "png";
27
28// TODO(edisonn): add ability to write to a new directory.
29static void usage(const char* argv0) {
30 SkDebugf("PDF to PNG rendering tool\n");
31 SkDebugf("\n"
32"Usage: \n"
33" %s <input>... -w <outputDir> \n"
34, argv0);
35 SkDebugf("\n\n");
36 SkDebugf(
37" input: A list of directories and files to use as input. Files are\n"
38" expected to have the .skp extension.\n\n");
39 SkDebugf(
40" outputDir: directory to write the rendered pdfs.\n\n");
41 SkDebugf("\n");
42}
43
44/** Replaces the extension of a file.
45 * @param path File name whose extension will be changed.
46 * @param old_extension The old extension.
47 * @param new_extension The new extension.
48 * @returns false if the file did not has the expected extension.
49 * if false is returned, contents of path are undefined.
50 */
51static bool replace_filename_extension(SkString* path,
52 const char old_extension[],
53 const char new_extension[]) {
54 if (path->endsWith(old_extension)) {
55 path->remove(path->size() - strlen(old_extension),
56 strlen(old_extension));
57 if (!path->endsWith(".")) {
58 return false;
59 }
60 path->append(new_extension);
61 return true;
62 }
63 return false;
64}
65
66/** Builds the output filename. path = dir/name, and it replaces expected
67 * .skp extension with .pdf extention.
68 * @param path Output filename.
69 * @param name The name of the file.
70 * @returns false if the file did not has the expected extension.
71 * if false is returned, contents of path are undefined.
72 */
73static bool make_output_filepath(SkString* path, const SkString& dir,
74 const SkString& name) {
75 sk_tools::make_filepath(path, dir, name);
76 return replace_filename_extension(path,
77 PDF_FILE_EXTENSION,
78 PNG_FILE_EXTENSION);
79}
80
81/** Write the output of pdf renderer to a file.
82 * @param outputDir Output dir.
83 * @param inputFilename The skp file that was read.
84 * @param renderer The object responsible to write the pdf file.
85 */
86static bool write_output(const SkString& outputDir,
87 const SkString& inputFilename,
88 const SkPdfViewer& renderer) {
89 if (outputDir.isEmpty()) {
90 SkDynamicMemoryWStream stream;
91 renderer.write(&stream);
92 return true;
93 }
94
95 SkString outputPath;
96 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
97 return false;
98 }
99
100 SkFILEWStream stream(outputPath.c_str());
101 if (!stream.isValid()) {
102 SkDebugf("Could not write to file %s\n", outputPath.c_str());
103 return false;
104 }
105 renderer.write(&stream);
106
107 return true;
108}
109
110/** Reads an skp file, renders it to pdf and writes the output to a pdf file
111 * @param inputPath The skp file to be read.
112 * @param outputDir Output dir.
113 * @param renderer The object responsible to render the skp object into pdf.
114 */
115static bool parse_pdf(const SkString& inputPath, const SkString& outputDir,
116 SkPdfViewer& renderer) {
117 SkString inputFilename;
118 sk_tools::get_basename(&inputFilename, inputPath);
119
120 SkFILEStream inputStream;
121 inputStream.setPath(inputPath.c_str());
122 if (!inputStream.isValid()) {
123 SkDebugf("Could not open file %s\n", inputPath.c_str());
124 return false;
125 }
126
127 bool success = false;
128
129 success = renderer.load(inputPath, NULL);
130
131
132// success = write_output(outputDir, inputFilename, renderer);
133
134 //renderer.end();
135 return success;
136}
137
138/** For each file in the directory or for the file passed in input, call
139 * parse_pdf.
140 * @param input A directory or an pdf file.
141 * @param outputDir Output dir.
142 * @param renderer The object responsible to render the skp object into pdf.
143 */
144static int process_input(const SkString& input, const SkString& outputDir,
145 SkPdfViewer& renderer) {
146 int failures = 0;
147 if (sk_isdir(input.c_str())) {
148 SkOSFile::Iter iter(input.c_str(), PDF_FILE_EXTENSION);
149 SkString inputFilename;
150 while (iter.next(&inputFilename)) {
151 SkString inputPath;
152 sk_tools::make_filepath(&inputPath, input, inputFilename);
153 if (!parse_pdf(inputPath, outputDir, renderer)) {
154 ++failures;
155 }
156 }
157 } else {
158 SkString inputPath(input);
159 if (!parse_pdf(inputPath, outputDir, renderer)) {
160 ++failures;
161 }
162 }
163 return failures;
164}
165
166static void parse_commandline(int argc, char* const argv[],
167 SkTArray<SkString>* inputs,
168 SkString* outputDir) {
169 const char* argv0 = argv[0];
170 char* const* stop = argv + argc;
171
172 for (++argv; argv < stop; ++argv) {
173 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
174 usage(argv0);
175 exit(-1);
176 } else if (0 == strcmp(*argv, "-w")) {
177 ++argv;
178 if (argv >= stop) {
179 SkDebugf("Missing outputDir for -w\n");
180 usage(argv0);
181 exit(-1);
182 }
183 *outputDir = SkString(*argv);
184 } else {
185 inputs->push_back(SkString(*argv));
186 }
187 }
188
189 if (inputs->count() < 1) {
190 usage(argv0);
191 exit(-1);
192 }
193}
194
195int tool_main(int argc, char** argv);
196int tool_main(int argc, char** argv) {
197 SkAutoGraphics ag;
198 SkTArray<SkString> inputs;
199
200 SkAutoTUnref<SkPdfViewer>
201 renderer(SkNEW(SkPdfViewer));
202 SkASSERT(renderer.get());
203
204 SkString outputDir;
205 parse_commandline(argc, argv, &inputs, &outputDir);
206
207 int failures = 0;
208 for (int i = 0; i < inputs.count(); i ++) {
209 failures += process_input(inputs[i], outputDir, *renderer);
210 }
211
212 reportPdfRenderStats();
213
214 if (failures != 0) {
215 SkDebugf("Failed to render %i PDFs.\n", failures);
216 return 1;
217 }
218
219 return 0;
220}
221
222#if !defined SK_BUILD_FOR_IOS
223int main(int argc, char * const argv[]) {
224 return tool_main(argc, (char**) argv);
225}
226#endif