blob: fcac67e4f6a7fc5061ce3ac384b8796222519ce0 [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
scroggo@google.com22125382013-11-14 16:01:23 +00008#include "SkBitmapDevice.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +00009#include "SkCanvas.h"
edisonn@google.coma5aaa792013-07-11 12:27:21 +000010#include "SkCommandLineFlags.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000011#include "SkDevice.h"
12#include "SkGraphics.h"
13#include "SkImageDecoder.h"
14#include "SkImageEncoder.h"
15#include "SkOSFile.h"
edisonn@google.come50d9a12013-10-10 20:58:22 +000016#include "SkPdfRenderer.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000017#include "SkPicture.h"
18#include "SkStream.h"
19#include "SkTypeface.h"
20#include "SkTArray.h"
edisonn@google.comac03d912013-07-22 15:36:39 +000021#include "SkNulCanvas.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000022
edisonn@google.com768bc6a2013-08-08 12:42:13 +000023#if SK_SUPPORT_GPU
24#include "GrContextFactory.h"
25#include "GrContext.h"
26#include "SkGpuDevice.h"
27#endif
28
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"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000046#if SK_SUPPORT_GPU
47 "\tgpu: use the gpu\n"
48#endif
49 "\tnul - render in null canvas, any draw will just return.\n"
edisonn@google.comac03d912013-07-22 15:36:39 +000050 );
edisonn@google.com598cf5d2013-10-09 15:13:19 +000051DEFINE_bool2(transparentBackground, t, false, "Make background transparent instead of white.");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000052
edisonn@google.com01cd4d52013-06-10 20:44:45 +000053/**
54 * Given list of directories and files to use as input, expects to find .pdf
55 * files and it will convert them to .png files writing them in the same directory
56 * one file for each page.
57 *
58 * Returns zero exit code if all .pdf files were converted successfully,
59 * otherwise returns error code 1.
60 */
61
62static const char PDF_FILE_EXTENSION[] = "pdf";
63static const char PNG_FILE_EXTENSION[] = "png";
64
edisonn@google.com01cd4d52013-06-10 20:44:45 +000065/** Replaces the extension of a file.
66 * @param path File name whose extension will be changed.
67 * @param old_extension The old extension.
68 * @param new_extension The new extension.
69 * @returns false if the file did not has the expected extension.
70 * if false is returned, contents of path are undefined.
71 */
edisonn@google.com222382b2013-07-10 22:33:10 +000072static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000073 const char old_extension[],
74 const char new_extension[]) {
75 if (path->endsWith(old_extension)) {
76 path->remove(path->size() - strlen(old_extension),
77 strlen(old_extension));
78 if (!path->endsWith(".")) {
79 return false;
80 }
edisonn@google.com222382b2013-07-10 22:33:10 +000081 if (page >= 0) {
82 path->appendf("%i.", page);
83 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000084 path->append(new_extension);
85 return true;
86 }
87 return false;
88}
edisonn@google.com222382b2013-07-10 22:33:10 +000089
edisonn@google.com01cd4d52013-06-10 20:44:45 +000090/** Builds the output filename. path = dir/name, and it replaces expected
91 * .skp extension with .pdf extention.
92 * @param path Output filename.
93 * @param name The name of the file.
94 * @returns false if the file did not has the expected extension.
95 * if false is returned, contents of path are undefined.
96 */
97static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +000098 const SkString& name,
99 int page) {
scroggo@google.com5c7be952013-11-12 14:43:44 +0000100 *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
edisonn@google.com222382b2013-07-10 22:33:10 +0000101 return add_page_and_replace_filename_extension(path, page,
102 PDF_FILE_EXTENSION,
103 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000104}
edisonn@google.com222382b2013-07-10 22:33:10 +0000105
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000106static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000107 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
108
109 bitmap->allocPixels();
110 bitmap->eraseColor(color);
111}
112
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000113/** Write the output of pdf renderer to a file.
114 * @param outputDir Output dir.
115 * @param inputFilename The skp file that was read.
116 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000117 * @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 +0000118 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000119
edisonn@google.com13102382013-07-11 14:50:12 +0000120extern "C" SkBitmap* gDumpBitmap;
121extern "C" SkCanvas* gDumpCanvas;
122
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000123#if SK_SUPPORT_GPU
124GrContextFactory gContextFactory;
125#endif
126
edisonn@google.com222382b2013-07-10 22:33:10 +0000127static bool render_page(const SkString& outputDir,
edisonn@google.com444e25a2013-07-11 15:20:50 +0000128 const SkString& inputFilename,
129 const SkPdfRenderer& renderer,
130 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000131 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
132
edisonn@google.comac03d912013-07-22 15:36:39 +0000133 // Exercise all pdf codepaths as in normal rendering, but no actual bits are changed.
134 if (!FLAGS_config.isEmpty() && strcmp(FLAGS_config[0], "nul") == 0) {
135 SkBitmap bitmap;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000136 SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.comac03d912013-07-22 15:36:39 +0000137 SkNulCanvas canvas(device);
138 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
139 } else {
140 // 8888
141 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000142
edisonn@google.comac03d912013-07-22 15:36:39 +0000143 SkBitmap bitmap;
edisonn@google.com04068b12013-11-12 21:56:39 +0000144 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(FLAGS_DPI / 72.0));
145 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(FLAGS_DPI / 72.0));
edisonn@google.comac03d912013-07-22 15:36:39 +0000146
147 rect = SkRect::MakeWH(width, height);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000148
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000149 SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE;
150
edisonn@google.com222382b2013-07-10 22:33:10 +0000151#ifdef PDF_DEBUG_3X
edisonn@google.come50d9a12013-10-10 20:58:22 +0000152 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height),
153 background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000154#else
edisonn@google.come50d9a12013-10-10 20:58:22 +0000155 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height),
156 background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000157#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000158 SkAutoTUnref<SkBaseDevice> device;
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000159 if (strcmp(FLAGS_config[0], "8888") == 0) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000160 device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000161 }
162#if SK_SUPPORT_GPU
163 else if (strcmp(FLAGS_config[0], "gpu") == 0) {
164 SkAutoTUnref<GrSurface> target;
165 GrContext* gr = gContextFactory.get(GrContextFactory::kNative_GLContextType);
166 if (gr) {
167 // create a render target to back the device
168 GrTextureDesc desc;
169 desc.fConfig = kSkia8888_GrPixelConfig;
170 desc.fFlags = kRenderTarget_GrTextureFlagBit;
171 desc.fWidth = width;
172 desc.fHeight = height;
173 desc.fSampleCnt = 0;
174 target.reset(gr->createUncachedTexture(desc, NULL, 0));
175 }
176 if (NULL == target.get()) {
177 SkASSERT(0);
178 return false;
179 }
180
181 device.reset(SkGpuDevice::Create(target));
182 }
183#endif
184 else {
185 SkDebugf("unknown --config: %s\n", FLAGS_config[0]);
186 return false;
187 }
edisonn@google.comac03d912013-07-22 15:36:39 +0000188 SkCanvas canvas(device);
edisonn@google.com222382b2013-07-10 22:33:10 +0000189
edisonn@google.comac03d912013-07-22 15:36:39 +0000190 gDumpBitmap = &bitmap;
edisonn@google.com222382b2013-07-10 22:33:10 +0000191
edisonn@google.comac03d912013-07-22 15:36:39 +0000192 gDumpCanvas = &canvas;
193 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
edisonn@google.com222382b2013-07-10 22:33:10 +0000194
edisonn@google.comac03d912013-07-22 15:36:39 +0000195 SkString outputPath;
196 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
197 return false;
198 }
199 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
200
201 if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000202 SkDebugf("Memory usage after page %i rendered: %u\n",
203 page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
edisonn@google.comac03d912013-07-22 15:36:39 +0000204 }
edisonn@google.com13102382013-07-11 14:50:12 +0000205 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000206 return true;
207}
edisonn@google.com222382b2013-07-10 22:33:10 +0000208
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000209/** Reads an skp file, renders it to pdf and writes the output to a pdf file
210 * @param inputPath The skp file to be read.
211 * @param outputDir Output dir.
212 * @param renderer The object responsible to render the skp object into pdf.
213 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000214static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000215 SkPdfRenderer& renderer) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000216 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
217
scroggo@google.com5c7be952013-11-12 14:43:44 +0000218 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000219
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000220 bool success = true;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000221
edisonn@google.com222382b2013-07-10 22:33:10 +0000222 success = renderer.load(inputPath);
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000223 if (FLAGS_showMemoryUsage) {
224 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
225 }
226
227 // TODO(edisonn): bench timers
228 if (FLAGS_benchLoad > 0) {
229 for (int i = 0 ; i < FLAGS_benchLoad; i++) {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000230 success = renderer.load(inputPath) && success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000231 if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000232 SkDebugf("Memory usage after load %i number : %u\n", i,
233 (unsigned int)renderer.bytesUsed());
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000234 }
235 }
236 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000237
edisonn@google.com222382b2013-07-10 22:33:10 +0000238 if (success) {
239 if (!renderer.pages())
240 {
241 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
242 return false;
243 } else {
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000244 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
245 // TODO(edisonn) if (i == 1) start timer
246 if (strcmp(FLAGS_pages[0], "all") == 0) {
247 for (int pn = 0; pn < renderer.pages(); ++pn) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000248 success = render_page(
249 outputDir,
250 inputFilename,
251 renderer,
252 FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 :
253 pn) &&
254 success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000255 }
256 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
257 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000258 success = render_page(
259 outputDir,
260 inputFilename,
261 renderer,
262 FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 :
263 pn) &&
264 success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000265 }
266 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000267 success = render_page(
268 outputDir,
269 inputFilename,
270 renderer,
271 FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) &&
272 success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000273 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000274 success = render_page(
275 outputDir,
276 inputFilename,
277 renderer,
278 FLAGS_noExtensionForOnePagePdf &&
279 renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000280 } else {
281 int pn = atoi(FLAGS_pages[0]);
edisonn@google.come50d9a12013-10-10 20:58:22 +0000282 success = render_page(outputDir, inputFilename, renderer,
283 FLAGS_noExtensionForOnePagePdf &&
284 renderer.pages() == 1 ? -1 : pn) && success;
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000285 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000286 }
287 }
288 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000289
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000290 if (!success) {
291 SkDebugf("Failures for file %s\n", inputPath.c_str());
292 }
293
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000294 return success;
295}
296
297/** For each file in the directory or for the file passed in input, call
298 * parse_pdf.
299 * @param input A directory or an pdf file.
300 * @param outputDir Output dir.
301 * @param renderer The object responsible to render the skp object into pdf.
302 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000303static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000304 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000305 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000306 if (sk_isdir(input)) {
307 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000308 SkString inputFilename;
309 while (iter.next(&inputFilename)) {
scroggo@google.com5c7be952013-11-12 14:43:44 +0000310 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000311 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000312 ++failures;
313 }
314 }
315 } else {
316 SkString inputPath(input);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000317 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000318 ++failures;
319 }
320 }
321 return failures;
322}
323
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000324int tool_main(int argc, char** argv);
325int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000326 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
327 SkCommandLineFlags::Parse(argc, argv);
328
329 if (FLAGS_readPath.isEmpty()) {
330 SkDebugf(".pdf files or directories are required.\n");
331 exit(-1);
332 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000333
edisonn@google.com222382b2013-07-10 22:33:10 +0000334 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000335
336 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000337 if (FLAGS_writePath.count() == 1) {
338 outputDir.set(FLAGS_writePath[0]);
339 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000340
341 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000342 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000343 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000344 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000345 }
346
347 reportPdfRenderStats();
348
349 if (failures != 0) {
350 SkDebugf("Failed to render %i PDFs.\n", failures);
351 return 1;
352 }
353
354 return 0;
355}
356
357#if !defined SK_BUILD_FOR_IOS
358int main(int argc, char * const argv[]) {
359 return tool_main(argc, (char**) argv);
360}
361#endif