blob: 4e13f7ebd08a58cb3e109207cec48d3c3e83a3cc [file] [log] [blame]
edisonn@google.comc319abe2012-11-01 19:52:38 +00001/*
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"
halcanary0bef17a2014-08-07 07:24:47 -07009#include "SkCommandLineFlags.h"
halcanary8b2cb332014-08-11 13:08:27 -070010#include "SkDocument.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000011#include "SkForceLinking.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000012#include "SkGraphics.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000013#include "SkImageEncoder.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000014#include "SkOSFile.h"
15#include "SkPicture.h"
16#include "SkStream.h"
17#include "SkTArray.h"
halcanary0bef17a2014-08-07 07:24:47 -070018#include "SkTSort.h"
halcanary0d154ee2014-08-11 11:33:51 -070019#include "ProcStats.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000020
scroggo@google.com7def5e12013-05-31 14:00:10 +000021__SK_FORCE_IMAGE_DECODER_LINKING;
22
edisonn@google.com184487c2013-03-08 18:00:16 +000023#ifdef SK_USE_CDB
24#include "win_dbghelp.h"
25#endif
26
edisonn@google.comc319abe2012-11-01 19:52:38 +000027/**
28 * render_pdfs
29 *
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
32 * directory.
33 *
34 * Returns zero exit code if all .skp files were converted successfully,
35 * otherwise returns error code 1.
36 */
37
38static const char PDF_FILE_EXTENSION[] = "pdf";
39static const char SKP_FILE_EXTENSION[] = "skp";
40
halcanary0bef17a2014-08-07 07:24:47 -070041
42DEFINE_string2(inputPaths, r, "",
43 "A list of directories and files to use as input. "
44 "Files are expected to have the .skp extension.");
45
46DEFINE_string2(outputDir, w, "",
47 "Directory to write the rendered pdfs.");
48
49DEFINE_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 ~");
58
edisonn@google.comc319abe2012-11-01 19:52:38 +000059/** 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
skia.committer@gmail.com760f2d92012-11-02 02:01:24 +000081/** Builds the output filename. path = dir/name, and it replaces expected
edisonn@google.comc319abe2012-11-01 19:52:38 +000082 * .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.
87 */
88static bool make_output_filepath(SkString* path, const SkString& dir,
89 const SkString& name) {
tfarinaa8e2e152014-07-28 19:26:58 -070090 *path = SkOSPath::Join(dir.c_str(), name.c_str());
edisonn@google.comc319abe2012-11-01 19:52:38 +000091 return replace_filename_extension(path,
92 SKP_FILE_EXTENSION,
93 PDF_FILE_EXTENSION);
94}
95
halcanary8b2cb332014-08-11 13:08:27 -070096namespace {
97// This is a write-only stream.
98class NullWStream : public SkWStream {
99public:
100 NullWStream() : fBytesWritten(0) { }
mtklein36352bf2015-03-25 18:17:31 -0700101 bool write(const void*, size_t size) override {
halcanary8b2cb332014-08-11 13:08:27 -0700102 fBytesWritten += size;
103 return true;
104 }
mtklein36352bf2015-03-25 18:17:31 -0700105 size_t bytesWritten() const override { return fBytesWritten; }
halcanary8b2cb332014-08-11 13:08:27 -0700106 size_t fBytesWritten;
107};
108} // namespace
109
edisonn@google.comc319abe2012-11-01 19:52:38 +0000110/** 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.
114 */
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000115static SkWStream* open_stream(const SkString& outputDir,
116 const SkString& inputFilename) {
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000117 if (outputDir.isEmpty()) {
halcanary8b2cb332014-08-11 13:08:27 -0700118 return SkNEW(NullWStream);
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000119 }
120
edisonn@google.comc319abe2012-11-01 19:52:38 +0000121 SkString outputPath;
122 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000123 return NULL;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000124 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000125
halcanary8b2cb332014-08-11 13:08:27 -0700126 SkAutoTDelete<SkFILEWStream> stream(
127 SkNEW_ARGS(SkFILEWStream, (outputPath.c_str())));
128 if (!stream.get() || !stream->isValid()) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000129 SkDebugf("Could not write to file %s\n", outputPath.c_str());
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000130 return NULL;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000131 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000132
halcanary8b2cb332014-08-11 13:08:27 -0700133 return stream.detach();
edisonn@google.comc319abe2012-11-01 19:52:38 +0000134}
135
halcanary8b2cb332014-08-11 13:08:27 -0700136/**
137 * Given a SkPicture, write a one-page PDF document to the given
138 * output, using the provided encoder.
edisonn@google.comc319abe2012-11-01 19:52:38 +0000139 */
halcanary8b2cb332014-08-11 13:08:27 -0700140static bool pdf_to_stream(SkPicture* picture,
halcanary8c92dc12015-02-19 18:50:05 -0800141 SkWStream* output) {
halcanary8b2cb332014-08-11 13:08:27 -0700142 SkAutoTUnref<SkDocument> pdfDocument(
halcanary8c92dc12015-02-19 18:50:05 -0800143 SkDocument::CreatePDF(output));
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700144 SkCanvas* canvas = pdfDocument->beginPage(picture->cullRect().width(),
145 picture->cullRect().height());
halcanary8b2cb332014-08-11 13:08:27 -0700146 canvas->drawPicture(picture);
147 canvas->flush();
148 return pdfDocument->close();
edisonn@google.comc319abe2012-11-01 19:52:38 +0000149}
150
halcanary0bef17a2014-08-07 07:24:47 -0700151static bool operator<(const SkString& a, const SkString& b) {
152 return strcmp(a.c_str(), b.c_str()) < 0;
153}
154
halcanary8b2cb332014-08-11 13:08:27 -0700155/**
156 * @param A list of directories or a skp files.
157 * @returns an alphabetical list of skp files.
edisonn@google.comc319abe2012-11-01 19:52:38 +0000158 */
halcanary8b2cb332014-08-11 13:08:27 -0700159static void process_input_files(
halcanary0bef17a2014-08-07 07:24:47 -0700160 const SkCommandLineFlags::StringArray& inputs,
halcanary8b2cb332014-08-11 13:08:27 -0700161 SkTArray<SkString>* files) {
halcanary0bef17a2014-08-07 07:24:47 -0700162 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())) {
halcanary8b2cb332014-08-11 13:08:27 -0700170 files->push_back(
halcanary0bef17a2014-08-07 07:24:47 -0700171 SkOSPath::Join(input, inputFilename.c_str()));
172 }
173 }
174 } else {
175 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, input)) {
halcanary8b2cb332014-08-11 13:08:27 -0700176 files->push_back(SkString(input));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000177 }
178 }
halcanary0bef17a2014-08-07 07:24:47 -0700179 }
halcanary8b2cb332014-08-11 13:08:27 -0700180 if (files->count() > 0) {
181 SkTQSort<SkString>(files->begin(), files->end() - 1);
halcanary0d154ee2014-08-11 11:33:51 -0700182 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000183}
184
halcanary8b2cb332014-08-11 13:08:27 -0700185/** For each input skp file, read it, render it to pdf and write. the
186 * output to a pdf file
187 */
edisonn@google.com184487c2013-03-08 18:00:16 +0000188int tool_main_core(int argc, char** argv);
189int tool_main_core(int argc, char** argv) {
halcanary0bef17a2014-08-07 07:24:47 -0700190 SkCommandLineFlags::Parse(argc, argv);
191
edisonn@google.comc319abe2012-11-01 19:52:38 +0000192 SkAutoGraphics ag;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000193
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000194 SkString outputDir;
halcanary0bef17a2014-08-07 07:24:47 -0700195 if (FLAGS_outputDir.count() > 0) {
196 outputDir = FLAGS_outputDir[0];
halcanary8b2cb332014-08-11 13:08:27 -0700197 if (!sk_mkdir(outputDir.c_str())) {
198 SkDebugf("Unable to mkdir '%s'\n", outputDir.c_str());
199 return 1;
200 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000201 }
202
halcanary8b2cb332014-08-11 13:08:27 -0700203 SkTArray<SkString> files;
204 process_input_files(FLAGS_inputPaths, &files);
halcanary0bef17a2014-08-07 07:24:47 -0700205
halcanary8b2cb332014-08-11 13:08:27 -0700206 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());
halcanary0d154ee2014-08-11 11:33:51 -0700210 }
211
halcanary8b2cb332014-08-11 13:08:27 -0700212 int failures = 0;
213 for (int i = 0; i < files.count(); i ++) {
214 SkString basename = SkOSPath::Basename(files[i].c_str());
215
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());
220 ++failures;
221 continue;
222 }
223
224 SkAutoTUnref<SkPicture> picture(
225 SkPicture::CreateFromStream(&inputStream));
226 if (NULL == picture.get()) {
227 SkDebugf("Could not read an SkPicture from %s\n",
228 files[i].c_str());
229 ++failures;
230 continue;
231 }
halcanary5bb97002014-10-16 10:32:52 -0700232 SkDebugf("[%6g %6g %6g %6g] %-*s",
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700233 picture->cullRect().fLeft, picture->cullRect().fTop,
234 picture->cullRect().fRight, picture->cullRect().fBottom,
235 maximumPathLength, basename.c_str());
halcanary8b2cb332014-08-11 13:08:27 -0700236
237 SkAutoTDelete<SkWStream> stream(open_stream(outputDir, files[i]));
238 if (!stream.get()) {
239 ++failures;
240 continue;
241 }
halcanary8c92dc12015-02-19 18:50:05 -0800242 if (!pdf_to_stream(picture, stream.get())) {
halcanary8b2cb332014-08-11 13:08:27 -0700243 SkDebugf("Error in PDF Serialization.");
244 ++failures;
245 }
246
mtkleinafb43792014-08-19 15:55:55 -0700247 int max_rss_mb = sk_tools::getMaxResidentSetSizeMB();
248 if (max_rss_mb >= 0) {
249 SkDebugf(" %4dM peak rss", max_rss_mb);
halcanary8b2cb332014-08-11 13:08:27 -0700250 }
251
252 SkDebugf("\n");
253 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000254 if (failures != 0) {
halcanary8b2cb332014-08-11 13:08:27 -0700255 SkDebugf("Failed to render %i of %i PDFs.\n", failures, files.count());
edisonn@google.comc319abe2012-11-01 19:52:38 +0000256 return 1;
257 }
edisonn@google.com184487c2013-03-08 18:00:16 +0000258
259 return 0;
260}
261
262int tool_main(int argc, char** argv);
263int tool_main(int argc, char** argv) {
264#ifdef SK_USE_CDB
265 setUpDebuggingFromArgs(argv[0]);
266 __try {
267#endif
268 return tool_main_core(argc, argv);
269#ifdef SK_USE_CDB
270 }
271 __except(GenerateDumpAndPrintCallstack(GetExceptionInformation()))
272 {
273 return -1;
274 }
275#endif
humper@google.comf2863292013-01-07 20:08:56 +0000276 return 0;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000277}
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000278#if !defined SK_BUILD_FOR_IOS
279int main(int argc, char * const argv[]) {
280 return tool_main(argc, (char**) argv);
281}
282#endif