blob: 6395585c4d0a96a084ba86a1267923851546879a [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"
scroggo@google.com5092adc2013-12-02 20:22:31 +000016#include "SkPdfConfig.h"
edisonn@google.come50d9a12013-10-10 20:58:22 +000017#include "SkPdfRenderer.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000018#include "SkPicture.h"
19#include "SkStream.h"
20#include "SkTypeface.h"
21#include "SkTArray.h"
edisonn@google.comac03d912013-07-22 15:36:39 +000022#include "SkNulCanvas.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000023
edisonn@google.com768bc6a2013-08-08 12:42:13 +000024#if SK_SUPPORT_GPU
25#include "GrContextFactory.h"
26#include "GrContext.h"
27#include "SkGpuDevice.h"
28#endif
29
edisonn@google.coma5aaa792013-07-11 12:27:21 +000030DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process.");
31DEFINE_string2(writePath, w, "", "Directory to write the rendered pages.");
32DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page.");
33DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage.");
edisonn@google.com7b328fd2013-07-11 12:53:06 +000034DEFINE_string2(pages, p, "all", "What pages to render and how:\n"
35 "\tall - all pages\n"
36 "\treverse - all pages, in reverse order\n"
37 "\tfirst - first page\n"
38 "\tlast - last page\n"
39 "\tnumber - a specific page number\n"
40 );
edisonn@google.com15b11182013-07-11 14:43:15 +000041DEFINE_double(DPI, 72, "DPI to be used for rendering (scale).");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000042DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any rendering and \n"
43 "\tminimal parsing to ensure correctness. Default 0 (disabled).");
44DEFINE_int32(benchRender, 0, "Render the pdf content N times. Default 0 (disabled)");
scroggo@google.comc6e1e9a2013-11-14 17:05:05 +000045#if SK_SUPPORT_GPU
edisonn@google.comac03d912013-07-22 15:36:39 +000046DEFINE_string2(config, c, "8888", "Canvas to render:\n"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000047 "\t8888 - argb\n"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000048 "\tgpu: use the gpu\n"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000049 "\tnul - render in null canvas, any draw will just return.\n"
edisonn@google.comac03d912013-07-22 15:36:39 +000050 );
scroggo@google.comc6e1e9a2013-11-14 17:05:05 +000051#else
52DEFINE_string2(config, c, "8888", "Canvas to render:\n"
53 "\t8888 - argb\n"
54 "\tnul - render in null canvas, any draw will just return.\n"
55 );
56#endif
edisonn@google.com598cf5d2013-10-09 15:13:19 +000057DEFINE_bool2(transparentBackground, t, false, "Make background transparent instead of white.");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000058
edisonn@google.com01cd4d52013-06-10 20:44:45 +000059/**
60 * Given list of directories and files to use as input, expects to find .pdf
61 * files and it will convert them to .png files writing them in the same directory
62 * one file for each page.
63 *
64 * Returns zero exit code if all .pdf files were converted successfully,
65 * otherwise returns error code 1.
66 */
67
68static const char PDF_FILE_EXTENSION[] = "pdf";
69static const char PNG_FILE_EXTENSION[] = "png";
70
edisonn@google.com01cd4d52013-06-10 20:44:45 +000071/** Replaces the extension of a file.
72 * @param path File name whose extension will be changed.
73 * @param old_extension The old extension.
74 * @param new_extension The new extension.
75 * @returns false if the file did not has the expected extension.
76 * if false is returned, contents of path are undefined.
77 */
edisonn@google.com222382b2013-07-10 22:33:10 +000078static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000079 const char old_extension[],
80 const char new_extension[]) {
81 if (path->endsWith(old_extension)) {
82 path->remove(path->size() - strlen(old_extension),
83 strlen(old_extension));
84 if (!path->endsWith(".")) {
85 return false;
86 }
edisonn@google.com222382b2013-07-10 22:33:10 +000087 if (page >= 0) {
88 path->appendf("%i.", page);
89 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000090 path->append(new_extension);
91 return true;
92 }
93 return false;
94}
edisonn@google.com222382b2013-07-10 22:33:10 +000095
edisonn@google.com01cd4d52013-06-10 20:44:45 +000096/** Builds the output filename. path = dir/name, and it replaces expected
97 * .skp extension with .pdf extention.
98 * @param path Output filename.
99 * @param name The name of the file.
100 * @returns false if the file did not has the expected extension.
101 * if false is returned, contents of path are undefined.
102 */
103static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +0000104 const SkString& name,
105 int page) {
scroggo@google.com5c7be952013-11-12 14:43:44 +0000106 *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
edisonn@google.com222382b2013-07-10 22:33:10 +0000107 return add_page_and_replace_filename_extension(path, page,
108 PDF_FILE_EXTENSION,
109 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000110}
edisonn@google.com222382b2013-07-10 22:33:10 +0000111
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000112static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000113 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
114
115 bitmap->allocPixels();
116 bitmap->eraseColor(color);
117}
118
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000119/** Write the output of pdf renderer to a file.
120 * @param outputDir Output dir.
121 * @param inputFilename The skp file that was read.
122 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000123 * @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 +0000124 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000125
scroggo@google.com5092adc2013-12-02 20:22:31 +0000126#ifdef PDF_TRACE_DIFF_IN_PNG
edisonn@google.com13102382013-07-11 14:50:12 +0000127extern "C" SkBitmap* gDumpBitmap;
128extern "C" SkCanvas* gDumpCanvas;
scroggo@google.com5092adc2013-12-02 20:22:31 +0000129#endif
edisonn@google.com13102382013-07-11 14:50:12 +0000130
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000131#if SK_SUPPORT_GPU
132GrContextFactory gContextFactory;
133#endif
134
edisonn@google.com222382b2013-07-10 22:33:10 +0000135static bool render_page(const SkString& outputDir,
edisonn@google.com444e25a2013-07-11 15:20:50 +0000136 const SkString& inputFilename,
137 const SkPdfRenderer& renderer,
138 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000139 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
140
edisonn@google.comac03d912013-07-22 15:36:39 +0000141 // Exercise all pdf codepaths as in normal rendering, but no actual bits are changed.
142 if (!FLAGS_config.isEmpty() && strcmp(FLAGS_config[0], "nul") == 0) {
143 SkBitmap bitmap;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000144 SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.comac03d912013-07-22 15:36:39 +0000145 SkNulCanvas canvas(device);
146 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
147 } else {
148 // 8888
149 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000150
edisonn@google.comac03d912013-07-22 15:36:39 +0000151 SkBitmap bitmap;
edisonn@google.com04068b12013-11-12 21:56:39 +0000152 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(FLAGS_DPI / 72.0));
153 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(FLAGS_DPI / 72.0));
edisonn@google.comac03d912013-07-22 15:36:39 +0000154
155 rect = SkRect::MakeWH(width, height);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000156
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000157 SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE;
158
edisonn@google.com222382b2013-07-10 22:33:10 +0000159#ifdef PDF_DEBUG_3X
edisonn@google.come50d9a12013-10-10 20:58:22 +0000160 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height),
161 background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000162#else
edisonn@google.come50d9a12013-10-10 20:58:22 +0000163 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height),
164 background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000165#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000166 SkAutoTUnref<SkBaseDevice> device;
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000167 if (strcmp(FLAGS_config[0], "8888") == 0) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000168 device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000169 }
170#if SK_SUPPORT_GPU
171 else if (strcmp(FLAGS_config[0], "gpu") == 0) {
172 SkAutoTUnref<GrSurface> target;
173 GrContext* gr = gContextFactory.get(GrContextFactory::kNative_GLContextType);
174 if (gr) {
175 // create a render target to back the device
176 GrTextureDesc desc;
177 desc.fConfig = kSkia8888_GrPixelConfig;
178 desc.fFlags = kRenderTarget_GrTextureFlagBit;
scroggo@google.comc6e1e9a2013-11-14 17:05:05 +0000179 desc.fWidth = SkScalarCeilToInt(width);
180 desc.fHeight = SkScalarCeilToInt(height);
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000181 desc.fSampleCnt = 0;
182 target.reset(gr->createUncachedTexture(desc, NULL, 0));
183 }
184 if (NULL == target.get()) {
185 SkASSERT(0);
186 return false;
187 }
188
189 device.reset(SkGpuDevice::Create(target));
190 }
191#endif
192 else {
193 SkDebugf("unknown --config: %s\n", FLAGS_config[0]);
194 return false;
195 }
edisonn@google.comac03d912013-07-22 15:36:39 +0000196 SkCanvas canvas(device);
edisonn@google.com222382b2013-07-10 22:33:10 +0000197
scroggo@google.com5092adc2013-12-02 20:22:31 +0000198#ifdef PDF_TRACE_DIFF_IN_PNG
edisonn@google.comac03d912013-07-22 15:36:39 +0000199 gDumpBitmap = &bitmap;
edisonn@google.comac03d912013-07-22 15:36:39 +0000200 gDumpCanvas = &canvas;
scroggo@google.com5092adc2013-12-02 20:22:31 +0000201#endif
edisonn@google.comac03d912013-07-22 15:36:39 +0000202 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
edisonn@google.com222382b2013-07-10 22:33:10 +0000203
edisonn@google.comac03d912013-07-22 15:36:39 +0000204 SkString outputPath;
205 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
206 return false;
207 }
208 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
209
210 if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000211 SkDebugf("Memory usage after page %i rendered: %u\n",
212 page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
edisonn@google.comac03d912013-07-22 15:36:39 +0000213 }
edisonn@google.com13102382013-07-11 14:50:12 +0000214 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000215 return true;
216}
edisonn@google.com222382b2013-07-10 22:33:10 +0000217
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000218/** Reads an skp file, renders it to pdf and writes the output to a pdf file
219 * @param inputPath The skp file to be read.
220 * @param outputDir Output dir.
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000221 */
scroggo@google.com90922892013-11-14 19:09:27 +0000222static bool process_pdf(const SkString& inputPath, const SkString& outputDir) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000223 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
224
scroggo@google.com5c7be952013-11-12 14:43:44 +0000225 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000226
scroggo@google.com90922892013-11-14 19:09:27 +0000227 SkAutoTDelete<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(inputPath.c_str()));
228 if (NULL == renderer.get()) {
229 SkDebugf("Failure loading file %s\n", inputPath.c_str());
230 return false;
231 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000232
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000233 if (FLAGS_showMemoryUsage) {
scroggo@google.com90922892013-11-14 19:09:27 +0000234 SkDebugf("Memory usage after load: %u\n", (unsigned int) renderer->bytesUsed());
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000235 }
236
237 // TODO(edisonn): bench timers
238 if (FLAGS_benchLoad > 0) {
239 for (int i = 0 ; i < FLAGS_benchLoad; i++) {
scroggo@google.com90922892013-11-14 19:09:27 +0000240 SkAutoTDelete<SkPdfRenderer> benchRenderer(
241 SkPdfRenderer::CreateFromFile(inputPath.c_str()));
242 if (NULL == benchRenderer.get()) {
243 SkDebugf("Failed to load on %ith attempt\n", i);
244 } else if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000245 SkDebugf("Memory usage after load %i number : %u\n", i,
scroggo@google.com90922892013-11-14 19:09:27 +0000246 (unsigned int) benchRenderer->bytesUsed());
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000247 }
248 }
249 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000250
scroggo@google.com90922892013-11-14 19:09:27 +0000251 if (!renderer->pages()) {
252 // This should never happen, since CreateFromFile will return NULL if there are no pages.
253 SkASSERT(false);
254 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
255 return false;
256 }
257
258 bool success = true;
259 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
260 // TODO(edisonn) if (i == 1) start timer
261 if (strcmp(FLAGS_pages[0], "all") == 0) {
262 for (int pn = 0; pn < renderer->pages(); ++pn) {
263 success &= render_page(outputDir, inputFilename, *renderer,
264 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : pn);
edisonn@google.com222382b2013-07-10 22:33:10 +0000265 }
scroggo@google.com90922892013-11-14 19:09:27 +0000266 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
267 for (int pn = renderer->pages() - 1; pn >= 0; --pn) {
268 success &= render_page(outputDir, inputFilename, *renderer,
269 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : pn);
270 }
271 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
272 success &= render_page(outputDir, inputFilename, *renderer,
273 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : 0);
274 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
275 success &= render_page(outputDir, inputFilename, *renderer,
276 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1
277 : renderer->pages() - 1);
278 } else {
279 int pn = atoi(FLAGS_pages[0]);
280 success &= render_page(outputDir, inputFilename, *renderer,
281 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : pn);
edisonn@google.com222382b2013-07-10 22:33:10 +0000282 }
283 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000284
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000285 if (!success) {
286 SkDebugf("Failures for file %s\n", inputPath.c_str());
287 }
288
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000289 return success;
290}
291
292/** For each file in the directory or for the file passed in input, call
293 * parse_pdf.
294 * @param input A directory or an pdf file.
295 * @param outputDir Output dir.
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000296 */
scroggo@google.com90922892013-11-14 19:09:27 +0000297static int process_input(const char* input, const SkString& outputDir) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000298 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000299 if (sk_isdir(input)) {
300 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000301 SkString inputFilename;
302 while (iter.next(&inputFilename)) {
scroggo@google.com5c7be952013-11-12 14:43:44 +0000303 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
scroggo@google.com90922892013-11-14 19:09:27 +0000304 if (!process_pdf(inputPath, outputDir)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000305 ++failures;
306 }
307 }
308 } else {
309 SkString inputPath(input);
scroggo@google.com90922892013-11-14 19:09:27 +0000310 if (!process_pdf(inputPath, outputDir)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000311 ++failures;
312 }
313 }
314 return failures;
315}
316
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000317int tool_main(int argc, char** argv);
318int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000319 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
320 SkCommandLineFlags::Parse(argc, argv);
321
322 if (FLAGS_readPath.isEmpty()) {
323 SkDebugf(".pdf files or directories are required.\n");
324 exit(-1);
325 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000326
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000327 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000328 if (FLAGS_writePath.count() == 1) {
329 outputDir.set(FLAGS_writePath[0]);
330 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000331
332 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000333 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
scroggo@google.com90922892013-11-14 19:09:27 +0000334 failures += process_input(FLAGS_readPath[i], outputDir);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000335 }
336
337 reportPdfRenderStats();
338
339 if (failures != 0) {
340 SkDebugf("Failed to render %i PDFs.\n", failures);
341 return 1;
342 }
343
344 return 0;
345}
346
347#if !defined SK_BUILD_FOR_IOS
348int main(int argc, char * const argv[]) {
349 return tool_main(argc, (char**) argv);
350}
351#endif