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