blob: 2f1a055e4d63c0c798cd84e08e3c8f3deeaed444 [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) {
reed6c225732014-06-09 19:52:07 -0700113 bitmap->allocN32Pixels(width, height);
edisonn@google.com222382b2013-07-10 22:33:10 +0000114 bitmap->eraseColor(color);
115}
116
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000117/** Write the output of pdf renderer to a file.
118 * @param outputDir Output dir.
119 * @param inputFilename The skp file that was read.
120 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000121 * @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 +0000122 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000123
scroggo@google.com5092adc2013-12-02 20:22:31 +0000124#ifdef PDF_TRACE_DIFF_IN_PNG
edisonn@google.com13102382013-07-11 14:50:12 +0000125extern "C" SkBitmap* gDumpBitmap;
126extern "C" SkCanvas* gDumpCanvas;
scroggo@google.com5092adc2013-12-02 20:22:31 +0000127#endif
edisonn@google.com13102382013-07-11 14:50:12 +0000128
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000129#if SK_SUPPORT_GPU
130GrContextFactory gContextFactory;
131#endif
132
edisonn@google.com222382b2013-07-10 22:33:10 +0000133static bool render_page(const SkString& outputDir,
edisonn@google.com444e25a2013-07-11 15:20:50 +0000134 const SkString& inputFilename,
135 const SkPdfRenderer& renderer,
136 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000137 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
138
edisonn@google.comac03d912013-07-22 15:36:39 +0000139 // Exercise all pdf codepaths as in normal rendering, but no actual bits are changed.
140 if (!FLAGS_config.isEmpty() && strcmp(FLAGS_config[0], "nul") == 0) {
141 SkBitmap bitmap;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000142 SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.comac03d912013-07-22 15:36:39 +0000143 SkNulCanvas canvas(device);
144 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
145 } else {
146 // 8888
147 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000148
edisonn@google.comac03d912013-07-22 15:36:39 +0000149 SkBitmap bitmap;
edisonn@google.com04068b12013-11-12 21:56:39 +0000150 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(FLAGS_DPI / 72.0));
151 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(FLAGS_DPI / 72.0));
edisonn@google.comac03d912013-07-22 15:36:39 +0000152
153 rect = SkRect::MakeWH(width, height);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000154
edisonn@google.com598cf5d2013-10-09 15:13:19 +0000155 SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE;
156
edisonn@google.com222382b2013-07-10 22:33:10 +0000157#ifdef PDF_DEBUG_3X
edisonn@google.come50d9a12013-10-10 20:58:22 +0000158 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height),
159 background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000160#else
edisonn@google.come50d9a12013-10-10 20:58:22 +0000161 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height),
162 background);
edisonn@google.com222382b2013-07-10 22:33:10 +0000163#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000164 SkAutoTUnref<SkBaseDevice> device;
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000165 if (strcmp(FLAGS_config[0], "8888") == 0) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000166 device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000167 }
168#if SK_SUPPORT_GPU
169 else if (strcmp(FLAGS_config[0], "gpu") == 0) {
170 SkAutoTUnref<GrSurface> target;
171 GrContext* gr = gContextFactory.get(GrContextFactory::kNative_GLContextType);
172 if (gr) {
173 // create a render target to back the device
174 GrTextureDesc desc;
175 desc.fConfig = kSkia8888_GrPixelConfig;
176 desc.fFlags = kRenderTarget_GrTextureFlagBit;
scroggo@google.comc6e1e9a2013-11-14 17:05:05 +0000177 desc.fWidth = SkScalarCeilToInt(width);
178 desc.fHeight = SkScalarCeilToInt(height);
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000179 desc.fSampleCnt = 0;
180 target.reset(gr->createUncachedTexture(desc, NULL, 0));
181 }
182 if (NULL == target.get()) {
183 SkASSERT(0);
184 return false;
185 }
186
187 device.reset(SkGpuDevice::Create(target));
188 }
189#endif
190 else {
191 SkDebugf("unknown --config: %s\n", FLAGS_config[0]);
192 return false;
193 }
edisonn@google.comac03d912013-07-22 15:36:39 +0000194 SkCanvas canvas(device);
edisonn@google.com222382b2013-07-10 22:33:10 +0000195
scroggo@google.com5092adc2013-12-02 20:22:31 +0000196#ifdef PDF_TRACE_DIFF_IN_PNG
edisonn@google.comac03d912013-07-22 15:36:39 +0000197 gDumpBitmap = &bitmap;
edisonn@google.comac03d912013-07-22 15:36:39 +0000198 gDumpCanvas = &canvas;
scroggo@google.com5092adc2013-12-02 20:22:31 +0000199#endif
edisonn@google.comac03d912013-07-22 15:36:39 +0000200 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
edisonn@google.com222382b2013-07-10 22:33:10 +0000201
edisonn@google.comac03d912013-07-22 15:36:39 +0000202 SkString outputPath;
203 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
204 return false;
205 }
206 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
207
208 if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000209 SkDebugf("Memory usage after page %i rendered: %u\n",
210 page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
edisonn@google.comac03d912013-07-22 15:36:39 +0000211 }
edisonn@google.com13102382013-07-11 14:50:12 +0000212 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000213 return true;
214}
edisonn@google.com222382b2013-07-10 22:33:10 +0000215
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000216/** Reads an skp file, renders it to pdf and writes the output to a pdf file
217 * @param inputPath The skp file to be read.
218 * @param outputDir Output dir.
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000219 */
scroggo@google.com90922892013-11-14 19:09:27 +0000220static bool process_pdf(const SkString& inputPath, const SkString& outputDir) {
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
scroggo@google.com90922892013-11-14 19:09:27 +0000225 SkAutoTDelete<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(inputPath.c_str()));
226 if (NULL == renderer.get()) {
227 SkDebugf("Failure loading file %s\n", inputPath.c_str());
228 return false;
229 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000230
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000231 if (FLAGS_showMemoryUsage) {
scroggo@google.com90922892013-11-14 19:09:27 +0000232 SkDebugf("Memory usage after load: %u\n", (unsigned int) renderer->bytesUsed());
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000233 }
234
235 // TODO(edisonn): bench timers
236 if (FLAGS_benchLoad > 0) {
237 for (int i = 0 ; i < FLAGS_benchLoad; i++) {
scroggo@google.com90922892013-11-14 19:09:27 +0000238 SkAutoTDelete<SkPdfRenderer> benchRenderer(
239 SkPdfRenderer::CreateFromFile(inputPath.c_str()));
240 if (NULL == benchRenderer.get()) {
241 SkDebugf("Failed to load on %ith attempt\n", i);
242 } else if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000243 SkDebugf("Memory usage after load %i number : %u\n", i,
scroggo@google.com90922892013-11-14 19:09:27 +0000244 (unsigned int) benchRenderer->bytesUsed());
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000245 }
246 }
247 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000248
scroggo@google.com90922892013-11-14 19:09:27 +0000249 if (!renderer->pages()) {
250 // This should never happen, since CreateFromFile will return NULL if there are no pages.
251 SkASSERT(false);
252 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
253 return false;
254 }
255
256 bool success = true;
257 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
258 // TODO(edisonn) if (i == 1) start timer
259 if (strcmp(FLAGS_pages[0], "all") == 0) {
260 for (int pn = 0; pn < renderer->pages(); ++pn) {
261 success &= render_page(outputDir, inputFilename, *renderer,
262 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : pn);
edisonn@google.com222382b2013-07-10 22:33:10 +0000263 }
scroggo@google.com90922892013-11-14 19:09:27 +0000264 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
265 for (int pn = renderer->pages() - 1; pn >= 0; --pn) {
266 success &= render_page(outputDir, inputFilename, *renderer,
267 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : pn);
268 }
269 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
270 success &= render_page(outputDir, inputFilename, *renderer,
271 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : 0);
272 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
273 success &= render_page(outputDir, inputFilename, *renderer,
274 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1
275 : renderer->pages() - 1);
276 } else {
277 int pn = atoi(FLAGS_pages[0]);
278 success &= render_page(outputDir, inputFilename, *renderer,
279 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : pn);
edisonn@google.com222382b2013-07-10 22:33:10 +0000280 }
281 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000282
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000283 if (!success) {
284 SkDebugf("Failures for file %s\n", inputPath.c_str());
285 }
286
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000287 return success;
288}
289
290/** For each file in the directory or for the file passed in input, call
291 * parse_pdf.
292 * @param input A directory or an pdf file.
293 * @param outputDir Output dir.
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000294 */
scroggo@google.com90922892013-11-14 19:09:27 +0000295static int process_input(const char* input, const SkString& outputDir) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000296 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000297 if (sk_isdir(input)) {
298 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000299 SkString inputFilename;
300 while (iter.next(&inputFilename)) {
scroggo@google.com5c7be952013-11-12 14:43:44 +0000301 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
scroggo@google.com90922892013-11-14 19:09:27 +0000302 if (!process_pdf(inputPath, outputDir)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000303 ++failures;
304 }
305 }
306 } else {
307 SkString inputPath(input);
scroggo@google.com90922892013-11-14 19:09:27 +0000308 if (!process_pdf(inputPath, outputDir)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000309 ++failures;
310 }
311 }
312 return failures;
313}
314
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000315int tool_main(int argc, char** argv);
316int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000317 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
318 SkCommandLineFlags::Parse(argc, argv);
319
320 if (FLAGS_readPath.isEmpty()) {
321 SkDebugf(".pdf files or directories are required.\n");
322 exit(-1);
323 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000324
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000325 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000326 if (FLAGS_writePath.count() == 1) {
327 outputDir.set(FLAGS_writePath[0]);
328 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000329
330 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000331 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
scroggo@google.com90922892013-11-14 19:09:27 +0000332 failures += process_input(FLAGS_readPath[i], outputDir);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000333 }
334
335 reportPdfRenderStats();
336
337 if (failures != 0) {
338 SkDebugf("Failed to render %i PDFs.\n", failures);
339 return 1;
340 }
341
342 return 0;
343}
344
345#if !defined SK_BUILD_FOR_IOS
346int main(int argc, char * const argv[]) {
347 return tool_main(argc, (char**) argv);
348}
349#endif