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