blob: 6f8fb538eb1dbc3548dc457f8622ee50b5ba6280 [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)");
scroggo@google.comc6e1e9a2013-11-14 17:05:05 +000044#if SK_SUPPORT_GPU
edisonn@google.comac03d912013-07-22 15:36:39 +000045DEFINE_string2(config, c, "8888", "Canvas to render:\n"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000046 "\t8888 - argb\n"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000047 "\tgpu: use the gpu\n"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000048 "\tnul - render in null canvas, any draw will just return.\n"
edisonn@google.comac03d912013-07-22 15:36:39 +000049 );
scroggo@google.comc6e1e9a2013-11-14 17:05:05 +000050#else
51DEFINE_string2(config, c, "8888", "Canvas to render:\n"
52 "\t8888 - argb\n"
53 "\tnul - render in null canvas, any draw will just return.\n"
54 );
55#endif
edisonn@google.com598cf5d2013-10-09 15:13:19 +000056DEFINE_bool2(transparentBackground, t, false, "Make background transparent instead of white.");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000057
edisonn@google.com01cd4d52013-06-10 20:44:45 +000058/**
59 * Given list of directories and files to use as input, expects to find .pdf
60 * files and it will convert them to .png files writing them in the same directory
61 * one file for each page.
62 *
63 * Returns zero exit code if all .pdf files were converted successfully,
64 * otherwise returns error code 1.
65 */
66
67static const char PDF_FILE_EXTENSION[] = "pdf";
68static const char PNG_FILE_EXTENSION[] = "png";
69
edisonn@google.com01cd4d52013-06-10 20:44:45 +000070/** Replaces the extension of a file.
71 * @param path File name whose extension will be changed.
72 * @param old_extension The old extension.
73 * @param new_extension The new extension.
74 * @returns false if the file did not has the expected extension.
75 * if false is returned, contents of path are undefined.
76 */
edisonn@google.com222382b2013-07-10 22:33:10 +000077static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000078 const char old_extension[],
79 const char new_extension[]) {
80 if (path->endsWith(old_extension)) {
81 path->remove(path->size() - strlen(old_extension),
82 strlen(old_extension));
83 if (!path->endsWith(".")) {
84 return false;
85 }
edisonn@google.com222382b2013-07-10 22:33:10 +000086 if (page >= 0) {
87 path->appendf("%i.", page);
88 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000089 path->append(new_extension);
90 return true;
91 }
92 return false;
93}
edisonn@google.com222382b2013-07-10 22:33:10 +000094
edisonn@google.com01cd4d52013-06-10 20:44:45 +000095/** Builds the output filename. path = dir/name, and it replaces expected
96 * .skp extension with .pdf extention.
97 * @param path Output filename.
98 * @param name The name of the file.
99 * @returns false if the file did not has the expected extension.
100 * if false is returned, contents of path are undefined.
101 */
102static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +0000103 const SkString& name,
104 int page) {
scroggo@google.com5c7be952013-11-12 14:43:44 +0000105 *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
edisonn@google.com222382b2013-07-10 22:33:10 +0000106 return add_page_and_replace_filename_extension(path, page,
107 PDF_FILE_EXTENSION,
108 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000109}
edisonn@google.com222382b2013-07-10 22:33:10 +0000110
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000111static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000112 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
113
114 bitmap->allocPixels();
115 bitmap->eraseColor(color);
116}
117
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000118/** Write the output of pdf renderer to a file.
119 * @param outputDir Output dir.
120 * @param inputFilename The skp file that was read.
121 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000122 * @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 +0000123 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000124
edisonn@google.com13102382013-07-11 14:50:12 +0000125extern "C" SkBitmap* gDumpBitmap;
126extern "C" SkCanvas* gDumpCanvas;
127
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000128#if SK_SUPPORT_GPU
129GrContextFactory gContextFactory;
130#endif
131
edisonn@google.com222382b2013-07-10 22:33:10 +0000132static bool render_page(const SkString& outputDir,
edisonn@google.com444e25a2013-07-11 15:20:50 +0000133 const SkString& inputFilename,
134 const SkPdfRenderer& renderer,
135 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000136 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
137
edisonn@google.comac03d912013-07-22 15:36:39 +0000138 // Exercise all pdf codepaths as in normal rendering, but no actual bits are changed.
139 if (!FLAGS_config.isEmpty() && strcmp(FLAGS_config[0], "nul") == 0) {
140 SkBitmap bitmap;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000141 SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.comac03d912013-07-22 15:36:39 +0000142 SkNulCanvas canvas(device);
143 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
144 } else {
145 // 8888
146 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000147
edisonn@google.comac03d912013-07-22 15:36:39 +0000148 SkBitmap bitmap;
edisonn@google.com04068b12013-11-12 21:56:39 +0000149 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(FLAGS_DPI / 72.0));
150 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(FLAGS_DPI / 72.0));
edisonn@google.comac03d912013-07-22 15:36:39 +0000151
152 rect = SkRect::MakeWH(width, height);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000153
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000154 SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE;
155
edisonn@google.com222382b2013-07-10 22:33:10 +0000156#ifdef PDF_DEBUG_3X
edisonn@google.come50d9a12013-10-10 20:58:22 +0000157 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height),
158 background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000159#else
edisonn@google.come50d9a12013-10-10 20:58:22 +0000160 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height),
161 background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000162#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000163 SkAutoTUnref<SkBaseDevice> device;
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000164 if (strcmp(FLAGS_config[0], "8888") == 0) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000165 device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000166 }
167#if SK_SUPPORT_GPU
168 else if (strcmp(FLAGS_config[0], "gpu") == 0) {
169 SkAutoTUnref<GrSurface> target;
170 GrContext* gr = gContextFactory.get(GrContextFactory::kNative_GLContextType);
171 if (gr) {
172 // create a render target to back the device
173 GrTextureDesc desc;
174 desc.fConfig = kSkia8888_GrPixelConfig;
175 desc.fFlags = kRenderTarget_GrTextureFlagBit;
scroggo@google.comc6e1e9a2013-11-14 17:05:05 +0000176 desc.fWidth = SkScalarCeilToInt(width);
177 desc.fHeight = SkScalarCeilToInt(height);
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000178 desc.fSampleCnt = 0;
179 target.reset(gr->createUncachedTexture(desc, NULL, 0));
180 }
181 if (NULL == target.get()) {
182 SkASSERT(0);
183 return false;
184 }
185
186 device.reset(SkGpuDevice::Create(target));
187 }
188#endif
189 else {
190 SkDebugf("unknown --config: %s\n", FLAGS_config[0]);
191 return false;
192 }
edisonn@google.comac03d912013-07-22 15:36:39 +0000193 SkCanvas canvas(device);
edisonn@google.com222382b2013-07-10 22:33:10 +0000194
edisonn@google.comac03d912013-07-22 15:36:39 +0000195 gDumpBitmap = &bitmap;
edisonn@google.com222382b2013-07-10 22:33:10 +0000196
edisonn@google.comac03d912013-07-22 15:36:39 +0000197 gDumpCanvas = &canvas;
198 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
edisonn@google.com222382b2013-07-10 22:33:10 +0000199
edisonn@google.comac03d912013-07-22 15:36:39 +0000200 SkString outputPath;
201 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
202 return false;
203 }
204 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
205
206 if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000207 SkDebugf("Memory usage after page %i rendered: %u\n",
208 page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
edisonn@google.comac03d912013-07-22 15:36:39 +0000209 }
edisonn@google.com13102382013-07-11 14:50:12 +0000210 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000211 return true;
212}
edisonn@google.com222382b2013-07-10 22:33:10 +0000213
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000214/** Reads an skp file, renders it to pdf and writes the output to a pdf file
215 * @param inputPath The skp file to be read.
216 * @param outputDir Output dir.
217 * @param renderer The object responsible to render the skp object into pdf.
218 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000219static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000220 SkPdfRenderer& renderer) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000221 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
222
scroggo@google.com5c7be952013-11-12 14:43:44 +0000223 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000224
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000225 bool success = true;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000226
edisonn@google.com222382b2013-07-10 22:33:10 +0000227 success = renderer.load(inputPath);
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000228 if (FLAGS_showMemoryUsage) {
229 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
230 }
231
232 // TODO(edisonn): bench timers
233 if (FLAGS_benchLoad > 0) {
234 for (int i = 0 ; i < FLAGS_benchLoad; i++) {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000235 success = renderer.load(inputPath) && success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000236 if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000237 SkDebugf("Memory usage after load %i number : %u\n", i,
238 (unsigned int)renderer.bytesUsed());
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000239 }
240 }
241 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000242
edisonn@google.com222382b2013-07-10 22:33:10 +0000243 if (success) {
244 if (!renderer.pages())
245 {
246 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
247 return false;
248 } else {
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000249 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
250 // TODO(edisonn) if (i == 1) start timer
251 if (strcmp(FLAGS_pages[0], "all") == 0) {
252 for (int pn = 0; pn < renderer.pages(); ++pn) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000253 success = render_page(
254 outputDir,
255 inputFilename,
256 renderer,
257 FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 :
258 pn) &&
259 success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000260 }
261 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
262 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000263 success = render_page(
264 outputDir,
265 inputFilename,
266 renderer,
267 FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 :
268 pn) &&
269 success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000270 }
271 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000272 success = render_page(
273 outputDir,
274 inputFilename,
275 renderer,
276 FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) &&
277 success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000278 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000279 success = render_page(
280 outputDir,
281 inputFilename,
282 renderer,
283 FLAGS_noExtensionForOnePagePdf &&
284 renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000285 } else {
286 int pn = atoi(FLAGS_pages[0]);
edisonn@google.come50d9a12013-10-10 20:58:22 +0000287 success = render_page(outputDir, inputFilename, renderer,
288 FLAGS_noExtensionForOnePagePdf &&
289 renderer.pages() == 1 ? -1 : pn) && success;
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000290 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000291 }
292 }
293 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000294
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000295 if (!success) {
296 SkDebugf("Failures for file %s\n", inputPath.c_str());
297 }
298
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000299 return success;
300}
301
302/** For each file in the directory or for the file passed in input, call
303 * parse_pdf.
304 * @param input A directory or an pdf file.
305 * @param outputDir Output dir.
306 * @param renderer The object responsible to render the skp object into pdf.
307 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000308static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000309 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000310 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000311 if (sk_isdir(input)) {
312 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000313 SkString inputFilename;
314 while (iter.next(&inputFilename)) {
scroggo@google.com5c7be952013-11-12 14:43:44 +0000315 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000316 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000317 ++failures;
318 }
319 }
320 } else {
321 SkString inputPath(input);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000322 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000323 ++failures;
324 }
325 }
326 return failures;
327}
328
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000329int tool_main(int argc, char** argv);
330int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000331 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
332 SkCommandLineFlags::Parse(argc, argv);
333
334 if (FLAGS_readPath.isEmpty()) {
335 SkDebugf(".pdf files or directories are required.\n");
336 exit(-1);
337 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000338
edisonn@google.com222382b2013-07-10 22:33:10 +0000339 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000340
341 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000342 if (FLAGS_writePath.count() == 1) {
343 outputDir.set(FLAGS_writePath[0]);
344 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000345
346 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000347 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000348 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000349 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000350 }
351
352 reportPdfRenderStats();
353
354 if (failures != 0) {
355 SkDebugf("Failed to render %i PDFs.\n", failures);
356 return 1;
357 }
358
359 return 0;
360}
361
362#if !defined SK_BUILD_FOR_IOS
363int main(int argc, char * const argv[]) {
364 return tool_main(argc, (char**) argv);
365}
366#endif