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.
9 #include "SkCommandLineFlags.h"
10 #include "SkDocument.h"
11 #include "SkForceLinking.h"
12 #include "SkGraphics.h"
13 #include "SkImageEncoder.h"
15 #include "SkPicture.h"
19 #include "ProcStats.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";
42 DEFINE_string2(inputPaths, r, "",
43 "A list of directories and files to use as input. "
44 "Files are expected to have the .skp extension.");
46 DEFINE_string2(outputDir, w, "",
47 "Directory to write the rendered pdfs.");
49 DEFINE_string2(match, m, "",
50 "[~][^]substring[$] [...] of filenames to run.\n"
51 "Multiple matches may be separated by spaces.\n"
52 "~ causes a matching file to always be skipped\n"
53 "^ requires the start of the file to match\n"
54 "$ requires the end of the file to match\n"
55 "^ and $ requires an exact match\n"
56 "If a file does not match any list entry,\n"
57 "it is skipped unless some list entry starts with ~");
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.
66 static 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(".")) {
75 path->append(new_extension);
81 /** Builds the output filename. path = dir/name, and it replaces expected
82 * .skp extension with .pdf extention.
83 * @param path Output filename.
84 * @param name The name of the file.
85 * @returns false if the file did not has the expected extension.
86 * if false is returned, contents of path are undefined.
88 static bool make_output_filepath(SkString* path, const SkString& dir,
89 const SkString& name) {
90 *path = SkOSPath::Join(dir.c_str(), name.c_str());
91 return replace_filename_extension(path,
97 // This is a write-only stream.
98 class NullWStream : public SkWStream {
100 NullWStream() : fBytesWritten(0) { }
101 bool write(const void*, size_t size) SK_OVERRIDE {
102 fBytesWritten += size;
105 size_t bytesWritten() const SK_OVERRIDE { return fBytesWritten; }
106 size_t fBytesWritten;
110 /** Write the output of pdf renderer to a file.
111 * @param outputDir Output dir.
112 * @param inputFilename The skp file that was read.
113 * @param renderer The object responsible to write the pdf file.
115 static SkWStream* open_stream(const SkString& outputDir,
116 const SkString& inputFilename) {
117 if (outputDir.isEmpty()) {
118 return SkNEW(NullWStream);
122 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
126 SkAutoTDelete<SkFILEWStream> stream(
127 SkNEW_ARGS(SkFILEWStream, (outputPath.c_str())));
128 if (!stream.get() || !stream->isValid()) {
129 SkDebugf("Could not write to file %s\n", outputPath.c_str());
133 return stream.detach();
137 * Given a SkPicture, write a one-page PDF document to the given
138 * output, using the provided encoder.
140 static bool pdf_to_stream(SkPicture* picture,
142 SkAutoTUnref<SkDocument> pdfDocument(
143 SkDocument::CreatePDF(output));
144 SkCanvas* canvas = pdfDocument->beginPage(picture->cullRect().width(),
145 picture->cullRect().height());
146 canvas->drawPicture(picture);
148 return pdfDocument->close();
151 static bool operator<(const SkString& a, const SkString& b) {
152 return strcmp(a.c_str(), b.c_str()) < 0;
156 * @param A list of directories or a skp files.
157 * @returns an alphabetical list of skp files.
159 static void process_input_files(
160 const SkCommandLineFlags::StringArray& inputs,
161 SkTArray<SkString>* files) {
162 for (int i = 0; i < inputs.count(); i ++) {
163 const char* input = inputs[i];
164 if (sk_isdir(input)) {
165 SkOSFile::Iter iter(input, SKP_FILE_EXTENSION);
166 SkString inputFilename;
167 while (iter.next(&inputFilename)) {
168 if (!SkCommandLineFlags::ShouldSkip(
169 FLAGS_match, inputFilename.c_str())) {
171 SkOSPath::Join(input, inputFilename.c_str()));
175 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, input)) {
176 files->push_back(SkString(input));
180 if (files->count() > 0) {
181 SkTQSort<SkString>(files->begin(), files->end() - 1);
185 /** For each input skp file, read it, render it to pdf and write. the
186 * output to a pdf file
188 int tool_main_core(int argc, char** argv);
189 int tool_main_core(int argc, char** argv) {
190 SkCommandLineFlags::Parse(argc, argv);
195 if (FLAGS_outputDir.count() > 0) {
196 outputDir = FLAGS_outputDir[0];
197 if (!sk_mkdir(outputDir.c_str())) {
198 SkDebugf("Unable to mkdir '%s'\n", outputDir.c_str());
203 SkTArray<SkString> files;
204 process_input_files(FLAGS_inputPaths, &files);
206 size_t maximumPathLength = 0;
207 for (int i = 0; i < files.count(); i ++) {
208 SkString basename = SkOSPath::Basename(files[i].c_str());
209 maximumPathLength = SkTMax(maximumPathLength, basename.size());
213 for (int i = 0; i < files.count(); i ++) {
214 SkString basename = SkOSPath::Basename(files[i].c_str());
216 SkFILEStream inputStream;
217 inputStream.setPath(files[i].c_str());
218 if (!inputStream.isValid()) {
219 SkDebugf("Could not open file %s\n", files[i].c_str());
224 SkAutoTUnref<SkPicture> picture(
225 SkPicture::CreateFromStream(&inputStream));
226 if (NULL == picture.get()) {
227 SkDebugf("Could not read an SkPicture from %s\n",
232 SkDebugf("[%6g %6g %6g %6g] %-*s",
233 picture->cullRect().fLeft, picture->cullRect().fTop,
234 picture->cullRect().fRight, picture->cullRect().fBottom,
235 maximumPathLength, basename.c_str());
237 SkAutoTDelete<SkWStream> stream(open_stream(outputDir, files[i]));
242 if (!pdf_to_stream(picture, stream.get())) {
243 SkDebugf("Error in PDF Serialization.");
247 int max_rss_mb = sk_tools::getMaxResidentSetSizeMB();
248 if (max_rss_mb >= 0) {
249 SkDebugf(" %4dM peak rss", max_rss_mb);
255 SkDebugf("Failed to render %i of %i PDFs.\n", failures, files.count());
262 int tool_main(int argc, char** argv);
263 int tool_main(int argc, char** argv) {
265 setUpDebuggingFromArgs(argv[0]);
268 return tool_main_core(argc, argv);
271 __except(GenerateDumpAndPrintCallstack(GetExceptionInformation()))
278 #if !defined SK_BUILD_FOR_IOS
279 int main(int argc, char * const argv[]) {
280 return tool_main(argc, (char**) argv);