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 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 9 | #include "SkCommandLineFlags.h" |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 10 | #include "SkDevice.h" |
| 11 | #include "SkGraphics.h" |
| 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkImageEncoder.h" |
| 14 | #include "SkOSFile.h" |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 15 | #include "SkPdfRenderer.h" |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 16 | #include "SkPicture.h" |
| 17 | #include "SkStream.h" |
| 18 | #include "SkTypeface.h" |
| 19 | #include "SkTArray.h" |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 20 | #include "SkNulCanvas.h" |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 21 | |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 22 | #if SK_SUPPORT_GPU |
| 23 | #include "GrContextFactory.h" |
| 24 | #include "GrContext.h" |
| 25 | #include "SkGpuDevice.h" |
| 26 | #endif |
| 27 | |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 28 | DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process."); |
| 29 | DEFINE_string2(writePath, w, "", "Directory to write the rendered pages."); |
| 30 | DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page."); |
| 31 | DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage."); |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 32 | DEFINE_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 | ); |
edisonn@google.com | 15b1118 | 2013-07-11 14:43:15 +0000 | [diff] [blame] | 39 | DEFINE_double(DPI, 72, "DPI to be used for rendering (scale)."); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 40 | DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any rendering and \n" |
| 41 | "\tminimal parsing to ensure correctness. Default 0 (disabled)."); |
| 42 | 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] | 43 | DEFINE_string2(config, c, "8888", "Canvas to render:\n" |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 44 | "\t8888 - argb\n" |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 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" |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 49 | ); |
edisonn@google.com | 598cf5d | 2013-10-09 15:13:19 +0000 | [diff] [blame] | 50 | DEFINE_bool2(transparentBackground, t, false, "Make background transparent instead of white."); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 51 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 | |
| 61 | static const char PDF_FILE_EXTENSION[] = "pdf"; |
| 62 | static const char PNG_FILE_EXTENSION[] = "png"; |
| 63 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 71 | 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] | 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 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 80 | if (page >= 0) { |
| 81 | path->appendf("%i.", page); |
| 82 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 83 | path->append(new_extension); |
| 84 | return true; |
| 85 | } |
| 86 | return false; |
| 87 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 88 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 | */ |
| 96 | static bool make_output_filepath(SkString* path, const SkString& dir, |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 97 | const SkString& name, |
| 98 | int page) { |
scroggo@google.com | 5c7be95 | 2013-11-12 14:43:44 +0000 | [diff] [blame] | 99 | *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str()); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 100 | return add_page_and_replace_filename_extension(path, page, |
| 101 | PDF_FILE_EXTENSION, |
| 102 | PNG_FILE_EXTENSION); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 103 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 104 | |
edisonn@google.com | 598cf5d | 2013-10-09 15:13:19 +0000 | [diff] [blame] | 105 | 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] | 106 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 107 | |
| 108 | bitmap->allocPixels(); |
| 109 | bitmap->eraseColor(color); |
| 110 | } |
| 111 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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. |
edisonn@google.com | cdad30b | 2013-07-10 22:37:38 +0000 | [diff] [blame] | 116 | * @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] | 117 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 118 | |
edisonn@google.com | 1310238 | 2013-07-11 14:50:12 +0000 | [diff] [blame] | 119 | extern "C" SkBitmap* gDumpBitmap; |
| 120 | extern "C" SkCanvas* gDumpCanvas; |
| 121 | |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 122 | #if SK_SUPPORT_GPU |
| 123 | GrContextFactory gContextFactory; |
| 124 | #endif |
| 125 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 126 | static bool render_page(const SkString& outputDir, |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 127 | const SkString& inputFilename, |
| 128 | const SkPdfRenderer& renderer, |
| 129 | int page) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 130 | SkRect rect = renderer.MediaBox(page < 0 ? 0 :page); |
| 131 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 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; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 135 | SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap))); |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 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); |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 141 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 142 | SkBitmap bitmap; |
edisonn@google.com | 04068b1 | 2013-11-12 21:56:39 +0000 | [diff] [blame^] | 143 | SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(FLAGS_DPI / 72.0)); |
| 144 | SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(FLAGS_DPI / 72.0)); |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 145 | |
| 146 | rect = SkRect::MakeWH(width, height); |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 147 | |
edisonn@google.com | 598cf5d | 2013-10-09 15:13:19 +0000 | [diff] [blame] | 148 | SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT : SK_ColorWHITE; |
| 149 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 150 | #ifdef PDF_DEBUG_3X |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 151 | setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height), |
| 152 | background); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 153 | #else |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 154 | setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height), |
| 155 | background); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 156 | #endif |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 157 | SkAutoTUnref<SkBaseDevice> device; |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 158 | if (strcmp(FLAGS_config[0], "8888") == 0) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 159 | device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap))); |
edisonn@google.com | 768bc6a | 2013-08-08 12:42:13 +0000 | [diff] [blame] | 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 | } |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 187 | SkCanvas canvas(device); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 188 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 189 | gDumpBitmap = &bitmap; |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 190 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 191 | gDumpCanvas = &canvas; |
| 192 | renderer.renderPage(page < 0 ? 0 : page, &canvas, rect); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 193 | |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 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) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 201 | SkDebugf("Memory usage after page %i rendered: %u\n", |
| 202 | page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed()); |
edisonn@google.com | ac03d91 | 2013-07-22 15:36:39 +0000 | [diff] [blame] | 203 | } |
edisonn@google.com | 1310238 | 2013-07-11 14:50:12 +0000 | [diff] [blame] | 204 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 205 | return true; |
| 206 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 207 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 213 | static bool process_pdf(const SkString& inputPath, const SkString& outputDir, |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 214 | SkPdfRenderer& renderer) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 215 | SkDebugf("Loading PDF: %s\n", inputPath.c_str()); |
| 216 | |
scroggo@google.com | 5c7be95 | 2013-11-12 14:43:44 +0000 | [diff] [blame] | 217 | SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str()); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 218 | |
edisonn@google.com | 2273f9b | 2013-08-06 21:48:44 +0000 | [diff] [blame] | 219 | bool success = true; |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 220 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 221 | success = renderer.load(inputPath); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 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++) { |
edisonn@google.com | 2273f9b | 2013-08-06 21:48:44 +0000 | [diff] [blame] | 229 | success = renderer.load(inputPath) && success; |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 230 | if (FLAGS_showMemoryUsage) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 231 | SkDebugf("Memory usage after load %i number : %u\n", i, |
| 232 | (unsigned int)renderer.bytesUsed()); |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 236 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 237 | if (success) { |
| 238 | if (!renderer.pages()) |
| 239 | { |
| 240 | SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); |
| 241 | return false; |
| 242 | } else { |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 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) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 247 | success = render_page( |
| 248 | outputDir, |
| 249 | inputFilename, |
| 250 | renderer, |
| 251 | FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : |
| 252 | pn) && |
| 253 | success; |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 254 | } |
| 255 | } else if (strcmp(FLAGS_pages[0], "reverse") == 0) { |
| 256 | for (int pn = renderer.pages() - 1; pn >= 0; --pn) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 257 | success = render_page( |
| 258 | outputDir, |
| 259 | inputFilename, |
| 260 | renderer, |
| 261 | FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : |
| 262 | pn) && |
| 263 | success; |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 264 | } |
| 265 | } else if (strcmp(FLAGS_pages[0], "first") == 0) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 266 | success = render_page( |
| 267 | outputDir, |
| 268 | inputFilename, |
| 269 | renderer, |
| 270 | FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && |
| 271 | success; |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 272 | } else if (strcmp(FLAGS_pages[0], "last") == 0) { |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 273 | success = render_page( |
| 274 | outputDir, |
| 275 | inputFilename, |
| 276 | renderer, |
| 277 | FLAGS_noExtensionForOnePagePdf && |
| 278 | renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success; |
edisonn@google.com | 6a9d436 | 2013-07-11 16:25:51 +0000 | [diff] [blame] | 279 | } else { |
| 280 | int pn = atoi(FLAGS_pages[0]); |
edisonn@google.com | e50d9a1 | 2013-10-10 20:58:22 +0000 | [diff] [blame] | 281 | success = render_page(outputDir, inputFilename, renderer, |
| 282 | FLAGS_noExtensionForOnePagePdf && |
| 283 | renderer.pages() == 1 ? -1 : pn) && success; |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 284 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 288 | |
edisonn@google.com | 2273f9b | 2013-08-06 21:48:44 +0000 | [diff] [blame] | 289 | if (!success) { |
| 290 | SkDebugf("Failures for file %s\n", inputPath.c_str()); |
| 291 | } |
| 292 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 | */ |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 302 | static int process_input(const char* input, const SkString& outputDir, |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 303 | SkPdfRenderer& renderer) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 304 | int failures = 0; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 305 | if (sk_isdir(input)) { |
| 306 | SkOSFile::Iter iter(input, PDF_FILE_EXTENSION); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 307 | SkString inputFilename; |
| 308 | while (iter.next(&inputFilename)) { |
scroggo@google.com | 5c7be95 | 2013-11-12 14:43:44 +0000 | [diff] [blame] | 309 | SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str()); |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 310 | if (!process_pdf(inputPath, outputDir, renderer)) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 311 | ++failures; |
| 312 | } |
| 313 | } |
| 314 | } else { |
| 315 | SkString inputPath(input); |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 316 | if (!process_pdf(inputPath, outputDir, renderer)) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 317 | ++failures; |
| 318 | } |
| 319 | } |
| 320 | return failures; |
| 321 | } |
| 322 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 323 | int tool_main(int argc, char** argv); |
| 324 | int tool_main(int argc, char** argv) { |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 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 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 332 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 333 | SkPdfRenderer renderer; |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 334 | |
| 335 | SkString outputDir; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 336 | if (FLAGS_writePath.count() == 1) { |
| 337 | outputDir.set(FLAGS_writePath[0]); |
| 338 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 339 | |
| 340 | int failures = 0; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 341 | for (int i = 0; i < FLAGS_readPath.count(); i ++) { |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 342 | failures += process_input(FLAGS_readPath[i], outputDir, renderer); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 343 | renderer.unload(); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 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 |
| 357 | int main(int argc, char * const argv[]) { |
| 358 | return tool_main(argc, (char**) argv); |
| 359 | } |
| 360 | #endif |