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); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame^] | 491 | if (!pDict) |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 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); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame^] | 605 | if (!pContext->m_hBitmap) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame^] | 643 | if (!hMemDC) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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) { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame^] | 766 | if (!page || !page_x || !page_y) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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 | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 773 | CFX_Matrix page2device; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 774 | pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, |
| 775 | rotate); |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 776 | CFX_Matrix device2page; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 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 | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 805 | CFX_Matrix page2device; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 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) { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame^] | 859 | if (!bitmap) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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) { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame^] | 874 | return bitmap ? ((CFX_DIBitmap*)bitmap)->GetBuffer() : nullptr; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 877 | DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame^] | 878 | return bitmap ? ((CFX_DIBitmap*)bitmap)->GetWidth() : 0; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 881 | DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame^] | 882 | return bitmap ? ((CFX_DIBitmap*)bitmap)->GetHeight() : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame^] | 886 | return bitmap ? ((CFX_DIBitmap*)bitmap)->GetPitch() : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) { |
| 890 | delete (CFX_DIBitmap*)bitmap; |
| 891 | } |
| 892 | |
| 893 | void FPDF_RenderPage_Retail(CRenderContext* pContext, |
| 894 | FPDF_PAGE page, |
| 895 | int start_x, |
| 896 | int start_y, |
| 897 | int size_x, |
| 898 | int size_y, |
| 899 | int rotate, |
| 900 | int flags, |
| 901 | FX_BOOL bNeedToRestore, |
| 902 | IFSDK_PAUSE_Adapter* pause) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 903 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 904 | if (!pPage) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 905 | return; |
| 906 | |
| 907 | if (!pContext->m_pOptions) |
| 908 | pContext->m_pOptions = new CPDF_RenderOptions; |
| 909 | |
| 910 | if (flags & FPDF_LCD_TEXT) |
| 911 | pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE; |
| 912 | else |
| 913 | pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE; |
| 914 | if (flags & FPDF_NO_NATIVETEXT) |
| 915 | pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT; |
| 916 | if (flags & FPDF_RENDER_LIMITEDIMAGECACHE) |
| 917 | pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE; |
| 918 | if (flags & FPDF_RENDER_FORCEHALFTONE) |
| 919 | pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 920 | #ifndef PDF_ENABLE_XFA |
| 921 | if (flags & FPDF_RENDER_NO_SMOOTHTEXT) |
| 922 | pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH; |
| 923 | if (flags & FPDF_RENDER_NO_SMOOTHIMAGE) |
| 924 | pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH; |
| 925 | if (flags & FPDF_RENDER_NO_SMOOTHPATH) |
| 926 | pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 927 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 928 | // Grayscale output |
| 929 | if (flags & FPDF_GRAYSCALE) { |
| 930 | pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY; |
| 931 | pContext->m_pOptions->m_ForeColor = 0; |
| 932 | pContext->m_pOptions->m_BackColor = 0xffffff; |
| 933 | } |
| 934 | const CPDF_OCContext::UsageType usage = |
| 935 | (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View; |
| 936 | pContext->m_pOptions->m_AddFlags = flags >> 8; |
| 937 | pContext->m_pOptions->m_pOCContext = |
| 938 | new CPDF_OCContext(pPage->m_pDocument, usage); |
| 939 | |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 940 | CFX_Matrix matrix; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 941 | pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate); |
| 942 | |
| 943 | FX_RECT clip; |
| 944 | clip.left = start_x; |
| 945 | clip.right = start_x + size_x; |
| 946 | clip.top = start_y; |
| 947 | clip.bottom = start_y + size_y; |
| 948 | pContext->m_pDevice->SaveState(); |
| 949 | pContext->m_pDevice->SetClip_Rect(&clip); |
| 950 | |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 951 | pContext->m_pContext = new CPDF_RenderContext; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 952 | pContext->m_pContext->Create(pPage); |
| 953 | pContext->m_pContext->AppendObjectList(pPage, &matrix); |
| 954 | |
| 955 | if (flags & FPDF_ANNOT) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 956 | pContext->m_pAnnots = new CPDF_AnnotList(pPage); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 957 | FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY; |
| 958 | pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext, bPrinting, |
| 959 | &matrix, TRUE, NULL); |
| 960 | } |
| 961 | |
Tom Sepez | b3b6762 | 2015-10-19 16:20:03 -0700 | [diff] [blame] | 962 | pContext->m_pRenderer = new CPDF_ProgressiveRenderer( |
| 963 | pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions); |
| 964 | pContext->m_pRenderer->Start(pause); |
Tom Sepez | c7e4c4f | 2015-11-20 09:45:24 -0800 | [diff] [blame] | 965 | if (bNeedToRestore) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 966 | pContext->m_pDevice->RestoreState(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document, |
| 970 | int page_index, |
| 971 | double* width, |
| 972 | double* height) { |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 973 | UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document); |
| 974 | if (!pDoc) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 975 | return FALSE; |
| 976 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 977 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 978 | int count = pDoc->GetPageCount(); |
| 979 | if (page_index < 0 || page_index >= count) |
| 980 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 981 | CPDFXFA_Page* pPage = pDoc->GetPage(page_index); |
| 982 | if (!pPage) |
| 983 | return FALSE; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 984 | *width = pPage->GetPageWidth(); |
| 985 | *height = pPage->GetPageHeight(); |
| 986 | #else // PDF_ENABLE_XFA |
| 987 | CPDF_Dictionary* pDict = pDoc->GetPage(page_index); |
| 988 | if (!pDict) |
| 989 | return FALSE; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 990 | CPDF_Page page; |
| 991 | page.Load(pDoc, pDict); |
| 992 | *width = page.GetPageWidth(); |
| 993 | *height = page.GetPageHeight(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 994 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 995 | |
| 996 | return TRUE; |
| 997 | } |
| 998 | |
| 999 | DLLEXPORT FPDF_BOOL STDCALL |
| 1000 | FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1001 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1002 | if (!pDoc) |
| 1003 | return TRUE; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1004 | CPDF_ViewerPreferences viewRef(pDoc); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1005 | return viewRef.PrintScaling(); |
| 1006 | } |
| 1007 | |
| 1008 | DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(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 1; |
| 1012 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1013 | return viewRef.NumCopies(); |
| 1014 | } |
| 1015 | |
| 1016 | DLLEXPORT FPDF_PAGERANGE STDCALL |
| 1017 | FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1018 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1019 | if (!pDoc) |
| 1020 | return NULL; |
| 1021 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1022 | return viewRef.PrintPageRange(); |
| 1023 | } |
| 1024 | |
| 1025 | DLLEXPORT FPDF_DUPLEXTYPE STDCALL |
| 1026 | FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1027 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1028 | if (!pDoc) |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1029 | return DuplexUndefined; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1030 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1031 | CFX_ByteString duplex = viewRef.Duplex(); |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1032 | if ("Simplex" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1033 | return Simplex; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1034 | if ("DuplexFlipShortEdge" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1035 | return DuplexFlipShortEdge; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1036 | if ("DuplexFlipLongEdge" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1037 | return DuplexFlipLongEdge; |
| 1038 | return DuplexUndefined; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1041 | DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1042 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1043 | if (!pDoc) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1044 | return 0; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1045 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1046 | CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
| 1047 | if (!pRoot) |
| 1048 | return 0; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1049 | |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1050 | CPDF_NameTree nameTree(pDoc, "Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1051 | int count = nameTree.GetCount(); |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1052 | CPDF_Dictionary* pDest = pRoot->GetDict("Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1053 | if (pDest) |
| 1054 | count += pDest->GetCount(); |
| 1055 | return count; |
| 1056 | } |
| 1057 | |
| 1058 | DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document, |
| 1059 | FPDF_BYTESTRING name) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1060 | if (!name || name[0] == 0) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1061 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1062 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1063 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1064 | if (!pDoc) |
| 1065 | return nullptr; |
| 1066 | |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1067 | CPDF_NameTree name_tree(pDoc, "Dests"); |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1068 | return name_tree.LookupNamedDest(pDoc, name); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1071 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1072 | FPDF_RESULT FPDF_BStr_Init(FPDF_BSTR* str) { |
| 1073 | if (!str) |
| 1074 | return -1; |
| 1075 | |
| 1076 | FXSYS_memset(str, 0, sizeof(FPDF_BSTR)); |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | FPDF_RESULT FPDF_BStr_Set(FPDF_BSTR* str, FPDF_LPCSTR bstr, int length) { |
| 1081 | if (!str) |
| 1082 | return -1; |
| 1083 | if (!bstr || !length) |
| 1084 | return -1; |
| 1085 | if (length == -1) |
| 1086 | length = FXSYS_strlen(bstr); |
| 1087 | |
| 1088 | if (length == 0) { |
| 1089 | if (str->str) { |
| 1090 | FX_Free(str->str); |
| 1091 | str->str = NULL; |
| 1092 | } |
| 1093 | str->len = 0; |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | if (str->str && str->len < length) |
| 1098 | str->str = FX_Realloc(char, str->str, length + 1); |
| 1099 | else if (!str->str) |
| 1100 | str->str = FX_Alloc(char, length + 1); |
| 1101 | |
| 1102 | str->str[length] = 0; |
| 1103 | if (str->str == NULL) |
| 1104 | return -1; |
| 1105 | |
| 1106 | FXSYS_memcpy(str->str, bstr, length); |
| 1107 | str->len = length; |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | FPDF_RESULT FPDF_BStr_Clear(FPDF_BSTR* str) { |
| 1113 | if (!str) |
| 1114 | return -1; |
| 1115 | |
| 1116 | if (str->str) { |
| 1117 | FX_Free(str->str); |
| 1118 | str->str = NULL; |
| 1119 | } |
| 1120 | str->len = 0; |
| 1121 | return 0; |
| 1122 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1123 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1124 | |
| 1125 | DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, |
| 1126 | int index, |
| 1127 | void* buffer, |
| 1128 | long* buflen) { |
| 1129 | if (!buffer) |
| 1130 | *buflen = 0; |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1131 | |
| 1132 | if (index < 0) |
| 1133 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1134 | |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 1135 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1136 | if (!pDoc) |
| 1137 | return nullptr; |
| 1138 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1139 | CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
| 1140 | if (!pRoot) |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1141 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1142 | |
| 1143 | CPDF_Object* pDestObj = NULL; |
| 1144 | CFX_ByteString bsName; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1145 | CPDF_NameTree nameTree(pDoc, "Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1146 | int count = nameTree.GetCount(); |
| 1147 | if (index >= count) { |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1148 | CPDF_Dictionary* pDest = pRoot->GetDict("Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1149 | if (!pDest) |
| 1150 | return NULL; |
| 1151 | if (index >= count + pDest->GetCount()) |
| 1152 | return NULL; |
| 1153 | index -= count; |
| 1154 | FX_POSITION pos = pDest->GetStartPos(); |
| 1155 | int i = 0; |
| 1156 | while (pos) { |
| 1157 | pDestObj = pDest->GetNextElement(pos, bsName); |
| 1158 | if (!pDestObj) |
| 1159 | continue; |
| 1160 | if (i == index) |
| 1161 | break; |
| 1162 | i++; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1163 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1164 | } else { |
| 1165 | pDestObj = nameTree.LookupValue(index, bsName); |
| 1166 | } |
| 1167 | if (!pDestObj) |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1168 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 1169 | if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) { |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1170 | pDestObj = pDict->GetArray("D"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1171 | if (!pDestObj) |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1172 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1173 | } |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1174 | if (!pDestObj->IsArray()) |
| 1175 | return nullptr; |
| 1176 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1177 | CFX_WideString wsName = PDF_DecodeText(bsName); |
| 1178 | CFX_ByteString utf16Name = wsName.UTF16LE_Encode(); |
| 1179 | unsigned int len = utf16Name.GetLength(); |
| 1180 | if (!buffer) { |
| 1181 | *buflen = len; |
| 1182 | } else if (*buflen >= len) { |
| 1183 | memcpy(buffer, utf16Name.c_str(), len); |
| 1184 | *buflen = len; |
| 1185 | } else { |
| 1186 | *buflen = -1; |
| 1187 | } |
| 1188 | return (FPDF_DEST)pDestObj; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1189 | } |