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