blob: b82714c8e5f8b3abc4576d2c51d5a4a87d66e4ff [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 Zhangaa8bf7e2015-12-24 19:13:32 -08009#include <memory>
10
Dan Sinclair455a4192016-03-16 09:48:56 -040011#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040012#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
13#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
dsinclaircac704d2016-07-28 12:59:09 -070014#include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h"
weili9f515bc2016-07-24 08:08:24 -070015#include "core/fpdfapi/fpdf_render/include/cpdf_progressiverenderer.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040016#include "core/fpdfapi/fpdf_render/include/cpdf_renderoptions.h"
17#include "core/fpdfapi/include/cpdf_modulemgr.h"
weili9f515bc2016-07-24 08:08:24 -070018#include "core/fpdfapi/include/cpdf_pagerendercontext.h"
dsinclaircac704d2016-07-28 12:59:09 -070019#include "core/fpdfdoc/include/cpdf_annotlist.h"
20#include "core/fpdfdoc/include/cpdf_nametree.h"
21#include "core/fpdfdoc/include/cpdf_viewerpreferences.h"
dsinclair86e54742016-04-06 12:30:31 -070022#include "core/fxcodec/include/fx_codec.h"
weili9f515bc2016-07-24 08:08:24 -070023#include "core/fxcrt/include/fx_memory.h"
Dan Sinclaira8a28e02016-03-23 15:41:39 -040024#include "core/fxcrt/include/fx_safe_types.h"
thestig25fa42f2016-05-25 21:39:46 -070025#include "core/fxge/include/fx_ge.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080026#include "fpdfsdk/include/fsdk_define.h"
27#include "fpdfsdk/include/fsdk_mgr.h"
weili9f515bc2016-07-24 08:08:24 -070028#include "fpdfsdk/include/fsdk_pauseadapter.h"
dsinclair64376be2016-03-31 20:03:24 -070029#include "fpdfsdk/javascript/ijs_runtime.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080030#include "public/fpdf_ext.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080031#include "public/fpdf_progressive.h"
Lei Zhang8241df72015-11-06 14:38:48 -080032#include "third_party/base/numerics/safe_conversions_impl.h"
Bo Xufdc00a72014-10-28 23:03:33 -070033
Tom Sepez40e9ff32015-11-30 12:39:54 -080034#ifdef PDF_ENABLE_XFA
dsinclair89bdd082016-04-06 10:47:54 -070035#include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h"
36#include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h"
37#include "fpdfsdk/fpdfxfa/include/fpdfxfa_page.h"
38#include "fpdfsdk/fpdfxfa/include/fpdfxfa_util.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080039#include "public/fpdf_formfill.h"
40#endif // PDF_ENABLE_XFA
41
thestig25fa42f2016-05-25 21:39:46 -070042#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
43#include "core/fxge/include/fx_ge_win32.h"
44#endif
45
Tom Sepez50d12ad2015-11-24 09:50:51 -080046UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
47 return static_cast<UnderlyingDocumentType*>(doc);
48}
49
50FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
51 return static_cast<FPDF_DOCUMENT>(doc);
52}
53
54UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
55 return static_cast<UnderlyingPageType*>(page);
56}
57
Tom Sepez471a1032015-10-15 16:17:18 -070058CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080059#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080060 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080061#else // PDF_ENABLE_XFA
62 return UnderlyingFromFPDFDocument(doc);
63#endif // PDF_ENABLE_XFA
Tom Sepez471a1032015-10-15 16:17:18 -070064}
65
Tom Sepezbf59a072015-10-21 14:07:23 -070066FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080067#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080068 return doc ? FPDFDocumentFromUnderlying(
69 new CPDFXFA_Document(doc, CPDFXFA_App::GetInstance()))
70 : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080071#else // PDF_ENABLE_XFA
72 return FPDFDocumentFromUnderlying(doc);
73#endif // PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -070074}
75
Tom Sepezdb0be962015-10-16 14:00:21 -070076CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
Tom Sepez40e9ff32015-11-30 12:39:54 -080077#ifdef PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -080078 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
Tom Sepez40e9ff32015-11-30 12:39:54 -080079#else // PDF_ENABLE_XFA
80 return UnderlyingFromFPDFPage(page);
81#endif // PDF_ENABLE_XFA
Tom Sepezdb0be962015-10-16 14:00:21 -070082}
83
thestigbefa4502016-05-26 20:15:19 -070084CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
85 return static_cast<CFX_DIBitmap*>(bitmap);
86}
87
Tom Sepez40e9ff32015-11-30 12:39:54 -080088#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
90 m_pFS = pFS;
91 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -070092}
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094IFX_FileStream* CFPDF_FileStream::Retain() {
95 return this;
Bo Xufdc00a72014-10-28 23:03:33 -070096}
97
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098void CFPDF_FileStream::Release() {
99 if (m_pFS && m_pFS->Release)
100 m_pFS->Release(m_pFS->clientData);
101 delete this;
Bo Xufdc00a72014-10-28 23:03:33 -0700102}
103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104FX_FILESIZE CFPDF_FileStream::GetSize() {
105 if (m_pFS && m_pFS->GetSize)
106 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
107 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700108}
109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110FX_BOOL CFPDF_FileStream::IsEOF() {
111 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -0700112}
113
weili625ad662016-06-15 11:21:33 -0700114FX_FILESIZE CFPDF_FileStream::GetPosition() {
115 return m_nCurPos;
116}
117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118FX_BOOL CFPDF_FileStream::ReadBlock(void* buffer,
119 FX_FILESIZE offset,
120 size_t size) {
121 if (!buffer || !size || !m_pFS->ReadBlock)
122 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
125 (FPDF_DWORD)size) == 0) {
126 m_nCurPos = offset + size;
127 return TRUE;
128 }
129 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700130}
131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
133 if (!buffer || !size || !m_pFS->ReadBlock)
134 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 FX_FILESIZE nSize = GetSize();
137 if (m_nCurPos >= nSize)
138 return 0;
139 FX_FILESIZE dwAvail = nSize - m_nCurPos;
140 if (dwAvail < (FX_FILESIZE)size)
141 size = (size_t)dwAvail;
142 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
143 (FPDF_DWORD)size) == 0) {
144 m_nCurPos += size;
145 return size;
146 }
Bo Xufdc00a72014-10-28 23:03:33 -0700147
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700149}
150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151FX_BOOL CFPDF_FileStream::WriteBlock(const void* buffer,
152 FX_FILESIZE offset,
153 size_t size) {
154 if (!m_pFS || !m_pFS->WriteBlock)
155 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700156
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
158 (FPDF_DWORD)size) == 0) {
159 m_nCurPos = offset + size;
160 return TRUE;
161 }
162 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700163}
164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165FX_BOOL CFPDF_FileStream::Flush() {
166 if (!m_pFS || !m_pFS->Flush)
167 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700170}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800171#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) {
174 m_FileAccess = *pFileAccess;
Tom Sepez51da0932015-11-25 16:05:49 -0800175#ifdef PDF_ENABLE_XFA
tsepezc3255f52016-03-25 14:52:27 -0700176 m_BufferOffset = (uint32_t)-1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800177#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700178}
179
Tom Sepez40e9ff32015-11-30 12:39:54 -0800180#ifdef PDF_ENABLE_XFA
weili625ad662016-06-15 11:21:33 -0700181CFX_ByteString CPDF_CustomAccess::GetFullPath() {
182 return "";
183}
184
tsepezc3255f52016-03-25 14:52:27 -0700185FX_BOOL CPDF_CustomAccess::GetByte(uint32_t pos, uint8_t& ch) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 if (pos >= m_FileAccess.m_FileLen)
187 return FALSE;
tsepezc3255f52016-03-25 14:52:27 -0700188 if (m_BufferOffset == (uint32_t)-1 || pos < m_BufferOffset ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 pos >= m_BufferOffset + 512) {
190 // Need to read from file access
191 m_BufferOffset = pos;
192 int size = 512;
193 if (pos + 512 > m_FileAccess.m_FileLen)
194 size = m_FileAccess.m_FileLen - pos;
195 if (!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, m_BufferOffset, m_Buffer,
196 size))
197 return FALSE;
198 }
199 ch = m_Buffer[pos - m_BufferOffset];
200 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700201}
202
tsepezc3255f52016-03-25 14:52:27 -0700203FX_BOOL CPDF_CustomAccess::GetBlock(uint32_t pos,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 uint8_t* pBuf,
tsepezc3255f52016-03-25 14:52:27 -0700205 uint32_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 if (pos + size > m_FileAccess.m_FileLen)
207 return FALSE;
208 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, pos, pBuf, size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800210#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211
weili625ad662016-06-15 11:21:33 -0700212FX_FILESIZE CPDF_CustomAccess::GetSize() {
213 return m_FileAccess.m_FileLen;
214}
215
216void CPDF_CustomAccess::Release() {
217 delete this;
218}
219
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer,
221 FX_FILESIZE offset,
222 size_t size) {
223 if (offset < 0) {
224 return FALSE;
225 }
226 FX_SAFE_FILESIZE newPos =
227 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
228 newPos += offset;
Wei Li05d53f02016-03-29 16:42:53 -0700229 if (!newPos.IsValid() ||
230 newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 return FALSE;
232 }
233 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, (uint8_t*)buffer,
234 size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235}
236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
tsepezc3255f52016-03-25 14:52:27 -0700238static uint32_t foxit_sandbox_policy = 0xFFFFFFFF;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
241 switch (policy) {
242 case FPDF_POLICY_MACHINETIME_ACCESS: {
243 if (enable)
244 foxit_sandbox_policy |= 0x01;
245 else
246 foxit_sandbox_policy &= 0xFFFFFFFE;
247 } break;
248 default:
249 break;
250 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251}
252
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
254 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700255 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700256 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 default:
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700258 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260}
261
Tom Sepez2c286192015-06-18 12:47:11 -0700262CCodec_ModuleMgr* g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700265 FPDF_InitLibraryWithConfig(nullptr);
266}
267
Dan Sinclairf766ad22016-03-14 13:51:24 -0400268DLLEXPORT void STDCALL
269FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271
weili47228ac2016-07-20 10:35:31 -0700272 CFX_GEModule* pModule = CFX_GEModule::Get();
273 pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr, g_pCodecModule);
Tom Sepez1b246282015-11-25 15:15:31 -0800274 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
275 pModuleMgr->SetCodecModule(g_pCodecModule);
276 pModuleMgr->InitPageModule();
dsinclairab5b60e2016-06-23 08:18:36 -0700277 pModuleMgr->LoadEmbeddedGB1CMaps();
278 pModuleMgr->LoadEmbeddedJapan1CMaps();
279 pModuleMgr->LoadEmbeddedCNS1CMaps();
280 pModuleMgr->LoadEmbeddedKorea1CMaps();
dsinclaird647a6b2016-04-26 13:13:20 -0700281
Tom Sepez40e9ff32015-11-30 12:39:54 -0800282#ifdef PDF_ENABLE_XFA
jinming_wang48661582016-02-04 09:41:56 +0800283 CPDFXFA_App::GetInstance()->Initialize(
dsinclairec3da5b2016-05-25 16:42:05 -0700284 (cfg && cfg->version >= 2) ? static_cast<v8::Isolate*>(cfg->m_pIsolate)
285 : nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800286#endif // PDF_ENABLE_XFA
dsinclaird647a6b2016-04-26 13:13:20 -0700287
Tom Sepez452b4f32015-10-13 09:27:27 -0700288 if (cfg && cfg->version >= 2)
289 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290}
291
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
Tom Sepez51da0932015-11-25 16:05:49 -0800293#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 CPDFXFA_App::ReleaseInstance();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800295#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 CPDF_ModuleMgr::Destroy();
297 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700298
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 delete g_pCodecModule;
300 g_pCodecModule = nullptr;
thestig2d6dda12016-06-28 07:39:09 -0700301
302 IJS_Runtime::Destroy();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700303}
304
305#ifndef _WIN32
306int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307void SetLastError(int err) {
308 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700309}
310
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311int GetLastError() {
312 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700313}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800314#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700315
Tom Sepezf10ae632016-01-26 14:19:52 -0800316void ProcessParseError(CPDF_Parser::Error err) {
tsepezc3255f52016-03-25 14:52:27 -0700317 uint32_t err_code = FPDF_ERR_SUCCESS;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800319 switch (err) {
320 case CPDF_Parser::SUCCESS:
321 err_code = FPDF_ERR_SUCCESS;
322 break;
323 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 err_code = FPDF_ERR_FILE;
325 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800326 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 err_code = FPDF_ERR_FORMAT;
328 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800329 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 err_code = FPDF_ERR_PASSWORD;
331 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800332 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 err_code = FPDF_ERR_SECURITY;
334 break;
335 }
336 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700337}
338
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
340 FPDF_BOOL enable) {
341 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342}
343
thestigfdb35ff2016-07-18 13:45:44 -0700344#if defined(_WIN32) && defined(PDFIUM_PRINT_TEXT_WITH_GDI)
345DLLEXPORT void STDCALL
346FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) {
347 g_pdfium_typeface_accessible_func = func;
348}
349
350DLLEXPORT void STDCALL FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) {
351 g_pdfium_print_text_with_gdi = !!use_gdi;
352}
353#endif
354
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
356 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700357 // NOTE: the creation of the file needs to be by the embedder on the
358 // other side of this API.
359 IFX_FileRead* pFileAccess = FX_CreateFileRead((const FX_CHAR*)file_path);
360 if (!pFileAccess) {
361 return nullptr;
362 }
363
364 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700366
Tom Sepezf10ae632016-01-26 14:19:52 -0800367 CPDF_Parser::Error error = pParser->StartParse(pFileAccess);
368 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800370 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700371 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800373#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 CPDF_Document* pPDFDoc = pParser->GetDocument();
375 if (!pPDFDoc)
thestig1cd352e2016-06-07 17:53:06 -0700376 return nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700377
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Lei Zhangcb78ef52015-10-02 10:10:49 -0700379 return new CPDFXFA_Document(pPDFDoc, pProvider);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800380#else // PDF_ENABLE_XFA
381 return pParser->GetDocument();
382#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700383}
Jun Fange118ce92015-02-17 06:50:08 -0800384
Tom Sepez40e9ff32015-11-30 12:39:54 -0800385#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
387 int* docType) {
388 if (!document)
389 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800390
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 CPDF_Document* pdfDoc =
392 (static_cast<CPDFXFA_Document*>(document))->GetPDFDoc();
393 if (!pdfDoc)
394 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800395
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
397 if (!pRoot)
398 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800399
Wei Li9b761132016-01-29 15:44:20 -0800400 CPDF_Dictionary* pAcroForm = pRoot->GetDictBy("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 if (!pAcroForm)
402 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800403
tsepezbd567552016-03-29 14:51:50 -0700404 CPDF_Object* pXFA = pAcroForm->GetObjectBy("XFA");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 if (!pXFA)
406 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800407
thestigded36342016-05-23 17:54:02 -0700408 bool bDynamicXFA = pRoot->GetBooleanBy("NeedsRendering", false);
409 *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 return TRUE;
Jun Fange118ce92015-02-17 06:50:08 -0800411}
412
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
414 return document && (static_cast<CPDFXFA_Document*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700415}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800416#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700417
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418class CMemFile final : public IFX_FileRead {
419 public:
420 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700421
Lei Zhang3884dba2015-10-19 17:27:53 -0700422 void Release() override { delete this; }
423 FX_FILESIZE GetSize() override { return m_size; }
424 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 if (offset < 0) {
426 return FALSE;
427 }
428 FX_SAFE_FILESIZE newPos =
429 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
430 newPos += offset;
weili47ca6922016-03-31 15:08:27 -0700431 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432 return FALSE;
433 }
434 FXSYS_memcpy(buffer, m_pBuf + offset, size);
435 return TRUE;
436 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700437
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438 private:
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700439 ~CMemFile() override {}
440
Lei Zhang3884dba2015-10-19 17:27:53 -0700441 uint8_t* const m_pBuf;
442 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700443};
Lei Zhang3884dba2015-10-19 17:27:53 -0700444
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
446 int size,
447 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700448 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 pParser->SetPassword(password);
450 CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
Tom Sepezf10ae632016-01-26 14:19:52 -0800451 CPDF_Parser::Error error = pParser->StartParse(pMemFile);
452 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800454 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700455 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 }
thestig1cd352e2016-06-07 17:53:06 -0700457 CPDF_Document* pDoc = pParser ? pParser->GetDocument() : nullptr;
Tom Sepezf10ae632016-01-26 14:19:52 -0800458 CheckUnSupportError(pDoc, error);
Tom Sepezbf59a072015-10-21 14:07:23 -0700459 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700460}
461
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462DLLEXPORT FPDF_DOCUMENT STDCALL
463FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
464 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700465 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 pParser->SetPassword(password);
Tom Sepezae51c812015-08-05 12:34:06 -0700467 CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
Tom Sepezf10ae632016-01-26 14:19:52 -0800468 CPDF_Parser::Error error = pParser->StartParse(pFile);
469 if (error != CPDF_Parser::SUCCESS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 delete pParser;
Tom Sepezf10ae632016-01-26 14:19:52 -0800471 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700472 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 }
thestig1cd352e2016-06-07 17:53:06 -0700474 CPDF_Document* pDoc = pParser ? pParser->GetDocument() : nullptr;
Tom Sepezf10ae632016-01-26 14:19:52 -0800475 CheckUnSupportError(pDoc, error);
Tom Sepezbf59a072015-10-21 14:07:23 -0700476 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700477}
478
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
480 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700481 if (!fileVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700483
Tom Sepez471a1032015-10-15 16:17:18 -0700484 *fileVersion = 0;
485 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
486 if (!pDoc)
487 return FALSE;
488
489 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 if (!pParser)
491 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700492
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700493 *fileVersion = pParser->GetFileVersion();
494 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700495}
496
tsepezc3255f52016-03-25 14:52:27 -0700497// jabdelmalek: changed return type from uint32_t to build on Linux (and match
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498// header).
499DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700500 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestig27ddf162016-05-23 15:06:59 -0700501 // https://bugs.chromium.org/p/pdfium/issues/detail?id=499
502 if (!pDoc) {
Tom Sepez51da0932015-11-25 16:05:49 -0800503#ifndef PDF_ENABLE_XFA
504 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800505#else // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700506 return 0xFFFFFFFF;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800507#endif // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700508 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700509
thestig27ddf162016-05-23 15:06:59 -0700510 return pDoc->GetUserPermissions();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700511}
512
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700513DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700514 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700515 if (!pDoc || !pDoc->GetParser())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700517
Tom Sepez471a1032015-10-15 16:17:18 -0700518 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
Wei Li9b761132016-01-29 15:44:20 -0800519 return pDict ? pDict->GetIntegerBy("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700520}
521
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800523 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700524 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700525}
526
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
528 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800529 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
530 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700531 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800532
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700533 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700534 return nullptr;
535
Tom Sepez40e9ff32015-11-30 12:39:54 -0800536#ifdef PDF_ENABLE_XFA
537 return pDoc->GetPage(page_index);
538#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800539 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800540 if (!pDict)
thestig5cc24652016-04-26 11:46:02 -0700541 return nullptr;
542
543 CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true);
544 pPage->ParseContent();
Tom Sepez51da0932015-11-25 16:05:49 -0800545 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800546#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700547}
548
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800550 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700551 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700552}
553
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800555 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700556 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700557}
558
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700559#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
561 FPDF_PAGE page,
562 int start_x,
563 int start_y,
564 int size_x,
565 int size_y,
566 int rotate,
567 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700568 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 if (!pPage)
570 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700571
weili9f515bc2016-07-24 08:08:24 -0700572 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
573 pPage->SetRenderContext(WrapUnique(pContext));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700574
Jun Fang1aeeceb2015-12-29 10:27:44 +0800575 CFX_DIBitmap* pBitmap = nullptr;
576 FX_BOOL bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();
577 FX_BOOL bHasImageMask = pPage->HasImageMask();
578 if (bBackgroundAlphaNeeded || bHasImageMask) {
Tom Sepezae51c812015-08-05 12:34:06 -0700579 pBitmap = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580 pBitmap->Create(size_x, size_y, FXDIB_Argb);
581 pBitmap->Clear(0x00ffffff);
thestigbefa4502016-05-26 20:15:19 -0700582 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
weili9f515bc2016-07-24 08:08:24 -0700583 pContext->m_pDevice.reset(pDevice);
thestigbefa4502016-05-26 20:15:19 -0700584 pDevice->Attach(pBitmap, false, nullptr, false);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800585 } else {
weili9f515bc2016-07-24 08:08:24 -0700586 pContext->m_pDevice.reset(new CFX_WindowsDevice(dc));
Jun Fang1aeeceb2015-12-29 10:27:44 +0800587 }
Bo Xud4e406e2014-08-13 11:03:19 -0700588
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700589 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
thestigbefa4502016-05-26 20:15:19 -0700590 rotate, flags, TRUE, nullptr);
Bo Xud4e406e2014-08-13 11:03:19 -0700591
Jun Fang1aeeceb2015-12-29 10:27:44 +0800592 if (bBackgroundAlphaNeeded || bHasImageMask) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700593 if (pBitmap) {
594 CFX_WindowsDevice WinDC(dc);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700595
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezae51c812015-08-05 12:34:06 -0700597 CFX_DIBitmap* pDst = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700598 int pitch = pBitmap->GetPitch();
599 pDst->Create(size_x, size_y, FXDIB_Rgb32);
600 FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
601 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
thestig1cd352e2016-06-07 17:53:06 -0700602 FXDIB_BLEND_NORMAL, nullptr, FALSE, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
604 delete pDst;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800605 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 WinDC.SetDIBits(pBitmap, 0, 0);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800607 }
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700608 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 }
Jun Fang1aeeceb2015-12-29 10:27:44 +0800610 if (bBackgroundAlphaNeeded || bHasImageMask)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 delete pBitmap;
Jun Fang1aeeceb2015-12-29 10:27:44 +0800612
weili9f515bc2016-07-24 08:08:24 -0700613 pPage->SetRenderContext(std::unique_ptr<CPDF_PageRenderContext>());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614}
Lei Zhangae85f872016-02-19 14:57:07 -0800615#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616
617DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
618 FPDF_PAGE page,
619 int start_x,
620 int start_y,
621 int size_x,
622 int size_y,
623 int rotate,
624 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700625 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700626 return;
tsepez1e2c5572016-05-25 14:58:09 -0700627
Tom Sepezdb0be962015-10-16 14:00:21 -0700628 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700629 if (!pPage)
630 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700631
weili9f515bc2016-07-24 08:08:24 -0700632 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
633 pPage->SetRenderContext(WrapUnique(pContext));
thestigbefa4502016-05-26 20:15:19 -0700634 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
weili9f515bc2016-07-24 08:08:24 -0700635 pContext->m_pDevice.reset(pDevice);
thestigbefa4502016-05-26 20:15:19 -0700636 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
637 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638
639 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez1e2c5572016-05-25 14:58:09 -0700640 rotate, flags, TRUE, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700641
weili9f515bc2016-07-24 08:08:24 -0700642 pPage->SetRenderContext(std::unique_ptr<CPDF_PageRenderContext>());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700643}
644
Cary Clark399be5b2016-03-14 16:51:29 -0400645#ifdef _SKIA_SUPPORT_
646DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page,
647 int size_x,
648 int size_y) {
649 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
650 if (!pPage)
651 return nullptr;
tsepez1e2c5572016-05-25 14:58:09 -0700652
weili9f515bc2016-07-24 08:08:24 -0700653 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
654 pPage->SetRenderContext(WrapUnique(pContext));
caryclarkd6e18872016-05-13 10:57:20 -0700655 CFX_FxgeDevice* skDevice = new CFX_FxgeDevice;
Cary Clark399be5b2016-03-14 16:51:29 -0400656 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
weili9f515bc2016-07-24 08:08:24 -0700657 pContext->m_pDevice.reset(skDevice);
thestig4d1311b2016-05-25 18:31:25 -0700658 FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, TRUE,
tsepez1e2c5572016-05-25 14:58:09 -0700659 nullptr);
weili9f515bc2016-07-24 08:08:24 -0700660 pPage->SetRenderContext(std::unique_ptr<CPDF_PageRenderContext>());
Cary Clark399be5b2016-03-14 16:51:29 -0400661 return recorder;
662}
663#endif
664
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700665DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
tsepez1e2c5572016-05-25 14:58:09 -0700666 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667 if (!page)
668 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800669#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -0800670 pPage->Release();
671#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800672 CPDFSDK_PageView* pPageView =
tsepez1e2c5572016-05-25 14:58:09 -0700673 static_cast<CPDFSDK_PageView*>(pPage->GetView());
Tom Sepez51da0932015-11-25 16:05:49 -0800674 if (pPageView && pPageView->IsLocked()) {
675 pPageView->TakeOverPage();
676 return;
677 }
tsepez1e2c5572016-05-25 14:58:09 -0700678 delete pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800679#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700680}
681
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700682DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800683#ifdef PDF_ENABLE_XFA
Jun Fangfc751362015-12-16 21:23:39 -0800684 delete UnderlyingFromFPDFDocument(document);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800685#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800686 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
687 if (!pDoc)
688 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800689 CPDF_Parser* pParser = pDoc->GetParser();
Tom Sepez51da0932015-11-25 16:05:49 -0800690 if (!pParser) {
691 delete pDoc;
692 return;
693 }
694 delete pParser;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800695#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700696}
697
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700698DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
699 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700700}
701
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700702DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
703 int start_x,
704 int start_y,
705 int size_x,
706 int size_y,
707 int rotate,
708 int device_x,
709 int device_y,
710 double* page_x,
711 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -0800712 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700713 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800714 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800715#ifdef PDF_ENABLE_XFA
716 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
717 device_y, page_x, page_y);
718#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800719 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800720 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
721 rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800722 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800723 device2page.SetReverse(page2device);
Tom Sepez51da0932015-11-25 16:05:49 -0800724 FX_FLOAT page_x_f, page_y_f;
725 device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
726 page_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800727 *page_x = (page_x_f);
728 *page_y = (page_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800729#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700730}
731
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
733 int start_x,
734 int start_y,
735 int size_x,
736 int size_y,
737 int rotate,
738 double page_x,
739 double page_y,
740 int* device_x,
741 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700742 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800744 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700745 if (!pPage)
746 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800747#ifdef PDF_ENABLE_XFA
748 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
749 device_x, device_y);
750#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800751 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800752 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
753 rotate);
Tom Sepez51da0932015-11-25 16:05:49 -0800754 FX_FLOAT device_x_f, device_y_f;
755 page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
756 device_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800757 *device_x = FXSYS_round(device_x_f);
758 *device_y = FXSYS_round(device_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800759#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700760}
761
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700762DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
763 int height,
764 int alpha) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800765 std::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700766 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
thestig1cd352e2016-06-07 17:53:06 -0700767 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700768 }
769 return pBitmap.release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700770}
771
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700772DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
773 int height,
774 int format,
775 void* first_scan,
776 int stride) {
777 FXDIB_Format fx_format;
778 switch (format) {
779 case FPDFBitmap_Gray:
780 fx_format = FXDIB_8bppRgb;
781 break;
782 case FPDFBitmap_BGR:
783 fx_format = FXDIB_Rgb;
784 break;
785 case FPDFBitmap_BGRx:
786 fx_format = FXDIB_Rgb32;
787 break;
788 case FPDFBitmap_BGRA:
789 fx_format = FXDIB_Argb;
790 break;
791 default:
thestig1cd352e2016-06-07 17:53:06 -0700792 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700793 }
794 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
795 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
796 return pBitmap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700797}
798
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700799DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
800 int left,
801 int top,
802 int width,
803 int height,
804 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800805 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700806 return;
thestigbefa4502016-05-26 20:15:19 -0700807
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700808 CFX_FxgeDevice device;
thestigbefa4502016-05-26 20:15:19 -0700809 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
810 device.Attach(pBitmap, false, nullptr, false);
811 if (!pBitmap->HasAlpha())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700812 color |= 0xFF000000;
813 FX_RECT rect(left, top, left + width, top + height);
814 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700815}
816
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700818 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700819}
820
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700821DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700822 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700823}
824
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700826 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827}
828
829DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700830 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700831}
832
833DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700834 delete CFXBitmapFromFPDFBitmap(bitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835}
836
weili9f515bc2016-07-24 08:08:24 -0700837void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700838 FPDF_PAGE page,
839 int start_x,
840 int start_y,
841 int size_x,
842 int size_y,
843 int rotate,
844 int flags,
845 FX_BOOL bNeedToRestore,
846 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700847 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
848 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849 return;
850
851 if (!pContext->m_pOptions)
weili9f515bc2016-07-24 08:08:24 -0700852 pContext->m_pOptions.reset(new CPDF_RenderOptions);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700853
854 if (flags & FPDF_LCD_TEXT)
855 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
856 else
857 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
858 if (flags & FPDF_NO_NATIVETEXT)
859 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
860 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
861 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
862 if (flags & FPDF_RENDER_FORCEHALFTONE)
863 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
Tom Sepez51da0932015-11-25 16:05:49 -0800864#ifndef PDF_ENABLE_XFA
865 if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
866 pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
867 if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
868 pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
869 if (flags & FPDF_RENDER_NO_SMOOTHPATH)
870 pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800871#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872 // Grayscale output
873 if (flags & FPDF_GRAYSCALE) {
874 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
875 pContext->m_pOptions->m_ForeColor = 0;
876 pContext->m_pOptions->m_BackColor = 0xffffff;
877 }
878 const CPDF_OCContext::UsageType usage =
879 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
880 pContext->m_pOptions->m_AddFlags = flags >> 8;
881 pContext->m_pOptions->m_pOCContext =
882 new CPDF_OCContext(pPage->m_pDocument, usage);
883
Tom Sepez60d909e2015-12-10 15:34:55 -0800884 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
886
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887 pContext->m_pDevice->SaveState();
Tom Sepezbec4ea12016-02-29 13:23:13 -0800888 pContext->m_pDevice->SetClip_Rect(
889 FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890
weili9f515bc2016-07-24 08:08:24 -0700891 pContext->m_pContext.reset(new CPDF_RenderContext(pPage));
Tom Sepez71fdc342016-01-22 12:06:32 -0800892 pContext->m_pContext->AppendLayer(pPage, &matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893
894 if (flags & FPDF_ANNOT) {
weili9f515bc2016-07-24 08:08:24 -0700895 pContext->m_pAnnots.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896 FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
weili9f515bc2016-07-24 08:08:24 -0700897 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext.get(),
898 bPrinting, &matrix, TRUE, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 }
900
weili9f515bc2016-07-24 08:08:24 -0700901 pContext->m_pRenderer.reset(new CPDF_ProgressiveRenderer(
902 pContext->m_pContext.get(), pContext->m_pDevice.get(),
903 pContext->m_pOptions.get()));
Tom Sepezb3b67622015-10-19 16:20:03 -0700904 pContext->m_pRenderer->Start(pause);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800905 if (bNeedToRestore)
thestig41846a02016-05-26 10:45:30 -0700906 pContext->m_pDevice->RestoreState(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907}
908
909DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
910 int page_index,
911 double* width,
912 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800913 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
914 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 return FALSE;
916
Tom Sepez40e9ff32015-11-30 12:39:54 -0800917#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918 int count = pDoc->GetPageCount();
919 if (page_index < 0 || page_index >= count)
920 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921 CPDFXFA_Page* pPage = pDoc->GetPage(page_index);
922 if (!pPage)
923 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800924 *width = pPage->GetPageWidth();
925 *height = pPage->GetPageHeight();
926#else // PDF_ENABLE_XFA
927 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
928 if (!pDict)
929 return FALSE;
thestig5cc24652016-04-26 11:46:02 -0700930
931 CPDF_Page page(pDoc, pDict, true);
Tom Sepez51da0932015-11-25 16:05:49 -0800932 *width = page.GetPageWidth();
933 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800934#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700935
936 return TRUE;
937}
938
939DLLEXPORT FPDF_BOOL STDCALL
940FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700941 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942 if (!pDoc)
943 return TRUE;
Tom Sepez471a1032015-10-15 16:17:18 -0700944 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 return viewRef.PrintScaling();
946}
947
948DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700949 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700950 if (!pDoc)
951 return 1;
952 CPDF_ViewerPreferences viewRef(pDoc);
953 return viewRef.NumCopies();
954}
955
956DLLEXPORT FPDF_PAGERANGE STDCALL
957FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700958 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700960 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961 CPDF_ViewerPreferences viewRef(pDoc);
962 return viewRef.PrintPageRange();
963}
964
965DLLEXPORT FPDF_DUPLEXTYPE STDCALL
966FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700967 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700968 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -0700969 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970 CPDF_ViewerPreferences viewRef(pDoc);
971 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -0800972 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -0800974 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -0800976 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977 return DuplexFlipLongEdge;
978 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -0700979}
980
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700982 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
983 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -0800985
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 CPDF_Dictionary* pRoot = pDoc->GetRoot();
987 if (!pRoot)
988 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -0800989
Lei Zhangd983b092015-12-14 16:58:33 -0800990 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800991 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
Wei Li9b761132016-01-29 15:44:20 -0800992 CPDF_Dictionary* pDest = pRoot->GetDictBy("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993 if (pDest)
994 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800995
996 if (!count.IsValid())
997 return 0;
998
999 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000}
1001
1002DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1003 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001005 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006
Tom Sepez471a1032015-10-15 16:17:18 -07001007 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1008 if (!pDoc)
1009 return nullptr;
1010
Lei Zhangd983b092015-12-14 16:58:33 -08001011 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001012 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013}
1014
Tom Sepez51da0932015-11-25 16:05:49 -08001015#ifdef PDF_ENABLE_XFA
thestig77d148d2016-04-06 06:28:31 -07001016DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Init(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017 if (!str)
1018 return -1;
1019
1020 FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
1021 return 0;
1022}
1023
thestig77d148d2016-04-06 06:28:31 -07001024DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Set(FPDF_BSTR* str,
1025 FPDF_LPCSTR bstr,
1026 int length) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027 if (!str)
1028 return -1;
1029 if (!bstr || !length)
1030 return -1;
1031 if (length == -1)
1032 length = FXSYS_strlen(bstr);
1033
1034 if (length == 0) {
1035 if (str->str) {
1036 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001037 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038 }
1039 str->len = 0;
1040 return 0;
1041 }
1042
1043 if (str->str && str->len < length)
1044 str->str = FX_Realloc(char, str->str, length + 1);
1045 else if (!str->str)
1046 str->str = FX_Alloc(char, length + 1);
1047
1048 str->str[length] = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 FXSYS_memcpy(str->str, bstr, length);
1050 str->len = length;
1051
1052 return 0;
1053}
1054
thestig77d148d2016-04-06 06:28:31 -07001055DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Clear(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056 if (!str)
1057 return -1;
1058
1059 if (str->str) {
1060 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001061 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001062 }
1063 str->len = 0;
1064 return 0;
1065}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001066#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067
1068DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1069 int index,
1070 void* buffer,
1071 long* buflen) {
1072 if (!buffer)
1073 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001074
1075 if (index < 0)
1076 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077
Tom Sepezbf59a072015-10-21 14:07:23 -07001078 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001079 if (!pDoc)
1080 return nullptr;
1081
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1083 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001084 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001085
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001086 CPDF_Object* pDestObj = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001088 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001089 int count = nameTree.GetCount();
1090 if (index >= count) {
Wei Li9b761132016-01-29 15:44:20 -08001091 CPDF_Dictionary* pDest = pRoot->GetDictBy("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001093 return nullptr;
1094
1095 pdfium::base::CheckedNumeric<int> checked_count = count;
1096 checked_count += pDest->GetCount();
1097 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1098 return nullptr;
1099
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101 int i = 0;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001102 for (const auto& it : *pDest) {
1103 bsName = it.first;
1104 pDestObj = it.second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105 if (!pDestObj)
1106 continue;
1107 if (i == index)
1108 break;
1109 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001110 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111 } else {
1112 pDestObj = nameTree.LookupValue(index, bsName);
1113 }
1114 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001115 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001116 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
Wei Li9b761132016-01-29 15:44:20 -08001117 pDestObj = pDict->GetArrayBy("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001119 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001120 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001121 if (!pDestObj->IsArray())
1122 return nullptr;
1123
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001124 CFX_WideString wsName = PDF_DecodeText(bsName);
1125 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -07001126 int len = utf16Name.GetLength();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127 if (!buffer) {
1128 *buflen = len;
1129 } else if (*buflen >= len) {
1130 memcpy(buffer, utf16Name.c_str(), len);
1131 *buflen = len;
1132 } else {
1133 *buflen = -1;
1134 }
1135 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001136}