blob: 8aa6ec0fb96e184df6c0e6e613cb40605afb1968 [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>
thestigd4c34f22016-09-28 17:04:51 -070010#include <utility>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080011
dsinclair39c62fd2016-09-29 12:49:17 -070012#include "core/fpdfapi/cpdf_modulemgr.h"
13#include "core/fpdfapi/cpdf_pagerendercontext.h"
dsinclair41872fa2016-10-04 11:29:35 -070014#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070015#include "core/fpdfapi/parser/cpdf_array.h"
16#include "core/fpdfapi/parser/cpdf_document.h"
17#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair69d9c682016-10-04 12:18:35 -070018#include "core/fpdfapi/render/cpdf_progressiverenderer.h"
19#include "core/fpdfapi/render/cpdf_renderoptions.h"
dsinclair1727aee2016-09-29 13:12:56 -070020#include "core/fpdfdoc/cpdf_annotlist.h"
21#include "core/fpdfdoc/cpdf_nametree.h"
22#include "core/fpdfdoc/cpdf_occontext.h"
23#include "core/fpdfdoc/cpdf_viewerpreferences.h"
dsinclair8a4e2862016-09-29 13:43:30 -070024#include "core/fxcodec/fx_codec.h"
dsinclaira52ab742016-09-29 13:59:29 -070025#include "core/fxcrt/fx_memory.h"
26#include "core/fxcrt/fx_safe_types.h"
dsinclair74a34fc2016-09-29 16:41:42 -070027#include "core/fxge/cfx_fxgedevice.h"
28#include "core/fxge/cfx_gemodule.h"
dsinclairb402b172016-10-11 09:26:32 -070029#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070030#include "fpdfsdk/cpdfsdk_pageview.h"
31#include "fpdfsdk/fsdk_define.h"
32#include "fpdfsdk/fsdk_pauseadapter.h"
dsinclair64376be2016-03-31 20:03:24 -070033#include "fpdfsdk/javascript/ijs_runtime.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080034#include "public/fpdf_ext.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080035#include "public/fpdf_progressive.h"
Lei Zhang8241df72015-11-06 14:38:48 -080036#include "third_party/base/numerics/safe_conversions_impl.h"
tsepez36eb4bd2016-10-03 15:24:27 -070037#include "third_party/base/ptr_util.h"
Bo Xufdc00a72014-10-28 23:03:33 -070038
Tom Sepez40e9ff32015-11-30 12:39:54 -080039#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070040#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070041#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
42#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080043#include "public/fpdf_formfill.h"
dsinclair8837c912016-11-01 11:22:37 -070044#include "xfa/fxbarcode/BC_Library.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080045#endif // PDF_ENABLE_XFA
46
thestig25fa42f2016-05-25 21:39:46 -070047#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
dsinclair74a34fc2016-09-29 16:41:42 -070048#include "core/fxge/cfx_windowsdevice.h"
thestig25fa42f2016-05-25 21:39:46 -070049#endif
50
thestiga78ba602016-11-23 15:25:48 -080051namespace {
52
tsepez02759102016-12-01 08:29:25 -080053// Also indicates whether library is currently initialized.
54CCodec_ModuleMgr* g_pCodecModule = nullptr;
55
thestiga78ba602016-11-23 15:25:48 -080056void RenderPageImpl(CPDF_PageRenderContext* pContext,
57 CPDF_Page* pPage,
58 const CFX_Matrix& matrix,
59 const FX_RECT& clipping_rect,
60 int flags,
61 bool bNeedToRestore,
62 IFSDK_PAUSE_Adapter* pause) {
63 if (!pContext->m_pOptions)
64 pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
65
66 if (flags & FPDF_LCD_TEXT)
67 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
68 else
69 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
70
71 if (flags & FPDF_NO_NATIVETEXT)
72 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
73 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
74 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
75 if (flags & FPDF_RENDER_FORCEHALFTONE)
76 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
77#ifndef PDF_ENABLE_XFA
78 if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
79 pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
80 if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
81 pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
82 if (flags & FPDF_RENDER_NO_SMOOTHPATH)
83 pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
84#endif // PDF_ENABLE_XFA
85
86 // Grayscale output
87 if (flags & FPDF_GRAYSCALE) {
88 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
89 pContext->m_pOptions->m_ForeColor = 0;
90 pContext->m_pOptions->m_BackColor = 0xffffff;
91 }
92
93 const CPDF_OCContext::UsageType usage =
94 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
95 pContext->m_pOptions->m_AddFlags = flags >> 8;
96 pContext->m_pOptions->m_pOCContext =
Tom Sepezf716f0b2017-01-30 10:04:07 -080097 pdfium::MakeRetain<CPDF_OCContext>(pPage->m_pDocument, usage);
thestiga78ba602016-11-23 15:25:48 -080098
99 pContext->m_pDevice->SaveState();
100 pContext->m_pDevice->SetClip_Rect(clipping_rect);
101
102 pContext->m_pContext = pdfium::MakeUnique<CPDF_RenderContext>(pPage);
103 pContext->m_pContext->AppendLayer(pPage, &matrix);
104
105 if (flags & FPDF_ANNOT) {
106 pContext->m_pAnnots = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
107 bool bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
108 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext.get(),
109 bPrinting, &matrix, false, nullptr);
110 }
111
112 pContext->m_pRenderer = pdfium::MakeUnique<CPDF_ProgressiveRenderer>(
113 pContext->m_pContext.get(), pContext->m_pDevice.get(),
114 pContext->m_pOptions.get());
115 pContext->m_pRenderer->Start(pause);
116 if (bNeedToRestore)
117 pContext->m_pDevice->RestoreState(false);
118}
119
tsepezbea04972016-12-01 13:54:42 -0800120class CPDF_CustomAccess final : public IFX_SeekableReadStream {
121 public:
tsepez833619b2016-12-07 09:21:17 -0800122 static CFX_RetainPtr<CPDF_CustomAccess> Create(FPDF_FILEACCESS* pFileAccess) {
123 return CFX_RetainPtr<CPDF_CustomAccess>(new CPDF_CustomAccess(pFileAccess));
124 }
tsepezbea04972016-12-01 13:54:42 -0800125
126 // IFX_SeekableReadStream
127 FX_FILESIZE GetSize() override;
tsepezbea04972016-12-01 13:54:42 -0800128 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
129
130 private:
tsepez833619b2016-12-07 09:21:17 -0800131 explicit CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess);
132
tsepezbea04972016-12-01 13:54:42 -0800133 FPDF_FILEACCESS m_FileAccess;
134};
135
136CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
137 : m_FileAccess(*pFileAccess) {}
138
139FX_FILESIZE CPDF_CustomAccess::GetSize() {
140 return m_FileAccess.m_FileLen;
141}
142
tsepezbea04972016-12-01 13:54:42 -0800143bool CPDF_CustomAccess::ReadBlock(void* buffer,
144 FX_FILESIZE offset,
145 size_t size) {
146 if (offset < 0)
147 return false;
148
149 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
150 newPos += offset;
151 if (!newPos.IsValid() ||
152 newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) {
153 return false;
154 }
155 return !!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,
156 reinterpret_cast<uint8_t*>(buffer), size);
157}
158
Tom Sepez40e9ff32015-11-30 12:39:54 -0800159#ifdef PDF_ENABLE_XFA
tsepezfa89a202016-12-02 09:48:30 -0800160class CFPDF_FileStream : public IFX_SeekableStream {
161 public:
tsepez833619b2016-12-07 09:21:17 -0800162 static CFX_RetainPtr<CFPDF_FileStream> Create(FPDF_FILEHANDLER* pFS) {
163 return CFX_RetainPtr<CFPDF_FileStream>(new CFPDF_FileStream(pFS));
164 }
165 ~CFPDF_FileStream() override;
Tom Sepez471a1032015-10-15 16:17:18 -0700166
tsepezfa89a202016-12-02 09:48:30 -0800167 // IFX_SeekableStream:
tsepezfa89a202016-12-02 09:48:30 -0800168 FX_FILESIZE GetSize() override;
169 bool IsEOF() override;
170 FX_FILESIZE GetPosition() override;
171 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
172 size_t ReadBlock(void* buffer, size_t size) override;
173 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override;
174 bool Flush() override;
Tom Sepezbf59a072015-10-21 14:07:23 -0700175
tsepezfa89a202016-12-02 09:48:30 -0800176 void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; }
Tom Sepezdb0be962015-10-16 14:00:21 -0700177
tsepezfa89a202016-12-02 09:48:30 -0800178 protected:
tsepez833619b2016-12-07 09:21:17 -0800179 explicit CFPDF_FileStream(FPDF_FILEHANDLER* pFS);
180
tsepezfa89a202016-12-02 09:48:30 -0800181 FPDF_FILEHANDLER* m_pFS;
182 FX_FILESIZE m_nCurPos;
183};
thestigbefa4502016-05-26 20:15:19 -0700184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
186 m_pFS = pFS;
187 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700188}
189
tsepez833619b2016-12-07 09:21:17 -0800190CFPDF_FileStream::~CFPDF_FileStream() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 if (m_pFS && m_pFS->Release)
192 m_pFS->Release(m_pFS->clientData);
Bo Xufdc00a72014-10-28 23:03:33 -0700193}
194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195FX_FILESIZE CFPDF_FileStream::GetSize() {
196 if (m_pFS && m_pFS->GetSize)
197 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
198 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700199}
200
tsepezf39074c2016-10-26 15:33:58 -0700201bool CFPDF_FileStream::IsEOF() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -0700203}
204
weili625ad662016-06-15 11:21:33 -0700205FX_FILESIZE CFPDF_FileStream::GetPosition() {
206 return m_nCurPos;
207}
208
tsepezf39074c2016-10-26 15:33:58 -0700209bool CFPDF_FileStream::ReadBlock(void* buffer,
210 FX_FILESIZE offset,
211 size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 if (!buffer || !size || !m_pFS->ReadBlock)
tsepezf39074c2016-10-26 15:33:58 -0700213 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700214
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
216 (FPDF_DWORD)size) == 0) {
217 m_nCurPos = offset + size;
tsepezf39074c2016-10-26 15:33:58 -0700218 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 }
tsepezf39074c2016-10-26 15:33:58 -0700220 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700221}
222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
224 if (!buffer || !size || !m_pFS->ReadBlock)
225 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700226
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 FX_FILESIZE nSize = GetSize();
228 if (m_nCurPos >= nSize)
229 return 0;
230 FX_FILESIZE dwAvail = nSize - m_nCurPos;
231 if (dwAvail < (FX_FILESIZE)size)
232 size = (size_t)dwAvail;
233 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
234 (FPDF_DWORD)size) == 0) {
235 m_nCurPos += size;
236 return size;
237 }
Bo Xufdc00a72014-10-28 23:03:33 -0700238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700240}
241
tsepezf39074c2016-10-26 15:33:58 -0700242bool CFPDF_FileStream::WriteBlock(const void* buffer,
243 FX_FILESIZE offset,
244 size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 if (!m_pFS || !m_pFS->WriteBlock)
tsepezf39074c2016-10-26 15:33:58 -0700246 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
249 (FPDF_DWORD)size) == 0) {
250 m_nCurPos = offset + size;
tsepezf39074c2016-10-26 15:33:58 -0700251 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 }
tsepezf39074c2016-10-26 15:33:58 -0700253 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700254}
255
tsepezf39074c2016-10-26 15:33:58 -0700256bool CFPDF_FileStream::Flush() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 if (!m_pFS || !m_pFS->Flush)
tsepezf39074c2016-10-26 15:33:58 -0700258 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700261}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800262#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263
tsepezfa89a202016-12-02 09:48:30 -0800264} // namespace
265
266UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
267 return static_cast<UnderlyingDocumentType*>(doc);
268}
269
270FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
271 return static_cast<FPDF_DOCUMENT>(doc);
272}
273
274UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
275 return static_cast<UnderlyingPageType*>(page);
276}
277
278CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
279#ifdef PDF_ENABLE_XFA
280 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
281#else // PDF_ENABLE_XFA
282 return UnderlyingFromFPDFDocument(doc);
283#endif // PDF_ENABLE_XFA
284}
285
286FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
287#ifdef PDF_ENABLE_XFA
288 return doc ? FPDFDocumentFromUnderlying(
289 new CPDFXFA_Context(pdfium::WrapUnique(doc)))
290 : nullptr;
291#else // PDF_ENABLE_XFA
292 return FPDFDocumentFromUnderlying(doc);
293#endif // PDF_ENABLE_XFA
294}
295
296CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
297#ifdef PDF_ENABLE_XFA
298 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
299#else // PDF_ENABLE_XFA
300 return UnderlyingFromFPDFPage(page);
301#endif // PDF_ENABLE_XFA
302}
303
304CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
305 return static_cast<CFX_DIBitmap*>(bitmap);
306}
307
tsepez833619b2016-12-07 09:21:17 -0800308CFX_RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream(
309 FPDF_FILEACCESS* pFileAccess) {
310 return CPDF_CustomAccess::Create(pFileAccess);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700311}
312
tsepezfa89a202016-12-02 09:48:30 -0800313#ifdef PDF_ENABLE_XFA
tsepez833619b2016-12-07 09:21:17 -0800314CFX_RetainPtr<IFX_SeekableStream> MakeSeekableStream(
315 FPDF_FILEHANDLER* pFilehandler) {
316 return CFPDF_FileStream::Create(pFilehandler);
tsepezfa89a202016-12-02 09:48:30 -0800317}
318#endif // PDF_ENABLE_XFA
319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
tsepezc3255f52016-03-25 14:52:27 -0700321static uint32_t foxit_sandbox_policy = 0xFFFFFFFF;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
324 switch (policy) {
325 case FPDF_POLICY_MACHINETIME_ACCESS: {
326 if (enable)
327 foxit_sandbox_policy |= 0x01;
328 else
329 foxit_sandbox_policy &= 0xFFFFFFFE;
330 } break;
331 default:
332 break;
333 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334}
335
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
337 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700338 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700339 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 default:
tsepez4cf55152016-11-02 14:37:54 -0700341 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343}
344
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700346 FPDF_InitLibraryWithConfig(nullptr);
347}
348
Dan Sinclairf766ad22016-03-14 13:51:24 -0400349DLLEXPORT void STDCALL
350FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
tsepez02759102016-12-01 08:29:25 -0800351 if (g_pCodecModule)
352 return;
353
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355
weili47228ac2016-07-20 10:35:31 -0700356 CFX_GEModule* pModule = CFX_GEModule::Get();
357 pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr, g_pCodecModule);
tsepez02759102016-12-01 08:29:25 -0800358
Tom Sepez1b246282015-11-25 15:15:31 -0800359 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
360 pModuleMgr->SetCodecModule(g_pCodecModule);
361 pModuleMgr->InitPageModule();
dsinclairab5b60e2016-06-23 08:18:36 -0700362 pModuleMgr->LoadEmbeddedGB1CMaps();
363 pModuleMgr->LoadEmbeddedJapan1CMaps();
364 pModuleMgr->LoadEmbeddedCNS1CMaps();
365 pModuleMgr->LoadEmbeddedKorea1CMaps();
dsinclaird647a6b2016-04-26 13:13:20 -0700366
Tom Sepez40e9ff32015-11-30 12:39:54 -0800367#ifdef PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700368 FXJSE_Initialize();
369 BC_Library_Init();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800370#endif // PDF_ENABLE_XFA
Tom Sepez452b4f32015-10-13 09:27:27 -0700371 if (cfg && cfg->version >= 2)
372 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373}
374
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
tsepez02759102016-12-01 08:29:25 -0800376 if (!g_pCodecModule)
377 return;
378
Tom Sepez51da0932015-11-25 16:05:49 -0800379#ifdef PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700380 BC_Library_Destory();
381 FXJSE_Finalize();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800382#endif // PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700383
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 CPDF_ModuleMgr::Destroy();
385 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700386
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 delete g_pCodecModule;
388 g_pCodecModule = nullptr;
thestig2d6dda12016-06-28 07:39:09 -0700389
390 IJS_Runtime::Destroy();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700391}
392
393#ifndef _WIN32
394int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395void SetLastError(int err) {
396 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700397}
398
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399int GetLastError() {
400 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700401}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800402#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700403
Tom Sepezf10ae632016-01-26 14:19:52 -0800404void ProcessParseError(CPDF_Parser::Error err) {
tsepezc3255f52016-03-25 14:52:27 -0700405 uint32_t err_code = FPDF_ERR_SUCCESS;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800407 switch (err) {
408 case CPDF_Parser::SUCCESS:
409 err_code = FPDF_ERR_SUCCESS;
410 break;
411 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 err_code = FPDF_ERR_FILE;
413 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800414 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 err_code = FPDF_ERR_FORMAT;
416 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800417 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 err_code = FPDF_ERR_PASSWORD;
419 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800420 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 err_code = FPDF_ERR_SECURITY;
422 break;
423 }
424 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700425}
426
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
428 FPDF_BOOL enable) {
429 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700430}
431
rbpotterdb764702017-01-12 10:31:43 -0800432#if defined(_WIN32)
433#if defined(PDFIUM_PRINT_TEXT_WITH_GDI)
thestigfdb35ff2016-07-18 13:45:44 -0700434DLLEXPORT void STDCALL
435FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) {
436 g_pdfium_typeface_accessible_func = func;
437}
438
439DLLEXPORT void STDCALL FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) {
440 g_pdfium_print_text_with_gdi = !!use_gdi;
441}
rbpotterdb764702017-01-12 10:31:43 -0800442#endif // PDFIUM_PRINT_TEXT_WITH_GDI
443
444DLLEXPORT FPDF_BOOL STDCALL FPDF_SetPrintPostscriptLevel(int postscript_level) {
445 if (postscript_level != 0 && postscript_level != 2 && postscript_level != 3)
446 return FALSE;
447 g_pdfium_print_postscript_level = postscript_level;
448 return TRUE;
449}
450#endif // defined(_WIN32)
thestigfdb35ff2016-07-18 13:45:44 -0700451
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
453 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700454 // NOTE: the creation of the file needs to be by the embedder on the
455 // other side of this API.
tsepez833619b2016-12-07 09:21:17 -0800456 CFX_RetainPtr<IFX_SeekableReadStream> pFileAccess =
tsepez345d4892016-11-30 15:10:55 -0800457 IFX_SeekableReadStream::CreateFromFilename((const FX_CHAR*)file_path);
458 if (!pFileAccess)
Tom Sepeze3166a82015-08-05 10:50:32 -0700459 return nullptr;
Tom Sepeze3166a82015-08-05 10:50:32 -0700460
tsepez345d4892016-11-30 15:10:55 -0800461 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700463
tsepez345d4892016-11-30 15:10:55 -0800464 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
dsinclair156de022016-08-24 11:58:24 -0700465 CPDF_Parser::Error error =
466 pDocument->GetParser()->StartParse(pFileAccess, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800467 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800468 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700469 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 }
dsinclair8837c912016-11-01 11:22:37 -0700471 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472}
Jun Fange118ce92015-02-17 06:50:08 -0800473
Tom Sepez40e9ff32015-11-30 12:39:54 -0800474#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
476 int* docType) {
477 if (!document)
tsepez4cf55152016-11-02 14:37:54 -0700478 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800479
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 CPDF_Document* pdfDoc =
dsinclair521b7502016-11-02 13:02:28 -0700481 (static_cast<CPDFXFA_Context*>(document))->GetPDFDoc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482 if (!pdfDoc)
tsepez4cf55152016-11-02 14:37:54 -0700483 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800484
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
486 if (!pRoot)
tsepez4cf55152016-11-02 14:37:54 -0700487 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800488
dsinclair38fd8442016-09-15 10:15:32 -0700489 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 if (!pAcroForm)
tsepez4cf55152016-11-02 14:37:54 -0700491 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800492
dsinclair38fd8442016-09-15 10:15:32 -0700493 CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494 if (!pXFA)
tsepez4cf55152016-11-02 14:37:54 -0700495 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800496
dsinclair38fd8442016-09-15 10:15:32 -0700497 bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false);
thestigded36342016-05-23 17:54:02 -0700498 *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA;
tsepez4cf55152016-11-02 14:37:54 -0700499 return true;
Jun Fange118ce92015-02-17 06:50:08 -0800500}
501
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
dsinclair521b7502016-11-02 13:02:28 -0700503 return document && (static_cast<CPDFXFA_Context*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700504}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800505#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700506
tsepezad2441e2016-10-24 10:19:11 -0700507class CMemFile final : public IFX_SeekableReadStream {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 public:
tsepez833619b2016-12-07 09:21:17 -0800509 static CFX_RetainPtr<CMemFile> Create(uint8_t* pBuf, FX_FILESIZE size) {
510 return CFX_RetainPtr<CMemFile>(new CMemFile(pBuf, size));
511 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700512
Lei Zhang3884dba2015-10-19 17:27:53 -0700513 FX_FILESIZE GetSize() override { return m_size; }
tsepezf39074c2016-10-26 15:33:58 -0700514 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
tsepez833619b2016-12-07 09:21:17 -0800515 if (offset < 0)
tsepezf39074c2016-10-26 15:33:58 -0700516 return false;
tsepez833619b2016-12-07 09:21:17 -0800517
tsepez4e597c82016-11-07 15:16:01 -0800518 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519 newPos += offset;
tsepez833619b2016-12-07 09:21:17 -0800520 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size)
tsepezf39074c2016-10-26 15:33:58 -0700521 return false;
tsepez833619b2016-12-07 09:21:17 -0800522
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523 FXSYS_memcpy(buffer, m_pBuf + offset, size);
tsepezf39074c2016-10-26 15:33:58 -0700524 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700526
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527 private:
tsepez833619b2016-12-07 09:21:17 -0800528 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700529
Lei Zhang3884dba2015-10-19 17:27:53 -0700530 uint8_t* const m_pBuf;
531 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700532};
Lei Zhang3884dba2015-10-19 17:27:53 -0700533
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
535 int size,
536 FPDF_BYTESTRING password) {
tsepez833619b2016-12-07 09:21:17 -0800537 CFX_RetainPtr<CMemFile> pMemFile = CMemFile::Create((uint8_t*)data_buf, size);
538 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
tsepez4540fba2016-08-16 11:12:21 -0700539 pParser->SetPassword(password);
540
tsepez833619b2016-12-07 09:21:17 -0800541 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
tsepez4540fba2016-08-16 11:12:21 -0700542 CPDF_Parser::Error error =
dsinclaircedaa552016-08-24 11:12:19 -0700543 pDocument->GetParser()->StartParse(pMemFile, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800544 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800545 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700546 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700547 }
dsinclaircedaa552016-08-24 11:12:19 -0700548 CheckUnSupportError(pDocument.get(), error);
549 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700550}
551
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552DLLEXPORT FPDF_DOCUMENT STDCALL
553FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
554 FPDF_BYTESTRING password) {
tsepez833619b2016-12-07 09:21:17 -0800555 CFX_RetainPtr<CPDF_CustomAccess> pFile =
556 CPDF_CustomAccess::Create(pFileAccess);
557 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
tsepez4540fba2016-08-16 11:12:21 -0700558 pParser->SetPassword(password);
559
tsepez833619b2016-12-07 09:21:17 -0800560 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
dsinclaircedaa552016-08-24 11:12:19 -0700561 CPDF_Parser::Error error =
562 pDocument->GetParser()->StartParse(pFile, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800563 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800564 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700565 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566 }
dsinclaircedaa552016-08-24 11:12:19 -0700567 CheckUnSupportError(pDocument.get(), error);
568 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700569}
570
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700571DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
572 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700573 if (!fileVersion)
tsepez4cf55152016-11-02 14:37:54 -0700574 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700575
Tom Sepez471a1032015-10-15 16:17:18 -0700576 *fileVersion = 0;
577 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
578 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700579 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700580
581 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 if (!pParser)
tsepez4cf55152016-11-02 14:37:54 -0700583 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700584
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585 *fileVersion = pParser->GetFileVersion();
tsepez4cf55152016-11-02 14:37:54 -0700586 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700587}
588
tsepezc3255f52016-03-25 14:52:27 -0700589// jabdelmalek: changed return type from uint32_t to build on Linux (and match
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590// header).
591DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700592 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestig27ddf162016-05-23 15:06:59 -0700593 // https://bugs.chromium.org/p/pdfium/issues/detail?id=499
594 if (!pDoc) {
Tom Sepez51da0932015-11-25 16:05:49 -0800595#ifndef PDF_ENABLE_XFA
596 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800597#else // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700598 return 0xFFFFFFFF;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800599#endif // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700600 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700601
thestig27ddf162016-05-23 15:06:59 -0700602 return pDoc->GetUserPermissions();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700603}
604
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700606 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700607 if (!pDoc || !pDoc->GetParser())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700609
Tom Sepez471a1032015-10-15 16:17:18 -0700610 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
dsinclair38fd8442016-09-15 10:15:32 -0700611 return pDict ? pDict->GetIntegerFor("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700612}
613
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800615 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700616 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700617}
618
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
620 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800621 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
622 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700623 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800624
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700625 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700626 return nullptr;
627
Tom Sepez40e9ff32015-11-30 12:39:54 -0800628#ifdef PDF_ENABLE_XFA
dsinclair9f206f02016-09-20 12:17:42 -0700629 return pDoc->GetXFAPage(page_index);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800630#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800631 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800632 if (!pDict)
thestig5cc24652016-04-26 11:46:02 -0700633 return nullptr;
634
635 CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true);
636 pPage->ParseContent();
Tom Sepez51da0932015-11-25 16:05:49 -0800637 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800638#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700639}
640
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700641DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800642 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700643 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700644}
645
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800647 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700648 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700649}
650
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700651#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
653 FPDF_PAGE page,
654 int start_x,
655 int start_y,
656 int size_x,
657 int size_y,
658 int rotate,
659 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700660 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661 if (!pPage)
662 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700663
weili9f515bc2016-07-24 08:08:24 -0700664 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700665 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700666
thestig84144e82016-09-27 15:06:01 -0700667 std::unique_ptr<CFX_DIBitmap> pBitmap;
rbpotterdb764702017-01-12 10:31:43 -0800668 // TODO: This results in unnecessary rasterization of some PDFs due to
669 // HasImageMask() returning true. If any image on the page is a mask, the
670 // entire page gets rasterized and the spool size gets huge.
thestig84144e82016-09-27 15:06:01 -0700671 const bool bNewBitmap =
672 pPage->BackgroundAlphaNeeded() || pPage->HasImageMask();
673 if (bNewBitmap) {
tsepez36eb4bd2016-10-03 15:24:27 -0700674 pBitmap = pdfium::MakeUnique<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 pBitmap->Create(size_x, size_y, FXDIB_Argb);
676 pBitmap->Clear(0x00ffffff);
thestigbefa4502016-05-26 20:15:19 -0700677 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
tsepez36eb4bd2016-10-03 15:24:27 -0700678 pContext->m_pDevice = pdfium::WrapUnique(pDevice);
thestig84144e82016-09-27 15:06:01 -0700679 pDevice->Attach(pBitmap.get(), false, nullptr, false);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800680 } else {
tsepez36eb4bd2016-10-03 15:24:27 -0700681 pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsDevice>(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800682 }
Bo Xud4e406e2014-08-13 11:03:19 -0700683
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700684 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700685 rotate, flags, true, nullptr);
Bo Xud4e406e2014-08-13 11:03:19 -0700686
thestig84144e82016-09-27 15:06:01 -0700687 if (bNewBitmap) {
688 CFX_WindowsDevice WinDC(dc);
689 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
tsepez36eb4bd2016-10-03 15:24:27 -0700690 std::unique_ptr<CFX_DIBitmap> pDst = pdfium::MakeUnique<CFX_DIBitmap>();
thestig84144e82016-09-27 15:06:01 -0700691 int pitch = pBitmap->GetPitch();
692 pDst->Create(size_x, size_y, FXDIB_Rgb32);
693 FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
694 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap.get(), 0, 0,
tsepez4cf55152016-11-02 14:37:54 -0700695 FXDIB_BLEND_NORMAL, nullptr, false, nullptr);
thestig84144e82016-09-27 15:06:01 -0700696 WinDC.StretchDIBits(pDst.get(), 0, 0, size_x, size_y);
697 } else {
698 WinDC.SetDIBits(pBitmap.get(), 0, 0);
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700699 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700 }
Jun Fang1aeeceb2015-12-29 10:27:44 +0800701
tsepeze5cb0b12016-10-26 15:06:11 -0700702 pPage->SetRenderContext(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700703}
Lei Zhangae85f872016-02-19 14:57:07 -0800704#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700705
706DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
707 FPDF_PAGE page,
708 int start_x,
709 int start_y,
710 int size_x,
711 int size_y,
712 int rotate,
713 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700714 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715 return;
tsepez1e2c5572016-05-25 14:58:09 -0700716
Tom Sepezdb0be962015-10-16 14:00:21 -0700717 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718 if (!pPage)
719 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700720
weili9f515bc2016-07-24 08:08:24 -0700721 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700722 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
thestigbefa4502016-05-26 20:15:19 -0700723 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
weili9f515bc2016-07-24 08:08:24 -0700724 pContext->m_pDevice.reset(pDevice);
thestigbefa4502016-05-26 20:15:19 -0700725 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
726 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727
728 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700729 rotate, flags, true, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730
caryclark687fbde2016-11-22 12:44:25 -0800731#ifdef _SKIA_SUPPORT_PATHS_
caryclark8f875502016-12-06 13:49:34 -0800732 pDevice->Flush();
caryclark687fbde2016-11-22 12:44:25 -0800733 pBitmap->UnPreMultiply();
734#endif
tsepeze5cb0b12016-10-26 15:06:11 -0700735 pPage->SetRenderContext(nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700736}
737
thestiga78ba602016-11-23 15:25:48 -0800738DLLEXPORT void STDCALL FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap,
739 FPDF_PAGE page,
740 const FS_MATRIX* matrix,
741 const FS_RECTF* clipping,
742 int flags) {
743 if (!bitmap)
744 return;
745
746 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
747 if (!pPage)
748 return;
749
750 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
751 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
752 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
753 pContext->m_pDevice.reset(pDevice);
754 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
755 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
756
757 CFX_Matrix transform_matrix = pPage->GetPageMatrix();
758 if (matrix) {
Dan Sinclairbba2a7c2017-02-07 16:36:39 -0500759 transform_matrix.Concat(CFX_Matrix(matrix->a, matrix->b, matrix->c,
760 matrix->d, matrix->e, matrix->f));
thestiga78ba602016-11-23 15:25:48 -0800761 }
762
763 CFX_FloatRect clipping_rect;
764 if (clipping) {
765 clipping_rect.left = clipping->left;
766 clipping_rect.bottom = clipping->bottom;
767 clipping_rect.right = clipping->right;
768 clipping_rect.top = clipping->top;
769 }
770 RenderPageImpl(pContext, pPage, transform_matrix, clipping_rect.ToFxRect(),
771 flags, true, nullptr);
772
773 pPage->SetRenderContext(nullptr);
774}
775
Cary Clark399be5b2016-03-14 16:51:29 -0400776#ifdef _SKIA_SUPPORT_
777DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page,
778 int size_x,
779 int size_y) {
780 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
781 if (!pPage)
782 return nullptr;
tsepez1e2c5572016-05-25 14:58:09 -0700783
weili9f515bc2016-07-24 08:08:24 -0700784 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700785 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
caryclarkd6e18872016-05-13 10:57:20 -0700786 CFX_FxgeDevice* skDevice = new CFX_FxgeDevice;
Cary Clark399be5b2016-03-14 16:51:29 -0400787 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
weili9f515bc2016-07-24 08:08:24 -0700788 pContext->m_pDevice.reset(skDevice);
tsepez4cf55152016-11-02 14:37:54 -0700789 FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, true,
tsepez1e2c5572016-05-25 14:58:09 -0700790 nullptr);
tsepeze5cb0b12016-10-26 15:06:11 -0700791 pPage->SetRenderContext(nullptr);
Cary Clark399be5b2016-03-14 16:51:29 -0400792 return recorder;
793}
794#endif
795
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
tsepez1e2c5572016-05-25 14:58:09 -0700797 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700798 if (!page)
799 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800800#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -0800801 pPage->Release();
802#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800803 CPDFSDK_PageView* pPageView =
tsepez1e2c5572016-05-25 14:58:09 -0700804 static_cast<CPDFSDK_PageView*>(pPage->GetView());
dsinclair1df1efa2016-09-07 09:55:37 -0700805 if (pPageView) {
dsinclairbcf46232016-10-03 13:02:27 -0700806 // We're already destroying the pageview, so bail early.
807 if (pPageView->IsBeingDestroyed())
808 return;
809
dsinclair1df1efa2016-09-07 09:55:37 -0700810 if (pPageView->IsLocked()) {
811 pPageView->TakePageOwnership();
812 return;
813 }
814
815 bool owned = pPageView->OwnsPage();
816 // This will delete the |pPageView| object. We must cleanup the PageView
817 // first because it will attempt to reset the View on the |pPage| during
818 // destruction.
dsinclair7cbe68e2016-10-12 11:56:23 -0700819 pPageView->GetFormFillEnv()->RemovePageView(pPage);
dsinclair1df1efa2016-09-07 09:55:37 -0700820 // If the page was owned then the pageview will have deleted the page.
821 if (owned)
822 return;
Tom Sepez51da0932015-11-25 16:05:49 -0800823 }
tsepez1e2c5572016-05-25 14:58:09 -0700824 delete pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800825#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700826}
827
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700828DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Jun Fangfc751362015-12-16 21:23:39 -0800829 delete UnderlyingFromFPDFDocument(document);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700830}
831
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700832DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
833 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700834}
835
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700836DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
837 int start_x,
838 int start_y,
839 int size_x,
840 int size_y,
841 int rotate,
842 int device_x,
843 int device_y,
844 double* page_x,
845 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -0800846 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700847 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800848 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800849#ifdef PDF_ENABLE_XFA
850 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
851 device_y, page_x, page_y);
852#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800853 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800854 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
855 rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800856 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800857 device2page.SetReverse(page2device);
Tom Sepez51da0932015-11-25 16:05:49 -0800858 FX_FLOAT page_x_f, page_y_f;
859 device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
860 page_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800861 *page_x = (page_x_f);
862 *page_y = (page_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800863#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700864}
865
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
867 int start_x,
868 int start_y,
869 int size_x,
870 int size_y,
871 int rotate,
872 double page_x,
873 double page_y,
874 int* device_x,
875 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700876 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800878 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700879 if (!pPage)
880 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800881#ifdef PDF_ENABLE_XFA
882 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
883 device_x, device_y);
884#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800885 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800886 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
887 rotate);
Tom Sepez51da0932015-11-25 16:05:49 -0800888 FX_FLOAT device_x_f, device_y_f;
889 page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
890 device_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800891 *device_x = FXSYS_round(device_x_f);
892 *device_y = FXSYS_round(device_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800893#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700894}
895
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700896DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
897 int height,
898 int alpha) {
tsepez37b12ad2016-12-14 19:50:23 -0800899 auto pBitmap = pdfium::MakeUnique<CFX_DIBitmap>();
900 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32))
thestig1cd352e2016-06-07 17:53:06 -0700901 return nullptr;
tsepez37b12ad2016-12-14 19:50:23 -0800902
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 return pBitmap.release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700904}
905
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
907 int height,
908 int format,
909 void* first_scan,
910 int stride) {
911 FXDIB_Format fx_format;
912 switch (format) {
913 case FPDFBitmap_Gray:
914 fx_format = FXDIB_8bppRgb;
915 break;
916 case FPDFBitmap_BGR:
917 fx_format = FXDIB_Rgb;
918 break;
919 case FPDFBitmap_BGRx:
920 fx_format = FXDIB_Rgb32;
921 break;
922 case FPDFBitmap_BGRA:
923 fx_format = FXDIB_Argb;
924 break;
925 default:
thestig1cd352e2016-06-07 17:53:06 -0700926 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927 }
928 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
929 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
930 return pBitmap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700931}
932
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700933DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
934 int left,
935 int top,
936 int width,
937 int height,
938 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800939 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700940 return;
thestigbefa4502016-05-26 20:15:19 -0700941
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942 CFX_FxgeDevice device;
thestigbefa4502016-05-26 20:15:19 -0700943 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
944 device.Attach(pBitmap, false, nullptr, false);
945 if (!pBitmap->HasAlpha())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 color |= 0xFF000000;
947 FX_RECT rect(left, top, left + width, top + height);
948 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700949}
950
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700952 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700953}
954
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700956 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700957}
958
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700960 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961}
962
963DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700964 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965}
966
967DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700968 delete CFXBitmapFromFPDFBitmap(bitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700969}
970
weili9f515bc2016-07-24 08:08:24 -0700971void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700972 FPDF_PAGE page,
973 int start_x,
974 int start_y,
975 int size_x,
976 int size_y,
977 int rotate,
978 int flags,
tsepez4cf55152016-11-02 14:37:54 -0700979 bool bNeedToRestore,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700981 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
982 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983 return;
984
Tom Sepez60d909e2015-12-10 15:34:55 -0800985 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
thestiga78ba602016-11-23 15:25:48 -0800987 FX_RECT rect(start_x, start_y, start_x + size_x, start_y + size_y);
988 RenderPageImpl(pContext, pPage, matrix, rect, flags, bNeedToRestore, pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700989}
990
991DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
992 int page_index,
993 double* width,
994 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800995 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
996 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700997 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998
Tom Sepez40e9ff32015-11-30 12:39:54 -0800999#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 int count = pDoc->GetPageCount();
1001 if (page_index < 0 || page_index >= count)
tsepez4cf55152016-11-02 14:37:54 -07001002 return false;
dsinclair9f206f02016-09-20 12:17:42 -07001003 CPDFXFA_Page* pPage = pDoc->GetXFAPage(page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 if (!pPage)
tsepez4cf55152016-11-02 14:37:54 -07001005 return false;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001006 *width = pPage->GetPageWidth();
1007 *height = pPage->GetPageHeight();
1008#else // PDF_ENABLE_XFA
1009 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
1010 if (!pDict)
tsepez4cf55152016-11-02 14:37:54 -07001011 return false;
thestig5cc24652016-04-26 11:46:02 -07001012
1013 CPDF_Page page(pDoc, pDict, true);
Tom Sepez51da0932015-11-25 16:05:49 -08001014 *width = page.GetPageWidth();
1015 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001016#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017
tsepez4cf55152016-11-02 14:37:54 -07001018 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019}
1020
1021DLLEXPORT FPDF_BOOL STDCALL
1022FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001023 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001025 return true;
Tom Sepez471a1032015-10-15 16:17:18 -07001026 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027 return viewRef.PrintScaling();
1028}
1029
1030DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001031 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032 if (!pDoc)
1033 return 1;
1034 CPDF_ViewerPreferences viewRef(pDoc);
1035 return viewRef.NumCopies();
1036}
1037
1038DLLEXPORT FPDF_PAGERANGE STDCALL
1039FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001040 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -07001042 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043 CPDF_ViewerPreferences viewRef(pDoc);
1044 return viewRef.PrintPageRange();
1045}
1046
1047DLLEXPORT FPDF_DUPLEXTYPE STDCALL
1048FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001049 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001050 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -07001051 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001052 CPDF_ViewerPreferences viewRef(pDoc);
1053 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001054 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001056 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001058 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 return DuplexFlipLongEdge;
1060 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001061}
1062
thestig04bebfe2016-11-04 16:07:25 -07001063DLLEXPORT unsigned long STDCALL FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document,
1064 FPDF_BYTESTRING key,
1065 char* buffer,
1066 unsigned long length) {
1067 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1068 if (!pDoc)
1069 return 0;
1070
1071 CPDF_ViewerPreferences viewRef(pDoc);
1072 CFX_ByteString bsVal;
1073 if (!viewRef.GenericName(key, &bsVal))
1074 return 0;
1075
1076 unsigned long dwStringLen = bsVal.GetLength() + 1;
1077 if (buffer && length >= dwStringLen)
1078 memcpy(buffer, bsVal.c_str(), dwStringLen);
1079 return dwStringLen;
1080}
1081
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001083 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1084 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001085 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001086
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1088 if (!pRoot)
1089 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001090
Lei Zhangd983b092015-12-14 16:58:33 -08001091 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001092 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
dsinclair38fd8442016-09-15 10:15:32 -07001093 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094 if (pDest)
1095 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001096
1097 if (!count.IsValid())
1098 return 0;
1099
1100 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101}
1102
1103DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1104 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001106 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107
Tom Sepez471a1032015-10-15 16:17:18 -07001108 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1109 if (!pDoc)
1110 return nullptr;
1111
Lei Zhangd983b092015-12-14 16:58:33 -08001112 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001113 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001114}
1115
Tom Sepez51da0932015-11-25 16:05:49 -08001116#ifdef PDF_ENABLE_XFA
thestig77d148d2016-04-06 06:28:31 -07001117DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Init(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118 if (!str)
1119 return -1;
1120
1121 FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
1122 return 0;
1123}
1124
thestig77d148d2016-04-06 06:28:31 -07001125DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Set(FPDF_BSTR* str,
1126 FPDF_LPCSTR bstr,
1127 int length) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001128 if (!str)
1129 return -1;
1130 if (!bstr || !length)
1131 return -1;
1132 if (length == -1)
1133 length = FXSYS_strlen(bstr);
1134
1135 if (length == 0) {
1136 if (str->str) {
1137 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001138 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001139 }
1140 str->len = 0;
1141 return 0;
1142 }
1143
1144 if (str->str && str->len < length)
1145 str->str = FX_Realloc(char, str->str, length + 1);
1146 else if (!str->str)
1147 str->str = FX_Alloc(char, length + 1);
1148
1149 str->str[length] = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150 FXSYS_memcpy(str->str, bstr, length);
1151 str->len = length;
1152
1153 return 0;
1154}
1155
thestig77d148d2016-04-06 06:28:31 -07001156DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Clear(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 if (!str)
1158 return -1;
1159
1160 if (str->str) {
1161 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001162 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 }
1164 str->len = 0;
1165 return 0;
1166}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001167#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168
1169DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1170 int index,
1171 void* buffer,
1172 long* buflen) {
1173 if (!buffer)
1174 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001175
1176 if (index < 0)
1177 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178
Tom Sepezbf59a072015-10-21 14:07:23 -07001179 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001180 if (!pDoc)
1181 return nullptr;
1182
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1184 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001185 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001187 CPDF_Object* pDestObj = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001188 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001189 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190 int count = nameTree.GetCount();
1191 if (index >= count) {
dsinclair38fd8442016-09-15 10:15:32 -07001192 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001194 return nullptr;
1195
1196 pdfium::base::CheckedNumeric<int> checked_count = count;
1197 checked_count += pDest->GetCount();
1198 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1199 return nullptr;
1200
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202 int i = 0;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001203 for (const auto& it : *pDest) {
1204 bsName = it.first;
tsepez0e606b52016-11-18 16:22:41 -08001205 pDestObj = it.second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 if (!pDestObj)
1207 continue;
1208 if (i == index)
1209 break;
1210 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001211 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 } else {
1213 pDestObj = nameTree.LookupValue(index, bsName);
1214 }
1215 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001216 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001217 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
dsinclair38fd8442016-09-15 10:15:32 -07001218 pDestObj = pDict->GetArrayFor("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001220 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001221 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001222 if (!pDestObj->IsArray())
1223 return nullptr;
1224
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225 CFX_WideString wsName = PDF_DecodeText(bsName);
1226 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -07001227 int len = utf16Name.GetLength();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001228 if (!buffer) {
1229 *buflen = len;
thestig9067fd62016-11-23 14:10:06 -08001230 } else if (len <= *buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001231 memcpy(buffer, utf16Name.c_str(), len);
1232 *buflen = len;
1233 } else {
1234 *buflen = -1;
1235 }
1236 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001237}