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