blob: c292384e88ba5f42ae4e7ca4c1827da5a46089bd [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 =
97 new CPDF_OCContext(pPage->m_pDocument, usage);
98
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
thestigfdb35ff2016-07-18 13:45:44 -0700432#if defined(_WIN32) && defined(PDFIUM_PRINT_TEXT_WITH_GDI)
433DLLEXPORT void STDCALL
434FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) {
435 g_pdfium_typeface_accessible_func = func;
436}
437
438DLLEXPORT void STDCALL FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) {
439 g_pdfium_print_text_with_gdi = !!use_gdi;
440}
441#endif
442
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
444 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700445 // NOTE: the creation of the file needs to be by the embedder on the
446 // other side of this API.
tsepez833619b2016-12-07 09:21:17 -0800447 CFX_RetainPtr<IFX_SeekableReadStream> pFileAccess =
tsepez345d4892016-11-30 15:10:55 -0800448 IFX_SeekableReadStream::CreateFromFilename((const FX_CHAR*)file_path);
449 if (!pFileAccess)
Tom Sepeze3166a82015-08-05 10:50:32 -0700450 return nullptr;
Tom Sepeze3166a82015-08-05 10:50:32 -0700451
tsepez345d4892016-11-30 15:10:55 -0800452 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700454
tsepez345d4892016-11-30 15:10:55 -0800455 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
dsinclair156de022016-08-24 11:58:24 -0700456 CPDF_Parser::Error error =
457 pDocument->GetParser()->StartParse(pFileAccess, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800458 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800459 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700460 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 }
dsinclair8837c912016-11-01 11:22:37 -0700462 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700463}
Jun Fange118ce92015-02-17 06:50:08 -0800464
Tom Sepez40e9ff32015-11-30 12:39:54 -0800465#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
467 int* docType) {
468 if (!document)
tsepez4cf55152016-11-02 14:37:54 -0700469 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800470
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 CPDF_Document* pdfDoc =
dsinclair521b7502016-11-02 13:02:28 -0700472 (static_cast<CPDFXFA_Context*>(document))->GetPDFDoc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 if (!pdfDoc)
tsepez4cf55152016-11-02 14:37:54 -0700474 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800475
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
477 if (!pRoot)
tsepez4cf55152016-11-02 14:37:54 -0700478 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800479
dsinclair38fd8442016-09-15 10:15:32 -0700480 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 if (!pAcroForm)
tsepez4cf55152016-11-02 14:37:54 -0700482 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800483
dsinclair38fd8442016-09-15 10:15:32 -0700484 CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485 if (!pXFA)
tsepez4cf55152016-11-02 14:37:54 -0700486 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800487
dsinclair38fd8442016-09-15 10:15:32 -0700488 bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false);
thestigded36342016-05-23 17:54:02 -0700489 *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA;
tsepez4cf55152016-11-02 14:37:54 -0700490 return true;
Jun Fange118ce92015-02-17 06:50:08 -0800491}
492
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700493DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
dsinclair521b7502016-11-02 13:02:28 -0700494 return document && (static_cast<CPDFXFA_Context*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700495}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800496#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700497
tsepezad2441e2016-10-24 10:19:11 -0700498class CMemFile final : public IFX_SeekableReadStream {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700499 public:
tsepez833619b2016-12-07 09:21:17 -0800500 static CFX_RetainPtr<CMemFile> Create(uint8_t* pBuf, FX_FILESIZE size) {
501 return CFX_RetainPtr<CMemFile>(new CMemFile(pBuf, size));
502 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700503
Lei Zhang3884dba2015-10-19 17:27:53 -0700504 FX_FILESIZE GetSize() override { return m_size; }
tsepezf39074c2016-10-26 15:33:58 -0700505 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
tsepez833619b2016-12-07 09:21:17 -0800506 if (offset < 0)
tsepezf39074c2016-10-26 15:33:58 -0700507 return false;
tsepez833619b2016-12-07 09:21:17 -0800508
tsepez4e597c82016-11-07 15:16:01 -0800509 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 newPos += offset;
tsepez833619b2016-12-07 09:21:17 -0800511 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size)
tsepezf39074c2016-10-26 15:33:58 -0700512 return false;
tsepez833619b2016-12-07 09:21:17 -0800513
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514 FXSYS_memcpy(buffer, m_pBuf + offset, size);
tsepezf39074c2016-10-26 15:33:58 -0700515 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700517
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518 private:
tsepez833619b2016-12-07 09:21:17 -0800519 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700520
Lei Zhang3884dba2015-10-19 17:27:53 -0700521 uint8_t* const m_pBuf;
522 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700523};
Lei Zhang3884dba2015-10-19 17:27:53 -0700524
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
526 int size,
527 FPDF_BYTESTRING password) {
tsepez833619b2016-12-07 09:21:17 -0800528 CFX_RetainPtr<CMemFile> pMemFile = CMemFile::Create((uint8_t*)data_buf, size);
529 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
tsepez4540fba2016-08-16 11:12:21 -0700530 pParser->SetPassword(password);
531
tsepez833619b2016-12-07 09:21:17 -0800532 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
tsepez4540fba2016-08-16 11:12:21 -0700533 CPDF_Parser::Error error =
dsinclaircedaa552016-08-24 11:12:19 -0700534 pDocument->GetParser()->StartParse(pMemFile, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800535 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800536 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700537 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700538 }
dsinclaircedaa552016-08-24 11:12:19 -0700539 CheckUnSupportError(pDocument.get(), error);
540 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700541}
542
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543DLLEXPORT FPDF_DOCUMENT STDCALL
544FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
545 FPDF_BYTESTRING password) {
tsepez833619b2016-12-07 09:21:17 -0800546 CFX_RetainPtr<CPDF_CustomAccess> pFile =
547 CPDF_CustomAccess::Create(pFileAccess);
548 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
tsepez4540fba2016-08-16 11:12:21 -0700549 pParser->SetPassword(password);
550
tsepez833619b2016-12-07 09:21:17 -0800551 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
dsinclaircedaa552016-08-24 11:12:19 -0700552 CPDF_Parser::Error error =
553 pDocument->GetParser()->StartParse(pFile, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800554 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800555 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700556 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557 }
dsinclaircedaa552016-08-24 11:12:19 -0700558 CheckUnSupportError(pDocument.get(), error);
559 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700560}
561
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
563 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700564 if (!fileVersion)
tsepez4cf55152016-11-02 14:37:54 -0700565 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700566
Tom Sepez471a1032015-10-15 16:17:18 -0700567 *fileVersion = 0;
568 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
569 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700570 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700571
572 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573 if (!pParser)
tsepez4cf55152016-11-02 14:37:54 -0700574 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700575
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576 *fileVersion = pParser->GetFileVersion();
tsepez4cf55152016-11-02 14:37:54 -0700577 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700578}
579
tsepezc3255f52016-03-25 14:52:27 -0700580// jabdelmalek: changed return type from uint32_t to build on Linux (and match
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581// header).
582DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700583 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestig27ddf162016-05-23 15:06:59 -0700584 // https://bugs.chromium.org/p/pdfium/issues/detail?id=499
585 if (!pDoc) {
Tom Sepez51da0932015-11-25 16:05:49 -0800586#ifndef PDF_ENABLE_XFA
587 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800588#else // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700589 return 0xFFFFFFFF;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800590#endif // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700591 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700592
thestig27ddf162016-05-23 15:06:59 -0700593 return pDoc->GetUserPermissions();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700594}
595
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700597 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700598 if (!pDoc || !pDoc->GetParser())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700600
Tom Sepez471a1032015-10-15 16:17:18 -0700601 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
dsinclair38fd8442016-09-15 10:15:32 -0700602 return pDict ? pDict->GetIntegerFor("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700603}
604
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800606 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700607 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700608}
609
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700610DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
611 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800612 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
613 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700614 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800615
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700616 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700617 return nullptr;
618
Tom Sepez40e9ff32015-11-30 12:39:54 -0800619#ifdef PDF_ENABLE_XFA
dsinclair9f206f02016-09-20 12:17:42 -0700620 return pDoc->GetXFAPage(page_index);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800621#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800622 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800623 if (!pDict)
thestig5cc24652016-04-26 11:46:02 -0700624 return nullptr;
625
626 CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true);
627 pPage->ParseContent();
Tom Sepez51da0932015-11-25 16:05:49 -0800628 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800629#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700630}
631
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800633 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700634 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700635}
636
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800638 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700639 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700640}
641
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700642#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
644 FPDF_PAGE page,
645 int start_x,
646 int start_y,
647 int size_x,
648 int size_y,
649 int rotate,
650 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700651 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652 if (!pPage)
653 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700654
weili9f515bc2016-07-24 08:08:24 -0700655 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700656 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700657
thestig84144e82016-09-27 15:06:01 -0700658 std::unique_ptr<CFX_DIBitmap> pBitmap;
659 const bool bNewBitmap =
660 pPage->BackgroundAlphaNeeded() || pPage->HasImageMask();
661 if (bNewBitmap) {
tsepez36eb4bd2016-10-03 15:24:27 -0700662 pBitmap = pdfium::MakeUnique<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700663 pBitmap->Create(size_x, size_y, FXDIB_Argb);
664 pBitmap->Clear(0x00ffffff);
thestigbefa4502016-05-26 20:15:19 -0700665 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
tsepez36eb4bd2016-10-03 15:24:27 -0700666 pContext->m_pDevice = pdfium::WrapUnique(pDevice);
thestig84144e82016-09-27 15:06:01 -0700667 pDevice->Attach(pBitmap.get(), false, nullptr, false);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800668 } else {
tsepez36eb4bd2016-10-03 15:24:27 -0700669 pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsDevice>(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800670 }
Bo Xud4e406e2014-08-13 11:03:19 -0700671
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700673 rotate, flags, true, nullptr);
Bo Xud4e406e2014-08-13 11:03:19 -0700674
thestig84144e82016-09-27 15:06:01 -0700675 if (bNewBitmap) {
676 CFX_WindowsDevice WinDC(dc);
677 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
tsepez36eb4bd2016-10-03 15:24:27 -0700678 std::unique_ptr<CFX_DIBitmap> pDst = pdfium::MakeUnique<CFX_DIBitmap>();
thestig84144e82016-09-27 15:06:01 -0700679 int pitch = pBitmap->GetPitch();
680 pDst->Create(size_x, size_y, FXDIB_Rgb32);
681 FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
682 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap.get(), 0, 0,
tsepez4cf55152016-11-02 14:37:54 -0700683 FXDIB_BLEND_NORMAL, nullptr, false, nullptr);
thestig84144e82016-09-27 15:06:01 -0700684 WinDC.StretchDIBits(pDst.get(), 0, 0, size_x, size_y);
685 } else {
686 WinDC.SetDIBits(pBitmap.get(), 0, 0);
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700687 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 }
Jun Fang1aeeceb2015-12-29 10:27:44 +0800689
tsepeze5cb0b12016-10-26 15:06:11 -0700690 pPage->SetRenderContext(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700691}
Lei Zhangae85f872016-02-19 14:57:07 -0800692#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693
694DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
695 FPDF_PAGE page,
696 int start_x,
697 int start_y,
698 int size_x,
699 int size_y,
700 int rotate,
701 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700702 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700703 return;
tsepez1e2c5572016-05-25 14:58:09 -0700704
Tom Sepezdb0be962015-10-16 14:00:21 -0700705 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700706 if (!pPage)
707 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708
weili9f515bc2016-07-24 08:08:24 -0700709 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700710 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
thestigbefa4502016-05-26 20:15:19 -0700711 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
weili9f515bc2016-07-24 08:08:24 -0700712 pContext->m_pDevice.reset(pDevice);
thestigbefa4502016-05-26 20:15:19 -0700713 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
714 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715
716 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700717 rotate, flags, true, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718
caryclark687fbde2016-11-22 12:44:25 -0800719#ifdef _SKIA_SUPPORT_PATHS_
caryclark8f875502016-12-06 13:49:34 -0800720 pDevice->Flush();
caryclark687fbde2016-11-22 12:44:25 -0800721 pBitmap->UnPreMultiply();
722#endif
tsepeze5cb0b12016-10-26 15:06:11 -0700723 pPage->SetRenderContext(nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700724}
725
thestiga78ba602016-11-23 15:25:48 -0800726DLLEXPORT void STDCALL FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap,
727 FPDF_PAGE page,
728 const FS_MATRIX* matrix,
729 const FS_RECTF* clipping,
730 int flags) {
731 if (!bitmap)
732 return;
733
734 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
735 if (!pPage)
736 return;
737
738 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
739 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
740 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
741 pContext->m_pDevice.reset(pDevice);
742 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
743 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
744
745 CFX_Matrix transform_matrix = pPage->GetPageMatrix();
746 if (matrix) {
747 CFX_Matrix cmatrix;
748 cmatrix.a = matrix->a;
749 cmatrix.b = matrix->b;
750 cmatrix.c = matrix->c;
751 cmatrix.d = matrix->d;
752 cmatrix.e = matrix->e;
753 cmatrix.f = matrix->f;
754 transform_matrix.Concat(cmatrix);
755 }
756
757 CFX_FloatRect clipping_rect;
758 if (clipping) {
759 clipping_rect.left = clipping->left;
760 clipping_rect.bottom = clipping->bottom;
761 clipping_rect.right = clipping->right;
762 clipping_rect.top = clipping->top;
763 }
764 RenderPageImpl(pContext, pPage, transform_matrix, clipping_rect.ToFxRect(),
765 flags, true, nullptr);
766
767 pPage->SetRenderContext(nullptr);
768}
769
Cary Clark399be5b2016-03-14 16:51:29 -0400770#ifdef _SKIA_SUPPORT_
771DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page,
772 int size_x,
773 int size_y) {
774 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
775 if (!pPage)
776 return nullptr;
tsepez1e2c5572016-05-25 14:58:09 -0700777
weili9f515bc2016-07-24 08:08:24 -0700778 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700779 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
caryclarkd6e18872016-05-13 10:57:20 -0700780 CFX_FxgeDevice* skDevice = new CFX_FxgeDevice;
Cary Clark399be5b2016-03-14 16:51:29 -0400781 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
weili9f515bc2016-07-24 08:08:24 -0700782 pContext->m_pDevice.reset(skDevice);
tsepez4cf55152016-11-02 14:37:54 -0700783 FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, true,
tsepez1e2c5572016-05-25 14:58:09 -0700784 nullptr);
tsepeze5cb0b12016-10-26 15:06:11 -0700785 pPage->SetRenderContext(nullptr);
Cary Clark399be5b2016-03-14 16:51:29 -0400786 return recorder;
787}
788#endif
789
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700790DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
tsepez1e2c5572016-05-25 14:58:09 -0700791 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792 if (!page)
793 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800794#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -0800795 pPage->Release();
796#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800797 CPDFSDK_PageView* pPageView =
tsepez1e2c5572016-05-25 14:58:09 -0700798 static_cast<CPDFSDK_PageView*>(pPage->GetView());
dsinclair1df1efa2016-09-07 09:55:37 -0700799 if (pPageView) {
dsinclairbcf46232016-10-03 13:02:27 -0700800 // We're already destroying the pageview, so bail early.
801 if (pPageView->IsBeingDestroyed())
802 return;
803
dsinclair1df1efa2016-09-07 09:55:37 -0700804 if (pPageView->IsLocked()) {
805 pPageView->TakePageOwnership();
806 return;
807 }
808
809 bool owned = pPageView->OwnsPage();
810 // This will delete the |pPageView| object. We must cleanup the PageView
811 // first because it will attempt to reset the View on the |pPage| during
812 // destruction.
dsinclair7cbe68e2016-10-12 11:56:23 -0700813 pPageView->GetFormFillEnv()->RemovePageView(pPage);
dsinclair1df1efa2016-09-07 09:55:37 -0700814 // If the page was owned then the pageview will have deleted the page.
815 if (owned)
816 return;
Tom Sepez51da0932015-11-25 16:05:49 -0800817 }
tsepez1e2c5572016-05-25 14:58:09 -0700818 delete pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800819#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700820}
821
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Jun Fangfc751362015-12-16 21:23:39 -0800823 delete UnderlyingFromFPDFDocument(document);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700824}
825
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
827 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700828}
829
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700830DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
831 int start_x,
832 int start_y,
833 int size_x,
834 int size_y,
835 int rotate,
836 int device_x,
837 int device_y,
838 double* page_x,
839 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -0800840 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700841 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800842 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800843#ifdef PDF_ENABLE_XFA
844 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
845 device_y, page_x, page_y);
846#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800847 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800848 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
849 rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800850 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800851 device2page.SetReverse(page2device);
Tom Sepez51da0932015-11-25 16:05:49 -0800852 FX_FLOAT page_x_f, page_y_f;
853 device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
854 page_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800855 *page_x = (page_x_f);
856 *page_y = (page_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800857#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700858}
859
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
861 int start_x,
862 int start_y,
863 int size_x,
864 int size_y,
865 int rotate,
866 double page_x,
867 double page_y,
868 int* device_x,
869 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700870 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700871 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800872 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700873 if (!pPage)
874 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800875#ifdef PDF_ENABLE_XFA
876 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
877 device_x, device_y);
878#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800879 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800880 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
881 rotate);
Tom Sepez51da0932015-11-25 16:05:49 -0800882 FX_FLOAT device_x_f, device_y_f;
883 page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
884 device_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800885 *device_x = FXSYS_round(device_x_f);
886 *device_y = FXSYS_round(device_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800887#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700888}
889
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
891 int height,
892 int alpha) {
tsepez37b12ad2016-12-14 19:50:23 -0800893 auto pBitmap = pdfium::MakeUnique<CFX_DIBitmap>();
894 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32))
thestig1cd352e2016-06-07 17:53:06 -0700895 return nullptr;
tsepez37b12ad2016-12-14 19:50:23 -0800896
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897 return pBitmap.release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700898}
899
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
901 int height,
902 int format,
903 void* first_scan,
904 int stride) {
905 FXDIB_Format fx_format;
906 switch (format) {
907 case FPDFBitmap_Gray:
908 fx_format = FXDIB_8bppRgb;
909 break;
910 case FPDFBitmap_BGR:
911 fx_format = FXDIB_Rgb;
912 break;
913 case FPDFBitmap_BGRx:
914 fx_format = FXDIB_Rgb32;
915 break;
916 case FPDFBitmap_BGRA:
917 fx_format = FXDIB_Argb;
918 break;
919 default:
thestig1cd352e2016-06-07 17:53:06 -0700920 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921 }
922 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
923 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
924 return pBitmap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700925}
926
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
928 int left,
929 int top,
930 int width,
931 int height,
932 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800933 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 return;
thestigbefa4502016-05-26 20:15:19 -0700935
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936 CFX_FxgeDevice device;
thestigbefa4502016-05-26 20:15:19 -0700937 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
938 device.Attach(pBitmap, false, nullptr, false);
939 if (!pBitmap->HasAlpha())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700940 color |= 0xFF000000;
941 FX_RECT rect(left, top, left + width, top + height);
942 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700943}
944
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700946 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700947}
948
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700950 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700951}
952
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700954 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955}
956
957DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700958 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959}
960
961DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700962 delete CFXBitmapFromFPDFBitmap(bitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963}
964
weili9f515bc2016-07-24 08:08:24 -0700965void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966 FPDF_PAGE page,
967 int start_x,
968 int start_y,
969 int size_x,
970 int size_y,
971 int rotate,
972 int flags,
tsepez4cf55152016-11-02 14:37:54 -0700973 bool bNeedToRestore,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700974 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700975 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
976 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977 return;
978
Tom Sepez60d909e2015-12-10 15:34:55 -0800979 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
thestiga78ba602016-11-23 15:25:48 -0800981 FX_RECT rect(start_x, start_y, start_x + size_x, start_y + size_y);
982 RenderPageImpl(pContext, pPage, matrix, rect, flags, bNeedToRestore, pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983}
984
985DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
986 int page_index,
987 double* width,
988 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800989 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
990 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700991 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992
Tom Sepez40e9ff32015-11-30 12:39:54 -0800993#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700994 int count = pDoc->GetPageCount();
995 if (page_index < 0 || page_index >= count)
tsepez4cf55152016-11-02 14:37:54 -0700996 return false;
dsinclair9f206f02016-09-20 12:17:42 -0700997 CPDFXFA_Page* pPage = pDoc->GetXFAPage(page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998 if (!pPage)
tsepez4cf55152016-11-02 14:37:54 -0700999 return false;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001000 *width = pPage->GetPageWidth();
1001 *height = pPage->GetPageHeight();
1002#else // PDF_ENABLE_XFA
1003 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
1004 if (!pDict)
tsepez4cf55152016-11-02 14:37:54 -07001005 return false;
thestig5cc24652016-04-26 11:46:02 -07001006
1007 CPDF_Page page(pDoc, pDict, true);
Tom Sepez51da0932015-11-25 16:05:49 -08001008 *width = page.GetPageWidth();
1009 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001010#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011
tsepez4cf55152016-11-02 14:37:54 -07001012 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001013}
1014
1015DLLEXPORT FPDF_BOOL STDCALL
1016FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001017 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001019 return true;
Tom Sepez471a1032015-10-15 16:17:18 -07001020 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001021 return viewRef.PrintScaling();
1022}
1023
1024DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001025 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026 if (!pDoc)
1027 return 1;
1028 CPDF_ViewerPreferences viewRef(pDoc);
1029 return viewRef.NumCopies();
1030}
1031
1032DLLEXPORT FPDF_PAGERANGE STDCALL
1033FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001034 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -07001036 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037 CPDF_ViewerPreferences viewRef(pDoc);
1038 return viewRef.PrintPageRange();
1039}
1040
1041DLLEXPORT FPDF_DUPLEXTYPE STDCALL
1042FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001043 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -07001045 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046 CPDF_ViewerPreferences viewRef(pDoc);
1047 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001048 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001050 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001052 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053 return DuplexFlipLongEdge;
1054 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001055}
1056
thestig04bebfe2016-11-04 16:07:25 -07001057DLLEXPORT unsigned long STDCALL FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document,
1058 FPDF_BYTESTRING key,
1059 char* buffer,
1060 unsigned long length) {
1061 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1062 if (!pDoc)
1063 return 0;
1064
1065 CPDF_ViewerPreferences viewRef(pDoc);
1066 CFX_ByteString bsVal;
1067 if (!viewRef.GenericName(key, &bsVal))
1068 return 0;
1069
1070 unsigned long dwStringLen = bsVal.GetLength() + 1;
1071 if (buffer && length >= dwStringLen)
1072 memcpy(buffer, bsVal.c_str(), dwStringLen);
1073 return dwStringLen;
1074}
1075
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001077 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1078 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001080
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1082 if (!pRoot)
1083 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001084
Lei Zhangd983b092015-12-14 16:58:33 -08001085 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001086 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
dsinclair38fd8442016-09-15 10:15:32 -07001087 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088 if (pDest)
1089 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001090
1091 if (!count.IsValid())
1092 return 0;
1093
1094 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001095}
1096
1097DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1098 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001099 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001100 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101
Tom Sepez471a1032015-10-15 16:17:18 -07001102 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1103 if (!pDoc)
1104 return nullptr;
1105
Lei Zhangd983b092015-12-14 16:58:33 -08001106 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001107 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001108}
1109
Tom Sepez51da0932015-11-25 16:05:49 -08001110#ifdef PDF_ENABLE_XFA
thestig77d148d2016-04-06 06:28:31 -07001111DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Init(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001112 if (!str)
1113 return -1;
1114
1115 FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
1116 return 0;
1117}
1118
thestig77d148d2016-04-06 06:28:31 -07001119DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Set(FPDF_BSTR* str,
1120 FPDF_LPCSTR bstr,
1121 int length) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001122 if (!str)
1123 return -1;
1124 if (!bstr || !length)
1125 return -1;
1126 if (length == -1)
1127 length = FXSYS_strlen(bstr);
1128
1129 if (length == 0) {
1130 if (str->str) {
1131 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001132 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133 }
1134 str->len = 0;
1135 return 0;
1136 }
1137
1138 if (str->str && str->len < length)
1139 str->str = FX_Realloc(char, str->str, length + 1);
1140 else if (!str->str)
1141 str->str = FX_Alloc(char, length + 1);
1142
1143 str->str[length] = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001144 FXSYS_memcpy(str->str, bstr, length);
1145 str->len = length;
1146
1147 return 0;
1148}
1149
thestig77d148d2016-04-06 06:28:31 -07001150DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Clear(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151 if (!str)
1152 return -1;
1153
1154 if (str->str) {
1155 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001156 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 }
1158 str->len = 0;
1159 return 0;
1160}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001161#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001162
1163DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1164 int index,
1165 void* buffer,
1166 long* buflen) {
1167 if (!buffer)
1168 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001169
1170 if (index < 0)
1171 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001172
Tom Sepezbf59a072015-10-21 14:07:23 -07001173 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001174 if (!pDoc)
1175 return nullptr;
1176
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1178 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001179 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001181 CPDF_Object* pDestObj = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001182 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001183 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184 int count = nameTree.GetCount();
1185 if (index >= count) {
dsinclair38fd8442016-09-15 10:15:32 -07001186 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001187 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001188 return nullptr;
1189
1190 pdfium::base::CheckedNumeric<int> checked_count = count;
1191 checked_count += pDest->GetCount();
1192 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1193 return nullptr;
1194
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001195 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196 int i = 0;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001197 for (const auto& it : *pDest) {
1198 bsName = it.first;
tsepez0e606b52016-11-18 16:22:41 -08001199 pDestObj = it.second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200 if (!pDestObj)
1201 continue;
1202 if (i == index)
1203 break;
1204 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001205 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 } else {
1207 pDestObj = nameTree.LookupValue(index, bsName);
1208 }
1209 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001210 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001211 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
dsinclair38fd8442016-09-15 10:15:32 -07001212 pDestObj = pDict->GetArrayFor("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001214 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001215 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001216 if (!pDestObj->IsArray())
1217 return nullptr;
1218
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001219 CFX_WideString wsName = PDF_DecodeText(bsName);
1220 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -07001221 int len = utf16Name.GetLength();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 if (!buffer) {
1223 *buflen = len;
thestig9067fd62016-11-23 14:10:06 -08001224 } else if (len <= *buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225 memcpy(buffer, utf16Name.c_str(), len);
1226 *buflen = len;
1227 } else {
1228 *buflen = -1;
1229 }
1230 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001231}