blob: 5e9c2e1ba9a06dffef84ead9c0e2343747fbf456 [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"
edisonn@google.come50d9a12013-10-10 20:58:22 +000015#include "SkPdfRenderer.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000016#include "SkPicture.h"
17#include "SkStream.h"
18#include "SkTypeface.h"
19#include "SkTArray.h"
edisonn@google.comac03d912013-07-22 15:36:39 +000020#include "SkNulCanvas.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000021
edisonn@google.com768bc6a2013-08-08 12:42:13 +000022#if SK_SUPPORT_GPU
23#include "GrContextFactory.h"
24#include "GrContext.h"
25#include "SkGpuDevice.h"
26#endif
27
edisonn@google.coma5aaa792013-07-11 12:27:21 +000028DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process.");
29DEFINE_string2(writePath, w, "", "Directory to write the rendered pages.");
30DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page.");
31DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage.");
edisonn@google.com7b328fd2013-07-11 12:53:06 +000032DEFINE_string2(pages, p, "all", "What pages to render and how:\n"
33 "\tall - all pages\n"
34 "\treverse - all pages, in reverse order\n"
35 "\tfirst - first page\n"
36 "\tlast - last page\n"
37 "\tnumber - a specific page number\n"
38 );
edisonn@google.com15b11182013-07-11 14:43:15 +000039DEFINE_double(DPI, 72, "DPI to be used for rendering (scale).");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000040DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any rendering and \n"
41 "\tminimal parsing to ensure correctness. Default 0 (disabled).");
42DEFINE_int32(benchRender, 0, "Render the pdf content N times. Default 0 (disabled)");
edisonn@google.comac03d912013-07-22 15:36:39 +000043DEFINE_string2(config, c, "8888", "Canvas to render:\n"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000044 "\t8888 - argb\n"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000045#if SK_SUPPORT_GPU
46 "\tgpu: use the gpu\n"
47#endif
48 "\tnul - render in null canvas, any draw will just return.\n"
edisonn@google.comac03d912013-07-22 15:36:39 +000049 );
edisonn@google.com598cf5d2013-10-09 15:13:19 +000050DEFINE_bool2(transparentBackground, t, false, "Make background transparent instead of white.");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000051
edisonn@google.com01cd4d52013-06-10 20:44:45 +000052/**
53 * Given list of directories and files to use as input, expects to find .pdf
54 * files and it will convert them to .png files writing them in the same directory
55 * one file for each page.
56 *
57 * Returns zero exit code if all .pdf files were converted successfully,
58 * otherwise returns error code 1.
59 */
60
61static const char PDF_FILE_EXTENSION[] = "pdf";
62static const char PNG_FILE_EXTENSION[] = "png";
63
edisonn@google.com01cd4d52013-06-10 20:44:45 +000064/** Replaces the extension of a file.
65 * @param path File name whose extension will be changed.
66 * @param old_extension The old extension.
67 * @param new_extension The new extension.
68 * @returns false if the file did not has the expected extension.
69 * if false is returned, contents of path are undefined.
70 */
edisonn@google.com222382b2013-07-10 22:33:10 +000071static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000072 const char old_extension[],
73 const char new_extension[]) {
74 if (path->endsWith(old_extension)) {
75 path->remove(path->size() - strlen(old_extension),
76 strlen(old_extension));
77 if (!path->endsWith(".")) {
78 return false;
79 }
edisonn@google.com222382b2013-07-10 22:33:10 +000080 if (page >= 0) {
81 path->appendf("%i.", page);
82 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000083 path->append(new_extension);
84 return true;
85 }
86 return false;
87}
edisonn@google.com222382b2013-07-10 22:33:10 +000088
edisonn@google.com01cd4d52013-06-10 20:44:45 +000089/** Builds the output filename. path = dir/name, and it replaces expected
90 * .skp extension with .pdf extention.
91 * @param path Output filename.
92 * @param name The name of the file.
93 * @returns false if the file did not has the expected extension.
94 * if false is returned, contents of path are undefined.
95 */
96static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +000097 const SkString& name,
98 int page) {
scroggo@google.com5c7be952013-11-12 14:43:44 +000099 *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
edisonn@google.com222382b2013-07-10 22:33:10 +0000100 return add_page_and_replace_filename_extension(path, page,
101 PDF_FILE_EXTENSION,
102 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000103}
edisonn@google.com222382b2013-07-10 22:33:10 +0000104
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000105static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000106 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
107
108 bitmap->allocPixels();
109 bitmap->eraseColor(color);
110}
111
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000112/** Write the output of pdf renderer to a file.
113 * @param outputDir Output dir.
114 * @param inputFilename The skp file that was read.
115 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000116 * @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 +0000117 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000118
edisonn@google.com13102382013-07-11 14:50:12 +0000119extern "C" SkBitmap* gDumpBitmap;
120extern "C" SkCanvas* gDumpCanvas;
121
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000122#if SK_SUPPORT_GPU
123GrContextFactory gContextFactory;
124#endif
125
edisonn@google.com222382b2013-07-10 22:33:10 +0000126static bool render_page(const SkString& outputDir,
edisonn@google.com444e25a2013-07-11 15:20:50 +0000127 const SkString& inputFilename,
128 const SkPdfRenderer& renderer,
129 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000130 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
131
edisonn@google.comac03d912013-07-22 15:36:39 +0000132 // Exercise all pdf codepaths as in normal rendering, but no actual bits are changed.
133 if (!FLAGS_config.isEmpty() && strcmp(FLAGS_config[0], "nul") == 0) {
134 SkBitmap bitmap;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000135 SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.comac03d912013-07-22 15:36:39 +0000136 SkNulCanvas canvas(device);
137 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
138 } else {
139 // 8888
140 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000141
edisonn@google.comac03d912013-07-22 15:36:39 +0000142 SkBitmap bitmap;
edisonn@google.com04068b12013-11-12 21:56:39 +0000143 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(FLAGS_DPI / 72.0));
144 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(FLAGS_DPI / 72.0));
edisonn@google.comac03d912013-07-22 15:36:39 +0000145
146 rect = SkRect::MakeWH(width, height);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000147
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000148 SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE;
149
edisonn@google.com222382b2013-07-10 22:33:10 +0000150#ifdef PDF_DEBUG_3X
edisonn@google.come50d9a12013-10-10 20:58:22 +0000151 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height),
152 background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000153#else
edisonn@google.come50d9a12013-10-10 20:58:22 +0000154 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height),
155 background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000156#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000157 SkAutoTUnref<SkBaseDevice> device;
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000158 if (strcmp(FLAGS_config[0], "8888") == 0) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000159 device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000160 }
161#if SK_SUPPORT_GPU
162 else if (strcmp(FLAGS_config[0], "gpu") == 0) {
163 SkAutoTUnref<GrSurface> target;
164 GrContext* gr = gContextFactory.get(GrContextFactory::kNative_GLContextType);
165 if (gr) {
166 // create a render target to back the device
167 GrTextureDesc desc;
168 desc.fConfig = kSkia8888_GrPixelConfig;
169 desc.fFlags = kRenderTarget_GrTextureFlagBit;
170 desc.fWidth = width;
171 desc.fHeight = height;
172 desc.fSampleCnt = 0;
173 target.reset(gr->createUncachedTexture(desc, NULL, 0));
174 }
175 if (NULL == target.get()) {
176 SkASSERT(0);
177 return false;
178 }
179
180 device.reset(SkGpuDevice::Create(target));
181 }
182#endif
183 else {
184 SkDebugf("unknown --config: %s\n", FLAGS_config[0]);
185 return false;
186 }
edisonn@google.comac03d912013-07-22 15:36:39 +0000187 SkCanvas canvas(device);
edisonn@google.com222382b2013-07-10 22:33:10 +0000188
edisonn@google.comac03d912013-07-22 15:36:39 +0000189 gDumpBitmap = &bitmap;
edisonn@google.com222382b2013-07-10 22:33:10 +0000190
edisonn@google.comac03d912013-07-22 15:36:39 +0000191 gDumpCanvas = &canvas;
192 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
edisonn@google.com222382b2013-07-10 22:33:10 +0000193
edisonn@google.comac03d912013-07-22 15:36:39 +0000194 SkString outputPath;
195 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
196 return false;
197 }
198 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
199
200 if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000201 SkDebugf("Memory usage after page %i rendered: %u\n",
202 page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
edisonn@google.comac03d912013-07-22 15:36:39 +0000203 }
edisonn@google.com13102382013-07-11 14:50:12 +0000204 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000205 return true;
206}
edisonn@google.com222382b2013-07-10 22:33:10 +0000207
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000208/** Reads an skp file, renders it to pdf and writes the output to a pdf file
209 * @param inputPath The skp file to be read.
210 * @param outputDir Output dir.
211 * @param renderer The object responsible to render the skp object into pdf.
212 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000213static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000214 SkPdfRenderer& renderer) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000215 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
216
scroggo@google.com5c7be952013-11-12 14:43:44 +0000217 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000218
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000219 bool success = true;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000220
edisonn@google.com222382b2013-07-10 22:33:10 +0000221 success = renderer.load(inputPath);
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000222 if (FLAGS_showMemoryUsage) {
223 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
224 }
225
226 // TODO(edisonn): bench timers
227 if (FLAGS_benchLoad > 0) {
228 for (int i = 0 ; i < FLAGS_benchLoad; i++) {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000229 success = renderer.load(inputPath) && success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000230 if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000231 SkDebugf("Memory usage after load %i number : %u\n", i,
232 (unsigned int)renderer.bytesUsed());
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000233 }
234 }
235 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000236
edisonn@google.com222382b2013-07-10 22:33:10 +0000237 if (success) {
238 if (!renderer.pages())
239 {
240 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
241 return false;
242 } else {
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000243 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
244 // TODO(edisonn) if (i == 1) start timer
245 if (strcmp(FLAGS_pages[0], "all") == 0) {
246 for (int pn = 0; pn < renderer.pages(); ++pn) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000247 success = render_page(
248 outputDir,
249 inputFilename,
250 renderer,
251 FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 :
252 pn) &&
253 success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000254 }
255 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
256 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000257 success = render_page(
258 outputDir,
259 inputFilename,
260 renderer,
261 FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 :
262 pn) &&
263 success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000264 }
265 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000266 success = render_page(
267 outputDir,
268 inputFilename,
269 renderer,
270 FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) &&
271 success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000272 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000273 success = render_page(
274 outputDir,
275 inputFilename,
276 renderer,
277 FLAGS_noExtensionForOnePagePdf &&
278 renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000279 } else {
280 int pn = atoi(FLAGS_pages[0]);
edisonn@google.come50d9a12013-10-10 20:58:22 +0000281 success = render_page(outputDir, inputFilename, renderer,
282 FLAGS_noExtensionForOnePagePdf &&
283 renderer.pages() == 1 ? -1 : pn) && success;
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000284 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000285 }
286 }
287 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000288
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000289 if (!success) {
290 SkDebugf("Failures for file %s\n", inputPath.c_str());
291 }
292
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000293 return success;
294}
295
296/** For each file in the directory or for the file passed in input, call
297 * parse_pdf.
298 * @param input A directory or an pdf file.
299 * @param outputDir Output dir.
300 * @param renderer The object responsible to render the skp object into pdf.
301 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000302static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000303 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000304 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000305 if (sk_isdir(input)) {
306 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000307 SkString inputFilename;
308 while (iter.next(&inputFilename)) {
scroggo@google.com5c7be952013-11-12 14:43:44 +0000309 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000310 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000311 ++failures;
312 }
313 }
314 } else {
315 SkString inputPath(input);
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 return failures;
321}
322
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000323int tool_main(int argc, char** argv);
324int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000325 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
326 SkCommandLineFlags::Parse(argc, argv);
327
328 if (FLAGS_readPath.isEmpty()) {
329 SkDebugf(".pdf files or directories are required.\n");
330 exit(-1);
331 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000332
edisonn@google.com222382b2013-07-10 22:33:10 +0000333 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000334
335 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000336 if (FLAGS_writePath.count() == 1) {
337 outputDir.set(FLAGS_writePath[0]);
338 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000339
340 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000341 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000342 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000343 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000344 }
345
346 reportPdfRenderStats();
347
348 if (failures != 0) {
349 SkDebugf("Failed to render %i PDFs.\n", failures);
350 return 1;
351 }
352
353 return 0;
354}
355
356#if !defined SK_BUILD_FOR_IOS
357int main(int argc, char * const argv[]) {
358 return tool_main(argc, (char**) argv);
359}
360#endif