render_pdfs_main.cpp revision 7839ce1af63bf12fe7b3caa866970bbbb3afb13d
1/* 2 * Copyright 2012 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 8#include "SkCanvas.h" 9#include "SkDevice.h" 10#include "SkGraphics.h" 11#include "SkImageDecoder.h" 12#include "SkImageEncoder.h" 13#include "SkOSFile.h" 14#include "SkPicture.h" 15#include "SkStream.h" 16#include "SkTArray.h" 17#include "PdfRenderer.h" 18#include "picture_utils.h" 19 20#ifdef SK_USE_CDB 21#include "win_dbghelp.h" 22#endif 23 24/** 25 * render_pdfs 26 * 27 * Given list of directories and files to use as input, expects to find .skp 28 * files and it will convert them to .pdf files writing them in the output 29 * directory. 30 * 31 * Returns zero exit code if all .skp files were converted successfully, 32 * otherwise returns error code 1. 33 */ 34 35static const char PDF_FILE_EXTENSION[] = "pdf"; 36static const char SKP_FILE_EXTENSION[] = "skp"; 37 38static void usage(const char* argv0) { 39 SkDebugf("SKP to PDF rendering tool\n"); 40 SkDebugf("\n" 41"Usage: \n" 42" %s <input>... [-w <outputDir>] [--jpegQuality N] \n" 43, argv0); 44 SkDebugf("\n\n"); 45 SkDebugf( 46" input: A list of directories and files to use as input. Files are\n" 47" expected to have the .skp extension.\n\n"); 48 SkDebugf( 49" outputDir: directory to write the rendered pdfs.\n\n"); 50 SkDebugf("\n"); 51 SkDebugf( 52" jpegQuality N: encodes images in JPEG at quality level N, which can\n" 53" be in range 0-100).\n" 54" N = -1 will disable JPEG compression.\n" 55" Default is N = 100, maximum quality.\n\n"); 56 SkDebugf("\n"); 57} 58 59/** Replaces the extension of a file. 60 * @param path File name whose extension will be changed. 61 * @param old_extension The old extension. 62 * @param new_extension The new extension. 63 * @returns false if the file did not has the expected extension. 64 * if false is returned, contents of path are undefined. 65 */ 66static bool replace_filename_extension(SkString* path, 67 const char old_extension[], 68 const char new_extension[]) { 69 if (path->endsWith(old_extension)) { 70 path->remove(path->size() - strlen(old_extension), 71 strlen(old_extension)); 72 if (!path->endsWith(".")) { 73 return false; 74 } 75 path->append(new_extension); 76 return true; 77 } 78 return false; 79} 80 81int gJpegQuality = 100; 82static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect) { 83 if (gJpegQuality == -1) return false; 84 85 SkIRect bitmapBounds; 86 SkBitmap subset; 87 const SkBitmap* bitmapToUse = &bitmap; 88 bitmap.getBounds(&bitmapBounds); 89 if (rect != bitmapBounds) { 90 SkAssertResult(bitmap.extractSubset(&subset, rect)); 91 bitmapToUse = ⊂ 92 } 93 94#if defined(SK_BUILD_FOR_MAC) 95 // Workaround bug #1043 where bitmaps with referenced pixels cause 96 // CGImageDestinationFinalize to crash 97 SkBitmap copy; 98 bitmapToUse->deepCopyTo(©, bitmapToUse->config()); 99 bitmapToUse = © 100#endif 101 102 return SkImageEncoder::EncodeStream(stream, 103 *bitmapToUse, 104 SkImageEncoder::kJPEG_Type, 105 gJpegQuality); 106} 107 108/** Builds the output filename. path = dir/name, and it replaces expected 109 * .skp extension with .pdf extention. 110 * @param path Output filename. 111 * @param name The name of the file. 112 * @returns false if the file did not has the expected extension. 113 * if false is returned, contents of path are undefined. 114 */ 115static bool make_output_filepath(SkString* path, const SkString& dir, 116 const SkString& name) { 117 sk_tools::make_filepath(path, dir, name); 118 return replace_filename_extension(path, 119 SKP_FILE_EXTENSION, 120 PDF_FILE_EXTENSION); 121} 122 123/** Write the output of pdf renderer to a file. 124 * @param outputDir Output dir. 125 * @param inputFilename The skp file that was read. 126 * @param renderer The object responsible to write the pdf file. 127 */ 128static bool write_output(const SkString& outputDir, 129 const SkString& inputFilename, 130 const sk_tools::PdfRenderer& renderer) { 131 if (outputDir.isEmpty()) { 132 SkDynamicMemoryWStream stream; 133 renderer.write(&stream); 134 return true; 135 } 136 137 SkString outputPath; 138 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) { 139 return false; 140 } 141 142 SkFILEWStream stream(outputPath.c_str()); 143 if (!stream.isValid()) { 144 SkDebugf("Could not write to file %s\n", outputPath.c_str()); 145 return false; 146 } 147 renderer.write(&stream); 148 149 return true; 150} 151 152/** Reads an skp file, renders it to pdf and writes the output to a pdf file 153 * @param inputPath The skp file to be read. 154 * @param outputDir Output dir. 155 * @param renderer The object responsible to render the skp object into pdf. 156 */ 157static bool render_pdf(const SkString& inputPath, const SkString& outputDir, 158 sk_tools::PdfRenderer& renderer) { 159 SkString inputFilename; 160 sk_tools::get_basename(&inputFilename, inputPath); 161 162 SkFILEStream inputStream; 163 inputStream.setPath(inputPath.c_str()); 164 if (!inputStream.isValid()) { 165 SkDebugf("Could not open file %s\n", inputPath.c_str()); 166 return false; 167 } 168 169 bool success = false; 170 SkAutoTUnref<SkPicture> 171 picture(SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory))); 172 173 if (!success) { 174 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); 175 return false; 176 } 177 178 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(), 179 inputPath.c_str()); 180 181 renderer.init(picture); 182 183 renderer.render(); 184 185 success = write_output(outputDir, inputFilename, renderer); 186 187 renderer.end(); 188 return success; 189} 190 191/** For each file in the directory or for the file passed in input, call 192 * render_pdf. 193 * @param input A directory or an skp file. 194 * @param outputDir Output dir. 195 * @param renderer The object responsible to render the skp object into pdf. 196 */ 197static int process_input(const SkString& input, const SkString& outputDir, 198 sk_tools::PdfRenderer& renderer) { 199 int failures = 0; 200 if (sk_isdir(input.c_str())) { 201 SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION); 202 SkString inputFilename; 203 while (iter.next(&inputFilename)) { 204 SkString inputPath; 205 sk_tools::make_filepath(&inputPath, input, inputFilename); 206 if (!render_pdf(inputPath, outputDir, renderer)) { 207 ++failures; 208 } 209 } 210 } else { 211 SkString inputPath(input); 212 if (!render_pdf(inputPath, outputDir, renderer)) { 213 ++failures; 214 } 215 } 216 return failures; 217} 218 219static void parse_commandline(int argc, char* const argv[], 220 SkTArray<SkString>* inputs, 221 SkString* outputDir) { 222 const char* argv0 = argv[0]; 223 char* const* stop = argv + argc; 224 225 for (++argv; argv < stop; ++argv) { 226 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) { 227 usage(argv0); 228 exit(-1); 229 } else if (0 == strcmp(*argv, "-w")) { 230 ++argv; 231 if (argv >= stop) { 232 SkDebugf("Missing outputDir for -w\n"); 233 usage(argv0); 234 exit(-1); 235 } 236 *outputDir = SkString(*argv); 237 } else if (0 == strcmp(*argv, "--jpegQuality")) { 238 ++argv; 239 if (argv >= stop) { 240 SkDebugf("Missing argument for --jpegQuality\n"); 241 usage(argv0); 242 exit(-1); 243 } 244 gJpegQuality = atoi(*argv); 245 if (gJpegQuality < -1 || gJpegQuality > 100) { 246 SkDebugf("Invalid argument for --jpegQuality\n"); 247 usage(argv0); 248 exit(-1); 249 } 250 } else { 251 inputs->push_back(SkString(*argv)); 252 } 253 } 254 255 if (inputs->count() < 1) { 256 usage(argv0); 257 exit(-1); 258 } 259} 260 261int tool_main_core(int argc, char** argv); 262int tool_main_core(int argc, char** argv) { 263 SkAutoGraphics ag; 264 SkTArray<SkString> inputs; 265 266 SkAutoTUnref<sk_tools::PdfRenderer> 267 renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_stream))); 268 SkASSERT(renderer.get()); 269 270 SkString outputDir; 271 parse_commandline(argc, argv, &inputs, &outputDir); 272 273 int failures = 0; 274 for (int i = 0; i < inputs.count(); i ++) { 275 failures += process_input(inputs[i], outputDir, *renderer); 276 } 277 278 if (failures != 0) { 279 SkDebugf("Failed to render %i PDFs.\n", failures); 280 return 1; 281 } 282 283 return 0; 284} 285 286int tool_main(int argc, char** argv); 287int tool_main(int argc, char** argv) { 288#ifdef SK_USE_CDB 289 setUpDebuggingFromArgs(argv[0]); 290 __try { 291#endif 292 return tool_main_core(argc, argv); 293#ifdef SK_USE_CDB 294 } 295 __except(GenerateDumpAndPrintCallstack(GetExceptionInformation())) 296 { 297 return -1; 298 } 299#endif 300 return 0; 301} 302 303#if !defined SK_BUILD_FOR_IOS 304int main(int argc, char * const argv[]) { 305 return tool_main(argc, (char**) argv); 306} 307#endif 308