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