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

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: 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 9
10 #include <list> 10 #include <list>
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 return true; 337 return true;
338 } 338 }
339 339
340 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { 340 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
341 return true; 341 return true;
342 } 342 }
343 343
344 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { 344 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
345 } 345 }
346 346
347 FPDF_BOOL RenderPage(const std::string& name,
348 const FPDF_DOCUMENT& doc,
349 const FPDF_FORMHANDLE& form,
350 const int page_index,
351 const Options& options) {
352 FPDF_PAGE page = FPDF_LoadPage(doc, page_index);
353 if (!page) {
354 return FALSE;
355 }
356 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
357 FORM_OnAfterLoadPage(page, form);
358 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
359
360 double scale = 1.0;
361 if (!options.scale_factor_as_string.empty()) {
362 std::stringstream(options.scale_factor_as_string) >> scale;
363 }
364 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
365 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
366
367 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
368 if (!bitmap) {
369 fprintf(stderr, "Page was too large to be rendered.\n");
370 return FALSE;
371 }
372
373 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
374 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
375
376 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
377 int stride = FPDFBitmap_GetStride(bitmap);
378 const char* buffer =
379 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
380
381 switch (options.output_format) {
382 #ifdef _WIN32
383 case OUTPUT_BMP:
384 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
385 break;
386
387 case OUTPUT_EMF:
388 WriteEmf(page, name.c_str(), page_index);
389 break;
390 #endif
391 case OUTPUT_PNG:
392 WritePng(name.c_str(), page_index, buffer, stride, width, height);
393 break;
394
395 case OUTPUT_PPM:
396 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
397 break;
398
399 default:
400 break;
401 }
402
403 FPDFBitmap_Destroy(bitmap);
404 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
405 FORM_OnBeforeClosePage(page, form);
406 FPDFText_ClosePage(text_page);
407 FPDF_ClosePage(page);
408 return TRUE;
409 }
410
347 void RenderPdf(const std::string& name, const char* pBuf, size_t len, 411 void RenderPdf(const std::string& name, const char* pBuf, size_t len,
348 const Options& options) { 412 const Options& options) {
349 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str()); 413 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
350 414
351 IPDF_JSPLATFORM platform_callbacks; 415 IPDF_JSPLATFORM platform_callbacks;
352 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); 416 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
353 platform_callbacks.version = 3; 417 platform_callbacks.version = 3;
354 platform_callbacks.app_alert = ExampleAppAlert; 418 platform_callbacks.app_alert = ExampleAppAlert;
355 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; 419 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
356 420
(...skipping 13 matching lines...) Expand all
370 memset(&file_avail, '\0', sizeof(file_avail)); 434 memset(&file_avail, '\0', sizeof(file_avail));
371 file_avail.version = 1; 435 file_avail.version = 1;
372 file_avail.IsDataAvail = Is_Data_Avail; 436 file_avail.IsDataAvail = Is_Data_Avail;
373 437
374 FX_DOWNLOADHINTS hints; 438 FX_DOWNLOADHINTS hints;
375 memset(&hints, '\0', sizeof(hints)); 439 memset(&hints, '\0', sizeof(hints));
376 hints.version = 1; 440 hints.version = 1;
377 hints.AddSegment = Add_Segment; 441 hints.AddSegment = Add_Segment;
378 442
379 FPDF_DOCUMENT doc; 443 FPDF_DOCUMENT doc;
444 int nRet = PDF_DATA_NOTAVAIL;
445 FPDF_BOOL bIsLinearized = FALSE;
380 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); 446 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
381 447 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
382 (void)FPDFAvail_IsDocAvail(pdf_avail, &hints);
383
384 if (FPDFAvail_IsLinearized(pdf_avail)) {
385 fprintf(stderr, "Linearized path...\n"); 448 fprintf(stderr, "Linearized path...\n");
386 doc = FPDFAvail_GetDocument(pdf_avail, nullptr); 449 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
450 if (doc) {
451 while (nRet == PDF_DATA_NOTAVAIL) {
452 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
453 }
454 if (nRet == PDF_DATA_ERROR) {
455 fprintf(stderr, "Unknown error in checking if doc was available.\n");
456 return;
457 }
458 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints);
459 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
460 fprintf(stderr,
461 "Error %d was returned in checking if form was available.\n",
462 nRet);
463 return;
464 }
465 bIsLinearized = TRUE;
466 }
387 } else { 467 } else {
388 fprintf(stderr, "Non-linearized path...\n"); 468 fprintf(stderr, "Non-linearized path...\n");
389 doc = FPDF_LoadCustomDocument(&file_access, nullptr); 469 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
390 } 470 }
391 471
392 if (!doc) { 472 if (!doc) {
393 unsigned long err = FPDF_GetLastError(); 473 unsigned long err = FPDF_GetLastError();
394 fprintf(stderr, "Load pdf docs unsuccessful: "); 474 fprintf(stderr, "Load pdf docs unsuccessful: ");
395 switch (err) { 475 switch (err) {
396 case FPDF_ERR_SUCCESS: 476 case FPDF_ERR_SUCCESS:
(...skipping 20 matching lines...) Expand all
417 default: 497 default:
418 fprintf(stderr, "Unknown error %ld", err); 498 fprintf(stderr, "Unknown error %ld", err);
419 } 499 }
420 fprintf(stderr, ".\n"); 500 fprintf(stderr, ".\n");
421 501
422 FPDFAvail_Destroy(pdf_avail); 502 FPDFAvail_Destroy(pdf_avail);
423 return; 503 return;
424 } 504 }
425 505
426 (void)FPDF_GetDocPermissions(doc); 506 (void)FPDF_GetDocPermissions(doc);
427 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints);
428 507
429 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); 508 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
430 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); 509 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
431 FPDF_SetFormFieldHighlightAlpha(form, 100); 510 FPDF_SetFormFieldHighlightAlpha(form, 100);
432 511
433 int first_page = FPDFAvail_GetFirstPageNum(doc);
434 (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
435
436 int page_count = FPDF_GetPageCount(doc);
437 for (int i = 0; i < page_count; ++i) {
438 (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
439 }
440
441 FORM_DoDocumentJSAction(form); 512 FORM_DoDocumentJSAction(form);
442 FORM_DoDocumentOpenAction(form); 513 FORM_DoDocumentOpenAction(form);
443 514
515 int page_count = FPDF_GetPageCount(doc);
444 int rendered_pages = 0; 516 int rendered_pages = 0;
445 int bad_pages = 0; 517 int bad_pages = 0;
446 for (int i = 0; i < page_count; ++i) { 518 for (int i = 0; i < page_count; ++i) {
447 FPDF_PAGE page = FPDF_LoadPage(doc, i); 519 if (bIsLinearized) {
448 if (!page) { 520 nRet = PDF_DATA_NOTAVAIL;
521 while (nRet == PDF_DATA_NOTAVAIL) {
522 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
523 }
524 if (nRet == PDF_DATA_ERROR) {
525 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
526 i);
527 return;
528 }
529 }
530 if (RenderPage(name, doc, form, i, options)) {
531 ++rendered_pages;
532 } else {
449 ++bad_pages; 533 ++bad_pages;
450 continue;
451 } 534 }
452 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
453 FORM_OnAfterLoadPage(page, form);
454 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
455
456 double scale = 1.0;
457 if (!options.scale_factor_as_string.empty()) {
458 std::stringstream(options.scale_factor_as_string) >> scale;
459 }
460 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
461 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
462
463 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
464 if (!bitmap) {
465 fprintf(stderr, "Page was too large to be rendered.\n");
466 bad_pages++;
467 continue;
468 }
469
470 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
471 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
472 ++rendered_pages;
473
474 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
475 int stride = FPDFBitmap_GetStride(bitmap);
476 const char* buffer =
477 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
478
479 switch (options.output_format) {
480 #ifdef _WIN32
481 case OUTPUT_BMP:
482 WriteBmp(name.c_str(), i, buffer, stride, width, height);
483 break;
484
485 case OUTPUT_EMF:
486 WriteEmf(page, name.c_str(), i);
487 break;
488 #endif
489 case OUTPUT_PNG:
490 WritePng(name.c_str(), i, buffer, stride, width, height);
491 break;
492
493 case OUTPUT_PPM:
494 WritePpm(name.c_str(), i, buffer, stride, width, height);
495 break;
496
497 default:
498 break;
499 }
500
501 FPDFBitmap_Destroy(bitmap);
502
503 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
504 FORM_OnBeforeClosePage(page, form);
505 FPDFText_ClosePage(text_page);
506 FPDF_ClosePage(page);
507 } 535 }
508 536
509 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); 537 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
510 FPDFDOC_ExitFormFillEnvironment(form); 538 FPDFDOC_ExitFormFillEnvironment(form);
511 FPDF_CloseDocument(doc); 539 FPDF_CloseDocument(doc);
512 FPDFAvail_Destroy(pdf_avail); 540 FPDFAvail_Destroy(pdf_avail);
513 541
514 fprintf(stderr, "Rendered %d pages.\n", rendered_pages); 542 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
515 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages); 543 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
516 } 544 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 } 609 }
582 610
583 FPDF_DestroyLibrary(); 611 FPDF_DestroyLibrary();
584 #ifdef PDF_ENABLE_V8 612 #ifdef PDF_ENABLE_V8
585 v8::V8::ShutdownPlatform(); 613 v8::V8::ShutdownPlatform();
586 delete platform; 614 delete platform;
587 #endif // PDF_ENABLE_V8 615 #endif // PDF_ENABLE_V8
588 616
589 return 0; 617 return 0;
590 } 618 }
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