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 "SkOSFile.h" 12 #include "SkPicture.h" 13 #include "SkStream.h" 14 #include "SkTArray.h" 15 #include "PdfRenderer.h" 16 #include "picture_utils.h" 17 18 /** 19 * render_pdfs 20 * 21 * Given list of directories and files to use as input, expects to find .skp 22 * files and it will convert them to .pdf files writing them in the output 23 * directory. 24 * 25 * Returns zero exit code if all .skp files were converted successfully, 26 * otherwise returns error code 1. 27 */ 28 29 static const char PDF_FILE_EXTENSION[] = "pdf"; 30 static const char SKP_FILE_EXTENSION[] = "skp"; 31 32 static void usage(const char* argv0) { 33 SkDebugf("SKP to PDF rendering tool\n"); 34 SkDebugf("\n" 35 "Usage: \n" 36 " %s <input>... -w <outputDir> \n" 37 , argv0); 38 SkDebugf("\n\n"); 39 SkDebugf( 40 " input: A list of directories and files to use as input. Files are\n" 41 " expected to have the .skp extension.\n\n"); 42 SkDebugf( 43 " outputDir: directory to write the rendered pdfs.\n\n"); 44 SkDebugf("\n"); 45 } 46 47 /** Replaces the extension of a file. 48 * @param path File name whose extension will be changed. 49 * @param old_extension The old extension. 50 * @param new_extension The new extension. 51 * @returns false if the file did not has the expected extension. 52 * if false is returned, contents of path are undefined. 53 */ 54 static bool replace_filename_extension(SkString* path, 55 const char old_extension[], 56 const char new_extension[]) { 57 if (path->endsWith(old_extension)) { 58 path->remove(path->size() - strlen(old_extension), 59 strlen(old_extension)); 60 if (!path->endsWith(".")) { 61 return false; 62 } 63 path->append(new_extension); 64 return true; 65 } 66 return false; 67 } 68 69 /** Builds the output filename. path = dir/name, and it replaces expected 70 * .skp extension with .pdf extention. 71 * @param path Output filename. 72 * @param name The name of the file. 73 * @returns false if the file did not has the expected extension. 74 * if false is returned, contents of path are undefined. 75 */ 76 static bool make_output_filepath(SkString* path, const SkString& dir, 77 const SkString& name) { 78 sk_tools::make_filepath(path, dir, name); 79 return replace_filename_extension(path, 80 SKP_FILE_EXTENSION, 81 PDF_FILE_EXTENSION); 82 } 83 84 /** Write the output of pdf renderer to a file. 85 * @param outputDir Output dir. 86 * @param inputFilename The skp file that was read. 87 * @param renderer The object responsible to write the pdf file. 88 */ 89 static bool write_output(const SkString& outputDir, 90 const SkString& inputFilename, 91 const sk_tools::PdfRenderer& renderer) { 92 if (outputDir.isEmpty()) { 93 SkDynamicMemoryWStream stream; 94 renderer.write(&stream); 95 return true; 96 } 97 98 SkString outputPath; 99 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) { 100 return false; 101 } 102 103 SkFILEWStream stream(outputPath.c_str()); 104 if (!stream.isValid()) { 105 SkDebugf("Could not write to file %s\n", outputPath.c_str()); 106 return false; 107 } 108 renderer.write(&stream); 109 110 return true; 111 } 112 113 /** Reads an skp file, renders it to pdf and writes the output to a pdf file 114 * @param inputPath The skp file to be read. 115 * @param outputDir Output dir. 116 * @param renderer The object responsible to render the skp object into pdf. 117 */ 118 static bool render_pdf(const SkString& inputPath, const SkString& outputDir, 119 sk_tools::PdfRenderer& renderer) { 120 SkString inputFilename; 121 sk_tools::get_basename(&inputFilename, inputPath); 122 123 SkFILEStream inputStream; 124 inputStream.setPath(inputPath.c_str()); 125 if (!inputStream.isValid()) { 126 SkDebugf("Could not open file %s\n", inputPath.c_str()); 127 return false; 128 } 129 130 bool success = false; 131 SkAutoTUnref<SkPicture> 132 picture(SkNEW_ARGS(SkPicture, (&inputStream, &success))); 133 134 if (!success) { 135 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); 136 return false; 137 } 138 139 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(), 140 inputPath.c_str()); 141 142 renderer.init(picture); 143 144 renderer.render(); 145 146 success = write_output(outputDir, inputFilename, renderer); 147 148 renderer.end(); 149 return success; 150 } 151 152 /** For each file in the directory or for the file passed in input, call 153 * render_pdf. 154 * @param input A directory or an skp file. 155 * @param outputDir Output dir. 156 * @param renderer The object responsible to render the skp object into pdf. 157 */ 158 static int process_input(const SkString& input, const SkString& outputDir, 159 sk_tools::PdfRenderer& renderer) { 160 int failures = 0; 161 if (sk_isdir(input.c_str())) { 162 SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION); 163 SkString inputFilename; 164 while (iter.next(&inputFilename)) { 165 SkString inputPath; 166 sk_tools::make_filepath(&inputPath, input, inputFilename); 167 if (!render_pdf(inputPath, outputDir, renderer)) { 168 ++failures; 169 } 170 } 171 } else { 172 SkString inputPath(input); 173 if (!render_pdf(inputPath, outputDir, renderer)) { 174 ++failures; 175 } 176 } 177 return failures; 178 } 179 180 static void parse_commandline(int argc, char* const argv[], 181 SkTArray<SkString>* inputs, 182 SkString* outputDir) { 183 const char* argv0 = argv[0]; 184 char* const* stop = argv + argc; 185 186 for (++argv; argv < stop; ++argv) { 187 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) { 188 usage(argv0); 189 exit(-1); 190 } else if (0 == strcmp(*argv, "-w")) { 191 ++argv; 192 if (argv >= stop) { 193 SkDebugf("Missing outputDir for -w\n"); 194 usage(argv0); 195 exit(-1); 196 } 197 *outputDir = SkString(*argv); 198 } else { 199 inputs->push_back(SkString(*argv)); 200 } 201 } 202 203 if (inputs->count() < 1) { 204 usage(argv0); 205 exit(-1); 206 } 207 } 208 209 int tool_main(int argc, char** argv); 210 int tool_main(int argc, char** argv) { 211 212 SkAutoGraphics ag; 213 SkTArray<SkString> inputs; 214 215 SkAutoTUnref<sk_tools::PdfRenderer> 216 renderer(SkNEW(sk_tools::SimplePdfRenderer)); 217 SkASSERT(renderer.get()); 218 219 SkString outputDir; 220 parse_commandline(argc, argv, &inputs, &outputDir); 221 222 int failures = 0; 223 for (int i = 0; i < inputs.count(); i ++) { 224 failures += process_input(inputs[i], outputDir, *renderer); 225 } 226 227 if (failures != 0) { 228 SkDebugf("Failed to render %i PDFs.\n", failures); 229 return 1; 230 } 231 return 0; 232 } 233 234 #if !defined SK_BUILD_FOR_IOS 235 int main(int argc, char * const argv[]) { 236 return tool_main(argc, (char**) argv); 237 } 238 #endif 239