blob: 2ab88dec423324a12b388a890f117a07c74fb4a9 [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"
edisonn@google.comac03d912013-07-22 15:36:39 +000012#include "SkNulCanvas.h"
edisonn@google.com01cd4d52013-06-10 20:44:45 +000013
edisonn@google.com768bc6a2013-08-08 12:42:13 +000014#if SK_SUPPORT_GPU
15#include "GrContextFactory.h"
16#include "GrContext.h"
17#include "SkGpuDevice.h"
18#endif
19
edisonn@google.com222382b2013-07-10 22:33:10 +000020#include "SkPdfRenderer.h"
edisonn@google.comb857a0c2013-06-25 20:45:40 +000021
edisonn@google.coma5aaa792013-07-11 12:27:21 +000022DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process.");
23DEFINE_string2(writePath, w, "", "Directory to write the rendered pages.");
24DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page.");
25DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage.");
edisonn@google.com7b328fd2013-07-11 12:53:06 +000026DEFINE_string2(pages, p, "all", "What pages to render and how:\n"
27 "\tall - all pages\n"
28 "\treverse - all pages, in reverse order\n"
29 "\tfirst - first page\n"
30 "\tlast - last page\n"
31 "\tnumber - a specific page number\n"
32 );
edisonn@google.com15b11182013-07-11 14:43:15 +000033DEFINE_double(DPI, 72, "DPI to be used for rendering (scale).");
edisonn@google.com6a9d4362013-07-11 16:25:51 +000034DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any rendering and \n"
35 "\tminimal parsing to ensure correctness. Default 0 (disabled).");
36DEFINE_int32(benchRender, 0, "Render the pdf content N times. Default 0 (disabled)");
edisonn@google.comac03d912013-07-22 15:36:39 +000037DEFINE_string2(config, c, "8888", "Canvas to render:\n"
edisonn@google.com768bc6a2013-08-08 12:42:13 +000038 "\t8888 - argb\n"
39
40#if SK_SUPPORT_GPU
41 "\tgpu: use the gpu\n"
42#endif
43 "\tnul - render in null canvas, any draw will just return.\n"
edisonn@google.comac03d912013-07-22 15:36:39 +000044 );
edisonn@google.com6a9d4362013-07-11 16:25:51 +000045
46
edisonn@google.com15b11182013-07-11 14:43:15 +000047// TODO(edisonn): add config for device target(gpu, raster, pdf), + ability not to render at all
edisonn@google.com7b328fd2013-07-11 12:53:06 +000048
edisonn@google.com01cd4d52013-06-10 20:44:45 +000049/**
50 * Given list of directories and files to use as input, expects to find .pdf
51 * files and it will convert them to .png files writing them in the same directory
52 * one file for each page.
53 *
54 * Returns zero exit code if all .pdf files were converted successfully,
55 * otherwise returns error code 1.
56 */
57
58static const char PDF_FILE_EXTENSION[] = "pdf";
59static const char PNG_FILE_EXTENSION[] = "png";
60
edisonn@google.com01cd4d52013-06-10 20:44:45 +000061/** Replaces the extension of a file.
62 * @param path File name whose extension will be changed.
63 * @param old_extension The old extension.
64 * @param new_extension The new extension.
65 * @returns false if the file did not has the expected extension.
66 * if false is returned, contents of path are undefined.
67 */
edisonn@google.com222382b2013-07-10 22:33:10 +000068static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000069 const char old_extension[],
70 const char new_extension[]) {
71 if (path->endsWith(old_extension)) {
72 path->remove(path->size() - strlen(old_extension),
73 strlen(old_extension));
74 if (!path->endsWith(".")) {
75 return false;
76 }
edisonn@google.com222382b2013-07-10 22:33:10 +000077 if (page >= 0) {
78 path->appendf("%i.", page);
79 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000080 path->append(new_extension);
81 return true;
82 }
83 return false;
84}
edisonn@google.com222382b2013-07-10 22:33:10 +000085
edisonn@google.com147adb12013-07-24 15:56:19 +000086void make_filepath(SkString* path, const SkString& dir, const SkString& name) {
87 size_t len = dir.size();
88 path->set(dir);
89 if (0 < len && '/' != dir[len - 1]) {
90 path->append("/");
91 }
92 path->append(name);
93}
94
95bool is_path_seperator(const char chr) {
96#if defined(SK_BUILD_FOR_WIN)
97 return chr == '\\' || chr == '/';
98#else
99 return chr == '/';
100#endif
101}
102
103void get_basename(SkString* basename, const SkString& path) {
104 if (path.size() == 0) {
105 basename->reset();
106 return;
107 }
108
109 size_t end = path.size() - 1;
110
111 // Paths pointing to directories often have a trailing slash,
112 // we remove it so the name is not empty
113 if (is_path_seperator(path[end])) {
114 if (end == 0) {
115 basename->reset();
116 return;
117 }
118
119 end -= 1;
120 }
121
122 size_t i = end;
123 do {
124 --i;
125 if (is_path_seperator(path[i])) {
126 const char* basenameStart = path.c_str() + i + 1;
127 size_t basenameLength = end - i;
128 basename->set(basenameStart, basenameLength);
129 return;
130 }
131 } while (i > 0);
132
133 basename->set(path.c_str(), end + 1);
134}
135
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000136/** Builds the output filename. path = dir/name, and it replaces expected
137 * .skp extension with .pdf extention.
138 * @param path Output filename.
139 * @param name The name of the file.
140 * @returns false if the file did not has the expected extension.
141 * if false is returned, contents of path are undefined.
142 */
143static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +0000144 const SkString& name,
145 int page) {
edisonn@google.com147adb12013-07-24 15:56:19 +0000146 make_filepath(path, dir, name);
edisonn@google.com222382b2013-07-10 22:33:10 +0000147 return add_page_and_replace_filename_extension(path, page,
148 PDF_FILE_EXTENSION,
149 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000150}
edisonn@google.com222382b2013-07-10 22:33:10 +0000151
edisonn@google.com5149bd92013-08-05 17:26:11 +0000152static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorTRANSPARENT) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000153 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
154
155 bitmap->allocPixels();
156 bitmap->eraseColor(color);
157}
158
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000159/** Write the output of pdf renderer to a file.
160 * @param outputDir Output dir.
161 * @param inputFilename The skp file that was read.
162 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +0000163 * @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 +0000164 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000165
edisonn@google.com13102382013-07-11 14:50:12 +0000166extern "C" SkBitmap* gDumpBitmap;
167extern "C" SkCanvas* gDumpCanvas;
168
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000169#if SK_SUPPORT_GPU
170GrContextFactory gContextFactory;
171#endif
172
edisonn@google.com222382b2013-07-10 22:33:10 +0000173static bool render_page(const SkString& outputDir,
edisonn@google.com444e25a2013-07-11 15:20:50 +0000174 const SkString& inputFilename,
175 const SkPdfRenderer& renderer,
176 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000177 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
178
edisonn@google.comac03d912013-07-22 15:36:39 +0000179 // Exercise all pdf codepaths as in normal rendering, but no actual bits are changed.
180 if (!FLAGS_config.isEmpty() && strcmp(FLAGS_config[0], "nul") == 0) {
181 SkBitmap bitmap;
182 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
183 SkNulCanvas canvas(device);
184 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
185 } else {
186 // 8888
187 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000188
edisonn@google.comac03d912013-07-22 15:36:39 +0000189 SkBitmap bitmap;
190 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0)));
191 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0)));
192
193 rect = SkRect::MakeWH(width, height);
edisonn@google.com444e25a2013-07-11 15:20:50 +0000194
edisonn@google.com222382b2013-07-10 22:33:10 +0000195#ifdef PDF_DEBUG_3X
edisonn@google.comac03d912013-07-22 15:36:39 +0000196 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height));
edisonn@google.com222382b2013-07-10 22:33:10 +0000197#else
edisonn@google.comac03d912013-07-22 15:36:39 +0000198 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
edisonn@google.com222382b2013-07-10 22:33:10 +0000199#endif
edisonn@google.com768bc6a2013-08-08 12:42:13 +0000200 SkAutoTUnref<SkDevice> device;
201 if (strcmp(FLAGS_config[0], "8888") == 0) {
202 device.reset(SkNEW_ARGS(SkDevice, (bitmap)));
203 }
204#if SK_SUPPORT_GPU
205 else if (strcmp(FLAGS_config[0], "gpu") == 0) {
206 SkAutoTUnref<GrSurface> target;
207 GrContext* gr = gContextFactory.get(GrContextFactory::kNative_GLContextType);
208 if (gr) {
209 // create a render target to back the device
210 GrTextureDesc desc;
211 desc.fConfig = kSkia8888_GrPixelConfig;
212 desc.fFlags = kRenderTarget_GrTextureFlagBit;
213 desc.fWidth = width;
214 desc.fHeight = height;
215 desc.fSampleCnt = 0;
216 target.reset(gr->createUncachedTexture(desc, NULL, 0));
217 }
218 if (NULL == target.get()) {
219 SkASSERT(0);
220 return false;
221 }
222
223 device.reset(SkGpuDevice::Create(target));
224 }
225#endif
226 else {
227 SkDebugf("unknown --config: %s\n", FLAGS_config[0]);
228 return false;
229 }
edisonn@google.comac03d912013-07-22 15:36:39 +0000230 SkCanvas canvas(device);
edisonn@google.com222382b2013-07-10 22:33:10 +0000231
edisonn@google.comac03d912013-07-22 15:36:39 +0000232 gDumpBitmap = &bitmap;
edisonn@google.com222382b2013-07-10 22:33:10 +0000233
edisonn@google.comac03d912013-07-22 15:36:39 +0000234 gDumpCanvas = &canvas;
235 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
edisonn@google.com222382b2013-07-10 22:33:10 +0000236
edisonn@google.comac03d912013-07-22 15:36:39 +0000237 SkString outputPath;
238 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
239 return false;
240 }
241 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
242
243 if (FLAGS_showMemoryUsage) {
244 SkDebugf("Memory usage after page %i rendered: %u\n", page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
245 }
edisonn@google.com13102382013-07-11 14:50:12 +0000246 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000247 return true;
248}
edisonn@google.com222382b2013-07-10 22:33:10 +0000249
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000250/** Reads an skp file, renders it to pdf and writes the output to a pdf file
251 * @param inputPath The skp file to be read.
252 * @param outputDir Output dir.
253 * @param renderer The object responsible to render the skp object into pdf.
254 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000255static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000256 SkPdfRenderer& renderer) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000257 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
258
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000259 SkString inputFilename;
edisonn@google.com147adb12013-07-24 15:56:19 +0000260 get_basename(&inputFilename, inputPath);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000261
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000262 bool success = true;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000263
edisonn@google.com222382b2013-07-10 22:33:10 +0000264 success = renderer.load(inputPath);
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000265 if (FLAGS_showMemoryUsage) {
266 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
267 }
268
269 // TODO(edisonn): bench timers
270 if (FLAGS_benchLoad > 0) {
271 for (int i = 0 ; i < FLAGS_benchLoad; i++) {
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000272 success = renderer.load(inputPath) && success;
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000273 if (FLAGS_showMemoryUsage) {
274 SkDebugf("Memory usage after load %i number : %u\n", i, (unsigned int)renderer.bytesUsed());
275 }
276 }
277 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000278
edisonn@google.com222382b2013-07-10 22:33:10 +0000279 if (success) {
280 if (!renderer.pages())
281 {
282 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
283 return false;
284 } else {
edisonn@google.com6a9d4362013-07-11 16:25:51 +0000285 for (int i = 0; i < FLAGS_benchRender + 1; i++) {
286 // TODO(edisonn) if (i == 1) start timer
287 if (strcmp(FLAGS_pages[0], "all") == 0) {
288 for (int pn = 0; pn < renderer.pages(); ++pn) {
289 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
290 }
291 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
292 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
293 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
294 }
295 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
296 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success;
297 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
298 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
299 } else {
300 int pn = atoi(FLAGS_pages[0]);
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000301 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000302 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000303 }
304 }
305 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000306
edisonn@google.com2273f9b2013-08-06 21:48:44 +0000307 if (!success) {
308 SkDebugf("Failures for file %s\n", inputPath.c_str());
309 }
310
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000311 return success;
312}
313
314/** For each file in the directory or for the file passed in input, call
315 * parse_pdf.
316 * @param input A directory or an pdf file.
317 * @param outputDir Output dir.
318 * @param renderer The object responsible to render the skp object into pdf.
319 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000320static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000321 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000322 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000323 if (sk_isdir(input)) {
324 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000325 SkString inputFilename;
326 while (iter.next(&inputFilename)) {
327 SkString inputPath;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000328 SkString _input;
329 _input.append(input);
edisonn@google.com147adb12013-07-24 15:56:19 +0000330 make_filepath(&inputPath, _input, inputFilename);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000331 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000332 ++failures;
333 }
334 }
335 } else {
336 SkString inputPath(input);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000337 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000338 ++failures;
339 }
340 }
341 return failures;
342}
343
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000344int tool_main(int argc, char** argv);
345int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000346 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
347 SkCommandLineFlags::Parse(argc, argv);
348
349 if (FLAGS_readPath.isEmpty()) {
350 SkDebugf(".pdf files or directories are required.\n");
351 exit(-1);
352 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000353
edisonn@google.com222382b2013-07-10 22:33:10 +0000354 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000355
356 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000357 if (FLAGS_writePath.count() == 1) {
358 outputDir.set(FLAGS_writePath[0]);
359 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000360
361 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000362 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000363 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000364 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000365 }
366
367 reportPdfRenderStats();
368
369 if (failures != 0) {
370 SkDebugf("Failed to render %i PDFs.\n", failures);
371 return 1;
372 }
373
374 return 0;
375}
376
377#if !defined SK_BUILD_FOR_IOS
378int main(int argc, char * const argv[]) {
379 return tool_main(argc, (char**) argv);
380}
381#endif