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