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