blob: 1ff100488085b9335326eeeb099cd7d3dad5240b [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 "../include/fpdfxfa/fpdfxfa_app.h"
10#include "../include/fpdfxfa/fpdfxfa_doc.h"
11#include "../include/fpdfxfa/fpdfxfa_page.h"
12#include "../include/fpdfxfa/fpdfxfa_util.h"
Lei Zhanga688a042015-11-09 13:57:49 -080013#include "core/include/fpdfapi/fpdf_module.h"
14#include "core/include/fxcodec/fx_codec.h"
15#include "core/include/fxcrt/fx_safe_types.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080016#include "fpdfsdk/include/fsdk_define.h"
17#include "fpdfsdk/include/fsdk_mgr.h"
18#include "fpdfsdk/include/fsdk_rendercontext.h"
19#include "fpdfsdk/include/javascript/IJavaScript.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080020#include "public/fpdf_ext.h"
21#include "public/fpdf_formfill.h"
22#include "public/fpdf_progressive.h"
Lei Zhang8241df72015-11-06 14:38:48 -080023#include "third_party/base/nonstd_unique_ptr.h"
24#include "third_party/base/numerics/safe_conversions_impl.h"
Bo Xufdc00a72014-10-28 23:03:33 -070025
Tom Sepez50d12ad2015-11-24 09:50:51 -080026UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
27 return static_cast<UnderlyingDocumentType*>(doc);
28}
29
30FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
31 return static_cast<FPDF_DOCUMENT>(doc);
32}
33
34UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
35 return static_cast<UnderlyingPageType*>(page);
36}
37
Tom Sepez471a1032015-10-15 16:17:18 -070038CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
Tom Sepez50d12ad2015-11-24 09:50:51 -080039 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
Tom Sepez471a1032015-10-15 16:17:18 -070040}
41
Tom Sepezbf59a072015-10-21 14:07:23 -070042FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
Tom Sepez50d12ad2015-11-24 09:50:51 -080043 return doc ? FPDFDocumentFromUnderlying(
44 new CPDFXFA_Document(doc, CPDFXFA_App::GetInstance()))
45 : nullptr;
Tom Sepezbf59a072015-10-21 14:07:23 -070046}
47
Tom Sepezdb0be962015-10-16 14:00:21 -070048CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -080049 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
Tom Sepezdb0be962015-10-16 14:00:21 -070050}
51
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
53 m_pFS = pFS;
54 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -070055}
56
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057IFX_FileStream* CFPDF_FileStream::Retain() {
58 return this;
Bo Xufdc00a72014-10-28 23:03:33 -070059}
60
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061void CFPDF_FileStream::Release() {
62 if (m_pFS && m_pFS->Release)
63 m_pFS->Release(m_pFS->clientData);
64 delete this;
Bo Xufdc00a72014-10-28 23:03:33 -070065}
66
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067FX_FILESIZE CFPDF_FileStream::GetSize() {
68 if (m_pFS && m_pFS->GetSize)
69 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
70 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -070071}
72
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073FX_BOOL CFPDF_FileStream::IsEOF() {
74 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -070075}
76
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077FX_BOOL CFPDF_FileStream::ReadBlock(void* buffer,
78 FX_FILESIZE offset,
79 size_t size) {
80 if (!buffer || !size || !m_pFS->ReadBlock)
81 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -070082
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
84 (FPDF_DWORD)size) == 0) {
85 m_nCurPos = offset + size;
86 return TRUE;
87 }
88 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -070089}
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
92 if (!buffer || !size || !m_pFS->ReadBlock)
93 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070094
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 FX_FILESIZE nSize = GetSize();
96 if (m_nCurPos >= nSize)
97 return 0;
98 FX_FILESIZE dwAvail = nSize - m_nCurPos;
99 if (dwAvail < (FX_FILESIZE)size)
100 size = (size_t)dwAvail;
101 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
102 (FPDF_DWORD)size) == 0) {
103 m_nCurPos += size;
104 return size;
105 }
Bo Xufdc00a72014-10-28 23:03:33 -0700106
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700108}
109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110FX_BOOL CFPDF_FileStream::WriteBlock(const void* buffer,
111 FX_FILESIZE offset,
112 size_t size) {
113 if (!m_pFS || !m_pFS->WriteBlock)
114 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
117 (FPDF_DWORD)size) == 0) {
118 m_nCurPos = offset + size;
119 return TRUE;
120 }
121 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700122}
123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124FX_BOOL CFPDF_FileStream::Flush() {
125 if (!m_pFS || !m_pFS->Flush)
126 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700129}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) {
132 m_FileAccess = *pFileAccess;
133 m_BufferOffset = (FX_DWORD)-1;
Bo Xufdc00a72014-10-28 23:03:33 -0700134}
135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136FX_BOOL CPDF_CustomAccess::GetByte(FX_DWORD pos, uint8_t& ch) {
137 if (pos >= m_FileAccess.m_FileLen)
138 return FALSE;
139 if (m_BufferOffset == (FX_DWORD)-1 || pos < m_BufferOffset ||
140 pos >= m_BufferOffset + 512) {
141 // Need to read from file access
142 m_BufferOffset = pos;
143 int size = 512;
144 if (pos + 512 > m_FileAccess.m_FileLen)
145 size = m_FileAccess.m_FileLen - pos;
146 if (!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, m_BufferOffset, m_Buffer,
147 size))
148 return FALSE;
149 }
150 ch = m_Buffer[pos - m_BufferOffset];
151 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700152}
153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154FX_BOOL CPDF_CustomAccess::GetBlock(FX_DWORD pos,
155 uint8_t* pBuf,
156 FX_DWORD size) {
157 if (pos + size > m_FileAccess.m_FileLen)
158 return FALSE;
159 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, pos, pBuf, size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160}
161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer,
163 FX_FILESIZE offset,
164 size_t size) {
165 if (offset < 0) {
166 return FALSE;
167 }
168 FX_SAFE_FILESIZE newPos =
169 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
170 newPos += offset;
171 if (!newPos.IsValid() || newPos.ValueOrDie() > m_FileAccess.m_FileLen) {
172 return FALSE;
173 }
174 return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, (uint8_t*)buffer,
175 size);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176}
177
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179static FX_DWORD foxit_sandbox_policy = 0xFFFFFFFF;
180
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
182 switch (policy) {
183 case FPDF_POLICY_MACHINETIME_ACCESS: {
184 if (enable)
185 foxit_sandbox_policy |= 0x01;
186 else
187 foxit_sandbox_policy &= 0xFFFFFFFE;
188 } break;
189 default:
190 break;
191 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192}
193
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
195 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700196 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700197 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 default:
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700199 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201}
202
Tom Sepez2c286192015-06-18 12:47:11 -0700203CCodec_ModuleMgr* g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700206 FPDF_InitLibraryWithConfig(nullptr);
207}
208
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800209DLLEXPORT void STDCALL FPDF_InitLibraryWithConfig(
210 const FPDF_LIBRARY_CONFIG* cfg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212
Lei Zhang6f62d532015-09-23 15:31:44 -0700213 CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700215
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 CPDF_ModuleMgr::Create();
217 CPDF_ModuleMgr::Get()->SetCodecModule(g_pCodecModule);
218 CPDF_ModuleMgr::Get()->InitPageModule();
219 CPDF_ModuleMgr::Get()->InitRenderModule();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 CPDFXFA_App::GetInstance()->Initialize();
Tom Sepez452b4f32015-10-13 09:27:27 -0700221 if (cfg && cfg->version >= 2)
222 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223}
224
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
226 CPDFXFA_App::ReleaseInstance();
227 CPDF_ModuleMgr::Destroy();
228 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700229
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 delete g_pCodecModule;
231 g_pCodecModule = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232}
233
234#ifndef _WIN32
235int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236void SetLastError(int err) {
237 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238}
239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240int GetLastError() {
241 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700242}
243#endif
244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245void ProcessParseError(FX_DWORD err_code) {
246 // Translate FPDFAPI error code to FPDFVIEW error code
247 switch (err_code) {
248 case PDFPARSE_ERROR_FILE:
249 err_code = FPDF_ERR_FILE;
250 break;
251 case PDFPARSE_ERROR_FORMAT:
252 err_code = FPDF_ERR_FORMAT;
253 break;
254 case PDFPARSE_ERROR_PASSWORD:
255 err_code = FPDF_ERR_PASSWORD;
256 break;
257 case PDFPARSE_ERROR_HANDLER:
258 err_code = FPDF_ERR_SECURITY;
259 break;
260 }
261 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262}
263
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
265 FPDF_BOOL enable) {
266 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267}
268
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
270 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700271 // NOTE: the creation of the file needs to be by the embedder on the
272 // other side of this API.
273 IFX_FileRead* pFileAccess = FX_CreateFileRead((const FX_CHAR*)file_path);
274 if (!pFileAccess) {
275 return nullptr;
276 }
277
278 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700280
Tom Sepeze3166a82015-08-05 10:50:32 -0700281 FX_DWORD err_code = pParser->StartParse(pFileAccess);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 if (err_code) {
283 delete pParser;
284 ProcessParseError(err_code);
285 return NULL;
286 }
287 CPDF_Document* pPDFDoc = pParser->GetDocument();
288 if (!pPDFDoc)
289 return NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700290
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Lei Zhangcb78ef52015-10-02 10:10:49 -0700292 return new CPDFXFA_Document(pPDFDoc, pProvider);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700293}
Jun Fange118ce92015-02-17 06:50:08 -0800294
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
296 int* docType) {
297 if (!document)
298 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800299
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 CPDF_Document* pdfDoc =
301 (static_cast<CPDFXFA_Document*>(document))->GetPDFDoc();
302 if (!pdfDoc)
303 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800304
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
306 if (!pRoot)
307 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800308
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");
310 if (!pAcroForm)
311 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800312
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
314 if (!pXFA)
315 return FALSE;
JUN FANG827a1722015-03-05 13:39:21 -0800316
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 FX_BOOL bDynamicXFA = pRoot->GetBoolean("NeedsRendering", FALSE);
JUN FANG827a1722015-03-05 13:39:21 -0800318
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 if (bDynamicXFA)
320 *docType = DOCTYPE_DYNIMIC_XFA;
321 else
322 *docType = DOCTYPE_STATIC_XFA;
JUN FANG827a1722015-03-05 13:39:21 -0800323
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 return TRUE;
Jun Fange118ce92015-02-17 06:50:08 -0800325}
326
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
328 return document && (static_cast<CPDFXFA_Document*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700329}
330
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331class CMemFile final : public IFX_FileRead {
332 public:
333 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334
Lei Zhang3884dba2015-10-19 17:27:53 -0700335 void Release() override { delete this; }
336 FX_FILESIZE GetSize() override { return m_size; }
337 FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 if (offset < 0) {
339 return FALSE;
340 }
341 FX_SAFE_FILESIZE newPos =
342 pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
343 newPos += offset;
344 if (!newPos.IsValid() || newPos.ValueOrDie() > (FX_DWORD)m_size) {
345 return FALSE;
346 }
347 FXSYS_memcpy(buffer, m_pBuf + offset, size);
348 return TRUE;
349 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700350
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 private:
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700352 ~CMemFile() override {}
353
Lei Zhang3884dba2015-10-19 17:27:53 -0700354 uint8_t* const m_pBuf;
355 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700356};
Lei Zhang3884dba2015-10-19 17:27:53 -0700357
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
359 int size,
360 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700361 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 pParser->SetPassword(password);
363 CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
364 FX_DWORD err_code = pParser->StartParse(pMemFile);
365 if (err_code) {
366 delete pParser;
367 ProcessParseError(err_code);
368 return NULL;
369 }
370 CPDF_Document* pDoc = NULL;
371 pDoc = pParser ? pParser->GetDocument() : NULL;
372 CheckUnSupportError(pDoc, err_code);
Tom Sepezbf59a072015-10-21 14:07:23 -0700373 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700374}
375
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376DLLEXPORT FPDF_DOCUMENT STDCALL
377FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
378 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700379 CPDF_Parser* pParser = new CPDF_Parser;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 pParser->SetPassword(password);
Tom Sepezae51c812015-08-05 12:34:06 -0700381 CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 FX_DWORD err_code = pParser->StartParse(pFile);
383 if (err_code) {
384 delete pParser;
385 ProcessParseError(err_code);
386 return NULL;
387 }
388 CPDF_Document* pDoc = NULL;
389 pDoc = pParser ? pParser->GetDocument() : NULL;
390 CheckUnSupportError(pDoc, err_code);
Tom Sepezbf59a072015-10-21 14:07:23 -0700391 return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392}
393
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
395 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700396 if (!fileVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700398
Tom Sepez471a1032015-10-15 16:17:18 -0700399 *fileVersion = 0;
400 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
401 if (!pDoc)
402 return FALSE;
403
404 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 if (!pParser)
406 return FALSE;
Tom Sepez471a1032015-10-15 16:17:18 -0700407
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408 *fileVersion = pParser->GetFileVersion();
409 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700410}
411
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412// jabdelmalek: changed return type from FX_DWORD to build on Linux (and match
413// header).
414DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700415 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
416 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417 return (FX_DWORD)-1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700418
Tom Sepez471a1032015-10-15 16:17:18 -0700419 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
420 return pDict ? pDict->GetInteger("P") : (FX_DWORD)-1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700421}
422
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700424 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
425 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700427
Tom Sepez471a1032015-10-15 16:17:18 -0700428 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
429 return pDict ? pDict->GetInteger("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700430}
431
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800433 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700434 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435}
436
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
438 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800439 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
440 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700441 return nullptr;
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700442 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700443 return nullptr;
444
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445 return pDoc->GetPage(page_index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700446}
447
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800449 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700450 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700451}
452
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800454 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700455 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700456}
457
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458void DropContext(void* data) {
459 delete (CRenderContext*)data;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700460}
461
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700462#if defined(_DEBUG) || defined(DEBUG)
463#define DEBUG_TRACE
464#endif
465
466#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
468 FPDF_PAGE page,
469 int start_x,
470 int start_y,
471 int size_x,
472 int size_y,
473 int rotate,
474 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700475 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 if (!pPage)
477 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700478
Tom Sepezae51c812015-08-05 12:34:06 -0700479 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 pPage->SetPrivateData((void*)1, pContext, DropContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700481
482#ifndef _WIN32_WCE
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483 CFX_DIBitmap* pBitmap = NULL;
484 FX_BOOL bBackgroundAlphaNeeded = FALSE;
485 bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();
486 if (bBackgroundAlphaNeeded) {
Tom Sepezae51c812015-08-05 12:34:06 -0700487 pBitmap = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488 pBitmap->Create(size_x, size_y, FXDIB_Argb);
489 pBitmap->Clear(0x00ffffff);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700490#ifdef _SKIA_SUPPORT_
Tom Sepezae51c812015-08-05 12:34:06 -0700491 pContext->m_pDevice = new CFX_SkiaDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700493#else
Tom Sepezae51c812015-08-05 12:34:06 -0700494 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700496#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497 } else
Tom Sepezae51c812015-08-05 12:34:06 -0700498 pContext->m_pDevice = new CFX_WindowsDevice(dc);
Bo Xud4e406e2014-08-13 11:03:19 -0700499
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
501 rotate, flags, TRUE, NULL);
Bo Xud4e406e2014-08-13 11:03:19 -0700502
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700503 if (bBackgroundAlphaNeeded) {
504 if (pBitmap) {
505 CFX_WindowsDevice WinDC(dc);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700506
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700507 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezae51c812015-08-05 12:34:06 -0700508 CFX_DIBitmap* pDst = new CFX_DIBitmap;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 int pitch = pBitmap->GetPitch();
510 pDst->Create(size_x, size_y, FXDIB_Rgb32);
511 FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
512 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
513 FXDIB_BLEND_NORMAL, NULL, FALSE, NULL);
514 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
515 delete pDst;
516 } else
517 WinDC.SetDIBits(pBitmap, 0, 0);
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700518 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700520#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521 // get clip region
522 RECT rect, cliprect;
523 rect.left = start_x;
524 rect.top = start_y;
525 rect.right = start_x + size_x;
526 rect.bottom = start_y + size_y;
527 GetClipBox(dc, &cliprect);
528 IntersectRect(&rect, &rect, &cliprect);
529 int width = rect.right - rect.left;
530 int height = rect.bottom - rect.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700531
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700532#ifdef DEBUG_TRACE
533 {
534 char str[128];
535 memset(str, 0, sizeof(str));
536 FXSYS_snprintf(str, sizeof(str) - 1, "Rendering DIB %d x %d", width,
537 height);
538 CPDF_ModuleMgr::Get()->ReportError(999, str);
539 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700540#endif
Bo Xud4e406e2014-08-13 11:03:19 -0700541
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700542 // Create a DIB section
543 LPVOID pBuffer;
544 BITMAPINFOHEADER bmih;
545 FXSYS_memset(&bmih, 0, sizeof bmih);
546 bmih.biSize = sizeof bmih;
547 bmih.biBitCount = 24;
548 bmih.biHeight = -height;
549 bmih.biPlanes = 1;
550 bmih.biWidth = width;
551 pContext->m_hBitmap = CreateDIBSection(dc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
552 &pBuffer, NULL, 0);
553 if (pContext->m_hBitmap == NULL) {
554#if defined(DEBUG) || defined(_DEBUG)
555 char str[128];
556 memset(str, 0, sizeof(str));
557 FXSYS_snprintf(str, sizeof(str) - 1,
558 "Error CreateDIBSection: %d x %d, error code = %d", width,
559 height, GetLastError());
560 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700561#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700563#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564 }
565 FXSYS_memset(pBuffer, 0xff, height * ((width * 3 + 3) / 4 * 4));
566
567#ifdef DEBUG_TRACE
568 { CPDF_ModuleMgr::Get()->ReportError(999, "DIBSection created"); }
569#endif
570
571 // Create a device with this external buffer
572 pContext->m_pBitmap = new CFX_DIBitmap;
573 pContext->m_pBitmap->Create(width, height, FXDIB_Rgb, (uint8_t*)pBuffer);
574 pContext->m_pDevice = new CPDF_FxgeDevice;
575 ((CPDF_FxgeDevice*)pContext->m_pDevice)->Attach(pContext->m_pBitmap);
576
577#ifdef DEBUG_TRACE
578 CPDF_ModuleMgr::Get()->ReportError(999, "Ready for PDF rendering");
579#endif
580
581 // output to bitmap device
582 FPDF_RenderPage_Retail(pContext, page, start_x - rect.left,
583 start_y - rect.top, size_x, size_y, rotate, flags);
584
585#ifdef DEBUG_TRACE
586 CPDF_ModuleMgr::Get()->ReportError(999, "Finished PDF rendering");
587#endif
588
589 // Now output to real device
590 HDC hMemDC = CreateCompatibleDC(dc);
591 if (hMemDC == NULL) {
592#if defined(DEBUG) || defined(_DEBUG)
593 char str[128];
594 memset(str, 0, sizeof(str));
595 FXSYS_snprintf(str, sizeof(str) - 1,
596 "Error CreateCompatibleDC. Error code = %d", GetLastError());
597 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
598#else
599 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
600#endif
601 }
602
603 HGDIOBJ hOldBitmap = SelectObject(hMemDC, pContext->m_hBitmap);
604
605#ifdef DEBUG_TRACE
606 CPDF_ModuleMgr::Get()->ReportError(999, "Ready for screen rendering");
607#endif
608
609 BitBlt(dc, rect.left, rect.top, width, height, hMemDC, 0, 0, SRCCOPY);
610 SelectObject(hMemDC, hOldBitmap);
611 DeleteDC(hMemDC);
612
613#ifdef DEBUG_TRACE
614 CPDF_ModuleMgr::Get()->ReportError(999, "Finished screen rendering");
615#endif
616
617#endif
618 if (bBackgroundAlphaNeeded) {
619 delete pBitmap;
620 pBitmap = NULL;
621 }
622 delete pContext;
623 pPage->RemovePrivateData((void*)1);
624}
625#endif
626
627DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
628 FPDF_PAGE page,
629 int start_x,
630 int start_y,
631 int size_x,
632 int size_y,
633 int rotate,
634 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700635 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700636 return;
Tom Sepezdb0be962015-10-16 14:00:21 -0700637 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 if (!pPage)
639 return;
Tom Sepezae51c812015-08-05 12:34:06 -0700640 CRenderContext* pContext = new CRenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700641 pPage->SetPrivateData((void*)1, pContext, DropContext);
642#ifdef _SKIA_SUPPORT_
Tom Sepezae51c812015-08-05 12:34:06 -0700643 pContext->m_pDevice = new CFX_SkiaDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644
645 if (flags & FPDF_REVERSE_BYTE_ORDER)
646 ((CFX_SkiaDevice*)pContext->m_pDevice)
647 ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
648 else
649 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
650#else
Tom Sepezae51c812015-08-05 12:34:06 -0700651 pContext->m_pDevice = new CFX_FxgeDevice;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652
653 if (flags & FPDF_REVERSE_BYTE_ORDER)
654 ((CFX_FxgeDevice*)pContext->m_pDevice)
655 ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
656 else
657 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
658#endif
659
660 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
661 rotate, flags, TRUE, NULL);
662
663 delete pContext;
664 pPage->RemovePrivateData((void*)1);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700665}
666
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
668 if (!page)
669 return;
670
671 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
672 pPage->Release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700673}
674
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700676 delete CPDFDocumentFromFPDFDocument(document);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700677}
678
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
680 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700681}
682
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700683DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
684 int start_x,
685 int start_y,
686 int size_x,
687 int size_y,
688 int rotate,
689 int device_x,
690 int device_y,
691 double* page_x,
692 double* page_y) {
693 if (page == NULL || page_x == NULL || page_y == NULL)
694 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800695 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700696 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
697 device_y, page_x, page_y);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700698}
699
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
701 int start_x,
702 int start_y,
703 int size_x,
704 int size_y,
705 int rotate,
706 double page_x,
707 double page_y,
708 int* device_x,
709 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700710 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800712 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700713 if (!pPage)
714 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
716 device_x, device_y);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700717}
718
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
720 int height,
721 int alpha) {
722 nonstd::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
723 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
724 return NULL;
725 }
726 return pBitmap.release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700727}
728
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700729DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
730 int height,
731 int format,
732 void* first_scan,
733 int stride) {
734 FXDIB_Format fx_format;
735 switch (format) {
736 case FPDFBitmap_Gray:
737 fx_format = FXDIB_8bppRgb;
738 break;
739 case FPDFBitmap_BGR:
740 fx_format = FXDIB_Rgb;
741 break;
742 case FPDFBitmap_BGRx:
743 fx_format = FXDIB_Rgb32;
744 break;
745 case FPDFBitmap_BGRA:
746 fx_format = FXDIB_Argb;
747 break;
748 default:
749 return NULL;
750 }
751 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
752 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
753 return pBitmap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700754}
755
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
757 int left,
758 int top,
759 int width,
760 int height,
761 FPDF_DWORD color) {
762 if (bitmap == NULL)
763 return;
764#ifdef _SKIA_SUPPORT_
765 CFX_SkiaDevice device;
766#else
767 CFX_FxgeDevice device;
768#endif
769 device.Attach((CFX_DIBitmap*)bitmap);
770 if (!((CFX_DIBitmap*)bitmap)->HasAlpha())
771 color |= 0xFF000000;
772 FX_RECT rect(left, top, left + width, top + height);
773 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700774}
775
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700776DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
777 if (bitmap == NULL)
778 return NULL;
779 return ((CFX_DIBitmap*)bitmap)->GetBuffer();
Bo Xu9114e832014-07-14 13:22:47 -0700780}
781
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700782DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
783 if (bitmap == NULL)
784 return 0;
785 return ((CFX_DIBitmap*)bitmap)->GetWidth();
Bo Xu9114e832014-07-14 13:22:47 -0700786}
787
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700788DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
789 if (bitmap == NULL)
790 return 0;
791 return ((CFX_DIBitmap*)bitmap)->GetHeight();
792}
793
794DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
795 if (bitmap == NULL)
796 return 0;
797 return ((CFX_DIBitmap*)bitmap)->GetPitch();
798}
799
800DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
801 delete (CFX_DIBitmap*)bitmap;
802}
803
804void FPDF_RenderPage_Retail(CRenderContext* pContext,
805 FPDF_PAGE page,
806 int start_x,
807 int start_y,
808 int size_x,
809 int size_y,
810 int rotate,
811 int flags,
812 FX_BOOL bNeedToRestore,
813 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700814 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
815 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 return;
817
818 if (!pContext->m_pOptions)
819 pContext->m_pOptions = new CPDF_RenderOptions;
820
821 if (flags & FPDF_LCD_TEXT)
822 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
823 else
824 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
825 if (flags & FPDF_NO_NATIVETEXT)
826 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
827 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
828 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
829 if (flags & FPDF_RENDER_FORCEHALFTONE)
830 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
831 // Grayscale output
832 if (flags & FPDF_GRAYSCALE) {
833 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
834 pContext->m_pOptions->m_ForeColor = 0;
835 pContext->m_pOptions->m_BackColor = 0xffffff;
836 }
837 const CPDF_OCContext::UsageType usage =
838 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
839 pContext->m_pOptions->m_AddFlags = flags >> 8;
840 pContext->m_pOptions->m_pOCContext =
841 new CPDF_OCContext(pPage->m_pDocument, usage);
842
843 CFX_AffineMatrix matrix;
844 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
845
846 FX_RECT clip;
847 clip.left = start_x;
848 clip.right = start_x + size_x;
849 clip.top = start_y;
850 clip.bottom = start_y + size_y;
851 pContext->m_pDevice->SaveState();
852 pContext->m_pDevice->SetClip_Rect(&clip);
853
Tom Sepezae51c812015-08-05 12:34:06 -0700854 pContext->m_pContext = new CPDF_RenderContext;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700855 pContext->m_pContext->Create(pPage);
856 pContext->m_pContext->AppendObjectList(pPage, &matrix);
857
858 if (flags & FPDF_ANNOT) {
Tom Sepezae51c812015-08-05 12:34:06 -0700859 pContext->m_pAnnots = new CPDF_AnnotList(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860 FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
861 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext, bPrinting,
862 &matrix, TRUE, NULL);
863 }
864
Tom Sepezb3b67622015-10-19 16:20:03 -0700865 pContext->m_pRenderer = new CPDF_ProgressiveRenderer(
866 pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions);
867 pContext->m_pRenderer->Start(pause);
Tom Sepezc7e4c4f2015-11-20 09:45:24 -0800868 if (bNeedToRestore)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700869 pContext->m_pDevice->RestoreState();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700870}
871
872DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
873 int page_index,
874 double* width,
875 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800876 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
877 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878 return FALSE;
879
880 int count = pDoc->GetPageCount();
881 if (page_index < 0 || page_index >= count)
882 return FALSE;
883
884 CPDFXFA_Page* pPage = pDoc->GetPage(page_index);
885 if (!pPage)
886 return FALSE;
887
888 *width = pPage->GetPageWidth();
889 *height = pPage->GetPageHeight();
890
891 return TRUE;
892}
893
894DLLEXPORT FPDF_BOOL STDCALL
895FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700896 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897 if (!pDoc)
898 return TRUE;
Tom Sepez471a1032015-10-15 16:17:18 -0700899 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900 return viewRef.PrintScaling();
901}
902
903DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700904 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905 if (!pDoc)
906 return 1;
907 CPDF_ViewerPreferences viewRef(pDoc);
908 return viewRef.NumCopies();
909}
910
911DLLEXPORT FPDF_PAGERANGE STDCALL
912FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700913 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914 if (!pDoc)
915 return NULL;
916 CPDF_ViewerPreferences viewRef(pDoc);
917 return viewRef.PrintPageRange();
918}
919
920DLLEXPORT FPDF_DUPLEXTYPE STDCALL
921FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700922 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700923 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -0700924 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700925 CPDF_ViewerPreferences viewRef(pDoc);
926 CFX_ByteString duplex = viewRef.Duplex();
927 if (FX_BSTRC("Simplex") == duplex)
928 return Simplex;
929 if (FX_BSTRC("DuplexFlipShortEdge") == duplex)
930 return DuplexFlipShortEdge;
931 if (FX_BSTRC("DuplexFlipLongEdge") == duplex)
932 return DuplexFlipLongEdge;
933 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -0700934}
935
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700937 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
938 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700939 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -0800940
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941 CPDF_Dictionary* pRoot = pDoc->GetRoot();
942 if (!pRoot)
943 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -0800944
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 CPDF_NameTree nameTree(pDoc, FX_BSTRC("Dests"));
946 int count = nameTree.GetCount();
947 CPDF_Dictionary* pDest = pRoot->GetDict(FX_BSTRC("Dests"));
948 if (pDest)
949 count += pDest->GetCount();
950 return count;
951}
952
953DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
954 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -0700956 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957
Tom Sepez471a1032015-10-15 16:17:18 -0700958 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
959 if (!pDoc)
960 return nullptr;
961
962 CPDF_NameTree name_tree(pDoc, FX_BSTRC("Dests"));
963 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964}
965
966FPDF_RESULT FPDF_BStr_Init(FPDF_BSTR* str) {
967 if (!str)
968 return -1;
969
970 FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
971 return 0;
972}
973
974FPDF_RESULT FPDF_BStr_Set(FPDF_BSTR* str, FPDF_LPCSTR bstr, int length) {
975 if (!str)
976 return -1;
977 if (!bstr || !length)
978 return -1;
979 if (length == -1)
980 length = FXSYS_strlen(bstr);
981
982 if (length == 0) {
983 if (str->str) {
984 FX_Free(str->str);
985 str->str = NULL;
986 }
987 str->len = 0;
988 return 0;
989 }
990
991 if (str->str && str->len < length)
992 str->str = FX_Realloc(char, str->str, length + 1);
993 else if (!str->str)
994 str->str = FX_Alloc(char, length + 1);
995
996 str->str[length] = 0;
997 if (str->str == NULL)
998 return -1;
999
1000 FXSYS_memcpy(str->str, bstr, length);
1001 str->len = length;
1002
1003 return 0;
1004}
1005
1006FPDF_RESULT FPDF_BStr_Clear(FPDF_BSTR* str) {
1007 if (!str)
1008 return -1;
1009
1010 if (str->str) {
1011 FX_Free(str->str);
1012 str->str = NULL;
1013 }
1014 str->len = 0;
1015 return 0;
1016}
1017
1018DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1019 int index,
1020 void* buffer,
1021 long* buflen) {
1022 if (!buffer)
1023 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001024
1025 if (index < 0)
1026 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027
Tom Sepezbf59a072015-10-21 14:07:23 -07001028 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001029 if (!pDoc)
1030 return nullptr;
1031
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1033 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001034 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035
1036 CPDF_Object* pDestObj = NULL;
1037 CFX_ByteString bsName;
1038 CPDF_NameTree nameTree(pDoc, FX_BSTRC("Dests"));
1039 int count = nameTree.GetCount();
1040 if (index >= count) {
Bo Xu4d62b6b2015-01-10 22:52:59 -08001041 CPDF_Dictionary* pDest = pRoot->GetDict(FX_BSTRC("Dests"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042 if (!pDest)
1043 return NULL;
1044 if (index >= count + pDest->GetCount())
1045 return NULL;
1046 index -= count;
1047 FX_POSITION pos = pDest->GetStartPos();
1048 int i = 0;
1049 while (pos) {
1050 pDestObj = pDest->GetNextElement(pos, bsName);
1051 if (!pDestObj)
1052 continue;
1053 if (i == index)
1054 break;
1055 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001056 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057 } else {
1058 pDestObj = nameTree.LookupValue(index, bsName);
1059 }
1060 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001061 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001062 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
1063 pDestObj = pDict->GetArray(FX_BSTRC("D"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001064 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001065 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001067 if (!pDestObj->IsArray())
1068 return nullptr;
1069
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001070 CFX_WideString wsName = PDF_DecodeText(bsName);
1071 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
1072 unsigned int len = utf16Name.GetLength();
1073 if (!buffer) {
1074 *buflen = len;
1075 } else if (*buflen >= len) {
1076 memcpy(buffer, utf16Name.c_str(), len);
1077 *buflen = len;
1078 } else {
1079 *buflen = -1;
1080 }
1081 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001082}