blob: 1ae1baf415571ca762ac8ef3721f5521d168dbf4 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdfview.h"
8
Lei Zhanga688a042015-11-09 13:57:49 -08009#include "core/include/fxcodec/fx_codec.h"
10#include "core/include/fxcrt/fx_safe_types.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080011#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 Zhangb4e7f302015-11-06 15:52:32 -080015#include "public/fpdf_ext.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080016#include "public/fpdf_progressive.h"
Lei Zhang8241df72015-11-06 14:38:48 -080017#include "third_party/base/nonstd_unique_ptr.h"
18#include "third_party/base/numerics/safe_conversions_impl.h"
Bo Xufdc00a72014-10-28 23:03:33 -070019
Tom Sepez40e9ff32015-11-30 12:39:54 -080020#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 Sepez50d12ad2015-11-24 09:50:51 -080029UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
30 return static_cast<UnderlyingDocumentType*>(doc);
31}
32
33FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
34 return static_cast<FPDF_DOCUMENT>(doc);
35}
36
37UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
38 return static_cast<UnderlyingPageType*>(page);
39}
40
Tom Sepez471a1032015-10-15 16:17:18 -070041CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080042#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080043 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080044#else // PDF_ENABLE_XFA
45 return UnderlyingFromFPDFDocument(doc);
46#endif // PDF_ENABLE_XFA
Tom Sepez471a1032015-10-15 16:17:18 -070047}
48
Tom Sepezbf59a072015-10-21 14:07:23 -070049FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080050#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080051 return doc ? FPDFDocumentFromUnderlying(
52 new CPDFXFA_Document(doc, CPDFXFA_App::GetInstance()))
53 : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080054#else // PDF_ENABLE_XFA
55 return FPDFDocumentFromUnderlying(doc);
56#endif // PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -070057}
58
Tom Sepezdb0be962015-10-16 14:00:21 -070059CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080060#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080061 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080062#else // PDF_ENABLE_XFA
63 return UnderlyingFromFPDFPage(page);
64#endif // PDF_ENABLE_XFA
Tom Sepezdb0be962015-10-16 14:00:21 -070065}
66
Tom Sepez40e9ff32015-11-30 12:39:54 -080067#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
69 m_pFS = pFS;
70 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -070071}
72
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073IFX_FileStream* CFPDF_FileStream::Retain() {
74 return this;
Bo Xufdc00a72014-10-28 23:03:33 -070075}
76
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077void CFPDF_FileStream::Release() {
78 if (m_pFS && m_pFS->Release)
79 m_pFS->Release(m_pFS->clientData);
80 delete this;
Bo Xufdc00a72014-10-28 23:03:33 -070081}
82
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083FX_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 Xufdc00a72014-10-28 23:03:33 -070087}
88
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089FX_BOOL CFPDF_FileStream::IsEOF() {
90 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -070091}
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093FX_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 Xufdc00a72014-10-28 23:03:33 -070098
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 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 Xufdc00a72014-10-28 23:03:33 -0700105}
106
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
108 if (!buffer || !size || !m_pFS->ReadBlock)
109 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700110
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 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 Xufdc00a72014-10-28 23:03:33 -0700122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126FX_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 Xufdc00a72014-10-28 23:03:33 -0700131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 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 Xufdc00a72014-10-28 23:03:33 -0700138}
139
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140FX_BOOL CFPDF_FileStream::Flush() {
141 if (!m_pFS || !m_pFS->Flush)
142 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700145}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800146#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) {
149 m_FileAccess = *pFileAccess;
Tom Sepez51da0932015-11-25 16:05:49 -0800150#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 m_BufferOffset = (FX_DWORD)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800152#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700153}
154
Tom Sepez40e9ff32015-11-30 12:39:54 -0800155#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156FX_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 Xufdc00a72014-10-28 23:03:33 -0700172}
173
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174FX_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-Malek3f3b45c2014-05-23 17:28:10 -0700180}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800181#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183FX_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-Malek3f3b45c2014-05-23 17:28:10 -0700197}
198
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200static FX_DWORD foxit_sandbox_policy = 0xFFFFFFFF;
201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202void 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-Malek3f3b45c2014-05-23 17:28:10 -0700213}
214
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
216 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700217 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700218 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 default:
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700220 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222}
223
Tom Sepez2c286192015-06-18 12:47:11 -0700224CCodec_ModuleMgr* g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700227 FPDF_InitLibraryWithConfig(nullptr);
228}
229
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800230DLLEXPORT void STDCALL FPDF_InitLibraryWithConfig(
231 const FPDF_LIBRARY_CONFIG* cfg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233
Lei Zhang6f62d532015-09-23 15:31:44 -0700234 CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 CPDF_ModuleMgr::Create();
Tom Sepez1b246282015-11-25 15:15:31 -0800238 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
239 pModuleMgr->SetCodecModule(g_pCodecModule);
240 pModuleMgr->InitPageModule();
241 pModuleMgr->InitRenderModule();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800242#ifdef PDF_ENABLE_XFA
243 CPDFXFA_App::GetInstance()->Initialize();
244#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800245 pModuleMgr->LoadEmbeddedGB1CMaps();
246 pModuleMgr->LoadEmbeddedJapan1CMaps();
247 pModuleMgr->LoadEmbeddedCNS1CMaps();
248 pModuleMgr->LoadEmbeddedKorea1CMaps();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800249#endif // PDF_ENABLE_XFA
Tom Sepez452b4f32015-10-13 09:27:27 -0700250 if (cfg && cfg->version >= 2)
251 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252}
253
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
Tom Sepez51da0932015-11-25 16:05:49 -0800255#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 CPDFXFA_App::ReleaseInstance();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800257#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 CPDF_ModuleMgr::Destroy();
259 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 delete g_pCodecModule;
262 g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263}
264
265#ifndef _WIN32
266int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267void SetLastError(int err) {
268 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269}
270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271int GetLastError() {
272 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800274#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276void 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-Malek3f3b45c2014-05-23 17:28:10 -0700293}
294
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
296 FPDF_BOOL enable) {
297 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298}
299
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
301 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700302 // 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 Weber9d8ec5a2015-08-04 13:00:21 -0700310 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700311
Tom Sepeze3166a82015-08-05 10:50:32 -0700312 FX_DWORD err_code = pParser->StartParse(pFileAccess);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 if (err_code) {
314 delete pParser;
315 ProcessParseError(err_code);
316 return NULL;
317 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800318#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 CPDF_Document* pPDFDoc = pParser->GetDocument();
320 if (!pPDFDoc)
321 return NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Lei Zhangcb78ef52015-10-02 10:10:49 -0700324 return new CPDFXFA_Document(pPDFDoc, pProvider);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800325#else // PDF_ENABLE_XFA
326 return pParser->GetDocument();
327#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700328}
Jun Fange118ce92015-02-17 06:50:08 -0800329
Tom Sepez40e9ff32015-11-30 12:39:54 -0800330#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
332 int* docType) {
333 if (!document)
334 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800335
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 CPDF_Document* pdfDoc =
337 (static_cast<CPDFXFA_Document*>(document))->GetPDFDoc();
338 if (!pdfDoc)
339 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800340
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
342 if (!pRoot)
343 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800344
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");
346 if (!pAcroForm)
347 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800348
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
350 if (!pXFA)
351 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 FX_BOOL bDynamicXFA = pRoot->GetBoolean("NeedsRendering", FALSE);
JUN FANG827a1722015-03-05 13:39:21 -0800354
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 if (bDynamicXFA)
Tom Sepezd3116dc2015-11-24 15:58:06 -0800356 *docType = DOCTYPE_DYNAMIC_XFA;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 else
358 *docType = DOCTYPE_STATIC_XFA;
JUN FANG827a1722015-03-05 13:39:21 -0800359
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 return TRUE;
Jun Fange118ce92015-02-17 06:50:08 -0800361}
362
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
364 return document && (static_cast<CPDFXFA_Document*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700365}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800366#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700367
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368class CMemFile final : public IFX_FileRead {
369 public:
370 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700371
Lei Zhang3884dba2015-10-19 17:27:53 -0700372 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 Weber9d8ec5a2015-08-04 13:00:21 -0700375 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-Malek3f3b45c2014-05-23 17:28:10 -0700387
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 private:
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700389 ~CMemFile() override {}
390
Lei Zhang3884dba2015-10-19 17:27:53 -0700391 uint8_t* const m_pBuf;
392 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700393};
Lei Zhang3884dba2015-10-19 17:27:53 -0700394
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
396 int size,
397 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700398 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 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 Sepezbf59a072015-10-21 14:07:23 -0700410 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411}
412
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413DLLEXPORT FPDF_DOCUMENT STDCALL
414FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
415 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700416 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417 pParser->SetPassword(password);
Tom Sepezae51c812015-08-05 12:34:06 -0700418 CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 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 Sepezbf59a072015-10-21 14:07:23 -0700428 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700429}
430
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
432 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700433 if (!fileVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700435
Tom Sepez471a1032015-10-15 16:17:18 -0700436 *fileVersion = 0;
437 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
438 if (!pDoc)
439 return FALSE;
440
441 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 if (!pParser)
443 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700444
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445 *fileVersion = pParser->GetFileVersion();
446 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700447}
448
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449// jabdelmalek: changed return type from FX_DWORD to build on Linux (and match
450// header).
451DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700452 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
453 if (!pDoc)
Tom Sepez51da0932015-11-25 16:05:49 -0800454#ifndef PDF_ENABLE_XFA
455 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800456#else // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 return (FX_DWORD)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800458#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700459
Tom Sepez471a1032015-10-15 16:17:18 -0700460 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
461 return pDict ? pDict->GetInteger("P") : (FX_DWORD)-1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700462}
463
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700465 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
466 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700468
Tom Sepez471a1032015-10-15 16:17:18 -0700469 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
470 return pDict ? pDict->GetInteger("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700471}
472
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800474 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700475 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700476}
477
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
479 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800480 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
481 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700482 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800483
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700484 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700485 return nullptr;
486
Tom Sepez40e9ff32015-11-30 12:39:54 -0800487#ifdef PDF_ENABLE_XFA
488 return pDoc->GetPage(page_index);
489#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800490 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800491 if (!pDict)
Tom Sepez51da0932015-11-25 16:05:49 -0800492 return NULL;
493 CPDF_Page* pPage = new CPDF_Page;
494 pPage->Load(pDoc, pDict);
495 pPage->ParseContent();
496 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800497#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700498}
499
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800501 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700502 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700503}
504
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800506 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700507 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700508}
509
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510void DropContext(void* data) {
511 delete (CRenderContext*)data;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700512}
513
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700514#if defined(_DEBUG) || defined(DEBUG)
515#define DEBUG_TRACE
516#endif
517
518#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519DLLEXPORT 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 Sepezdb0be962015-10-16 14:00:21 -0700527 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528 if (!pPage)
529 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700530
Tom Sepezae51c812015-08-05 12:34:06 -0700531 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700532 pPage->SetPrivateData((void*)1, pContext, DropContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700533
534#ifndef _WIN32_WCE
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535 CFX_DIBitmap* pBitmap = NULL;
536 FX_BOOL bBackgroundAlphaNeeded = FALSE;
537 bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();
538 if (bBackgroundAlphaNeeded) {
Tom Sepezae51c812015-08-05 12:34:06 -0700539 pBitmap = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700540 pBitmap->Create(size_x, size_y, FXDIB_Argb);
541 pBitmap->Clear(0x00ffffff);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700542#ifdef _SKIA_SUPPORT_
Tom Sepezae51c812015-08-05 12:34:06 -0700543 pContext->m_pDevice = new CFX_SkiaDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700545#else
Tom Sepezae51c812015-08-05 12:34:06 -0700546 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700547 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700548#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549 } else
Tom Sepezae51c812015-08-05 12:34:06 -0700550 pContext->m_pDevice = new CFX_WindowsDevice(dc);
Bo Xud4e406e2014-08-13 11:03:19 -0700551
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
553 rotate, flags, TRUE, NULL);
Bo Xud4e406e2014-08-13 11:03:19 -0700554
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700555 if (bBackgroundAlphaNeeded) {
556 if (pBitmap) {
557 CFX_WindowsDevice WinDC(dc);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700558
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezae51c812015-08-05 12:34:06 -0700560 CFX_DIBitmap* pDst = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 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 Zhang6d8b1c22015-06-19 17:26:17 -0700570 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700571 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700572#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573 // 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-Malek3f3b45c2014-05-23 17:28:10 -0700583
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584#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-Malek3f3b45c2014-05-23 17:28:10 -0700592#endif
Bo Xud4e406e2014-08-13 11:03:19 -0700593
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594 // 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 Zhang412e9082015-12-14 18:34:00 -0800605 if (!pContext->m_hBitmap) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606#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-Malek3f3b45c2014-05-23 17:28:10 -0700613#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700615#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 }
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 Zhang412e9082015-12-14 18:34:00 -0800643 if (!hMemDC) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644#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
679DLLEXPORT 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 Sepezdb0be962015-10-16 14:00:21 -0700687 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 return;
Tom Sepezdb0be962015-10-16 14:00:21 -0700689 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 if (!pPage)
691 return;
Tom Sepezae51c812015-08-05 12:34:06 -0700692 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693 pPage->SetPrivateData((void*)1, pContext, DropContext);
694#ifdef _SKIA_SUPPORT_
Tom Sepezae51c812015-08-05 12:34:06 -0700695 pContext->m_pDevice = new CFX_SkiaDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700696
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 Sepezae51c812015-08-05 12:34:06 -0700703 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700704
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-Malek3f3b45c2014-05-23 17:28:10 -0700717}
718
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
720 if (!page)
721 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800722#ifdef PDF_ENABLE_XFA
723 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
724 pPage->Release();
725#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800726 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 Sepez40e9ff32015-11-30 12:39:54 -0800733#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700734}
735
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800737#ifdef PDF_ENABLE_XFA
738 delete CPDFDocumentFromFPDFDocument(document);
739#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800740 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
741 if (!pDoc)
742 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800743 CPDF_Parser* pParser = pDoc->GetParser();
Tom Sepez51da0932015-11-25 16:05:49 -0800744 if (!pParser) {
745 delete pDoc;
746 return;
747 }
748 delete pParser;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800749#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700750}
751
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700752DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
753 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700754}
755
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756DLLEXPORT 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 Zhang412e9082015-12-14 18:34:00 -0800766 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800768 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800769#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 Sepez60d909e2015-12-10 15:34:55 -0800773 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800774 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
775 rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800776 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800777 device2page.SetReverse(page2device);
Tom Sepez51da0932015-11-25 16:05:49 -0800778 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 Sepez51da0932015-11-25 16:05:49 -0800781 *page_x = (page_x_f);
782 *page_y = (page_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800783#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700784}
785
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700786DLLEXPORT 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 Sepezdb0be962015-10-16 14:00:21 -0700796 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700797 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800798 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700799 if (!pPage)
800 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800801#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 Sepez60d909e2015-12-10 15:34:55 -0800805 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800806 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
807 rotate);
Tom Sepez51da0932015-11-25 16:05:49 -0800808 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 Sepez51da0932015-11-25 16:05:49 -0800811 *device_x = FXSYS_round(device_x_f);
812 *device_y = FXSYS_round(device_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800813#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700814}
815
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816DLLEXPORT 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-Malek3f3b45c2014-05-23 17:28:10 -0700824}
825
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826DLLEXPORT 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-Malek3f3b45c2014-05-23 17:28:10 -0700851}
852
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700853DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
854 int left,
855 int top,
856 int width,
857 int height,
858 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800859 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860 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-Malek3f3b45c2014-05-23 17:28:10 -0700871}
872
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800874 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700875}
876
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800878 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700879}
880
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800882 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883}
884
885DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
Lei Zhang412e9082015-12-14 18:34:00 -0800886 return bitmap ? ((CFX_DIBitmap*)bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887}
888
889DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
890 delete (CFX_DIBitmap*)bitmap;
891}
892
893void 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 Sepezdb0be962015-10-16 14:00:21 -0700903 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
904 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905 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 Sepez51da0932015-11-25 16:05:49 -0800920#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 Sepez40e9ff32015-11-30 12:39:54 -0800927#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700928 // 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 Sepez60d909e2015-12-10 15:34:55 -0800940 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941 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 Sepezae51c812015-08-05 12:34:06 -0700951 pContext->m_pContext = new CPDF_RenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952 pContext->m_pContext->Create(pPage);
953 pContext->m_pContext->AppendObjectList(pPage, &matrix);
954
955 if (flags & FPDF_ANNOT) {
Tom Sepezae51c812015-08-05 12:34:06 -0700956 pContext->m_pAnnots = new CPDF_AnnotList(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 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 Sepezb3b67622015-10-19 16:20:03 -0700962 pContext->m_pRenderer = new CPDF_ProgressiveRenderer(
963 pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions);
964 pContext->m_pRenderer->Start(pause);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800965 if (bNeedToRestore)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966 pContext->m_pDevice->RestoreState();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967}
968
969DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
970 int page_index,
971 double* width,
972 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800973 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
974 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 return FALSE;
976
Tom Sepez40e9ff32015-11-30 12:39:54 -0800977#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978 int count = pDoc->GetPageCount();
979 if (page_index < 0 || page_index >= count)
980 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981 CPDFXFA_Page* pPage = pDoc->GetPage(page_index);
982 if (!pPage)
983 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800984 *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 Sepez51da0932015-11-25 16:05:49 -0800990 CPDF_Page page;
991 page.Load(pDoc, pDict);
992 *width = page.GetPageWidth();
993 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800994#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700995
996 return TRUE;
997}
998
999DLLEXPORT FPDF_BOOL STDCALL
1000FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001001 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001002 if (!pDoc)
1003 return TRUE;
Tom Sepez471a1032015-10-15 16:17:18 -07001004 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 return viewRef.PrintScaling();
1006}
1007
1008DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001009 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010 if (!pDoc)
1011 return 1;
1012 CPDF_ViewerPreferences viewRef(pDoc);
1013 return viewRef.NumCopies();
1014}
1015
1016DLLEXPORT FPDF_PAGERANGE STDCALL
1017FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001018 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 if (!pDoc)
1020 return NULL;
1021 CPDF_ViewerPreferences viewRef(pDoc);
1022 return viewRef.PrintPageRange();
1023}
1024
1025DLLEXPORT FPDF_DUPLEXTYPE STDCALL
1026FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001027 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -07001029 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030 CPDF_ViewerPreferences viewRef(pDoc);
1031 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001032 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001034 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001036 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037 return DuplexFlipLongEdge;
1038 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001039}
1040
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001042 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1043 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001045
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1047 if (!pRoot)
1048 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001049
Lei Zhangd983b092015-12-14 16:58:33 -08001050 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 int count = nameTree.GetCount();
Lei Zhangd983b092015-12-14 16:58:33 -08001052 CPDF_Dictionary* pDest = pRoot->GetDict("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053 if (pDest)
1054 count += pDest->GetCount();
1055 return count;
1056}
1057
1058DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1059 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001060 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001061 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001062
Tom Sepez471a1032015-10-15 16:17:18 -07001063 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1064 if (!pDoc)
1065 return nullptr;
1066
Lei Zhangd983b092015-12-14 16:58:33 -08001067 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001068 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069}
1070
Tom Sepez51da0932015-11-25 16:05:49 -08001071#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072FPDF_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
1080FPDF_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
1112FPDF_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 Sepez40e9ff32015-11-30 12:39:54 -08001123#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001124
1125DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1126 int index,
1127 void* buffer,
1128 long* buflen) {
1129 if (!buffer)
1130 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001131
1132 if (index < 0)
1133 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001134
Tom Sepezbf59a072015-10-21 14:07:23 -07001135 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001136 if (!pDoc)
1137 return nullptr;
1138
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1140 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001141 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001142
1143 CPDF_Object* pDestObj = NULL;
1144 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001145 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146 int count = nameTree.GetCount();
1147 if (index >= count) {
Lei Zhangd983b092015-12-14 16:58:33 -08001148 CPDF_Dictionary* pDest = pRoot->GetDict("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 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 Xu4d62b6b2015-01-10 22:52:59 -08001163 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001164 } else {
1165 pDestObj = nameTree.LookupValue(index, bsName);
1166 }
1167 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001168 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001169 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
Lei Zhangd983b092015-12-14 16:58:33 -08001170 pDestObj = pDict->GetArray("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001172 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001174 if (!pDestObj->IsArray())
1175 return nullptr;
1176
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177 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 Xu4d62b6b2015-01-10 22:52:59 -08001189}