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