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