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