2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkForceLinking.h"
11 #include "SkGraphics.h"
12 #include "SkImageEncoder.h"
14 #include "SkPicture.h"
15 #include "SkPixelRef.h"
18 #include "PdfRenderer.h"
19 #include "picture_utils.h"
21 __SK_FORCE_IMAGE_DECODER_LINKING;
24 #include "win_dbghelp.h"
30 * Given list of directories and files to use as input, expects to find .skp
31 * files and it will convert them to .pdf files writing them in the output
34 * Returns zero exit code if all .skp files were converted successfully,
35 * otherwise returns error code 1.
38 static const char PDF_FILE_EXTENSION[] = "pdf";
39 static const char SKP_FILE_EXTENSION[] = "skp";
41 static void usage(const char* argv0) {
42 SkDebugf("SKP to PDF rendering tool\n");
45 " %s <input>... [-w <outputDir>] [--jpegQuality N] \n"
49 " input: A list of directories and files to use as input. Files are\n"
50 " expected to have the .skp extension.\n\n");
52 " outputDir: directory to write the rendered pdfs.\n\n");
55 " jpegQuality N: encodes images in JPEG at quality level N, which can\n"
56 " be in range 0-100).\n"
57 " N = -1 will disable JPEG compression.\n"
58 " Default is N = 100, maximum quality.\n\n");
62 /** Replaces the extension of a file.
63 * @param path File name whose extension will be changed.
64 * @param old_extension The old extension.
65 * @param new_extension The new extension.
66 * @returns false if the file did not has the expected extension.
67 * if false is returned, contents of path are undefined.
69 static bool replace_filename_extension(SkString* path,
70 const char old_extension[],
71 const char new_extension[]) {
72 if (path->endsWith(old_extension)) {
73 path->remove(path->size() - strlen(old_extension),
74 strlen(old_extension));
75 if (!path->endsWith(".")) {
78 path->append(new_extension);
84 int gJpegQuality = 100;
85 // the size_t* parameter is deprecated, so we ignore it
86 static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) {
87 if (gJpegQuality == -1) {
92 #if defined(SK_BUILD_FOR_MAC)
93 // Workaround bug #1043 where bitmaps with referenced pixels cause
94 // CGImageDestinationFinalize to crash
96 bitmap.deepCopyTo(©);
100 return SkImageEncoder::EncodeData(bm,
101 SkImageEncoder::kJPEG_Type,
105 /** Builds the output filename. path = dir/name, and it replaces expected
106 * .skp extension with .pdf extention.
107 * @param path Output filename.
108 * @param name The name of the file.
109 * @returns false if the file did not has the expected extension.
110 * if false is returned, contents of path are undefined.
112 static bool make_output_filepath(SkString* path, const SkString& dir,
113 const SkString& name) {
114 *path = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
115 return replace_filename_extension(path,
120 /** Write the output of pdf renderer to a file.
121 * @param outputDir Output dir.
122 * @param inputFilename The skp file that was read.
123 * @param renderer The object responsible to write the pdf file.
125 static SkWStream* open_stream(const SkString& outputDir,
126 const SkString& inputFilename) {
127 if (outputDir.isEmpty()) {
128 return SkNEW(SkDynamicMemoryWStream);
132 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
136 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (outputPath.c_str()));
137 if (!stream->isValid()) {
138 SkDebugf("Could not write to file %s\n", outputPath.c_str());
145 /** Reads an skp file, renders it to pdf and writes the output to a pdf file
146 * @param inputPath The skp file to be read.
147 * @param outputDir Output dir.
148 * @param renderer The object responsible to render the skp object into pdf.
150 static bool render_pdf(const SkString& inputPath, const SkString& outputDir,
151 sk_tools::PdfRenderer& renderer) {
152 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
154 SkFILEStream inputStream;
155 inputStream.setPath(inputPath.c_str());
156 if (!inputStream.isValid()) {
157 SkDebugf("Could not open file %s\n", inputPath.c_str());
161 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream));
163 if (NULL == picture.get()) {
164 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
168 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(),
171 SkWStream* stream(open_stream(outputDir, inputFilename));
177 renderer.init(picture, stream);
179 bool success = renderer.render();
187 /** For each file in the directory or for the file passed in input, call
189 * @param input A directory or an skp file.
190 * @param outputDir Output dir.
191 * @param renderer The object responsible to render the skp object into pdf.
193 static int process_input(const SkString& input, const SkString& outputDir,
194 sk_tools::PdfRenderer& renderer) {
196 if (sk_isdir(input.c_str())) {
197 SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION);
198 SkString inputFilename;
199 while (iter.next(&inputFilename)) {
200 SkString inputPath = SkOSPath::SkPathJoin(input.c_str(), inputFilename.c_str());
201 if (!render_pdf(inputPath, outputDir, renderer)) {
206 SkString inputPath(input);
207 if (!render_pdf(inputPath, outputDir, renderer)) {
214 static void parse_commandline(int argc, char* const argv[],
215 SkTArray<SkString>* inputs,
216 SkString* outputDir) {
217 const char* argv0 = argv[0];
218 char* const* stop = argv + argc;
220 for (++argv; argv < stop; ++argv) {
221 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
224 } else if (0 == strcmp(*argv, "-w")) {
227 SkDebugf("Missing outputDir for -w\n");
231 *outputDir = SkString(*argv);
232 } else if (0 == strcmp(*argv, "--jpegQuality")) {
235 SkDebugf("Missing argument for --jpegQuality\n");
239 gJpegQuality = atoi(*argv);
240 if (gJpegQuality < -1 || gJpegQuality > 100) {
241 SkDebugf("Invalid argument for --jpegQuality\n");
246 inputs->push_back(SkString(*argv));
250 if (inputs->count() < 1) {
256 int tool_main_core(int argc, char** argv);
257 int tool_main_core(int argc, char** argv) {
259 SkTArray<SkString> inputs;
261 SkAutoTUnref<sk_tools::PdfRenderer>
262 renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data)));
263 SkASSERT(renderer.get());
266 parse_commandline(argc, argv, &inputs, &outputDir);
269 for (int i = 0; i < inputs.count(); i ++) {
270 failures += process_input(inputs[i], outputDir, *renderer);
274 SkDebugf("Failed to render %i PDFs.\n", failures);
281 int tool_main(int argc, char** argv);
282 int tool_main(int argc, char** argv) {
284 setUpDebuggingFromArgs(argv[0]);
287 return tool_main_core(argc, argv);
290 __except(GenerateDumpAndPrintCallstack(GetExceptionInformation()))
298 #if !defined SK_BUILD_FOR_IOS
299 int main(int argc, char * const argv[]) {
300 return tool_main(argc, (char**) argv);