1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "public/fpdfview.h"
#include <memory>
#include <utility>
#include "core/fpdfapi/cpdf_modulemgr.h"
#include "core/fpdfapi/cpdf_pagerendercontext.h"
#include "core/fpdfapi/page/cpdf_page.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_document.h"
#include "core/fpdfapi/parser/fpdf_parser_decode.h"
#include "core/fpdfapi/render/cpdf_progressiverenderer.h"
#include "core/fpdfapi/render/cpdf_renderoptions.h"
#include "core/fpdfdoc/cpdf_annotlist.h"
#include "core/fpdfdoc/cpdf_nametree.h"
#include "core/fpdfdoc/cpdf_occontext.h"
#include "core/fpdfdoc/cpdf_viewerpreferences.h"
#include "core/fxcodec/fx_codec.h"
#include "core/fxcrt/fx_memory.h"
#include "core/fxcrt/fx_safe_types.h"
#include "core/fxge/cfx_fxgedevice.h"
#include "core/fxge/cfx_gemodule.h"
#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
#include "fpdfsdk/cpdfsdk_pageview.h"
#include "fpdfsdk/fsdk_define.h"
#include "fpdfsdk/fsdk_pauseadapter.h"
#include "fpdfsdk/javascript/ijs_runtime.h"
#include "public/fpdf_ext.h"
#include "public/fpdf_progressive.h"
#include "third_party/base/numerics/safe_conversions_impl.h"
#include "third_party/base/ptr_util.h"
#ifdef PDF_ENABLE_XFA
#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
#include "public/fpdf_formfill.h"
#include "xfa/fxbarcode/BC_Library.h"
#endif // PDF_ENABLE_XFA
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
#include "core/fxge/cfx_windowsdevice.h"
#endif
UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
return static_cast<UnderlyingDocumentType*>(doc);
}
FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
return static_cast<FPDF_DOCUMENT>(doc);
}
UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
return static_cast<UnderlyingPageType*>(page);
}
CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
#ifdef PDF_ENABLE_XFA
return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
#else // PDF_ENABLE_XFA
return UnderlyingFromFPDFDocument(doc);
#endif // PDF_ENABLE_XFA
}
FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
#ifdef PDF_ENABLE_XFA
return doc ? FPDFDocumentFromUnderlying(
new CPDFXFA_Context(pdfium::WrapUnique(doc)))
: nullptr;
#else // PDF_ENABLE_XFA
return FPDFDocumentFromUnderlying(doc);
#endif // PDF_ENABLE_XFA
}
CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
#ifdef PDF_ENABLE_XFA
return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
#else // PDF_ENABLE_XFA
return UnderlyingFromFPDFPage(page);
#endif // PDF_ENABLE_XFA
}
CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
return static_cast<CFX_DIBitmap*>(bitmap);
}
#ifdef PDF_ENABLE_XFA
CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
m_pFS = pFS;
m_nCurPos = 0;
}
IFX_SeekableStream* CFPDF_FileStream::Retain() {
return this;
}
void CFPDF_FileStream::Release() {
if (m_pFS && m_pFS->Release)
m_pFS->Release(m_pFS->clientData);
delete this;
}
FX_FILESIZE CFPDF_FileStream::GetSize() {
if (m_pFS && m_pFS->GetSize)
return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
return 0;
}
bool CFPDF_FileStream::IsEOF() {
return m_nCurPos >= GetSize();
}
FX_FILESIZE CFPDF_FileStream::GetPosition() {
return m_nCurPos;
}
bool CFPDF_FileStream::ReadBlock(void* buffer,
FX_FILESIZE offset,
size_t size) {
if (!buffer || !size || !m_pFS->ReadBlock)
return false;
if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
(FPDF_DWORD)size) == 0) {
m_nCurPos = offset + size;
return true;
}
return false;
}
size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
if (!buffer || !size || !m_pFS->ReadBlock)
return 0;
FX_FILESIZE nSize = GetSize();
if (m_nCurPos >= nSize)
return 0;
FX_FILESIZE dwAvail = nSize - m_nCurPos;
if (dwAvail < (FX_FILESIZE)size)
size = (size_t)dwAvail;
if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
(FPDF_DWORD)size) == 0) {
m_nCurPos += size;
return size;
}
return 0;
}
bool CFPDF_FileStream::WriteBlock(const void* buffer,
FX_FILESIZE offset,
size_t size) {
if (!m_pFS || !m_pFS->WriteBlock)
return false;
if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
(FPDF_DWORD)size) == 0) {
m_nCurPos = offset + size;
return true;
}
return false;
}
bool CFPDF_FileStream::Flush() {
if (!m_pFS || !m_pFS->Flush)
return true;
return m_pFS->Flush(m_pFS->clientData) == 0;
}
#endif // PDF_ENABLE_XFA
CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
: m_FileAccess(*pFileAccess) {}
FX_FILESIZE CPDF_CustomAccess::GetSize() {
return m_FileAccess.m_FileLen;
}
void CPDF_CustomAccess::Release() {
delete this;
}
bool CPDF_CustomAccess::ReadBlock(void* buffer,
FX_FILESIZE offset,
size_t size) {
if (offset < 0)
return false;
FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
newPos += offset;
if (!newPos.IsValid() ||
newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) {
return false;
}
return !!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,
reinterpret_cast<uint8_t*>(buffer), size);
}
// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
static uint32_t foxit_sandbox_policy = 0xFFFFFFFF;
void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
switch (policy) {
case FPDF_POLICY_MACHINETIME_ACCESS: {
if (enable)
foxit_sandbox_policy |= 0x01;
else
foxit_sandbox_policy &= 0xFFFFFFFE;
} break;
default:
break;
}
}
FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
switch (policy) {
case FPDF_POLICY_MACHINETIME_ACCESS:
return !!(foxit_sandbox_policy & 0x01);
default:
return false;
}
}
CCodec_ModuleMgr* g_pCodecModule = nullptr;
DLLEXPORT void STDCALL FPDF_InitLibrary() {
FPDF_InitLibraryWithConfig(nullptr);
}
DLLEXPORT void STDCALL
FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
g_pCodecModule = new CCodec_ModuleMgr();
CFX_GEModule* pModule = CFX_GEModule::Get();
pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr, g_pCodecModule);
CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
pModuleMgr->SetCodecModule(g_pCodecModule);
pModuleMgr->InitPageModule();
pModuleMgr->LoadEmbeddedGB1CMaps();
pModuleMgr->LoadEmbeddedJapan1CMaps();
pModuleMgr->LoadEmbeddedCNS1CMaps();
pModuleMgr->LoadEmbeddedKorea1CMaps();
#ifdef PDF_ENABLE_XFA
FXJSE_Initialize();
BC_Library_Init();
#endif // PDF_ENABLE_XFA
if (cfg && cfg->version >= 2)
IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
}
DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
#ifdef PDF_ENABLE_XFA
BC_Library_Destory();
FXJSE_Finalize();
#endif // PDF_ENABLE_XFA
CPDF_ModuleMgr::Destroy();
CFX_GEModule::Destroy();
delete g_pCodecModule;
g_pCodecModule = nullptr;
IJS_Runtime::Destroy();
}
#ifndef _WIN32
int g_LastError;
void SetLastError(int err) {
g_LastError = err;
}
int GetLastError() {
return g_LastError;
}
#endif // _WIN32
void ProcessParseError(CPDF_Parser::Error err) {
uint32_t err_code = FPDF_ERR_SUCCESS;
// Translate FPDFAPI error code to FPDFVIEW error code
switch (err) {
case CPDF_Parser::SUCCESS:
err_code = FPDF_ERR_SUCCESS;
break;
case CPDF_Parser::FILE_ERROR:
err_code = FPDF_ERR_FILE;
break;
case CPDF_Parser::FORMAT_ERROR:
err_code = FPDF_ERR_FORMAT;
break;
case CPDF_Parser::PASSWORD_ERROR:
err_code = FPDF_ERR_PASSWORD;
break;
case CPDF_Parser::HANDLER_ERROR:
err_code = FPDF_ERR_SECURITY;
break;
}
SetLastError(err_code);
}
DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
FPDF_BOOL enable) {
return FSDK_SetSandBoxPolicy(policy, enable);
}
#if defined(_WIN32) && defined(PDFIUM_PRINT_TEXT_WITH_GDI)
DLLEXPORT void STDCALL
FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) {
g_pdfium_typeface_accessible_func = func;
}
DLLEXPORT void STDCALL FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) {
g_pdfium_print_text_with_gdi = !!use_gdi;
}
#endif
DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
FPDF_BYTESTRING password) {
// NOTE: the creation of the file needs to be by the embedder on the
// other side of this API.
IFX_SeekableReadStream* pFileAccess =
FX_CreateFileRead((const FX_CHAR*)file_path);
if (!pFileAccess) {
return nullptr;
}
std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
pParser->SetPassword(password);
std::unique_ptr<CPDF_Document> pDocument(
new CPDF_Document(std::move(pParser)));
CPDF_Parser::Error error =
pDocument->GetParser()->StartParse(pFileAccess, pDocument.get());
if (error != CPDF_Parser::SUCCESS) {
ProcessParseError(error);
return nullptr;
}
return FPDFDocumentFromCPDFDocument(pDocument.release());
}
#ifdef PDF_ENABLE_XFA
DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
int* docType) {
if (!document)
return false;
CPDF_Document* pdfDoc =
(static_cast<CPDFXFA_Context*>(document))->GetPDFDoc();
if (!pdfDoc)
return false;
CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
if (!pRoot)
return false;
CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
if (!pAcroForm)
return false;
CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA");
if (!pXFA)
return false;
bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false);
*docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA;
return true;
}
DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
return document && (static_cast<CPDFXFA_Context*>(document))->LoadXFADoc();
}
#endif // PDF_ENABLE_XFA
class CMemFile final : public IFX_SeekableReadStream {
public:
CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
void Release() override { delete this; }
FX_FILESIZE GetSize() override { return m_size; }
bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
if (offset < 0) {
return false;
}
FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
newPos += offset;
if (!newPos.IsValid() || newPos.ValueOrDie() > m_size) {
return false;
}
FXSYS_memcpy(buffer, m_pBuf + offset, size);
return true;
}
private:
~CMemFile() override {}
uint8_t* const m_pBuf;
const FX_FILESIZE m_size;
};
DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
int size,
FPDF_BYTESTRING password) {
CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
pParser->SetPassword(password);
std::unique_ptr<CPDF_Document> pDocument(
new CPDF_Document(std::move(pParser)));
CPDF_Parser::Error error =
pDocument->GetParser()->StartParse(pMemFile, pDocument.get());
if (error != CPDF_Parser::SUCCESS) {
ProcessParseError(error);
return nullptr;
}
CheckUnSupportError(pDocument.get(), error);
return FPDFDocumentFromCPDFDocument(pDocument.release());
}
DLLEXPORT FPDF_DOCUMENT STDCALL
FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
FPDF_BYTESTRING password) {
CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
pParser->SetPassword(password);
std::unique_ptr<CPDF_Document> pDocument(
new CPDF_Document(std::move(pParser)));
CPDF_Parser::Error error =
pDocument->GetParser()->StartParse(pFile, pDocument.get());
if (error != CPDF_Parser::SUCCESS) {
ProcessParseError(error);
return nullptr;
}
CheckUnSupportError(pDocument.get(), error);
return FPDFDocumentFromCPDFDocument(pDocument.release());
}
DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
int* fileVersion) {
if (!fileVersion)
return false;
*fileVersion = 0;
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
if (!pDoc)
return false;
CPDF_Parser* pParser = pDoc->GetParser();
if (!pParser)
return false;
*fileVersion = pParser->GetFileVersion();
return true;
}
// jabdelmalek: changed return type from uint32_t to build on Linux (and match
// header).
DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
// https://bugs.chromium.org/p/pdfium/issues/detail?id=499
if (!pDoc) {
#ifndef PDF_ENABLE_XFA
return 0;
#else // PDF_ENABLE_XFA
return 0xFFFFFFFF;
#endif // PDF_ENABLE_XFA
}
return pDoc->GetUserPermissions();
}
DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc || !pDoc->GetParser())
return -1;
CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
return pDict ? pDict->GetIntegerFor("R") : -1;
}
DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
return pDoc ? pDoc->GetPageCount() : 0;
}
DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
int page_index) {
UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
if (!pDoc)
return nullptr;
if (page_index < 0 || page_index >= pDoc->GetPageCount())
return nullptr;
#ifdef PDF_ENABLE_XFA
return pDoc->GetXFAPage(page_index);
#else // PDF_ENABLE_XFA
CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
if (!pDict)
return nullptr;
CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true);
pPage->ParseContent();
return pPage;
#endif // PDF_ENABLE_XFA
}
DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
return pPage ? pPage->GetPageWidth() : 0.0;
}
DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
return pPage ? pPage->GetPageHeight() : 0.0;
}
#if defined(_WIN32)
DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
FPDF_PAGE page,
int start_x,
int start_y,
int size_x,
int size_y,
int rotate,
int flags) {
CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
if (!pPage)
return;
CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
pPage->SetRenderContext(pdfium::WrapUnique(pContext));
std::unique_ptr<CFX_DIBitmap> pBitmap;
const bool bNewBitmap =
pPage->BackgroundAlphaNeeded() || pPage->HasImageMask();
if (bNewBitmap) {
pBitmap = pdfium::MakeUnique<CFX_DIBitmap>();
pBitmap->Create(size_x, size_y, FXDIB_Argb);
pBitmap->Clear(0x00ffffff);
CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
pContext->m_pDevice = pdfium::WrapUnique(pDevice);
pDevice->Attach(pBitmap.get(), false, nullptr, false);
} else {
pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsDevice>(dc);
}
FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
rotate, flags, true, nullptr);
if (bNewBitmap) {
CFX_WindowsDevice WinDC(dc);
if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
std::unique_ptr<CFX_DIBitmap> pDst = pdfium::MakeUnique<CFX_DIBitmap>();
int pitch = pBitmap->GetPitch();
pDst->Create(size_x, size_y, FXDIB_Rgb32);
FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap.get(), 0, 0,
FXDIB_BLEND_NORMAL, nullptr, false, nullptr);
WinDC.StretchDIBits(pDst.get(), 0, 0, size_x, size_y);
} else {
WinDC.SetDIBits(pBitmap.get(), 0, 0);
}
}
pPage->SetRenderContext(nullptr);
}
#endif // defined(_WIN32)
DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
FPDF_PAGE page,
int start_x,
int start_y,
int size_x,
int size_y,
int rotate,
int flags) {
if (!bitmap)
return;
CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
if (!pPage)
return;
CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
pPage->SetRenderContext(pdfium::WrapUnique(pContext));
CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
pContext->m_pDevice.reset(pDevice);
CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
rotate, flags, true, nullptr);
pPage->SetRenderContext(nullptr);
}
#ifdef _SKIA_SUPPORT_
DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page,
int size_x,
int size_y) {
CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
if (!pPage)
return nullptr;
CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
pPage->SetRenderContext(pdfium::WrapUnique(pContext));
CFX_FxgeDevice* skDevice = new CFX_FxgeDevice;
FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
pContext->m_pDevice.reset(skDevice);
FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, true,
nullptr);
pPage->SetRenderContext(nullptr);
return recorder;
}
#endif
DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
if (!page)
return;
#ifdef PDF_ENABLE_XFA
pPage->Release();
#else // PDF_ENABLE_XFA
CPDFSDK_PageView* pPageView =
static_cast<CPDFSDK_PageView*>(pPage->GetView());
if (pPageView) {
// We're already destroying the pageview, so bail early.
if (pPageView->IsBeingDestroyed())
return;
if (pPageView->IsLocked()) {
pPageView->TakePageOwnership();
return;
}
bool owned = pPageView->OwnsPage();
// This will delete the |pPageView| object. We must cleanup the PageView
// first because it will attempt to reset the View on the |pPage| during
// destruction.
pPageView->GetFormFillEnv()->RemovePageView(pPage);
// If the page was owned then the pageview will have deleted the page.
if (owned)
return;
}
delete pPage;
#endif // PDF_ENABLE_XFA
}
DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
delete UnderlyingFromFPDFDocument(document);
}
DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
return GetLastError();
}
DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
int start_x,
int start_y,
int size_x,
int size_y,
int rotate,
int device_x,
int device_y,
double* page_x,
double* page_y) {
if (!page || !page_x || !page_y)
return;
UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
#ifdef PDF_ENABLE_XFA
pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
device_y, page_x, page_y);
#else // PDF_ENABLE_XFA
CFX_Matrix page2device;
pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
rotate);
CFX_Matrix device2page;
device2page.SetReverse(page2device);
FX_FLOAT page_x_f, page_y_f;
device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
page_y_f);
*page_x = (page_x_f);
*page_y = (page_y_f);
#endif // PDF_ENABLE_XFA
}
DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
int start_x,
int start_y,
int size_x,
int size_y,
int rotate,
double page_x,
double page_y,
int* device_x,
int* device_y) {
if (!device_x || !device_y)
return;
UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
if (!pPage)
return;
#ifdef PDF_ENABLE_XFA
pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
device_x, device_y);
#else // PDF_ENABLE_XFA
CFX_Matrix page2device;
pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
rotate);
FX_FLOAT device_x_f, device_y_f;
page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
device_y_f);
*device_x = FXSYS_round(device_x_f);
*device_y = FXSYS_round(device_y_f);
#endif // PDF_ENABLE_XFA
}
DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
int height,
int alpha) {
std::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
return nullptr;
}
return pBitmap.release();
}
DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
int height,
int format,
void* first_scan,
int stride) {
FXDIB_Format fx_format;
switch (format) {
case FPDFBitmap_Gray:
fx_format = FXDIB_8bppRgb;
break;
case FPDFBitmap_BGR:
fx_format = FXDIB_Rgb;
break;
case FPDFBitmap_BGRx:
fx_format = FXDIB_Rgb32;
break;
case FPDFBitmap_BGRA:
fx_format = FXDIB_Argb;
break;
default:
return nullptr;
}
CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
return pBitmap;
}
DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
int left,
int top,
int width,
int height,
FPDF_DWORD color) {
if (!bitmap)
return;
CFX_FxgeDevice device;
CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
device.Attach(pBitmap, false, nullptr, false);
if (!pBitmap->HasAlpha())
color |= 0xFF000000;
FX_RECT rect(left, top, left + width, top + height);
device.FillRect(&rect, color);
}
DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr;
}
DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0;
}
DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0;
}
DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0;
}
DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
delete CFXBitmapFromFPDFBitmap(bitmap);
}
void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext,
FPDF_PAGE page,
int start_x,
int start_y,
int size_x,
int size_y,
int rotate,
int flags,
bool bNeedToRestore,
IFSDK_PAUSE_Adapter* pause) {
CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
if (!pPage)
return;
if (!pContext->m_pOptions)
pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
if (flags & FPDF_LCD_TEXT)
pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
else
pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
if (flags & FPDF_NO_NATIVETEXT)
pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
if (flags & FPDF_RENDER_FORCEHALFTONE)
pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
#ifndef PDF_ENABLE_XFA
if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
if (flags & FPDF_RENDER_NO_SMOOTHPATH)
pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
#endif // PDF_ENABLE_XFA
// Grayscale output
if (flags & FPDF_GRAYSCALE) {
pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
pContext->m_pOptions->m_ForeColor = 0;
pContext->m_pOptions->m_BackColor = 0xffffff;
}
const CPDF_OCContext::UsageType usage =
(flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
pContext->m_pOptions->m_AddFlags = flags >> 8;
pContext->m_pOptions->m_pOCContext =
new CPDF_OCContext(pPage->m_pDocument, usage);
CFX_Matrix matrix;
pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
pContext->m_pDevice->SaveState();
pContext->m_pDevice->SetClip_Rect(
FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y));
pContext->m_pContext = pdfium::MakeUnique<CPDF_RenderContext>(pPage);
pContext->m_pContext->AppendLayer(pPage, &matrix);
if (flags & FPDF_ANNOT) {
pContext->m_pAnnots = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
bool bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext.get(),
bPrinting, &matrix, false, nullptr);
}
pContext->m_pRenderer = pdfium::MakeUnique<CPDF_ProgressiveRenderer>(
pContext->m_pContext.get(), pContext->m_pDevice.get(),
pContext->m_pOptions.get());
pContext->m_pRenderer->Start(pause);
if (bNeedToRestore)
pContext->m_pDevice->RestoreState(false);
}
DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
int page_index,
double* width,
double* height) {
UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
if (!pDoc)
return false;
#ifdef PDF_ENABLE_XFA
int count = pDoc->GetPageCount();
if (page_index < 0 || page_index >= count)
return false;
CPDFXFA_Page* pPage = pDoc->GetXFAPage(page_index);
if (!pPage)
return false;
*width = pPage->GetPageWidth();
*height = pPage->GetPageHeight();
#else // PDF_ENABLE_XFA
CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
if (!pDict)
return false;
CPDF_Page page(pDoc, pDict, true);
*width = page.GetPageWidth();
*height = page.GetPageHeight();
#endif // PDF_ENABLE_XFA
return true;
}
DLLEXPORT FPDF_BOOL STDCALL
FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc)
return true;
CPDF_ViewerPreferences viewRef(pDoc);
return viewRef.PrintScaling();
}
DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc)
return 1;
CPDF_ViewerPreferences viewRef(pDoc);
return viewRef.NumCopies();
}
DLLEXPORT FPDF_PAGERANGE STDCALL
FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc)
return nullptr;
CPDF_ViewerPreferences viewRef(pDoc);
return viewRef.PrintPageRange();
}
DLLEXPORT FPDF_DUPLEXTYPE STDCALL
FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc)
return DuplexUndefined;
CPDF_ViewerPreferences viewRef(pDoc);
CFX_ByteString duplex = viewRef.Duplex();
if ("Simplex" == duplex)
return Simplex;
if ("DuplexFlipShortEdge" == duplex)
return DuplexFlipShortEdge;
if ("DuplexFlipLongEdge" == duplex)
return DuplexFlipLongEdge;
return DuplexUndefined;
}
DLLEXPORT unsigned long STDCALL FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document,
FPDF_BYTESTRING key,
char* buffer,
unsigned long length) {
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc)
return 0;
CPDF_ViewerPreferences viewRef(pDoc);
CFX_ByteString bsVal;
if (!viewRef.GenericName(key, &bsVal))
return 0;
unsigned long dwStringLen = bsVal.GetLength() + 1;
if (buffer && length >= dwStringLen)
memcpy(buffer, bsVal.c_str(), dwStringLen);
return dwStringLen;
}
DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc)
return 0;
CPDF_Dictionary* pRoot = pDoc->GetRoot();
if (!pRoot)
return 0;
CPDF_NameTree nameTree(pDoc, "Dests");
pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
if (pDest)
count += pDest->GetCount();
if (!count.IsValid())
return 0;
return count.ValueOrDie();
}
DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
FPDF_BYTESTRING name) {
if (!name || name[0] == 0)
return nullptr;
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc)
return nullptr;
CPDF_NameTree name_tree(pDoc, "Dests");
return name_tree.LookupNamedDest(pDoc, name);
}
#ifdef PDF_ENABLE_XFA
DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Init(FPDF_BSTR* str) {
if (!str)
return -1;
FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
return 0;
}
DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Set(FPDF_BSTR* str,
FPDF_LPCSTR bstr,
int length) {
if (!str)
return -1;
if (!bstr || !length)
return -1;
if (length == -1)
length = FXSYS_strlen(bstr);
if (length == 0) {
if (str->str) {
FX_Free(str->str);
str->str = nullptr;
}
str->len = 0;
return 0;
}
if (str->str && str->len < length)
str->str = FX_Realloc(char, str->str, length + 1);
else if (!str->str)
str->str = FX_Alloc(char, length + 1);
str->str[length] = 0;
FXSYS_memcpy(str->str, bstr, length);
str->len = length;
return 0;
}
DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Clear(FPDF_BSTR* str) {
if (!str)
return -1;
if (str->str) {
FX_Free(str->str);
str->str = nullptr;
}
str->len = 0;
return 0;
}
#endif // PDF_ENABLE_XFA
DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
int index,
void* buffer,
long* buflen) {
if (!buffer)
*buflen = 0;
if (index < 0)
return nullptr;
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc)
return nullptr;
CPDF_Dictionary* pRoot = pDoc->GetRoot();
if (!pRoot)
return nullptr;
CPDF_Object* pDestObj = nullptr;
CFX_ByteString bsName;
CPDF_NameTree nameTree(pDoc, "Dests");
int count = nameTree.GetCount();
if (index >= count) {
CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
if (!pDest)
return nullptr;
pdfium::base::CheckedNumeric<int> checked_count = count;
checked_count += pDest->GetCount();
if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
return nullptr;
index -= count;
int i = 0;
for (const auto& it : *pDest) {
bsName = it.first;
pDestObj = it.second;
if (!pDestObj)
continue;
if (i == index)
break;
i++;
}
} else {
pDestObj = nameTree.LookupValue(index, bsName);
}
if (!pDestObj)
return nullptr;
if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
pDestObj = pDict->GetArrayFor("D");
if (!pDestObj)
return nullptr;
}
if (!pDestObj->IsArray())
return nullptr;
CFX_WideString wsName = PDF_DecodeText(bsName);
CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
int len = utf16Name.GetLength();
if (!buffer) {
*buflen = len;
} else if (*buflen >= len) {
memcpy(buffer, utf16Name.c_str(), len);
*buflen = len;
} else {
*buflen = -1;
}
return (FPDF_DEST)pDestObj;
}
|