blob: a28e56197f8dbd970b1387c9624a89d5061f1573 [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.com01cd4d52013-06-10 20:44:45 +000031/**
32 * Given list of directories and files to use as input, expects to find .pdf
33 * files and it will convert them to .png files writing them in the same directory
34 * one file for each page.
35 *
36 * Returns zero exit code if all .pdf files were converted successfully,
37 * otherwise returns error code 1.
38 */
39
40static const char PDF_FILE_EXTENSION[] = "pdf";
41static const char PNG_FILE_EXTENSION[] = "png";
42
edisonn@google.com01cd4d52013-06-10 20:44:45 +000043/** Replaces the extension of a file.
44 * @param path File name whose extension will be changed.
45 * @param old_extension The old extension.
46 * @param new_extension The new extension.
47 * @returns false if the file did not has the expected extension.
48 * if false is returned, contents of path are undefined.
49 */
edisonn@google.com222382b2013-07-10 22:33:10 +000050static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000051 const char old_extension[],
52 const char new_extension[]) {
53 if (path->endsWith(old_extension)) {
54 path->remove(path->size() - strlen(old_extension),
55 strlen(old_extension));
56 if (!path->endsWith(".")) {
57 return false;
58 }
edisonn@google.com222382b2013-07-10 22:33:10 +000059 if (page >= 0) {
60 path->appendf("%i.", page);
61 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000062 path->append(new_extension);
63 return true;
64 }
65 return false;
66}
edisonn@google.com222382b2013-07-10 22:33:10 +000067
edisonn@google.com01cd4d52013-06-10 20:44:45 +000068/** Builds the output filename. path = dir/name, and it replaces expected
69 * .skp extension with .pdf extention.
70 * @param path Output filename.
71 * @param name The name of the file.
72 * @returns false if the file did not has the expected extension.
73 * if false is returned, contents of path are undefined.
74 */
edisonn@google.com596d2e22013-07-10 17:44:55 +000075
edisonn@google.com222382b2013-07-10 22:33:10 +000076
edisonn@google.com01cd4d52013-06-10 20:44:45 +000077static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +000078 const SkString& name,
79 int page) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +000080 sk_tools::make_filepath(path, dir, name);
edisonn@google.com222382b2013-07-10 22:33:10 +000081 return add_page_and_replace_filename_extension(path, page,
82 PDF_FILE_EXTENSION,
83 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +000084}
edisonn@google.com222382b2013-07-10 22:33:10 +000085
86static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
87 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
88
89 bitmap->allocPixels();
90 bitmap->eraseColor(color);
91}
92
edisonn@google.com01cd4d52013-06-10 20:44:45 +000093/** Write the output of pdf renderer to a file.
94 * @param outputDir Output dir.
95 * @param inputFilename The skp file that was read.
96 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +000097 * @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 +000098 */
edisonn@google.com222382b2013-07-10 22:33:10 +000099
edisonn@google.com13102382013-07-11 14:50:12 +0000100extern "C" SkBitmap* gDumpBitmap;
101extern "C" SkCanvas* gDumpCanvas;
102
edisonn@google.com222382b2013-07-10 22:33:10 +0000103static bool render_page(const SkString& outputDir,
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000104 const SkString& inputFilename,
edisonn@google.com222382b2013-07-10 22:33:10 +0000105 const SkPdfRenderer& renderer,
106 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000107 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
108
109 SkBitmap bitmap;
110#ifdef PDF_DEBUG_3X
111 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(rect.width()), 3 * (int)SkScalarToDouble(rect.height()));
112#else
113 setup_bitmap(&bitmap, (int)SkScalarToDouble(rect.width()), (int)SkScalarToDouble(rect.height()));
114#endif
115 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
116 SkCanvas canvas(device);
117
118 gDumpBitmap = &bitmap;
119
120 gDumpCanvas = &canvas;
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000121 renderer.renderPage(page < 0 ? 0 : page, &canvas);
edisonn@google.com222382b2013-07-10 22:33:10 +0000122
edisonn@google.com13102382013-07-11 14:50:12 +0000123 SkString outputPath;
124 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
125 return false;
126 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000127 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000128
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000129 if (FLAGS_showMemoryUsage) {
130 SkDebugf("Memory usage after page %i rendered: %u\n", page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
131 }
132
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000133 return true;
134}
edisonn@google.com222382b2013-07-10 22:33:10 +0000135
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000136/** Reads an skp file, renders it to pdf and writes the output to a pdf file
137 * @param inputPath The skp file to be read.
138 * @param outputDir Output dir.
139 * @param renderer The object responsible to render the skp object into pdf.
140 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000141static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000142 SkPdfRenderer& renderer) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000143 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
144
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000145 SkString inputFilename;
146 sk_tools::get_basename(&inputFilename, inputPath);
147
148 SkFILEStream inputStream;
149 inputStream.setPath(inputPath.c_str());
150 if (!inputStream.isValid()) {
151 SkDebugf("Could not open file %s\n", inputPath.c_str());
152 return false;
153 }
154
155 bool success = false;
156
edisonn@google.com222382b2013-07-10 22:33:10 +0000157 success = renderer.load(inputPath);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000158
edisonn@google.com222382b2013-07-10 22:33:10 +0000159 if (success) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000160 if (FLAGS_showMemoryUsage) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000161 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
162 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000163 if (!renderer.pages())
164 {
165 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
166 return false;
167 } else {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000168 if (strcmp(FLAGS_pages[0], "all") == 0) {
169 for (int pn = 0; pn < renderer.pages(); ++pn) {
170 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
171 }
172 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
173 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
174 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
175 }
176 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
177 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success;
178 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
179 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
180 } else {
181 int pn = atoi(FLAGS_pages[0]);
182 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && pn;
edisonn@google.com222382b2013-07-10 22:33:10 +0000183 }
184 }
185 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000186
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000187 return success;
188}
189
190/** For each file in the directory or for the file passed in input, call
191 * parse_pdf.
192 * @param input A directory or an pdf file.
193 * @param outputDir Output dir.
194 * @param renderer The object responsible to render the skp object into pdf.
195 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000196static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000197 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000198 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000199 if (sk_isdir(input)) {
200 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000201 SkString inputFilename;
202 while (iter.next(&inputFilename)) {
203 SkString inputPath;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000204 SkString _input;
205 _input.append(input);
206 sk_tools::make_filepath(&inputPath, _input, inputFilename);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000207 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000208 ++failures;
209 }
210 }
211 } else {
212 SkString inputPath(input);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000213 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000214 ++failures;
215 }
216 }
217 return failures;
218}
219
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000220int tool_main(int argc, char** argv);
221int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000222 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
223 SkCommandLineFlags::Parse(argc, argv);
224
225 if (FLAGS_readPath.isEmpty()) {
226 SkDebugf(".pdf files or directories are required.\n");
227 exit(-1);
228 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000229
edisonn@google.com222382b2013-07-10 22:33:10 +0000230 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000231
232 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000233 if (FLAGS_writePath.count() == 1) {
234 outputDir.set(FLAGS_writePath[0]);
235 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000236
237 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000238 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000239 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000240 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000241 }
242
243 reportPdfRenderStats();
244
245 if (failures != 0) {
246 SkDebugf("Failed to render %i PDFs.\n", failures);
247 return 1;
248 }
249
250 return 0;
251}
252
253#if !defined SK_BUILD_FOR_IOS
254int main(int argc, char * const argv[]) {
255 return tool_main(argc, (char**) argv);
256}
257#endif