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

Side by Side Diff: samples/pdfium_test.cc

Issue 1353093003: Support linearized loading (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Add an argument 'must_linearize' in OpenDocument Created 5 years, 2 months 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
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>
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 return 1; 430 return 1;
431 } 431 }
432 432
433 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { 433 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
434 return true; 434 return true;
435 } 435 }
436 436
437 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { 437 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
438 } 438 }
439 439
440 FPDF_BOOL RenderPage(const std::string& name,
441 const FPDF_DOCUMENT& doc,
442 const FPDF_FORMHANDLE& form,
443 const int page_index,
444 const Options& options) {
445 FPDF_PAGE page = FPDF_LoadPage(doc, page_index);
446 if (!page) {
447 return FALSE;
448 }
449 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
450 FORM_OnAfterLoadPage(page, form);
451 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
452
453 double scale = 1.0;
454 if (!options.scale_factor_as_string.empty()) {
455 std::stringstream(options.scale_factor_as_string) >> scale;
456 }
457 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
458 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
459
460 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
461 if (!bitmap) {
462 fprintf(stderr, "Page was too large to be rendered.\n");
463 return FALSE;
464 }
465
466 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
467 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
468
469 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
470 int stride = FPDFBitmap_GetStride(bitmap);
471 const char* buffer =
472 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
473
474 switch (options.output_format) {
475 #ifdef _WIN32
476 case OUTPUT_BMP:
477 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
478 break;
479
480 case OUTPUT_EMF:
481 WriteEmf(page, name.c_str(), page_index);
482 break;
483 #endif
484 case OUTPUT_PNG:
485 WritePng(name.c_str(), page_index, buffer, stride, width, height);
486 break;
487
488 case OUTPUT_PPM:
489 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
490 break;
491
492 default:
493 break;
494 }
495
496 FPDFBitmap_Destroy(bitmap);
497 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
498 FORM_OnBeforeClosePage(page, form);
499 FPDFText_ClosePage(text_page);
500 FPDF_ClosePage(page);
501 return TRUE;
502 }
503
440 void RenderPdf(const std::string& name, const char* pBuf, size_t len, 504 void RenderPdf(const std::string& name, const char* pBuf, size_t len,
441 const Options& options) { 505 const Options& options) {
442 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str()); 506 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
443 507
444 IPDF_JSPLATFORM platform_callbacks; 508 IPDF_JSPLATFORM platform_callbacks;
445 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); 509 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
446 platform_callbacks.version = 3; 510 platform_callbacks.version = 3;
447 platform_callbacks.app_alert = ExampleAppAlert; 511 platform_callbacks.app_alert = ExampleAppAlert;
448 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; 512 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
449 513
(...skipping 14 matching lines...) Expand all
464 memset(&file_avail, '\0', sizeof(file_avail)); 528 memset(&file_avail, '\0', sizeof(file_avail));
465 file_avail.version = 1; 529 file_avail.version = 1;
466 file_avail.IsDataAvail = Is_Data_Avail; 530 file_avail.IsDataAvail = Is_Data_Avail;
467 531
468 FX_DOWNLOADHINTS hints; 532 FX_DOWNLOADHINTS hints;
469 memset(&hints, '\0', sizeof(hints)); 533 memset(&hints, '\0', sizeof(hints));
470 hints.version = 1; 534 hints.version = 1;
471 hints.AddSegment = Add_Segment; 535 hints.AddSegment = Add_Segment;
472 536
473 FPDF_DOCUMENT doc; 537 FPDF_DOCUMENT doc;
538 int32_t nRet = PDF_DATA_NOTAVAIL;
539 FPDF_BOOL bIsLinearized = FALSE;
474 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); 540 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
475 541 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
476 (void)FPDFAvail_IsDocAvail(pdf_avail, &hints);
477
478 if (FPDFAvail_IsLinearized(pdf_avail)) {
479 fprintf(stderr, "Linearized path...\n"); 542 fprintf(stderr, "Linearized path...\n");
480 doc = FPDFAvail_GetDocument(pdf_avail, nullptr); 543 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
544 if (!doc) {
545 fprintf(stderr, "Load linearized pdf docs unsuccessful.\n");
546 return;
547 }
548 while (nRet == PDF_DATA_NOTAVAIL) {
549 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
Tom Sepez 2015/10/21 17:53:08 For example, if we had a separate function to chec
jun_fang 2015/10/22 00:50:06 Using the example as above, we shall use FPDFAvai
550 }
551 if (nRet == PDF_DATA_ERROR) {
552 return;
553 }
554 if (FPDFAvail_IsFormAvail(pdf_avail, &hints) == PDF_FORM_NOTAVAIL) {
555 return;
556 }
557 bIsLinearized = TRUE;
481 } else { 558 } else {
482 fprintf(stderr, "Non-linearized path...\n"); 559 fprintf(stderr, "Non-linearized path...\n");
483 doc = FPDF_LoadCustomDocument(&file_access, nullptr); 560 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
484 } 561 if (!doc) {
485 562 fprintf(stderr, "Load non-linearized pdf docs unsuccessful.\n");
486 if (!doc) { 563 return;
487 fprintf(stderr, "Load pdf docs unsuccessful.\n"); 564 }
488 return;
489 } 565 }
490 566
491 (void)FPDF_GetDocPermissions(doc); 567 (void)FPDF_GetDocPermissions(doc);
492 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints);
493 568
494 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); 569 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
495 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); 570 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
496 FPDF_SetFormFieldHighlightAlpha(form, 100); 571 FPDF_SetFormFieldHighlightAlpha(form, 100);
497 572
498 int first_page = FPDFAvail_GetFirstPageNum(doc);
499 (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
500
501 int page_count = FPDF_GetPageCount(doc);
502 for (int i = 0; i < page_count; ++i) {
503 (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
504 }
505
506 FORM_DoDocumentJSAction(form); 573 FORM_DoDocumentJSAction(form);
507 FORM_DoDocumentOpenAction(form); 574 FORM_DoDocumentOpenAction(form);
508 575
576 int page_count = FPDF_GetPageCount(doc);
509 int rendered_pages = 0; 577 int rendered_pages = 0;
510 int bad_pages = 0; 578 int bad_pages = 0;
511 for (int i = 0; i < page_count; ++i) { 579 for (int i = 0; i < page_count; ++i) {
512 FPDF_PAGE page = FPDF_LoadPage(doc, i); 580 if (bIsLinearized) {
513 if (!page) { 581 nRet = PDF_DATA_NOTAVAIL;
582 while (nRet == PDF_DATA_NOTAVAIL) {
583 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
584 }
585 if (nRet == PDF_DATA_ERROR) {
586 return;
587 }
588 }
589 if (RenderPage(name, doc, form, i, options)) {
590 ++rendered_pages;
591 } else {
514 ++bad_pages; 592 ++bad_pages;
515 continue;
516 } 593 }
517 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
518 FORM_OnAfterLoadPage(page, form);
519 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
520
521 double scale = 1.0;
522 if (!options.scale_factor_as_string.empty()) {
523 std::stringstream(options.scale_factor_as_string) >> scale;
524 }
525 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
526 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
527
528 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
529 if (!bitmap) {
530 fprintf(stderr, "Page was too large to be rendered.\n");
531 bad_pages++;
532 continue;
533 }
534
535 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
536 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
537 ++rendered_pages;
538
539 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
540 int stride = FPDFBitmap_GetStride(bitmap);
541 const char* buffer =
542 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
543
544 switch (options.output_format) {
545 #ifdef _WIN32
546 case OUTPUT_BMP:
547 WriteBmp(name.c_str(), i, buffer, stride, width, height);
548 break;
549
550 case OUTPUT_EMF:
551 WriteEmf(page, name.c_str(), i);
552 break;
553 #endif
554 case OUTPUT_PNG:
555 WritePng(name.c_str(), i, buffer, stride, width, height);
556 break;
557
558 case OUTPUT_PPM:
559 WritePpm(name.c_str(), i, buffer, stride, width, height);
560 break;
561
562 default:
563 break;
564 }
565
566 FPDFBitmap_Destroy(bitmap);
567
568 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
569 FORM_OnBeforeClosePage(page, form);
570 FPDFText_ClosePage(text_page);
571 FPDF_ClosePage(page);
572 } 594 }
573 595
574 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); 596 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
575 FPDFDOC_ExitFormFillEnvironment(form); 597 FPDFDOC_ExitFormFillEnvironment(form);
576 FPDF_CloseDocument(doc); 598 FPDF_CloseDocument(doc);
577 FPDFAvail_Destroy(pdf_avail); 599 FPDFAvail_Destroy(pdf_avail);
578 600
579 fprintf(stderr, "Rendered %d pages.\n", rendered_pages); 601 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
580 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages); 602 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
581 } 603 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 } 679 }
658 680
659 FPDF_DestroyLibrary(); 681 FPDF_DestroyLibrary();
660 #ifdef PDF_ENABLE_V8 682 #ifdef PDF_ENABLE_V8
661 v8::V8::ShutdownPlatform(); 683 v8::V8::ShutdownPlatform();
662 delete platform; 684 delete platform;
663 #endif // PDF_ENABLE_V8 685 #endif // PDF_ENABLE_V8
664 686
665 return 0; 687 return 0;
666 } 688 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698