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> |
| 12 | #include <string> |
| 13 | #include <utility> |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 14 | #include <vector> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 15 | |
John Abd-El-Malek | 197fd8d | 2014-05-23 19:27:48 -0700 | [diff] [blame] | 16 | #include "../fpdfsdk/include/fpdf_dataavail.h" |
| 17 | #include "../fpdfsdk/include/fpdf_ext.h" |
| 18 | #include "../fpdfsdk/include/fpdfformfill.h" |
| 19 | #include "../fpdfsdk/include/fpdftext.h" |
| 20 | #include "../fpdfsdk/include/fpdfview.h" |
Bruce Dawson | ddc2062 | 2014-11-18 13:42:28 -0800 | [diff] [blame] | 21 | #include "../core/include/fxcrt/fx_system.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 22 | #include "v8/include/v8.h" |
| 23 | |
| 24 | #ifdef _WIN32 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 25 | #define snprintf _snprintf |
| 26 | #define PATH_SEPARATOR '\\' |
| 27 | #else |
| 28 | #define PATH_SEPARATOR '/' |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 29 | #endif |
| 30 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 31 | enum OutputFormat { |
| 32 | OUTPUT_NONE, |
| 33 | OUTPUT_PPM, |
| 34 | #ifdef _WIN32 |
| 35 | OUTPUT_BMP, |
| 36 | OUTPUT_EMF, |
| 37 | #endif |
| 38 | }; |
| 39 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 40 | struct Options { |
| 41 | Options() : output_format(OUTPUT_NONE) { } |
| 42 | |
| 43 | OutputFormat output_format; |
| 44 | std::string exe_path; |
| 45 | std::string bin_directory; |
| 46 | }; |
| 47 | |
| 48 | // Reads the entire contents of a file into a newly malloc'd buffer. |
| 49 | static char* GetFileContents(const char* filename, size_t* retlen) { |
| 50 | FILE* file = fopen(filename, "rb"); |
| 51 | if (!file) { |
| 52 | fprintf(stderr, "Failed to open: %s\n", filename); |
| 53 | return NULL; |
| 54 | } |
| 55 | (void) fseek(file, 0, SEEK_END); |
| 56 | size_t file_length = ftell(file); |
| 57 | if (!file_length) { |
| 58 | return NULL; |
| 59 | } |
| 60 | (void) fseek(file, 0, SEEK_SET); |
| 61 | char* buffer = (char*) malloc(file_length); |
| 62 | if (!buffer) { |
| 63 | return NULL; |
| 64 | } |
| 65 | size_t bytes_read = fread(buffer, 1, file_length, file); |
| 66 | (void) fclose(file); |
| 67 | if (bytes_read != file_length) { |
| 68 | fprintf(stderr, "Failed to read: %s\n", filename); |
| 69 | free(buffer); |
| 70 | return NULL; |
| 71 | } |
| 72 | *retlen = bytes_read; |
| 73 | return buffer; |
| 74 | } |
| 75 | |
| 76 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 77 | // Returns the full path for an external V8 data file based on either |
| 78 | // the currect exectuable path or an explicit override. |
| 79 | static std::string GetFullPathForSnapshotFile(const Options& options, |
| 80 | const std::string& filename) { |
| 81 | std::string result; |
| 82 | if (!options.bin_directory.empty()) { |
| 83 | result = options.bin_directory; |
| 84 | if (*options.bin_directory.rbegin() != PATH_SEPARATOR) { |
| 85 | result += PATH_SEPARATOR; |
| 86 | } |
| 87 | } else if (!options.exe_path.empty()) { |
| 88 | size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR); |
| 89 | if (last_separator != std::string::npos) { |
| 90 | result = options.exe_path.substr(0, last_separator + 1); |
| 91 | } |
| 92 | } |
| 93 | result += filename; |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | // Reads an extenal V8 data file from the |options|-indicated location, |
| 98 | // returing true on success and false on error. |
| 99 | static bool GetExternalData(const Options& options, |
| 100 | const std::string& bin_filename, |
| 101 | v8::StartupData* result_data) { |
| 102 | std::string full_path = GetFullPathForSnapshotFile(options, bin_filename); |
| 103 | size_t data_length = 0; |
| 104 | char* data_buffer = GetFileContents(full_path.c_str(), &data_length); |
| 105 | if (!data_buffer) { |
| 106 | return false; |
| 107 | } |
| 108 | result_data->data = const_cast<const char*>(data_buffer); |
| 109 | result_data->raw_size = data_length; |
| 110 | return true; |
| 111 | } |
| 112 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 113 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 114 | static void WritePpm(const char* pdf_name, int num, const void* buffer_void, |
| 115 | int stride, int width, int height) { |
| 116 | const char* buffer = reinterpret_cast<const char*>(buffer_void); |
| 117 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 118 | if (stride < 0 || width < 0 || height < 0) |
| 119 | return; |
| 120 | if (height > 0 && width > INT_MAX / height) |
| 121 | return; |
| 122 | int out_len = width * height; |
| 123 | if (out_len > INT_MAX / 3) |
| 124 | return; |
| 125 | out_len *= 3; |
| 126 | |
| 127 | char filename[256]; |
| 128 | snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num); |
John Abd-El-Malek | a548d30 | 2014-06-26 10:18:11 -0700 | [diff] [blame] | 129 | FILE* fp = fopen(filename, "wb"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 130 | if (!fp) |
| 131 | return; |
| 132 | fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height); |
| 133 | // Source data is B, G, R, unused. |
| 134 | // Dest data is R, G, B. |
| 135 | char* result = new char[out_len]; |
| 136 | if (result) { |
| 137 | for (int h = 0; h < height; ++h) { |
| 138 | const char* src_line = buffer + (stride * h); |
| 139 | char* dest_line = result + (width * h * 3); |
| 140 | for (int w = 0; w < width; ++w) { |
| 141 | // R |
| 142 | dest_line[w * 3] = src_line[(w * 4) + 2]; |
| 143 | // G |
| 144 | dest_line[(w * 3) + 1] = src_line[(w * 4) + 1]; |
| 145 | // B |
| 146 | dest_line[(w * 3) + 2] = src_line[w * 4]; |
| 147 | } |
| 148 | } |
| 149 | fwrite(result, out_len, 1, fp); |
| 150 | delete [] result; |
| 151 | } |
| 152 | fclose(fp); |
| 153 | } |
| 154 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 155 | #ifdef _WIN32 |
| 156 | static void WriteBmp(const char* pdf_name, int num, const void* buffer, |
| 157 | int stride, int width, int height) { |
| 158 | if (stride < 0 || width < 0 || height < 0) |
| 159 | return; |
| 160 | if (height > 0 && width > INT_MAX / height) |
| 161 | return; |
| 162 | int out_len = stride * height; |
| 163 | if (out_len > INT_MAX / 3) |
| 164 | return; |
| 165 | |
| 166 | char filename[256]; |
| 167 | snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num); |
| 168 | FILE* fp = fopen(filename, "wb"); |
| 169 | if (!fp) |
| 170 | return; |
| 171 | |
| 172 | BITMAPINFO bmi = {0}; |
| 173 | bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD); |
| 174 | bmi.bmiHeader.biWidth = width; |
| 175 | bmi.bmiHeader.biHeight = -height; // top-down image |
| 176 | bmi.bmiHeader.biPlanes = 1; |
| 177 | bmi.bmiHeader.biBitCount = 32; |
| 178 | bmi.bmiHeader.biCompression = BI_RGB; |
| 179 | bmi.bmiHeader.biSizeImage = 0; |
| 180 | |
| 181 | BITMAPFILEHEADER file_header = {0}; |
| 182 | file_header.bfType = 0x4d42; |
| 183 | file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len; |
| 184 | file_header.bfOffBits = file_header.bfSize - out_len; |
| 185 | |
| 186 | fwrite(&file_header, sizeof(file_header), 1, fp); |
| 187 | fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp); |
| 188 | fwrite(buffer, out_len, 1, fp); |
| 189 | fclose(fp); |
| 190 | } |
| 191 | |
| 192 | void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { |
| 193 | int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 194 | int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 195 | |
| 196 | char filename[256]; |
| 197 | snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num); |
| 198 | |
| 199 | HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL); |
| 200 | |
| 201 | HRGN rgn = CreateRectRgn(0, 0, width, height); |
| 202 | SelectClipRgn(dc, rgn); |
| 203 | DeleteObject(rgn); |
| 204 | |
| 205 | SelectObject(dc, GetStockObject(NULL_PEN)); |
| 206 | SelectObject(dc, GetStockObject(WHITE_BRUSH)); |
| 207 | // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. |
| 208 | Rectangle(dc, 0, 0, width + 1, height + 1); |
| 209 | |
| 210 | FPDF_RenderPage(dc, page, 0, 0, width, height, 0, |
| 211 | FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); |
| 212 | |
| 213 | DeleteEnhMetaFile(CloseEnhMetaFile(dc)); |
| 214 | } |
| 215 | #endif |
| 216 | |
Tom Sepez | fb94728 | 2014-12-08 09:55:11 -0800 | [diff] [blame] | 217 | int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING, |
| 218 | int, int) { |
| 219 | // Deal with differences between UTF16LE and wchar_t on this platform. |
| 220 | size_t characters = 0; |
| 221 | while (msg[characters]) { |
| 222 | ++characters; |
| 223 | } |
| 224 | wchar_t* platform_string = |
| 225 | (wchar_t*)malloc((characters + 1) * sizeof(wchar_t)); |
| 226 | for (size_t i = 0; i < characters + 1; ++i) { |
| 227 | unsigned char* ptr = (unsigned char*)&msg[i]; |
| 228 | platform_string[i] = ptr[0] + 256 * ptr[1]; |
| 229 | } |
| 230 | printf("Alert: %ls\n", platform_string); |
| 231 | free(platform_string); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | void Unsupported_Handler(UNSUPPORT_INFO*, int type) { |
| 236 | std::string feature = "Unknown"; |
| 237 | switch (type) { |
| 238 | case FPDF_UNSP_DOC_XFAFORM: |
| 239 | feature = "XFA"; |
| 240 | break; |
| 241 | case FPDF_UNSP_DOC_PORTABLECOLLECTION: |
| 242 | feature = "Portfolios_Packages"; |
| 243 | break; |
| 244 | case FPDF_UNSP_DOC_ATTACHMENT: |
| 245 | case FPDF_UNSP_ANNOT_ATTACHMENT: |
| 246 | feature = "Attachment"; |
| 247 | break; |
| 248 | case FPDF_UNSP_DOC_SECURITY: |
| 249 | feature = "Rights_Management"; |
| 250 | break; |
| 251 | case FPDF_UNSP_DOC_SHAREDREVIEW: |
| 252 | feature = "Shared_Review"; |
| 253 | break; |
| 254 | case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT: |
| 255 | case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM: |
| 256 | case FPDF_UNSP_DOC_SHAREDFORM_EMAIL: |
| 257 | feature = "Shared_Form"; |
| 258 | break; |
| 259 | case FPDF_UNSP_ANNOT_3DANNOT: |
| 260 | feature = "3D"; |
| 261 | break; |
| 262 | case FPDF_UNSP_ANNOT_MOVIE: |
| 263 | feature = "Movie"; |
| 264 | break; |
| 265 | case FPDF_UNSP_ANNOT_SOUND: |
| 266 | feature = "Sound"; |
| 267 | break; |
| 268 | case FPDF_UNSP_ANNOT_SCREEN_MEDIA: |
| 269 | case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 270 | feature = "Screen"; |
| 271 | break; |
| 272 | case FPDF_UNSP_ANNOT_SIG: |
| 273 | feature = "Digital_Signature"; |
| 274 | break; |
| 275 | } |
| 276 | printf("Unsupported feature: %s.\n", feature.c_str()); |
| 277 | } |
| 278 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 279 | bool ParseCommandLine(const std::vector<std::string>& args, |
| 280 | Options* options, std::list<std::string>* files) { |
| 281 | if (args.empty()) { |
| 282 | return false; |
| 283 | } |
| 284 | options->exe_path = args[0]; |
| 285 | int cur_idx = 1; |
| 286 | for (; cur_idx < args.size(); ++cur_idx) { |
| 287 | const std::string& cur_arg = args[cur_idx]; |
| 288 | if (cur_arg == "--ppm") { |
| 289 | if (options->output_format != OUTPUT_NONE) { |
| 290 | fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); |
| 291 | return false; |
| 292 | } |
| 293 | options->output_format = OUTPUT_PPM; |
| 294 | } |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 295 | #ifdef _WIN32 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 296 | else if (cur_arg == "--emf") { |
| 297 | if (options->output_format != OUTPUT_NONE) { |
| 298 | fprintf(stderr, "Duplicate or conflicting --emf argument\n"); |
| 299 | return false; |
| 300 | } |
| 301 | options->output_format = OUTPUT_EMF; |
| 302 | } |
| 303 | else if (cur_arg == "--bmp") { |
| 304 | if (options->output_format != OUTPUT_NONE) { |
| 305 | fprintf(stderr, "Duplicate or conflicting --bmp argument\n"); |
| 306 | return false; |
| 307 | } |
| 308 | options->output_format = OUTPUT_BMP; |
| 309 | } |
| 310 | #endif // _WIN32 |
| 311 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 312 | else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) { |
| 313 | if (!options->bin_directory.empty()) { |
| 314 | fprintf(stderr, "Duplicate --bin-dir argument\n"); |
| 315 | return false; |
| 316 | } |
| 317 | options->bin_directory = cur_arg.substr(10); |
| 318 | } |
| 319 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Vitaly Buka | 8f2c3dc | 2014-08-20 10:32:36 -0700 | [diff] [blame] | 320 | else |
| 321 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 322 | } |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 323 | if (cur_idx >= args.size()) { |
| 324 | fprintf(stderr, "No input files.\n"); |
Vitaly Buka | 8f2c3dc | 2014-08-20 10:32:36 -0700 | [diff] [blame] | 325 | return false; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 326 | } |
| 327 | for (int i = cur_idx; i < args.size(); i++) { |
| 328 | files->push_back(args[i]); |
| 329 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | |
| 333 | class TestLoader { |
| 334 | public: |
| 335 | TestLoader(const char* pBuf, size_t len); |
| 336 | |
| 337 | const char* m_pBuf; |
| 338 | size_t m_Len; |
| 339 | }; |
| 340 | |
| 341 | TestLoader::TestLoader(const char* pBuf, size_t len) |
| 342 | : m_pBuf(pBuf), m_Len(len) { |
| 343 | } |
| 344 | |
| 345 | int Get_Block(void* param, unsigned long pos, unsigned char* pBuf, |
| 346 | unsigned long size) { |
| 347 | TestLoader* pLoader = (TestLoader*) param; |
| 348 | if (pos + size < pos || pos + size > pLoader->m_Len) return 0; |
| 349 | memcpy(pBuf, pLoader->m_pBuf + pos, size); |
| 350 | return 1; |
| 351 | } |
| 352 | |
| 353 | bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
| 358 | } |
| 359 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 360 | void RenderPdf(const std::string& name, const char* pBuf, size_t len, |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 361 | OutputFormat format) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 362 | printf("Rendering PDF file %s.\n", name.c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 363 | |
| 364 | IPDF_JSPLATFORM platform_callbacks; |
| 365 | memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
| 366 | platform_callbacks.version = 1; |
| 367 | platform_callbacks.app_alert = Form_Alert; |
| 368 | |
| 369 | FPDF_FORMFILLINFO form_callbacks; |
| 370 | memset(&form_callbacks, '\0', sizeof(form_callbacks)); |
Tom Sepez | ed63138 | 2014-11-18 14:10:25 -0800 | [diff] [blame] | 371 | form_callbacks.version = 2; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 372 | form_callbacks.m_pJsPlatform = &platform_callbacks; |
| 373 | |
| 374 | TestLoader loader(pBuf, len); |
| 375 | |
| 376 | FPDF_FILEACCESS file_access; |
| 377 | memset(&file_access, '\0', sizeof(file_access)); |
John Abd-El-Malek | 7dc5172 | 2014-05-26 12:54:31 -0700 | [diff] [blame] | 378 | file_access.m_FileLen = static_cast<unsigned long>(len); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 379 | file_access.m_GetBlock = Get_Block; |
| 380 | file_access.m_Param = &loader; |
| 381 | |
| 382 | FX_FILEAVAIL file_avail; |
| 383 | memset(&file_avail, '\0', sizeof(file_avail)); |
| 384 | file_avail.version = 1; |
| 385 | file_avail.IsDataAvail = Is_Data_Avail; |
| 386 | |
| 387 | FX_DOWNLOADHINTS hints; |
| 388 | memset(&hints, '\0', sizeof(hints)); |
| 389 | hints.version = 1; |
| 390 | hints.AddSegment = Add_Segment; |
| 391 | |
| 392 | FPDF_DOCUMENT doc; |
| 393 | FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); |
| 394 | |
| 395 | (void) FPDFAvail_IsDocAvail(pdf_avail, &hints); |
| 396 | |
| 397 | if (!FPDFAvail_IsLinearized(pdf_avail)) { |
| 398 | printf("Non-linearized path...\n"); |
| 399 | doc = FPDF_LoadCustomDocument(&file_access, NULL); |
| 400 | } else { |
| 401 | printf("Linearized path...\n"); |
| 402 | doc = FPDFAvail_GetDocument(pdf_avail, NULL); |
| 403 | } |
| 404 | |
| 405 | (void) FPDF_GetDocPermissions(doc); |
| 406 | (void) FPDFAvail_IsFormAvail(pdf_avail, &hints); |
| 407 | |
Bo Xu | 2b7a49d | 2014-11-14 17:40:50 -0800 | [diff] [blame] | 408 | FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); |
Tom Sepez | 5645138 | 2014-12-05 13:30:51 -0800 | [diff] [blame] | 409 | if (!FPDF_LoadXFA(doc)) { |
| 410 | printf("LoadXFA unsuccessful, continuing anyway.\n"); |
| 411 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 412 | FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); |
| 413 | FPDF_SetFormFieldHighlightAlpha(form, 100); |
| 414 | |
| 415 | int first_page = FPDFAvail_GetFirstPageNum(doc); |
| 416 | (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints); |
| 417 | |
| 418 | int page_count = FPDF_GetPageCount(doc); |
| 419 | for (int i = 0; i < page_count; ++i) { |
| 420 | (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints); |
| 421 | } |
| 422 | |
| 423 | FORM_DoDocumentJSAction(form); |
| 424 | FORM_DoDocumentOpenAction(form); |
| 425 | |
Jun Fang | aeacba4 | 2014-08-22 17:04:29 -0700 | [diff] [blame] | 426 | size_t rendered_pages = 0; |
| 427 | size_t bad_pages = 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 428 | for (int i = 0; i < page_count; ++i) { |
| 429 | FPDF_PAGE page = FPDF_LoadPage(doc, i); |
Jun Fang | aeacba4 | 2014-08-22 17:04:29 -0700 | [diff] [blame] | 430 | if (!page) { |
| 431 | bad_pages ++; |
| 432 | continue; |
| 433 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 434 | FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); |
| 435 | FORM_OnAfterLoadPage(page, form); |
| 436 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
| 437 | |
| 438 | int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 439 | int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 440 | FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); |
Lei Zhang | 532a6a7 | 2014-07-09 11:47:15 -0700 | [diff] [blame] | 441 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 442 | |
| 443 | FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
Jun Fang | aeacba4 | 2014-08-22 17:04:29 -0700 | [diff] [blame] | 444 | rendered_pages ++; |
| 445 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 446 | FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 447 | int stride = FPDFBitmap_GetStride(bitmap); |
| 448 | const char* buffer = |
| 449 | reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); |
| 450 | |
| 451 | switch (format) { |
| 452 | #ifdef _WIN32 |
| 453 | case OUTPUT_BMP: |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 454 | WriteBmp(name.c_str(), i, buffer, stride, width, height); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 455 | break; |
| 456 | |
| 457 | case OUTPUT_EMF: |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 458 | WriteEmf(page.c_str(), name, i); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 459 | break; |
| 460 | #endif |
| 461 | case OUTPUT_PPM: |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 462 | WritePpm(name.c_str(), i, buffer, stride, width, height); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 463 | break; |
| 464 | default: |
| 465 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | FPDFBitmap_Destroy(bitmap); |
| 469 | |
| 470 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 471 | FORM_OnBeforeClosePage(page, form); |
| 472 | FPDFText_ClosePage(text_page); |
| 473 | FPDF_ClosePage(page); |
| 474 | } |
| 475 | |
| 476 | FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 477 | FPDF_CloseDocument(doc); |
Bo Xu | 2b7a49d | 2014-11-14 17:40:50 -0800 | [diff] [blame] | 478 | FPDFDOC_ExitFormFillEnvironment(form); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 479 | FPDFAvail_Destroy(pdf_avail); |
| 480 | |
Bruce Dawson | ddc2062 | 2014-11-18 13:42:28 -0800 | [diff] [blame] | 481 | printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages); |
| 482 | printf("Skipped %" PRIuS " bad pages.\n", bad_pages); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | int main(int argc, const char* argv[]) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 486 | std::vector<std::string> args(argv, argv + argc); |
| 487 | Options options; |
| 488 | std::list<std::string> files; |
| 489 | if (!ParseCommandLine(args, &options, &files)) { |
Vitaly Buka | 8f2c3dc | 2014-08-20 10:32:36 -0700 | [diff] [blame] | 490 | printf("Usage: pdfium_test [OPTION] [FILE]...\n"); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 491 | printf("--bin-dir=<path> - override path to v8 external data\n"); |
| 492 | printf("--ppm - write page images <pdf-name>.<page-number>.ppm\n"); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 493 | #ifdef _WIN32 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 494 | printf("--bmp - write page images <pdf-name>.<page-number>.bmp\n"); |
| 495 | printf("--emf - write page meta files <pdf-name>.<page-number>.emf\n"); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 496 | #endif |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 497 | return 1; |
| 498 | } |
| 499 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 500 | v8::V8::InitializeICU(); |
| 501 | |
| 502 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 503 | v8::StartupData natives; |
| 504 | v8::StartupData snapshot; |
| 505 | if (!GetExternalData(options, "natives_blob.bin", &natives) || |
| 506 | !GetExternalData(options, "snapshot_blob.bin", &snapshot)) { |
| 507 | return 1; |
| 508 | } |
| 509 | v8::V8::SetNativesDataBlob(&natives); |
| 510 | v8::V8::SetSnapshotDataBlob(&snapshot); |
| 511 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 512 | |
John Abd-El-Malek | 207299b | 2014-12-15 12:13:45 -0800 | [diff] [blame] | 513 | FPDF_InitLibrary(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 514 | |
| 515 | UNSUPPORT_INFO unsuppored_info; |
| 516 | memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
| 517 | unsuppored_info.version = 1; |
| 518 | unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; |
| 519 | |
| 520 | FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 521 | |
| 522 | while (!files.empty()) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 523 | std::string filename = files.front(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 524 | files.pop_front(); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 525 | size_t file_length = 0; |
| 526 | char* file_contents = GetFileContents(filename.c_str(), &file_length); |
| 527 | if (!file_contents) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 528 | continue; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame^] | 529 | RenderPdf(filename, file_contents, file_length, options.output_format); |
| 530 | free(file_contents); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | FPDF_DestroyLibrary(); |
| 534 | |
| 535 | return 0; |
| 536 | } |