blob: 0b3b1aa017ced2bc8e9c8109a8ec7ae98123662f [file] [log] [blame]
edisonn@google.com01cd4d52013-06-10 20:44:45 +00001#include "SkCanvas.h"
edisonn@google.coma5aaa792013-07-11 12:27:21 +00002#include "SkCommandLineFlags.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +00003#include "SkDevice.h"
4#include "SkGraphics.h"
5#include "SkImageDecoder.h"
6#include "SkImageEncoder.h"
7#include "SkOSFile.h"
8#include "SkPicture.h"
9#include "SkStream.h"
10#include "SkTypeface.h"
11#include "SkTArray.h"
12#include "picture_utils.h"
13
edisonn@google.com222382b2013-07-10 22:33:10 +000014#include "SkPdfRenderer.h"
edisonn@google.comb857a0c2013-06-25 20:45:40 +000015
edisonn@google.coma5aaa792013-07-11 12:27:21 +000016DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process.");
17DEFINE_string2(writePath, w, "", "Directory to write the rendered pages.");
18DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page.");
19DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage.");
20
edisonn@google.com01cd4d52013-06-10 20:44:45 +000021/**
22 * Given list of directories and files to use as input, expects to find .pdf
23 * files and it will convert them to .png files writing them in the same directory
24 * one file for each page.
25 *
26 * Returns zero exit code if all .pdf files were converted successfully,
27 * otherwise returns error code 1.
28 */
29
30static const char PDF_FILE_EXTENSION[] = "pdf";
31static const char PNG_FILE_EXTENSION[] = "png";
32
edisonn@google.com01cd4d52013-06-10 20:44:45 +000033/** Replaces the extension of a file.
34 * @param path File name whose extension will be changed.
35 * @param old_extension The old extension.
36 * @param new_extension The new extension.
37 * @returns false if the file did not has the expected extension.
38 * if false is returned, contents of path are undefined.
39 */
edisonn@google.com222382b2013-07-10 22:33:10 +000040static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000041 const char old_extension[],
42 const char new_extension[]) {
43 if (path->endsWith(old_extension)) {
44 path->remove(path->size() - strlen(old_extension),
45 strlen(old_extension));
46 if (!path->endsWith(".")) {
47 return false;
48 }
edisonn@google.com222382b2013-07-10 22:33:10 +000049 if (page >= 0) {
50 path->appendf("%i.", page);
51 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000052 path->append(new_extension);
53 return true;
54 }
55 return false;
56}
edisonn@google.com222382b2013-07-10 22:33:10 +000057
edisonn@google.com01cd4d52013-06-10 20:44:45 +000058/** Builds the output filename. path = dir/name, and it replaces expected
59 * .skp extension with .pdf extention.
60 * @param path Output filename.
61 * @param name The name of the file.
62 * @returns false if the file did not has the expected extension.
63 * if false is returned, contents of path are undefined.
64 */
edisonn@google.com596d2e22013-07-10 17:44:55 +000065
edisonn@google.com222382b2013-07-10 22:33:10 +000066
edisonn@google.com01cd4d52013-06-10 20:44:45 +000067static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +000068 const SkString& name,
69 int page) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +000070 sk_tools::make_filepath(path, dir, name);
edisonn@google.com222382b2013-07-10 22:33:10 +000071 return add_page_and_replace_filename_extension(path, page,
72 PDF_FILE_EXTENSION,
73 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +000074}
edisonn@google.com222382b2013-07-10 22:33:10 +000075
76static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
77 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
78
79 bitmap->allocPixels();
80 bitmap->eraseColor(color);
81}
82
edisonn@google.com01cd4d52013-06-10 20:44:45 +000083/** Write the output of pdf renderer to a file.
84 * @param outputDir Output dir.
85 * @param inputFilename The skp file that was read.
86 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +000087 * @param page -1 means there is only one page (0), and render in a file without page extension
edisonn@google.com01cd4d52013-06-10 20:44:45 +000088 */
edisonn@google.com222382b2013-07-10 22:33:10 +000089
90static bool render_page(const SkString& outputDir,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000091 const SkString& inputFilename,
edisonn@google.com222382b2013-07-10 22:33:10 +000092 const SkPdfRenderer& renderer,
93 int page) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +000094 if (outputDir.isEmpty()) {
edisonn@google.com222382b2013-07-10 22:33:10 +000095 SkBitmap bitmap;
96 setup_bitmap(&bitmap, 1, 1);
97 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
98 SkCanvas canvas(device);
99 return renderer.renderPage(page < 0 ? 0 : page, &canvas);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000100 }
101
102 SkString outputPath;
edisonn@google.com222382b2013-07-10 22:33:10 +0000103 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000104 return false;
105 }
106
edisonn@google.com222382b2013-07-10 22:33:10 +0000107 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
108
109 SkBitmap bitmap;
110#ifdef PDF_DEBUG_3X
111 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(rect.width()), 3 * (int)SkScalarToDouble(rect.height()));
112#else
113 setup_bitmap(&bitmap, (int)SkScalarToDouble(rect.width()), (int)SkScalarToDouble(rect.height()));
114#endif
115 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
116 SkCanvas canvas(device);
117
118 gDumpBitmap = &bitmap;
119
120 gDumpCanvas = &canvas;
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000121 renderer.renderPage(page < 0 ? 0 : page, &canvas);
edisonn@google.com222382b2013-07-10 22:33:10 +0000122
123 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000124
125 return true;
126}
edisonn@google.com222382b2013-07-10 22:33:10 +0000127
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000128/** Reads an skp file, renders it to pdf and writes the output to a pdf file
129 * @param inputPath The skp file to be read.
130 * @param outputDir Output dir.
131 * @param renderer The object responsible to render the skp object into pdf.
132 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000133static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000134 SkPdfRenderer& renderer, bool noPageExt, bool showMemoryUsage) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000135 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
136
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000137 SkString inputFilename;
138 sk_tools::get_basename(&inputFilename, inputPath);
139
140 SkFILEStream inputStream;
141 inputStream.setPath(inputPath.c_str());
142 if (!inputStream.isValid()) {
143 SkDebugf("Could not open file %s\n", inputPath.c_str());
144 return false;
145 }
146
147 bool success = false;
148
edisonn@google.com222382b2013-07-10 22:33:10 +0000149 success = renderer.load(inputPath);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000150
edisonn@google.com222382b2013-07-10 22:33:10 +0000151 if (success) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000152 if (showMemoryUsage) {
153 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
154 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000155 if (!renderer.pages())
156 {
157 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
158 return false;
159 } else {
160 for (int pn = 0; pn < renderer.pages(); ++pn) {
161 success = render_page(outputDir, inputFilename, renderer, noPageExt && renderer.pages() == 1 ? -1 : pn) && success;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000162 SkDebugf("Memory usage after page %i rendered: %u\n", pn, (unsigned int)renderer.bytesUsed());
edisonn@google.com222382b2013-07-10 22:33:10 +0000163 }
164 }
165 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000166
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000167 return success;
168}
169
170/** For each file in the directory or for the file passed in input, call
171 * parse_pdf.
172 * @param input A directory or an pdf file.
173 * @param outputDir Output dir.
174 * @param renderer The object responsible to render the skp object into pdf.
175 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000176static int process_input(const char* input, const SkString& outputDir,
177 SkPdfRenderer& renderer, bool noPageExt, bool showMemoryUsage) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000178 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000179 if (sk_isdir(input)) {
180 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000181 SkString inputFilename;
182 while (iter.next(&inputFilename)) {
183 SkString inputPath;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000184 SkString _input;
185 _input.append(input);
186 sk_tools::make_filepath(&inputPath, _input, inputFilename);
187 if (!process_pdf(inputPath, outputDir, renderer, noPageExt, showMemoryUsage)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000188 ++failures;
189 }
190 }
191 } else {
192 SkString inputPath(input);
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000193 if (!process_pdf(inputPath, outputDir, renderer, noPageExt, showMemoryUsage)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000194 ++failures;
195 }
196 }
197 return failures;
198}
199
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000200int tool_main(int argc, char** argv);
201int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000202 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
203 SkCommandLineFlags::Parse(argc, argv);
204
205 if (FLAGS_readPath.isEmpty()) {
206 SkDebugf(".pdf files or directories are required.\n");
207 exit(-1);
208 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000209
edisonn@google.com222382b2013-07-10 22:33:10 +0000210 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000211
212 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000213 if (FLAGS_writePath.count() == 1) {
214 outputDir.set(FLAGS_writePath[0]);
215 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000216
217 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000218 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
219 failures += process_input(FLAGS_readPath[i], outputDir, renderer,
220 FLAGS_noExtensionForOnePagePdf,
221 FLAGS_showMemoryUsage);
edisonn@google.com222382b2013-07-10 22:33:10 +0000222 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000223 }
224
225 reportPdfRenderStats();
226
227 if (failures != 0) {
228 SkDebugf("Failed to render %i PDFs.\n", failures);
229 return 1;
230 }
231
232 return 0;
233}
234
235#if !defined SK_BUILD_FOR_IOS
236int main(int argc, char * const argv[]) {
237 return tool_main(argc, (char**) argv);
238}
239#endif