blob: a679d7c4be18ce1ec7e3305111e0d9ed21ee2ea6 [file] [log] [blame]
edisonn@google.comcf2cfa12013-08-21 16:31:37 +00001/*
2 * Copyright 2013 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
edisonn@google.com01cd4d52013-06-10 20:44:45 +00008#include "SkCanvas.h"
edisonn@google.coma5aaa792013-07-11 12:27:21 +00009#include "SkCommandLineFlags.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000010#include "SkDevice.h"
11#include "SkGraphics.h"
12#include "SkImageDecoder.h"
13#include "SkImageEncoder.h"
14#include "SkOSFile.h"
15#include "SkPicture.h"
16#include "SkStream.h"
17#include "SkTypeface.h"
18#include "SkTArray.h"
edisonn@google.comac03d912013-07-22 15:36:39 +000019#include "SkNulCanvas.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000020
edisonn@google.com768bc6a2013-08-08 12:42:13 +000021#if SK_SUPPORT_GPU
22#include "GrContextFactory.h"
23#include "GrContext.h"
24#include "SkGpuDevice.h"
25#endif
26
edisonn@google.com222382b2013-07-10 22:33:10 +000027#include "SkPdfRenderer.h"
edisonn@google.comb857a0c2013-06-25 20:45:40 +000028
edisonn@google.coma5aaa792013-07-11 12:27:21 +000029DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process.");
30DEFINE_string2(writePath, w, "", "Directory to write the rendered pages.");
31DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page.");
32DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage.");
edisonn@google.com7b328fd2013-07-11 12:53:06 +000033DEFINE_string2(pages, p, "all", "What pages to render and how:\n"
34 "\tall - all pages\n"
35 "\treverse - all pages, in reverse order\n"
36 "\tfirst - first page\n"
37 "\tlast - last page\n"
38 "\tnumber - a specific page number\n"
39 );
edisonn@google.com15b11182013-07-11 14:43:15 +000040DEFINE_double(DPI, 72, "DPI to be used for rendering (scale).");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000041DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any rendering and \n"
42 "\tminimal parsing to ensure correctness. Default 0 (disabled).");
43DEFINE_int32(benchRender, 0, "Render the pdf content N times. Default 0 (disabled)");
edisonn@google.comac03d912013-07-22 15:36:39 +000044DEFINE_string2(config, c, "8888", "Canvas to render:\n"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000045 "\t8888 - argb\n"
46
47#if SK_SUPPORT_GPU
48 "\tgpu: use the gpu\n"
49#endif
50 "\tnul - render in null canvas, any draw will just return.\n"
edisonn@google.comac03d912013-07-22 15:36:39 +000051 );
edisonn@google.com598cf5d2013-10-09 15:13:19 +000052DEFINE_bool2(transparentBackground, t, false, "Make background transparent instead of white.");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000053
54
edisonn@google.com15b11182013-07-11 14:43:15 +000055// TODO(edisonn): add config for device target(gpu, raster, pdf), + ability not to render at all
edisonn@google.com7b328fd2013-07-11 12:53:06 +000056
edisonn@google.com01cd4d52013-06-10 20:44:45 +000057/**
58 * Given list of directories and files to use as input, expects to find .pdf
59 * files and it will convert them to .png files writing them in the same directory
60 * one file for each page.
61 *
62 * Returns zero exit code if all .pdf files were converted successfully,
63 * otherwise returns error code 1.
64 */
65
66static const char PDF_FILE_EXTENSION[] = "pdf";
67static const char PNG_FILE_EXTENSION[] = "png";
68
edisonn@google.com01cd4d52013-06-10 20:44:45 +000069/** Replaces the extension of a file.
70 * @param path File name whose extension will be changed.
71 * @param old_extension The old extension.
72 * @param new_extension The new extension.
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.com222382b2013-07-10 22:33:10 +000076static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000077 const char old_extension[],
78 const char new_extension[]) {
79 if (path->endsWith(old_extension)) {
80 path->remove(path->size() - strlen(old_extension),
81 strlen(old_extension));
82 if (!path->endsWith(".")) {
83 return false;
84 }
edisonn@google.com222382b2013-07-10 22:33:10 +000085 if (page >= 0) {
86 path->appendf("%i.", page);
87 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000088 path->append(new_extension);
89 return true;
90 }
91 return false;
92}
edisonn@google.com222382b2013-07-10 22:33:10 +000093
edisonn@google.com33f11b62013-08-14 21:35:27 +000094static void make_filepath(SkString* path, const SkString& dir, const SkString& name) {
edisonn@google.com147adb12013-07-24 15:56:19 +000095 size_t len = dir.size();
96 path->set(dir);
97 if (0 < len && '/' != dir[len - 1]) {
98 path->append("/");
99 }
100 path->append(name);
101}
102
edisonn@google.com33f11b62013-08-14 21:35:27 +0000103static bool is_path_seperator(const char chr) {
edisonn@google.com147adb12013-07-24 15:56:19 +0000104#if defined(SK_BUILD_FOR_WIN)
105 return chr == '\\' || chr == '/';
106#else
107 return chr == '/';
108#endif
109}
110
edisonn@google.com33f11b62013-08-14 21:35:27 +0000111static void get_basename(SkString* basename, const SkString& path) {
edisonn@google.com147adb12013-07-24 15:56:19 +0000112 if (path.size() == 0) {
113 basename->reset();
114 return;
115 }
116
117 size_t end = path.size() - 1;
118
119 // Paths pointing to directories often have a trailing slash,
120 // we remove it so the name is not empty
121 if (is_path_seperator(path[end])) {
122 if (end == 0) {
123 basename->reset();
124 return;
125 }
126
127 end -= 1;
128 }
129
130 size_t i = end;
131 do {
132 --i;
133 if (is_path_seperator(path[i])) {
134 const char* basenameStart = path.c_str() + i + 1;
135 size_t basenameLength = end - i;
136 basename->set(basenameStart, basenameLength);
137 return;
138 }
139 } while (i > 0);
140
141 basename->set(path.c_str(), end + 1);
142}
143
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000144/** Builds the output filename. path = dir/name, and it replaces expected
145 * .skp extension with .pdf extention.
146 * @param path Output filename.
147 * @param name The name of the file.
148 * @returns false if the file did not has the expected extension.
149 * if false is returned, contents of path are undefined.
150 */
151static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +0000152 const SkString& name,
153 int page) {
edisonn@google.com147adb12013-07-24 15:56:19 +0000154 make_filepath(path, dir, name);
edisonn@google.com222382b2013-07-10 22:33:10 +0000155 return add_page_and_replace_filename_extension(path, page,
156 PDF_FILE_EXTENSION,
157 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000158}
edisonn@google.com222382b2013-07-10 22:33:10 +0000159
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000160static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000161 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
162
163 bitmap->allocPixels();
164 bitmap->eraseColor(color);
165}
166
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000167/** Write the output of pdf renderer to a file.
168 * @param outputDir Output dir.
169 * @param inputFilename The skp file that was read.
170 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000171 * @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 +0000172 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000173
edisonn@google.com13102382013-07-11 14:50:12 +0000174extern "C" SkBitmap* gDumpBitmap;
175extern "C" SkCanvas* gDumpCanvas;
176
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000177#if SK_SUPPORT_GPU
178GrContextFactory gContextFactory;
179#endif
180
edisonn@google.com222382b2013-07-10 22:33:10 +0000181static bool render_page(const SkString& outputDir,
edisonn@google.com444e25a2013-07-11 15:20:50 +0000182 const SkString& inputFilename,
183 const SkPdfRenderer& renderer,
184 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000185 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
186
edisonn@google.comac03d912013-07-22 15:36:39 +0000187 // Exercise all pdf codepaths as in normal rendering, but no actual bits are changed.
188 if (!FLAGS_config.isEmpty() && strcmp(FLAGS_config[0], "nul") == 0) {
189 SkBitmap bitmap;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000190 SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.comac03d912013-07-22 15:36:39 +0000191 SkNulCanvas canvas(device);
192 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
193 } else {
194 // 8888
195 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000196
edisonn@google.comac03d912013-07-22 15:36:39 +0000197 SkBitmap bitmap;
198 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0)));
199 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0)));
200
201 rect = SkRect::MakeWH(width, height);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000202
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000203 SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE;
204
edisonn@google.com222382b2013-07-10 22:33:10 +0000205#ifdef PDF_DEBUG_3X
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000206 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height), background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000207#else
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000208 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height), background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000209#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000210 SkAutoTUnref<SkBaseDevice> device;
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000211 if (strcmp(FLAGS_config[0], "8888") == 0) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000212 device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000213 }
214#if SK_SUPPORT_GPU
215 else if (strcmp(FLAGS_config[0], "gpu") == 0) {
216 SkAutoTUnref<GrSurface> target;
217 GrContext* gr = gContextFactory.get(GrContextFactory::kNative_GLContextType);
218 if (gr) {
219 // create a render target to back the device
220 GrTextureDesc desc;
221 desc.fConfig = kSkia8888_GrPixelConfig;
222 desc.fFlags = kRenderTarget_GrTextureFlagBit;
223 desc.fWidth = width;
224 desc.fHeight = height;
225 desc.fSampleCnt = 0;
226 target.reset(gr->createUncachedTexture(desc, NULL, 0));
227 }
228 if (NULL == target.get()) {
229 SkASSERT(0);
230 return false;
231 }
232
233 device.reset(SkGpuDevice::Create(target));
234 }
235#endif
236 else {
237 SkDebugf("unknown --config: %s\n", FLAGS_config[0]);
238 return false;
239 }
edisonn@google.comac03d912013-07-22 15:36:39 +0000240 SkCanvas canvas(device);
edisonn@google.com222382b2013-07-10 22:33:10 +0000241
edisonn@google.comac03d912013-07-22 15:36:39 +0000242 gDumpBitmap = &bitmap;
edisonn@google.com222382b2013-07-10 22:33:10 +0000243
edisonn@google.comac03d912013-07-22 15:36:39 +0000244 gDumpCanvas = &canvas;
245 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
edisonn@google.com222382b2013-07-10 22:33:10 +0000246
edisonn@google.comac03d912013-07-22 15:36:39 +0000247 SkString outputPath;
248 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
249 return false;
250 }
251 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
252
253 if (FLAGS_showMemoryUsage) {
254 SkDebugf("Memory usage after page %i rendered: %u\n", page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
255 }
edisonn@google.com13102382013-07-11 14:50:12 +0000256 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000257 return true;
258}
edisonn@google.com222382b2013-07-10 22:33:10 +0000259
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000260/** Reads an skp file, renders it to pdf and writes the output to a pdf file
261 * @param inputPath The skp file to be read.
262 * @param outputDir Output dir.
263 * @param renderer The object responsible to render the skp object into pdf.
264 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000265static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000266 SkPdfRenderer& renderer) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000267 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
268
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000269 SkString inputFilename;
edisonn@google.com147adb12013-07-24 15:56:19 +0000270 get_basename(&inputFilename, inputPath);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000271
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000272 bool success = true;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000273
edisonn@google.com222382b2013-07-10 22:33:10 +0000274 success = renderer.load(inputPath);
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000275 if (FLAGS_showMemoryUsage) {
276 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
277 }
278
279 // TODO(edisonn): bench timers
280 if (FLAGS_benchLoad > 0) {
281 for (int i = 0 ; i < FLAGS_benchLoad; i++) {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000282 success = renderer.load(inputPath) && success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000283 if (FLAGS_showMemoryUsage) {
284 SkDebugf("Memory usage after load %i number : %u\n", i, (unsigned int)renderer.bytesUsed());
285 }
286 }
287 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000288
edisonn@google.com222382b2013-07-10 22:33:10 +0000289 if (success) {
290 if (!renderer.pages())
291 {
292 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
293 return false;
294 } else {
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000295 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
296 // TODO(edisonn) if (i == 1) start timer
297 if (strcmp(FLAGS_pages[0], "all") == 0) {
298 for (int pn = 0; pn < renderer.pages(); ++pn) {
299 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
300 }
301 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
302 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
303 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
304 }
305 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
306 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success;
307 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
308 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
309 } else {
310 int pn = atoi(FLAGS_pages[0]);
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000311 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000312 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000313 }
314 }
315 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000316
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000317 if (!success) {
318 SkDebugf("Failures for file %s\n", inputPath.c_str());
319 }
320
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000321 return success;
322}
323
324/** For each file in the directory or for the file passed in input, call
325 * parse_pdf.
326 * @param input A directory or an pdf file.
327 * @param outputDir Output dir.
328 * @param renderer The object responsible to render the skp object into pdf.
329 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000330static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000331 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000332 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000333 if (sk_isdir(input)) {
334 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000335 SkString inputFilename;
336 while (iter.next(&inputFilename)) {
337 SkString inputPath;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000338 SkString _input;
339 _input.append(input);
edisonn@google.com147adb12013-07-24 15:56:19 +0000340 make_filepath(&inputPath, _input, inputFilename);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000341 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000342 ++failures;
343 }
344 }
345 } else {
346 SkString inputPath(input);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000347 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000348 ++failures;
349 }
350 }
351 return failures;
352}
353
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000354int tool_main(int argc, char** argv);
355int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000356 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
357 SkCommandLineFlags::Parse(argc, argv);
358
359 if (FLAGS_readPath.isEmpty()) {
360 SkDebugf(".pdf files or directories are required.\n");
361 exit(-1);
362 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000363
edisonn@google.com222382b2013-07-10 22:33:10 +0000364 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000365
366 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000367 if (FLAGS_writePath.count() == 1) {
368 outputDir.set(FLAGS_writePath[0]);
369 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000370
371 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000372 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000373 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000374 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000375 }
376
377 reportPdfRenderStats();
378
379 if (failures != 0) {
380 SkDebugf("Failed to render %i PDFs.\n", failures);
381 return 1;
382 }
383
384 return 0;
385}
386
387#if !defined SK_BUILD_FOR_IOS
388int main(int argc, char * const argv[]) {
389 return tool_main(argc, (char**) argv);
390}
391#endif