blob: 5ecd2dbd97dcab019d14f586ac79059b5bfcdcb1 [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.
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000217 */
scroggo@google.com90922892013-11-14 19:09:27 +0000218static bool process_pdf(const SkString& inputPath, const SkString& outputDir) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000219 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
220
scroggo@google.com5c7be952013-11-12 14:43:44 +0000221 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000222
scroggo@google.com90922892013-11-14 19:09:27 +0000223 SkAutoTDelete<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(inputPath.c_str()));
224 if (NULL == renderer.get()) {
225 SkDebugf("Failure loading file %s\n", inputPath.c_str());
226 return false;
227 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000228
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000229 if (FLAGS_showMemoryUsage) {
scroggo@google.com90922892013-11-14 19:09:27 +0000230 SkDebugf("Memory usage after load: %u\n", (unsigned int) renderer->bytesUsed());
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000231 }
232
233 // TODO(edisonn): bench timers
234 if (FLAGS_benchLoad > 0) {
235 for (int i = 0 ; i < FLAGS_benchLoad; i++) {
scroggo@google.com90922892013-11-14 19:09:27 +0000236 SkAutoTDelete<SkPdfRenderer> benchRenderer(
237 SkPdfRenderer::CreateFromFile(inputPath.c_str()));
238 if (NULL == benchRenderer.get()) {
239 SkDebugf("Failed to load on %ith attempt\n", i);
240 } else if (FLAGS_showMemoryUsage) {
edisonn@google.come50d9a12013-10-10 20:58:22 +0000241 SkDebugf("Memory usage after load %i number : %u\n", i,
scroggo@google.com90922892013-11-14 19:09:27 +0000242 (unsigned int) benchRenderer->bytesUsed());
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000243 }
244 }
245 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000246
scroggo@google.com90922892013-11-14 19:09:27 +0000247 if (!renderer->pages()) {
248 // This should never happen, since CreateFromFile will return NULL if there are no pages.
249 SkASSERT(false);
250 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
251 return false;
252 }
253
254 bool success = true;
255 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
256 // TODO(edisonn) if (i == 1) start timer
257 if (strcmp(FLAGS_pages[0], "all") == 0) {
258 for (int pn = 0; pn < renderer->pages(); ++pn) {
259 success &= render_page(outputDir, inputFilename, *renderer,
260 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : pn);
edisonn@google.com222382b2013-07-10 22:33:10 +0000261 }
scroggo@google.com90922892013-11-14 19:09:27 +0000262 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
263 for (int pn = renderer->pages() - 1; pn >= 0; --pn) {
264 success &= render_page(outputDir, inputFilename, *renderer,
265 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : pn);
266 }
267 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
268 success &= render_page(outputDir, inputFilename, *renderer,
269 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : 0);
270 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
271 success &= render_page(outputDir, inputFilename, *renderer,
272 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1
273 : renderer->pages() - 1);
274 } else {
275 int pn = atoi(FLAGS_pages[0]);
276 success &= render_page(outputDir, inputFilename, *renderer,
277 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -1 : pn);
edisonn@google.com222382b2013-07-10 22:33:10 +0000278 }
279 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000280
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000281 if (!success) {
282 SkDebugf("Failures for file %s\n", inputPath.c_str());
283 }
284
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000285 return success;
286}
287
288/** For each file in the directory or for the file passed in input, call
289 * parse_pdf.
290 * @param input A directory or an pdf file.
291 * @param outputDir Output dir.
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000292 */
scroggo@google.com90922892013-11-14 19:09:27 +0000293static int process_input(const char* input, const SkString& outputDir) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000294 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000295 if (sk_isdir(input)) {
296 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000297 SkString inputFilename;
298 while (iter.next(&inputFilename)) {
scroggo@google.com5c7be952013-11-12 14:43:44 +0000299 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
scroggo@google.com90922892013-11-14 19:09:27 +0000300 if (!process_pdf(inputPath, outputDir)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000301 ++failures;
302 }
303 }
304 } else {
305 SkString inputPath(input);
scroggo@google.com90922892013-11-14 19:09:27 +0000306 if (!process_pdf(inputPath, outputDir)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000307 ++failures;
308 }
309 }
310 return failures;
311}
312
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000313int tool_main(int argc, char** argv);
314int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000315 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
316 SkCommandLineFlags::Parse(argc, argv);
317
318 if (FLAGS_readPath.isEmpty()) {
319 SkDebugf(".pdf files or directories are required.\n");
320 exit(-1);
321 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000322
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000323 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000324 if (FLAGS_writePath.count() == 1) {
325 outputDir.set(FLAGS_writePath[0]);
326 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000327
328 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000329 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
scroggo@google.com90922892013-11-14 19:09:27 +0000330 failures += process_input(FLAGS_readPath[i], outputDir);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000331 }
332
333 reportPdfRenderStats();
334
335 if (failures != 0) {
336 SkDebugf("Failed to render %i PDFs.\n", failures);
337 return 1;
338 }
339
340 return 0;
341}
342
343#if !defined SK_BUILD_FOR_IOS
344int main(int argc, char * const argv[]) {
345 return tool_main(argc, (char**) argv);
346}
347#endif