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