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> |
Tom Sepez | fb94728 | 2014-12-08 09:55:11 -0800 | [diff] [blame] | 9 | #include <wchar.h> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 10 | |
| 11 | #include <list> |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 12 | #include <sstream> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <utility> |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 15 | #include <vector> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 16 | |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 17 | #include "../public/fpdf_dataavail.h" |
| 18 | #include "../public/fpdf_ext.h" |
| 19 | #include "../public/fpdf_formfill.h" |
| 20 | #include "../public/fpdf_text.h" |
| 21 | #include "../public/fpdfview.h" |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 22 | #include "../testing/test_support.h" |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 23 | #include "image_diff_png.h" |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 24 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 25 | #ifdef PDF_ENABLE_V8 |
John Abd-El-Malek | b045ed2 | 2015-02-10 09:15:12 -0800 | [diff] [blame] | 26 | #include "v8/include/libplatform/libplatform.h" |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 27 | #include "v8/include/v8.h" |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 28 | #endif |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 29 | |
| 30 | #ifdef _WIN32 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 31 | #define snprintf _snprintf |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 32 | #endif |
| 33 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 34 | enum OutputFormat { |
| 35 | OUTPUT_NONE, |
| 36 | OUTPUT_PPM, |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 37 | OUTPUT_PNG, |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 38 | #ifdef _WIN32 |
| 39 | OUTPUT_BMP, |
| 40 | OUTPUT_EMF, |
| 41 | #endif |
| 42 | }; |
| 43 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 44 | struct Options { |
| 45 | Options() : output_format(OUTPUT_NONE) { } |
| 46 | |
| 47 | OutputFormat output_format; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 48 | std::string scale_factor_as_string; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 49 | std::string exe_path; |
| 50 | std::string bin_directory; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 51 | std::string font_directory; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 54 | static bool CheckDimensions(int stride, int width, int height) { |
| 55 | if (stride < 0 || width < 0 || height < 0) |
| 56 | return false; |
| 57 | if (height > 0 && width > INT_MAX / height) |
| 58 | return false; |
| 59 | return true; |
| 60 | } |
| 61 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 62 | static void WritePpm(const char* pdf_name, int num, const void* buffer_void, |
| 63 | int stride, int width, int height) { |
| 64 | const char* buffer = reinterpret_cast<const char*>(buffer_void); |
| 65 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 66 | if (!CheckDimensions(stride, width, height)) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 67 | return; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 68 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 69 | int out_len = width * height; |
| 70 | if (out_len > INT_MAX / 3) |
| 71 | return; |
| 72 | out_len *= 3; |
| 73 | |
| 74 | char filename[256]; |
| 75 | snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num); |
John Abd-El-Malek | a548d30 | 2014-06-26 10:18:11 -0700 | [diff] [blame] | 76 | FILE* fp = fopen(filename, "wb"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 77 | if (!fp) |
| 78 | return; |
| 79 | fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height); |
| 80 | // Source data is B, G, R, unused. |
| 81 | // Dest data is R, G, B. |
| 82 | char* result = new char[out_len]; |
Lei Zhang | e00660b | 2015-08-13 15:40:18 -0700 | [diff] [blame] | 83 | for (int h = 0; h < height; ++h) { |
| 84 | const char* src_line = buffer + (stride * h); |
| 85 | char* dest_line = result + (width * h * 3); |
| 86 | for (int w = 0; w < width; ++w) { |
| 87 | // R |
| 88 | dest_line[w * 3] = src_line[(w * 4) + 2]; |
| 89 | // G |
| 90 | dest_line[(w * 3) + 1] = src_line[(w * 4) + 1]; |
| 91 | // B |
| 92 | dest_line[(w * 3) + 2] = src_line[w * 4]; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 93 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 94 | } |
Lei Zhang | e00660b | 2015-08-13 15:40:18 -0700 | [diff] [blame] | 95 | fwrite(result, out_len, 1, fp); |
| 96 | delete[] result; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 97 | fclose(fp); |
| 98 | } |
| 99 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 100 | static void WritePng(const char* pdf_name, int num, const void* buffer_void, |
| 101 | int stride, int width, int height) { |
| 102 | if (!CheckDimensions(stride, width, height)) |
| 103 | return; |
| 104 | |
| 105 | std::vector<unsigned char> png_encoding; |
| 106 | const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void); |
| 107 | if (!image_diff_png::EncodeBGRAPNG( |
| 108 | buffer, width, height, stride, false, &png_encoding)) { |
| 109 | fprintf(stderr, "Failed to convert bitmap to PNG\n"); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | char filename[256]; |
| 114 | int chars_formatted = snprintf( |
| 115 | filename, sizeof(filename), "%s.%d.png", pdf_name, num); |
| 116 | if (chars_formatted < 0 || |
| 117 | static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
| 118 | fprintf(stderr, "Filname %s is too long\n", filename); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | FILE* fp = fopen(filename, "wb"); |
| 123 | if (!fp) { |
| 124 | fprintf(stderr, "Failed to open %s for output\n", filename); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | size_t bytes_written = fwrite( |
| 129 | &png_encoding.front(), 1, png_encoding.size(), fp); |
| 130 | if (bytes_written != png_encoding.size()) |
| 131 | fprintf(stderr, "Failed to write to %s\n", filename); |
| 132 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 133 | (void)fclose(fp); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 136 | #ifdef _WIN32 |
| 137 | static void WriteBmp(const char* pdf_name, int num, const void* buffer, |
| 138 | int stride, int width, int height) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 139 | if (!CheckDimensions(stride, width, height)) |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 140 | return; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 141 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 142 | int out_len = stride * height; |
| 143 | if (out_len > INT_MAX / 3) |
| 144 | return; |
| 145 | |
| 146 | char filename[256]; |
| 147 | snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num); |
| 148 | FILE* fp = fopen(filename, "wb"); |
| 149 | if (!fp) |
| 150 | return; |
| 151 | |
Nico Weber | 2827bdd | 2015-07-01 14:08:08 -0700 | [diff] [blame] | 152 | BITMAPINFO bmi = {}; |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 153 | bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD); |
| 154 | bmi.bmiHeader.biWidth = width; |
| 155 | bmi.bmiHeader.biHeight = -height; // top-down image |
| 156 | bmi.bmiHeader.biPlanes = 1; |
| 157 | bmi.bmiHeader.biBitCount = 32; |
| 158 | bmi.bmiHeader.biCompression = BI_RGB; |
| 159 | bmi.bmiHeader.biSizeImage = 0; |
| 160 | |
Nico Weber | 2827bdd | 2015-07-01 14:08:08 -0700 | [diff] [blame] | 161 | BITMAPFILEHEADER file_header = {}; |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 162 | file_header.bfType = 0x4d42; |
| 163 | file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len; |
| 164 | file_header.bfOffBits = file_header.bfSize - out_len; |
| 165 | |
| 166 | fwrite(&file_header, sizeof(file_header), 1, fp); |
| 167 | fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp); |
| 168 | fwrite(buffer, out_len, 1, fp); |
| 169 | fclose(fp); |
| 170 | } |
| 171 | |
| 172 | void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { |
| 173 | int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 174 | int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 175 | |
| 176 | char filename[256]; |
| 177 | snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num); |
| 178 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 179 | HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 180 | |
| 181 | HRGN rgn = CreateRectRgn(0, 0, width, height); |
| 182 | SelectClipRgn(dc, rgn); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 183 | DeleteObject(rgn); |
| 184 | |
| 185 | SelectObject(dc, GetStockObject(NULL_PEN)); |
| 186 | SelectObject(dc, GetStockObject(WHITE_BRUSH)); |
| 187 | // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. |
| 188 | Rectangle(dc, 0, 0, width + 1, height + 1); |
| 189 | |
| 190 | FPDF_RenderPage(dc, page, 0, 0, width, height, 0, |
| 191 | FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); |
| 192 | |
| 193 | DeleteEnhMetaFile(CloseEnhMetaFile(dc)); |
| 194 | } |
| 195 | #endif |
| 196 | |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 197 | int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING, |
| 198 | int, int) { |
Lei Zhang | 79e893a | 2015-11-04 16:02:47 -0800 | [diff] [blame^] | 199 | std::wstring platform_string = GetWideString(msg); |
| 200 | printf("Alert: %ls\n", platform_string.c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 204 | void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) { |
| 205 | printf("Goto Page: %d\n", pageNumber); |
| 206 | } |
| 207 | |
| 208 | void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 209 | std::string feature = "Unknown"; |
| 210 | switch (type) { |
| 211 | case FPDF_UNSP_DOC_XFAFORM: |
| 212 | feature = "XFA"; |
| 213 | break; |
| 214 | case FPDF_UNSP_DOC_PORTABLECOLLECTION: |
| 215 | feature = "Portfolios_Packages"; |
| 216 | break; |
| 217 | case FPDF_UNSP_DOC_ATTACHMENT: |
| 218 | case FPDF_UNSP_ANNOT_ATTACHMENT: |
| 219 | feature = "Attachment"; |
| 220 | break; |
| 221 | case FPDF_UNSP_DOC_SECURITY: |
| 222 | feature = "Rights_Management"; |
| 223 | break; |
| 224 | case FPDF_UNSP_DOC_SHAREDREVIEW: |
| 225 | feature = "Shared_Review"; |
| 226 | break; |
| 227 | case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT: |
| 228 | case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM: |
| 229 | case FPDF_UNSP_DOC_SHAREDFORM_EMAIL: |
| 230 | feature = "Shared_Form"; |
| 231 | break; |
| 232 | case FPDF_UNSP_ANNOT_3DANNOT: |
| 233 | feature = "3D"; |
| 234 | break; |
| 235 | case FPDF_UNSP_ANNOT_MOVIE: |
| 236 | feature = "Movie"; |
| 237 | break; |
| 238 | case FPDF_UNSP_ANNOT_SOUND: |
| 239 | feature = "Sound"; |
| 240 | break; |
| 241 | case FPDF_UNSP_ANNOT_SCREEN_MEDIA: |
| 242 | case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 243 | feature = "Screen"; |
| 244 | break; |
| 245 | case FPDF_UNSP_ANNOT_SIG: |
| 246 | feature = "Digital_Signature"; |
| 247 | break; |
| 248 | } |
| 249 | printf("Unsupported feature: %s.\n", feature.c_str()); |
| 250 | } |
| 251 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 252 | bool ParseCommandLine(const std::vector<std::string>& args, |
| 253 | Options* options, std::list<std::string>* files) { |
| 254 | if (args.empty()) { |
| 255 | return false; |
| 256 | } |
| 257 | options->exe_path = args[0]; |
Bo Xu | d44e392 | 2014-12-19 02:27:25 -0800 | [diff] [blame] | 258 | size_t cur_idx = 1; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 259 | for (; cur_idx < args.size(); ++cur_idx) { |
| 260 | const std::string& cur_arg = args[cur_idx]; |
| 261 | if (cur_arg == "--ppm") { |
| 262 | if (options->output_format != OUTPUT_NONE) { |
| 263 | fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); |
| 264 | return false; |
| 265 | } |
| 266 | options->output_format = OUTPUT_PPM; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 267 | } else if (cur_arg == "--png") { |
| 268 | if (options->output_format != OUTPUT_NONE) { |
| 269 | fprintf(stderr, "Duplicate or conflicting --png argument\n"); |
| 270 | return false; |
| 271 | } |
| 272 | options->output_format = OUTPUT_PNG; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 273 | } else if (cur_arg.size() > 11 && |
| 274 | cur_arg.compare(0, 11, "--font-dir=") == 0) { |
| 275 | if (!options->font_directory.empty()) { |
| 276 | fprintf(stderr, "Duplicate --font-dir argument\n"); |
| 277 | return false; |
| 278 | } |
| 279 | options->font_directory = cur_arg.substr(11); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 280 | } |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 281 | #ifdef _WIN32 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 282 | else if (cur_arg == "--emf") { |
| 283 | if (options->output_format != OUTPUT_NONE) { |
| 284 | fprintf(stderr, "Duplicate or conflicting --emf argument\n"); |
| 285 | return false; |
| 286 | } |
| 287 | options->output_format = OUTPUT_EMF; |
| 288 | } |
| 289 | else if (cur_arg == "--bmp") { |
| 290 | if (options->output_format != OUTPUT_NONE) { |
| 291 | fprintf(stderr, "Duplicate or conflicting --bmp argument\n"); |
| 292 | return false; |
| 293 | } |
| 294 | options->output_format = OUTPUT_BMP; |
| 295 | } |
| 296 | #endif // _WIN32 |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 297 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 298 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 299 | else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) { |
| 300 | if (!options->bin_directory.empty()) { |
| 301 | fprintf(stderr, "Duplicate --bin-dir argument\n"); |
| 302 | return false; |
| 303 | } |
| 304 | options->bin_directory = cur_arg.substr(10); |
| 305 | } |
| 306 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 307 | #endif // PDF_ENABLE_V8 |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 308 | else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) { |
| 309 | if (!options->scale_factor_as_string.empty()) { |
| 310 | fprintf(stderr, "Duplicate --scale argument\n"); |
| 311 | return false; |
| 312 | } |
| 313 | options->scale_factor_as_string = cur_arg.substr(8); |
| 314 | } |
Vitaly Buka | 8f2c3dc | 2014-08-20 10:32:36 -0700 | [diff] [blame] | 315 | else |
| 316 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 317 | } |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 318 | if (cur_idx >= args.size()) { |
| 319 | fprintf(stderr, "No input files.\n"); |
Vitaly Buka | 8f2c3dc | 2014-08-20 10:32:36 -0700 | [diff] [blame] | 320 | return false; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 321 | } |
Bo Xu | d44e392 | 2014-12-19 02:27:25 -0800 | [diff] [blame] | 322 | for (size_t i = cur_idx; i < args.size(); i++) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 323 | files->push_back(args[i]); |
| 324 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 325 | return true; |
| 326 | } |
| 327 | |
Tom Sepez | cf22eb8 | 2015-05-12 17:28:08 -0700 | [diff] [blame] | 328 | FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 329 | return true; |
| 330 | } |
| 331 | |
| 332 | void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
| 333 | } |
| 334 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 335 | void RenderPdf(const std::string& name, const char* pBuf, size_t len, |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 336 | const Options& options) { |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 337 | fprintf(stderr, "Rendering PDF file %s.\n", name.c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 338 | |
| 339 | IPDF_JSPLATFORM platform_callbacks; |
| 340 | memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 341 | platform_callbacks.version = 3; |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 342 | platform_callbacks.app_alert = ExampleAppAlert; |
| 343 | platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 344 | |
| 345 | FPDF_FORMFILLINFO form_callbacks; |
| 346 | memset(&form_callbacks, '\0', sizeof(form_callbacks)); |
Tom Sepez | ed63138 | 2014-11-18 14:10:25 -0800 | [diff] [blame] | 347 | form_callbacks.version = 2; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 348 | form_callbacks.m_pJsPlatform = &platform_callbacks; |
| 349 | |
| 350 | TestLoader loader(pBuf, len); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 351 | FPDF_FILEACCESS file_access; |
| 352 | memset(&file_access, '\0', sizeof(file_access)); |
John Abd-El-Malek | 7dc5172 | 2014-05-26 12:54:31 -0700 | [diff] [blame] | 353 | file_access.m_FileLen = static_cast<unsigned long>(len); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 354 | file_access.m_GetBlock = TestLoader::GetBlock; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 355 | file_access.m_Param = &loader; |
| 356 | |
| 357 | FX_FILEAVAIL file_avail; |
| 358 | memset(&file_avail, '\0', sizeof(file_avail)); |
| 359 | file_avail.version = 1; |
| 360 | file_avail.IsDataAvail = Is_Data_Avail; |
| 361 | |
| 362 | FX_DOWNLOADHINTS hints; |
| 363 | memset(&hints, '\0', sizeof(hints)); |
| 364 | hints.version = 1; |
| 365 | hints.AddSegment = Add_Segment; |
| 366 | |
| 367 | FPDF_DOCUMENT doc; |
| 368 | FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); |
| 369 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 370 | (void)FPDFAvail_IsDocAvail(pdf_avail, &hints); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 371 | |
Dan Sinclair | d3fca80 | 2015-10-29 09:26:51 -0400 | [diff] [blame] | 372 | if (FPDFAvail_IsLinearized(pdf_avail)) |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 373 | doc = FPDFAvail_GetDocument(pdf_avail, nullptr); |
Dan Sinclair | d3fca80 | 2015-10-29 09:26:51 -0400 | [diff] [blame] | 374 | else |
Lei Zhang | 600d407 | 2015-09-23 15:35:25 -0700 | [diff] [blame] | 375 | doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 376 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 377 | if (!doc) { |
Dan Sinclair | eb815bf | 2015-10-27 13:08:41 -0400 | [diff] [blame] | 378 | unsigned long err = FPDF_GetLastError(); |
| 379 | fprintf(stderr, "Load pdf docs unsuccessful: "); |
| 380 | switch (err) { |
| 381 | case FPDF_ERR_SUCCESS: |
| 382 | fprintf(stderr, "Success"); |
| 383 | break; |
| 384 | case FPDF_ERR_UNKNOWN: |
| 385 | fprintf(stderr, "Unknown error"); |
| 386 | break; |
| 387 | case FPDF_ERR_FILE: |
| 388 | fprintf(stderr, "File not found or could not be opened"); |
| 389 | break; |
| 390 | case FPDF_ERR_FORMAT: |
| 391 | fprintf(stderr, "File not in PDF format or corrupted"); |
| 392 | break; |
| 393 | case FPDF_ERR_PASSWORD: |
| 394 | fprintf(stderr, "Password required or incorrect password"); |
| 395 | break; |
| 396 | case FPDF_ERR_SECURITY: |
| 397 | fprintf(stderr, "Unsupported security scheme"); |
| 398 | break; |
| 399 | case FPDF_ERR_PAGE: |
| 400 | fprintf(stderr, "Page not found or content error"); |
| 401 | break; |
| 402 | default: |
| 403 | fprintf(stderr, "Unknown error %ld", err); |
| 404 | } |
| 405 | fprintf(stderr, ".\n"); |
| 406 | |
Qin Zhao | 0522497 | 2015-10-20 18:31:06 -0400 | [diff] [blame] | 407 | FPDFAvail_Destroy(pdf_avail); |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 408 | return; |
| 409 | } |
| 410 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 411 | (void)FPDF_GetDocPermissions(doc); |
| 412 | (void)FPDFAvail_IsFormAvail(pdf_avail, &hints); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 413 | |
Bo Xu | 2b7a49d | 2014-11-14 17:40:50 -0800 | [diff] [blame] | 414 | FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 415 | int docType = DOCTYPE_PDF; |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 416 | if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF && |
| 417 | !FPDF_LoadXFA(doc)) { |
| 418 | fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); |
Tom Sepez | 5645138 | 2014-12-05 13:30:51 -0800 | [diff] [blame] | 419 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 420 | FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); |
| 421 | FPDF_SetFormFieldHighlightAlpha(form, 100); |
| 422 | |
| 423 | int first_page = FPDFAvail_GetFirstPageNum(doc); |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 424 | (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 425 | |
| 426 | int page_count = FPDF_GetPageCount(doc); |
| 427 | for (int i = 0; i < page_count; ++i) { |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 428 | (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | FORM_DoDocumentJSAction(form); |
| 432 | FORM_DoDocumentOpenAction(form); |
| 433 | |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 434 | int rendered_pages = 0; |
| 435 | int bad_pages = 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 436 | for (int i = 0; i < page_count; ++i) { |
| 437 | FPDF_PAGE page = FPDF_LoadPage(doc, i); |
Jun Fang | aeacba4 | 2014-08-22 17:04:29 -0700 | [diff] [blame] | 438 | if (!page) { |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 439 | ++bad_pages; |
| 440 | continue; |
Jun Fang | aeacba4 | 2014-08-22 17:04:29 -0700 | [diff] [blame] | 441 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 442 | FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); |
| 443 | FORM_OnAfterLoadPage(page, form); |
| 444 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
| 445 | |
John Abd-El-Malek | 8a7b669 | 2015-02-25 11:08:16 -0800 | [diff] [blame] | 446 | double scale = 1.0; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 447 | if (!options.scale_factor_as_string.empty()) { |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 448 | std::stringstream(options.scale_factor_as_string) >> scale; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 449 | } |
John Abd-El-Malek | 8a7b669 | 2015-02-25 11:08:16 -0800 | [diff] [blame] | 450 | int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); |
| 451 | int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 452 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 453 | FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); |
Tom Sepez | bad79b3 | 2015-05-08 12:02:05 -0700 | [diff] [blame] | 454 | if (!bitmap) { |
| 455 | fprintf(stderr, "Page was too large to be rendered.\n"); |
| 456 | bad_pages++; |
| 457 | continue; |
| 458 | } |
| 459 | |
Lei Zhang | 532a6a7 | 2014-07-09 11:47:15 -0700 | [diff] [blame] | 460 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 461 | FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 462 | ++rendered_pages; |
Jun Fang | aeacba4 | 2014-08-22 17:04:29 -0700 | [diff] [blame] | 463 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 464 | FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 465 | int stride = FPDFBitmap_GetStride(bitmap); |
| 466 | const char* buffer = |
| 467 | reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); |
| 468 | |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 469 | switch (options.output_format) { |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 470 | #ifdef _WIN32 |
| 471 | case OUTPUT_BMP: |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 472 | WriteBmp(name.c_str(), i, buffer, stride, width, height); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 473 | break; |
| 474 | |
| 475 | case OUTPUT_EMF: |
Tom Sepez | c151fbb | 2014-12-18 11:14:19 -0800 | [diff] [blame] | 476 | WriteEmf(page, name.c_str(), i); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 477 | break; |
| 478 | #endif |
| 479 | case OUTPUT_PPM: |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 480 | WritePpm(name.c_str(), i, buffer, stride, width, height); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 481 | break; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 482 | |
| 483 | case OUTPUT_PNG: |
| 484 | WritePng(name.c_str(), i, buffer, stride, width, height); |
| 485 | break; |
| 486 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 487 | default: |
| 488 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | FPDFBitmap_Destroy(bitmap); |
| 492 | |
| 493 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 494 | FORM_OnBeforeClosePage(page, form); |
| 495 | FPDFText_ClosePage(text_page); |
| 496 | FPDF_ClosePage(page); |
| 497 | } |
| 498 | |
| 499 | FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
Lei Zhang | ba02691 | 2015-07-16 10:06:11 -0700 | [diff] [blame] | 500 | |
| 501 | // Note: The shut down order here is the reverse of the non-XFA branch order. |
| 502 | // Need to work out if this is required, and if it is, the lifetimes of |
| 503 | // objects owned by |doc| that |form| reference. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 504 | FPDF_CloseDocument(doc); |
Bo Xu | 2b7a49d | 2014-11-14 17:40:50 -0800 | [diff] [blame] | 505 | FPDFDOC_ExitFormFillEnvironment(form); |
Lei Zhang | ba02691 | 2015-07-16 10:06:11 -0700 | [diff] [blame] | 506 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 507 | FPDFAvail_Destroy(pdf_avail); |
| 508 | |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 509 | fprintf(stderr, "Rendered %d pages.\n", rendered_pages); |
| 510 | fprintf(stderr, "Skipped %d bad pages.\n", bad_pages); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 513 | static const char usage_string[] = |
| 514 | "Usage: pdfium_test [OPTION] [FILE]...\n" |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 515 | " --bin-dir=<path> - override path to v8 external data\n" |
| 516 | " --font-dir=<path> - override path to external fonts\n" |
| 517 | " --scale=<number> - scale output size by number (e.g. 0.5)\n" |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 518 | #ifdef _WIN32 |
| 519 | " --bmp - write page images <pdf-name>.<page-number>.bmp\n" |
| 520 | " --emf - write page meta files <pdf-name>.<page-number>.emf\n" |
| 521 | #endif |
| 522 | " --png - write page images <pdf-name>.<page-number>.png\n" |
| 523 | " --ppm - write page images <pdf-name>.<page-number>.ppm\n"; |
| 524 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 525 | int main(int argc, const char* argv[]) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 526 | std::vector<std::string> args(argv, argv + argc); |
| 527 | Options options; |
| 528 | std::list<std::string> files; |
| 529 | if (!ParseCommandLine(args, &options, &files)) { |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 530 | fprintf(stderr, "%s", usage_string); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 531 | return 1; |
| 532 | } |
| 533 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 534 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 535 | v8::Platform* platform; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 536 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 537 | v8::StartupData natives; |
| 538 | v8::StartupData snapshot; |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 539 | InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives, |
| 540 | &snapshot, &platform); |
| 541 | #else // V8_USE_EXTERNAL_STARTUP_DATA |
| 542 | InitializeV8ForPDFium(&platform); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 543 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 544 | #endif // PDF_ENABLE_V8 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 545 | |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 546 | FPDF_LIBRARY_CONFIG config; |
| 547 | config.version = 2; |
| 548 | config.m_pUserFontPaths = nullptr; |
| 549 | config.m_pIsolate = nullptr; |
| 550 | config.m_v8EmbedderSlot = 0; |
| 551 | |
| 552 | const char* path_array[2]; |
| 553 | if (!options.font_directory.empty()) { |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 554 | path_array[0] = options.font_directory.c_str(); |
| 555 | path_array[1] = nullptr; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 556 | config.m_pUserFontPaths = path_array; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 557 | } |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 558 | FPDF_InitLibraryWithConfig(&config); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 559 | |
| 560 | UNSUPPORT_INFO unsuppored_info; |
| 561 | memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
| 562 | unsuppored_info.version = 1; |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 563 | unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 564 | |
| 565 | FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 566 | |
| 567 | while (!files.empty()) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 568 | std::string filename = files.front(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 569 | files.pop_front(); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 570 | size_t file_length = 0; |
| 571 | char* file_contents = GetFileContents(filename.c_str(), &file_length); |
| 572 | if (!file_contents) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 573 | continue; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 574 | RenderPdf(filename, file_contents, file_length, options); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 575 | free(file_contents); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | FPDF_DestroyLibrary(); |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 579 | #ifdef PDF_ENABLE_V8 |
John Abd-El-Malek | b045ed2 | 2015-02-10 09:15:12 -0800 | [diff] [blame] | 580 | v8::V8::ShutdownPlatform(); |
| 581 | delete platform; |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 582 | #endif // PDF_ENABLE_V8 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 583 | |
| 584 | return 0; |
| 585 | } |