pdf_viewer_main.cpp revision 04068b13e5ec0c0aa2ab0fe82990ec8146c33243
1/*
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
8#include "SkCanvas.h"
9#include "SkCommandLineFlags.h"
10#include "SkDevice.h"
11#include "SkGraphics.h"
12#include "SkImageDecoder.h"
13#include "SkImageEncoder.h"
14#include "SkOSFile.h"
15#include "SkPdfRenderer.h"
16#include "SkPicture.h"
17#include "SkStream.h"
18#include "SkTypeface.h"
19#include "SkTArray.h"
20#include "SkNulCanvas.h"
21
22#if SK_SUPPORT_GPU
23#include "GrContextFactory.h"
24#include "GrContext.h"
25#include "SkGpuDevice.h"
26#endif
27
28DEFINE_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.");
32DEFINE_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               );
39DEFINE_double(DPI, 72, "DPI to be used for rendering (scale).");
40DEFINE_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)");
43DEFINE_string2(config, c, "8888", "Canvas to render:\n"
44                                  "\t8888 - argb\n"
45#if SK_SUPPORT_GPU
46                                  "\tgpu: use the gpu\n"
47#endif
48                                  "\tnul - render in null canvas, any draw will just return.\n"
49               );
50DEFINE_bool2(transparentBackground, t, false, "Make background transparent instead of white.");
51
52/**
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
64/** 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 */
71static bool add_page_and_replace_filename_extension(SkString* path, int page,
72                                       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        }
80        if (page >= 0) {
81            path->appendf("%i.", page);
82        }
83        path->append(new_extension);
84        return true;
85    }
86    return false;
87}
88
89/** 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,
97                                 const SkString& name,
98                                 int page) {
99    *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
100    return add_page_and_replace_filename_extension(path, page,
101                                                   PDF_FILE_EXTENSION,
102                                                   PNG_FILE_EXTENSION);
103}
104
105static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) {
106    bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
107
108    bitmap->allocPixels();
109    bitmap->eraseColor(color);
110}
111
112/** 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.
116 * @param page -1 means there is only one page (0), and render in a file without page extension
117 */
118
119extern "C" SkBitmap* gDumpBitmap;
120extern "C" SkCanvas* gDumpCanvas;
121
122#if SK_SUPPORT_GPU
123GrContextFactory gContextFactory;
124#endif
125
126static bool render_page(const SkString& outputDir,
127                        const SkString& inputFilename,
128                        const SkPdfRenderer& renderer,
129                        int page) {
130    SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
131
132    // 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;
135        SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
136        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);
141
142        SkBitmap bitmap;
143        SkScalar width = SkScalarMul(rect.width(),  SkDoubleToScalar(FLAGS_DPI / 72.0));
144        SkScalar height = SkScalarMul(rect.height(),  SkDoubleToScalar(FLAGS_DPI / 72.0));
145
146        rect = SkRect::MakeWH(width, height);
147
148        SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE;
149
150#ifdef PDF_DEBUG_3X
151        setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height),
152                     background);
153#else
154        setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height),
155                     background);
156#endif
157        SkAutoTUnref<SkBaseDevice> device;
158        if (strcmp(FLAGS_config[0], "8888") == 0) {
159            device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
160        }
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        }
187        SkCanvas canvas(device);
188
189        gDumpBitmap = &bitmap;
190
191        gDumpCanvas = &canvas;
192        renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
193
194        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) {
201            SkDebugf("Memory usage after page %i rendered: %u\n",
202                     page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
203        }
204    }
205    return true;
206}
207
208/** 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 */
213static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
214                        SkPdfRenderer& renderer) {
215    SkDebugf("Loading PDF:  %s\n", inputPath.c_str());
216
217    SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
218
219    bool success = true;
220
221    success = renderer.load(inputPath);
222    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++) {
229            success = renderer.load(inputPath) && success;
230            if (FLAGS_showMemoryUsage) {
231                SkDebugf("Memory usage after load %i number : %u\n", i,
232                         (unsigned int)renderer.bytesUsed());
233            }
234        }
235    }
236
237    if (success) {
238        if (!renderer.pages())
239        {
240            SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
241            return false;
242        } else {
243            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) {
247                        success = render_page(
248                                outputDir,
249                                inputFilename,
250                                renderer,
251                                FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 :
252                                                                                          pn) &&
253                                     success;
254                    }
255                } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
256                    for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
257                        success = render_page(
258                                outputDir,
259                                inputFilename,
260                                renderer,
261                                FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 :
262                                                                                          pn) &&
263                                   success;
264                    }
265                } else if (strcmp(FLAGS_pages[0], "first") == 0) {
266                    success = render_page(
267                            outputDir,
268                            inputFilename,
269                            renderer,
270                            FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) &&
271                                success;
272                } else if (strcmp(FLAGS_pages[0], "last") == 0) {
273                    success = render_page(
274                            outputDir,
275                            inputFilename,
276                            renderer,
277                            FLAGS_noExtensionForOnePagePdf &&
278                                    renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
279                } else {
280                    int pn = atoi(FLAGS_pages[0]);
281                    success = render_page(outputDir, inputFilename, renderer,
282                                          FLAGS_noExtensionForOnePagePdf &&
283                                              renderer.pages() == 1 ? -1 : pn) && success;
284                }
285            }
286        }
287    }
288
289    if (!success) {
290        SkDebugf("Failures for file %s\n", inputPath.c_str());
291    }
292
293    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 */
302static int process_input(const char* input, const SkString& outputDir,
303                         SkPdfRenderer& renderer) {
304    int failures = 0;
305    if (sk_isdir(input)) {
306        SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
307        SkString inputFilename;
308        while (iter.next(&inputFilename)) {
309            SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
310            if (!process_pdf(inputPath, outputDir, renderer)) {
311                ++failures;
312            }
313        }
314    } else {
315        SkString inputPath(input);
316        if (!process_pdf(inputPath, outputDir, renderer)) {
317            ++failures;
318        }
319    }
320    return failures;
321}
322
323int tool_main(int argc, char** argv);
324int tool_main(int argc, char** argv) {
325    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    }
332
333    SkPdfRenderer renderer;
334
335    SkString outputDir;
336    if (FLAGS_writePath.count() == 1) {
337        outputDir.set(FLAGS_writePath[0]);
338    }
339
340    int failures = 0;
341    for (int i = 0; i < FLAGS_readPath.count(); i ++) {
342        failures += process_input(FLAGS_readPath[i], outputDir, renderer);
343        renderer.unload();
344    }
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
361