blob: 14c29ca9b9ecbff74e7b3ca82ee90e1e3180ae04 [file] [log] [blame]
edisonn@google.com01cd4d52013-06-10 20:44:45 +00001#include "SkCanvas.h"
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002#include "SkCommandLineFlags.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +00003#include "SkDevice.h"
4#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.com222382b2013-07-10 22:33:10 +000014#include "SkPdfRenderer.h"
edisonn@google.comb857a0c2013-06-25 20:45:40 +000015
edisonn@google.coma5aaa792013-07-11 12:27:21 +000016DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process.");
17DEFINE_string2(writePath, w, "", "Directory to write the rendered pages.");
18DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page.");
19DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage.");
edisonn@google.com7b328fd2013-07-11 12:53:06 +000020DEFINE_string2(pages, p, "all", "What pages to render and how:\n"
21 "\tall - all pages\n"
22 "\treverse - all pages, in reverse order\n"
23 "\tfirst - first page\n"
24 "\tlast - last page\n"
25 "\tnumber - a specific page number\n"
26 );
edisonn@google.com15b11182013-07-11 14:43:15 +000027DEFINE_double(DPI, 72, "DPI to be used for rendering (scale).");
28// TODO(edisonn): add config for device target(gpu, raster, pdf), + ability not to render at all
29// TODO(edisonn): add ability to do the op N times, bench (either load N times, render n times or load + render n times)
edisonn@google.com7b328fd2013-07-11 12:53:06 +000030
edisonn@google.coma5aaa792013-07-11 12:27:21 +000031
edisonn@google.com01cd4d52013-06-10 20:44:45 +000032/**
33 * Given list of directories and files to use as input, expects to find .pdf
34 * files and it will convert them to .png files writing them in the same directory
35 * one file for each page.
36 *
37 * Returns zero exit code if all .pdf files were converted successfully,
38 * otherwise returns error code 1.
39 */
40
41static const char PDF_FILE_EXTENSION[] = "pdf";
42static const char PNG_FILE_EXTENSION[] = "png";
43
edisonn@google.com01cd4d52013-06-10 20:44:45 +000044/** 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.com222382b2013-07-10 22:33:10 +000051static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000052 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 }
edisonn@google.com222382b2013-07-10 22:33:10 +000060 if (page >= 0) {
61 path->appendf("%i.", page);
62 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000063 path->append(new_extension);
64 return true;
65 }
66 return false;
67}
edisonn@google.com222382b2013-07-10 22:33:10 +000068
edisonn@google.com01cd4d52013-06-10 20:44:45 +000069/** Builds the output filename. path = dir/name, and it replaces expected
70 * .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 */
edisonn@google.com596d2e22013-07-10 17:44:55 +000076
edisonn@google.com222382b2013-07-10 22:33:10 +000077
edisonn@google.com01cd4d52013-06-10 20:44:45 +000078static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +000079 const SkString& name,
80 int page) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +000081 sk_tools::make_filepath(path, dir, name);
edisonn@google.com222382b2013-07-10 22:33:10 +000082 return add_page_and_replace_filename_extension(path, page,
83 PDF_FILE_EXTENSION,
84 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +000085}
edisonn@google.com222382b2013-07-10 22:33:10 +000086
87static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
88 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
89
90 bitmap->allocPixels();
91 bitmap->eraseColor(color);
92}
93
edisonn@google.com01cd4d52013-06-10 20:44:45 +000094/** Write the output of pdf renderer to a file.
95 * @param outputDir Output dir.
96 * @param inputFilename The skp file that was read.
97 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +000098 * @param page -1 means there is only one page (0), and render in a file without page extension
edisonn@google.com01cd4d52013-06-10 20:44:45 +000099 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000100
101static bool render_page(const SkString& outputDir,
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000102 const SkString& inputFilename,
edisonn@google.com222382b2013-07-10 22:33:10 +0000103 const SkPdfRenderer& renderer,
104 int page) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000105 if (outputDir.isEmpty()) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000106 SkBitmap bitmap;
107 setup_bitmap(&bitmap, 1, 1);
108 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
109 SkCanvas canvas(device);
110 return renderer.renderPage(page < 0 ? 0 : page, &canvas);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000111 }
112
113 SkString outputPath;
edisonn@google.com222382b2013-07-10 22:33:10 +0000114 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000115 return false;
116 }
117
edisonn@google.com222382b2013-07-10 22:33:10 +0000118 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
119
120 SkBitmap bitmap;
121#ifdef PDF_DEBUG_3X
122 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(rect.width()), 3 * (int)SkScalarToDouble(rect.height()));
123#else
124 setup_bitmap(&bitmap, (int)SkScalarToDouble(rect.width()), (int)SkScalarToDouble(rect.height()));
125#endif
126 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
127 SkCanvas canvas(device);
128
129 gDumpBitmap = &bitmap;
130
131 gDumpCanvas = &canvas;
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000132 renderer.renderPage(page < 0 ? 0 : page, &canvas);
edisonn@google.com222382b2013-07-10 22:33:10 +0000133
134 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000135
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000136 if (FLAGS_showMemoryUsage) {
137 SkDebugf("Memory usage after page %i rendered: %u\n", page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
138 }
139
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000140 return true;
141}
edisonn@google.com222382b2013-07-10 22:33:10 +0000142
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000143/** Reads an skp file, renders it to pdf and writes the output to a pdf file
144 * @param inputPath The skp file to be read.
145 * @param outputDir Output dir.
146 * @param renderer The object responsible to render the skp object into pdf.
147 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000148static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000149 SkPdfRenderer& renderer) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000150 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
151
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000152 SkString inputFilename;
153 sk_tools::get_basename(&inputFilename, inputPath);
154
155 SkFILEStream inputStream;
156 inputStream.setPath(inputPath.c_str());
157 if (!inputStream.isValid()) {
158 SkDebugf("Could not open file %s\n", inputPath.c_str());
159 return false;
160 }
161
162 bool success = false;
163
edisonn@google.com222382b2013-07-10 22:33:10 +0000164 success = renderer.load(inputPath);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000165
edisonn@google.com222382b2013-07-10 22:33:10 +0000166 if (success) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000167 if (FLAGS_showMemoryUsage) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000168 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
169 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000170 if (!renderer.pages())
171 {
172 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
173 return false;
174 } else {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000175 if (strcmp(FLAGS_pages[0], "all") == 0) {
176 for (int pn = 0; pn < renderer.pages(); ++pn) {
177 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
178 }
179 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
180 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
181 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
182 }
183 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
184 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success;
185 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
186 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
187 } else {
188 int pn = atoi(FLAGS_pages[0]);
189 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && pn;
edisonn@google.com222382b2013-07-10 22:33:10 +0000190 }
191 }
192 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000193
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000194 return success;
195}
196
197/** For each file in the directory or for the file passed in input, call
198 * parse_pdf.
199 * @param input A directory or an pdf file.
200 * @param outputDir Output dir.
201 * @param renderer The object responsible to render the skp object into pdf.
202 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000203static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000204 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000205 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000206 if (sk_isdir(input)) {
207 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000208 SkString inputFilename;
209 while (iter.next(&inputFilename)) {
210 SkString inputPath;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000211 SkString _input;
212 _input.append(input);
213 sk_tools::make_filepath(&inputPath, _input, inputFilename);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000214 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000215 ++failures;
216 }
217 }
218 } else {
219 SkString inputPath(input);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000220 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000221 ++failures;
222 }
223 }
224 return failures;
225}
226
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000227int tool_main(int argc, char** argv);
228int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000229 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
230 SkCommandLineFlags::Parse(argc, argv);
231
232 if (FLAGS_readPath.isEmpty()) {
233 SkDebugf(".pdf files or directories are required.\n");
234 exit(-1);
235 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000236
edisonn@google.com222382b2013-07-10 22:33:10 +0000237 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000238
239 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000240 if (FLAGS_writePath.count() == 1) {
241 outputDir.set(FLAGS_writePath[0]);
242 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000243
244 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000245 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000246 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000247 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000248 }
249
250 reportPdfRenderStats();
251
252 if (failures != 0) {
253 SkDebugf("Failed to render %i PDFs.\n", failures);
254 return 1;
255 }
256
257 return 0;
258}
259
260#if !defined SK_BUILD_FOR_IOS
261int main(int argc, char * const argv[]) {
262 return tool_main(argc, (char**) argv);
263}
264#endif