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

Side by Side Diff: samples/pdfium_test.cc

Issue 1412083010: Merge to XFA: Support linearized loading (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: Created 5 years, 1 month 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 | « public/fpdf_dataavail.h ('k') | testing/embedder_test.h » ('j') | 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 #include <wchar.h> 9 #include <wchar.h>
10 10
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 return true; 325 return true;
326 } 326 }
327 327
328 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { 328 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
329 return true; 329 return true;
330 } 330 }
331 331
332 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { 332 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
333 } 333 }
334 334
335 FPDF_BOOL RenderPage(const std::string& name,
336 const FPDF_DOCUMENT& doc,
337 const FPDF_FORMHANDLE& form,
338 const int page_index,
339 const Options& options) {
340 FPDF_PAGE page = FPDF_LoadPage(doc, page_index);
341 if (!page) {
342 return FALSE;
Lei Zhang 2015/11/10 10:41:02 Looks like you need to merge 71fca3fffc40f63b28c00
343 }
344 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
345 FORM_OnAfterLoadPage(page, form);
346 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
347
348 double scale = 1.0;
349 if (!options.scale_factor_as_string.empty()) {
350 std::stringstream(options.scale_factor_as_string) >> scale;
351 }
352 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
353 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
354
355 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
356 if (!bitmap) {
357 fprintf(stderr, "Page was too large to be rendered.\n");
358 return FALSE;
359 }
360
361 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
362 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
363
364 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
365 int stride = FPDFBitmap_GetStride(bitmap);
366 const char* buffer =
367 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
368
369 switch (options.output_format) {
370 #ifdef _WIN32
371 case OUTPUT_BMP:
372 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
373 break;
374
375 case OUTPUT_EMF:
376 WriteEmf(page, name.c_str(), page_index);
377 break;
378 #endif
379 case OUTPUT_PNG:
380 WritePng(name.c_str(), page_index, buffer, stride, width, height);
381 break;
382
383 case OUTPUT_PPM:
384 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
385 break;
386
387 default:
388 break;
389 }
390
391 FPDFBitmap_Destroy(bitmap);
392 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
393 FORM_OnBeforeClosePage(page, form);
394 FPDFText_ClosePage(text_page);
395 FPDF_ClosePage(page);
396 return TRUE;
397 }
398
335 void RenderPdf(const std::string& name, const char* pBuf, size_t len, 399 void RenderPdf(const std::string& name, const char* pBuf, size_t len,
336 const Options& options) { 400 const Options& options) {
337 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str()); 401 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
338 402
339 IPDF_JSPLATFORM platform_callbacks; 403 IPDF_JSPLATFORM platform_callbacks;
340 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); 404 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
341 platform_callbacks.version = 3; 405 platform_callbacks.version = 3;
342 platform_callbacks.app_alert = ExampleAppAlert; 406 platform_callbacks.app_alert = ExampleAppAlert;
343 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; 407 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
344 408
(...skipping 13 matching lines...) Expand all
358 memset(&file_avail, '\0', sizeof(file_avail)); 422 memset(&file_avail, '\0', sizeof(file_avail));
359 file_avail.version = 1; 423 file_avail.version = 1;
360 file_avail.IsDataAvail = Is_Data_Avail; 424 file_avail.IsDataAvail = Is_Data_Avail;
361 425
362 FX_DOWNLOADHINTS hints; 426 FX_DOWNLOADHINTS hints;
363 memset(&hints, '\0', sizeof(hints)); 427 memset(&hints, '\0', sizeof(hints));
364 hints.version = 1; 428 hints.version = 1;
365 hints.AddSegment = Add_Segment; 429 hints.AddSegment = Add_Segment;
366 430
367 FPDF_DOCUMENT doc; 431 FPDF_DOCUMENT doc;
432 int nRet = PDF_DATA_NOTAVAIL;
433 FPDF_BOOL bIsLinearized = FALSE;
368 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); 434 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
369 435 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
370 (void)FPDFAvail_IsDocAvail(pdf_avail, &hints); 436 fprintf(stderr, "Linearized path...\n");
371
372 if (FPDFAvail_IsLinearized(pdf_avail))
373 doc = FPDFAvail_GetDocument(pdf_avail, nullptr); 437 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
374 else 438 if (doc) {
439 while (nRet == PDF_DATA_NOTAVAIL) {
440 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
441 }
442 if (nRet == PDF_DATA_ERROR) {
443 fprintf(stderr, "Unknown error in checking if doc was available.\n");
444 return;
445 }
446 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints);
447 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
448 fprintf(stderr,
449 "Error %d was returned in checking if form was available.\n",
450 nRet);
451 return;
452 }
453 bIsLinearized = TRUE;
454 }
455 } else {
456 fprintf(stderr, "Non-linearized path...\n");
375 doc = FPDF_LoadCustomDocument(&file_access, nullptr); 457 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
458 }
376 459
377 if (!doc) { 460 if (!doc) {
378 unsigned long err = FPDF_GetLastError(); 461 unsigned long err = FPDF_GetLastError();
379 fprintf(stderr, "Load pdf docs unsuccessful: "); 462 fprintf(stderr, "Load pdf docs unsuccessful: ");
380 switch (err) { 463 switch (err) {
381 case FPDF_ERR_SUCCESS: 464 case FPDF_ERR_SUCCESS:
382 fprintf(stderr, "Success"); 465 fprintf(stderr, "Success");
383 break; 466 break;
384 case FPDF_ERR_UNKNOWN: 467 case FPDF_ERR_UNKNOWN:
385 fprintf(stderr, "Unknown error"); 468 fprintf(stderr, "Unknown error");
(...skipping 16 matching lines...) Expand all
402 default: 485 default:
403 fprintf(stderr, "Unknown error %ld", err); 486 fprintf(stderr, "Unknown error %ld", err);
404 } 487 }
405 fprintf(stderr, ".\n"); 488 fprintf(stderr, ".\n");
406 489
407 FPDFAvail_Destroy(pdf_avail); 490 FPDFAvail_Destroy(pdf_avail);
408 return; 491 return;
409 } 492 }
410 493
411 (void)FPDF_GetDocPermissions(doc); 494 (void)FPDF_GetDocPermissions(doc);
412 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints);
413 495
414 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); 496 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
415 int docType = DOCTYPE_PDF; 497 int docType = DOCTYPE_PDF;
416 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF && 498 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
417 !FPDF_LoadXFA(doc)) { 499 !FPDF_LoadXFA(doc)) {
418 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); 500 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
419 } 501 }
420 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); 502 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
421 FPDF_SetFormFieldHighlightAlpha(form, 100); 503 FPDF_SetFormFieldHighlightAlpha(form, 100);
422 504
423 int first_page = FPDFAvail_GetFirstPageNum(doc);
424 (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
425
426 int page_count = FPDF_GetPageCount(doc);
427 for (int i = 0; i < page_count; ++i) {
428 (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
429 }
430
431 FORM_DoDocumentJSAction(form); 505 FORM_DoDocumentJSAction(form);
432 FORM_DoDocumentOpenAction(form); 506 FORM_DoDocumentOpenAction(form);
433 507
508 int page_count = FPDF_GetPageCount(doc);
434 int rendered_pages = 0; 509 int rendered_pages = 0;
435 int bad_pages = 0; 510 int bad_pages = 0;
436 for (int i = 0; i < page_count; ++i) { 511 for (int i = 0; i < page_count; ++i) {
437 FPDF_PAGE page = FPDF_LoadPage(doc, i); 512 if (bIsLinearized) {
438 if (!page) { 513 nRet = PDF_DATA_NOTAVAIL;
514 while (nRet == PDF_DATA_NOTAVAIL) {
515 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
516 }
517 if (nRet == PDF_DATA_ERROR) {
518 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
519 i);
520 return;
521 }
522 }
523 if (RenderPage(name, doc, form, i, options)) {
524 ++rendered_pages;
525 } else {
439 ++bad_pages; 526 ++bad_pages;
440 continue;
441 } 527 }
442 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
443 FORM_OnAfterLoadPage(page, form);
444 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
445
446 double scale = 1.0;
447 if (!options.scale_factor_as_string.empty()) {
448 std::stringstream(options.scale_factor_as_string) >> scale;
449 }
450 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
451 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
452
453 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
454 if (!bitmap) {
455 fprintf(stderr, "Page was too large to be rendered.\n");
456 bad_pages++;
457 continue;
458 }
459
460 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
461 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
462 ++rendered_pages;
463
464 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
465 int stride = FPDFBitmap_GetStride(bitmap);
466 const char* buffer =
467 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
468
469 switch (options.output_format) {
470 #ifdef _WIN32
471 case OUTPUT_BMP:
472 WriteBmp(name.c_str(), i, buffer, stride, width, height);
473 break;
474
475 case OUTPUT_EMF:
476 WriteEmf(page, name.c_str(), i);
477 break;
478 #endif
479 case OUTPUT_PPM:
480 WritePpm(name.c_str(), i, buffer, stride, width, height);
481 break;
482
483 case OUTPUT_PNG:
484 WritePng(name.c_str(), i, buffer, stride, width, height);
485 break;
486
487 default:
488 break;
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 } 528 }
498 529
499 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); 530 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
500 531
501 // Note: The shut down order here is the reverse of the non-XFA branch order. 532 // 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 533 // Need to work out if this is required, and if it is, the lifetimes of
503 // objects owned by |doc| that |form| reference. 534 // objects owned by |doc| that |form| reference.
504 FPDF_CloseDocument(doc); 535 FPDF_CloseDocument(doc);
505 FPDFDOC_ExitFormFillEnvironment(form); 536 FPDFDOC_ExitFormFillEnvironment(form);
506 537
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 } 607 }
577 608
578 FPDF_DestroyLibrary(); 609 FPDF_DestroyLibrary();
579 #ifdef PDF_ENABLE_V8 610 #ifdef PDF_ENABLE_V8
580 v8::V8::ShutdownPlatform(); 611 v8::V8::ShutdownPlatform();
581 delete platform; 612 delete platform;
582 #endif // PDF_ENABLE_V8 613 #endif // PDF_ENABLE_V8
583 614
584 return 0; 615 return 0;
585 } 616 }
OLDNEW
« no previous file with comments | « public/fpdf_dataavail.h ('k') | testing/embedder_test.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698