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