OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 int32_t 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 is available.\ n"); | |
jun_fang
2015/10/29 15:31:45
format error.
Tom Sepez
2015/10/29 16:20:17
nit: run git cl format.
jun_fang
2015/10/30 06:02:43
Acknowledged.
| |
456 return; | |
457 } | |
458 if (FPDFAvail_IsFormAvail(pdf_avail, &hints) == PDF_FORM_NOTAVAIL) { | |
jun_fang
2015/10/29 15:31:45
should check whether an error is returned.
| |
459 fprintf(stderr, "Form is not available.\n"); | |
460 return; | |
461 } | |
462 bIsLinearized = TRUE; | |
463 } | |
jun_fang
2015/10/29 15:31:45
format error.
| |
387 } else { | 464 } else { |
388 fprintf(stderr, "Non-linearized path...\n"); | 465 fprintf(stderr, "Non-linearized path...\n"); |
389 doc = FPDF_LoadCustomDocument(&file_access, nullptr); | 466 doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
390 } | 467 } |
391 | 468 |
392 if (!doc) { | 469 if (!doc) { |
393 unsigned long err = FPDF_GetLastError(); | 470 unsigned long err = FPDF_GetLastError(); |
394 fprintf(stderr, "Load pdf docs unsuccessful: "); | 471 fprintf(stderr, "Load pdf docs unsuccessful: "); |
395 switch (err) { | 472 switch (err) { |
396 case FPDF_ERR_SUCCESS: | 473 case FPDF_ERR_SUCCESS: |
(...skipping 20 matching lines...) Expand all Loading... | |
417 default: | 494 default: |
418 fprintf(stderr, "Unknown error %ld", err); | 495 fprintf(stderr, "Unknown error %ld", err); |
419 } | 496 } |
420 fprintf(stderr, ".\n"); | 497 fprintf(stderr, ".\n"); |
421 | 498 |
422 FPDFAvail_Destroy(pdf_avail); | 499 FPDFAvail_Destroy(pdf_avail); |
423 return; | 500 return; |
424 } | 501 } |
425 | 502 |
426 (void)FPDF_GetDocPermissions(doc); | 503 (void)FPDF_GetDocPermissions(doc); |
427 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints); | |
428 | 504 |
429 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); | 505 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); |
430 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); | 506 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); |
431 FPDF_SetFormFieldHighlightAlpha(form, 100); | 507 FPDF_SetFormFieldHighlightAlpha(form, 100); |
432 | 508 |
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); | 509 FORM_DoDocumentJSAction(form); |
442 FORM_DoDocumentOpenAction(form); | 510 FORM_DoDocumentOpenAction(form); |
443 | 511 |
512 int page_count = FPDF_GetPageCount(doc); | |
444 int rendered_pages = 0; | 513 int rendered_pages = 0; |
445 int bad_pages = 0; | 514 int bad_pages = 0; |
446 for (int i = 0; i < page_count; ++i) { | 515 for (int i = 0; i < page_count; ++i) { |
447 FPDF_PAGE page = FPDF_LoadPage(doc, i); | 516 if (bIsLinearized) { |
448 if (!page) { | 517 nRet = PDF_DATA_NOTAVAIL; |
518 while (nRet == PDF_DATA_NOTAVAIL) { | |
519 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); | |
520 } | |
521 if (nRet == PDF_DATA_ERROR) { | |
522 fprintf(stderr, "Unknown error in checking if page %d is available.\n", | |
523 i); | |
524 return; | |
525 } | |
526 } | |
527 if (RenderPage(name, doc, form, i, options)) { | |
528 ++rendered_pages; | |
529 } else { | |
449 ++bad_pages; | 530 ++bad_pages; |
450 continue; | |
451 } | 531 } |
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 } | 532 } |
508 | 533 |
509 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); | 534 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
510 FPDFDOC_ExitFormFillEnvironment(form); | 535 FPDFDOC_ExitFormFillEnvironment(form); |
511 FPDF_CloseDocument(doc); | 536 FPDF_CloseDocument(doc); |
512 FPDFAvail_Destroy(pdf_avail); | 537 FPDFAvail_Destroy(pdf_avail); |
513 | 538 |
514 fprintf(stderr, "Rendered %d pages.\n", rendered_pages); | 539 fprintf(stderr, "Rendered %d pages.\n", rendered_pages); |
515 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages); | 540 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages); |
516 } | 541 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
581 } | 606 } |
582 | 607 |
583 FPDF_DestroyLibrary(); | 608 FPDF_DestroyLibrary(); |
584 #ifdef PDF_ENABLE_V8 | 609 #ifdef PDF_ENABLE_V8 |
585 v8::V8::ShutdownPlatform(); | 610 v8::V8::ShutdownPlatform(); |
586 delete platform; | 611 delete platform; |
587 #endif // PDF_ENABLE_V8 | 612 #endif // PDF_ENABLE_V8 |
588 | 613 |
589 return 0; | 614 return 0; |
590 } | 615 } |
OLD | NEW |