blob: 7541cdfae6266f13171d7db9ed2d063bfc1475aa [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.com2ccc3af2013-07-23 17:43:18 +0000202 printf("renders = %i\n", FLAGS_benchRender);
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000203 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
204 // TODO(edisonn) if (i == 1) start timer
205 if (strcmp(FLAGS_pages[0], "all") == 0) {
206 for (int pn = 0; pn < renderer.pages(); ++pn) {
207 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
208 }
209 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
210 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
211 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
212 }
213 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
214 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success;
215 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
216 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
217 } else {
218 int pn = atoi(FLAGS_pages[0]);
219 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && pn;
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000220 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000221 }
222 }
223 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000224
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000225 return success;
226}
227
228/** For each file in the directory or for the file passed in input, call
229 * parse_pdf.
230 * @param input A directory or an pdf file.
231 * @param outputDir Output dir.
232 * @param renderer The object responsible to render the skp object into pdf.
233 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000234static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000235 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000236 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000237 if (sk_isdir(input)) {
238 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000239 SkString inputFilename;
240 while (iter.next(&inputFilename)) {
241 SkString inputPath;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000242 SkString _input;
243 _input.append(input);
244 sk_tools::make_filepath(&inputPath, _input, inputFilename);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000245 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000246 ++failures;
247 }
248 }
249 } else {
250 SkString inputPath(input);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000251 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000252 ++failures;
253 }
254 }
255 return failures;
256}
257
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000258int tool_main(int argc, char** argv);
259int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000260 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
261 SkCommandLineFlags::Parse(argc, argv);
262
263 if (FLAGS_readPath.isEmpty()) {
264 SkDebugf(".pdf files or directories are required.\n");
265 exit(-1);
266 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000267
edisonn@google.com222382b2013-07-10 22:33:10 +0000268 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000269
270 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000271 if (FLAGS_writePath.count() == 1) {
272 outputDir.set(FLAGS_writePath[0]);
273 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000274
275 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000276 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000277 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000278 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000279 }
280
281 reportPdfRenderStats();
282
283 if (failures != 0) {
284 SkDebugf("Failed to render %i PDFs.\n", failures);
285 return 1;
286 }
287
288 return 0;
289}
290
291#if !defined SK_BUILD_FOR_IOS
292int main(int argc, char * const argv[]) {
293 return tool_main(argc, (char**) argv);
294}
295#endif