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