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