blob: 7bff14a92e31de8bc439b5fc5da9ea82098f7beb [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).");
28// TODO(edisonn): add config for device target(gpu, raster, pdf), + ability not to render at all
29// TODO(edisonn): add ability to do the op N times, bench (either load N times, render n times or load + render n times)
edisonn@google.com7b328fd2013-07-11 12:53:06 +000030
edisonn@google.com01cd4d52013-06-10 20:44:45 +000031/**
32 * Given list of directories and files to use as input, expects to find .pdf
33 * files and it will convert them to .png files writing them in the same directory
34 * one file for each page.
35 *
36 * Returns zero exit code if all .pdf files were converted successfully,
37 * otherwise returns error code 1.
38 */
39
40static const char PDF_FILE_EXTENSION[] = "pdf";
41static const char PNG_FILE_EXTENSION[] = "png";
42
edisonn@google.com01cd4d52013-06-10 20:44:45 +000043/** Replaces the extension of a file.
44 * @param path File name whose extension will be changed.
45 * @param old_extension The old extension.
46 * @param new_extension The new extension.
47 * @returns false if the file did not has the expected extension.
48 * if false is returned, contents of path are undefined.
49 */
edisonn@google.com222382b2013-07-10 22:33:10 +000050static bool add_page_and_replace_filename_extension(SkString* path, int page,
edisonn@google.com01cd4d52013-06-10 20:44:45 +000051 const char old_extension[],
52 const char new_extension[]) {
53 if (path->endsWith(old_extension)) {
54 path->remove(path->size() - strlen(old_extension),
55 strlen(old_extension));
56 if (!path->endsWith(".")) {
57 return false;
58 }
edisonn@google.com222382b2013-07-10 22:33:10 +000059 if (page >= 0) {
60 path->appendf("%i.", page);
61 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +000062 path->append(new_extension);
63 return true;
64 }
65 return false;
66}
edisonn@google.com222382b2013-07-10 22:33:10 +000067
edisonn@google.com01cd4d52013-06-10 20:44:45 +000068/** Builds the output filename. path = dir/name, and it replaces expected
69 * .skp extension with .pdf extention.
70 * @param path Output filename.
71 * @param name The name of the file.
72 * @returns false if the file did not has the expected extension.
73 * if false is returned, contents of path are undefined.
74 */
edisonn@google.com596d2e22013-07-10 17:44:55 +000075
edisonn@google.com222382b2013-07-10 22:33:10 +000076
edisonn@google.com01cd4d52013-06-10 20:44:45 +000077static bool make_output_filepath(SkString* path, const SkString& dir,
edisonn@google.com222382b2013-07-10 22:33:10 +000078 const SkString& name,
79 int page) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +000080 sk_tools::make_filepath(path, dir, name);
edisonn@google.com222382b2013-07-10 22:33:10 +000081 return add_page_and_replace_filename_extension(path, page,
82 PDF_FILE_EXTENSION,
83 PNG_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +000084}
edisonn@google.com222382b2013-07-10 22:33:10 +000085
86static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
87 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
88
89 bitmap->allocPixels();
90 bitmap->eraseColor(color);
91}
92
edisonn@google.com01cd4d52013-06-10 20:44:45 +000093/** Write the output of pdf renderer to a file.
94 * @param outputDir Output dir.
95 * @param inputFilename The skp file that was read.
96 * @param renderer The object responsible to write the pdf file.
edisonn@google.comcdad30b2013-07-10 22:37:38 +000097 * @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 +000098 */
edisonn@google.com222382b2013-07-10 22:33:10 +000099
edisonn@google.com13102382013-07-11 14:50:12 +0000100extern "C" SkBitmap* gDumpBitmap;
101extern "C" SkCanvas* gDumpCanvas;
102
edisonn@google.com222382b2013-07-10 22:33:10 +0000103static bool render_page(const SkString& outputDir,
edisonn@google.com444e25a2013-07-11 15:20:50 +0000104 const SkString& inputFilename,
105 const SkPdfRenderer& renderer,
106 int page) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000107 SkRect rect = renderer.MediaBox(page < 0 ? 0 :page);
108
109 SkBitmap bitmap;
edisonn@google.com444e25a2013-07-11 15:20:50 +0000110 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0)));
111 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(FLAGS_DPI / 72.0)));
112
113 rect = SkRect::MakeWH(width, height);
114
edisonn@google.com222382b2013-07-10 22:33:10 +0000115#ifdef PDF_DEBUG_3X
edisonn@google.com444e25a2013-07-11 15:20:50 +0000116 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScalarToDouble(height));
edisonn@google.com222382b2013-07-10 22:33:10 +0000117#else
edisonn@google.com444e25a2013-07-11 15:20:50 +0000118 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDouble(height));
edisonn@google.com222382b2013-07-10 22:33:10 +0000119#endif
120 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
121 SkCanvas canvas(device);
122
123 gDumpBitmap = &bitmap;
124
125 gDumpCanvas = &canvas;
edisonn@google.com444e25a2013-07-11 15:20:50 +0000126 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect);
edisonn@google.com222382b2013-07-10 22:33:10 +0000127
edisonn@google.com13102382013-07-11 14:50:12 +0000128 SkString outputPath;
129 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
130 return false;
131 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000132 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000133
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000134 if (FLAGS_showMemoryUsage) {
135 SkDebugf("Memory usage after page %i rendered: %u\n", page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed());
136 }
137
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000138 return true;
139}
edisonn@google.com222382b2013-07-10 22:33:10 +0000140
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000141/** Reads an skp file, renders it to pdf and writes the output to a pdf file
142 * @param inputPath The skp file to be read.
143 * @param outputDir Output dir.
144 * @param renderer The object responsible to render the skp object into pdf.
145 */
edisonn@google.com222382b2013-07-10 22:33:10 +0000146static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000147 SkPdfRenderer& renderer) {
edisonn@google.com222382b2013-07-10 22:33:10 +0000148 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
149
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000150 SkString inputFilename;
151 sk_tools::get_basename(&inputFilename, inputPath);
152
153 SkFILEStream inputStream;
154 inputStream.setPath(inputPath.c_str());
155 if (!inputStream.isValid()) {
156 SkDebugf("Could not open file %s\n", inputPath.c_str());
157 return false;
158 }
159
160 bool success = false;
161
edisonn@google.com222382b2013-07-10 22:33:10 +0000162 success = renderer.load(inputPath);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000163
edisonn@google.com222382b2013-07-10 22:33:10 +0000164 if (success) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000165 if (FLAGS_showMemoryUsage) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000166 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
167 }
edisonn@google.com222382b2013-07-10 22:33:10 +0000168 if (!renderer.pages())
169 {
170 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
171 return false;
172 } else {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000173 if (strcmp(FLAGS_pages[0], "all") == 0) {
174 for (int pn = 0; pn < renderer.pages(); ++pn) {
175 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
176 }
177 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) {
178 for (int pn = renderer.pages() - 1; pn >= 0; --pn) {
179 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success;
180 }
181 } else if (strcmp(FLAGS_pages[0], "first") == 0) {
182 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success;
183 } else if (strcmp(FLAGS_pages[0], "last") == 0) {
184 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && success;
185 } else {
186 int pn = atoi(FLAGS_pages[0]);
187 success = render_page(outputDir, inputFilename, renderer, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) && pn;
edisonn@google.com222382b2013-07-10 22:33:10 +0000188 }
189 }
190 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000191
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000192 return success;
193}
194
195/** For each file in the directory or for the file passed in input, call
196 * parse_pdf.
197 * @param input A directory or an pdf file.
198 * @param outputDir Output dir.
199 * @param renderer The object responsible to render the skp object into pdf.
200 */
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000201static int process_input(const char* input, const SkString& outputDir,
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000202 SkPdfRenderer& renderer) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000203 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000204 if (sk_isdir(input)) {
205 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000206 SkString inputFilename;
207 while (iter.next(&inputFilename)) {
208 SkString inputPath;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000209 SkString _input;
210 _input.append(input);
211 sk_tools::make_filepath(&inputPath, _input, inputFilename);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000212 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000213 ++failures;
214 }
215 }
216 } else {
217 SkString inputPath(input);
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000218 if (!process_pdf(inputPath, outputDir, renderer)) {
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000219 ++failures;
220 }
221 }
222 return failures;
223}
224
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000225int tool_main(int argc, char** argv);
226int tool_main(int argc, char** argv) {
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000227 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
228 SkCommandLineFlags::Parse(argc, argv);
229
230 if (FLAGS_readPath.isEmpty()) {
231 SkDebugf(".pdf files or directories are required.\n");
232 exit(-1);
233 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000234
edisonn@google.com222382b2013-07-10 22:33:10 +0000235 SkPdfRenderer renderer;
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000236
237 SkString outputDir;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000238 if (FLAGS_writePath.count() == 1) {
239 outputDir.set(FLAGS_writePath[0]);
240 }
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000241
242 int failures = 0;
edisonn@google.coma5aaa792013-07-11 12:27:21 +0000243 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
edisonn@google.com7b328fd2013-07-11 12:53:06 +0000244 failures += process_input(FLAGS_readPath[i], outputDir, renderer);
edisonn@google.com222382b2013-07-10 22:33:10 +0000245 renderer.unload();
edisonn@google.com01cd4d52013-06-10 20:44:45 +0000246 }
247
248 reportPdfRenderStats();
249
250 if (failures != 0) {
251 SkDebugf("Failed to render %i PDFs.\n", failures);
252 return 1;
253 }
254
255 return 0;
256}
257
258#if !defined SK_BUILD_FOR_IOS
259int main(int argc, char * const argv[]) {
260 return tool_main(argc, (char**) argv);
261}
262#endif