blob: 207cd9679f5d9aeadd2d0bb6d4430071126bd056 [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.");
edisonn@google.com7b328fd2013-07-11 12:53:06 +000020DEFINE_string2(pages, p, "all", "What pages to render and how:\n"
21 "\tall - all pages\n"
22 "\treverse - all pages, in reverse order\n"
23 "\tfirst - first page\n"
24 "\tlast - last page\n"
25 "\tnumber - a specific page number\n"
26 );
edisonn@google.com15b11182013-07-11 14:43:15 +000027DEFINE_double(DPI, 72, "DPI to be used for rendering (scale).");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000028DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any rendering and \n"
29 "\tminimal parsing to ensure correctness. Default 0 (disabled).");
30DEFINE_int32(benchRender, 0, "Render the pdf content N times. Default 0 (disabled)");
31
32
edisonn@google.com15b11182013-07-11 14:43:15 +000033// TODO(edisonn): add config for device target(gpu, raster, pdf), + ability not to render at all
edisonn@google.com7b328fd2013-07-11 12:53:06 +000034
edisonn@google.com01cd4d52013-06-10 20:44:45 +000035/**
36 * Given list of directories and files to use as input, expects to find .pdf
37 * files and it will convert them to .png files writing them in the same directory
38 * one file for each page.
39 *
40 * Returns zero exit code if all .pdf files were converted successfully,
41 * otherwise returns error code 1.
42 */
43
44static const char PDF_FILE_EXTENSION[] = "pdf";
45static const char PNG_FILE_EXTENSION[] = "png";
46
edisonn@google.com01cd4d52013-06-10 20:44:45 +000047/** 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 */
edisonn@google.com222382b2013-07-10 22:33:10 +000054static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000055 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 }
edisonn@google.com222382b2013-07-10 22:33:10 +000063 if (page >= 0) {
64 path->appendf("%i.", page);
65 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000066 path->append(new_extension);
67 return true;
68 }
69 return false;
70}
edisonn@google.com222382b2013-07-10 22:33:10 +000071
edisonn@google.com01cd4d52013-06-10 20:44:45 +000072/** Builds the output filename. path = dir/name, and it replaces expected
73 * .skp extension with .pdf extention.
74 * @param path Output filename.
75 * @param name The name of the file.
76 * @returns false if the file did not has the expected extension.
77 * if false is returned, contents of path are undefined.
78 */
edisonn@google.com596d2e22013-07-10 17:44:55 +000079
edisonn@google.com222382b2013-07-10 22:33:10 +000080
edisonn@google.com01cd4d52013-06-10 20:44:45 +000081static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +000082 const SkString& name,
83 int page) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +000084 sk_tools::make_filepath(path, dir, name);
edisonn@google.com222382b2013-07-10 22:33:10 +000085 return add_page_and_replace_filename_extension(path, page,
86 PDF_FILE_EXTENSION,
87 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +000088}
edisonn@google.com222382b2013-07-10 22:33:10 +000089
90static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
91 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
92
93 bitmap->allocPixels();
94 bitmap->eraseColor(color);
95}
96
edisonn@google.com01cd4d52013-06-10 20:44:45 +000097/** Write the output of pdf renderer to a file.
98 * @param outputDir Output dir.
99 * @param inputFilename The skp file that was read.
100 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000101 * @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 +0000102 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000103
edisonn@google.com13102382013-07-11 14:50:12 +0000104extern "C" SkBitmap* gDumpBitmap;
105extern "C" SkCanvas* gDumpCanvas;
106
edisonn@google.com222382b2013-07-10 22:33:10 +0000107static bool render_page(const SkString& outputDir,
edisonn@google.com444e25a2013-07-11 15:20:50 +0000108 const SkString& inputFilename,
109 const SkPdfRenderer& renderer,
110 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000111 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
112
113 SkBitmap bitmap;
edisonn@google.com444e25a2013-07-11 15:20:50 +0000114 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0)));
115 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0)));
116
117 rect = SkRect::MakeWH(width, height);
118
edisonn@google.com222382b2013-07-10 22:33:10 +0000119#ifdef PDF_DEBUG_3X
edisonn@google.com444e25a2013-07-11 15:20:50 +0000120 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height));
edisonn@google.com222382b2013-07-10 22:33:10 +0000121#else
edisonn@google.com444e25a2013-07-11 15:20:50 +0000122 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
edisonn@google.com222382b2013-07-10 22:33:10 +0000123#endif
124 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
125 SkCanvas canvas(device);
126
127 gDumpBitmap = &bitmap;
128
129 gDumpCanvas = &canvas;
edisonn@google.com444e25a2013-07-11 15:20:50 +0000130 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
edisonn@google.com222382b2013-07-10 22:33:10 +0000131
edisonn@google.com13102382013-07-11 14:50:12 +0000132 SkString outputPath;
133 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
134 return false;
135 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000136 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000137
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000138 if (FLAGS_showMemoryUsage) {
139 SkDebugf("Memory usage after page %i rendered: %u\n", page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
140 }
141
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000142 return true;
143}
edisonn@google.com222382b2013-07-10 22:33:10 +0000144
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000145/** 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.
149 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000150static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000151 SkPdfRenderer& renderer) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000152 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
153
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000154 SkString inputFilename;
155 sk_tools::get_basename(&inputFilename, inputPath);
156
157 SkFILEStream inputStream;
158 inputStream.setPath(inputPath.c_str());
159 if (!inputStream.isValid()) {
160 SkDebugf("Could not open file %s\n", inputPath.c_str());
161 return false;
162 }
163
164 bool success = false;
165
edisonn@google.com222382b2013-07-10 22:33:10 +0000166 success = renderer.load(inputPath);
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000167 if (FLAGS_showMemoryUsage) {
168 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
169 }
170
171 // TODO(edisonn): bench timers
172 if (FLAGS_benchLoad > 0) {
173 for (int i = 0 ; i < FLAGS_benchLoad; i++) {
174 success = renderer.load(inputPath);
175 if (FLAGS_showMemoryUsage) {
176 SkDebugf("Memory usage after load %i number : %u\n", i, (unsigned int)renderer.bytesUsed());
177 }
178 }
179 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000180
edisonn@google.com222382b2013-07-10 22:33:10 +0000181 if (success) {
182 if (!renderer.pages())
183 {
184 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
185 return false;
186 } else {
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000187 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
188 // TODO(edisonn) if (i == 1) start timer
189 if (strcmp(FLAGS_pages[0], "all") == 0) {
190 for (int pn = 0; pn < renderer.pages(); ++pn) {
191 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
192 }
193 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
194 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
195 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
196 }
197 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
198 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success;
199 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
200 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
201 } else {
202 int pn = atoi(FLAGS_pages[0]);
203 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && pn;
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000204 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000205 }
206 }
207 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000208
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000209 return success;
210}
211
212/** For each file in the directory or for the file passed in input, call
213 * parse_pdf.
214 * @param input A directory or an pdf file.
215 * @param outputDir Output dir.
216 * @param renderer The object responsible to render the skp object into pdf.
217 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000218static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000219 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000220 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000221 if (sk_isdir(input)) {
222 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000223 SkString inputFilename;
224 while (iter.next(&inputFilename)) {
225 SkString inputPath;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000226 SkString _input;
227 _input.append(input);
228 sk_tools::make_filepath(&inputPath, _input, inputFilename);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000229 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000230 ++failures;
231 }
232 }
233 } else {
234 SkString inputPath(input);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000235 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000236 ++failures;
237 }
238 }
239 return failures;
240}
241
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000242int tool_main(int argc, char** argv);
243int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000244 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
245 SkCommandLineFlags::Parse(argc, argv);
246
247 if (FLAGS_readPath.isEmpty()) {
248 SkDebugf(".pdf files or directories are required.\n");
249 exit(-1);
250 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000251
edisonn@google.com222382b2013-07-10 22:33:10 +0000252 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000253
254 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000255 if (FLAGS_writePath.count() == 1) {
256 outputDir.set(FLAGS_writePath[0]);
257 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000258
259 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000260 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000261 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000262 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000263 }
264
265 reportPdfRenderStats();
266
267 if (failures != 0) {
268 SkDebugf("Failed to render %i PDFs.\n", failures);
269 return 1;
270 }
271
272 return 0;
273}
274
275#if !defined SK_BUILD_FOR_IOS
276int main(int argc, char * const argv[]) {
277 return tool_main(argc, (char**) argv);
278}
279#endif