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" |
| 12 | #include "picture_utils.h" |
| 13 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 14 | #include "SkPdfRenderer.h" |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 15 | |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 16 | DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process."); |
| 17 | DEFINE_string2(writePath, w, "", "Directory to write the rendered pages."); |
| 18 | DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page."); |
| 19 | DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage."); |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 20 | DEFINE_string2(pages, p, "all", "What pages to render and how:\n" |
| 21 | "\tall - all pages\n" |
| 22 | "\treverse - all pages, in reverse order\n" |
| 23 | "\tfirst - first page\n" |
| 24 | "\tlast - last page\n" |
| 25 | "\tnumber - a specific page number\n" |
| 26 | ); |
edisonn@google.com | 15b1118 | 2013-07-11 14:43:15 +0000 | [diff] [blame] | 27 | DEFINE_double(DPI, 72, "DPI to be used for rendering (scale)."); |
| 28 | // TODO(edisonn): add config for device target(gpu, raster, pdf), + ability not to render at all |
| 29 | // TODO(edisonn): add ability to do the op N times, bench (either load N times, render n times or load + render n times) |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 30 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Given list of directories and files to use as input, expects to find .pdf |
| 33 | * files and it will convert them to .png files writing them in the same directory |
| 34 | * one file for each page. |
| 35 | * |
| 36 | * Returns zero exit code if all .pdf files were converted successfully, |
| 37 | * otherwise returns error code 1. |
| 38 | */ |
| 39 | |
| 40 | static const char PDF_FILE_EXTENSION[] = "pdf"; |
| 41 | static const char PNG_FILE_EXTENSION[] = "png"; |
| 42 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 43 | /** Replaces the extension of a file. |
| 44 | * @param path File name whose extension will be changed. |
| 45 | * @param old_extension The old extension. |
| 46 | * @param new_extension The new extension. |
| 47 | * @returns false if the file did not has the expected extension. |
| 48 | * if false is returned, contents of path are undefined. |
| 49 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 50 | 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] | 51 | const char old_extension[], |
| 52 | const char new_extension[]) { |
| 53 | if (path->endsWith(old_extension)) { |
| 54 | path->remove(path->size() - strlen(old_extension), |
| 55 | strlen(old_extension)); |
| 56 | if (!path->endsWith(".")) { |
| 57 | return false; |
| 58 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 59 | if (page >= 0) { |
| 60 | path->appendf("%i.", page); |
| 61 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 62 | path->append(new_extension); |
| 63 | return true; |
| 64 | } |
| 65 | return false; |
| 66 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 67 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 68 | /** Builds the output filename. path = dir/name, and it replaces expected |
| 69 | * .skp extension with .pdf extention. |
| 70 | * @param path Output filename. |
| 71 | * @param name The name of the file. |
| 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 | 596d2e2 | 2013-07-10 17:44:55 +0000 | [diff] [blame] | 75 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 76 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 77 | static bool make_output_filepath(SkString* path, const SkString& dir, |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 78 | const SkString& name, |
| 79 | int page) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 80 | sk_tools::make_filepath(path, dir, name); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 81 | return add_page_and_replace_filename_extension(path, page, |
| 82 | PDF_FILE_EXTENSION, |
| 83 | PNG_FILE_EXTENSION); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 84 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 85 | |
| 86 | static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) { |
| 87 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 88 | |
| 89 | bitmap->allocPixels(); |
| 90 | bitmap->eraseColor(color); |
| 91 | } |
| 92 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 93 | /** Write the output of pdf renderer to a file. |
| 94 | * @param outputDir Output dir. |
| 95 | * @param inputFilename The skp file that was read. |
| 96 | * @param renderer The object responsible to write the pdf file. |
edisonn@google.com | cdad30b | 2013-07-10 22:37:38 +0000 | [diff] [blame] | 97 | * @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] | 98 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 99 | |
edisonn@google.com | 1310238 | 2013-07-11 14:50:12 +0000 | [diff] [blame] | 100 | extern "C" SkBitmap* gDumpBitmap; |
| 101 | extern "C" SkCanvas* gDumpCanvas; |
| 102 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 103 | static bool render_page(const SkString& outputDir, |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame^] | 104 | const SkString& inputFilename, |
| 105 | const SkPdfRenderer& renderer, |
| 106 | int page) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 107 | SkRect rect = renderer.MediaBox(page < 0 ? 0 :page); |
| 108 | |
| 109 | SkBitmap bitmap; |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame^] | 110 | SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0))); |
| 111 | SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0))); |
| 112 | |
| 113 | rect = SkRect::MakeWH(width, height); |
| 114 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 115 | #ifdef PDF_DEBUG_3X |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame^] | 116 | setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height)); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 117 | #else |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame^] | 118 | setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height)); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 119 | #endif |
| 120 | SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap))); |
| 121 | SkCanvas canvas(device); |
| 122 | |
| 123 | gDumpBitmap = &bitmap; |
| 124 | |
| 125 | gDumpCanvas = &canvas; |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame^] | 126 | renderer.renderPage(page < 0 ? 0 : page, &canvas, rect); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 127 | |
edisonn@google.com | 1310238 | 2013-07-11 14:50:12 +0000 | [diff] [blame] | 128 | SkString outputPath; |
| 129 | if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) { |
| 130 | return false; |
| 131 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 132 | SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 133 | |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 134 | if (FLAGS_showMemoryUsage) { |
| 135 | SkDebugf("Memory usage after page %i rendered: %u\n", page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed()); |
| 136 | } |
| 137 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 138 | return true; |
| 139 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 140 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 141 | /** Reads an skp file, renders it to pdf and writes the output to a pdf file |
| 142 | * @param inputPath The skp file to be read. |
| 143 | * @param outputDir Output dir. |
| 144 | * @param renderer The object responsible to render the skp object into pdf. |
| 145 | */ |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 146 | static bool process_pdf(const SkString& inputPath, const SkString& outputDir, |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 147 | SkPdfRenderer& renderer) { |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 148 | SkDebugf("Loading PDF: %s\n", inputPath.c_str()); |
| 149 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 150 | SkString inputFilename; |
| 151 | sk_tools::get_basename(&inputFilename, inputPath); |
| 152 | |
| 153 | SkFILEStream inputStream; |
| 154 | inputStream.setPath(inputPath.c_str()); |
| 155 | if (!inputStream.isValid()) { |
| 156 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | bool success = false; |
| 161 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 162 | success = renderer.load(inputPath); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 163 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 164 | if (success) { |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 165 | if (FLAGS_showMemoryUsage) { |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 166 | SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed()); |
| 167 | } |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 168 | if (!renderer.pages()) |
| 169 | { |
| 170 | SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); |
| 171 | return false; |
| 172 | } else { |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 173 | if (strcmp(FLAGS_pages[0], "all") == 0) { |
| 174 | for (int pn = 0; pn < renderer.pages(); ++pn) { |
| 175 | success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success; |
| 176 | } |
| 177 | } else if (strcmp(FLAGS_pages[0], "reverse") == 0) { |
| 178 | for (int pn = renderer.pages() - 1; pn >= 0; --pn) { |
| 179 | success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success; |
| 180 | } |
| 181 | } else if (strcmp(FLAGS_pages[0], "first") == 0) { |
| 182 | success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success; |
| 183 | } else if (strcmp(FLAGS_pages[0], "last") == 0) { |
| 184 | success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success; |
| 185 | } else { |
| 186 | int pn = atoi(FLAGS_pages[0]); |
| 187 | success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && pn; |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 191 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 192 | return success; |
| 193 | } |
| 194 | |
| 195 | /** For each file in the directory or for the file passed in input, call |
| 196 | * parse_pdf. |
| 197 | * @param input A directory or an pdf file. |
| 198 | * @param outputDir Output dir. |
| 199 | * @param renderer The object responsible to render the skp object into pdf. |
| 200 | */ |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 201 | static int process_input(const char* input, const SkString& outputDir, |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 202 | SkPdfRenderer& renderer) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 203 | int failures = 0; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 204 | if (sk_isdir(input)) { |
| 205 | SkOSFile::Iter iter(input, PDF_FILE_EXTENSION); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 206 | SkString inputFilename; |
| 207 | while (iter.next(&inputFilename)) { |
| 208 | SkString inputPath; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 209 | SkString _input; |
| 210 | _input.append(input); |
| 211 | sk_tools::make_filepath(&inputPath, _input, inputFilename); |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 212 | if (!process_pdf(inputPath, outputDir, renderer)) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 213 | ++failures; |
| 214 | } |
| 215 | } |
| 216 | } else { |
| 217 | SkString inputPath(input); |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 218 | if (!process_pdf(inputPath, outputDir, renderer)) { |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 219 | ++failures; |
| 220 | } |
| 221 | } |
| 222 | return failures; |
| 223 | } |
| 224 | |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 225 | int tool_main(int argc, char** argv); |
| 226 | int tool_main(int argc, char** argv) { |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 227 | SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer)."); |
| 228 | SkCommandLineFlags::Parse(argc, argv); |
| 229 | |
| 230 | if (FLAGS_readPath.isEmpty()) { |
| 231 | SkDebugf(".pdf files or directories are required.\n"); |
| 232 | exit(-1); |
| 233 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 234 | |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 235 | SkPdfRenderer renderer; |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 236 | |
| 237 | SkString outputDir; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 238 | if (FLAGS_writePath.count() == 1) { |
| 239 | outputDir.set(FLAGS_writePath[0]); |
| 240 | } |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 241 | |
| 242 | int failures = 0; |
edisonn@google.com | a5aaa79 | 2013-07-11 12:27:21 +0000 | [diff] [blame] | 243 | for (int i = 0; i < FLAGS_readPath.count(); i ++) { |
edisonn@google.com | 7b328fd | 2013-07-11 12:53:06 +0000 | [diff] [blame] | 244 | failures += process_input(FLAGS_readPath[i], outputDir, renderer); |
edisonn@google.com | 222382b | 2013-07-10 22:33:10 +0000 | [diff] [blame] | 245 | renderer.unload(); |
edisonn@google.com | 01cd4d5 | 2013-06-10 20:44:45 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | reportPdfRenderStats(); |
| 249 | |
| 250 | if (failures != 0) { |
| 251 | SkDebugf("Failed to render %i PDFs.\n", failures); |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | #if !defined SK_BUILD_FOR_IOS |
| 259 | int main(int argc, char * const argv[]) { |
| 260 | return tool_main(argc, (char**) argv); |
| 261 | } |
| 262 | #endif |