edisonn@google.com | cf2cfa1 | 2013-08-21 16:31:37 +0000 | [diff] [blame] | 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 | |
scroggo@google.com | 2212538 | 2013-11-14 16:01:23 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 10 | #include "SkCommandLineFlags.h" |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 11 | #include "SkDevice.h" |
| 12 | #include "SkGraphics.h" |
| 13 | #include "SkImageDecoder.h" |
| 14 | #include "SkImageEncoder.h" |
| 15 | #include "SkOSFile.h" |
scroggo@google.com | 5092adc | 2013-12-02 20:22:31 +0000 | [diff] [blame] | 16 | #include "SkPdfConfig.h" |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 17 | #include "SkPdfRenderer.h" |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 18 | #include "SkPicture.h" |
| 19 | #include "SkStream.h" |
| 20 | #include "SkTypeface.h" |
| 21 | #include "SkTArray.h" |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 22 | #include "SkNulCanvas.h" |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 23 | |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 24 | #if SK_SUPPORT_GPU |
| 25 | #include "GrContextFactory.h" |
| 26 | #include "GrContext.h" |
| 27 | #include "SkGpuDevice.h" |
| 28 | #endif |
| 29 | |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 30 | DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process."); |
| 31 | DEFINE_string2(writePath, w, "", "Directory to write the rendered pages."); |
| 32 | DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page."); |
| 33 | DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage."); |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 34 | DEFINE_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.com | 15b1118 | 2013-07-11 14:43:15 +0000 | [diff] [blame] | 41 | DEFINE_double(DPI, 72, "DPI to be used for rendering (scale)."); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 42 | DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any rendering and \n" |
| 43 | "\tminimal parsing to ensure correctness. Default 0 (disabled)."); |
| 44 | DEFINE_int32(benchRender, 0, "Render the pdf content N times. Default 0 (disabled)"); |
scroggo@google.com | c6e1e9a | 2013-11-14 17:05:05 +0000 | [diff] [blame] | 45 | #if SK_SUPPORT_GPU |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 46 | DEFINE_string2(config, c, "8888", "Canvas to render:\n" |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 47 | "\t8888 - argb\n" |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 48 | "\tgpu: use the gpu\n" |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 49 | "\tnul - render in null canvas, any draw will just return.\n" |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 50 | ); |
scroggo@google.com | c6e1e9a | 2013-11-14 17:05:05 +0000 | [diff] [blame] | 51 | #else |
| 52 | DEFINE_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.com | 598cf5d | 2013-10-09 15:13:19 +0000 | [diff] [blame] | 57 | DEFINE_bool2(transparentBackground, t, false, "Make background transparent instead of white."); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 58 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 | |
| 68 | static const char PDF_FILE_EXTENSION[] = "pdf"; |
| 69 | static const char PNG_FILE_EXTENSION[] = "png"; |
| 70 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 78 | static bool add_page_and_replace_filename_extension(SkString* path, int page, |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 87 | if (page >= 0) { |
| 88 | path->appendf("%i.", page); |
| 89 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 90 | path->append(new_extension); |
| 91 | return true; |
| 92 | } |
| 93 | return false; |
| 94 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 95 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 | */ |
| 103 | static bool make_output_filepath(SkString* path, const SkString& dir, |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 104 | const SkString& name, |
| 105 | int page) { |
scroggo@google.com | 5c7be95 | 2013-11-12 14:43:44 +0000 | [diff] [blame] | 106 | *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str()); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 107 | return add_page_and_replace_filename_extension(path, page, |
| 108 | PDF_FILE_EXTENSION, |
| 109 | PNG_FILE_EXTENSION); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 110 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 111 | |
edisonn@google.com | 598cf5d | 2013-10-09 15:13:19 +0000 | [diff] [blame] | 112 | static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 113 | bitmap->allocN32Pixels(width, height); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 114 | bitmap->eraseColor(color); |
| 115 | } |
| 116 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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. |
edisonn@google.com | cdad30b | 2013-07-10 22:37:38 +0000 | [diff] [blame] | 121 | * @param page -1 means there is only one page (0), and render in a file without page extension |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 122 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 123 | |
scroggo@google.com | 5092adc | 2013-12-02 20:22:31 +0000 | [diff] [blame] | 124 | #ifdef PDF_TRACE_DIFF_IN_PNG |
edisonn@google.com | 1310238 | 2013-07-11 14:50:12 +0000 | [diff] [blame] | 125 | extern "C" SkBitmap* gDumpBitmap; |
| 126 | extern "C" SkCanvas* gDumpCanvas; |
scroggo@google.com | 5092adc | 2013-12-02 20:22:31 +0000 | [diff] [blame] | 127 | #endif |
edisonn@google.com | 1310238 | 2013-07-11 14:50:12 +0000 | [diff] [blame] | 128 | |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 129 | #if SK_SUPPORT_GPU |
| 130 | GrContextFactory gContextFactory; |
| 131 | #endif |
| 132 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 133 | static bool render_page(const SkString& outputDir, |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 134 | const SkString& inputFilename, |
| 135 | const SkPdfRenderer& renderer, |
| 136 | int page) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 137 | SkRect rect = renderer.MediaBox(page < 0 ? 0 :page); |
| 138 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 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; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 142 | SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap))); |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 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); |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 148 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 149 | SkBitmap bitmap; |
edisonn@google.com | 04068b1 | 2013-11-12 21:56:39 +0000 | [diff] [blame] | 150 | SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(FLAGS_DPI / 72.0)); |
| 151 | SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(FLAGS_DPI / 72.0)); |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 152 | |
| 153 | rect = SkRect::MakeWH(width, height); |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 154 | |
edisonn@google.com | 598cf5d | 2013-10-09 15:13:19 +0000 | [diff] [blame] | 155 | SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE; |
| 156 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 157 | #ifdef PDF_DEBUG_3X |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 158 | setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height), |
| 159 | background); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 160 | #else |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 161 | setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height), |
| 162 | background); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 163 | #endif |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 164 | SkAutoTUnref<SkBaseDevice> device; |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 165 | if (strcmp(FLAGS_config[0], "8888") == 0) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 166 | device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap))); |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 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; |
scroggo@google.com | c6e1e9a | 2013-11-14 17:05:05 +0000 | [diff] [blame] | 177 | desc.fWidth = SkScalarCeilToInt(width); |
| 178 | desc.fHeight = SkScalarCeilToInt(height); |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 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 | } |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 194 | SkCanvas canvas(device); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 195 | |
scroggo@google.com | 5092adc | 2013-12-02 20:22:31 +0000 | [diff] [blame] | 196 | #ifdef PDF_TRACE_DIFF_IN_PNG |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 197 | gDumpBitmap = &bitmap; |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 198 | gDumpCanvas = &canvas; |
scroggo@google.com | 5092adc | 2013-12-02 20:22:31 +0000 | [diff] [blame] | 199 | #endif |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 200 | renderer.renderPage(page < 0 ? 0 : page, &canvas, rect); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 201 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 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) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 209 | SkDebugf("Memory usage after page %i rendered: %u\n", |
| 210 | page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed()); |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 211 | } |
edisonn@google.com | 1310238 | 2013-07-11 14:50:12 +0000 | [diff] [blame] | 212 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 213 | return true; |
| 214 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 215 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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. |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 219 | */ |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 220 | static bool process_pdf(const SkString& inputPath, const SkString& outputDir) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 221 | SkDebugf("Loading PDF: %s\n", inputPath.c_str()); |
| 222 | |
scroggo@google.com | 5c7be95 | 2013-11-12 14:43:44 +0000 | [diff] [blame] | 223 | SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str()); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 224 | |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 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 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 230 | |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 231 | if (FLAGS_showMemoryUsage) { |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 232 | SkDebugf("Memory usage after load: %u\n", (unsigned int) renderer->bytesUsed()); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // TODO(edisonn): bench timers |
| 236 | if (FLAGS_benchLoad > 0) { |
| 237 | for (int i = 0 ; i < FLAGS_benchLoad; i++) { |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 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) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 243 | SkDebugf("Memory usage after load %i number : %u\n", i, |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 244 | (unsigned int) benchRenderer->bytesUsed()); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 248 | |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 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); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 263 | } |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 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); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 282 | |
edisonn@google.com | 2273f9b | 2013-08-06 21:48:44 +0000 | [diff] [blame] | 283 | if (!success) { |
| 284 | SkDebugf("Failures for file %s\n", inputPath.c_str()); |
| 285 | } |
| 286 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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. |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 294 | */ |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 295 | static int process_input(const char* input, const SkString& outputDir) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 296 | int failures = 0; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 297 | if (sk_isdir(input)) { |
| 298 | SkOSFile::Iter iter(input, PDF_FILE_EXTENSION); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 299 | SkString inputFilename; |
| 300 | while (iter.next(&inputFilename)) { |
scroggo@google.com | 5c7be95 | 2013-11-12 14:43:44 +0000 | [diff] [blame] | 301 | SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str()); |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 302 | if (!process_pdf(inputPath, outputDir)) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 303 | ++failures; |
| 304 | } |
| 305 | } |
| 306 | } else { |
| 307 | SkString inputPath(input); |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 308 | if (!process_pdf(inputPath, outputDir)) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 309 | ++failures; |
| 310 | } |
| 311 | } |
| 312 | return failures; |
| 313 | } |
| 314 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 315 | int tool_main(int argc, char** argv); |
| 316 | int tool_main(int argc, char** argv) { |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 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 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 324 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 325 | SkString outputDir; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 326 | if (FLAGS_writePath.count() == 1) { |
| 327 | outputDir.set(FLAGS_writePath[0]); |
| 328 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 329 | |
| 330 | int failures = 0; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 331 | for (int i = 0; i < FLAGS_readPath.count(); i ++) { |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 332 | failures += process_input(FLAGS_readPath[i], outputDir); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 |
| 346 | int main(int argc, char * const argv[]) { |
| 347 | return tool_main(argc, (char**) argv); |
| 348 | } |
| 349 | #endif |