blob: ac1aef01ad52f61b5dadc997cbd0bf4aa75aad12 [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 */
edisonn@google.com596d2e22013-07-10 17:44:55 +000051/*
edisonn@google.com01cd4d52013-06-10 20:44:45 +000052static bool replace_filename_extension(SkString* path,
53 const char old_extension[],
54 const char new_extension[]) {
55 if (path->endsWith(old_extension)) {
56 path->remove(path->size() - strlen(old_extension),
57 strlen(old_extension));
58 if (!path->endsWith(".")) {
59 return false;
60 }
61 path->append(new_extension);
62 return true;
63 }
64 return false;
65}
edisonn@google.com596d2e22013-07-10 17:44:55 +000066*/
edisonn@google.com01cd4d52013-06-10 20:44:45 +000067/** Builds the output filename. path = dir/name, and it replaces expected
68 * .skp extension with .pdf extention.
69 * @param path Output filename.
70 * @param name The name of the file.
71 * @returns false if the file did not has the expected extension.
72 * if false is returned, contents of path are undefined.
73 */
edisonn@google.com596d2e22013-07-10 17:44:55 +000074
75/*
edisonn@google.com01cd4d52013-06-10 20:44:45 +000076static bool make_output_filepath(SkString* path, const SkString& dir,
77 const SkString& name) {
78 sk_tools::make_filepath(path, dir, name);
79 return replace_filename_extension(path,
80 PDF_FILE_EXTENSION,
81 PNG_FILE_EXTENSION);
82}
edisonn@google.com596d2e22013-07-10 17:44:55 +000083*/
edisonn@google.com01cd4d52013-06-10 20:44:45 +000084/** Write the output of pdf renderer to a file.
85 * @param outputDir Output dir.
86 * @param inputFilename The skp file that was read.
87 * @param renderer The object responsible to write the pdf file.
88 */
edisonn@google.com596d2e22013-07-10 17:44:55 +000089/*
edisonn@google.com01cd4d52013-06-10 20:44:45 +000090static bool write_output(const SkString& outputDir,
91 const SkString& inputFilename,
92 const SkPdfViewer& renderer) {
93 if (outputDir.isEmpty()) {
94 SkDynamicMemoryWStream stream;
95 renderer.write(&stream);
96 return true;
97 }
98
99 SkString outputPath;
100 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
101 return false;
102 }
103
104 SkFILEWStream stream(outputPath.c_str());
105 if (!stream.isValid()) {
106 SkDebugf("Could not write to file %s\n", outputPath.c_str());
107 return false;
108 }
109 renderer.write(&stream);
110
111 return true;
112}
edisonn@google.com596d2e22013-07-10 17:44:55 +0000113*/
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000114/** 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 parse_pdf(const SkString& inputPath, const SkString& outputDir,
120 SkPdfViewer& 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
133 success = renderer.load(inputPath, NULL);
134
135
136// success = write_output(outputDir, inputFilename, renderer);
137
138 //renderer.end();
139 return success;
140}
141
142/** For each file in the directory or for the file passed in input, call
143 * parse_pdf.
144 * @param input A directory or an pdf file.
145 * @param outputDir Output dir.
146 * @param renderer The object responsible to render the skp object into pdf.
147 */
148static int process_input(const SkString& input, const SkString& outputDir,
149 SkPdfViewer& renderer) {
150 int failures = 0;
151 if (sk_isdir(input.c_str())) {
152 SkOSFile::Iter iter(input.c_str(), PDF_FILE_EXTENSION);
153 SkString inputFilename;
154 while (iter.next(&inputFilename)) {
155 SkString inputPath;
156 sk_tools::make_filepath(&inputPath, input, inputFilename);
157 if (!parse_pdf(inputPath, outputDir, renderer)) {
158 ++failures;
159 }
160 }
161 } else {
162 SkString inputPath(input);
163 if (!parse_pdf(inputPath, outputDir, renderer)) {
164 ++failures;
165 }
166 }
167 return failures;
168}
169
170static void parse_commandline(int argc, char* const argv[],
171 SkTArray<SkString>* inputs,
172 SkString* outputDir) {
173 const char* argv0 = argv[0];
174 char* const* stop = argv + argc;
175
176 for (++argv; argv < stop; ++argv) {
177 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
178 usage(argv0);
179 exit(-1);
180 } else if (0 == strcmp(*argv, "-w")) {
181 ++argv;
182 if (argv >= stop) {
183 SkDebugf("Missing outputDir for -w\n");
184 usage(argv0);
185 exit(-1);
186 }
187 *outputDir = SkString(*argv);
188 } else {
189 inputs->push_back(SkString(*argv));
190 }
191 }
192
193 if (inputs->count() < 1) {
194 usage(argv0);
195 exit(-1);
196 }
197}
198
199int tool_main(int argc, char** argv);
200int tool_main(int argc, char** argv) {
201 SkAutoGraphics ag;
202 SkTArray<SkString> inputs;
203
204 SkAutoTUnref<SkPdfViewer>
205 renderer(SkNEW(SkPdfViewer));
206 SkASSERT(renderer.get());
207
208 SkString outputDir;
209 parse_commandline(argc, argv, &inputs, &outputDir);
210
211 int failures = 0;
212 for (int i = 0; i < inputs.count(); i ++) {
213 failures += process_input(inputs[i], outputDir, *renderer);
214 }
215
216 reportPdfRenderStats();
217
218 if (failures != 0) {
219 SkDebugf("Failed to render %i PDFs.\n", failures);
220 return 1;
221 }
222
223 return 0;
224}
225
226#if !defined SK_BUILD_FOR_IOS
227int main(int argc, char * const argv[]) {
228 return tool_main(argc, (char**) argv);
229}
230#endif