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" |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 16 | #include "SkPdfRenderer.h" |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 17 | #include "SkPicture.h" |
| 18 | #include "SkStream.h" |
| 19 | #include "SkTypeface.h" |
| 20 | #include "SkTArray.h" |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 21 | #include "SkNulCanvas.h" |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 22 | |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 23 | #if SK_SUPPORT_GPU |
| 24 | #include "GrContextFactory.h" |
| 25 | #include "GrContext.h" |
| 26 | #include "SkGpuDevice.h" |
| 27 | #endif |
| 28 | |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 29 | DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process."); |
| 30 | DEFINE_string2(writePath, w, "", "Directory to write the rendered pages."); |
| 31 | DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page."); |
| 32 | DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage."); |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 33 | DEFINE_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.com | 15b1118 | 2013-07-11 14:43:15 +0000 | [diff] [blame] | 40 | DEFINE_double(DPI, 72, "DPI to be used for rendering (scale)."); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 41 | DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any rendering and \n" |
| 42 | "\tminimal parsing to ensure correctness. Default 0 (disabled)."); |
| 43 | DEFINE_int32(benchRender, 0, "Render the pdf content N times. Default 0 (disabled)"); |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 44 | DEFINE_string2(config, c, "8888", "Canvas to render:\n" |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 45 | "\t8888 - argb\n" |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 46 | #if SK_SUPPORT_GPU |
| 47 | "\tgpu: use the gpu\n" |
| 48 | #endif |
| 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 | ); |
edisonn@google.com | 598cf5d | 2013-10-09 15:13:19 +0000 | [diff] [blame] | 51 | DEFINE_bool2(transparentBackground, t, false, "Make background transparent instead of white."); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 52 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 53 | /** |
| 54 | * Given list of directories and files to use as input, expects to find .pdf |
| 55 | * files and it will convert them to .png files writing them in the same directory |
| 56 | * one file for each page. |
| 57 | * |
| 58 | * Returns zero exit code if all .pdf files were converted successfully, |
| 59 | * otherwise returns error code 1. |
| 60 | */ |
| 61 | |
| 62 | static const char PDF_FILE_EXTENSION[] = "pdf"; |
| 63 | static const char PNG_FILE_EXTENSION[] = "png"; |
| 64 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 65 | /** Replaces the extension of a file. |
| 66 | * @param path File name whose extension will be changed. |
| 67 | * @param old_extension The old extension. |
| 68 | * @param new_extension The new extension. |
| 69 | * @returns false if the file did not has the expected extension. |
| 70 | * if false is returned, contents of path are undefined. |
| 71 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 72 | 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] | 73 | const char old_extension[], |
| 74 | const char new_extension[]) { |
| 75 | if (path->endsWith(old_extension)) { |
| 76 | path->remove(path->size() - strlen(old_extension), |
| 77 | strlen(old_extension)); |
| 78 | if (!path->endsWith(".")) { |
| 79 | return false; |
| 80 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 81 | if (page >= 0) { |
| 82 | path->appendf("%i.", page); |
| 83 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 84 | path->append(new_extension); |
| 85 | return true; |
| 86 | } |
| 87 | return false; |
| 88 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 89 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 90 | /** Builds the output filename. path = dir/name, and it replaces expected |
| 91 | * .skp extension with .pdf extention. |
| 92 | * @param path Output filename. |
| 93 | * @param name The name of the file. |
| 94 | * @returns false if the file did not has the expected extension. |
| 95 | * if false is returned, contents of path are undefined. |
| 96 | */ |
| 97 | static bool make_output_filepath(SkString* path, const SkString& dir, |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 98 | const SkString& name, |
| 99 | int page) { |
scroggo@google.com | 5c7be95 | 2013-11-12 14:43:44 +0000 | [diff] [blame] | 100 | *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str()); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 101 | return add_page_and_replace_filename_extension(path, page, |
| 102 | PDF_FILE_EXTENSION, |
| 103 | PNG_FILE_EXTENSION); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 104 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 105 | |
edisonn@google.com | 598cf5d | 2013-10-09 15:13:19 +0000 | [diff] [blame] | 106 | static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 107 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 108 | |
| 109 | bitmap->allocPixels(); |
| 110 | bitmap->eraseColor(color); |
| 111 | } |
| 112 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 113 | /** Write the output of pdf renderer to a file. |
| 114 | * @param outputDir Output dir. |
| 115 | * @param inputFilename The skp file that was read. |
| 116 | * @param renderer The object responsible to write the pdf file. |
edisonn@google.com | cdad30b | 2013-07-10 22:37:38 +0000 | [diff] [blame] | 117 | * @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] | 118 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 119 | |
edisonn@google.com | 1310238 | 2013-07-11 14:50:12 +0000 | [diff] [blame] | 120 | extern "C" SkBitmap* gDumpBitmap; |
| 121 | extern "C" SkCanvas* gDumpCanvas; |
| 122 | |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 123 | #if SK_SUPPORT_GPU |
| 124 | GrContextFactory gContextFactory; |
| 125 | #endif |
| 126 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 127 | static bool render_page(const SkString& outputDir, |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 128 | const SkString& inputFilename, |
| 129 | const SkPdfRenderer& renderer, |
| 130 | int page) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 131 | SkRect rect = renderer.MediaBox(page < 0 ? 0 :page); |
| 132 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 133 | // Exercise all pdf codepaths as in normal rendering, but no actual bits are changed. |
| 134 | if (!FLAGS_config.isEmpty() && strcmp(FLAGS_config[0], "nul") == 0) { |
| 135 | SkBitmap bitmap; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 136 | SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap))); |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 137 | SkNulCanvas canvas(device); |
| 138 | renderer.renderPage(page < 0 ? 0 : page, &canvas, rect); |
| 139 | } else { |
| 140 | // 8888 |
| 141 | SkRect rect = renderer.MediaBox(page < 0 ? 0 :page); |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 142 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 143 | SkBitmap bitmap; |
edisonn@google.com | 04068b1 | 2013-11-12 21:56:39 +0000 | [diff] [blame] | 144 | SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(FLAGS_DPI / 72.0)); |
| 145 | SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(FLAGS_DPI / 72.0)); |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 146 | |
| 147 | rect = SkRect::MakeWH(width, height); |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 148 | |
edisonn@google.com | 598cf5d | 2013-10-09 15:13:19 +0000 | [diff] [blame] | 149 | SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE; |
| 150 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 151 | #ifdef PDF_DEBUG_3X |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 152 | setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height), |
| 153 | background); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 154 | #else |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 155 | setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height), |
| 156 | background); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 157 | #endif |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 158 | SkAutoTUnref<SkBaseDevice> device; |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 159 | if (strcmp(FLAGS_config[0], "8888") == 0) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 160 | device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap))); |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 161 | } |
| 162 | #if SK_SUPPORT_GPU |
| 163 | else if (strcmp(FLAGS_config[0], "gpu") == 0) { |
| 164 | SkAutoTUnref<GrSurface> target; |
| 165 | GrContext* gr = gContextFactory.get(GrContextFactory::kNative_GLContextType); |
| 166 | if (gr) { |
| 167 | // create a render target to back the device |
| 168 | GrTextureDesc desc; |
| 169 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 170 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 171 | desc.fWidth = width; |
| 172 | desc.fHeight = height; |
| 173 | desc.fSampleCnt = 0; |
| 174 | target.reset(gr->createUncachedTexture(desc, NULL, 0)); |
| 175 | } |
| 176 | if (NULL == target.get()) { |
| 177 | SkASSERT(0); |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | device.reset(SkGpuDevice::Create(target)); |
| 182 | } |
| 183 | #endif |
| 184 | else { |
| 185 | SkDebugf("unknown --config: %s\n", FLAGS_config[0]); |
| 186 | return false; |
| 187 | } |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 188 | SkCanvas canvas(device); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 189 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 190 | gDumpBitmap = &bitmap; |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 191 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 192 | gDumpCanvas = &canvas; |
| 193 | renderer.renderPage(page < 0 ? 0 : page, &canvas, rect); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 194 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 195 | SkString outputPath; |
| 196 | if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) { |
| 197 | return false; |
| 198 | } |
| 199 | SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); |
| 200 | |
| 201 | if (FLAGS_showMemoryUsage) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 202 | SkDebugf("Memory usage after page %i rendered: %u\n", |
| 203 | page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed()); |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 204 | } |
edisonn@google.com | 1310238 | 2013-07-11 14:50:12 +0000 | [diff] [blame] | 205 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 206 | return true; |
| 207 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 208 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 209 | /** Reads an skp file, renders it to pdf and writes the output to a pdf file |
| 210 | * @param inputPath The skp file to be read. |
| 211 | * @param outputDir Output dir. |
| 212 | * @param renderer The object responsible to render the skp object into pdf. |
| 213 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 214 | static bool process_pdf(const SkString& inputPath, const SkString& outputDir, |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 215 | SkPdfRenderer& renderer) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 216 | SkDebugf("Loading PDF: %s\n", inputPath.c_str()); |
| 217 | |
scroggo@google.com | 5c7be95 | 2013-11-12 14:43:44 +0000 | [diff] [blame] | 218 | SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str()); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 219 | |
edisonn@google.com | 2273f9b | 2013-08-06 21:48:44 +0000 | [diff] [blame] | 220 | bool success = true; |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 221 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 222 | success = renderer.load(inputPath); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 223 | if (FLAGS_showMemoryUsage) { |
| 224 | SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed()); |
| 225 | } |
| 226 | |
| 227 | // TODO(edisonn): bench timers |
| 228 | if (FLAGS_benchLoad > 0) { |
| 229 | for (int i = 0 ; i < FLAGS_benchLoad; i++) { |
edisonn@google.com | 2273f9b | 2013-08-06 21:48:44 +0000 | [diff] [blame] | 230 | success = renderer.load(inputPath) && success; |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 231 | if (FLAGS_showMemoryUsage) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 232 | SkDebugf("Memory usage after load %i number : %u\n", i, |
| 233 | (unsigned int)renderer.bytesUsed()); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 237 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 238 | if (success) { |
| 239 | if (!renderer.pages()) |
| 240 | { |
| 241 | SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); |
| 242 | return false; |
| 243 | } else { |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 244 | for (int i = 0; i < FLAGS_benchRender + 1; i++) { |
| 245 | // TODO(edisonn) if (i == 1) start timer |
| 246 | if (strcmp(FLAGS_pages[0], "all") == 0) { |
| 247 | for (int pn = 0; pn < renderer.pages(); ++pn) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 248 | success = render_page( |
| 249 | outputDir, |
| 250 | inputFilename, |
| 251 | renderer, |
| 252 | FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : |
| 253 | pn) && |
| 254 | success; |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 255 | } |
| 256 | } else if (strcmp(FLAGS_pages[0], "reverse") == 0) { |
| 257 | for (int pn = renderer.pages() - 1; pn >= 0; --pn) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 258 | success = render_page( |
| 259 | outputDir, |
| 260 | inputFilename, |
| 261 | renderer, |
| 262 | FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : |
| 263 | pn) && |
| 264 | success; |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 265 | } |
| 266 | } else if (strcmp(FLAGS_pages[0], "first") == 0) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 267 | success = render_page( |
| 268 | outputDir, |
| 269 | inputFilename, |
| 270 | renderer, |
| 271 | FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && |
| 272 | success; |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 273 | } else if (strcmp(FLAGS_pages[0], "last") == 0) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 274 | success = render_page( |
| 275 | outputDir, |
| 276 | inputFilename, |
| 277 | renderer, |
| 278 | FLAGS_noExtensionForOnePagePdf && |
| 279 | renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success; |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 280 | } else { |
| 281 | int pn = atoi(FLAGS_pages[0]); |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 282 | success = render_page(outputDir, inputFilename, renderer, |
| 283 | FLAGS_noExtensionForOnePagePdf && |
| 284 | renderer.pages() == 1 ? -1 : pn) && success; |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 285 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 289 | |
edisonn@google.com | 2273f9b | 2013-08-06 21:48:44 +0000 | [diff] [blame] | 290 | if (!success) { |
| 291 | SkDebugf("Failures for file %s\n", inputPath.c_str()); |
| 292 | } |
| 293 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 294 | return success; |
| 295 | } |
| 296 | |
| 297 | /** For each file in the directory or for the file passed in input, call |
| 298 | * parse_pdf. |
| 299 | * @param input A directory or an pdf file. |
| 300 | * @param outputDir Output dir. |
| 301 | * @param renderer The object responsible to render the skp object into pdf. |
| 302 | */ |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 303 | static int process_input(const char* input, const SkString& outputDir, |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 304 | SkPdfRenderer& renderer) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 305 | int failures = 0; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 306 | if (sk_isdir(input)) { |
| 307 | SkOSFile::Iter iter(input, PDF_FILE_EXTENSION); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 308 | SkString inputFilename; |
| 309 | while (iter.next(&inputFilename)) { |
scroggo@google.com | 5c7be95 | 2013-11-12 14:43:44 +0000 | [diff] [blame] | 310 | SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str()); |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 311 | if (!process_pdf(inputPath, outputDir, renderer)) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 312 | ++failures; |
| 313 | } |
| 314 | } |
| 315 | } else { |
| 316 | SkString inputPath(input); |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 317 | if (!process_pdf(inputPath, outputDir, renderer)) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 318 | ++failures; |
| 319 | } |
| 320 | } |
| 321 | return failures; |
| 322 | } |
| 323 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 324 | int tool_main(int argc, char** argv); |
| 325 | int tool_main(int argc, char** argv) { |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 326 | SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer)."); |
| 327 | SkCommandLineFlags::Parse(argc, argv); |
| 328 | |
| 329 | if (FLAGS_readPath.isEmpty()) { |
| 330 | SkDebugf(".pdf files or directories are required.\n"); |
| 331 | exit(-1); |
| 332 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 333 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 334 | SkPdfRenderer renderer; |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 335 | |
| 336 | SkString outputDir; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 337 | if (FLAGS_writePath.count() == 1) { |
| 338 | outputDir.set(FLAGS_writePath[0]); |
| 339 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 340 | |
| 341 | int failures = 0; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 342 | for (int i = 0; i < FLAGS_readPath.count(); i ++) { |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 343 | failures += process_input(FLAGS_readPath[i], outputDir, renderer); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 344 | renderer.unload(); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | reportPdfRenderStats(); |
| 348 | |
| 349 | if (failures != 0) { |
| 350 | SkDebugf("Failed to render %i PDFs.\n", failures); |
| 351 | return 1; |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | #if !defined SK_BUILD_FOR_IOS |
| 358 | int main(int argc, char * const argv[]) { |
| 359 | return tool_main(argc, (char**) argv); |
| 360 | } |
| 361 | #endif |