John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 | |
Lei Zhang | 9a25ede | 2017-06-22 00:05:12 -0700 | [diff] [blame] | 10 | #include <iterator> |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 11 | #include <map> |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 12 | #include <sstream> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <utility> |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 15 | #include <vector> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 16 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 17 | #if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_ |
| 18 | #define _SKIA_SUPPORT_ |
| 19 | #endif |
| 20 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 21 | #include "public/cpp/fpdf_deleters.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 22 | #include "public/fpdf_annot.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 23 | #include "public/fpdf_dataavail.h" |
Lei Zhang | 453d96b | 2015-12-31 13:13:10 -0800 | [diff] [blame] | 24 | #include "public/fpdf_edit.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 25 | #include "public/fpdf_ext.h" |
| 26 | #include "public/fpdf_formfill.h" |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 27 | #include "public/fpdf_structtree.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 28 | #include "public/fpdf_text.h" |
| 29 | #include "public/fpdfview.h" |
Lei Zhang | 143959d | 2017-06-22 12:20:58 -0700 | [diff] [blame] | 30 | #include "testing/image_diff/image_diff_png.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 31 | #include "testing/test_support.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 32 | #include "third_party/base/logging.h" |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 33 | |
thestig | f2b940c | 2016-10-13 06:48:47 -0700 | [diff] [blame] | 34 | #ifdef _WIN32 |
| 35 | #include <io.h> |
| 36 | #else |
| 37 | #include <unistd.h> |
| 38 | #endif |
| 39 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 40 | #ifdef PDF_ENABLE_V8 |
John Abd-El-Malek | b045ed2 | 2015-02-10 09:15:12 -0800 | [diff] [blame] | 41 | #include "v8/include/libplatform/libplatform.h" |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 42 | #include "v8/include/v8.h" |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 43 | #endif // PDF_ENABLE_V8 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 44 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 45 | #ifdef PDF_ENABLE_SKIA |
| 46 | #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 47 | #include "third_party/skia/include/core/SkStream.h" |
| 48 | #endif |
| 49 | |
thestig | f2b940c | 2016-10-13 06:48:47 -0700 | [diff] [blame] | 50 | #ifdef _WIN32 |
| 51 | #define access _access |
| 52 | #define snprintf _snprintf |
| 53 | #define R_OK 4 |
| 54 | #endif |
| 55 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 56 | enum OutputFormat { |
| 57 | OUTPUT_NONE, |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 58 | OUTPUT_STRUCTURE, |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 59 | OUTPUT_TEXT, |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 60 | OUTPUT_PPM, |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 61 | OUTPUT_PNG, |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 62 | OUTPUT_ANNOT, |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 63 | #ifdef _WIN32 |
| 64 | OUTPUT_BMP, |
| 65 | OUTPUT_EMF, |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 66 | OUTPUT_PS2, |
| 67 | OUTPUT_PS3, |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 68 | #endif |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 69 | #ifdef PDF_ENABLE_SKIA |
| 70 | OUTPUT_SKP, |
| 71 | #endif |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 74 | struct Options { |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 75 | Options() |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 76 | : show_config(false), |
Henrique Nakashima | b73ce7b | 2017-06-19 16:04:34 -0400 | [diff] [blame] | 77 | show_metadata(false), |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 78 | send_events(false), |
| 79 | pages(false), |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 80 | md5(false), |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 81 | output_format(OUTPUT_NONE) {} |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 82 | |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 83 | bool show_config; |
Henrique Nakashima | b73ce7b | 2017-06-19 16:04:34 -0400 | [diff] [blame] | 84 | bool show_metadata; |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 85 | bool send_events; |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 86 | bool pages; |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 87 | bool md5; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 88 | OutputFormat output_format; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 89 | std::string scale_factor_as_string; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 90 | std::string exe_path; |
| 91 | std::string bin_directory; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 92 | std::string font_directory; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 93 | // 0-based page numbers to be rendered. |
| 94 | int first_page; |
| 95 | int last_page; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 98 | struct FPDF_FORMFILLINFO_PDFiumTest : public FPDF_FORMFILLINFO { |
| 99 | // Hold a map of the currently loaded pages in order to avoid them |
| 100 | // to get loaded twice. |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 101 | std::map<int, FPDF_PAGE> loaded_pages; |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 102 | |
| 103 | // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can |
| 104 | // make use of it. |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 105 | FPDF_FORMHANDLE form_handle; |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo( |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 109 | FPDF_FORMFILLINFO* form_fill_info) { |
| 110 | return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(form_fill_info); |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 113 | static bool CheckDimensions(int stride, int width, int height) { |
| 114 | if (stride < 0 || width < 0 || height < 0) |
| 115 | return false; |
| 116 | if (height > 0 && width > INT_MAX / height) |
| 117 | return false; |
| 118 | return true; |
| 119 | } |
| 120 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 121 | static void OutputMD5Hash(const char* file_name, const char* buffer, int len) { |
| 122 | // Get the MD5 hash and write it to stdout. |
Lei Zhang | 143959d | 2017-06-22 12:20:58 -0700 | [diff] [blame] | 123 | std::string hash = |
| 124 | GenerateMD5Base16(reinterpret_cast<const uint8_t*>(buffer), len); |
| 125 | printf("MD5:%s:%s\n", file_name, hash.c_str()); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static std::string WritePpm(const char* pdf_name, |
| 129 | int num, |
| 130 | const void* buffer_void, |
| 131 | int stride, |
| 132 | int width, |
| 133 | int height) { |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 134 | const char* buffer = reinterpret_cast<const char*>(buffer_void); |
| 135 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 136 | if (!CheckDimensions(stride, width, height)) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 137 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 138 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 139 | int out_len = width * height; |
| 140 | if (out_len > INT_MAX / 3) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 141 | return ""; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 142 | out_len *= 3; |
| 143 | |
| 144 | char filename[256]; |
| 145 | snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num); |
John Abd-El-Malek | a548d30 | 2014-06-26 10:18:11 -0700 | [diff] [blame] | 146 | FILE* fp = fopen(filename, "wb"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 147 | if (!fp) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 148 | return ""; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 149 | fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height); |
| 150 | // Source data is B, G, R, unused. |
| 151 | // Dest data is R, G, B. |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 152 | std::vector<char> result(out_len); |
Lei Zhang | e00660b | 2015-08-13 15:40:18 -0700 | [diff] [blame] | 153 | for (int h = 0; h < height; ++h) { |
| 154 | const char* src_line = buffer + (stride * h); |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 155 | char* dest_line = result.data() + (width * h * 3); |
Lei Zhang | e00660b | 2015-08-13 15:40:18 -0700 | [diff] [blame] | 156 | for (int w = 0; w < width; ++w) { |
| 157 | // R |
| 158 | dest_line[w * 3] = src_line[(w * 4) + 2]; |
| 159 | // G |
| 160 | dest_line[(w * 3) + 1] = src_line[(w * 4) + 1]; |
| 161 | // B |
| 162 | dest_line[(w * 3) + 2] = src_line[w * 4]; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 163 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 164 | } |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 165 | fwrite(result.data(), out_len, 1, fp); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 166 | fclose(fp); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 167 | return std::string(filename); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 168 | } |
| 169 | |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 170 | void WriteText(FPDF_PAGE page, const char* pdf_name, int num) { |
| 171 | char filename[256]; |
| 172 | int chars_formatted = |
| 173 | snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num); |
| 174 | if (chars_formatted < 0 || |
| 175 | static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
| 176 | fprintf(stderr, "Filename %s is too long\n", filename); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | FILE* fp = fopen(filename, "w"); |
| 181 | if (!fp) { |
| 182 | fprintf(stderr, "Failed to open %s for output\n", filename); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | // Output in UTF32-LE. |
| 187 | uint32_t bom = 0x0000FEFF; |
| 188 | fwrite(&bom, sizeof(bom), 1, fp); |
| 189 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 190 | std::unique_ptr<void, FPDFTextPageDeleter> textpage(FPDFText_LoadPage(page)); |
| 191 | for (int i = 0; i < FPDFText_CountChars(textpage.get()); i++) { |
| 192 | uint32_t c = FPDFText_GetUnicode(textpage.get(), i); |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 193 | fwrite(&c, sizeof(c), 1, fp); |
| 194 | } |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 195 | (void)fclose(fp); |
| 196 | } |
| 197 | |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 198 | std::string AnnotSubtypeToString(FPDF_ANNOTATION_SUBTYPE subtype) { |
| 199 | if (subtype == FPDF_ANNOT_TEXT) |
| 200 | return "Text"; |
| 201 | if (subtype == FPDF_ANNOT_LINK) |
| 202 | return "Link"; |
| 203 | if (subtype == FPDF_ANNOT_FREETEXT) |
| 204 | return "FreeText"; |
| 205 | if (subtype == FPDF_ANNOT_LINE) |
| 206 | return "Line"; |
| 207 | if (subtype == FPDF_ANNOT_SQUARE) |
| 208 | return "Square"; |
| 209 | if (subtype == FPDF_ANNOT_CIRCLE) |
| 210 | return "Circle"; |
| 211 | if (subtype == FPDF_ANNOT_POLYGON) |
| 212 | return "Polygon"; |
| 213 | if (subtype == FPDF_ANNOT_POLYLINE) |
| 214 | return "PolyLine"; |
| 215 | if (subtype == FPDF_ANNOT_HIGHLIGHT) |
| 216 | return "Highlight"; |
| 217 | if (subtype == FPDF_ANNOT_UNDERLINE) |
| 218 | return "Underline"; |
| 219 | if (subtype == FPDF_ANNOT_SQUIGGLY) |
| 220 | return "Squiggly"; |
| 221 | if (subtype == FPDF_ANNOT_STRIKEOUT) |
| 222 | return "StrikeOut"; |
| 223 | if (subtype == FPDF_ANNOT_STAMP) |
| 224 | return "Stamp"; |
| 225 | if (subtype == FPDF_ANNOT_CARET) |
| 226 | return "Caret"; |
| 227 | if (subtype == FPDF_ANNOT_INK) |
| 228 | return "Ink"; |
| 229 | if (subtype == FPDF_ANNOT_POPUP) |
| 230 | return "Popup"; |
| 231 | if (subtype == FPDF_ANNOT_FILEATTACHMENT) |
| 232 | return "FileAttachment"; |
| 233 | if (subtype == FPDF_ANNOT_SOUND) |
| 234 | return "Sound"; |
| 235 | if (subtype == FPDF_ANNOT_MOVIE) |
| 236 | return "Movie"; |
| 237 | if (subtype == FPDF_ANNOT_WIDGET) |
| 238 | return "Widget"; |
| 239 | if (subtype == FPDF_ANNOT_SCREEN) |
| 240 | return "Screen"; |
| 241 | if (subtype == FPDF_ANNOT_PRINTERMARK) |
| 242 | return "PrinterMark"; |
| 243 | if (subtype == FPDF_ANNOT_TRAPNET) |
| 244 | return "TrapNet"; |
| 245 | if (subtype == FPDF_ANNOT_WATERMARK) |
| 246 | return "Watermark"; |
| 247 | if (subtype == FPDF_ANNOT_THREED) |
| 248 | return "3D"; |
| 249 | if (subtype == FPDF_ANNOT_RICHMEDIA) |
| 250 | return "RichMedia"; |
| 251 | if (subtype == FPDF_ANNOT_XFAWIDGET) |
| 252 | return "XFAWidget"; |
| 253 | NOTREACHED(); |
| 254 | return ""; |
| 255 | } |
| 256 | |
| 257 | void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) { |
| 258 | // Open the output text file. |
| 259 | char filename[256]; |
| 260 | int chars_formatted = |
| 261 | snprintf(filename, sizeof(filename), "%s.%d.annot.txt", pdf_name, num); |
| 262 | if (chars_formatted < 0 || |
| 263 | static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
| 264 | fprintf(stderr, "Filename %s is too long\n", filename); |
| 265 | return; |
| 266 | } |
| 267 | FILE* fp = fopen(filename, "w"); |
| 268 | if (!fp) { |
| 269 | fprintf(stderr, "Failed to open %s for output\n", filename); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | int annot_count = FPDFPage_GetAnnotCount(page); |
| 274 | fprintf(fp, "Number of annotations: %d\n\n", annot_count); |
| 275 | |
| 276 | // Iterate through all annotations on this page. |
| 277 | for (int i = 0; i < annot_count; i++) { |
| 278 | // Retrieve the annotation object and its subtype. |
| 279 | fprintf(fp, "Annotation #%d:\n", i + 1); |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 280 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, i); |
| 281 | if (!annot) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 282 | fprintf(fp, "Failed to retrieve annotation!\n\n"); |
| 283 | continue; |
| 284 | } |
| 285 | FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot); |
| 286 | fprintf(fp, "Subtype: %s\n", AnnotSubtypeToString(subtype).c_str()); |
| 287 | |
| 288 | // Retrieve the annotation's color and interior color. |
| 289 | unsigned int R; |
| 290 | unsigned int G; |
| 291 | unsigned int B; |
| 292 | unsigned int A; |
| 293 | if (!FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)) { |
| 294 | fprintf(fp, "Failed to retrieve color.\n"); |
| 295 | } else { |
| 296 | fprintf(fp, "Color in RGBA: %d %d %d %d\n", R, G, B, A); |
| 297 | } |
| 298 | if (!FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_InteriorColor, &R, &G, |
| 299 | &B, &A)) { |
| 300 | fprintf(fp, "Failed to retrieve interior color.\n"); |
| 301 | } else { |
| 302 | fprintf(fp, "Interior color in RGBA: %d %d %d %d\n", R, G, B, A); |
| 303 | } |
| 304 | |
| 305 | // Retrieve the annotation's contents and author. |
| 306 | unsigned long len = |
| 307 | FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0); |
| 308 | std::vector<char> buf(len); |
| 309 | FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, buf.data(), len); |
| 310 | fprintf(fp, "Content: %ls\n", |
| 311 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 312 | .c_str()); |
| 313 | len = FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Author, nullptr, 0); |
| 314 | buf.clear(); |
| 315 | buf.resize(len); |
| 316 | FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Author, buf.data(), len); |
| 317 | fprintf(fp, "Author: %ls\n", |
| 318 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 319 | .c_str()); |
| 320 | |
| 321 | // Retrieve the annotation's quadpoints if it is a markup annotation. |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 322 | if (FPDFAnnot_HasAttachmentPoints(annot)) { |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 323 | FS_QUADPOINTSF quadpoints = FPDFAnnot_GetAttachmentPoints(annot); |
| 324 | fprintf(fp, "Quadpoints: (%f, %f), (%f, %f), (%f, %f), (%f, %f)\n", |
| 325 | quadpoints.x1, quadpoints.y1, quadpoints.x2, quadpoints.y2, |
| 326 | quadpoints.x3, quadpoints.y3, quadpoints.x4, quadpoints.y4); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Retrieve the annotation's rectangle coordinates. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 330 | FS_RECTF rect = FPDFAnnot_GetRect(annot); |
| 331 | fprintf(fp, "Rectangle: l - %f, b - %f, r - %f, t - %f\n\n", rect.left, |
| 332 | rect.bottom, rect.right, rect.top); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | (void)fclose(fp); |
| 336 | } |
| 337 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 338 | static std::string WritePng(const char* pdf_name, |
| 339 | int num, |
| 340 | const void* buffer_void, |
| 341 | int stride, |
| 342 | int width, |
| 343 | int height) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 344 | if (!CheckDimensions(stride, width, height)) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 345 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 346 | |
| 347 | std::vector<unsigned char> png_encoding; |
| 348 | const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void); |
| 349 | if (!image_diff_png::EncodeBGRAPNG( |
| 350 | buffer, width, height, stride, false, &png_encoding)) { |
| 351 | fprintf(stderr, "Failed to convert bitmap to PNG\n"); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 352 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | char filename[256]; |
| 356 | int chars_formatted = snprintf( |
| 357 | filename, sizeof(filename), "%s.%d.png", pdf_name, num); |
| 358 | if (chars_formatted < 0 || |
| 359 | static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 360 | fprintf(stderr, "Filename %s is too long\n", filename); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 361 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | FILE* fp = fopen(filename, "wb"); |
| 365 | if (!fp) { |
| 366 | fprintf(stderr, "Failed to open %s for output\n", filename); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 367 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | size_t bytes_written = fwrite( |
| 371 | &png_encoding.front(), 1, png_encoding.size(), fp); |
| 372 | if (bytes_written != png_encoding.size()) |
| 373 | fprintf(stderr, "Failed to write to %s\n", filename); |
| 374 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 375 | (void)fclose(fp); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 376 | return std::string(filename); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 379 | #ifdef _WIN32 |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 380 | static std::string WriteBmp(const char* pdf_name, |
| 381 | int num, |
| 382 | const void* buffer, |
| 383 | int stride, |
| 384 | int width, |
| 385 | int height) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 386 | if (!CheckDimensions(stride, width, height)) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 387 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 388 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 389 | int out_len = stride * height; |
| 390 | if (out_len > INT_MAX / 3) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 391 | return ""; |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 392 | |
| 393 | char filename[256]; |
| 394 | snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num); |
| 395 | FILE* fp = fopen(filename, "wb"); |
| 396 | if (!fp) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 397 | return ""; |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 398 | |
Nico Weber | 2827bdd | 2015-07-01 14:08:08 -0700 | [diff] [blame] | 399 | BITMAPINFO bmi = {}; |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 400 | bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD); |
| 401 | bmi.bmiHeader.biWidth = width; |
| 402 | bmi.bmiHeader.biHeight = -height; // top-down image |
| 403 | bmi.bmiHeader.biPlanes = 1; |
| 404 | bmi.bmiHeader.biBitCount = 32; |
| 405 | bmi.bmiHeader.biCompression = BI_RGB; |
| 406 | bmi.bmiHeader.biSizeImage = 0; |
| 407 | |
Nico Weber | 2827bdd | 2015-07-01 14:08:08 -0700 | [diff] [blame] | 408 | BITMAPFILEHEADER file_header = {}; |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 409 | file_header.bfType = 0x4d42; |
| 410 | file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len; |
| 411 | file_header.bfOffBits = file_header.bfSize - out_len; |
| 412 | |
| 413 | fwrite(&file_header, sizeof(file_header), 1, fp); |
| 414 | fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp); |
| 415 | fwrite(buffer, out_len, 1, fp); |
| 416 | fclose(fp); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 417 | return std::string(filename); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 421 | char filename[256]; |
| 422 | snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num); |
| 423 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 424 | HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 425 | |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 426 | int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 427 | int height = static_cast<int>(FPDF_GetPageHeight(page)); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 428 | HRGN rgn = CreateRectRgn(0, 0, width, height); |
| 429 | SelectClipRgn(dc, rgn); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 430 | DeleteObject(rgn); |
| 431 | |
| 432 | SelectObject(dc, GetStockObject(NULL_PEN)); |
| 433 | SelectObject(dc, GetStockObject(WHITE_BRUSH)); |
| 434 | // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. |
| 435 | Rectangle(dc, 0, 0, width + 1, height + 1); |
| 436 | |
| 437 | FPDF_RenderPage(dc, page, 0, 0, width, height, 0, |
| 438 | FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); |
| 439 | |
| 440 | DeleteEnhMetaFile(CloseEnhMetaFile(dc)); |
| 441 | } |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 442 | |
| 443 | int CALLBACK EnhMetaFileProc(HDC hdc, |
| 444 | HANDLETABLE* handle_table, |
| 445 | const ENHMETARECORD* record, |
| 446 | int objects_count, |
| 447 | LPARAM param) { |
| 448 | std::vector<const ENHMETARECORD*>& items = |
| 449 | *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param); |
| 450 | items.push_back(record); |
| 451 | return 1; |
| 452 | } |
| 453 | |
| 454 | void WritePS(FPDF_PAGE page, const char* pdf_name, int num) { |
| 455 | char filename[256]; |
| 456 | snprintf(filename, sizeof(filename), "%s.%d.ps", pdf_name, num); |
| 457 | FILE* fp = fopen(filename, "wb"); |
| 458 | if (!fp) |
| 459 | return; |
| 460 | |
| 461 | HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr); |
| 462 | |
| 463 | int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 464 | int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 465 | FPDF_RenderPage(dc, page, 0, 0, width, height, 0, |
| 466 | FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); |
| 467 | |
| 468 | HENHMETAFILE emf = CloseEnhMetaFile(dc); |
| 469 | std::vector<const ENHMETARECORD*> items; |
| 470 | EnumEnhMetaFile(nullptr, emf, &EnhMetaFileProc, &items, nullptr); |
| 471 | for (const ENHMETARECORD* record : items) { |
| 472 | if (record->iType != EMR_GDICOMMENT) |
| 473 | continue; |
| 474 | |
| 475 | const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record); |
| 476 | const char* data = reinterpret_cast<const char*>(comment->Data); |
| 477 | uint16_t size = *reinterpret_cast<const uint16_t*>(data); |
| 478 | fwrite(data + sizeof(uint16_t), size, 1, fp); |
| 479 | } |
| 480 | fclose(fp); |
| 481 | DeleteEnhMetaFile(emf); |
| 482 | } |
| 483 | #endif // _WIN32 |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 484 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 485 | #ifdef PDF_ENABLE_SKIA |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 486 | static std::string WriteSkp(const char* pdf_name, |
| 487 | int num, |
| 488 | SkPictureRecorder* recorder) { |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 489 | char filename[256]; |
| 490 | int chars_formatted = |
| 491 | snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num); |
| 492 | |
| 493 | if (chars_formatted < 0 || |
| 494 | static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
| 495 | fprintf(stderr, "Filename %s is too long\n", filename); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 496 | return ""; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 497 | } |
| 498 | |
thestig | b97e07e | 2016-09-28 22:16:40 -0700 | [diff] [blame] | 499 | sk_sp<SkPicture> picture(recorder->finishRecordingAsPicture()); |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 500 | SkFILEWStream wStream(filename); |
| 501 | picture->serialize(&wStream); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 502 | return std::string(filename); |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 503 | } |
| 504 | #endif |
| 505 | |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 506 | // These example JS platform callback handlers are entirely optional, |
| 507 | // and exist here to show the flow of information from a document back |
| 508 | // to the embedder. |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 509 | int ExampleAppAlert(IPDF_JSPLATFORM*, |
| 510 | FPDF_WIDESTRING msg, |
| 511 | FPDF_WIDESTRING title, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 512 | int type, |
| 513 | int icon) { |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 514 | printf("%ls", GetPlatformWString(title).c_str()); |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 515 | if (icon || type) |
| 516 | printf("[icon=%d,type=%d]", icon, type); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 517 | printf(": %ls\n", GetPlatformWString(msg).c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 521 | int ExampleAppResponse(IPDF_JSPLATFORM*, |
| 522 | FPDF_WIDESTRING question, |
| 523 | FPDF_WIDESTRING title, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 524 | FPDF_WIDESTRING default_value, |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 525 | FPDF_WIDESTRING label, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 526 | FPDF_BOOL is_password, |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 527 | void* response, |
| 528 | int length) { |
| 529 | printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n", |
| 530 | GetPlatformWString(title).c_str(), |
| 531 | GetPlatformWString(question).c_str(), |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 532 | GetPlatformWString(default_value).c_str(), |
| 533 | GetPlatformWString(label).c_str(), is_password, length); |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 534 | |
| 535 | // UTF-16, always LE regardless of platform. |
| 536 | uint8_t* ptr = static_cast<uint8_t*>(response); |
| 537 | ptr[0] = 'N'; |
| 538 | ptr[1] = 0; |
| 539 | ptr[2] = 'o'; |
| 540 | ptr[3] = 0; |
| 541 | return 4; |
| 542 | } |
| 543 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 544 | void ExampleDocGotoPage(IPDF_JSPLATFORM*, int page_number) { |
| 545 | printf("Goto Page: %d\n", page_number); |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 548 | void ExampleDocMail(IPDF_JSPLATFORM*, |
| 549 | void* mailData, |
| 550 | int length, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 551 | FPDF_BOOL UI, |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 552 | FPDF_WIDESTRING To, |
| 553 | FPDF_WIDESTRING Subject, |
| 554 | FPDF_WIDESTRING CC, |
| 555 | FPDF_WIDESTRING BCC, |
| 556 | FPDF_WIDESTRING Msg) { |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 557 | printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", UI, |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 558 | GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(), |
| 559 | GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(), |
| 560 | GetPlatformWString(Msg).c_str()); |
| 561 | } |
| 562 | |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 563 | void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 564 | std::string feature = "Unknown"; |
| 565 | switch (type) { |
| 566 | case FPDF_UNSP_DOC_XFAFORM: |
| 567 | feature = "XFA"; |
| 568 | break; |
| 569 | case FPDF_UNSP_DOC_PORTABLECOLLECTION: |
| 570 | feature = "Portfolios_Packages"; |
| 571 | break; |
| 572 | case FPDF_UNSP_DOC_ATTACHMENT: |
| 573 | case FPDF_UNSP_ANNOT_ATTACHMENT: |
| 574 | feature = "Attachment"; |
| 575 | break; |
| 576 | case FPDF_UNSP_DOC_SECURITY: |
| 577 | feature = "Rights_Management"; |
| 578 | break; |
| 579 | case FPDF_UNSP_DOC_SHAREDREVIEW: |
| 580 | feature = "Shared_Review"; |
| 581 | break; |
| 582 | case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT: |
| 583 | case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM: |
| 584 | case FPDF_UNSP_DOC_SHAREDFORM_EMAIL: |
| 585 | feature = "Shared_Form"; |
| 586 | break; |
| 587 | case FPDF_UNSP_ANNOT_3DANNOT: |
| 588 | feature = "3D"; |
| 589 | break; |
| 590 | case FPDF_UNSP_ANNOT_MOVIE: |
| 591 | feature = "Movie"; |
| 592 | break; |
| 593 | case FPDF_UNSP_ANNOT_SOUND: |
| 594 | feature = "Sound"; |
| 595 | break; |
| 596 | case FPDF_UNSP_ANNOT_SCREEN_MEDIA: |
| 597 | case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 598 | feature = "Screen"; |
| 599 | break; |
| 600 | case FPDF_UNSP_ANNOT_SIG: |
| 601 | feature = "Digital_Signature"; |
| 602 | break; |
| 603 | } |
| 604 | printf("Unsupported feature: %s.\n", feature.c_str()); |
| 605 | } |
| 606 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 607 | bool ParseCommandLine(const std::vector<std::string>& args, |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 608 | Options* options, |
| 609 | std::vector<std::string>* files) { |
| 610 | if (args.empty()) |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 611 | return false; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 612 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 613 | options->exe_path = args[0]; |
Bo Xu | d44e392 | 2014-12-19 02:27:25 -0800 | [diff] [blame] | 614 | size_t cur_idx = 1; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 615 | for (; cur_idx < args.size(); ++cur_idx) { |
| 616 | const std::string& cur_arg = args[cur_idx]; |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 617 | if (cur_arg == "--show-config") { |
| 618 | options->show_config = true; |
Henrique Nakashima | b73ce7b | 2017-06-19 16:04:34 -0400 | [diff] [blame] | 619 | } else if (cur_arg == "--show-metadata") { |
| 620 | options->show_metadata = true; |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 621 | } else if (cur_arg == "--send-events") { |
| 622 | options->send_events = true; |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 623 | } else if (cur_arg == "--ppm") { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 624 | if (options->output_format != OUTPUT_NONE) { |
| 625 | fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); |
| 626 | return false; |
| 627 | } |
| 628 | options->output_format = OUTPUT_PPM; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 629 | } else if (cur_arg == "--png") { |
| 630 | if (options->output_format != OUTPUT_NONE) { |
| 631 | fprintf(stderr, "Duplicate or conflicting --png argument\n"); |
| 632 | return false; |
| 633 | } |
| 634 | options->output_format = OUTPUT_PNG; |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 635 | } else if (cur_arg == "--txt") { |
| 636 | if (options->output_format != OUTPUT_NONE) { |
| 637 | fprintf(stderr, "Duplicate or conflicting --txt argument\n"); |
| 638 | return false; |
| 639 | } |
| 640 | options->output_format = OUTPUT_TEXT; |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 641 | } else if (cur_arg == "--annot") { |
| 642 | if (options->output_format != OUTPUT_NONE) { |
| 643 | fprintf(stderr, "Duplicate or conflicting --annot argument\n"); |
| 644 | return false; |
| 645 | } |
| 646 | options->output_format = OUTPUT_ANNOT; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 647 | #ifdef PDF_ENABLE_SKIA |
| 648 | } else if (cur_arg == "--skp") { |
| 649 | if (options->output_format != OUTPUT_NONE) { |
| 650 | fprintf(stderr, "Duplicate or conflicting --skp argument\n"); |
| 651 | return false; |
| 652 | } |
| 653 | options->output_format = OUTPUT_SKP; |
| 654 | #endif |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 655 | } else if (cur_arg.size() > 11 && |
| 656 | cur_arg.compare(0, 11, "--font-dir=") == 0) { |
| 657 | if (!options->font_directory.empty()) { |
| 658 | fprintf(stderr, "Duplicate --font-dir argument\n"); |
| 659 | return false; |
| 660 | } |
| 661 | options->font_directory = cur_arg.substr(11); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 662 | #ifdef _WIN32 |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 663 | } else if (cur_arg == "--emf") { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 664 | if (options->output_format != OUTPUT_NONE) { |
| 665 | fprintf(stderr, "Duplicate or conflicting --emf argument\n"); |
| 666 | return false; |
| 667 | } |
| 668 | options->output_format = OUTPUT_EMF; |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 669 | } else if (cur_arg == "--ps2") { |
| 670 | if (options->output_format != OUTPUT_NONE) { |
| 671 | fprintf(stderr, "Duplicate or conflicting --ps2 argument\n"); |
| 672 | return false; |
| 673 | } |
| 674 | options->output_format = OUTPUT_PS2; |
| 675 | } else if (cur_arg == "--ps3") { |
| 676 | if (options->output_format != OUTPUT_NONE) { |
| 677 | fprintf(stderr, "Duplicate or conflicting --ps3 argument\n"); |
| 678 | return false; |
| 679 | } |
| 680 | options->output_format = OUTPUT_PS3; |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 681 | } else if (cur_arg == "--bmp") { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 682 | if (options->output_format != OUTPUT_NONE) { |
| 683 | fprintf(stderr, "Duplicate or conflicting --bmp argument\n"); |
| 684 | return false; |
| 685 | } |
| 686 | options->output_format = OUTPUT_BMP; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 687 | #endif // _WIN32 |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 688 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 689 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 690 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 691 | } else if (cur_arg.size() > 10 && |
| 692 | cur_arg.compare(0, 10, "--bin-dir=") == 0) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 693 | if (!options->bin_directory.empty()) { |
| 694 | fprintf(stderr, "Duplicate --bin-dir argument\n"); |
| 695 | return false; |
| 696 | } |
| 697 | options->bin_directory = cur_arg.substr(10); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 698 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 699 | #endif // PDF_ENABLE_V8 |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 700 | |
| 701 | } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) { |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 702 | if (!options->scale_factor_as_string.empty()) { |
| 703 | fprintf(stderr, "Duplicate --scale argument\n"); |
| 704 | return false; |
| 705 | } |
| 706 | options->scale_factor_as_string = cur_arg.substr(8); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 707 | } else if (cur_arg == "--show-structure") { |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 708 | if (options->output_format != OUTPUT_NONE) { |
| 709 | fprintf(stderr, "Duplicate or conflicting --show-structure argument\n"); |
| 710 | return false; |
| 711 | } |
| 712 | options->output_format = OUTPUT_STRUCTURE; |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 713 | } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--pages=") == 0) { |
| 714 | if (options->pages) { |
| 715 | fprintf(stderr, "Duplicate --pages argument\n"); |
| 716 | return false; |
| 717 | } |
| 718 | options->pages = true; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 719 | const std::string pages_string = cur_arg.substr(8); |
| 720 | size_t first_dash = pages_string.find("-"); |
| 721 | if (first_dash == std::string::npos) { |
| 722 | std::stringstream(pages_string) >> options->first_page; |
| 723 | options->last_page = options->first_page; |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 724 | } else { |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 725 | std::stringstream(pages_string.substr(0, first_dash)) >> |
| 726 | options->first_page; |
| 727 | std::stringstream(pages_string.substr(first_dash + 1)) >> |
| 728 | options->last_page; |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 729 | } |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 730 | } else if (cur_arg == "--md5") { |
| 731 | options->md5 = true; |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 732 | } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') { |
| 733 | fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str()); |
| 734 | return false; |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 735 | } else { |
Vitaly Buka | 8f2c3dc | 2014-08-20 10:32:36 -0700 | [diff] [blame] | 736 | break; |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 737 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 738 | } |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 739 | for (size_t i = cur_idx; i < args.size(); i++) |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 740 | files->push_back(args[i]); |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 741 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 742 | return true; |
| 743 | } |
| 744 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 745 | FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* avail, size_t offset, size_t size) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 746 | return true; |
| 747 | } |
| 748 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 749 | void Add_Segment(FX_DOWNLOADHINTS* hints, size_t offset, size_t size) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 750 | |
Lei Zhang | 54d91ec | 2017-06-16 19:08:02 -0700 | [diff] [blame] | 751 | void SendPageEvents(FPDF_FORMHANDLE form, |
| 752 | FPDF_PAGE page, |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 753 | const std::string& events) { |
| 754 | auto lines = StringSplit(events, '\n'); |
| 755 | for (auto line : lines) { |
| 756 | auto command = StringSplit(line, '#'); |
| 757 | if (command[0].empty()) |
| 758 | continue; |
| 759 | auto tokens = StringSplit(command[0], ','); |
Nicolas Pena | e30f07a | 2017-05-18 18:37:46 -0400 | [diff] [blame] | 760 | if (tokens[0] == "charcode") { |
| 761 | if (tokens.size() == 2) { |
| 762 | int keycode = atoi(tokens[1].c_str()); |
| 763 | FORM_OnChar(form, page, keycode, 0); |
| 764 | } else { |
| 765 | fprintf(stderr, "charcode: bad args\n"); |
| 766 | } |
| 767 | } else if (tokens[0] == "keycode") { |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 768 | if (tokens.size() == 2) { |
| 769 | int keycode = atoi(tokens[1].c_str()); |
| 770 | FORM_OnKeyDown(form, page, keycode, 0); |
| 771 | FORM_OnKeyUp(form, page, keycode, 0); |
| 772 | } else { |
| 773 | fprintf(stderr, "keycode: bad args\n"); |
| 774 | } |
| 775 | } else if (tokens[0] == "mousedown") { |
| 776 | if (tokens.size() == 4) { |
| 777 | int x = atoi(tokens[2].c_str()); |
| 778 | int y = atoi(tokens[3].c_str()); |
| 779 | if (tokens[1] == "left") |
| 780 | FORM_OnLButtonDown(form, page, 0, x, y); |
| 781 | #ifdef PDF_ENABLE_XFA |
| 782 | else if (tokens[1] == "right") |
| 783 | FORM_OnRButtonDown(form, page, 0, x, y); |
| 784 | #endif |
| 785 | else |
| 786 | fprintf(stderr, "mousedown: bad button name\n"); |
| 787 | } else { |
| 788 | fprintf(stderr, "mousedown: bad args\n"); |
| 789 | } |
| 790 | } else if (tokens[0] == "mouseup") { |
| 791 | if (tokens.size() == 4) { |
| 792 | int x = atoi(tokens[2].c_str()); |
| 793 | int y = atoi(tokens[3].c_str()); |
| 794 | if (tokens[1] == "left") |
| 795 | FORM_OnLButtonUp(form, page, 0, x, y); |
| 796 | #ifdef PDF_ENABLE_XFA |
| 797 | else if (tokens[1] == "right") |
| 798 | FORM_OnRButtonUp(form, page, 0, x, y); |
| 799 | #endif |
| 800 | else |
| 801 | fprintf(stderr, "mouseup: bad button name\n"); |
| 802 | } else { |
| 803 | fprintf(stderr, "mouseup: bad args\n"); |
| 804 | } |
| 805 | } else if (tokens[0] == "mousemove") { |
| 806 | if (tokens.size() == 3) { |
| 807 | int x = atoi(tokens[1].c_str()); |
| 808 | int y = atoi(tokens[2].c_str()); |
| 809 | FORM_OnMouseMove(form, page, 0, x, y); |
| 810 | } else { |
| 811 | fprintf(stderr, "mousemove: bad args\n"); |
| 812 | } |
| 813 | } else { |
| 814 | fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str()); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 819 | FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param, |
| 820 | FPDF_DOCUMENT doc, |
| 821 | int index) { |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 822 | FPDF_FORMFILLINFO_PDFiumTest* form_fill_info = |
| 823 | ToPDFiumTestFormFillInfo(param); |
| 824 | auto& loaded_pages = form_fill_info->loaded_pages; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 825 | auto iter = loaded_pages.find(index); |
| 826 | if (iter != loaded_pages.end()) |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 827 | return iter->second; |
| 828 | |
| 829 | FPDF_PAGE page = FPDF_LoadPage(doc, index); |
| 830 | if (!page) |
| 831 | return nullptr; |
| 832 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 833 | FPDF_FORMHANDLE& form_handle = form_fill_info->form_handle; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 834 | FORM_OnAfterLoadPage(page, form_handle); |
| 835 | FORM_DoPageAAction(page, form_handle, FPDFPAGE_AACTION_OPEN); |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 836 | loaded_pages[index] = page; |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 837 | return page; |
| 838 | } |
| 839 | |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 840 | std::wstring ConvertToWString(const unsigned short* buf, |
| 841 | unsigned long buf_size) { |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 842 | std::wstring result; |
| 843 | result.reserve(buf_size); |
| 844 | std::copy(buf, buf + buf_size, std::back_inserter(result)); |
| 845 | return result; |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 846 | } |
| 847 | |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 848 | void DumpChildStructure(FPDF_STRUCTELEMENT child, int indent) { |
| 849 | static const size_t kBufSize = 1024; |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 850 | unsigned short buf[kBufSize]; |
| 851 | unsigned long len = FPDF_StructElement_GetType(child, buf, kBufSize); |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 852 | printf("%*s%ls", indent * 2, "", ConvertToWString(buf, len).c_str()); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 853 | |
| 854 | memset(buf, 0, sizeof(buf)); |
dan sinclair | d9dad3a | 2017-04-06 14:44:02 -0400 | [diff] [blame] | 855 | len = FPDF_StructElement_GetTitle(child, buf, kBufSize); |
| 856 | if (len > 0) |
| 857 | printf(": '%ls'", ConvertToWString(buf, len).c_str()); |
| 858 | |
| 859 | memset(buf, 0, sizeof(buf)); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 860 | len = FPDF_StructElement_GetAltText(child, buf, kBufSize); |
| 861 | if (len > 0) |
| 862 | printf(" (%ls)", ConvertToWString(buf, len).c_str()); |
| 863 | printf("\n"); |
| 864 | |
| 865 | for (int i = 0; i < FPDF_StructElement_CountChildren(child); ++i) { |
| 866 | FPDF_STRUCTELEMENT sub_child = FPDF_StructElement_GetChildAtIndex(child, i); |
| 867 | // If the child is not an Element then this will return null. This can |
| 868 | // happen if the element is things like an object reference or a stream. |
| 869 | if (!sub_child) |
| 870 | continue; |
| 871 | |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 872 | DumpChildStructure(sub_child, indent + 1); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | void DumpPageStructure(FPDF_PAGE page, const int page_idx) { |
| 877 | std::unique_ptr<void, FPDFStructTreeDeleter> tree( |
| 878 | FPDF_StructTree_GetForPage(page)); |
| 879 | if (!tree) { |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 880 | fprintf(stderr, "Failed to load struct tree for page %d\n", page_idx); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | |
| 884 | printf("Structure Tree for Page %d\n", page_idx); |
| 885 | for (int i = 0; i < FPDF_StructTree_CountChildren(tree.get()); ++i) { |
| 886 | FPDF_STRUCTELEMENT child = FPDF_StructTree_GetChildAtIndex(tree.get(), i); |
| 887 | if (!child) { |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 888 | fprintf(stderr, "Failed to load child %d for page %d\n", i, page_idx); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 889 | continue; |
| 890 | } |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 891 | DumpChildStructure(child, 0); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 892 | } |
| 893 | printf("\n\n"); |
| 894 | } |
| 895 | |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 896 | bool RenderPage(const std::string& name, |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 897 | FPDF_DOCUMENT doc, |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 898 | FPDF_FORMHANDLE form, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 899 | FPDF_FORMFILLINFO_PDFiumTest& form_fill_info, |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 900 | const int page_index, |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 901 | const Options& options, |
| 902 | const std::string& events) { |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 903 | std::unique_ptr<void, FPDFPageDeleter> page( |
| 904 | GetPageForIndex(&form_fill_info, doc, page_index)); |
| 905 | if (!page.get()) |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 906 | return false; |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 907 | if (options.send_events) |
| 908 | SendPageEvents(form, page.get(), events); |
| 909 | if (options.output_format == OUTPUT_STRUCTURE) { |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 910 | DumpPageStructure(page.get(), page_index); |
| 911 | return true; |
| 912 | } |
| 913 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 914 | std::unique_ptr<void, FPDFTextPageDeleter> text_page( |
| 915 | FPDFText_LoadPage(page.get())); |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 916 | |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 917 | double scale = 1.0; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 918 | if (!options.scale_factor_as_string.empty()) |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 919 | std::stringstream(options.scale_factor_as_string) >> scale; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 920 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 921 | int width = static_cast<int>(FPDF_GetPageWidth(page.get()) * scale); |
| 922 | int height = static_cast<int>(FPDF_GetPageHeight(page.get()) * scale); |
| 923 | int alpha = FPDFPage_HasTransparency(page.get()) ? 1 : 0; |
| 924 | std::unique_ptr<void, FPDFBitmapDeleter> bitmap( |
| 925 | FPDFBitmap_Create(width, height, alpha)); |
| 926 | |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 927 | if (bitmap) { |
| 928 | FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF; |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 929 | FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color); |
| 930 | FPDF_RenderPageBitmap(bitmap.get(), page.get(), 0, 0, width, height, 0, |
| 931 | FPDF_ANNOT); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 932 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 933 | FPDF_FFLDraw(form, bitmap.get(), page.get(), 0, 0, width, height, 0, |
| 934 | FPDF_ANNOT); |
| 935 | int stride = FPDFBitmap_GetStride(bitmap.get()); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 936 | const char* buffer = |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 937 | reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap.get())); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 938 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 939 | std::string&& image_file_name = ""; |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 940 | switch (options.output_format) { |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 941 | #ifdef _WIN32 |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 942 | case OUTPUT_BMP: |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 943 | image_file_name = |
| 944 | WriteBmp(name.c_str(), page_index, buffer, stride, width, height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 945 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 946 | |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 947 | case OUTPUT_EMF: |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 948 | WriteEmf(page.get(), name.c_str(), page_index); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 949 | break; |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 950 | |
| 951 | case OUTPUT_PS2: |
| 952 | case OUTPUT_PS3: |
| 953 | WritePS(page.get(), name.c_str(), page_index); |
| 954 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 955 | #endif |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 956 | case OUTPUT_TEXT: |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 957 | WriteText(page.get(), name.c_str(), page_index); |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 958 | break; |
| 959 | |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 960 | case OUTPUT_ANNOT: |
| 961 | WriteAnnot(page.get(), name.c_str(), page_index); |
| 962 | break; |
| 963 | |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 964 | case OUTPUT_PNG: |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 965 | image_file_name = |
| 966 | WritePng(name.c_str(), page_index, buffer, stride, width, height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 967 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 968 | |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 969 | case OUTPUT_PPM: |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 970 | image_file_name = |
| 971 | WritePpm(name.c_str(), page_index, buffer, stride, width, height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 972 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 973 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 974 | #ifdef PDF_ENABLE_SKIA |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 975 | case OUTPUT_SKP: { |
| 976 | std::unique_ptr<SkPictureRecorder> recorder( |
thestig | b97e07e | 2016-09-28 22:16:40 -0700 | [diff] [blame] | 977 | reinterpret_cast<SkPictureRecorder*>( |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 978 | FPDF_RenderPageSkp(page.get(), width, height))); |
| 979 | FPDF_FFLRecord(form, recorder.get(), page.get(), 0, 0, width, height, 0, |
| 980 | 0); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 981 | image_file_name = WriteSkp(name.c_str(), page_index, recorder.get()); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 982 | } break; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 983 | #endif |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 984 | default: |
| 985 | break; |
| 986 | } |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 987 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 988 | // Write the filename and the MD5 of the buffer to stdout if we wrote a |
| 989 | // file. |
| 990 | if (options.md5 && image_file_name != "") |
| 991 | OutputMD5Hash(image_file_name.c_str(), buffer, stride * height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 992 | } else { |
| 993 | fprintf(stderr, "Page was too large to be rendered.\n"); |
| 994 | } |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 995 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 996 | form_fill_info.loaded_pages.erase(page_index); |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 997 | FORM_DoPageAAction(page.get(), form, FPDFPAGE_AACTION_CLOSE); |
| 998 | FORM_OnBeforeClosePage(page.get(), form); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 999 | return !!bitmap; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1000 | } |
| 1001 | |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 1002 | void RenderPdf(const std::string& name, |
| 1003 | const char* pBuf, |
| 1004 | size_t len, |
| 1005 | const Options& options, |
| 1006 | const std::string& events) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1007 | IPDF_JSPLATFORM platform_callbacks; |
| 1008 | memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 1009 | platform_callbacks.version = 3; |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 1010 | platform_callbacks.app_alert = ExampleAppAlert; |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 1011 | platform_callbacks.app_response = ExampleAppResponse; |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 1012 | platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 1013 | platform_callbacks.Doc_mail = ExampleDocMail; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1014 | |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 1015 | FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {}; |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 1016 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | ed63138 | 2014-11-18 14:10:25 -0800 | [diff] [blame] | 1017 | form_callbacks.version = 2; |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 1018 | #else // PDF_ENABLE_XFA |
| 1019 | form_callbacks.version = 1; |
| 1020 | #endif // PDF_ENABLE_XFA |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 1021 | form_callbacks.FFI_GetPage = GetPageForIndex; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1022 | form_callbacks.m_pJsPlatform = &platform_callbacks; |
| 1023 | |
| 1024 | TestLoader loader(pBuf, len); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1025 | FPDF_FILEACCESS file_access; |
| 1026 | memset(&file_access, '\0', sizeof(file_access)); |
John Abd-El-Malek | 7dc5172 | 2014-05-26 12:54:31 -0700 | [diff] [blame] | 1027 | file_access.m_FileLen = static_cast<unsigned long>(len); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 1028 | file_access.m_GetBlock = TestLoader::GetBlock; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1029 | file_access.m_Param = &loader; |
| 1030 | |
| 1031 | FX_FILEAVAIL file_avail; |
| 1032 | memset(&file_avail, '\0', sizeof(file_avail)); |
| 1033 | file_avail.version = 1; |
| 1034 | file_avail.IsDataAvail = Is_Data_Avail; |
| 1035 | |
| 1036 | FX_DOWNLOADHINTS hints; |
| 1037 | memset(&hints, '\0', sizeof(hints)); |
| 1038 | hints.version = 1; |
| 1039 | hints.AddSegment = Add_Segment; |
| 1040 | |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1041 | int nRet = PDF_DATA_NOTAVAIL; |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 1042 | bool bIsLinearized = false; |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1043 | std::unique_ptr<void, FPDFDocumentDeleter> doc; |
| 1044 | std::unique_ptr<void, FPDFAvailDeleter> pdf_avail( |
| 1045 | FPDFAvail_Create(&file_avail, &file_access)); |
Tom Sepez | c98895c | 2015-11-24 15:30:36 -0800 | [diff] [blame] | 1046 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1047 | if (FPDFAvail_IsLinearized(pdf_avail.get()) == PDF_LINEARIZED) { |
| 1048 | doc.reset(FPDFAvail_GetDocument(pdf_avail.get(), nullptr)); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1049 | if (doc) { |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1050 | while (nRet == PDF_DATA_NOTAVAIL) |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1051 | nRet = FPDFAvail_IsDocAvail(pdf_avail.get(), &hints); |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1052 | |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1053 | if (nRet == PDF_DATA_ERROR) { |
| 1054 | fprintf(stderr, "Unknown error in checking if doc was available.\n"); |
| 1055 | return; |
| 1056 | } |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1057 | nRet = FPDFAvail_IsFormAvail(pdf_avail.get(), &hints); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1058 | if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) { |
| 1059 | fprintf(stderr, |
| 1060 | "Error %d was returned in checking if form was available.\n", |
| 1061 | nRet); |
| 1062 | return; |
| 1063 | } |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 1064 | bIsLinearized = true; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1065 | } |
| 1066 | } else { |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1067 | doc.reset(FPDF_LoadCustomDocument(&file_access, nullptr)); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1068 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1069 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 1070 | if (!doc) { |
Dan Sinclair | eb815bf | 2015-10-27 13:08:41 -0400 | [diff] [blame] | 1071 | unsigned long err = FPDF_GetLastError(); |
| 1072 | fprintf(stderr, "Load pdf docs unsuccessful: "); |
| 1073 | switch (err) { |
| 1074 | case FPDF_ERR_SUCCESS: |
| 1075 | fprintf(stderr, "Success"); |
| 1076 | break; |
| 1077 | case FPDF_ERR_UNKNOWN: |
| 1078 | fprintf(stderr, "Unknown error"); |
| 1079 | break; |
| 1080 | case FPDF_ERR_FILE: |
| 1081 | fprintf(stderr, "File not found or could not be opened"); |
| 1082 | break; |
| 1083 | case FPDF_ERR_FORMAT: |
| 1084 | fprintf(stderr, "File not in PDF format or corrupted"); |
| 1085 | break; |
| 1086 | case FPDF_ERR_PASSWORD: |
| 1087 | fprintf(stderr, "Password required or incorrect password"); |
| 1088 | break; |
| 1089 | case FPDF_ERR_SECURITY: |
| 1090 | fprintf(stderr, "Unsupported security scheme"); |
| 1091 | break; |
| 1092 | case FPDF_ERR_PAGE: |
| 1093 | fprintf(stderr, "Page not found or content error"); |
| 1094 | break; |
| 1095 | default: |
| 1096 | fprintf(stderr, "Unknown error %ld", err); |
| 1097 | } |
| 1098 | fprintf(stderr, ".\n"); |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 1099 | return; |
| 1100 | } |
| 1101 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1102 | (void)FPDF_GetDocPermissions(doc.get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1103 | |
Henrique Nakashima | b73ce7b | 2017-06-19 16:04:34 -0400 | [diff] [blame] | 1104 | if (options.show_metadata) { |
| 1105 | const char* metaTags[] = {"Title", "Author", "Subject", "Keywords", |
| 1106 | "Creator", "Producer", "CreationDate", "ModDate"}; |
| 1107 | for (const char* metaTag : metaTags) { |
| 1108 | char metaBuffer[4096]; |
| 1109 | int len = FPDF_GetMetaText(doc.get(), metaTag, metaBuffer, 4096); |
| 1110 | printf("%-12s = %ls (%d bytes)\n", metaTag, |
| 1111 | GetPlatformWString(reinterpret_cast<unsigned short*>(metaBuffer)) |
| 1112 | .c_str(), |
| 1113 | len); |
| 1114 | } |
| 1115 | } |
| 1116 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1117 | std::unique_ptr<void, FPDFFormHandleDeleter> form( |
| 1118 | FPDFDOC_InitFormFillEnvironment(doc.get(), &form_callbacks)); |
| 1119 | form_callbacks.form_handle = form.get(); |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 1120 | |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 1121 | #ifdef PDF_ENABLE_XFA |
thestig | b97e07e | 2016-09-28 22:16:40 -0700 | [diff] [blame] | 1122 | int doc_type = DOCTYPE_PDF; |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1123 | if (FPDF_HasXFAField(doc.get(), &doc_type) && doc_type != DOCTYPE_PDF && |
| 1124 | !FPDF_LoadXFA(doc.get())) { |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 1125 | fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); |
Tom Sepez | 5645138 | 2014-12-05 13:30:51 -0800 | [diff] [blame] | 1126 | } |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 1127 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1128 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1129 | FPDF_SetFormFieldHighlightColor(form.get(), 0, 0xFFE4DD); |
| 1130 | FPDF_SetFormFieldHighlightAlpha(form.get(), 100); |
| 1131 | FORM_DoDocumentJSAction(form.get()); |
| 1132 | FORM_DoDocumentOpenAction(form.get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1133 | |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 1134 | #if _WIN32 |
| 1135 | if (options.output_format == OUTPUT_PS2) |
| 1136 | FPDF_SetPrintPostscriptLevel(2); |
| 1137 | else if (options.output_format == OUTPUT_PS3) |
| 1138 | FPDF_SetPrintPostscriptLevel(3); |
| 1139 | #endif |
| 1140 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1141 | int page_count = FPDF_GetPageCount(doc.get()); |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 1142 | int rendered_pages = 0; |
| 1143 | int bad_pages = 0; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 1144 | int first_page = options.pages ? options.first_page : 0; |
| 1145 | int last_page = options.pages ? options.last_page + 1 : page_count; |
| 1146 | for (int i = first_page; i < last_page; ++i) { |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1147 | if (bIsLinearized) { |
| 1148 | nRet = PDF_DATA_NOTAVAIL; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1149 | while (nRet == PDF_DATA_NOTAVAIL) |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1150 | nRet = FPDFAvail_IsPageAvail(pdf_avail.get(), i, &hints); |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1151 | |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1152 | if (nRet == PDF_DATA_ERROR) { |
| 1153 | fprintf(stderr, "Unknown error in checking if page %d is available.\n", |
| 1154 | i); |
| 1155 | return; |
| 1156 | } |
| 1157 | } |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1158 | if (RenderPage(name, doc.get(), form.get(), form_callbacks, i, options, |
| 1159 | events)) |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1160 | ++rendered_pages; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1161 | else |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 1162 | ++bad_pages; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1165 | FORM_DoDocumentAAction(form.get(), FPDFDOC_AACTION_WC); |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 1166 | fprintf(stderr, "Rendered %d pages.\n", rendered_pages); |
tsepez | 10b01bf | 2016-05-04 12:52:42 -0700 | [diff] [blame] | 1167 | if (bad_pages) |
| 1168 | fprintf(stderr, "Skipped %d bad pages.\n", bad_pages); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 1171 | static void ShowConfig() { |
| 1172 | std::string config; |
| 1173 | std::string maybe_comma; |
| 1174 | #if PDF_ENABLE_V8 |
| 1175 | config.append(maybe_comma); |
| 1176 | config.append("V8"); |
| 1177 | maybe_comma = ","; |
| 1178 | #endif // PDF_ENABLE_V8 |
| 1179 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 1180 | config.append(maybe_comma); |
| 1181 | config.append("V8_EXTERNAL"); |
| 1182 | maybe_comma = ","; |
| 1183 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 1184 | #ifdef PDF_ENABLE_XFA |
| 1185 | config.append(maybe_comma); |
| 1186 | config.append("XFA"); |
| 1187 | maybe_comma = ","; |
| 1188 | #endif // PDF_ENABLE_XFA |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 1189 | #ifdef PDF_ENABLE_ASAN |
| 1190 | config.append(maybe_comma); |
| 1191 | config.append("ASAN"); |
| 1192 | maybe_comma = ","; |
| 1193 | #endif // PDF_ENABLE_ASAN |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 1194 | printf("%s\n", config.c_str()); |
| 1195 | } |
| 1196 | |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1197 | static const char kUsageString[] = |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 1198 | "Usage: pdfium_test [OPTION] [FILE]...\n" |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 1199 | " --show-config - print build options and exit\n" |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 1200 | " --show-structure - print the structure elements from the document\n" |
tsepez | 10b01bf | 2016-05-04 12:52:42 -0700 | [diff] [blame] | 1201 | " --send-events - send input described by .evt file\n" |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 1202 | " --bin-dir=<path> - override path to v8 external data\n" |
| 1203 | " --font-dir=<path> - override path to external fonts\n" |
| 1204 | " --scale=<number> - scale output size by number (e.g. 0.5)\n" |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 1205 | " --pages=<number>(-<number>) - only render the given 0-based page(s)\n" |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 1206 | #ifdef _WIN32 |
| 1207 | " --bmp - write page images <pdf-name>.<page-number>.bmp\n" |
| 1208 | " --emf - write page meta files <pdf-name>.<page-number>.emf\n" |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 1209 | " --ps2 - write page raw PostScript (Lvl 2) <pdf-name>.<page-number>.ps\n" |
| 1210 | " --ps3 - write page raw PostScript (Lvl 3) <pdf-name>.<page-number>.ps\n" |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 1211 | #endif // _WIN32 |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1212 | " --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n" |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 1213 | " --png - write page images <pdf-name>.<page-number>.png\n" |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1214 | " --ppm - write page images <pdf-name>.<page-number>.ppm\n" |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1215 | #ifdef PDF_ENABLE_SKIA |
| 1216 | " --skp - write page images <pdf-name>.<page-number>.skp\n" |
| 1217 | #endif |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 1218 | " --md5 - write output image paths and their md5 hashes to stdout.\n" |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1219 | ""; |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 1220 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1221 | int main(int argc, const char* argv[]) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1222 | std::vector<std::string> args(argv, argv + argc); |
| 1223 | Options options; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1224 | std::vector<std::string> files; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1225 | if (!ParseCommandLine(args, &options, &files)) { |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1226 | fprintf(stderr, "%s", kUsageString); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1227 | return 1; |
| 1228 | } |
| 1229 | |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 1230 | if (options.show_config) { |
| 1231 | ShowConfig(); |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | if (files.empty()) { |
| 1236 | fprintf(stderr, "No input files.\n"); |
| 1237 | return 1; |
| 1238 | } |
| 1239 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 1240 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 1241 | v8::Platform* platform; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1242 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 1243 | v8::StartupData natives; |
| 1244 | v8::StartupData snapshot; |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 1245 | InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives, |
| 1246 | &snapshot, &platform); |
| 1247 | #else // V8_USE_EXTERNAL_STARTUP_DATA |
jochen | 9e077d2 | 2016-06-09 02:51:13 -0700 | [diff] [blame] | 1248 | InitializeV8ForPDFium(options.exe_path, &platform); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1249 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 1250 | #endif // PDF_ENABLE_V8 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1251 | |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 1252 | FPDF_LIBRARY_CONFIG config; |
| 1253 | config.version = 2; |
| 1254 | config.m_pUserFontPaths = nullptr; |
| 1255 | config.m_pIsolate = nullptr; |
| 1256 | config.m_v8EmbedderSlot = 0; |
| 1257 | |
| 1258 | const char* path_array[2]; |
| 1259 | if (!options.font_directory.empty()) { |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 1260 | path_array[0] = options.font_directory.c_str(); |
| 1261 | path_array[1] = nullptr; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 1262 | config.m_pUserFontPaths = path_array; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 1263 | } |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 1264 | FPDF_InitLibraryWithConfig(&config); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1265 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 1266 | UNSUPPORT_INFO unsupported_info; |
| 1267 | memset(&unsupported_info, '\0', sizeof(unsupported_info)); |
| 1268 | unsupported_info.version = 1; |
| 1269 | unsupported_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1270 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 1271 | FSDK_SetUnSpObjProcessHandler(&unsupported_info); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1272 | |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1273 | for (const std::string& filename : files) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1274 | size_t file_length = 0; |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 1275 | std::unique_ptr<char, pdfium::FreeDeleter> file_contents = |
| 1276 | GetFileContents(filename.c_str(), &file_length); |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 1277 | if (!file_contents) |
| 1278 | continue; |
tsepez | 10b01bf | 2016-05-04 12:52:42 -0700 | [diff] [blame] | 1279 | fprintf(stderr, "Rendering PDF file %s.\n", filename.c_str()); |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 1280 | std::string events; |
| 1281 | if (options.send_events) { |
| 1282 | std::string event_filename = filename; |
| 1283 | size_t event_length = 0; |
| 1284 | size_t extension_pos = event_filename.find(".pdf"); |
| 1285 | if (extension_pos != std::string::npos) { |
| 1286 | event_filename.replace(extension_pos, 4, ".evt"); |
thestig | f2b940c | 2016-10-13 06:48:47 -0700 | [diff] [blame] | 1287 | if (access(event_filename.c_str(), R_OK) == 0) { |
| 1288 | fprintf(stderr, "Using event file %s.\n", event_filename.c_str()); |
| 1289 | std::unique_ptr<char, pdfium::FreeDeleter> event_contents = |
| 1290 | GetFileContents(event_filename.c_str(), &event_length); |
| 1291 | if (event_contents) { |
| 1292 | fprintf(stderr, "Sending events from: %s\n", |
| 1293 | event_filename.c_str()); |
| 1294 | events = std::string(event_contents.get(), event_length); |
| 1295 | } |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | RenderPdf(filename, file_contents.get(), file_length, options, events); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | FPDF_DestroyLibrary(); |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 1303 | #ifdef PDF_ENABLE_V8 |
John Abd-El-Malek | b045ed2 | 2015-02-10 09:15:12 -0800 | [diff] [blame] | 1304 | v8::V8::ShutdownPlatform(); |
| 1305 | delete platform; |
thestig | c08cd7a | 2016-06-27 09:47:59 -0700 | [diff] [blame] | 1306 | |
| 1307 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 1308 | free(const_cast<char*>(natives.data)); |
| 1309 | free(const_cast<char*>(snapshot.data)); |
| 1310 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 1311 | #endif // PDF_ENABLE_V8 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1312 | |
| 1313 | return 0; |
| 1314 | } |