Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(913)

Side by Side Diff: samples/pdfium_test.cc

Issue 808293002: Merge Initialize V8 external data back to master. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <limits.h> 5 #include <limits.h>
6 #include <stdio.h> 6 #include <stdio.h>
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <list> 10 #include <list>
11 #include <string> 11 #include <string>
12 #include <utility> 12 #include <utility>
13 #include <vector>
13 14
14 #include "../fpdfsdk/include/fpdf_dataavail.h" 15 #include "../fpdfsdk/include/fpdf_dataavail.h"
15 #include "../fpdfsdk/include/fpdf_ext.h" 16 #include "../fpdfsdk/include/fpdf_ext.h"
16 #include "../fpdfsdk/include/fpdfformfill.h" 17 #include "../fpdfsdk/include/fpdfformfill.h"
17 #include "../fpdfsdk/include/fpdftext.h" 18 #include "../fpdfsdk/include/fpdftext.h"
18 #include "../fpdfsdk/include/fpdfview.h" 19 #include "../fpdfsdk/include/fpdfview.h"
19 #include "../core/include/fxcrt/fx_system.h" 20 #include "../core/include/fxcrt/fx_system.h"
20 #include "v8/include/v8.h" 21 #include "v8/include/v8.h"
21 22
22 #ifdef _WIN32 23 #ifdef _WIN32
23 #define snprintf _snprintf 24 #define snprintf _snprintf
25 #define PATH_SEPARATOR '\\'
26 #else
27 #define PATH_SEPARATOR '/'
24 #endif 28 #endif
25 29
26 enum OutputFormat { 30 enum OutputFormat {
27 OUTPUT_NONE, 31 OUTPUT_NONE,
28 OUTPUT_PPM, 32 OUTPUT_PPM,
29 #ifdef _WIN32 33 #ifdef _WIN32
30 OUTPUT_BMP, 34 OUTPUT_BMP,
31 OUTPUT_EMF, 35 OUTPUT_EMF,
32 #endif 36 #endif
33 }; 37 };
34 38
39 struct Options {
40 Options() : output_format(OUTPUT_NONE) { }
41
42 OutputFormat output_format;
43 std::string exe_path;
44 std::string bin_directory;
45 };
46
47 // Reads the entire contents of a file into a newly malloc'd buffer.
48 static char* GetFileContents(const char* filename, size_t* retlen) {
49 FILE* file = fopen(filename, "rb");
50 if (!file) {
51 fprintf(stderr, "Failed to open: %s\n", filename);
52 return NULL;
53 }
54 (void) fseek(file, 0, SEEK_END);
55 size_t file_length = ftell(file);
56 if (!file_length) {
57 return NULL;
58 }
59 (void) fseek(file, 0, SEEK_SET);
60 char* buffer = (char*) malloc(file_length);
61 if (!buffer) {
62 return NULL;
63 }
64 size_t bytes_read = fread(buffer, 1, file_length, file);
65 (void) fclose(file);
66 if (bytes_read != file_length) {
67 fprintf(stderr, "Failed to read: %s\n", filename);
68 free(buffer);
69 return NULL;
70 }
71 *retlen = bytes_read;
72 return buffer;
73 }
74
75 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
76 // Returns the full path for an external V8 data file based on either
77 // the currect exectuable path or an explicit override.
78 static std::string GetFullPathForSnapshotFile(const Options& options,
79 const std::string& filename) {
80 std::string result;
81 if (!options.bin_directory.empty()) {
82 result = options.bin_directory;
83 if (*options.bin_directory.rbegin() != PATH_SEPARATOR) {
84 result += PATH_SEPARATOR;
85 }
86 } else if (!options.exe_path.empty()) {
87 size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR);
88 if (last_separator != std::string::npos) {
89 result = options.exe_path.substr(0, last_separator + 1);
90 }
91 }
92 result += filename;
93 return result;
94 }
95
96 // Reads an extenal V8 data file from the |options|-indicated location,
97 // returing true on success and false on error.
98 static bool GetExternalData(const Options& options,
99 const std::string& bin_filename,
100 v8::StartupData* result_data) {
101 std::string full_path = GetFullPathForSnapshotFile(options, bin_filename);
102 size_t data_length = 0;
103 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
104 if (!data_buffer) {
105 return false;
106 }
107 result_data->data = const_cast<const char*>(data_buffer);
108 result_data->raw_size = data_length;
109 return true;
110 }
111 #endif // V8_USE_EXTERNAL_STARTUP_DATA
112
35 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, 113 static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
36 int stride, int width, int height) { 114 int stride, int width, int height) {
37 const char* buffer = reinterpret_cast<const char*>(buffer_void); 115 const char* buffer = reinterpret_cast<const char*>(buffer_void);
38 116
39 if (stride < 0 || width < 0 || height < 0) 117 if (stride < 0 || width < 0 || height < 0)
40 return; 118 return;
41 if (height > 0 && width > INT_MAX / height) 119 if (height > 0 && width > INT_MAX / height)
42 return; 120 return;
43 int out_len = width * height; 121 int out_len = width * height;
44 if (out_len > INT_MAX / 3) 122 if (out_len > INT_MAX / 3)
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: 255 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
178 feature = "Screen"; 256 feature = "Screen";
179 break; 257 break;
180 case FPDF_UNSP_ANNOT_SIG: 258 case FPDF_UNSP_ANNOT_SIG:
181 feature = "Digital_Signature"; 259 feature = "Digital_Signature";
182 break; 260 break;
183 } 261 }
184 printf("Unsupported feature: %s.\n", feature.c_str()); 262 printf("Unsupported feature: %s.\n", feature.c_str());
185 } 263 }
186 264
187 bool ParseCommandLine(int argc, const char* argv[], OutputFormat* output_format, 265 bool ParseCommandLine(const std::vector<std::string>& args,
188 std::list<const char*>* files) { 266 Options* options, std::list<std::string>* files) {
189 *output_format = OUTPUT_NONE; 267 if (args.empty()) {
190 files->clear(); 268 return false;
191 269 }
192 int cur_arg = 1; 270 options->exe_path = args[0];
193 for (; cur_arg < argc; ++cur_arg) { 271 int cur_idx = 1;
194 if (strcmp(argv[cur_arg], "--ppm") == 0) 272 for (; cur_idx < args.size(); ++cur_idx) {
195 *output_format = OUTPUT_PPM; 273 const std::string& cur_arg = args[cur_idx];
274 if (cur_arg == "--ppm") {
275 if (options->output_format != OUTPUT_NONE) {
276 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
277 return false;
278 }
279 options->output_format = OUTPUT_PPM;
280 }
196 #ifdef _WIN32 281 #ifdef _WIN32
197 else if (strcmp(argv[cur_arg], "--emf") == 0) 282 else if (cur_arg == "--emf") {
198 *output_format = OUTPUT_EMF; 283 if (options->output_format != OUTPUT_NONE) {
199 else if (strcmp(argv[cur_arg], "--bmp") == 0) 284 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
200 *output_format = OUTPUT_BMP; 285 return false;
201 #endif 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
297 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
298 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) {
299 if (!options->bin_directory.empty()) {
300 fprintf(stderr, "Duplicate --bin-dir argument\n");
301 return false;
302 }
303 options->bin_directory = cur_arg.substr(10);
304 }
305 #endif // V8_USE_EXTERNAL_STARTUP_DATA
202 else 306 else
203 break; 307 break;
204 } 308 }
205 309 if (cur_idx >= args.size()) {
206 if (cur_arg > 2) // Multiple options. 310 fprintf(stderr, "No input files.\n");
207 return false; 311 return false;
208 312 }
209 if (cur_arg >= argc) // No input files. 313 for (int i = cur_idx; i < args.size(); i++) {
210 return false; 314 files->push_back(args[i]);
211 315 }
212 for (int i = cur_arg; i < argc; i++)
213 files->push_back(argv[i]);
214
215 return true; 316 return true;
216 } 317 }
217 318
218 class TestLoader { 319 class TestLoader {
219 public: 320 public:
220 TestLoader(const char* pBuf, size_t len); 321 TestLoader(const char* pBuf, size_t len);
221 322
222 const char* m_pBuf; 323 const char* m_pBuf;
223 size_t m_Len; 324 size_t m_Len;
224 }; 325 };
(...skipping 10 matching lines...) Expand all
235 return 1; 336 return 1;
236 } 337 }
237 338
238 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { 339 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
239 return true; 340 return true;
240 } 341 }
241 342
242 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { 343 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
243 } 344 }
244 345
245 void RenderPdf(const char* name, const char* pBuf, size_t len, 346 void RenderPdf(const std::string& name, const char* pBuf, size_t len,
246 OutputFormat format) { 347 OutputFormat format) {
247 printf("Rendering PDF file %s.\n", name); 348 printf("Rendering PDF file %s.\n", name.c_str());
248 349
249 IPDF_JSPLATFORM platform_callbacks; 350 IPDF_JSPLATFORM platform_callbacks;
250 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); 351 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
251 platform_callbacks.version = 1; 352 platform_callbacks.version = 1;
252 platform_callbacks.app_alert = Form_Alert; 353 platform_callbacks.app_alert = Form_Alert;
253 354
254 FPDF_FORMFILLINFO form_callbacks; 355 FPDF_FORMFILLINFO form_callbacks;
255 memset(&form_callbacks, '\0', sizeof(form_callbacks)); 356 memset(&form_callbacks, '\0', sizeof(form_callbacks));
256 form_callbacks.version = 1; 357 form_callbacks.version = 1;
257 form_callbacks.m_pJsPlatform = &platform_callbacks; 358 form_callbacks.m_pJsPlatform = &platform_callbacks;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 rendered_pages ++; 427 rendered_pages ++;
327 428
328 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); 429 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
329 int stride = FPDFBitmap_GetStride(bitmap); 430 int stride = FPDFBitmap_GetStride(bitmap);
330 const char* buffer = 431 const char* buffer =
331 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); 432 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
332 433
333 switch (format) { 434 switch (format) {
334 #ifdef _WIN32 435 #ifdef _WIN32
335 case OUTPUT_BMP: 436 case OUTPUT_BMP:
336 WriteBmp(name, i, buffer, stride, width, height); 437 WriteBmp(name.c_str(), i, buffer, stride, width, height);
337 break; 438 break;
338 439
339 case OUTPUT_EMF: 440 case OUTPUT_EMF:
340 WriteEmf(page, name, i); 441 WriteEmf(page.c_str(), name, i);
341 break; 442 break;
342 #endif 443 #endif
343 case OUTPUT_PPM: 444 case OUTPUT_PPM:
344 WritePpm(name, i, buffer, stride, width, height); 445 WritePpm(name.c_str(), i, buffer, stride, width, height);
345 break; 446 break;
346 default: 447 default:
347 break; 448 break;
348 } 449 }
349 450
350 FPDFBitmap_Destroy(bitmap); 451 FPDFBitmap_Destroy(bitmap);
351 452
352 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); 453 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
353 FORM_OnBeforeClosePage(page, form); 454 FORM_OnBeforeClosePage(page, form);
354 FPDFText_ClosePage(text_page); 455 FPDFText_ClosePage(text_page);
355 FPDF_ClosePage(page); 456 FPDF_ClosePage(page);
356 } 457 }
357 458
358 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); 459 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
359 FPDFDOC_ExitFormFillEnvironment(form); 460 FPDFDOC_ExitFormFillEnvironment(form);
360 FPDF_CloseDocument(doc); 461 FPDF_CloseDocument(doc);
361 FPDFAvail_Destroy(pdf_avail); 462 FPDFAvail_Destroy(pdf_avail);
362 463
363 printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages); 464 printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages);
364 printf("Skipped %" PRIuS " bad pages.\n", bad_pages); 465 printf("Skipped %" PRIuS " bad pages.\n", bad_pages);
365 } 466 }
366 467
367 int main(int argc, const char* argv[]) { 468 int main(int argc, const char* argv[]) {
368 v8::V8::InitializeICU(); 469 std::vector<std::string> args(argv, argv + argc);
369 OutputFormat format = OUTPUT_NONE; 470 Options options;
370 std::list<const char*> files; 471 std::list<std::string> files;
371 if (!ParseCommandLine(argc, argv, &format, &files)) { 472 if (!ParseCommandLine(args, &options, &files)) {
372 printf("Usage: pdfium_test [OPTION] [FILE]...\n"); 473 printf("Usage: pdfium_test [OPTION] [FILE]...\n");
373 printf("--ppm write page images <pdf-name>.<page-number>.ppm\n"); 474 printf("--bin-dir=<path> - override path to v8 external data\n");
475 printf("--ppm - write page images <pdf-name>.<page-number>.ppm\n");
374 #ifdef _WIN32 476 #ifdef _WIN32
375 printf("--bmp write page images <pdf-name>.<page-number>.bmp\n"); 477 printf("--bmp - write page images <pdf-name>.<page-number>.bmp\n");
376 printf("--emf write page meta files <pdf-name>.<page-number>.emf\n"); 478 printf("--emf - write page meta files <pdf-name>.<page-number>.emf\n");
377 #endif 479 #endif
378 return 1; 480 return 1;
379 } 481 }
380 482
483 v8::V8::InitializeICU();
484
485 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
486 v8::StartupData natives;
487 v8::StartupData snapshot;
488 if (!GetExternalData(options, "natives_blob.bin", &natives) ||
489 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) {
490 return 1;
491 }
492 v8::V8::SetNativesDataBlob(&natives);
493 v8::V8::SetSnapshotDataBlob(&snapshot);
494 #endif // V8_USE_EXTERNAL_STARTUP_DATA
495
381 FPDF_InitLibrary(); 496 FPDF_InitLibrary();
382 497
383 UNSUPPORT_INFO unsuppored_info; 498 UNSUPPORT_INFO unsuppored_info;
384 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); 499 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
385 unsuppored_info.version = 1; 500 unsuppored_info.version = 1;
386 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; 501 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;
387 502
388 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); 503 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
389 504
390 while (!files.empty()) { 505 while (!files.empty()) {
391 const char* filename = files.front(); 506 std::string filename = files.front();
392 files.pop_front(); 507 files.pop_front();
393 FILE* file = fopen(filename, "rb"); 508 size_t file_length = 0;
394 if (!file) { 509 char* file_contents = GetFileContents(filename.c_str(), &file_length);
395 fprintf(stderr, "Failed to open: %s\n", filename); 510 if (!file_contents)
396 continue; 511 continue;
397 } 512 RenderPdf(filename, file_contents, file_length, options.output_format);
398 (void) fseek(file, 0, SEEK_END); 513 free(file_contents);
399 size_t len = ftell(file);
400 (void) fseek(file, 0, SEEK_SET);
401 char* pBuf = (char*) malloc(len);
402 size_t ret = fread(pBuf, 1, len, file);
403 (void) fclose(file);
404 if (ret != len) {
405 fprintf(stderr, "Failed to read: %s\n", filename);
406 } else {
407 RenderPdf(filename, pBuf, len, format);
408 }
409 free(pBuf);
410 } 514 }
411 515
412 FPDF_DestroyLibrary(); 516 FPDF_DestroyLibrary();
413 517
414 return 0; 518 return 0;
415 } 519 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698