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 "SkBitmapDevice.h"
9#include "SkCanvas.h"
10#include "SkCommandLineFlags.h"
11#include "SkDevice.h"
12#include "SkGraphics.h"
13#include "SkImageDecoder.h"
14#include "SkImageEncoder.h"
15#include "SkOSFile.h"
16#include "SkPdfConfig.h"
17#include "SkPdfRenderer.h"
18#include "SkPicture.h"
19#include "SkStream.h"
20#include "SkTypeface.h"
21#include "SkTArray.h"
22#include "SkNulCanvas.h"
23
24#if SK_SUPPORT_GPU
25#include "GrContextFactory.h"
26#include "GrContext.h"
27#include "SkGpuDevice.h"
28#endif
29
30DEFINE_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.");
34DEFINE_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               );
41DEFINE_double(DPI, 72, "DPI to be used for rendering (scale).");
42DEFINE_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)");
45#if SK_SUPPORT_GPU
46DEFINE_string2(config, c, "8888", "Canvas to render:\n"
47                                  "\t8888 - argb\n"
48                                  "\tgpu: use the gpu\n"
49                                  "\tnul - render in null canvas, any draw will just return.\n"
50               );
51#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
57DEFINE_bool2(transparentBackground, t, false, "Make background transparent instead of white.");
58
59/**
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
71/** 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 */
78static bool add_page_and_replace_filename_extension(SkString* path, int page,
79                                       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        }
87        if (page >= 0) {
88            path->appendf("%i.", page);
89        }
90        path->append(new_extension);
91        return true;
92    }
93    return false;
94}
95
96/** 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,
104                                 const SkString& name,
105                                 int page) {
106    *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
107    return add_page_and_replace_filename_extension(path, page,
108                                                   PDF_FILE_EXTENSION,
109                                                   PNG_FILE_EXTENSION);
110}
111
112static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) {
113    bitmap->allocN32Pixels(width, height);
114    bitmap->eraseColor(color);
115}
116
117/** 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.
121 * @param page -1 means there is only one page (0), and render in a file without page extension
122 */
123
124#ifdef PDF_TRACE_DIFF_IN_PNG
125extern "C" SkBitmap* gDumpBitmap;
126extern "C" SkCanvas* gDumpCanvas;
127#endif
128
129#if SK_SUPPORT_GPU
130GrContextFactory gContextFactory;
131#endif
132
133static bool render_page(const SkString& outputDir,
134                        const SkString& inputFilename,
135                        const SkPdfRenderer& renderer,
136                        int page) {
137    SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
138
139    // 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;
142        SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
143        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);
148
149        SkBitmap bitmap;
150        SkScalar width = SkScalarMul(rect.width(),  SkDoubleToScalar(FLAGS_DPI / 72.0));
151        SkScalar height = SkScalarMul(rect.height(),  SkDoubleToScalar(FLAGS_DPI / 72.0));
152
153        rect = SkRect::MakeWH(width, height);
154
155        SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE;
156
157#ifdef PDF_DEBUG_3X
158        setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height),
159                     background);
160#else
161        setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height),
162                     background);
163#endif
164        SkAutoTUnref<SkBaseDevice> device;
165        if (strcmp(FLAGS_config[0], "8888") == 0) {
166            device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
167        }
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;
177                desc.fWidth = SkScalarCeilToInt(width);
178                desc.fHeight = SkScalarCeilToInt(height);
179                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        }
194        SkCanvas canvas(device);
195
196#ifdef PDF_TRACE_DIFF_IN_PNG
197        gDumpBitmap = &bitmap;
198        gDumpCanvas = &canvas;
199#endif
200        renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
201
202        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) {
209            SkDebugf("Memory usage after page %i rendered: %u\n",
210                     page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
211        }
212    }
213    return true;
214}
215
216/** 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.
219 */
220static bool process_pdf(const SkString& inputPath, const SkString& outputDir) {
221    SkDebugf("Loading PDF:  %s\n", inputPath.c_str());
222
223    SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
224
225    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    }
230
231    if (FLAGS_showMemoryUsage) {
232        SkDebugf("Memory usage after load: %u\n", (unsigned int) renderer->bytesUsed());
233    }
234
235    // TODO(edisonn): bench timers
236    if (FLAGS_benchLoad > 0) {
237        for (int i = 0 ; i < FLAGS_benchLoad; i++) {
238            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) {
243                SkDebugf("Memory usage after load %i number : %u\n", i,
244                         (unsigned int) benchRenderer->bytesUsed());
245            }
246        }
247    }
248
249    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);
263            }
264        } 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);
280        }
281    }
282
283    if (!success) {
284        SkDebugf("Failures for file %s\n", inputPath.c_str());
285    }
286
287    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.
294 */
295static int process_input(const char* input, const SkString& outputDir) {
296    int failures = 0;
297    if (sk_isdir(input)) {
298        SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
299        SkString inputFilename;
300        while (iter.next(&inputFilename)) {
301            SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
302            if (!process_pdf(inputPath, outputDir)) {
303                ++failures;
304            }
305        }
306    } else {
307        SkString inputPath(input);
308        if (!process_pdf(inputPath, outputDir)) {
309            ++failures;
310        }
311    }
312    return failures;
313}
314
315int tool_main(int argc, char** argv);
316int tool_main(int argc, char** argv) {
317    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    }
324
325    SkString outputDir;
326    if (FLAGS_writePath.count() == 1) {
327        outputDir.set(FLAGS_writePath[0]);
328    }
329
330    int failures = 0;
331    for (int i = 0; i < FLAGS_readPath.count(); i ++) {
332        failures += process_input(FLAGS_readPath[i], outputDir);
333    }
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
350