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