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