blob: 231f52e9c76fdb6d65adf5ff8c9a23903e55a55c [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"
11#include "SkOSFile.h"
12#include "SkPicture.h"
13#include "SkStream.h"
14#include "SkTArray.h"
15#include "PdfRenderer.h"
16#include "picture_utils.h"
17
18/**
19 * render_pdfs
20 *
21 * Given list of directories and files to use as input, expects to find .skp
22 * files and it will convert them to .pdf files writing them in the output
23 * directory.
24 *
25 * Returns zero exit code if all .skp files were converted successfully,
26 * otherwise returns error code 1.
27 */
28
29static const char PDF_FILE_EXTENSION[] = "pdf";
30static const char SKP_FILE_EXTENSION[] = "skp";
31
32static void usage(const char* argv0) {
33 SkDebugf("SKP to PDF rendering tool\n");
34 SkDebugf("\n"
35"Usage: \n"
36" %s <input>... <outputDir> \n"
37, argv0);
38 SkDebugf("\n\n");
39 SkDebugf(
40" input: A list of directories and files to use as input. Files are\n"
41" expected to have the .skp extension.\n\n");
42 SkDebugf(
43" outputDir: directory to write the rendered pdfs.\n\n");
44 SkDebugf("\n");
45}
46
47/** Replaces the extension of a file.
48 * @param path File name whose extension will be changed.
49 * @param old_extension The old extension.
50 * @param new_extension The new extension.
51 * @returns false if the file did not has the expected extension.
52 * if false is returned, contents of path are undefined.
53 */
54static bool replace_filename_extension(SkString* path,
55 const char old_extension[],
56 const char new_extension[]) {
57 if (path->endsWith(old_extension)) {
58 path->remove(path->size() - strlen(old_extension),
59 strlen(old_extension));
60 if (!path->endsWith(".")) {
61 return false;
62 }
63 path->append(new_extension);
64 return true;
65 }
66 return false;
67}
68
skia.committer@gmail.com760f2d92012-11-02 02:01:24 +000069/** Builds the output filename. path = dir/name, and it replaces expected
edisonn@google.comc319abe2012-11-01 19:52:38 +000070 * .skp extension with .pdf extention.
71 * @param path Output filename.
72 * @param name The name of the file.
73 * @returns false if the file did not has the expected extension.
74 * if false is returned, contents of path are undefined.
75 */
76static 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 SKP_FILE_EXTENSION,
81 PDF_FILE_EXTENSION);
82}
83
84/** 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 */
89static bool write_output(const SkString& outputDir,
90 const SkString& inputFilename,
91 const sk_tools::PdfRenderer& renderer) {
92 SkString outputPath;
93 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
94 return false;
95 }
96 bool isWritten = renderer.write(outputPath);
97 if (!isWritten) {
98 SkDebugf("Could not write to file %s\n", outputPath.c_str());
99 }
100 return isWritten;
101}
102
103/** Reads an skp file, renders it to pdf and writes the output to a pdf file
104 * @param inputPath The skp file to be read.
105 * @param outputDir Output dir.
106 * @param renderer The object responsible to render the skp object into pdf.
107 */
108static bool render_pdf(const SkString& inputPath, const SkString& outputDir,
109 sk_tools::PdfRenderer& renderer) {
110 SkString inputFilename;
111 sk_tools::get_basename(&inputFilename, inputPath);
112
113 SkFILEStream inputStream;
114 inputStream.setPath(inputPath.c_str());
115 if (!inputStream.isValid()) {
116 SkDebugf("Could not open file %s\n", inputPath.c_str());
117 return false;
118 }
119
120 bool success = false;
121 SkAutoTUnref<SkPicture>
122 picture(SkNEW_ARGS(SkPicture, (&inputStream, &success)));
123
124 if (!success) {
125 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
126 return false;
127 }
128
129 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(),
130 inputPath.c_str());
131
132 renderer.init(picture);
133
134 renderer.render();
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 * render_pdf.
144 * @param input A directory or an skp 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 sk_tools::PdfRenderer& renderer) {
150 int failures = 0;
151 if (sk_isdir(input.c_str())) {
152 SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION);
153 SkString inputFilename;
154 while (iter.next(&inputFilename)) {
155 SkString inputPath;
156 sk_tools::make_filepath(&inputPath, input, inputFilename);
157 if (!render_pdf(inputPath, outputDir, renderer)) {
158 ++failures;
159 }
160 }
161 } else {
162 SkString inputPath(input);
163 if (!render_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 const char* argv0 = argv[0];
173 char* const* stop = argv + argc;
174
175 for (++argv; argv < stop; ++argv) {
176 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
177 usage(argv0);
178 exit(-1);
179 } else {
180 inputs->push_back(SkString(*argv));
181 }
182 }
183
184 if (inputs->count() < 2) {
185 usage(argv0);
186 exit(-1);
187 }
188}
189
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000190int tool_main(int argc, char** argv);
191int tool_main(int argc, char** argv) {
192
edisonn@google.comc319abe2012-11-01 19:52:38 +0000193 SkAutoGraphics ag;
194 SkTArray<SkString> inputs;
195
196 SkAutoTUnref<sk_tools::PdfRenderer>
197 renderer(SkNEW(sk_tools::SimplePdfRenderer));
198 SkASSERT(renderer.get());
199
200 parse_commandline(argc, argv, &inputs);
201 SkString outputDir = inputs[inputs.count() - 1];
202
203 int failures = 0;
204 for (int i = 0; i < inputs.count() - 1; i ++) {
205 failures += process_input(inputs[i], outputDir, *renderer);
206 }
207
208 if (failures != 0) {
209 SkDebugf("Failed to render %i PDFs.\n", failures);
210 return 1;
211 }
212}
213
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000214#if !defined SK_BUILD_FOR_IOS
215int main(int argc, char * const argv[]) {
216 return tool_main(argc, (char**) argv);
217}
218#endif
219