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