blob: 0c002a8c63c7749d9a83a36fd54f179d64c915f7 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <limits.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
Jane Liu57f228d2017-07-13 18:10:30 -040010#include <bitset>
Lei Zhang9a25ede2017-06-22 00:05:12 -070011#include <iterator>
tonikitoo3e981582016-08-26 08:37:10 -070012#include <map>
Tom Sepezbfa2a972017-07-24 11:38:31 -070013#include <memory>
Tom Sepezdaa2e842015-01-29 15:44:37 -080014#include <sstream>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015#include <string>
16#include <utility>
Tom Sepez5ee12d72014-12-17 16:24:01 -080017#include <vector>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
Cary Clark399be5b2016-03-14 16:51:29 -040019#if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_
20#define _SKIA_SUPPORT_
21#endif
22
Tom Sepez1d17a042017-03-16 13:22:47 -070023#include "public/cpp/fpdf_deleters.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040024#include "public/fpdf_annot.h"
Jane Liu4442d452017-07-19 10:19:42 -040025#include "public/fpdf_attachment.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080026#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080027#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080028#include "public/fpdf_ext.h"
29#include "public/fpdf_formfill.h"
Dan Sinclairaeadad12017-07-18 16:43:41 -040030#include "public/fpdf_progressive.h"
Dan Sinclairddcb6e72017-04-05 10:30:33 -040031#include "public/fpdf_structtree.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080032#include "public/fpdf_text.h"
33#include "public/fpdfview.h"
Lei Zhang143959d2017-06-22 12:20:58 -070034#include "testing/image_diff/image_diff_png.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080035#include "testing/test_support.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040036#include "third_party/base/logging.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070037
thestigf2b940c2016-10-13 06:48:47 -070038#ifdef _WIN32
39#include <io.h>
40#else
41#include <unistd.h>
42#endif
43
Henrique Nakashima95ea7782017-07-11 16:42:43 -040044#ifdef ENABLE_CALLGRIND
45#include <valgrind/callgrind.h>
46#endif // ENABLE_CALLGRIND
47
Tom Sepez452b4f32015-10-13 09:27:27 -070048#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080049#include "v8/include/libplatform/libplatform.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -070050#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080051#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
Cary Clark399be5b2016-03-14 16:51:29 -040053#ifdef PDF_ENABLE_SKIA
54#include "third_party/skia/include/core/SkPictureRecorder.h"
55#include "third_party/skia/include/core/SkStream.h"
56#endif
57
thestigf2b940c2016-10-13 06:48:47 -070058#ifdef _WIN32
59#define access _access
60#define snprintf _snprintf
61#define R_OK 4
62#endif
63
Vitaly Buka9e0177a2014-07-22 18:15:42 -070064enum OutputFormat {
65 OUTPUT_NONE,
Dan Sinclair3c67fbd2017-04-05 16:07:50 -040066 OUTPUT_STRUCTURE,
dsinclairb63068f2016-06-16 07:58:09 -070067 OUTPUT_TEXT,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070068 OUTPUT_PPM,
Tom Sepezaf18cb32015-02-05 15:06:01 -080069 OUTPUT_PNG,
Jane Liu4fd9a472017-06-01 18:56:09 -040070 OUTPUT_ANNOT,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070071#ifdef _WIN32
72 OUTPUT_BMP,
73 OUTPUT_EMF,
Lei Zhangd1c9b452017-05-05 17:21:36 -070074 OUTPUT_PS2,
75 OUTPUT_PS3,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070076#endif
Cary Clark399be5b2016-03-14 16:51:29 -040077#ifdef PDF_ENABLE_SKIA
78 OUTPUT_SKP,
79#endif
Vitaly Buka9e0177a2014-07-22 18:15:42 -070080};
81
Lei Zhangd2be6462017-07-21 14:31:21 -070082namespace {
83
Tom Sepez5ee12d72014-12-17 16:24:01 -080084struct Options {
tsepezf09bdfa2016-04-18 16:08:26 -070085 Options()
Dan Sinclair3c67fbd2017-04-05 16:07:50 -040086 : show_config(false),
Henrique Nakashimab73ce7b2017-06-19 16:04:34 -040087 show_metadata(false),
npme3c73152016-11-14 09:14:52 -080088 send_events(false),
Dan Sinclairaeadad12017-07-18 16:43:41 -040089 render_oneshot(false),
Jane Liu4442d452017-07-19 10:19:42 -040090 save_attachments(false),
Jane Liub35dbad2017-08-03 16:29:17 -040091 save_images(false),
Henrique Nakashima95ea7782017-07-11 16:42:43 -040092#ifdef ENABLE_CALLGRIND
93 callgrind_delimiters(false),
94#endif // ENABLE_CALLGRIND
npme3c73152016-11-14 09:14:52 -080095 pages(false),
stephanafa05e972017-01-02 06:19:41 -080096 md5(false),
Henrique Nakashima95ea7782017-07-11 16:42:43 -040097 output_format(OUTPUT_NONE) {
98 }
Tom Sepez5ee12d72014-12-17 16:24:01 -080099
Tom Sepez2991d8d2016-01-15 16:02:48 -0800100 bool show_config;
Henrique Nakashimab73ce7b2017-06-19 16:04:34 -0400101 bool show_metadata;
tsepezf09bdfa2016-04-18 16:08:26 -0700102 bool send_events;
Dan Sinclairaeadad12017-07-18 16:43:41 -0400103 bool render_oneshot;
Jane Liu4442d452017-07-19 10:19:42 -0400104 bool save_attachments;
Jane Liub35dbad2017-08-03 16:29:17 -0400105 bool save_images;
Henrique Nakashima95ea7782017-07-11 16:42:43 -0400106#ifdef ENABLE_CALLGRIND
107 bool callgrind_delimiters;
108#endif // ENABLE_CALLGRIND
npme3c73152016-11-14 09:14:52 -0800109 bool pages;
stephanafa05e972017-01-02 06:19:41 -0800110 bool md5;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800111 OutputFormat output_format;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800112 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800113 std::string exe_path;
114 std::string bin_directory;
Lei Zhang6f62d532015-09-23 15:31:44 -0700115 std::string font_directory;
npmfa20cd52016-11-14 13:33:40 -0800116 // 0-based page numbers to be rendered.
117 int first_page;
118 int last_page;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800119};
120
tonikitoo81d92f82016-09-21 12:44:56 -0700121struct FPDF_FORMFILLINFO_PDFiumTest : public FPDF_FORMFILLINFO {
122 // Hold a map of the currently loaded pages in order to avoid them
123 // to get loaded twice.
Tom Sepezbfa2a972017-07-24 11:38:31 -0700124 std::map<int, std::unique_ptr<void, FPDFPageDeleter>> loaded_pages;
tonikitoo81d92f82016-09-21 12:44:56 -0700125
126 // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can
127 // make use of it.
npmfa20cd52016-11-14 13:33:40 -0800128 FPDF_FORMHANDLE form_handle;
tonikitoo81d92f82016-09-21 12:44:56 -0700129};
130
Lei Zhangd2be6462017-07-21 14:31:21 -0700131FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo(
npmfa20cd52016-11-14 13:33:40 -0800132 FPDF_FORMFILLINFO* form_fill_info) {
133 return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(form_fill_info);
tonikitoo81d92f82016-09-21 12:44:56 -0700134}
135
Lei Zhangd2be6462017-07-21 14:31:21 -0700136bool CheckDimensions(int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800137 if (stride < 0 || width < 0 || height < 0)
138 return false;
139 if (height > 0 && width > INT_MAX / height)
140 return false;
141 return true;
142}
143
Lei Zhangd2be6462017-07-21 14:31:21 -0700144void OutputMD5Hash(const char* file_name, const char* buffer, int len) {
stephanafa05e972017-01-02 06:19:41 -0800145 // Get the MD5 hash and write it to stdout.
Lei Zhang143959d2017-06-22 12:20:58 -0700146 std::string hash =
147 GenerateMD5Base16(reinterpret_cast<const uint8_t*>(buffer), len);
148 printf("MD5:%s:%s\n", file_name, hash.c_str());
stephanafa05e972017-01-02 06:19:41 -0800149}
150
Lei Zhangd2be6462017-07-21 14:31:21 -0700151std::string WritePpm(const char* pdf_name,
152 int num,
153 const void* buffer_void,
154 int stride,
155 int width,
156 int height) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700157 const char* buffer = reinterpret_cast<const char*>(buffer_void);
158
Tom Sepezaf18cb32015-02-05 15:06:01 -0800159 if (!CheckDimensions(stride, width, height))
stephanafa05e972017-01-02 06:19:41 -0800160 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800161
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162 int out_len = width * height;
163 if (out_len > INT_MAX / 3)
stephanafa05e972017-01-02 06:19:41 -0800164 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165 out_len *= 3;
166
167 char filename[256];
168 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700169 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170 if (!fp)
stephanafa05e972017-01-02 06:19:41 -0800171 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
173 // Source data is B, G, R, unused.
174 // Dest data is R, G, B.
thestig514e8c92016-07-15 17:57:54 -0700175 std::vector<char> result(out_len);
Lei Zhange00660b2015-08-13 15:40:18 -0700176 for (int h = 0; h < height; ++h) {
177 const char* src_line = buffer + (stride * h);
thestig514e8c92016-07-15 17:57:54 -0700178 char* dest_line = result.data() + (width * h * 3);
Lei Zhange00660b2015-08-13 15:40:18 -0700179 for (int w = 0; w < width; ++w) {
180 // R
181 dest_line[w * 3] = src_line[(w * 4) + 2];
182 // G
183 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
184 // B
185 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187 }
Lei Zhangd2be6462017-07-21 14:31:21 -0700188 if (fwrite(result.data(), out_len, 1, fp) != 1)
189 fprintf(stderr, "Failed to write to %s\n", filename);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190 fclose(fp);
stephanafa05e972017-01-02 06:19:41 -0800191 return std::string(filename);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192}
193
dsinclairb63068f2016-06-16 07:58:09 -0700194void WriteText(FPDF_PAGE page, const char* pdf_name, int num) {
195 char filename[256];
196 int chars_formatted =
197 snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num);
198 if (chars_formatted < 0 ||
199 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
200 fprintf(stderr, "Filename %s is too long\n", filename);
201 return;
202 }
203
204 FILE* fp = fopen(filename, "w");
205 if (!fp) {
206 fprintf(stderr, "Failed to open %s for output\n", filename);
207 return;
208 }
209
210 // Output in UTF32-LE.
211 uint32_t bom = 0x0000FEFF;
Lei Zhangd2be6462017-07-21 14:31:21 -0700212 if (fwrite(&bom, sizeof(bom), 1, fp) != 1) {
213 fprintf(stderr, "Failed to write to %s\n", filename);
214 (void)fclose(fp);
215 return;
216 }
dsinclairb63068f2016-06-16 07:58:09 -0700217
Tom Sepez1d17a042017-03-16 13:22:47 -0700218 std::unique_ptr<void, FPDFTextPageDeleter> textpage(FPDFText_LoadPage(page));
219 for (int i = 0; i < FPDFText_CountChars(textpage.get()); i++) {
220 uint32_t c = FPDFText_GetUnicode(textpage.get(), i);
Lei Zhangd2be6462017-07-21 14:31:21 -0700221 if (fwrite(&c, sizeof(c), 1, fp) != 1) {
222 fprintf(stderr, "Failed to write to %s\n", filename);
223 break;
224 }
dsinclairb63068f2016-06-16 07:58:09 -0700225 }
dsinclairb63068f2016-06-16 07:58:09 -0700226 (void)fclose(fp);
227}
228
Lei Zhangd2be6462017-07-21 14:31:21 -0700229const char* AnnotSubtypeToCString(FPDF_ANNOTATION_SUBTYPE subtype) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400230 if (subtype == FPDF_ANNOT_TEXT)
231 return "Text";
232 if (subtype == FPDF_ANNOT_LINK)
233 return "Link";
234 if (subtype == FPDF_ANNOT_FREETEXT)
235 return "FreeText";
236 if (subtype == FPDF_ANNOT_LINE)
237 return "Line";
238 if (subtype == FPDF_ANNOT_SQUARE)
239 return "Square";
240 if (subtype == FPDF_ANNOT_CIRCLE)
241 return "Circle";
242 if (subtype == FPDF_ANNOT_POLYGON)
243 return "Polygon";
244 if (subtype == FPDF_ANNOT_POLYLINE)
245 return "PolyLine";
246 if (subtype == FPDF_ANNOT_HIGHLIGHT)
247 return "Highlight";
248 if (subtype == FPDF_ANNOT_UNDERLINE)
249 return "Underline";
250 if (subtype == FPDF_ANNOT_SQUIGGLY)
251 return "Squiggly";
252 if (subtype == FPDF_ANNOT_STRIKEOUT)
253 return "StrikeOut";
254 if (subtype == FPDF_ANNOT_STAMP)
255 return "Stamp";
256 if (subtype == FPDF_ANNOT_CARET)
257 return "Caret";
258 if (subtype == FPDF_ANNOT_INK)
259 return "Ink";
260 if (subtype == FPDF_ANNOT_POPUP)
261 return "Popup";
262 if (subtype == FPDF_ANNOT_FILEATTACHMENT)
263 return "FileAttachment";
264 if (subtype == FPDF_ANNOT_SOUND)
265 return "Sound";
266 if (subtype == FPDF_ANNOT_MOVIE)
267 return "Movie";
268 if (subtype == FPDF_ANNOT_WIDGET)
269 return "Widget";
270 if (subtype == FPDF_ANNOT_SCREEN)
271 return "Screen";
272 if (subtype == FPDF_ANNOT_PRINTERMARK)
273 return "PrinterMark";
274 if (subtype == FPDF_ANNOT_TRAPNET)
275 return "TrapNet";
276 if (subtype == FPDF_ANNOT_WATERMARK)
277 return "Watermark";
278 if (subtype == FPDF_ANNOT_THREED)
279 return "3D";
280 if (subtype == FPDF_ANNOT_RICHMEDIA)
281 return "RichMedia";
282 if (subtype == FPDF_ANNOT_XFAWIDGET)
283 return "XFAWidget";
284 NOTREACHED();
285 return "";
286}
287
Lei Zhangd2be6462017-07-21 14:31:21 -0700288void AppendFlagString(const char* flag, std::string* output) {
289 if (!output->empty())
290 *output += ", ";
291 *output += flag;
292}
293
Jane Liu57f228d2017-07-13 18:10:30 -0400294std::string AnnotFlagsToString(int flags) {
Lei Zhangd2be6462017-07-21 14:31:21 -0700295 std::string str;
Jane Liu57f228d2017-07-13 18:10:30 -0400296 if (flags & FPDF_ANNOT_FLAG_INVISIBLE)
Lei Zhangd2be6462017-07-21 14:31:21 -0700297 AppendFlagString("Invisible", &str);
Jane Liu57f228d2017-07-13 18:10:30 -0400298 if (flags & FPDF_ANNOT_FLAG_HIDDEN)
Lei Zhangd2be6462017-07-21 14:31:21 -0700299 AppendFlagString("Hidden", &str);
Jane Liu57f228d2017-07-13 18:10:30 -0400300 if (flags & FPDF_ANNOT_FLAG_PRINT)
Lei Zhangd2be6462017-07-21 14:31:21 -0700301 AppendFlagString("Print", &str);
Jane Liu57f228d2017-07-13 18:10:30 -0400302 if (flags & FPDF_ANNOT_FLAG_NOZOOM)
Lei Zhangd2be6462017-07-21 14:31:21 -0700303 AppendFlagString("NoZoom", &str);
Jane Liu57f228d2017-07-13 18:10:30 -0400304 if (flags & FPDF_ANNOT_FLAG_NOROTATE)
Lei Zhangd2be6462017-07-21 14:31:21 -0700305 AppendFlagString("NoRotate", &str);
Jane Liu57f228d2017-07-13 18:10:30 -0400306 if (flags & FPDF_ANNOT_FLAG_NOVIEW)
Lei Zhangd2be6462017-07-21 14:31:21 -0700307 AppendFlagString("NoView", &str);
Jane Liu57f228d2017-07-13 18:10:30 -0400308 if (flags & FPDF_ANNOT_FLAG_READONLY)
Lei Zhangd2be6462017-07-21 14:31:21 -0700309 AppendFlagString("ReadOnly", &str);
Jane Liu57f228d2017-07-13 18:10:30 -0400310 if (flags & FPDF_ANNOT_FLAG_LOCKED)
Lei Zhangd2be6462017-07-21 14:31:21 -0700311 AppendFlagString("Locked", &str);
Jane Liu57f228d2017-07-13 18:10:30 -0400312 if (flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW)
Lei Zhangd2be6462017-07-21 14:31:21 -0700313 AppendFlagString("ToggleNoView", &str);
Jane Liu57f228d2017-07-13 18:10:30 -0400314 return str;
315}
316
Lei Zhangd2be6462017-07-21 14:31:21 -0700317const char* PageObjectTypeToCString(int type) {
Jane Liu57f228d2017-07-13 18:10:30 -0400318 if (type == FPDF_PAGEOBJ_TEXT)
319 return "Text";
320 if (type == FPDF_PAGEOBJ_PATH)
321 return "Path";
322 if (type == FPDF_PAGEOBJ_IMAGE)
323 return "Image";
324 if (type == FPDF_PAGEOBJ_SHADING)
325 return "Shading";
326 if (type == FPDF_PAGEOBJ_FORM)
327 return "Form";
328 NOTREACHED();
329 return "";
330}
331
Jane Liu4fd9a472017-06-01 18:56:09 -0400332void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) {
333 // Open the output text file.
334 char filename[256];
335 int chars_formatted =
336 snprintf(filename, sizeof(filename), "%s.%d.annot.txt", pdf_name, num);
337 if (chars_formatted < 0 ||
338 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
339 fprintf(stderr, "Filename %s is too long\n", filename);
340 return;
341 }
342 FILE* fp = fopen(filename, "w");
343 if (!fp) {
344 fprintf(stderr, "Failed to open %s for output\n", filename);
345 return;
346 }
347
348 int annot_count = FPDFPage_GetAnnotCount(page);
349 fprintf(fp, "Number of annotations: %d\n\n", annot_count);
350
351 // Iterate through all annotations on this page.
Jane Liu57f228d2017-07-13 18:10:30 -0400352 for (int i = 0; i < annot_count; ++i) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400353 // Retrieve the annotation object and its subtype.
354 fprintf(fp, "Annotation #%d:\n", i + 1);
Jane Liud60e9ad2017-06-26 11:28:36 -0400355 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, i);
356 if (!annot) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400357 fprintf(fp, "Failed to retrieve annotation!\n\n");
358 continue;
359 }
360 FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot);
Lei Zhangd2be6462017-07-21 14:31:21 -0700361 fprintf(fp, "Subtype: %s\n", AnnotSubtypeToCString(subtype));
Jane Liu4fd9a472017-06-01 18:56:09 -0400362
Jane Liu57f228d2017-07-13 18:10:30 -0400363 // Retrieve the annotation flags.
364 fprintf(fp, "Flags set: %s\n",
365 AnnotFlagsToString(FPDFAnnot_GetFlags(annot)).c_str());
366
367 // Retrieve the annotation's object count and object types.
368 const int obj_count = FPDFAnnot_GetObjectCount(annot);
369 fprintf(fp, "Number of objects: %d\n", obj_count);
370 if (obj_count > 0) {
371 fprintf(fp, "Object types: ");
372 for (int j = 0; j < obj_count; ++j) {
Lei Zhangd2be6462017-07-21 14:31:21 -0700373 const char* type = PageObjectTypeToCString(
374 FPDFPageObj_GetType(FPDFAnnot_GetObject(annot, j)));
375 fprintf(fp, "%s ", type);
Jane Liu57f228d2017-07-13 18:10:30 -0400376 }
377 fprintf(fp, "\n");
378 }
379
Jane Liu4fd9a472017-06-01 18:56:09 -0400380 // Retrieve the annotation's color and interior color.
381 unsigned int R;
382 unsigned int G;
383 unsigned int B;
384 unsigned int A;
385 if (!FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)) {
386 fprintf(fp, "Failed to retrieve color.\n");
387 } else {
388 fprintf(fp, "Color in RGBA: %d %d %d %d\n", R, G, B, A);
389 }
390 if (!FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_InteriorColor, &R, &G,
391 &B, &A)) {
392 fprintf(fp, "Failed to retrieve interior color.\n");
393 } else {
394 fprintf(fp, "Interior color in RGBA: %d %d %d %d\n", R, G, B, A);
395 }
396
397 // Retrieve the annotation's contents and author.
Lei Zhangdf064df2017-08-31 02:33:27 -0700398 static constexpr char kContentsKey[] = "Contents";
399 static constexpr char kAuthorKey[] = "T";
Jane Liu4fd9a472017-06-01 18:56:09 -0400400 unsigned long len =
Lei Zhangdf064df2017-08-31 02:33:27 -0700401 FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400402 std::vector<char> buf(len);
Lei Zhangdf064df2017-08-31 02:33:27 -0700403 FPDFAnnot_GetStringValue(annot, kContentsKey, buf.data(), len);
Jane Liu4fd9a472017-06-01 18:56:09 -0400404 fprintf(fp, "Content: %ls\n",
405 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
406 .c_str());
Lei Zhangdf064df2017-08-31 02:33:27 -0700407 len = FPDFAnnot_GetStringValue(annot, kAuthorKey, nullptr, 0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400408 buf.clear();
409 buf.resize(len);
Lei Zhangdf064df2017-08-31 02:33:27 -0700410 FPDFAnnot_GetStringValue(annot, kAuthorKey, buf.data(), len);
Jane Liu4fd9a472017-06-01 18:56:09 -0400411 fprintf(fp, "Author: %ls\n",
412 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
413 .c_str());
414
415 // Retrieve the annotation's quadpoints if it is a markup annotation.
Jane Liu4fd9a472017-06-01 18:56:09 -0400416 if (FPDFAnnot_HasAttachmentPoints(annot)) {
Jane Liu0c6b07d2017-08-15 10:50:22 -0400417 FS_QUADPOINTSF quadpoints;
418 if (FPDFAnnot_GetAttachmentPoints(annot, &quadpoints)) {
419 fprintf(fp,
420 "Quadpoints: (%.3f, %.3f), (%.3f, %.3f), (%.3f, %.3f), (%.3f, "
421 "%.3f)\n",
422 quadpoints.x1, quadpoints.y1, quadpoints.x2, quadpoints.y2,
423 quadpoints.x3, quadpoints.y3, quadpoints.x4, quadpoints.y4);
424 } else {
425 fprintf(fp, "Failed to retrieve quadpoints.\n");
426 }
Jane Liu4fd9a472017-06-01 18:56:09 -0400427 }
428
429 // Retrieve the annotation's rectangle coordinates.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400430 FS_RECTF rect;
431 if (FPDFAnnot_GetRect(annot, &rect)) {
432 fprintf(fp, "Rectangle: l - %.3f, b - %.3f, r - %.3f, t - %.3f\n\n",
433 rect.left, rect.bottom, rect.right, rect.top);
434 } else {
435 fprintf(fp, "Failed to retrieve annotation rectangle.\n");
436 }
Jane Liu57f228d2017-07-13 18:10:30 -0400437
438 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400439 }
440
441 (void)fclose(fp);
442}
443
Lei Zhangd2be6462017-07-21 14:31:21 -0700444std::string WritePng(const char* pdf_name,
445 int num,
446 const void* buffer_void,
447 int stride,
448 int width,
449 int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800450 if (!CheckDimensions(stride, width, height))
stephanafa05e972017-01-02 06:19:41 -0800451 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800452
453 std::vector<unsigned char> png_encoding;
454 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
455 if (!image_diff_png::EncodeBGRAPNG(
456 buffer, width, height, stride, false, &png_encoding)) {
457 fprintf(stderr, "Failed to convert bitmap to PNG\n");
stephanafa05e972017-01-02 06:19:41 -0800458 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800459 }
460
461 char filename[256];
462 int chars_formatted = snprintf(
463 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
464 if (chars_formatted < 0 ||
465 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
Cary Clark399be5b2016-03-14 16:51:29 -0400466 fprintf(stderr, "Filename %s is too long\n", filename);
stephanafa05e972017-01-02 06:19:41 -0800467 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800468 }
469
470 FILE* fp = fopen(filename, "wb");
471 if (!fp) {
472 fprintf(stderr, "Failed to open %s for output\n", filename);
stephanafa05e972017-01-02 06:19:41 -0800473 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800474 }
475
476 size_t bytes_written = fwrite(
477 &png_encoding.front(), 1, png_encoding.size(), fp);
478 if (bytes_written != png_encoding.size())
Lei Zhangd2be6462017-07-21 14:31:21 -0700479 fprintf(stderr, "Failed to write to %s\n", filename);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800480
Lei Zhang5377ebf2015-09-23 14:52:53 -0700481 (void)fclose(fp);
stephanafa05e972017-01-02 06:19:41 -0800482 return std::string(filename);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800483}
484
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700485#ifdef _WIN32
Lei Zhangd2be6462017-07-21 14:31:21 -0700486std::string WriteBmp(const char* pdf_name,
487 int num,
488 const void* buffer,
489 int stride,
490 int width,
491 int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800492 if (!CheckDimensions(stride, width, height))
stephanafa05e972017-01-02 06:19:41 -0800493 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800494
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700495 int out_len = stride * height;
496 if (out_len > INT_MAX / 3)
stephanafa05e972017-01-02 06:19:41 -0800497 return "";
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700498
499 char filename[256];
500 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
501 FILE* fp = fopen(filename, "wb");
502 if (!fp)
stephanafa05e972017-01-02 06:19:41 -0800503 return "";
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700504
Nico Weber2827bdd2015-07-01 14:08:08 -0700505 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700506 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
507 bmi.bmiHeader.biWidth = width;
508 bmi.bmiHeader.biHeight = -height; // top-down image
509 bmi.bmiHeader.biPlanes = 1;
510 bmi.bmiHeader.biBitCount = 32;
511 bmi.bmiHeader.biCompression = BI_RGB;
512 bmi.bmiHeader.biSizeImage = 0;
513
Nico Weber2827bdd2015-07-01 14:08:08 -0700514 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700515 file_header.bfType = 0x4d42;
516 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
517 file_header.bfOffBits = file_header.bfSize - out_len;
518
Lei Zhangd2be6462017-07-21 14:31:21 -0700519 if (fwrite(&file_header, sizeof(file_header), 1, fp) != 1 ||
520 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp) != 1 ||
521 fwrite(buffer, out_len, 1, fp) != 1) {
522 fprintf(stderr, "Failed to write to %s\n", filename);
523 }
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700524 fclose(fp);
stephanafa05e972017-01-02 06:19:41 -0800525 return std::string(filename);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700526}
527
528void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700529 char filename[256];
530 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
531
Lei Zhang5377ebf2015-09-23 14:52:53 -0700532 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800533
Lei Zhangd1c9b452017-05-05 17:21:36 -0700534 int width = static_cast<int>(FPDF_GetPageWidth(page));
535 int height = static_cast<int>(FPDF_GetPageHeight(page));
Tom Sepezaf18cb32015-02-05 15:06:01 -0800536 HRGN rgn = CreateRectRgn(0, 0, width, height);
537 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700538 DeleteObject(rgn);
539
540 SelectObject(dc, GetStockObject(NULL_PEN));
541 SelectObject(dc, GetStockObject(WHITE_BRUSH));
542 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
543 Rectangle(dc, 0, 0, width + 1, height + 1);
544
545 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
546 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
547
548 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
549}
Lei Zhangd1c9b452017-05-05 17:21:36 -0700550
551int CALLBACK EnhMetaFileProc(HDC hdc,
552 HANDLETABLE* handle_table,
553 const ENHMETARECORD* record,
554 int objects_count,
555 LPARAM param) {
556 std::vector<const ENHMETARECORD*>& items =
557 *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
558 items.push_back(record);
559 return 1;
560}
561
562void WritePS(FPDF_PAGE page, const char* pdf_name, int num) {
563 char filename[256];
564 snprintf(filename, sizeof(filename), "%s.%d.ps", pdf_name, num);
565 FILE* fp = fopen(filename, "wb");
566 if (!fp)
567 return;
568
569 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
570
571 int width = static_cast<int>(FPDF_GetPageWidth(page));
572 int height = static_cast<int>(FPDF_GetPageHeight(page));
573 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
574 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
575
576 HENHMETAFILE emf = CloseEnhMetaFile(dc);
577 std::vector<const ENHMETARECORD*> items;
578 EnumEnhMetaFile(nullptr, emf, &EnhMetaFileProc, &items, nullptr);
579 for (const ENHMETARECORD* record : items) {
580 if (record->iType != EMR_GDICOMMENT)
581 continue;
582
583 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
584 const char* data = reinterpret_cast<const char*>(comment->Data);
585 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
Lei Zhangd2be6462017-07-21 14:31:21 -0700586 if (fwrite(data + sizeof(uint16_t), size, 1, fp) != 1) {
587 fprintf(stderr, "Failed to write to %s\n", filename);
588 break;
589 }
Lei Zhangd1c9b452017-05-05 17:21:36 -0700590 }
591 fclose(fp);
592 DeleteEnhMetaFile(emf);
593}
594#endif // _WIN32
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700595
Cary Clark399be5b2016-03-14 16:51:29 -0400596#ifdef PDF_ENABLE_SKIA
Lei Zhangd2be6462017-07-21 14:31:21 -0700597std::string WriteSkp(const char* pdf_name,
598 int num,
599 SkPictureRecorder* recorder) {
Cary Clark399be5b2016-03-14 16:51:29 -0400600 char filename[256];
601 int chars_formatted =
602 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num);
603
604 if (chars_formatted < 0 ||
605 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
606 fprintf(stderr, "Filename %s is too long\n", filename);
stephanafa05e972017-01-02 06:19:41 -0800607 return "";
Cary Clark399be5b2016-03-14 16:51:29 -0400608 }
609
thestigb97e07e2016-09-28 22:16:40 -0700610 sk_sp<SkPicture> picture(recorder->finishRecordingAsPicture());
Cary Clark399be5b2016-03-14 16:51:29 -0400611 SkFILEWStream wStream(filename);
612 picture->serialize(&wStream);
stephanafa05e972017-01-02 06:19:41 -0800613 return std::string(filename);
Cary Clark399be5b2016-03-14 16:51:29 -0400614}
615#endif
616
Tom Sepez58fb36a2016-02-01 10:32:14 -0800617// These example JS platform callback handlers are entirely optional,
618// and exist here to show the flow of information from a document back
619// to the embedder.
Tom Sepezbd932572016-01-29 09:10:41 -0800620int ExampleAppAlert(IPDF_JSPLATFORM*,
621 FPDF_WIDESTRING msg,
622 FPDF_WIDESTRING title,
npmfa20cd52016-11-14 13:33:40 -0800623 int type,
624 int icon) {
Tom Sepezbd932572016-01-29 09:10:41 -0800625 printf("%ls", GetPlatformWString(title).c_str());
npmfa20cd52016-11-14 13:33:40 -0800626 if (icon || type)
627 printf("[icon=%d,type=%d]", icon, type);
Tom Sepezbd932572016-01-29 09:10:41 -0800628 printf(": %ls\n", GetPlatformWString(msg).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700629 return 0;
630}
631
Tom Sepez58fb36a2016-02-01 10:32:14 -0800632int ExampleAppResponse(IPDF_JSPLATFORM*,
633 FPDF_WIDESTRING question,
634 FPDF_WIDESTRING title,
npmfa20cd52016-11-14 13:33:40 -0800635 FPDF_WIDESTRING default_value,
Tom Sepez58fb36a2016-02-01 10:32:14 -0800636 FPDF_WIDESTRING label,
npmfa20cd52016-11-14 13:33:40 -0800637 FPDF_BOOL is_password,
Tom Sepez58fb36a2016-02-01 10:32:14 -0800638 void* response,
639 int length) {
640 printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
641 GetPlatformWString(title).c_str(),
642 GetPlatformWString(question).c_str(),
npmfa20cd52016-11-14 13:33:40 -0800643 GetPlatformWString(default_value).c_str(),
644 GetPlatformWString(label).c_str(), is_password, length);
Tom Sepez58fb36a2016-02-01 10:32:14 -0800645
646 // UTF-16, always LE regardless of platform.
647 uint8_t* ptr = static_cast<uint8_t*>(response);
648 ptr[0] = 'N';
649 ptr[1] = 0;
650 ptr[2] = 'o';
651 ptr[3] = 0;
652 return 4;
653}
654
npmfa20cd52016-11-14 13:33:40 -0800655void ExampleDocGotoPage(IPDF_JSPLATFORM*, int page_number) {
656 printf("Goto Page: %d\n", page_number);
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800657}
658
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800659void ExampleDocMail(IPDF_JSPLATFORM*,
660 void* mailData,
661 int length,
npmfa20cd52016-11-14 13:33:40 -0800662 FPDF_BOOL UI,
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800663 FPDF_WIDESTRING To,
664 FPDF_WIDESTRING Subject,
665 FPDF_WIDESTRING CC,
666 FPDF_WIDESTRING BCC,
667 FPDF_WIDESTRING Msg) {
npmfa20cd52016-11-14 13:33:40 -0800668 printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", UI,
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800669 GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
670 GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
671 GetPlatformWString(Msg).c_str());
672}
673
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800674void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700675 std::string feature = "Unknown";
676 switch (type) {
677 case FPDF_UNSP_DOC_XFAFORM:
678 feature = "XFA";
679 break;
680 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
681 feature = "Portfolios_Packages";
682 break;
683 case FPDF_UNSP_DOC_ATTACHMENT:
684 case FPDF_UNSP_ANNOT_ATTACHMENT:
685 feature = "Attachment";
686 break;
687 case FPDF_UNSP_DOC_SECURITY:
688 feature = "Rights_Management";
689 break;
690 case FPDF_UNSP_DOC_SHAREDREVIEW:
691 feature = "Shared_Review";
692 break;
693 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
694 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
695 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
696 feature = "Shared_Form";
697 break;
698 case FPDF_UNSP_ANNOT_3DANNOT:
699 feature = "3D";
700 break;
701 case FPDF_UNSP_ANNOT_MOVIE:
702 feature = "Movie";
703 break;
704 case FPDF_UNSP_ANNOT_SOUND:
705 feature = "Sound";
706 break;
707 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
708 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
709 feature = "Screen";
710 break;
711 case FPDF_UNSP_ANNOT_SIG:
712 feature = "Digital_Signature";
713 break;
714 }
715 printf("Unsupported feature: %s.\n", feature.c_str());
716}
717
Tom Sepez5ee12d72014-12-17 16:24:01 -0800718bool ParseCommandLine(const std::vector<std::string>& args,
thestig514e8c92016-07-15 17:57:54 -0700719 Options* options,
720 std::vector<std::string>* files) {
721 if (args.empty())
Tom Sepez5ee12d72014-12-17 16:24:01 -0800722 return false;
thestig514e8c92016-07-15 17:57:54 -0700723
Tom Sepez5ee12d72014-12-17 16:24:01 -0800724 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800725 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800726 for (; cur_idx < args.size(); ++cur_idx) {
727 const std::string& cur_arg = args[cur_idx];
Tom Sepez2991d8d2016-01-15 16:02:48 -0800728 if (cur_arg == "--show-config") {
729 options->show_config = true;
Henrique Nakashimab73ce7b2017-06-19 16:04:34 -0400730 } else if (cur_arg == "--show-metadata") {
731 options->show_metadata = true;
tsepezf09bdfa2016-04-18 16:08:26 -0700732 } else if (cur_arg == "--send-events") {
733 options->send_events = true;
Dan Sinclairaeadad12017-07-18 16:43:41 -0400734 } else if (cur_arg == "--render-oneshot") {
735 options->render_oneshot = true;
Jane Liu4442d452017-07-19 10:19:42 -0400736 } else if (cur_arg == "--save-attachments") {
737 options->save_attachments = true;
Jane Liub35dbad2017-08-03 16:29:17 -0400738 } else if (cur_arg == "--save-images") {
739 options->save_images = true;
Henrique Nakashima95ea7782017-07-11 16:42:43 -0400740#ifdef ENABLE_CALLGRIND
741 } else if (cur_arg == "--callgrind-delim") {
742 options->callgrind_delimiters = true;
743#endif // ENABLE_CALLGRIND
Tom Sepez2991d8d2016-01-15 16:02:48 -0800744 } else if (cur_arg == "--ppm") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800745 if (options->output_format != OUTPUT_NONE) {
746 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
747 return false;
748 }
749 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800750 } else if (cur_arg == "--png") {
751 if (options->output_format != OUTPUT_NONE) {
752 fprintf(stderr, "Duplicate or conflicting --png argument\n");
753 return false;
754 }
755 options->output_format = OUTPUT_PNG;
dsinclairb63068f2016-06-16 07:58:09 -0700756 } else if (cur_arg == "--txt") {
757 if (options->output_format != OUTPUT_NONE) {
758 fprintf(stderr, "Duplicate or conflicting --txt argument\n");
759 return false;
760 }
761 options->output_format = OUTPUT_TEXT;
Jane Liu4fd9a472017-06-01 18:56:09 -0400762 } else if (cur_arg == "--annot") {
763 if (options->output_format != OUTPUT_NONE) {
764 fprintf(stderr, "Duplicate or conflicting --annot argument\n");
765 return false;
766 }
767 options->output_format = OUTPUT_ANNOT;
Cary Clark399be5b2016-03-14 16:51:29 -0400768#ifdef PDF_ENABLE_SKIA
769 } else if (cur_arg == "--skp") {
770 if (options->output_format != OUTPUT_NONE) {
771 fprintf(stderr, "Duplicate or conflicting --skp argument\n");
772 return false;
773 }
774 options->output_format = OUTPUT_SKP;
775#endif
Lei Zhang6f62d532015-09-23 15:31:44 -0700776 } else if (cur_arg.size() > 11 &&
777 cur_arg.compare(0, 11, "--font-dir=") == 0) {
778 if (!options->font_directory.empty()) {
779 fprintf(stderr, "Duplicate --font-dir argument\n");
780 return false;
781 }
782 options->font_directory = cur_arg.substr(11);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700783#ifdef _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500784 } else if (cur_arg == "--emf") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800785 if (options->output_format != OUTPUT_NONE) {
786 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
787 return false;
788 }
789 options->output_format = OUTPUT_EMF;
Lei Zhangd1c9b452017-05-05 17:21:36 -0700790 } else if (cur_arg == "--ps2") {
791 if (options->output_format != OUTPUT_NONE) {
792 fprintf(stderr, "Duplicate or conflicting --ps2 argument\n");
793 return false;
794 }
795 options->output_format = OUTPUT_PS2;
796 } else if (cur_arg == "--ps3") {
797 if (options->output_format != OUTPUT_NONE) {
798 fprintf(stderr, "Duplicate or conflicting --ps3 argument\n");
799 return false;
800 }
801 options->output_format = OUTPUT_PS3;
Dan Sinclair50cce602016-02-24 09:51:16 -0500802 } else if (cur_arg == "--bmp") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800803 if (options->output_format != OUTPUT_NONE) {
804 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
805 return false;
806 }
807 options->output_format = OUTPUT_BMP;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800808#endif // _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500809
Tom Sepez452b4f32015-10-13 09:27:27 -0700810#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800811#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Dan Sinclair738b08c2016-03-01 14:45:20 -0500812 } else if (cur_arg.size() > 10 &&
813 cur_arg.compare(0, 10, "--bin-dir=") == 0) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800814 if (!options->bin_directory.empty()) {
815 fprintf(stderr, "Duplicate --bin-dir argument\n");
816 return false;
817 }
818 options->bin_directory = cur_arg.substr(10);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800819#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700820#endif // PDF_ENABLE_V8
Dan Sinclair738b08c2016-03-01 14:45:20 -0500821
822 } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800823 if (!options->scale_factor_as_string.empty()) {
824 fprintf(stderr, "Duplicate --scale argument\n");
825 return false;
826 }
827 options->scale_factor_as_string = cur_arg.substr(8);
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400828 } else if (cur_arg == "--show-structure") {
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400829 if (options->output_format != OUTPUT_NONE) {
830 fprintf(stderr, "Duplicate or conflicting --show-structure argument\n");
831 return false;
832 }
833 options->output_format = OUTPUT_STRUCTURE;
npme3c73152016-11-14 09:14:52 -0800834 } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--pages=") == 0) {
835 if (options->pages) {
836 fprintf(stderr, "Duplicate --pages argument\n");
837 return false;
838 }
839 options->pages = true;
npmfa20cd52016-11-14 13:33:40 -0800840 const std::string pages_string = cur_arg.substr(8);
841 size_t first_dash = pages_string.find("-");
842 if (first_dash == std::string::npos) {
843 std::stringstream(pages_string) >> options->first_page;
844 options->last_page = options->first_page;
npme3c73152016-11-14 09:14:52 -0800845 } else {
npmfa20cd52016-11-14 13:33:40 -0800846 std::stringstream(pages_string.substr(0, first_dash)) >>
847 options->first_page;
848 std::stringstream(pages_string.substr(first_dash + 1)) >>
849 options->last_page;
npme3c73152016-11-14 09:14:52 -0800850 }
stephanafa05e972017-01-02 06:19:41 -0800851 } else if (cur_arg == "--md5") {
852 options->md5 = true;
Tom Sepez2991d8d2016-01-15 16:02:48 -0800853 } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') {
854 fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str());
855 return false;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500856 } else {
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700857 break;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500858 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700859 }
thestig514e8c92016-07-15 17:57:54 -0700860 for (size_t i = cur_idx; i < args.size(); i++)
Tom Sepez5ee12d72014-12-17 16:24:01 -0800861 files->push_back(args[i]);
thestig514e8c92016-07-15 17:57:54 -0700862
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700863 return true;
864}
865
Lei Zhangd2be6462017-07-21 14:31:21 -0700866void PrintLastError() {
867 unsigned long err = FPDF_GetLastError();
868 fprintf(stderr, "Load pdf docs unsuccessful: ");
869 switch (err) {
870 case FPDF_ERR_SUCCESS:
871 fprintf(stderr, "Success");
872 break;
873 case FPDF_ERR_UNKNOWN:
874 fprintf(stderr, "Unknown error");
875 break;
876 case FPDF_ERR_FILE:
877 fprintf(stderr, "File not found or could not be opened");
878 break;
879 case FPDF_ERR_FORMAT:
880 fprintf(stderr, "File not in PDF format or corrupted");
881 break;
882 case FPDF_ERR_PASSWORD:
883 fprintf(stderr, "Password required or incorrect password");
884 break;
885 case FPDF_ERR_SECURITY:
886 fprintf(stderr, "Unsupported security scheme");
887 break;
888 case FPDF_ERR_PAGE:
889 fprintf(stderr, "Page not found or content error");
890 break;
891 default:
892 fprintf(stderr, "Unknown error %ld", err);
893 }
894 fprintf(stderr, ".\n");
895 return;
896}
897
npmfa20cd52016-11-14 13:33:40 -0800898FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* avail, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700899 return true;
900}
901
npmfa20cd52016-11-14 13:33:40 -0800902void Add_Segment(FX_DOWNLOADHINTS* hints, size_t offset, size_t size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700903
Lei Zhang54d91ec2017-06-16 19:08:02 -0700904void SendPageEvents(FPDF_FORMHANDLE form,
905 FPDF_PAGE page,
tsepezf09bdfa2016-04-18 16:08:26 -0700906 const std::string& events) {
907 auto lines = StringSplit(events, '\n');
908 for (auto line : lines) {
909 auto command = StringSplit(line, '#');
910 if (command[0].empty())
911 continue;
912 auto tokens = StringSplit(command[0], ',');
Nicolas Penae30f07a2017-05-18 18:37:46 -0400913 if (tokens[0] == "charcode") {
914 if (tokens.size() == 2) {
915 int keycode = atoi(tokens[1].c_str());
916 FORM_OnChar(form, page, keycode, 0);
917 } else {
918 fprintf(stderr, "charcode: bad args\n");
919 }
920 } else if (tokens[0] == "keycode") {
tsepezf09bdfa2016-04-18 16:08:26 -0700921 if (tokens.size() == 2) {
922 int keycode = atoi(tokens[1].c_str());
923 FORM_OnKeyDown(form, page, keycode, 0);
924 FORM_OnKeyUp(form, page, keycode, 0);
925 } else {
926 fprintf(stderr, "keycode: bad args\n");
927 }
928 } else if (tokens[0] == "mousedown") {
929 if (tokens.size() == 4) {
930 int x = atoi(tokens[2].c_str());
931 int y = atoi(tokens[3].c_str());
932 if (tokens[1] == "left")
933 FORM_OnLButtonDown(form, page, 0, x, y);
934#ifdef PDF_ENABLE_XFA
935 else if (tokens[1] == "right")
936 FORM_OnRButtonDown(form, page, 0, x, y);
937#endif
938 else
939 fprintf(stderr, "mousedown: bad button name\n");
940 } else {
941 fprintf(stderr, "mousedown: bad args\n");
942 }
943 } else if (tokens[0] == "mouseup") {
944 if (tokens.size() == 4) {
945 int x = atoi(tokens[2].c_str());
946 int y = atoi(tokens[3].c_str());
947 if (tokens[1] == "left")
948 FORM_OnLButtonUp(form, page, 0, x, y);
949#ifdef PDF_ENABLE_XFA
950 else if (tokens[1] == "right")
951 FORM_OnRButtonUp(form, page, 0, x, y);
952#endif
953 else
954 fprintf(stderr, "mouseup: bad button name\n");
955 } else {
956 fprintf(stderr, "mouseup: bad args\n");
957 }
958 } else if (tokens[0] == "mousemove") {
959 if (tokens.size() == 3) {
960 int x = atoi(tokens[1].c_str());
961 int y = atoi(tokens[2].c_str());
962 FORM_OnMouseMove(form, page, 0, x, y);
963 } else {
964 fprintf(stderr, "mousemove: bad args\n");
965 }
Lei Zhang63b01262017-08-31 08:54:46 -0700966 } else if (tokens[0] == "focus") {
967 if (tokens.size() == 3) {
968 int x = atoi(tokens[1].c_str());
969 int y = atoi(tokens[2].c_str());
970 FORM_OnFocus(form, page, 0, x, y);
971 } else {
972 fprintf(stderr, "focus: bad args\n");
973 }
tsepezf09bdfa2016-04-18 16:08:26 -0700974 } else {
975 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
976 }
977 }
978}
979
tonikitoo3e981582016-08-26 08:37:10 -0700980FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
981 FPDF_DOCUMENT doc,
982 int index) {
npmfa20cd52016-11-14 13:33:40 -0800983 FPDF_FORMFILLINFO_PDFiumTest* form_fill_info =
984 ToPDFiumTestFormFillInfo(param);
985 auto& loaded_pages = form_fill_info->loaded_pages;
npmfa20cd52016-11-14 13:33:40 -0800986 auto iter = loaded_pages.find(index);
987 if (iter != loaded_pages.end())
Tom Sepezbfa2a972017-07-24 11:38:31 -0700988 return iter->second.get();
tonikitoo3e981582016-08-26 08:37:10 -0700989
990 FPDF_PAGE page = FPDF_LoadPage(doc, index);
991 if (!page)
992 return nullptr;
993
npmfa20cd52016-11-14 13:33:40 -0800994 FPDF_FORMHANDLE& form_handle = form_fill_info->form_handle;
npmfa20cd52016-11-14 13:33:40 -0800995 FORM_OnAfterLoadPage(page, form_handle);
996 FORM_DoPageAAction(page, form_handle, FPDFPAGE_AACTION_OPEN);
Tom Sepezbfa2a972017-07-24 11:38:31 -0700997 loaded_pages[index].reset(page);
tonikitoo3e981582016-08-26 08:37:10 -0700998 return page;
999}
1000
Dan Sinclairddcb6e72017-04-05 10:30:33 -04001001std::wstring ConvertToWString(const unsigned short* buf,
1002 unsigned long buf_size) {
Dan Sinclair3c67fbd2017-04-05 16:07:50 -04001003 std::wstring result;
1004 result.reserve(buf_size);
1005 std::copy(buf, buf + buf_size, std::back_inserter(result));
1006 return result;
Dan Sinclairddcb6e72017-04-05 10:30:33 -04001007}
1008
Dan Sinclair3c67fbd2017-04-05 16:07:50 -04001009void DumpChildStructure(FPDF_STRUCTELEMENT child, int indent) {
1010 static const size_t kBufSize = 1024;
Dan Sinclairddcb6e72017-04-05 10:30:33 -04001011 unsigned short buf[kBufSize];
1012 unsigned long len = FPDF_StructElement_GetType(child, buf, kBufSize);
Dan Sinclair3c67fbd2017-04-05 16:07:50 -04001013 printf("%*s%ls", indent * 2, "", ConvertToWString(buf, len).c_str());
Dan Sinclairddcb6e72017-04-05 10:30:33 -04001014
1015 memset(buf, 0, sizeof(buf));
dan sinclaird9dad3a2017-04-06 14:44:02 -04001016 len = FPDF_StructElement_GetTitle(child, buf, kBufSize);
1017 if (len > 0)
1018 printf(": '%ls'", ConvertToWString(buf, len).c_str());
1019
1020 memset(buf, 0, sizeof(buf));
Dan Sinclairddcb6e72017-04-05 10:30:33 -04001021 len = FPDF_StructElement_GetAltText(child, buf, kBufSize);
1022 if (len > 0)
1023 printf(" (%ls)", ConvertToWString(buf, len).c_str());
1024 printf("\n");
1025
1026 for (int i = 0; i < FPDF_StructElement_CountChildren(child); ++i) {
1027 FPDF_STRUCTELEMENT sub_child = FPDF_StructElement_GetChildAtIndex(child, i);
1028 // If the child is not an Element then this will return null. This can
1029 // happen if the element is things like an object reference or a stream.
1030 if (!sub_child)
1031 continue;
1032
Dan Sinclair3c67fbd2017-04-05 16:07:50 -04001033 DumpChildStructure(sub_child, indent + 1);
Dan Sinclairddcb6e72017-04-05 10:30:33 -04001034 }
1035}
1036
1037void DumpPageStructure(FPDF_PAGE page, const int page_idx) {
1038 std::unique_ptr<void, FPDFStructTreeDeleter> tree(
1039 FPDF_StructTree_GetForPage(page));
1040 if (!tree) {
Dan Sinclair3c67fbd2017-04-05 16:07:50 -04001041 fprintf(stderr, "Failed to load struct tree for page %d\n", page_idx);
Dan Sinclairddcb6e72017-04-05 10:30:33 -04001042 return;
1043 }
1044
1045 printf("Structure Tree for Page %d\n", page_idx);
1046 for (int i = 0; i < FPDF_StructTree_CountChildren(tree.get()); ++i) {
1047 FPDF_STRUCTELEMENT child = FPDF_StructTree_GetChildAtIndex(tree.get(), i);
1048 if (!child) {
Dan Sinclair3c67fbd2017-04-05 16:07:50 -04001049 fprintf(stderr, "Failed to load child %d for page %d\n", i, page_idx);
Dan Sinclairddcb6e72017-04-05 10:30:33 -04001050 continue;
1051 }
Dan Sinclair3c67fbd2017-04-05 16:07:50 -04001052 DumpChildStructure(child, 0);
Dan Sinclairddcb6e72017-04-05 10:30:33 -04001053 }
1054 printf("\n\n");
1055}
1056
Lei Zhangd2be6462017-07-21 14:31:21 -07001057void DumpMetaData(FPDF_DOCUMENT doc) {
1058 constexpr const char* meta_tags[] = {"Title", "Author", "Subject",
1059 "Keywords", "Creator", "Producer",
1060 "CreationDate", "ModDate"};
1061 for (const char* meta_tag : meta_tags) {
1062 char meta_buffer[4096];
1063 unsigned long len =
1064 FPDF_GetMetaText(doc, meta_tag, meta_buffer, sizeof(meta_buffer));
1065 if (!len)
1066 continue;
1067
1068 auto* meta_string = reinterpret_cast<unsigned short*>(meta_buffer);
1069 printf("%-12s = %ls (%lu bytes)\n", meta_tag,
1070 GetPlatformWString(meta_string).c_str(), len);
1071 }
1072}
1073
1074void SaveAttachments(FPDF_DOCUMENT doc, const std::string& name) {
1075 for (int i = 0; i < FPDFDoc_GetAttachmentCount(doc); ++i) {
1076 FPDF_ATTACHMENT attachment = FPDFDoc_GetAttachment(doc, i);
1077
1078 // Retrieve the attachment file name.
1079 std::string attachment_name;
1080 unsigned long len = FPDFAttachment_GetName(attachment, nullptr, 0);
1081 if (len) {
1082 std::vector<char> buf(len);
1083 unsigned long actual_len =
1084 FPDFAttachment_GetName(attachment, buf.data(), len);
1085 if (actual_len == len) {
1086 attachment_name =
1087 GetPlatformString(reinterpret_cast<unsigned short*>(buf.data()));
1088 }
1089 }
1090 if (attachment_name.empty()) {
1091 fprintf(stderr, "Attachment #%d has an empty file name.\n", i + 1);
1092 continue;
1093 }
1094
1095 // Calculate the full attachment file name.
1096 char save_name[256];
1097 int chars_formatted =
1098 snprintf(save_name, sizeof(save_name), "%s.attachment.%s", name.c_str(),
1099 attachment_name.c_str());
1100 if (chars_formatted < 0 ||
1101 static_cast<size_t>(chars_formatted) >= sizeof(save_name)) {
1102 fprintf(stderr, "Filename %s is too long\n", save_name);
1103 continue;
1104 }
1105
1106 // Retrieve the attachment.
1107 len = FPDFAttachment_GetFile(attachment, nullptr, 0);
1108 std::vector<char> data_buf(len);
1109 if (len) {
1110 unsigned long actual_len =
1111 FPDFAttachment_GetFile(attachment, data_buf.data(), len);
1112 if (actual_len != len)
1113 data_buf.clear();
1114 }
1115 if (data_buf.empty()) {
1116 fprintf(stderr, "Attachment \"%s\" is empty.\n", attachment_name.c_str());
1117 continue;
1118 }
1119
1120 // Write the attachment file.
1121 FILE* fp = fopen(save_name, "wb");
1122 if (!fp) {
1123 fprintf(stderr, "Failed to open %s for saving attachment.\n", save_name);
1124 continue;
1125 }
1126
1127 size_t written_len = fwrite(data_buf.data(), 1, len, fp);
1128 if (written_len == len) {
1129 fprintf(stderr, "Saved attachment \"%s\" as: %s.\n",
1130 attachment_name.c_str(), save_name);
1131 } else {
1132 fprintf(stderr, "Failed to write to %s\n", save_name);
1133 }
1134 fclose(fp);
1135 }
1136}
1137
Jane Liub35dbad2017-08-03 16:29:17 -04001138void SaveImages(FPDF_PAGE page, const char* pdf_name, int page_num) {
Miklos Vajna92627612017-09-25 12:59:29 +02001139 for (int i = 0; i < FPDFPage_CountObjects(page); ++i) {
Jane Liub35dbad2017-08-03 16:29:17 -04001140 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, i);
1141 if (FPDFPageObj_GetType(obj) != FPDF_PAGEOBJ_IMAGE)
1142 continue;
1143
1144 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
1145 FPDFImageObj_GetBitmap(obj));
1146 if (!bitmap) {
1147 fprintf(stderr, "Image object #%d on page #%d has an empty bitmap.\n",
1148 i + 1, page_num + 1);
1149 continue;
1150 }
1151
1152 int format = FPDFBitmap_GetFormat(bitmap.get());
1153 if (format == FPDFBitmap_Unknown) {
1154 fprintf(stderr,
1155 "Image object #%d on page #%d has a bitmap of unknown format.\n",
1156 i + 1, page_num + 1);
1157 continue;
1158 }
1159
1160 std::vector<unsigned char> png_encoding;
1161 const unsigned char* buffer =
1162 static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap.get()));
1163 int width = FPDFBitmap_GetWidth(bitmap.get());
1164 int height = FPDFBitmap_GetHeight(bitmap.get());
1165 int stride = FPDFBitmap_GetStride(bitmap.get());
1166 bool ret = false;
1167 switch (format) {
1168 case FPDFBitmap_Gray:
1169 ret = image_diff_png::EncodeGrayPNG(buffer, width, height, stride,
1170 &png_encoding);
1171 break;
1172 case FPDFBitmap_BGR:
1173 ret = image_diff_png::EncodeBGRPNG(buffer, width, height, stride,
1174 &png_encoding);
1175 break;
1176 case FPDFBitmap_BGRx:
1177 ret = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride, true,
1178 &png_encoding);
1179 break;
1180 case FPDFBitmap_BGRA:
1181 ret = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride,
1182 false, &png_encoding);
1183 break;
1184 default:
1185 NOTREACHED();
1186 }
1187 if (!ret) {
1188 fprintf(stderr,
1189 "Failed to convert image object #%d on page #%d to png.\n", i + 1,
1190 page_num + 1);
1191 continue;
1192 }
1193
1194 char filename[256];
1195 int chars_formatted = snprintf(filename, sizeof(filename), "%s.%d.%d.png",
1196 pdf_name, page_num, i);
1197 if (chars_formatted < 0 ||
1198 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
1199 fprintf(stderr, "Filename %s for saving image is too long\n", filename);
1200 continue;
1201 }
1202
1203 FILE* fp = fopen(filename, "wb");
1204 if (!fp) {
1205 fprintf(stderr, "Failed to open %s for saving image.\n", filename);
1206 continue;
1207 }
1208
1209 size_t bytes_written =
1210 fwrite(&png_encoding.front(), 1, png_encoding.size(), fp);
1211 if (bytes_written != png_encoding.size())
1212 fprintf(stderr, "Failed to write to %s.\n", filename);
1213 else
1214 fprintf(stderr, "Successfully wrote embedded image %s.\n", filename);
1215
1216 (void)fclose(fp);
1217 }
1218}
1219
Dan Sinclairaeadad12017-07-18 16:43:41 -04001220// Note, for a client using progressive rendering you'd want to determine if you
1221// need the rendering to pause instead of always saying |true|. This is for
1222// testing to force the renderer to break whenever possible.
1223FPDF_BOOL NeedToPauseNow(IFSDK_PAUSE* p) {
1224 return true;
1225}
1226
Jun Fangb553bcb2015-11-10 18:49:04 +08001227bool RenderPage(const std::string& name,
tonikitoo3e981582016-08-26 08:37:10 -07001228 FPDF_DOCUMENT doc,
Tom Sepez1d17a042017-03-16 13:22:47 -07001229 FPDF_FORMHANDLE form,
Lei Zhangd2be6462017-07-21 14:31:21 -07001230 FPDF_FORMFILLINFO_PDFiumTest* form_fill_info,
Jun Fangb553bcb2015-11-10 18:49:04 +08001231 const int page_index,
tsepezf09bdfa2016-04-18 16:08:26 -07001232 const Options& options,
1233 const std::string& events) {
Tom Sepezbfa2a972017-07-24 11:38:31 -07001234 FPDF_PAGE page = GetPageForIndex(form_fill_info, doc, page_index);
1235 if (!page)
Jun Fangb553bcb2015-11-10 18:49:04 +08001236 return false;
Dan Sinclair3c67fbd2017-04-05 16:07:50 -04001237 if (options.send_events)
Tom Sepezbfa2a972017-07-24 11:38:31 -07001238 SendPageEvents(form, page, events);
Jane Liub35dbad2017-08-03 16:29:17 -04001239 if (options.save_images)
1240 SaveImages(page, name.c_str(), page_index);
Dan Sinclair3c67fbd2017-04-05 16:07:50 -04001241 if (options.output_format == OUTPUT_STRUCTURE) {
Tom Sepezbfa2a972017-07-24 11:38:31 -07001242 DumpPageStructure(page, page_index);
Dan Sinclairddcb6e72017-04-05 10:30:33 -04001243 return true;
1244 }
1245
Tom Sepezbfa2a972017-07-24 11:38:31 -07001246 std::unique_ptr<void, FPDFTextPageDeleter> text_page(FPDFText_LoadPage(page));
tsepezf09bdfa2016-04-18 16:08:26 -07001247
Jun Fangdf7f3662015-11-10 18:29:18 +08001248 double scale = 1.0;
thestig514e8c92016-07-15 17:57:54 -07001249 if (!options.scale_factor_as_string.empty())
Jun Fangdf7f3662015-11-10 18:29:18 +08001250 std::stringstream(options.scale_factor_as_string) >> scale;
thestig514e8c92016-07-15 17:57:54 -07001251
Tom Sepezbfa2a972017-07-24 11:38:31 -07001252 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
1253 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
1254 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepez1d17a042017-03-16 13:22:47 -07001255 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
1256 FPDFBitmap_Create(width, height, alpha));
1257
thestige97ea032016-05-19 10:59:15 -07001258 if (bitmap) {
1259 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
Tom Sepez1d17a042017-03-16 13:22:47 -07001260 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
Dan Sinclairaeadad12017-07-18 16:43:41 -04001261
1262 if (options.render_oneshot) {
1263 // Note, client programs probably want to use this method instead of the
1264 // progressive calls. The progressive calls are if you need to pause the
1265 // rendering to update the UI, the PDF renderer will break when possible.
Tom Sepezbfa2a972017-07-24 11:38:31 -07001266 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0,
Dan Sinclairaeadad12017-07-18 16:43:41 -04001267 FPDF_ANNOT);
1268 } else {
1269 IFSDK_PAUSE pause;
1270 pause.version = 1;
1271 pause.NeedToPauseNow = &NeedToPauseNow;
1272
Tom Sepezbfa2a972017-07-24 11:38:31 -07001273 int rv = FPDF_RenderPageBitmap_Start(bitmap.get(), page, 0, 0, width,
1274 height, 0, FPDF_ANNOT, &pause);
Dan Sinclairaeadad12017-07-18 16:43:41 -04001275 while (rv == FPDF_RENDER_TOBECOUNTINUED)
Tom Sepezbfa2a972017-07-24 11:38:31 -07001276 rv = FPDF_RenderPage_Continue(page, &pause);
Dan Sinclairaeadad12017-07-18 16:43:41 -04001277 }
Jun Fangdf7f3662015-11-10 18:29:18 +08001278
Tom Sepezbfa2a972017-07-24 11:38:31 -07001279 FPDF_FFLDraw(form, bitmap.get(), page, 0, 0, width, height, 0, FPDF_ANNOT);
Dan Sinclairaeadad12017-07-18 16:43:41 -04001280
1281 if (!options.render_oneshot)
Tom Sepezbfa2a972017-07-24 11:38:31 -07001282 FPDF_RenderPage_Close(page);
Dan Sinclairaeadad12017-07-18 16:43:41 -04001283
Tom Sepez1d17a042017-03-16 13:22:47 -07001284 int stride = FPDFBitmap_GetStride(bitmap.get());
thestige97ea032016-05-19 10:59:15 -07001285 const char* buffer =
Tom Sepez1d17a042017-03-16 13:22:47 -07001286 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap.get()));
Jun Fangdf7f3662015-11-10 18:29:18 +08001287
stephanafa05e972017-01-02 06:19:41 -08001288 std::string&& image_file_name = "";
thestige97ea032016-05-19 10:59:15 -07001289 switch (options.output_format) {
Jun Fangdf7f3662015-11-10 18:29:18 +08001290#ifdef _WIN32
thestige97ea032016-05-19 10:59:15 -07001291 case OUTPUT_BMP:
stephanafa05e972017-01-02 06:19:41 -08001292 image_file_name =
1293 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
thestige97ea032016-05-19 10:59:15 -07001294 break;
Jun Fangdf7f3662015-11-10 18:29:18 +08001295
thestige97ea032016-05-19 10:59:15 -07001296 case OUTPUT_EMF:
Tom Sepezbfa2a972017-07-24 11:38:31 -07001297 WriteEmf(page, name.c_str(), page_index);
thestige97ea032016-05-19 10:59:15 -07001298 break;
Lei Zhangd1c9b452017-05-05 17:21:36 -07001299
1300 case OUTPUT_PS2:
1301 case OUTPUT_PS3:
Tom Sepezbfa2a972017-07-24 11:38:31 -07001302 WritePS(page, name.c_str(), page_index);
Lei Zhangd1c9b452017-05-05 17:21:36 -07001303 break;
Jun Fangdf7f3662015-11-10 18:29:18 +08001304#endif
dsinclairb63068f2016-06-16 07:58:09 -07001305 case OUTPUT_TEXT:
Tom Sepezbfa2a972017-07-24 11:38:31 -07001306 WriteText(page, name.c_str(), page_index);
dsinclairb63068f2016-06-16 07:58:09 -07001307 break;
1308
Jane Liu4fd9a472017-06-01 18:56:09 -04001309 case OUTPUT_ANNOT:
Tom Sepezbfa2a972017-07-24 11:38:31 -07001310 WriteAnnot(page, name.c_str(), page_index);
Jane Liu4fd9a472017-06-01 18:56:09 -04001311 break;
1312
thestige97ea032016-05-19 10:59:15 -07001313 case OUTPUT_PNG:
stephanafa05e972017-01-02 06:19:41 -08001314 image_file_name =
1315 WritePng(name.c_str(), page_index, buffer, stride, width, height);
thestige97ea032016-05-19 10:59:15 -07001316 break;
Jun Fangdf7f3662015-11-10 18:29:18 +08001317
thestige97ea032016-05-19 10:59:15 -07001318 case OUTPUT_PPM:
stephanafa05e972017-01-02 06:19:41 -08001319 image_file_name =
1320 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
thestige97ea032016-05-19 10:59:15 -07001321 break;
Jun Fangdf7f3662015-11-10 18:29:18 +08001322
Cary Clark399be5b2016-03-14 16:51:29 -04001323#ifdef PDF_ENABLE_SKIA
thestige97ea032016-05-19 10:59:15 -07001324 case OUTPUT_SKP: {
1325 std::unique_ptr<SkPictureRecorder> recorder(
thestigb97e07e2016-09-28 22:16:40 -07001326 reinterpret_cast<SkPictureRecorder*>(
Tom Sepezbfa2a972017-07-24 11:38:31 -07001327 FPDF_RenderPageSkp(page, width, height)));
1328 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0);
stephanafa05e972017-01-02 06:19:41 -08001329 image_file_name = WriteSkp(name.c_str(), page_index, recorder.get());
thestige97ea032016-05-19 10:59:15 -07001330 } break;
Cary Clark399be5b2016-03-14 16:51:29 -04001331#endif
thestige97ea032016-05-19 10:59:15 -07001332 default:
1333 break;
1334 }
Jun Fangdf7f3662015-11-10 18:29:18 +08001335
stephanafa05e972017-01-02 06:19:41 -08001336 // Write the filename and the MD5 of the buffer to stdout if we wrote a
1337 // file.
1338 if (options.md5 && image_file_name != "")
1339 OutputMD5Hash(image_file_name.c_str(), buffer, stride * height);
thestige97ea032016-05-19 10:59:15 -07001340 } else {
1341 fprintf(stderr, "Page was too large to be rendered.\n");
1342 }
tonikitoo3e981582016-08-26 08:37:10 -07001343
Tom Sepezbfa2a972017-07-24 11:38:31 -07001344 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
1345 FORM_OnBeforeClosePage(page, form);
thestige97ea032016-05-19 10:59:15 -07001346 return !!bitmap;
Jun Fangdf7f3662015-11-10 18:29:18 +08001347}
1348
tsepezf09bdfa2016-04-18 16:08:26 -07001349void RenderPdf(const std::string& name,
1350 const char* pBuf,
1351 size_t len,
1352 const Options& options,
1353 const std::string& events) {
Lei Zhangd2be6462017-07-21 14:31:21 -07001354 IPDF_JSPLATFORM platform_callbacks = {};
Tom Sepeza72e8e22015-10-07 10:17:53 -07001355 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -08001356 platform_callbacks.app_alert = ExampleAppAlert;
Tom Sepez58fb36a2016-02-01 10:32:14 -08001357 platform_callbacks.app_response = ExampleAppResponse;
Tom Sepezb7cb36a2015-02-13 16:54:48 -08001358 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
Tom Sepeze5fbd7a2016-01-29 17:05:08 -08001359 platform_callbacks.Doc_mail = ExampleDocMail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001360
Artem Strygin834ebec2017-07-27 14:01:32 +03001361 // The pdf_avail must outlive doc.
1362 std::unique_ptr<void, FPDFAvailDeleter> pdf_avail;
Tom Sepezbfa2a972017-07-24 11:38:31 -07001363 // The document must outlive |form_callbacks.loaded_pages|.
1364 std::unique_ptr<void, FPDFDocumentDeleter> doc;
tonikitoo81d92f82016-09-21 12:44:56 -07001365 FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {};
Tom Sepezc46d0002015-11-30 15:46:36 -08001366#ifdef PDF_ENABLE_XFA
Tom Sepezed631382014-11-18 14:10:25 -08001367 form_callbacks.version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -08001368#else // PDF_ENABLE_XFA
1369 form_callbacks.version = 1;
1370#endif // PDF_ENABLE_XFA
tonikitoo3e981582016-08-26 08:37:10 -07001371 form_callbacks.FFI_GetPage = GetPageForIndex;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001372 form_callbacks.m_pJsPlatform = &platform_callbacks;
1373
1374 TestLoader loader(pBuf, len);
Lei Zhangd2be6462017-07-21 14:31:21 -07001375 FPDF_FILEACCESS file_access = {};
John Abd-El-Malek7dc51722014-05-26 12:54:31 -07001376 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -07001377 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001378 file_access.m_Param = &loader;
1379
Lei Zhangd2be6462017-07-21 14:31:21 -07001380 FX_FILEAVAIL file_avail = {};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001381 file_avail.version = 1;
1382 file_avail.IsDataAvail = Is_Data_Avail;
1383
Lei Zhangd2be6462017-07-21 14:31:21 -07001384 FX_DOWNLOADHINTS hints = {};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001385 hints.version = 1;
1386 hints.AddSegment = Add_Segment;
1387
Jun Fangdf7f3662015-11-10 18:29:18 +08001388 int nRet = PDF_DATA_NOTAVAIL;
Jun Fangb553bcb2015-11-10 18:49:04 +08001389 bool bIsLinearized = false;
Artem Strygin834ebec2017-07-27 14:01:32 +03001390 pdf_avail.reset(FPDFAvail_Create(&file_avail, &file_access));
Tom Sepezc98895c2015-11-24 15:30:36 -08001391
Tom Sepez1d17a042017-03-16 13:22:47 -07001392 if (FPDFAvail_IsLinearized(pdf_avail.get()) == PDF_LINEARIZED) {
1393 doc.reset(FPDFAvail_GetDocument(pdf_avail.get(), nullptr));
Jun Fangdf7f3662015-11-10 18:29:18 +08001394 if (doc) {
thestig514e8c92016-07-15 17:57:54 -07001395 while (nRet == PDF_DATA_NOTAVAIL)
Tom Sepez1d17a042017-03-16 13:22:47 -07001396 nRet = FPDFAvail_IsDocAvail(pdf_avail.get(), &hints);
thestig514e8c92016-07-15 17:57:54 -07001397
Jun Fangdf7f3662015-11-10 18:29:18 +08001398 if (nRet == PDF_DATA_ERROR) {
1399 fprintf(stderr, "Unknown error in checking if doc was available.\n");
1400 return;
1401 }
Tom Sepez1d17a042017-03-16 13:22:47 -07001402 nRet = FPDFAvail_IsFormAvail(pdf_avail.get(), &hints);
Jun Fangdf7f3662015-11-10 18:29:18 +08001403 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
1404 fprintf(stderr,
1405 "Error %d was returned in checking if form was available.\n",
1406 nRet);
1407 return;
1408 }
Jun Fangb553bcb2015-11-10 18:49:04 +08001409 bIsLinearized = true;
Jun Fangdf7f3662015-11-10 18:29:18 +08001410 }
1411 } else {
Tom Sepez1d17a042017-03-16 13:22:47 -07001412 doc.reset(FPDF_LoadCustomDocument(&file_access, nullptr));
Jun Fangdf7f3662015-11-10 18:29:18 +08001413 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001414
Lei Zhang5377ebf2015-09-23 14:52:53 -07001415 if (!doc) {
Lei Zhangd2be6462017-07-21 14:31:21 -07001416 PrintLastError();
JUN FANG827a1722015-03-05 13:39:21 -08001417 return;
1418 }
1419
Tom Sepez1d17a042017-03-16 13:22:47 -07001420 (void)FPDF_GetDocPermissions(doc.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001421
Lei Zhangd2be6462017-07-21 14:31:21 -07001422 if (options.show_metadata)
1423 DumpMetaData(doc.get());
Henrique Nakashimab73ce7b2017-06-19 16:04:34 -04001424
Lei Zhangd2be6462017-07-21 14:31:21 -07001425 if (options.save_attachments)
1426 SaveAttachments(doc.get(), name);
Jane Liu4442d452017-07-19 10:19:42 -04001427
Tom Sepez1d17a042017-03-16 13:22:47 -07001428 std::unique_ptr<void, FPDFFormHandleDeleter> form(
1429 FPDFDOC_InitFormFillEnvironment(doc.get(), &form_callbacks));
1430 form_callbacks.form_handle = form.get();
tonikitoo81d92f82016-09-21 12:44:56 -07001431
Tom Sepezc46d0002015-11-30 15:46:36 -08001432#ifdef PDF_ENABLE_XFA
thestigb97e07e2016-09-28 22:16:40 -07001433 int doc_type = DOCTYPE_PDF;
Tom Sepez1d17a042017-03-16 13:22:47 -07001434 if (FPDF_HasXFAField(doc.get(), &doc_type) && doc_type != DOCTYPE_PDF &&
1435 !FPDF_LoadXFA(doc.get())) {
Lei Zhang5377ebf2015-09-23 14:52:53 -07001436 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -08001437 }
Tom Sepezc46d0002015-11-30 15:46:36 -08001438#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001439
Tom Sepez1d17a042017-03-16 13:22:47 -07001440 FPDF_SetFormFieldHighlightColor(form.get(), 0, 0xFFE4DD);
1441 FPDF_SetFormFieldHighlightAlpha(form.get(), 100);
1442 FORM_DoDocumentJSAction(form.get());
1443 FORM_DoDocumentOpenAction(form.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001444
Lei Zhangd1c9b452017-05-05 17:21:36 -07001445#if _WIN32
1446 if (options.output_format == OUTPUT_PS2)
Lei Zhangd2be6462017-07-21 14:31:21 -07001447 FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT2);
Lei Zhangd1c9b452017-05-05 17:21:36 -07001448 else if (options.output_format == OUTPUT_PS3)
Lei Zhangd2be6462017-07-21 14:31:21 -07001449 FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT3);
Lei Zhangd1c9b452017-05-05 17:21:36 -07001450#endif
1451
Tom Sepez1d17a042017-03-16 13:22:47 -07001452 int page_count = FPDF_GetPageCount(doc.get());
Tom Sepez1ed8a212015-05-11 15:25:39 -07001453 int rendered_pages = 0;
1454 int bad_pages = 0;
npmfa20cd52016-11-14 13:33:40 -08001455 int first_page = options.pages ? options.first_page : 0;
1456 int last_page = options.pages ? options.last_page + 1 : page_count;
1457 for (int i = first_page; i < last_page; ++i) {
Jun Fangdf7f3662015-11-10 18:29:18 +08001458 if (bIsLinearized) {
1459 nRet = PDF_DATA_NOTAVAIL;
thestig514e8c92016-07-15 17:57:54 -07001460 while (nRet == PDF_DATA_NOTAVAIL)
Tom Sepez1d17a042017-03-16 13:22:47 -07001461 nRet = FPDFAvail_IsPageAvail(pdf_avail.get(), i, &hints);
thestig514e8c92016-07-15 17:57:54 -07001462
Jun Fangdf7f3662015-11-10 18:29:18 +08001463 if (nRet == PDF_DATA_ERROR) {
1464 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
1465 i);
1466 return;
1467 }
1468 }
Lei Zhangd2be6462017-07-21 14:31:21 -07001469 if (RenderPage(name, doc.get(), form.get(), &form_callbacks, i, options,
1470 events)) {
Jun Fangdf7f3662015-11-10 18:29:18 +08001471 ++rendered_pages;
Lei Zhangd2be6462017-07-21 14:31:21 -07001472 } else {
Lei Zhang5377ebf2015-09-23 14:52:53 -07001473 ++bad_pages;
Lei Zhangd2be6462017-07-21 14:31:21 -07001474 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001475 }
1476
Tom Sepez1d17a042017-03-16 13:22:47 -07001477 FORM_DoDocumentAAction(form.get(), FPDFDOC_AACTION_WC);
Tom Sepez1ed8a212015-05-11 15:25:39 -07001478 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
tsepez10b01bf2016-05-04 12:52:42 -07001479 if (bad_pages)
1480 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001481}
1482
Lei Zhangd2be6462017-07-21 14:31:21 -07001483void ShowConfig() {
Tom Sepez2991d8d2016-01-15 16:02:48 -08001484 std::string config;
1485 std::string maybe_comma;
1486#if PDF_ENABLE_V8
1487 config.append(maybe_comma);
1488 config.append("V8");
1489 maybe_comma = ",";
1490#endif // PDF_ENABLE_V8
1491#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1492 config.append(maybe_comma);
1493 config.append("V8_EXTERNAL");
1494 maybe_comma = ",";
1495#endif // V8_USE_EXTERNAL_STARTUP_DATA
1496#ifdef PDF_ENABLE_XFA
1497 config.append(maybe_comma);
1498 config.append("XFA");
1499 maybe_comma = ",";
1500#endif // PDF_ENABLE_XFA
dan sinclair00d40642017-01-30 19:48:54 -08001501#ifdef PDF_ENABLE_ASAN
1502 config.append(maybe_comma);
1503 config.append("ASAN");
1504 maybe_comma = ",";
1505#endif // PDF_ENABLE_ASAN
Tom Sepez2991d8d2016-01-15 16:02:48 -08001506 printf("%s\n", config.c_str());
1507}
1508
Lei Zhangd2be6462017-07-21 14:31:21 -07001509constexpr char kUsageString[] =
Tom Sepez23b4e3f2015-02-06 16:05:23 -08001510 "Usage: pdfium_test [OPTION] [FILE]...\n"
Jane Liu4442d452017-07-19 10:19:42 -04001511 " --show-config - print build options and exit\n"
1512 " --show-metadata - print the file metadata\n"
1513 " --show-structure - print the structure elements from the document\n"
1514 " --send-events - send input described by .evt file\n"
1515 " --render-oneshot - render image without using progressive renderer\n"
1516 " --save-attachments - write embedded attachments "
1517 "<pdf-name>.attachment.<attachment-name>\n"
Jane Liub35dbad2017-08-03 16:29:17 -04001518 " --save-images - write embedded images "
1519 "<pdf-name>.<page-number>.<object-number>.png\n"
Henrique Nakashima95ea7782017-07-11 16:42:43 -04001520#ifdef ENABLE_CALLGRIND
Jane Liu4442d452017-07-19 10:19:42 -04001521 " --callgrind-delim - delimit interesting section when using callgrind\n"
Henrique Nakashima95ea7782017-07-11 16:42:43 -04001522#endif // ENABLE_CALLGRIND
Jane Liu4442d452017-07-19 10:19:42 -04001523 " --bin-dir=<path> - override path to v8 external data\n"
1524 " --font-dir=<path> - override path to external fonts\n"
1525 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
npmfa20cd52016-11-14 13:33:40 -08001526 " --pages=<number>(-<number>) - only render the given 0-based page(s)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -08001527#ifdef _WIN32
Jane Liu57f228d2017-07-13 18:10:30 -04001528 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
1529 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
1530 " --ps2 - write page raw PostScript (Lvl 2) "
1531 "<pdf-name>.<page-number>.ps\n"
1532 " --ps3 - write page raw PostScript (Lvl 3) "
1533 "<pdf-name>.<page-number>.ps\n"
Tom Sepezc46d0002015-11-30 15:46:36 -08001534#endif // _WIN32
Jane Liu57f228d2017-07-13 18:10:30 -04001535 " --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n"
1536 " --png - write page images <pdf-name>.<page-number>.png\n"
1537 " --ppm - write page images <pdf-name>.<page-number>.ppm\n"
1538 " --annot - write annotation info <pdf-name>.<page-number>.annot.txt\n"
Cary Clark399be5b2016-03-14 16:51:29 -04001539#ifdef PDF_ENABLE_SKIA
Jane Liu57f228d2017-07-13 18:10:30 -04001540 " --skp - write page images <pdf-name>.<page-number>.skp\n"
Cary Clark399be5b2016-03-14 16:51:29 -04001541#endif
Jane Liu57f228d2017-07-13 18:10:30 -04001542 " --md5 - write output image paths and their md5 hashes to stdout.\n"
Cary Clark399be5b2016-03-14 16:51:29 -04001543 "";
Tom Sepez23b4e3f2015-02-06 16:05:23 -08001544
Lei Zhangd2be6462017-07-21 14:31:21 -07001545} // namespace
1546
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001547int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -08001548 std::vector<std::string> args(argv, argv + argc);
1549 Options options;
thestig514e8c92016-07-15 17:57:54 -07001550 std::vector<std::string> files;
Tom Sepez5ee12d72014-12-17 16:24:01 -08001551 if (!ParseCommandLine(args, &options, &files)) {
thestig514e8c92016-07-15 17:57:54 -07001552 fprintf(stderr, "%s", kUsageString);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001553 return 1;
1554 }
1555
Tom Sepez2991d8d2016-01-15 16:02:48 -08001556 if (options.show_config) {
1557 ShowConfig();
1558 return 0;
1559 }
1560
1561 if (files.empty()) {
1562 fprintf(stderr, "No input files.\n");
1563 return 1;
1564 }
1565
Tom Sepez452b4f32015-10-13 09:27:27 -07001566#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -07001567 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -08001568#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1569 v8::StartupData natives;
1570 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -07001571 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
1572 &snapshot, &platform);
1573#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -07001574 InitializeV8ForPDFium(options.exe_path, &platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -08001575#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -07001576#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -08001577
Tom Sepeza72e8e22015-10-07 10:17:53 -07001578 FPDF_LIBRARY_CONFIG config;
1579 config.version = 2;
1580 config.m_pUserFontPaths = nullptr;
1581 config.m_pIsolate = nullptr;
1582 config.m_v8EmbedderSlot = 0;
1583
1584 const char* path_array[2];
1585 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -07001586 path_array[0] = options.font_directory.c_str();
1587 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -07001588 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -07001589 }
Tom Sepeza72e8e22015-10-07 10:17:53 -07001590 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001591
Lei Zhangd2be6462017-07-21 14:31:21 -07001592 UNSUPPORT_INFO unsupported_info = {};
npmfa20cd52016-11-14 13:33:40 -08001593 unsupported_info.version = 1;
1594 unsupported_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001595
npmfa20cd52016-11-14 13:33:40 -08001596 FSDK_SetUnSpObjProcessHandler(&unsupported_info);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001597
thestig514e8c92016-07-15 17:57:54 -07001598 for (const std::string& filename : files) {
Tom Sepez5ee12d72014-12-17 16:24:01 -08001599 size_t file_length = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -08001600 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
1601 GetFileContents(filename.c_str(), &file_length);
tsepezf09bdfa2016-04-18 16:08:26 -07001602 if (!file_contents)
1603 continue;
tsepez10b01bf2016-05-04 12:52:42 -07001604 fprintf(stderr, "Rendering PDF file %s.\n", filename.c_str());
Henrique Nakashima95ea7782017-07-11 16:42:43 -04001605
1606#ifdef ENABLE_CALLGRIND
1607 if (options.callgrind_delimiters)
1608 CALLGRIND_START_INSTRUMENTATION;
1609#endif // ENABLE_CALLGRIND
1610
tsepezf09bdfa2016-04-18 16:08:26 -07001611 std::string events;
1612 if (options.send_events) {
1613 std::string event_filename = filename;
1614 size_t event_length = 0;
1615 size_t extension_pos = event_filename.find(".pdf");
1616 if (extension_pos != std::string::npos) {
1617 event_filename.replace(extension_pos, 4, ".evt");
thestigf2b940c2016-10-13 06:48:47 -07001618 if (access(event_filename.c_str(), R_OK) == 0) {
1619 fprintf(stderr, "Using event file %s.\n", event_filename.c_str());
1620 std::unique_ptr<char, pdfium::FreeDeleter> event_contents =
1621 GetFileContents(event_filename.c_str(), &event_length);
1622 if (event_contents) {
1623 fprintf(stderr, "Sending events from: %s\n",
1624 event_filename.c_str());
1625 events = std::string(event_contents.get(), event_length);
1626 }
tsepezf09bdfa2016-04-18 16:08:26 -07001627 }
1628 }
1629 }
1630 RenderPdf(filename, file_contents.get(), file_length, options, events);
Henrique Nakashima95ea7782017-07-11 16:42:43 -04001631
1632#ifdef ENABLE_CALLGRIND
1633 if (options.callgrind_delimiters)
1634 CALLGRIND_STOP_INSTRUMENTATION;
1635#endif // ENABLE_CALLGRIND
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001636 }
1637
1638 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -07001639#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -08001640 v8::V8::ShutdownPlatform();
1641 delete platform;
thestigc08cd7a2016-06-27 09:47:59 -07001642
1643#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1644 free(const_cast<char*>(natives.data));
1645 free(const_cast<char*>(snapshot.data));
1646#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -07001647#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001648
1649 return 0;
1650}