John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdfview.h" |
| 8 | |
Lei Zhang | aa8bf7e | 2015-12-24 19:13:32 -0800 | [diff] [blame] | 9 | #include <memory> |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 10 | #include <utility> |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 11 | #include <vector> |
Lei Zhang | aa8bf7e | 2015-12-24 19:13:32 -0800 | [diff] [blame] | 12 | |
dsinclair | 39c62fd | 2016-09-29 12:49:17 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/cpdf_modulemgr.h" |
| 14 | #include "core/fpdfapi/cpdf_pagerendercontext.h" |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 15 | #include "core/fpdfapi/page/cpdf_image.h" |
| 16 | #include "core/fpdfapi/page/cpdf_imageobject.h" |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 17 | #include "core/fpdfapi/page/cpdf_page.h" |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 18 | #include "core/fpdfapi/page/cpdf_pageobject.h" |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 19 | #include "core/fpdfapi/page/cpdf_pathobject.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 20 | #include "core/fpdfapi/parser/cpdf_array.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 21 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 22 | #include "core/fpdfapi/parser/cpdf_document.h" |
| 23 | #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
dsinclair | 69d9c68 | 2016-10-04 12:18:35 -0700 | [diff] [blame] | 24 | #include "core/fpdfapi/render/cpdf_progressiverenderer.h" |
| 25 | #include "core/fpdfapi/render/cpdf_renderoptions.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 26 | #include "core/fpdfdoc/cpdf_annotlist.h" |
| 27 | #include "core/fpdfdoc/cpdf_nametree.h" |
| 28 | #include "core/fpdfdoc/cpdf_occontext.h" |
| 29 | #include "core/fpdfdoc/cpdf_viewerpreferences.h" |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 30 | #include "core/fxcrt/fx_memory.h" |
| 31 | #include "core/fxcrt/fx_safe_types.h" |
Dan Sinclair | bcd1e70 | 2017-08-31 13:19:18 -0400 | [diff] [blame] | 32 | #include "core/fxcrt/fx_stream.h" |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 33 | #include "core/fxge/cfx_defaultrenderdevice.h" |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 34 | #include "core/fxge/cfx_gemodule.h" |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 35 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 36 | #include "fpdfsdk/cpdfsdk_pageview.h" |
| 37 | #include "fpdfsdk/fsdk_define.h" |
| 38 | #include "fpdfsdk/fsdk_pauseadapter.h" |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 39 | #include "fpdfsdk/javascript/ijs_runtime.h" |
rbpotter | e8468c4 | 2017-07-11 10:04:29 -0700 | [diff] [blame] | 40 | #include "public/fpdf_edit.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 41 | #include "public/fpdf_ext.h" |
Ryan Harrison | a74a627 | 2017-10-16 10:57:48 -0400 | [diff] [blame^] | 42 | #include "public/fpdf_formfill.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 43 | #include "public/fpdf_progressive.h" |
Chris Palmer | e4b035b | 2017-03-26 15:48:34 -0700 | [diff] [blame] | 44 | #include "third_party/base/allocator/partition_allocator/partition_alloc.h" |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 45 | #include "third_party/base/numerics/safe_conversions_impl.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 46 | #include "third_party/base/ptr_util.h" |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 47 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 48 | #ifdef PDF_ENABLE_XFA |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 49 | #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h" |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 50 | #include "fpdfsdk/fpdfxfa/cpdfxfa_page.h" |
| 51 | #include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h" |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 52 | #include "fxbarcode/BC_Library.h" |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 53 | #endif // PDF_ENABLE_XFA |
| 54 | |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 55 | #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 56 | #include "core/fxge/cfx_windowsrenderdevice.h" |
rbpotter | e8468c4 | 2017-07-11 10:04:29 -0700 | [diff] [blame] | 57 | |
| 58 | // These checks are here because core/ and public/ cannot depend on each other. |
| 59 | static_assert(WindowsPrintMode::kModeEmf == FPDF_PRINTMODE_EMF, |
| 60 | "WindowsPrintMode::kModeEmf value mismatch"); |
| 61 | static_assert(WindowsPrintMode::kModeTextOnly == FPDF_PRINTMODE_TEXTONLY, |
| 62 | "WindowsPrintMode::kModeTextOnly value mismatch"); |
| 63 | static_assert(WindowsPrintMode::kModePostScript2 == FPDF_PRINTMODE_POSTSCRIPT2, |
| 64 | "WindowsPrintMode::kModePostScript2 value mismatch"); |
| 65 | static_assert(WindowsPrintMode::kModePostScript3 == FPDF_PRINTMODE_POSTSCRIPT3, |
| 66 | "WindowsPrintMode::kModePostScript3 value mismatch"); |
thestig | 25fa42f | 2016-05-25 21:39:46 -0700 | [diff] [blame] | 67 | #endif |
| 68 | |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 69 | namespace { |
| 70 | |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 71 | bool g_bLibraryInitialized = false; |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 72 | |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 73 | void RenderPageImpl(CPDF_PageRenderContext* pContext, |
| 74 | CPDF_Page* pPage, |
| 75 | const CFX_Matrix& matrix, |
| 76 | const FX_RECT& clipping_rect, |
| 77 | int flags, |
| 78 | bool bNeedToRestore, |
| 79 | IFSDK_PAUSE_Adapter* pause) { |
| 80 | if (!pContext->m_pOptions) |
| 81 | pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>(); |
| 82 | |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 83 | uint32_t option_flags = pContext->m_pOptions->GetFlags(); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 84 | if (flags & FPDF_LCD_TEXT) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 85 | option_flags |= RENDER_CLEARTYPE; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 86 | else |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 87 | option_flags &= ~RENDER_CLEARTYPE; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 88 | |
| 89 | if (flags & FPDF_NO_NATIVETEXT) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 90 | option_flags |= RENDER_NO_NATIVETEXT; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 91 | if (flags & FPDF_RENDER_LIMITEDIMAGECACHE) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 92 | option_flags |= RENDER_LIMITEDIMAGECACHE; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 93 | if (flags & FPDF_RENDER_FORCEHALFTONE) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 94 | option_flags |= RENDER_FORCE_HALFTONE; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 95 | #ifndef PDF_ENABLE_XFA |
| 96 | if (flags & FPDF_RENDER_NO_SMOOTHTEXT) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 97 | option_flags |= RENDER_NOTEXTSMOOTH; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 98 | if (flags & FPDF_RENDER_NO_SMOOTHIMAGE) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 99 | option_flags |= RENDER_NOIMAGESMOOTH; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 100 | if (flags & FPDF_RENDER_NO_SMOOTHPATH) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 101 | option_flags |= RENDER_NOPATHSMOOTH; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 102 | #endif // PDF_ENABLE_XFA |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 103 | pContext->m_pOptions->SetFlags(option_flags); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 104 | |
| 105 | // Grayscale output |
Dan Sinclair | f55e72e | 2017-07-13 14:53:28 -0400 | [diff] [blame] | 106 | if (flags & FPDF_GRAYSCALE) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 107 | pContext->m_pOptions->SetColorMode(CPDF_RenderOptions::kGray); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 108 | |
| 109 | const CPDF_OCContext::UsageType usage = |
| 110 | (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View; |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 111 | pContext->m_pOptions->SetOCContext( |
| 112 | pdfium::MakeRetain<CPDF_OCContext>(pPage->m_pDocument.Get(), usage)); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 113 | |
| 114 | pContext->m_pDevice->SaveState(); |
| 115 | pContext->m_pDevice->SetClip_Rect(clipping_rect); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 116 | pContext->m_pContext = pdfium::MakeUnique<CPDF_RenderContext>(pPage); |
| 117 | pContext->m_pContext->AppendLayer(pPage, &matrix); |
| 118 | |
| 119 | if (flags & FPDF_ANNOT) { |
| 120 | pContext->m_pAnnots = pdfium::MakeUnique<CPDF_AnnotList>(pPage); |
| 121 | bool bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY; |
| 122 | pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext.get(), |
| 123 | bPrinting, &matrix, false, nullptr); |
| 124 | } |
| 125 | |
| 126 | pContext->m_pRenderer = pdfium::MakeUnique<CPDF_ProgressiveRenderer>( |
| 127 | pContext->m_pContext.get(), pContext->m_pDevice.get(), |
| 128 | pContext->m_pOptions.get()); |
| 129 | pContext->m_pRenderer->Start(pause); |
| 130 | if (bNeedToRestore) |
| 131 | pContext->m_pDevice->RestoreState(false); |
| 132 | } |
| 133 | |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 134 | class CPDF_CustomAccess final : public IFX_SeekableReadStream { |
| 135 | public: |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 136 | template <typename T, typename... Args> |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 137 | friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 138 | |
| 139 | // IFX_SeekableReadStream |
| 140 | FX_FILESIZE GetSize() override; |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 141 | bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
| 142 | |
| 143 | private: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 144 | explicit CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess); |
| 145 | |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 146 | FPDF_FILEACCESS m_FileAccess; |
| 147 | }; |
| 148 | |
| 149 | CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) |
| 150 | : m_FileAccess(*pFileAccess) {} |
| 151 | |
| 152 | FX_FILESIZE CPDF_CustomAccess::GetSize() { |
| 153 | return m_FileAccess.m_FileLen; |
| 154 | } |
| 155 | |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 156 | bool CPDF_CustomAccess::ReadBlock(void* buffer, |
| 157 | FX_FILESIZE offset, |
| 158 | size_t size) { |
| 159 | if (offset < 0) |
| 160 | return false; |
| 161 | |
| 162 | FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size); |
| 163 | newPos += offset; |
| 164 | if (!newPos.IsValid() || |
| 165 | newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) { |
| 166 | return false; |
| 167 | } |
| 168 | return !!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 169 | static_cast<uint8_t*>(buffer), size); |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 172 | #ifdef PDF_ENABLE_XFA |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 173 | class FPDF_FileHandlerContext : public IFX_SeekableStream { |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 174 | public: |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 175 | template <typename T, typename... Args> |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 176 | friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 177 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 178 | ~FPDF_FileHandlerContext() override; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 179 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 180 | // IFX_SeekableStream: |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 181 | FX_FILESIZE GetSize() override; |
| 182 | bool IsEOF() override; |
| 183 | FX_FILESIZE GetPosition() override; |
| 184 | bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
| 185 | size_t ReadBlock(void* buffer, size_t size) override; |
| 186 | bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; |
| 187 | bool Flush() override; |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 188 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 189 | void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; } |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 190 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 191 | protected: |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 192 | explicit FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS); |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 193 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 194 | FPDF_FILEHANDLER* m_pFS; |
| 195 | FX_FILESIZE m_nCurPos; |
| 196 | }; |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 197 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 198 | FPDF_FileHandlerContext::FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | m_pFS = pFS; |
| 200 | m_nCurPos = 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 203 | FPDF_FileHandlerContext::~FPDF_FileHandlerContext() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 204 | if (m_pFS && m_pFS->Release) |
| 205 | m_pFS->Release(m_pFS->clientData); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 208 | FX_FILESIZE FPDF_FileHandlerContext::GetSize() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | if (m_pFS && m_pFS->GetSize) |
| 210 | return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData); |
| 211 | return 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 214 | bool FPDF_FileHandlerContext::IsEOF() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 215 | return m_nCurPos >= GetSize(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 218 | FX_FILESIZE FPDF_FileHandlerContext::GetPosition() { |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 219 | return m_nCurPos; |
| 220 | } |
| 221 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 222 | bool FPDF_FileHandlerContext::ReadBlock(void* buffer, |
| 223 | FX_FILESIZE offset, |
| 224 | size_t size) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 225 | if (!buffer || !size || !m_pFS->ReadBlock) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 226 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 227 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 228 | if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, |
| 229 | (FPDF_DWORD)size) == 0) { |
| 230 | m_nCurPos = offset + size; |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 231 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 232 | } |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 233 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 236 | size_t FPDF_FileHandlerContext::ReadBlock(void* buffer, size_t size) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 237 | if (!buffer || !size || !m_pFS->ReadBlock) |
| 238 | return 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 239 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | FX_FILESIZE nSize = GetSize(); |
| 241 | if (m_nCurPos >= nSize) |
| 242 | return 0; |
| 243 | FX_FILESIZE dwAvail = nSize - m_nCurPos; |
| 244 | if (dwAvail < (FX_FILESIZE)size) |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 245 | size = static_cast<size_t>(dwAvail); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 246 | if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer, |
| 247 | (FPDF_DWORD)size) == 0) { |
| 248 | m_nCurPos += size; |
| 249 | return size; |
| 250 | } |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 251 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 252 | return 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 255 | bool FPDF_FileHandlerContext::WriteBlock(const void* buffer, |
| 256 | FX_FILESIZE offset, |
| 257 | size_t size) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | if (!m_pFS || !m_pFS->WriteBlock) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 259 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 260 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, |
| 262 | (FPDF_DWORD)size) == 0) { |
| 263 | m_nCurPos = offset + size; |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 264 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | } |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 266 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 269 | bool FPDF_FileHandlerContext::Flush() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 270 | if (!m_pFS || !m_pFS->Flush) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 271 | return true; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 272 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 273 | return m_pFS->Flush(m_pFS->clientData) == 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 274 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 275 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 276 | |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 277 | FPDF_DOCUMENT LoadDocumentImpl( |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 278 | const RetainPtr<IFX_SeekableReadStream>& pFileAccess, |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 279 | FPDF_BYTESTRING password) { |
Jane Liu | 8a1081f | 2017-08-22 16:28:05 -0400 | [diff] [blame] | 280 | if (!pFileAccess) { |
| 281 | ProcessParseError(CPDF_Parser::FILE_ERROR); |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 282 | return nullptr; |
Jane Liu | 8a1081f | 2017-08-22 16:28:05 -0400 | [diff] [blame] | 283 | } |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 284 | |
| 285 | auto pParser = pdfium::MakeUnique<CPDF_Parser>(); |
| 286 | pParser->SetPassword(password); |
| 287 | |
| 288 | auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser)); |
| 289 | CPDF_Parser::Error error = |
| 290 | pDocument->GetParser()->StartParse(pFileAccess, pDocument.get()); |
| 291 | if (error != CPDF_Parser::SUCCESS) { |
| 292 | ProcessParseError(error); |
| 293 | return nullptr; |
| 294 | } |
| 295 | CheckUnSupportError(pDocument.get(), error); |
| 296 | return FPDFDocumentFromCPDFDocument(pDocument.release()); |
| 297 | } |
| 298 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 299 | } // namespace |
| 300 | |
| 301 | UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) { |
| 302 | return static_cast<UnderlyingDocumentType*>(doc); |
| 303 | } |
| 304 | |
| 305 | FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) { |
| 306 | return static_cast<FPDF_DOCUMENT>(doc); |
| 307 | } |
| 308 | |
| 309 | UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) { |
| 310 | return static_cast<UnderlyingPageType*>(page); |
| 311 | } |
| 312 | |
| 313 | CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) { |
| 314 | #ifdef PDF_ENABLE_XFA |
| 315 | return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr; |
| 316 | #else // PDF_ENABLE_XFA |
| 317 | return UnderlyingFromFPDFDocument(doc); |
| 318 | #endif // PDF_ENABLE_XFA |
| 319 | } |
| 320 | |
| 321 | FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) { |
| 322 | #ifdef PDF_ENABLE_XFA |
| 323 | return doc ? FPDFDocumentFromUnderlying( |
| 324 | new CPDFXFA_Context(pdfium::WrapUnique(doc))) |
| 325 | : nullptr; |
| 326 | #else // PDF_ENABLE_XFA |
| 327 | return FPDFDocumentFromUnderlying(doc); |
| 328 | #endif // PDF_ENABLE_XFA |
| 329 | } |
| 330 | |
| 331 | CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) { |
| 332 | #ifdef PDF_ENABLE_XFA |
| 333 | return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr; |
| 334 | #else // PDF_ENABLE_XFA |
| 335 | return UnderlyingFromFPDFPage(page); |
| 336 | #endif // PDF_ENABLE_XFA |
| 337 | } |
| 338 | |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 339 | CPDF_PathObject* CPDFPathObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) { |
Lei Zhang | 038740c | 2017-09-13 13:22:54 -0700 | [diff] [blame] | 340 | auto* obj = CPDFPageObjectFromFPDFPageObject(page_object); |
| 341 | return obj ? obj->AsPath() : nullptr; |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 344 | CPDF_PageObject* CPDFPageObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) { |
| 345 | return static_cast<CPDF_PageObject*>(page_object); |
| 346 | } |
| 347 | |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 348 | CPDF_Object* CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment) { |
| 349 | return static_cast<CPDF_Object*>(attachment); |
| 350 | } |
| 351 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 352 | ByteString CFXByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string) { |
| 353 | return WideString::FromUTF16LE(wide_string, |
| 354 | WideString::WStringLength(wide_string)) |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 355 | .UTF8Encode(); |
| 356 | } |
| 357 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 358 | CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) { |
| 359 | return static_cast<CFX_DIBitmap*>(bitmap); |
| 360 | } |
| 361 | |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 362 | const FX_PATHPOINT* FXPathPointFromFPDFPathSegment(FPDF_PATHSEGMENT segment) { |
| 363 | return static_cast<const FX_PATHPOINT*>(segment); |
| 364 | } |
| 365 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 366 | unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text, |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 367 | void* buffer, |
| 368 | unsigned long buflen) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 369 | ByteString encoded_text = text.UTF16LE_Encode(); |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 370 | unsigned long len = encoded_text.GetLength(); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 371 | if (buffer && len <= buflen) |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 372 | memcpy(buffer, encoded_text.c_str(), len); |
| 373 | return len; |
| 374 | } |
| 375 | |
| 376 | unsigned long DecodeStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream, |
| 377 | void* buffer, |
| 378 | unsigned long buflen) { |
| 379 | ASSERT(stream); |
| 380 | uint8_t* data = stream->GetRawData(); |
| 381 | uint32_t len = stream->GetRawSize(); |
| 382 | CPDF_Dictionary* dict = stream->GetDict(); |
| 383 | CPDF_Object* decoder = dict ? dict->GetDirectObjectFor("Filter") : nullptr; |
| 384 | if (decoder && (decoder->IsArray() || decoder->IsName())) { |
| 385 | // Decode the stream if one or more stream filters are specified. |
| 386 | uint8_t* decoded_data = nullptr; |
| 387 | uint32_t decoded_len = 0; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 388 | ByteString dummy_last_decoder; |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 389 | CPDF_Dictionary* dummy_last_param; |
| 390 | if (PDF_DataDecode(data, len, dict, dict->GetIntegerFor("DL"), false, |
| 391 | &decoded_data, &decoded_len, &dummy_last_decoder, |
| 392 | &dummy_last_param)) { |
| 393 | if (buffer && buflen >= decoded_len) |
| 394 | memcpy(buffer, decoded_data, decoded_len); |
| 395 | |
| 396 | // Free the buffer for the decoded data if it was allocated by |
| 397 | // PDF_DataDecode(). Note that for images with a single image-specific |
| 398 | // filter, |decoded_data| is directly assigned to be |data|, so |
| 399 | // |decoded_data| does not need to be freed. |
| 400 | if (decoded_data != data) |
| 401 | FX_Free(decoded_data); |
| 402 | |
| 403 | return decoded_len; |
| 404 | } |
| 405 | } |
| 406 | // Copy the raw data and return its length if there is no valid filter |
| 407 | // specified or if decoding failed. |
| 408 | if (buffer && buflen >= len) |
| 409 | memcpy(buffer, data, len); |
| 410 | |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 411 | return len; |
| 412 | } |
| 413 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 414 | RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream( |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 415 | FPDF_FILEACCESS* pFileAccess) { |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 416 | return pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 417 | } |
| 418 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 419 | #ifdef PDF_ENABLE_XFA |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 420 | RetainPtr<IFX_SeekableStream> MakeSeekableStream( |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 421 | FPDF_FILEHANDLER* pFilehandler) { |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 422 | return pdfium::MakeRetain<FPDF_FileHandlerContext>(pFilehandler); |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 423 | } |
| 424 | #endif // PDF_ENABLE_XFA |
| 425 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 426 | // 0 bit: FPDF_POLICY_MACHINETIME_ACCESS |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 427 | static uint32_t foxit_sandbox_policy = 0xFFFFFFFF; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 428 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 429 | void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) { |
| 430 | switch (policy) { |
| 431 | case FPDF_POLICY_MACHINETIME_ACCESS: { |
| 432 | if (enable) |
| 433 | foxit_sandbox_policy |= 0x01; |
| 434 | else |
| 435 | foxit_sandbox_policy &= 0xFFFFFFFE; |
| 436 | } break; |
| 437 | default: |
| 438 | break; |
| 439 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 442 | FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) { |
| 443 | switch (policy) { |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 444 | case FPDF_POLICY_MACHINETIME_ACCESS: |
Lei Zhang | b0748bb | 2015-10-19 12:11:49 -0700 | [diff] [blame] | 445 | return !!(foxit_sandbox_policy & 0x01); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 446 | default: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 447 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 448 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 451 | FPDF_EXPORT void FPDF_CALLCONV FPDF_InitLibrary() { |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 452 | FPDF_InitLibraryWithConfig(nullptr); |
| 453 | } |
| 454 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 455 | FPDF_EXPORT void FPDF_CALLCONV |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 456 | FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) { |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 457 | if (g_bLibraryInitialized) |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 458 | return; |
| 459 | |
Dan Sinclair | dbc3d3e | 2017-05-11 13:41:38 -0400 | [diff] [blame] | 460 | FXMEM_InitializePartitionAlloc(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 461 | |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 462 | CFX_GEModule* pModule = CFX_GEModule::Get(); |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 463 | pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr); |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 464 | |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 465 | CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get(); |
Lei Zhang | 76020fc | 2017-05-18 15:51:20 -0700 | [diff] [blame] | 466 | pModuleMgr->Init(); |
Tom Sepez | b4a2618 | 2017-03-01 12:15:00 -0800 | [diff] [blame] | 467 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 468 | #ifdef PDF_ENABLE_XFA |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 469 | FXJSE_Initialize(); |
| 470 | BC_Library_Init(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 471 | #endif // PDF_ENABLE_XFA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 472 | if (cfg && cfg->version >= 2) |
| 473 | IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate); |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 474 | |
| 475 | g_bLibraryInitialized = true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 478 | FPDF_EXPORT void FPDF_CALLCONV FPDF_DestroyLibrary() { |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 479 | if (!g_bLibraryInitialized) |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 480 | return; |
| 481 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 482 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 5628fd7 | 2017-04-27 14:58:53 -0700 | [diff] [blame] | 483 | BC_Library_Destroy(); |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 484 | FXJSE_Finalize(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 485 | #endif // PDF_ENABLE_XFA |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 486 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 487 | CPDF_ModuleMgr::Destroy(); |
| 488 | CFX_GEModule::Destroy(); |
Tom Sepez | 2c28619 | 2015-06-18 12:47:11 -0700 | [diff] [blame] | 489 | |
thestig | 2d6dda1 | 2016-06-28 07:39:09 -0700 | [diff] [blame] | 490 | IJS_Runtime::Destroy(); |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 491 | |
| 492 | g_bLibraryInitialized = false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | #ifndef _WIN32 |
| 496 | int g_LastError; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 497 | void SetLastError(int err) { |
| 498 | g_LastError = err; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 501 | int GetLastError() { |
| 502 | return g_LastError; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 503 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 504 | #endif // _WIN32 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 505 | |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 506 | void ProcessParseError(CPDF_Parser::Error err) { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 507 | uint32_t err_code = FPDF_ERR_SUCCESS; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 508 | // Translate FPDFAPI error code to FPDFVIEW error code |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 509 | switch (err) { |
| 510 | case CPDF_Parser::SUCCESS: |
| 511 | err_code = FPDF_ERR_SUCCESS; |
| 512 | break; |
| 513 | case CPDF_Parser::FILE_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 514 | err_code = FPDF_ERR_FILE; |
| 515 | break; |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 516 | case CPDF_Parser::FORMAT_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 517 | err_code = FPDF_ERR_FORMAT; |
| 518 | break; |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 519 | case CPDF_Parser::PASSWORD_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 520 | err_code = FPDF_ERR_PASSWORD; |
| 521 | break; |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 522 | case CPDF_Parser::HANDLER_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 523 | err_code = FPDF_ERR_SECURITY; |
| 524 | break; |
| 525 | } |
| 526 | SetLastError(err_code); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 529 | FPDF_EXPORT void FPDF_CALLCONV FPDF_SetSandBoxPolicy(FPDF_DWORD policy, |
| 530 | FPDF_BOOL enable) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 531 | return FSDK_SetSandBoxPolicy(policy, enable); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 532 | } |
| 533 | |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 534 | #if defined(_WIN32) |
| 535 | #if defined(PDFIUM_PRINT_TEXT_WITH_GDI) |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 536 | FPDF_EXPORT void FPDF_CALLCONV |
thestig | fdb35ff | 2016-07-18 13:45:44 -0700 | [diff] [blame] | 537 | FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) { |
| 538 | g_pdfium_typeface_accessible_func = func; |
| 539 | } |
| 540 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 541 | FPDF_EXPORT void FPDF_CALLCONV FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) { |
thestig | fdb35ff | 2016-07-18 13:45:44 -0700 | [diff] [blame] | 542 | g_pdfium_print_text_with_gdi = !!use_gdi; |
| 543 | } |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 544 | #endif // PDFIUM_PRINT_TEXT_WITH_GDI |
| 545 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 546 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 547 | FPDF_SetPrintPostscriptLevel(int postscript_level) { |
rbpotter | 38a01b9 | 2017-07-13 11:58:01 -0700 | [diff] [blame] | 548 | return postscript_level != 1 && FPDF_SetPrintMode(postscript_level); |
| 549 | } |
| 550 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 551 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_SetPrintMode(int mode) { |
rbpotter | e8468c4 | 2017-07-11 10:04:29 -0700 | [diff] [blame] | 552 | if (mode < FPDF_PRINTMODE_EMF || mode > FPDF_PRINTMODE_POSTSCRIPT3) |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 553 | return FALSE; |
rbpotter | e8468c4 | 2017-07-11 10:04:29 -0700 | [diff] [blame] | 554 | g_pdfium_print_mode = mode; |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 555 | return TRUE; |
| 556 | } |
| 557 | #endif // defined(_WIN32) |
thestig | fdb35ff | 2016-07-18 13:45:44 -0700 | [diff] [blame] | 558 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 559 | FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV |
| 560 | FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BYTESTRING password) { |
Tom Sepez | e3166a8 | 2015-08-05 10:50:32 -0700 | [diff] [blame] | 561 | // NOTE: the creation of the file needs to be by the embedder on the |
| 562 | // other side of this API. |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 563 | return LoadDocumentImpl( |
| 564 | IFX_SeekableReadStream::CreateFromFilename((const char*)file_path), |
| 565 | password); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 566 | } |
Jun Fang | e118ce9 | 2015-02-17 06:50:08 -0800 | [diff] [blame] | 567 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 568 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_HasXFAField(FPDF_DOCUMENT document, |
| 569 | int* docType) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 570 | if (!document) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 571 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 572 | |
Ryan Harrison | a74a627 | 2017-10-16 10:57:48 -0400 | [diff] [blame^] | 573 | const CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 574 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 575 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 576 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 577 | const CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 578 | if (!pRoot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 579 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 580 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 581 | CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 582 | if (!pAcroForm) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 583 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 584 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 585 | CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 586 | if (!pXFA) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 587 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 588 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 589 | bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false); |
thestig | ded3634 | 2016-05-23 17:54:02 -0700 | [diff] [blame] | 590 | *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 591 | return true; |
Jun Fang | e118ce9 | 2015-02-17 06:50:08 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Ryan Harrison | a74a627 | 2017-10-16 10:57:48 -0400 | [diff] [blame^] | 594 | #ifdef PDF_ENABLE_XFA |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 595 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_LoadXFA(FPDF_DOCUMENT document) { |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 596 | return document && static_cast<CPDFXFA_Context*>(document)->LoadXFADoc(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 597 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 598 | #endif // PDF_ENABLE_XFA |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 599 | |
tsepez | ad2441e | 2016-10-24 10:19:11 -0700 | [diff] [blame] | 600 | class CMemFile final : public IFX_SeekableReadStream { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 601 | public: |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 602 | static RetainPtr<CMemFile> Create(uint8_t* pBuf, FX_FILESIZE size) { |
| 603 | return RetainPtr<CMemFile>(new CMemFile(pBuf, size)); |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 604 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 605 | |
Lei Zhang | 3884dba | 2015-10-19 17:27:53 -0700 | [diff] [blame] | 606 | FX_FILESIZE GetSize() override { return m_size; } |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 607 | bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 608 | if (offset < 0) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 609 | return false; |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 610 | |
tsepez | 4e597c8 | 2016-11-07 15:16:01 -0800 | [diff] [blame] | 611 | FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 612 | newPos += offset; |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 613 | if (!newPos.IsValid() || newPos.ValueOrDie() > m_size) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 614 | return false; |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 615 | |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 616 | memcpy(buffer, m_pBuf + offset, size); |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 617 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 618 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 619 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 620 | private: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 621 | CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {} |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 622 | |
Lei Zhang | 3884dba | 2015-10-19 17:27:53 -0700 | [diff] [blame] | 623 | uint8_t* const m_pBuf; |
| 624 | const FX_FILESIZE m_size; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 625 | }; |
Lei Zhang | 3884dba | 2015-10-19 17:27:53 -0700 | [diff] [blame] | 626 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 627 | FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV |
| 628 | FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password) { |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 629 | return LoadDocumentImpl(CMemFile::Create((uint8_t*)data_buf, size), password); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 632 | FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 633 | FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess, |
| 634 | FPDF_BYTESTRING password) { |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 635 | return LoadDocumentImpl(pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess), |
| 636 | password); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 639 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_GetFileVersion(FPDF_DOCUMENT doc, |
| 640 | int* fileVersion) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 641 | if (!fileVersion) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 642 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 643 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 644 | *fileVersion = 0; |
| 645 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc); |
| 646 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 647 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 648 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 649 | const CPDF_Parser* pParser = pDoc->GetParser(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 650 | if (!pParser) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 651 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 652 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 653 | *fileVersion = pParser->GetFileVersion(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 654 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 655 | } |
| 656 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 657 | // jabdelmalek: changed return type from uint32_t to build on Linux (and match |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 658 | // header). |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 659 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 660 | FPDF_GetDocPermissions(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 661 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 662 | // https://bugs.chromium.org/p/pdfium/issues/detail?id=499 |
| 663 | if (!pDoc) { |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 664 | #ifndef PDF_ENABLE_XFA |
| 665 | return 0; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 666 | #else // PDF_ENABLE_XFA |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 667 | return 0xFFFFFFFF; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 668 | #endif // PDF_ENABLE_XFA |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 669 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 670 | |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 671 | return pDoc->GetUserPermissions(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 674 | FPDF_EXPORT int FPDF_CALLCONV |
| 675 | FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 676 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 677 | if (!pDoc || !pDoc->GetParser()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 678 | return -1; |
Bo Xu | c5cab02 | 2014-09-19 19:16:31 -0700 | [diff] [blame] | 679 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 680 | CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 681 | return pDict ? pDict->GetIntegerFor("R") : -1; |
Bo Xu | c5cab02 | 2014-09-19 19:16:31 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 684 | FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageCount(FPDF_DOCUMENT document) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 685 | UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document); |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 686 | return pDoc ? pDoc->GetPageCount() : 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 689 | FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDF_LoadPage(FPDF_DOCUMENT document, |
| 690 | int page_index) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 691 | UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document); |
| 692 | if (!pDoc) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 693 | return nullptr; |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 694 | |
Tom Sepez | bbe0e4d | 2015-10-20 15:41:40 -0700 | [diff] [blame] | 695 | if (page_index < 0 || page_index >= pDoc->GetPageCount()) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 696 | return nullptr; |
| 697 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 698 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 9792f16 | 2017-05-16 14:11:30 -0700 | [diff] [blame] | 699 | return pDoc->GetXFAPage(page_index).Leak(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 700 | #else // PDF_ENABLE_XFA |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 701 | CPDF_Dictionary* pDict = pDoc->GetPage(page_index); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 702 | if (!pDict) |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 703 | return nullptr; |
| 704 | |
| 705 | CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true); |
| 706 | pPage->ParseContent(); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 707 | return pPage; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 708 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 711 | FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageWidth(FPDF_PAGE page) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 712 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 713 | return pPage ? pPage->GetPageWidth() : 0.0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 716 | FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageHeight(FPDF_PAGE page) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 717 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 718 | return pPage ? pPage->GetPageHeight() : 0.0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 719 | } |
| 720 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 721 | #if defined(_WIN32) |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 722 | namespace { |
| 723 | |
| 724 | const double kEpsilonSize = 0.01f; |
| 725 | |
| 726 | void GetScaling(CPDF_Page* pPage, |
| 727 | int size_x, |
| 728 | int size_y, |
| 729 | int rotate, |
| 730 | double* scale_x, |
| 731 | double* scale_y) { |
| 732 | ASSERT(pPage); |
| 733 | ASSERT(scale_x); |
| 734 | ASSERT(scale_y); |
| 735 | double page_width = pPage->GetPageWidth(); |
| 736 | double page_height = pPage->GetPageHeight(); |
| 737 | if (page_width < kEpsilonSize || page_height < kEpsilonSize) |
| 738 | return; |
| 739 | |
| 740 | if (rotate % 2 == 0) { |
| 741 | *scale_x = size_x / page_width; |
| 742 | *scale_y = size_y / page_height; |
| 743 | } else { |
| 744 | *scale_x = size_y / page_width; |
| 745 | *scale_y = size_x / page_height; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | FX_RECT GetMaskDimensionsAndOffsets(CPDF_Page* pPage, |
| 750 | int start_x, |
| 751 | int start_y, |
| 752 | int size_x, |
| 753 | int size_y, |
| 754 | int rotate, |
| 755 | const CFX_FloatRect& mask_box) { |
| 756 | double scale_x = 0.0f; |
| 757 | double scale_y = 0.0f; |
| 758 | GetScaling(pPage, size_x, size_y, rotate, &scale_x, &scale_y); |
| 759 | if (scale_x < kEpsilonSize || scale_y < kEpsilonSize) |
| 760 | return FX_RECT(); |
| 761 | |
| 762 | // Compute sizes in page points. Round down to catch the entire bitmap. |
| 763 | int start_x_bm = static_cast<int>(mask_box.left * scale_x); |
| 764 | int start_y_bm = static_cast<int>(mask_box.bottom * scale_y); |
| 765 | int size_x_bm = static_cast<int>(mask_box.right * scale_x + 1.0f) - |
| 766 | static_cast<int>(mask_box.left * scale_x); |
| 767 | int size_y_bm = static_cast<int>(mask_box.top * scale_y + 1.0f) - |
| 768 | static_cast<int>(mask_box.bottom * scale_y); |
| 769 | |
| 770 | // Get page rotation |
rbpotter | ce8e51e | 2017-04-28 12:42:47 -0700 | [diff] [blame] | 771 | int page_rotation = pPage->GetPageRotation(); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 772 | |
| 773 | // Compute offsets |
| 774 | int offset_x = 0; |
| 775 | int offset_y = 0; |
rbpotter | 8d7672e | 2017-08-16 16:45:44 -0700 | [diff] [blame] | 776 | if (size_x > size_y) |
| 777 | std::swap(size_x_bm, size_y_bm); |
| 778 | |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 779 | switch ((rotate + page_rotation) % 4) { |
| 780 | case 0: |
| 781 | offset_x = start_x_bm + start_x; |
| 782 | offset_y = start_y + size_y - size_y_bm - start_y_bm; |
| 783 | break; |
| 784 | case 1: |
| 785 | offset_x = start_y_bm + start_x; |
| 786 | offset_y = start_x_bm + start_y; |
| 787 | break; |
| 788 | case 2: |
| 789 | offset_x = start_x + size_x - size_x_bm - start_x_bm; |
| 790 | offset_y = start_y_bm + start_y; |
| 791 | break; |
| 792 | case 3: |
| 793 | offset_x = start_x + size_x - size_x_bm - start_y_bm; |
| 794 | offset_y = start_y + size_y - size_y_bm - start_x_bm; |
| 795 | break; |
| 796 | } |
| 797 | return FX_RECT(offset_x, offset_y, offset_x + size_x_bm, |
| 798 | offset_y + size_y_bm); |
| 799 | } |
| 800 | |
| 801 | // Get a bitmap of just the mask section defined by |mask_box| from a full page |
| 802 | // bitmap |pBitmap|. |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 803 | RetainPtr<CFX_DIBitmap> GetMaskBitmap(CPDF_Page* pPage, |
| 804 | int start_x, |
| 805 | int start_y, |
| 806 | int size_x, |
| 807 | int size_y, |
| 808 | int rotate, |
| 809 | RetainPtr<CFX_DIBitmap>& pSrc, |
| 810 | const CFX_FloatRect& mask_box, |
| 811 | FX_RECT* bitmap_area) { |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 812 | ASSERT(bitmap_area); |
| 813 | *bitmap_area = GetMaskDimensionsAndOffsets(pPage, start_x, start_y, size_x, |
| 814 | size_y, rotate, mask_box); |
| 815 | if (bitmap_area->IsEmpty()) |
| 816 | return nullptr; |
| 817 | |
| 818 | // Create a new bitmap to transfer part of the page bitmap to. |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 819 | RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>(); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 820 | pDst->Create(bitmap_area->Width(), bitmap_area->Height(), FXDIB_Argb); |
| 821 | pDst->Clear(0x00ffffff); |
| 822 | pDst->TransferBitmap(0, 0, bitmap_area->Width(), bitmap_area->Height(), pSrc, |
| 823 | bitmap_area->left, bitmap_area->top); |
| 824 | return pDst; |
| 825 | } |
| 826 | |
| 827 | void RenderBitmap(CFX_RenderDevice* device, |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 828 | const RetainPtr<CFX_DIBitmap>& pSrc, |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 829 | const FX_RECT& mask_area) { |
| 830 | int size_x_bm = mask_area.Width(); |
| 831 | int size_y_bm = mask_area.Height(); |
| 832 | if (size_x_bm == 0 || size_y_bm == 0) |
| 833 | return; |
| 834 | |
| 835 | // Create a new bitmap from the old one |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 836 | RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>(); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 837 | pDst->Create(size_x_bm, size_y_bm, FXDIB_Rgb32); |
| 838 | pDst->Clear(0xffffffff); |
| 839 | pDst->CompositeBitmap(0, 0, size_x_bm, size_y_bm, pSrc, 0, 0, |
| 840 | FXDIB_BLEND_NORMAL, nullptr, false); |
| 841 | |
| 842 | if (device->GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) { |
rbpotter | 8d7672e | 2017-08-16 16:45:44 -0700 | [diff] [blame] | 843 | device->StretchDIBits(pDst, mask_area.left, mask_area.top, size_x_bm, |
| 844 | size_y_bm); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 845 | } else { |
| 846 | device->SetDIBits(pDst, mask_area.left, mask_area.top); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | } // namespace |
| 851 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 852 | FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage(HDC dc, |
| 853 | FPDF_PAGE page, |
| 854 | int start_x, |
| 855 | int start_y, |
| 856 | int size_x, |
| 857 | int size_y, |
| 858 | int rotate, |
| 859 | int flags) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 860 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 861 | if (!pPage) |
| 862 | return; |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 863 | pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>()); |
| 864 | CPDF_PageRenderContext* pContext = pPage->GetRenderContext(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 865 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 866 | RetainPtr<CFX_DIBitmap> pBitmap; |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 867 | // Don't render the full page to bitmap for a mask unless there are a lot |
| 868 | // of masks. Full page bitmaps result in large spool sizes, so they should |
| 869 | // only be used when necessary. For large numbers of masks, rendering each |
| 870 | // individually is inefficient and unlikely to significantly improve spool |
rbpotter | 8d7672e | 2017-08-16 16:45:44 -0700 | [diff] [blame] | 871 | // size. |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 872 | const bool bNewBitmap = |
rbpotter | 8d7672e | 2017-08-16 16:45:44 -0700 | [diff] [blame] | 873 | pPage->BackgroundAlphaNeeded() || |
| 874 | (pPage->HasImageMask() && pPage->GetMaskBoundingBoxes().size() > 100); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 875 | const bool bHasMask = pPage->HasImageMask() && !bNewBitmap; |
| 876 | if (bNewBitmap || bHasMask) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 877 | pBitmap = pdfium::MakeRetain<CFX_DIBitmap>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 878 | pBitmap->Create(size_x, size_y, FXDIB_Argb); |
| 879 | pBitmap->Clear(0x00ffffff); |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 880 | CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice; |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 881 | pContext->m_pDevice = pdfium::WrapUnique(pDevice); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 882 | pDevice->Attach(pBitmap, false, nullptr, false); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 883 | if (bHasMask) { |
| 884 | pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>(); |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 885 | uint32_t option_flags = pContext->m_pOptions->GetFlags(); |
| 886 | option_flags |= RENDER_BREAKFORMASKS; |
| 887 | pContext->m_pOptions->SetFlags(option_flags); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 888 | } |
Jun Fang | 1aeeceb | 2015-12-29 10:27:44 +0800 | [diff] [blame] | 889 | } else { |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 890 | pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc); |
Jun Fang | 1aeeceb | 2015-12-29 10:27:44 +0800 | [diff] [blame] | 891 | } |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 892 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 893 | FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 894 | rotate, flags, true, nullptr); |
Bo Xu | d4e406e | 2014-08-13 11:03:19 -0700 | [diff] [blame] | 895 | |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 896 | if (bHasMask) { |
| 897 | // Finish rendering the page to bitmap and copy the correct segments |
| 898 | // of the page to individual image mask bitmaps. |
| 899 | const std::vector<CFX_FloatRect>& mask_boxes = |
| 900 | pPage->GetMaskBoundingBoxes(); |
| 901 | std::vector<FX_RECT> bitmap_areas(mask_boxes.size()); |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 902 | std::vector<RetainPtr<CFX_DIBitmap>> bitmaps(mask_boxes.size()); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 903 | for (size_t i = 0; i < mask_boxes.size(); i++) { |
| 904 | bitmaps[i] = |
| 905 | GetMaskBitmap(pPage, start_x, start_y, size_x, size_y, rotate, |
| 906 | pBitmap, mask_boxes[i], &bitmap_areas[i]); |
| 907 | pContext->m_pRenderer->Continue(nullptr); |
| 908 | } |
| 909 | |
| 910 | // Reset rendering context |
| 911 | pPage->SetRenderContext(nullptr); |
| 912 | |
| 913 | // Begin rendering to the printer. Add flag to indicate the renderer should |
| 914 | // pause after each image mask. |
| 915 | pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>()); |
| 916 | pContext = pPage->GetRenderContext(); |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 917 | pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 918 | pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>(); |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 919 | |
| 920 | uint32_t option_flags = pContext->m_pOptions->GetFlags(); |
| 921 | option_flags |= RENDER_BREAKFORMASKS; |
| 922 | pContext->m_pOptions->SetFlags(option_flags); |
| 923 | |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 924 | FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y, |
| 925 | rotate, flags, true, nullptr); |
| 926 | |
| 927 | // Render masks |
| 928 | for (size_t i = 0; i < mask_boxes.size(); i++) { |
| 929 | // Render the bitmap for the mask and free the bitmap. |
| 930 | if (bitmaps[i]) { // will be null if mask has zero area |
| 931 | RenderBitmap(pContext->m_pDevice.get(), bitmaps[i], bitmap_areas[i]); |
| 932 | } |
| 933 | // Render the next portion of page. |
| 934 | pContext->m_pRenderer->Continue(nullptr); |
| 935 | } |
| 936 | } else if (bNewBitmap) { |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 937 | CFX_WindowsRenderDevice WinDC(dc); |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 938 | if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 939 | auto pDst = pdfium::MakeRetain<CFX_DIBitmap>(); |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 940 | int pitch = pBitmap->GetPitch(); |
| 941 | pDst->Create(size_x, size_y, FXDIB_Rgb32); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 942 | memset(pDst->GetBuffer(), -1, pitch * size_y); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 943 | pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0, |
Nicolas Pena | 6a5c20c | 2017-04-07 14:12:31 -0400 | [diff] [blame] | 944 | FXDIB_BLEND_NORMAL, nullptr, false); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 945 | WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y); |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 946 | } else { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 947 | WinDC.SetDIBits(pBitmap, 0, 0); |
Lei Zhang | 6d8b1c2 | 2015-06-19 17:26:17 -0700 | [diff] [blame] | 948 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 949 | } |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 950 | |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 951 | pPage->SetRenderContext(nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 952 | } |
Lei Zhang | ae85f87 | 2016-02-19 14:57:07 -0800 | [diff] [blame] | 953 | #endif // defined(_WIN32) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 954 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 955 | FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPageBitmap(FPDF_BITMAP bitmap, |
| 956 | FPDF_PAGE page, |
| 957 | int start_x, |
| 958 | int start_y, |
| 959 | int size_x, |
| 960 | int size_y, |
| 961 | int rotate, |
| 962 | int flags) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 963 | if (!bitmap) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 964 | return; |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 965 | |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 966 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 967 | if (!pPage) |
| 968 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 969 | |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 970 | CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext; |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 971 | pPage->SetRenderContext(pdfium::WrapUnique(pContext)); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 972 | |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 973 | CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice; |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 974 | pContext->m_pDevice.reset(pDevice); |
dsinclair | 31b08d4 | 2017-03-28 15:47:47 +0000 | [diff] [blame] | 975 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 976 | RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap)); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 977 | pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 978 | FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 979 | rotate, flags, true, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 980 | |
caryclark | 687fbde | 2016-11-22 12:44:25 -0800 | [diff] [blame] | 981 | #ifdef _SKIA_SUPPORT_PATHS_ |
Cary Clark | 364d18b | 2017-07-25 10:39:10 -0400 | [diff] [blame] | 982 | pDevice->Flush(true); |
caryclark | 687fbde | 2016-11-22 12:44:25 -0800 | [diff] [blame] | 983 | pBitmap->UnPreMultiply(); |
| 984 | #endif |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 985 | pPage->SetRenderContext(nullptr); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 988 | FPDF_EXPORT void FPDF_CALLCONV |
| 989 | FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap, |
| 990 | FPDF_PAGE page, |
| 991 | const FS_MATRIX* matrix, |
| 992 | const FS_RECTF* clipping, |
| 993 | int flags) { |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 994 | if (!bitmap) |
| 995 | return; |
| 996 | |
| 997 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 998 | if (!pPage) |
| 999 | return; |
| 1000 | |
| 1001 | CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext; |
| 1002 | pPage->SetRenderContext(pdfium::WrapUnique(pContext)); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1003 | |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 1004 | CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 1005 | pContext->m_pDevice.reset(pDevice); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1006 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 1007 | RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap)); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 1008 | pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false); |
| 1009 | |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 1010 | CFX_FloatRect clipping_rect; |
| 1011 | if (clipping) { |
| 1012 | clipping_rect.left = clipping->left; |
| 1013 | clipping_rect.bottom = clipping->bottom; |
| 1014 | clipping_rect.right = clipping->right; |
| 1015 | clipping_rect.top = clipping->top; |
| 1016 | } |
Nicolas Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 1017 | FX_RECT clip_rect = clipping_rect.ToFxRect(); |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 1018 | RenderPageImpl( |
| 1019 | pContext, pPage, |
| 1020 | pPage->GetDisplayMatrixWithTransformation( |
| 1021 | clip_rect.left, clip_rect.top, clip_rect.Width(), clip_rect.Height(), |
| 1022 | CFX_Matrix(matrix->a, matrix->b, matrix->c, matrix->d, matrix->e, |
| 1023 | matrix->f)), |
| 1024 | clip_rect, flags, true, nullptr); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 1025 | |
| 1026 | pPage->SetRenderContext(nullptr); |
| 1027 | } |
| 1028 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1029 | #ifdef _SKIA_SUPPORT_ |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1030 | FPDF_EXPORT FPDF_RECORDER FPDF_CALLCONV FPDF_RenderPageSkp(FPDF_PAGE page, |
| 1031 | int size_x, |
| 1032 | int size_y) { |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1033 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 1034 | if (!pPage) |
| 1035 | return nullptr; |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1036 | |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 1037 | CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext; |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 1038 | pPage->SetRenderContext(pdfium::WrapUnique(pContext)); |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 1039 | CFX_DefaultRenderDevice* skDevice = new CFX_DefaultRenderDevice; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1040 | FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y); |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 1041 | pContext->m_pDevice.reset(skDevice); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1042 | FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, true, |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1043 | nullptr); |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 1044 | pPage->SetRenderContext(nullptr); |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1045 | return recorder; |
| 1046 | } |
| 1047 | #endif |
| 1048 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1049 | FPDF_EXPORT void FPDF_CALLCONV FPDF_ClosePage(FPDF_PAGE page) { |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1050 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1051 | if (!page) |
| 1052 | return; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1053 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 9792f16 | 2017-05-16 14:11:30 -0700 | [diff] [blame] | 1054 | // Take it back across the API and throw it away. |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 1055 | RetainPtr<CPDFXFA_Page>().Unleak(pPage); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1056 | #else // PDF_ENABLE_XFA |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1057 | CPDFSDK_PageView* pPageView = |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1058 | static_cast<CPDFSDK_PageView*>(pPage->GetView()); |
dsinclair | 1df1efa | 2016-09-07 09:55:37 -0700 | [diff] [blame] | 1059 | if (pPageView) { |
dsinclair | bcf4623 | 2016-10-03 13:02:27 -0700 | [diff] [blame] | 1060 | // We're already destroying the pageview, so bail early. |
| 1061 | if (pPageView->IsBeingDestroyed()) |
| 1062 | return; |
| 1063 | |
dsinclair | 1df1efa | 2016-09-07 09:55:37 -0700 | [diff] [blame] | 1064 | if (pPageView->IsLocked()) { |
| 1065 | pPageView->TakePageOwnership(); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | bool owned = pPageView->OwnsPage(); |
| 1070 | // This will delete the |pPageView| object. We must cleanup the PageView |
| 1071 | // first because it will attempt to reset the View on the |pPage| during |
| 1072 | // destruction. |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 1073 | pPageView->GetFormFillEnv()->RemovePageView(pPage); |
dsinclair | 1df1efa | 2016-09-07 09:55:37 -0700 | [diff] [blame] | 1074 | // If the page was owned then the pageview will have deleted the page. |
| 1075 | if (owned) |
| 1076 | return; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1077 | } |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1078 | delete pPage; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1079 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1082 | FPDF_EXPORT void FPDF_CALLCONV FPDF_CloseDocument(FPDF_DOCUMENT document) { |
Jun Fang | fc75136 | 2015-12-16 21:23:39 -0800 | [diff] [blame] | 1083 | delete UnderlyingFromFPDFDocument(document); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1086 | FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetLastError() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1087 | return GetLastError(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1090 | FPDF_EXPORT void FPDF_CALLCONV FPDF_DeviceToPage(FPDF_PAGE page, |
| 1091 | int start_x, |
| 1092 | int start_y, |
| 1093 | int size_x, |
| 1094 | int size_y, |
| 1095 | int rotate, |
| 1096 | int device_x, |
| 1097 | int device_y, |
| 1098 | double* page_x, |
| 1099 | double* page_y) { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 1100 | if (!page || !page_x || !page_y) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1101 | return; |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 1102 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1103 | #ifdef PDF_ENABLE_XFA |
| 1104 | pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x, |
| 1105 | device_y, page_x, page_y); |
| 1106 | #else // PDF_ENABLE_XFA |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1107 | CFX_Matrix page2device = |
| 1108 | pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate); |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1109 | |
Nicolas Pena | b21f174 | 2017-06-29 12:02:06 -0400 | [diff] [blame] | 1110 | CFX_PointF pos = page2device.GetInverse().Transform( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 1111 | CFX_PointF(static_cast<float>(device_x), static_cast<float>(device_y))); |
Dan Sinclair | afb4456 | 2017-02-09 13:07:43 -0500 | [diff] [blame] | 1112 | |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 1113 | *page_x = pos.x; |
| 1114 | *page_y = pos.y; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1115 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1118 | FPDF_EXPORT void FPDF_CALLCONV FPDF_PageToDevice(FPDF_PAGE page, |
| 1119 | int start_x, |
| 1120 | int start_y, |
| 1121 | int size_x, |
| 1122 | int size_y, |
| 1123 | int rotate, |
| 1124 | double page_x, |
| 1125 | double page_y, |
| 1126 | int* device_x, |
| 1127 | int* device_y) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 1128 | if (!device_x || !device_y) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1129 | return; |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 1130 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 1131 | if (!pPage) |
| 1132 | return; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1133 | #ifdef PDF_ENABLE_XFA |
| 1134 | pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y, |
| 1135 | device_x, device_y); |
| 1136 | #else // PDF_ENABLE_XFA |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1137 | CFX_Matrix page2device = |
| 1138 | pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate); |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 1139 | CFX_PointF pos = page2device.Transform( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 1140 | CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y))); |
Dan Sinclair | afb4456 | 2017-02-09 13:07:43 -0500 | [diff] [blame] | 1141 | |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 1142 | *device_x = FXSYS_round(pos.x); |
| 1143 | *device_y = FXSYS_round(pos.y); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1144 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1147 | FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_Create(int width, |
| 1148 | int height, |
| 1149 | int alpha) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1150 | auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>(); |
tsepez | 37b12ad | 2016-12-14 19:50:23 -0800 | [diff] [blame] | 1151 | if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1152 | return nullptr; |
tsepez | 37b12ad | 2016-12-14 19:50:23 -0800 | [diff] [blame] | 1153 | |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1154 | return pBitmap.Leak(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1157 | FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_CreateEx(int width, |
| 1158 | int height, |
| 1159 | int format, |
| 1160 | void* first_scan, |
| 1161 | int stride) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1162 | FXDIB_Format fx_format; |
| 1163 | switch (format) { |
| 1164 | case FPDFBitmap_Gray: |
| 1165 | fx_format = FXDIB_8bppRgb; |
| 1166 | break; |
| 1167 | case FPDFBitmap_BGR: |
| 1168 | fx_format = FXDIB_Rgb; |
| 1169 | break; |
| 1170 | case FPDFBitmap_BGRx: |
| 1171 | fx_format = FXDIB_Rgb32; |
| 1172 | break; |
| 1173 | case FPDFBitmap_BGRA: |
| 1174 | fx_format = FXDIB_Argb; |
| 1175 | break; |
| 1176 | default: |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1177 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1178 | } |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1179 | auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1180 | pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1181 | return pBitmap.Leak(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1184 | FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetFormat(FPDF_BITMAP bitmap) { |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 1185 | if (!bitmap) |
| 1186 | return FPDFBitmap_Unknown; |
| 1187 | |
| 1188 | FXDIB_Format format = CFXBitmapFromFPDFBitmap(bitmap)->GetFormat(); |
| 1189 | switch (format) { |
| 1190 | case FXDIB_8bppRgb: |
| 1191 | case FXDIB_8bppMask: |
| 1192 | return FPDFBitmap_Gray; |
| 1193 | case FXDIB_Rgb: |
| 1194 | return FPDFBitmap_BGR; |
| 1195 | case FXDIB_Rgb32: |
| 1196 | return FPDFBitmap_BGRx; |
| 1197 | case FXDIB_Argb: |
| 1198 | return FPDFBitmap_BGRA; |
| 1199 | default: |
| 1200 | return FPDFBitmap_Unknown; |
| 1201 | } |
| 1202 | } |
| 1203 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1204 | FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_FillRect(FPDF_BITMAP bitmap, |
| 1205 | int left, |
| 1206 | int top, |
| 1207 | int width, |
| 1208 | int height, |
| 1209 | FPDF_DWORD color) { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 1210 | if (!bitmap) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1211 | return; |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1212 | |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 1213 | CFX_DefaultRenderDevice device; |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 1214 | RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap)); |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1215 | device.Attach(pBitmap, false, nullptr, false); |
| 1216 | if (!pBitmap->HasAlpha()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1217 | color |= 0xFF000000; |
| 1218 | FX_RECT rect(left, top, left + width, top + height); |
| 1219 | device.FillRect(&rect, color); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1222 | FPDF_EXPORT void* FPDF_CALLCONV FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1223 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1226 | FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1227 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1230 | FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1231 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1234 | FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetStride(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1235 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1238 | FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_Destroy(FPDF_BITMAP bitmap) { |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 1239 | RetainPtr<CFX_DIBitmap> destroyer; |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1240 | destroyer.Unleak(CFXBitmapFromFPDFBitmap(bitmap)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 1243 | void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1244 | FPDF_PAGE page, |
| 1245 | int start_x, |
| 1246 | int start_y, |
| 1247 | int size_x, |
| 1248 | int size_y, |
| 1249 | int rotate, |
| 1250 | int flags, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1251 | bool bNeedToRestore, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1252 | IFSDK_PAUSE_Adapter* pause) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 1253 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 1254 | if (!pPage) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1255 | return; |
| 1256 | |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1257 | RenderPageImpl(pContext, pPage, pPage->GetDisplayMatrix( |
| 1258 | start_x, start_y, size_x, size_y, rotate), |
| 1259 | FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y), |
| 1260 | flags, bNeedToRestore, pause); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1263 | FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document, |
| 1264 | int page_index, |
| 1265 | double* width, |
| 1266 | double* height) { |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1267 | UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document); |
| 1268 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1269 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1270 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1271 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1272 | int count = pDoc->GetPageCount(); |
| 1273 | if (page_index < 0 || page_index >= count) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1274 | return false; |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 1275 | RetainPtr<CPDFXFA_Page> pPage = pDoc->GetXFAPage(page_index); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1276 | if (!pPage) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1277 | return false; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1278 | *width = pPage->GetPageWidth(); |
| 1279 | *height = pPage->GetPageHeight(); |
| 1280 | #else // PDF_ENABLE_XFA |
| 1281 | CPDF_Dictionary* pDict = pDoc->GetPage(page_index); |
| 1282 | if (!pDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1283 | return false; |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 1284 | |
| 1285 | CPDF_Page page(pDoc, pDict, true); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1286 | *width = page.GetPageWidth(); |
| 1287 | *height = page.GetPageHeight(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1288 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1289 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1290 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1293 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1294 | FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1295 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1296 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1297 | return true; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1298 | CPDF_ViewerPreferences viewRef(pDoc); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1299 | return viewRef.PrintScaling(); |
| 1300 | } |
| 1301 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1302 | FPDF_EXPORT int FPDF_CALLCONV |
| 1303 | FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1304 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1305 | if (!pDoc) |
| 1306 | return 1; |
| 1307 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1308 | return viewRef.NumCopies(); |
| 1309 | } |
| 1310 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1311 | FPDF_EXPORT FPDF_PAGERANGE FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1312 | FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1313 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1314 | if (!pDoc) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1315 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1316 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1317 | return viewRef.PrintPageRange(); |
| 1318 | } |
| 1319 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1320 | FPDF_EXPORT FPDF_DUPLEXTYPE FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1321 | FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1322 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1323 | if (!pDoc) |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1324 | return DuplexUndefined; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1325 | CPDF_ViewerPreferences viewRef(pDoc); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1326 | ByteString duplex = viewRef.Duplex(); |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1327 | if ("Simplex" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1328 | return Simplex; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1329 | if ("DuplexFlipShortEdge" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1330 | return DuplexFlipShortEdge; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1331 | if ("DuplexFlipLongEdge" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1332 | return DuplexFlipLongEdge; |
| 1333 | return DuplexUndefined; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1336 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 1337 | FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document, |
| 1338 | FPDF_BYTESTRING key, |
| 1339 | char* buffer, |
| 1340 | unsigned long length) { |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 1341 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1342 | if (!pDoc) |
| 1343 | return 0; |
| 1344 | |
| 1345 | CPDF_ViewerPreferences viewRef(pDoc); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1346 | ByteString bsVal; |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 1347 | if (!viewRef.GenericName(key, &bsVal)) |
| 1348 | return 0; |
| 1349 | |
| 1350 | unsigned long dwStringLen = bsVal.GetLength() + 1; |
| 1351 | if (buffer && length >= dwStringLen) |
| 1352 | memcpy(buffer, bsVal.c_str(), dwStringLen); |
| 1353 | return dwStringLen; |
| 1354 | } |
| 1355 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1356 | FPDF_EXPORT FPDF_DWORD FPDF_CALLCONV |
| 1357 | FPDF_CountNamedDests(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1358 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1359 | if (!pDoc) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1360 | return 0; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1361 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 1362 | const CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1363 | if (!pRoot) |
| 1364 | return 0; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1365 | |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1366 | CPDF_NameTree nameTree(pDoc, "Dests"); |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1367 | pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1368 | CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1369 | if (pDest) |
| 1370 | count += pDest->GetCount(); |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1371 | |
| 1372 | if (!count.IsValid()) |
| 1373 | return 0; |
| 1374 | |
| 1375 | return count.ValueOrDie(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1378 | FPDF_EXPORT FPDF_DEST FPDF_CALLCONV |
| 1379 | FPDF_GetNamedDestByName(FPDF_DOCUMENT document, FPDF_BYTESTRING name) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1380 | if (!name || name[0] == 0) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1381 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1382 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1383 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1384 | if (!pDoc) |
| 1385 | return nullptr; |
| 1386 | |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1387 | CPDF_NameTree name_tree(pDoc, "Dests"); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1388 | return name_tree.LookupNamedDest(pDoc, PDF_DecodeText(ByteString(name))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1391 | #ifdef PDF_ENABLE_XFA |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1392 | FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Init(FPDF_BSTR* str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1393 | if (!str) |
| 1394 | return -1; |
| 1395 | |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 1396 | memset(str, 0, sizeof(FPDF_BSTR)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1397 | return 0; |
| 1398 | } |
| 1399 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1400 | FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Set(FPDF_BSTR* str, |
| 1401 | FPDF_LPCSTR bstr, |
| 1402 | int length) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1403 | if (!str) |
| 1404 | return -1; |
| 1405 | if (!bstr || !length) |
| 1406 | return -1; |
| 1407 | if (length == -1) |
Ryan Harrison | 8b1408e | 2017-09-27 11:07:51 -0400 | [diff] [blame] | 1408 | length = strlen(bstr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1409 | |
| 1410 | if (length == 0) { |
| 1411 | if (str->str) { |
| 1412 | FX_Free(str->str); |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1413 | str->str = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1414 | } |
| 1415 | str->len = 0; |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | if (str->str && str->len < length) |
| 1420 | str->str = FX_Realloc(char, str->str, length + 1); |
| 1421 | else if (!str->str) |
| 1422 | str->str = FX_Alloc(char, length + 1); |
| 1423 | |
| 1424 | str->str[length] = 0; |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 1425 | memcpy(str->str, bstr, length); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1426 | str->len = length; |
| 1427 | |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1431 | FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Clear(FPDF_BSTR* str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1432 | if (!str) |
| 1433 | return -1; |
| 1434 | |
| 1435 | if (str->str) { |
| 1436 | FX_Free(str->str); |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1437 | str->str = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1438 | } |
| 1439 | str->len = 0; |
| 1440 | return 0; |
| 1441 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1442 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1443 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1444 | FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDF_GetNamedDest(FPDF_DOCUMENT document, |
| 1445 | int index, |
| 1446 | void* buffer, |
| 1447 | long* buflen) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1448 | if (!buffer) |
| 1449 | *buflen = 0; |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1450 | |
| 1451 | if (index < 0) |
| 1452 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1453 | |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 1454 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1455 | if (!pDoc) |
| 1456 | return nullptr; |
| 1457 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 1458 | const CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1459 | if (!pRoot) |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1460 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1461 | |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1462 | CPDF_Object* pDestObj = nullptr; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1463 | WideString wsName; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1464 | CPDF_NameTree nameTree(pDoc, "Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1465 | int count = nameTree.GetCount(); |
| 1466 | if (index >= count) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1467 | CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1468 | if (!pDest) |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1469 | return nullptr; |
| 1470 | |
| 1471 | pdfium::base::CheckedNumeric<int> checked_count = count; |
| 1472 | checked_count += pDest->GetCount(); |
| 1473 | if (!checked_count.IsValid() || index >= checked_count.ValueOrDie()) |
| 1474 | return nullptr; |
| 1475 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1476 | index -= count; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1477 | int i = 0; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1478 | ByteString bsName; |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1479 | for (const auto& it : *pDest) { |
| 1480 | bsName = it.first; |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 1481 | pDestObj = it.second.get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1482 | if (!pDestObj) |
| 1483 | continue; |
| 1484 | if (i == index) |
| 1485 | break; |
| 1486 | i++; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1487 | } |
Jane Liu | 67ccef7 | 2017-07-19 13:10:50 -0400 | [diff] [blame] | 1488 | wsName = PDF_DecodeText(bsName); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1489 | } else { |
Jane Liu | 67ccef7 | 2017-07-19 13:10:50 -0400 | [diff] [blame] | 1490 | pDestObj = nameTree.LookupValueAndName(index, &wsName); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1491 | } |
| 1492 | if (!pDestObj) |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1493 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 1494 | if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1495 | pDestObj = pDict->GetArrayFor("D"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1496 | if (!pDestObj) |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1497 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1498 | } |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1499 | if (!pDestObj->IsArray()) |
| 1500 | return nullptr; |
| 1501 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1502 | ByteString utf16Name = wsName.UTF16LE_Encode(); |
weili | 47ca692 | 2016-03-31 15:08:27 -0700 | [diff] [blame] | 1503 | int len = utf16Name.GetLength(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1504 | if (!buffer) { |
| 1505 | *buflen = len; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 1506 | } else if (len <= *buflen) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1507 | memcpy(buffer, utf16Name.c_str(), len); |
| 1508 | *buflen = len; |
| 1509 | } else { |
| 1510 | *buflen = -1; |
| 1511 | } |
| 1512 | return (FPDF_DEST)pDestObj; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1513 | } |