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