blob: f08fa510fded6d74926d07c4912f362012f9f9ce [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"
edisonn@google.comac03d912013-07-22 15:36:39 +000013#include "SkNulCanvas.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000014
edisonn@google.com222382b2013-07-10 22:33:10 +000015#include "SkPdfRenderer.h"
edisonn@google.comb857a0c2013-06-25 20:45:40 +000016
edisonn@google.coma5aaa792013-07-11 12:27:21 +000017DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process.");
18DEFINE_string2(writePath, w, "", "Directory to write the rendered pages.");
19DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page.");
20DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage.");
edisonn@google.com7b328fd2013-07-11 12:53:06 +000021DEFINE_string2(pages, p, "all", "What pages to render and how:\n"
22 "\tall - all pages\n"
23 "\treverse - all pages, in reverse order\n"
24 "\tfirst - first page\n"
25 "\tlast - last page\n"
26 "\tnumber - a specific page number\n"
27 );
edisonn@google.com15b11182013-07-11 14:43:15 +000028DEFINE_double(DPI, 72, "DPI to be used for rendering (scale).");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000029DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any rendering and \n"
30 "\tminimal parsing to ensure correctness. Default 0 (disabled).");
31DEFINE_int32(benchRender, 0, "Render the pdf content N times. Default 0 (disabled)");
edisonn@google.comac03d912013-07-22 15:36:39 +000032DEFINE_string2(config, c, "8888", "Canvas to render:\n"
33 "\t8888 - all pages\n"
34 "\tnul - all pages, in reverse order\n"
35 );
edisonn@google.com6a9d4362013-07-11 16:25:51 +000036
37
edisonn@google.com15b11182013-07-11 14:43:15 +000038// TODO(edisonn): add config for device target(gpu, raster, pdf), + ability not to render at all
edisonn@google.com7b328fd2013-07-11 12:53:06 +000039
edisonn@google.com01cd4d52013-06-10 20:44:45 +000040/**
41 * Given list of directories and files to use as input, expects to find .pdf
42 * files and it will convert them to .png files writing them in the same directory
43 * one file for each page.
44 *
45 * Returns zero exit code if all .pdf files were converted successfully,
46 * otherwise returns error code 1.
47 */
48
49static const char PDF_FILE_EXTENSION[] = "pdf";
50static const char PNG_FILE_EXTENSION[] = "png";
51
edisonn@google.com01cd4d52013-06-10 20:44:45 +000052/** Replaces the extension of a file.
53 * @param path File name whose extension will be changed.
54 * @param old_extension The old extension.
55 * @param new_extension The new extension.
56 * @returns false if the file did not has the expected extension.
57 * if false is returned, contents of path are undefined.
58 */
edisonn@google.com222382b2013-07-10 22:33:10 +000059static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000060 const char old_extension[],
61 const char new_extension[]) {
62 if (path->endsWith(old_extension)) {
63 path->remove(path->size() - strlen(old_extension),
64 strlen(old_extension));
65 if (!path->endsWith(".")) {
66 return false;
67 }
edisonn@google.com222382b2013-07-10 22:33:10 +000068 if (page >= 0) {
69 path->appendf("%i.", page);
70 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000071 path->append(new_extension);
72 return true;
73 }
74 return false;
75}
edisonn@google.com222382b2013-07-10 22:33:10 +000076
edisonn@google.com01cd4d52013-06-10 20:44:45 +000077/** Builds the output filename. path = dir/name, and it replaces expected
78 * .skp extension with .pdf extention.
79 * @param path Output filename.
80 * @param name The name of the file.
81 * @returns false if the file did not has the expected extension.
82 * if false is returned, contents of path are undefined.
83 */
edisonn@google.com596d2e22013-07-10 17:44:55 +000084
edisonn@google.com222382b2013-07-10 22:33:10 +000085
edisonn@google.com01cd4d52013-06-10 20:44:45 +000086static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +000087 const SkString& name,
88 int page) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +000089 sk_tools::make_filepath(path, dir, name);
edisonn@google.com222382b2013-07-10 22:33:10 +000090 return add_page_and_replace_filename_extension(path, page,
91 PDF_FILE_EXTENSION,
92 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +000093}
edisonn@google.com222382b2013-07-10 22:33:10 +000094
95static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
96 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
97
98 bitmap->allocPixels();
99 bitmap->eraseColor(color);
100}
101
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000102/** Write the output of pdf renderer to a file.
103 * @param outputDir Output dir.
104 * @param inputFilename The skp file that was read.
105 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000106 * @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 +0000107 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000108
edisonn@google.com13102382013-07-11 14:50:12 +0000109extern "C" SkBitmap* gDumpBitmap;
110extern "C" SkCanvas* gDumpCanvas;
111
edisonn@google.com222382b2013-07-10 22:33:10 +0000112static bool render_page(const SkString& outputDir,
edisonn@google.com444e25a2013-07-11 15:20:50 +0000113 const SkString& inputFilename,
114 const SkPdfRenderer& renderer,
115 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000116 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
117
edisonn@google.comac03d912013-07-22 15:36:39 +0000118 // Exercise all pdf codepaths as in normal rendering, but no actual bits are changed.
119 if (!FLAGS_config.isEmpty() && strcmp(FLAGS_config[0], "nul") == 0) {
120 SkBitmap bitmap;
121 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
122 SkNulCanvas canvas(device);
123 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
124 } else {
125 // 8888
126 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000127
edisonn@google.comac03d912013-07-22 15:36:39 +0000128 SkBitmap bitmap;
129 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0)));
130 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0)));
131
132 rect = SkRect::MakeWH(width, height);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000133
edisonn@google.com222382b2013-07-10 22:33:10 +0000134#ifdef PDF_DEBUG_3X
edisonn@google.comac03d912013-07-22 15:36:39 +0000135 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height));
edisonn@google.com222382b2013-07-10 22:33:10 +0000136#else
edisonn@google.comac03d912013-07-22 15:36:39 +0000137 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
edisonn@google.com222382b2013-07-10 22:33:10 +0000138#endif
edisonn@google.comac03d912013-07-22 15:36:39 +0000139 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
140 SkCanvas canvas(device);
edisonn@google.com222382b2013-07-10 22:33:10 +0000141
edisonn@google.comac03d912013-07-22 15:36:39 +0000142 gDumpBitmap = &bitmap;
edisonn@google.com222382b2013-07-10 22:33:10 +0000143
edisonn@google.comac03d912013-07-22 15:36:39 +0000144 gDumpCanvas = &canvas;
145 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
edisonn@google.com222382b2013-07-10 22:33:10 +0000146
edisonn@google.comac03d912013-07-22 15:36:39 +0000147 SkString outputPath;
148 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
149 return false;
150 }
151 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
152
153 if (FLAGS_showMemoryUsage) {
154 SkDebugf("Memory usage after page %i rendered: %u\n", page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
155 }
edisonn@google.com13102382013-07-11 14:50:12 +0000156 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000157 return true;
158}
edisonn@google.com222382b2013-07-10 22:33:10 +0000159
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000160/** Reads an skp file, renders it to pdf and writes the output to a pdf file
161 * @param inputPath The skp file to be read.
162 * @param outputDir Output dir.
163 * @param renderer The object responsible to render the skp object into pdf.
164 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000165static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000166 SkPdfRenderer& renderer) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000167 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
168
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000169 SkString inputFilename;
170 sk_tools::get_basename(&inputFilename, inputPath);
171
172 SkFILEStream inputStream;
173 inputStream.setPath(inputPath.c_str());
174 if (!inputStream.isValid()) {
175 SkDebugf("Could not open file %s\n", inputPath.c_str());
176 return false;
177 }
178
179 bool success = false;
180
edisonn@google.com222382b2013-07-10 22:33:10 +0000181 success = renderer.load(inputPath);
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000182 if (FLAGS_showMemoryUsage) {
183 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
184 }
185
186 // TODO(edisonn): bench timers
187 if (FLAGS_benchLoad > 0) {
188 for (int i = 0 ; i < FLAGS_benchLoad; i++) {
189 success = renderer.load(inputPath);
190 if (FLAGS_showMemoryUsage) {
191 SkDebugf("Memory usage after load %i number : %u\n", i, (unsigned int)renderer.bytesUsed());
192 }
193 }
194 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000195
edisonn@google.com222382b2013-07-10 22:33:10 +0000196 if (success) {
197 if (!renderer.pages())
198 {
199 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
200 return false;
201 } else {
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000202 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
203 // TODO(edisonn) if (i == 1) start timer
204 if (strcmp(FLAGS_pages[0], "all") == 0) {
205 for (int pn = 0; pn < renderer.pages(); ++pn) {
206 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
207 }
208 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
209 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
210 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
211 }
212 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
213 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success;
214 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
215 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
216 } else {
217 int pn = atoi(FLAGS_pages[0]);
218 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && pn;
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000219 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000220 }
221 }
222 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000223
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000224 return success;
225}
226
227/** For each file in the directory or for the file passed in input, call
228 * parse_pdf.
229 * @param input A directory or an pdf file.
230 * @param outputDir Output dir.
231 * @param renderer The object responsible to render the skp object into pdf.
232 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000233static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000234 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000235 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000236 if (sk_isdir(input)) {
237 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000238 SkString inputFilename;
239 while (iter.next(&inputFilename)) {
240 SkString inputPath;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000241 SkString _input;
242 _input.append(input);
243 sk_tools::make_filepath(&inputPath, _input, inputFilename);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000244 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000245 ++failures;
246 }
247 }
248 } else {
249 SkString inputPath(input);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000250 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000251 ++failures;
252 }
253 }
254 return failures;
255}
256
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000257int tool_main(int argc, char** argv);
258int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000259 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
260 SkCommandLineFlags::Parse(argc, argv);
261
262 if (FLAGS_readPath.isEmpty()) {
263 SkDebugf(".pdf files or directories are required.\n");
264 exit(-1);
265 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000266
edisonn@google.com222382b2013-07-10 22:33:10 +0000267 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000268
269 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000270 if (FLAGS_writePath.count() == 1) {
271 outputDir.set(FLAGS_writePath[0]);
272 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000273
274 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000275 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000276 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000277 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000278 }
279
280 reportPdfRenderStats();
281
282 if (failures != 0) {
283 SkDebugf("Failed to render %i PDFs.\n", failures);
284 return 1;
285 }
286
287 return 0;
288}
289
290#if !defined SK_BUILD_FOR_IOS
291int main(int argc, char * const argv[]) {
292 return tool_main(argc, (char**) argv);
293}
294#endif