blob: fb87c838d4c34467f10bf7ffd119112bdbedea76 [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:
122 explicit CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess);
123 ~CPDF_CustomAccess() override {}
124
125 // IFX_SeekableReadStream
126 FX_FILESIZE GetSize() override;
127 void Release() override;
128 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
129
130 private:
131 FPDF_FILEACCESS m_FileAccess;
132};
133
134CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
135 : m_FileAccess(*pFileAccess) {}
136
137FX_FILESIZE CPDF_CustomAccess::GetSize() {
138 return m_FileAccess.m_FileLen;
139}
140
141void CPDF_CustomAccess::Release() {
142 delete this;
143}
144
145bool CPDF_CustomAccess::ReadBlock(void* buffer,
146 FX_FILESIZE offset,
147 size_t size) {
148 if (offset < 0)
149 return false;
150
151 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
152 newPos += offset;
153 if (!newPos.IsValid() ||
154 newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) {
155 return false;
156 }
157 return !!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,
158 reinterpret_cast<uint8_t*>(buffer), size);
159}
160
Tom Sepez40e9ff32015-11-30 12:39:54 -0800161#ifdef PDF_ENABLE_XFA
tsepezfa89a202016-12-02 09:48:30 -0800162class CFPDF_FileStream : public IFX_SeekableStream {
163 public:
164 explicit CFPDF_FileStream(FPDF_FILEHANDLER* pFS);
165 ~CFPDF_FileStream() override {}
Tom Sepez471a1032015-10-15 16:17:18 -0700166
tsepezfa89a202016-12-02 09:48:30 -0800167 // IFX_SeekableStream:
168 IFX_SeekableStream* Retain() override;
169 void Release() override;
170 FX_FILESIZE GetSize() override;
171 bool IsEOF() override;
172 FX_FILESIZE GetPosition() override;
173 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
174 size_t ReadBlock(void* buffer, size_t size) override;
175 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override;
176 bool Flush() override;
Tom Sepezbf59a072015-10-21 14:07:23 -0700177
tsepezfa89a202016-12-02 09:48:30 -0800178 void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; }
Tom Sepezdb0be962015-10-16 14:00:21 -0700179
tsepezfa89a202016-12-02 09:48:30 -0800180 protected:
181 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
tsepezad2441e2016-10-24 10:19:11 -0700190IFX_SeekableStream* CFPDF_FileStream::Retain() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 return this;
Bo Xufdc00a72014-10-28 23:03:33 -0700192}
193
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194void CFPDF_FileStream::Release() {
195 if (m_pFS && m_pFS->Release)
196 m_pFS->Release(m_pFS->clientData);
197 delete this;
Bo Xufdc00a72014-10-28 23:03:33 -0700198}
199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200FX_FILESIZE CFPDF_FileStream::GetSize() {
201 if (m_pFS && m_pFS->GetSize)
202 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
203 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700204}
205
tsepezf39074c2016-10-26 15:33:58 -0700206bool CFPDF_FileStream::IsEOF() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -0700208}
209
weili625ad662016-06-15 11:21:33 -0700210FX_FILESIZE CFPDF_FileStream::GetPosition() {
211 return m_nCurPos;
212}
213
tsepezf39074c2016-10-26 15:33:58 -0700214bool CFPDF_FileStream::ReadBlock(void* buffer,
215 FX_FILESIZE offset,
216 size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 if (!buffer || !size || !m_pFS->ReadBlock)
tsepezf39074c2016-10-26 15:33:58 -0700218 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700219
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
221 (FPDF_DWORD)size) == 0) {
222 m_nCurPos = offset + size;
tsepezf39074c2016-10-26 15:33:58 -0700223 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 }
tsepezf39074c2016-10-26 15:33:58 -0700225 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700226}
227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
229 if (!buffer || !size || !m_pFS->ReadBlock)
230 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700231
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 FX_FILESIZE nSize = GetSize();
233 if (m_nCurPos >= nSize)
234 return 0;
235 FX_FILESIZE dwAvail = nSize - m_nCurPos;
236 if (dwAvail < (FX_FILESIZE)size)
237 size = (size_t)dwAvail;
238 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
239 (FPDF_DWORD)size) == 0) {
240 m_nCurPos += size;
241 return size;
242 }
Bo Xufdc00a72014-10-28 23:03:33 -0700243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700245}
246
tsepezf39074c2016-10-26 15:33:58 -0700247bool CFPDF_FileStream::WriteBlock(const void* buffer,
248 FX_FILESIZE offset,
249 size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 if (!m_pFS || !m_pFS->WriteBlock)
tsepezf39074c2016-10-26 15:33:58 -0700251 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700252
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
254 (FPDF_DWORD)size) == 0) {
255 m_nCurPos = offset + size;
tsepezf39074c2016-10-26 15:33:58 -0700256 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 }
tsepezf39074c2016-10-26 15:33:58 -0700258 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700259}
260
tsepezf39074c2016-10-26 15:33:58 -0700261bool CFPDF_FileStream::Flush() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 if (!m_pFS || !m_pFS->Flush)
tsepezf39074c2016-10-26 15:33:58 -0700263 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700266}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800267#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268
tsepezfa89a202016-12-02 09:48:30 -0800269} // namespace
270
271UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
272 return static_cast<UnderlyingDocumentType*>(doc);
273}
274
275FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
276 return static_cast<FPDF_DOCUMENT>(doc);
277}
278
279UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
280 return static_cast<UnderlyingPageType*>(page);
281}
282
283CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
284#ifdef PDF_ENABLE_XFA
285 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
286#else // PDF_ENABLE_XFA
287 return UnderlyingFromFPDFDocument(doc);
288#endif // PDF_ENABLE_XFA
289}
290
291FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
292#ifdef PDF_ENABLE_XFA
293 return doc ? FPDFDocumentFromUnderlying(
294 new CPDFXFA_Context(pdfium::WrapUnique(doc)))
295 : nullptr;
296#else // PDF_ENABLE_XFA
297 return FPDFDocumentFromUnderlying(doc);
298#endif // PDF_ENABLE_XFA
299}
300
301CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
302#ifdef PDF_ENABLE_XFA
303 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
304#else // PDF_ENABLE_XFA
305 return UnderlyingFromFPDFPage(page);
306#endif // PDF_ENABLE_XFA
307}
308
309CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
310 return static_cast<CFX_DIBitmap*>(bitmap);
311}
312
tsepezbea04972016-12-01 13:54:42 -0800313IFX_SeekableReadStream* MakeSeekableReadStream(FPDF_FILEACCESS* pFileAccess) {
314 return new CPDF_CustomAccess(pFileAccess);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700315}
316
tsepezfa89a202016-12-02 09:48:30 -0800317#ifdef PDF_ENABLE_XFA
318IFX_SeekableStream* MakeSeekableStream(FPDF_FILEHANDLER* pFilehandler) {
319 return new CFPDF_FileStream(pFilehandler);
320}
321#endif // PDF_ENABLE_XFA
322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
tsepezc3255f52016-03-25 14:52:27 -0700324static uint32_t foxit_sandbox_policy = 0xFFFFFFFF;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700325
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
327 switch (policy) {
328 case FPDF_POLICY_MACHINETIME_ACCESS: {
329 if (enable)
330 foxit_sandbox_policy |= 0x01;
331 else
332 foxit_sandbox_policy &= 0xFFFFFFFE;
333 } break;
334 default:
335 break;
336 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700337}
338
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
340 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700341 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700342 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343 default:
tsepez4cf55152016-11-02 14:37:54 -0700344 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346}
347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700349 FPDF_InitLibraryWithConfig(nullptr);
350}
351
Dan Sinclairf766ad22016-03-14 13:51:24 -0400352DLLEXPORT void STDCALL
353FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
tsepez02759102016-12-01 08:29:25 -0800354 if (g_pCodecModule)
355 return;
356
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700358
weili47228ac2016-07-20 10:35:31 -0700359 CFX_GEModule* pModule = CFX_GEModule::Get();
360 pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr, g_pCodecModule);
tsepez02759102016-12-01 08:29:25 -0800361
Tom Sepez1b246282015-11-25 15:15:31 -0800362 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
363 pModuleMgr->SetCodecModule(g_pCodecModule);
364 pModuleMgr->InitPageModule();
dsinclairab5b60e2016-06-23 08:18:36 -0700365 pModuleMgr->LoadEmbeddedGB1CMaps();
366 pModuleMgr->LoadEmbeddedJapan1CMaps();
367 pModuleMgr->LoadEmbeddedCNS1CMaps();
368 pModuleMgr->LoadEmbeddedKorea1CMaps();
dsinclaird647a6b2016-04-26 13:13:20 -0700369
Tom Sepez40e9ff32015-11-30 12:39:54 -0800370#ifdef PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700371 FXJSE_Initialize();
372 BC_Library_Init();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800373#endif // PDF_ENABLE_XFA
Tom Sepez452b4f32015-10-13 09:27:27 -0700374 if (cfg && cfg->version >= 2)
375 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700376}
377
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
tsepez02759102016-12-01 08:29:25 -0800379 if (!g_pCodecModule)
380 return;
381
Tom Sepez51da0932015-11-25 16:05:49 -0800382#ifdef PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700383 BC_Library_Destory();
384 FXJSE_Finalize();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800385#endif // PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700386
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 CPDF_ModuleMgr::Destroy();
388 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700389
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 delete g_pCodecModule;
391 g_pCodecModule = nullptr;
thestig2d6dda12016-06-28 07:39:09 -0700392
393 IJS_Runtime::Destroy();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700394}
395
396#ifndef _WIN32
397int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398void SetLastError(int err) {
399 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700400}
401
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402int GetLastError() {
403 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700404}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800405#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700406
Tom Sepezf10ae632016-01-26 14:19:52 -0800407void ProcessParseError(CPDF_Parser::Error err) {
tsepezc3255f52016-03-25 14:52:27 -0700408 uint32_t err_code = FPDF_ERR_SUCCESS;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800410 switch (err) {
411 case CPDF_Parser::SUCCESS:
412 err_code = FPDF_ERR_SUCCESS;
413 break;
414 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 err_code = FPDF_ERR_FILE;
416 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800417 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 err_code = FPDF_ERR_FORMAT;
419 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800420 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 err_code = FPDF_ERR_PASSWORD;
422 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800423 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 err_code = FPDF_ERR_SECURITY;
425 break;
426 }
427 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428}
429
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
431 FPDF_BOOL enable) {
432 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700433}
434
thestigfdb35ff2016-07-18 13:45:44 -0700435#if defined(_WIN32) && defined(PDFIUM_PRINT_TEXT_WITH_GDI)
436DLLEXPORT void STDCALL
437FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) {
438 g_pdfium_typeface_accessible_func = func;
439}
440
441DLLEXPORT void STDCALL FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) {
442 g_pdfium_print_text_with_gdi = !!use_gdi;
443}
444#endif
445
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
447 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700448 // NOTE: the creation of the file needs to be by the embedder on the
449 // other side of this API.
tsepezad2441e2016-10-24 10:19:11 -0700450 IFX_SeekableReadStream* pFileAccess =
tsepez345d4892016-11-30 15:10:55 -0800451 IFX_SeekableReadStream::CreateFromFilename((const FX_CHAR*)file_path);
452 if (!pFileAccess)
Tom Sepeze3166a82015-08-05 10:50:32 -0700453 return nullptr;
Tom Sepeze3166a82015-08-05 10:50:32 -0700454
tsepez345d4892016-11-30 15:10:55 -0800455 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700457
tsepez345d4892016-11-30 15:10:55 -0800458 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
dsinclair156de022016-08-24 11:58:24 -0700459 CPDF_Parser::Error error =
460 pDocument->GetParser()->StartParse(pFileAccess, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800461 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800462 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700463 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 }
dsinclair8837c912016-11-01 11:22:37 -0700465 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700466}
Jun Fange118ce92015-02-17 06:50:08 -0800467
Tom Sepez40e9ff32015-11-30 12:39:54 -0800468#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
470 int* docType) {
471 if (!document)
tsepez4cf55152016-11-02 14:37:54 -0700472 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800473
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474 CPDF_Document* pdfDoc =
dsinclair521b7502016-11-02 13:02:28 -0700475 (static_cast<CPDFXFA_Context*>(document))->GetPDFDoc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 if (!pdfDoc)
tsepez4cf55152016-11-02 14:37:54 -0700477 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800478
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
480 if (!pRoot)
tsepez4cf55152016-11-02 14:37:54 -0700481 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800482
dsinclair38fd8442016-09-15 10:15:32 -0700483 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 if (!pAcroForm)
tsepez4cf55152016-11-02 14:37:54 -0700485 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800486
dsinclair38fd8442016-09-15 10:15:32 -0700487 CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488 if (!pXFA)
tsepez4cf55152016-11-02 14:37:54 -0700489 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800490
dsinclair38fd8442016-09-15 10:15:32 -0700491 bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false);
thestigded36342016-05-23 17:54:02 -0700492 *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA;
tsepez4cf55152016-11-02 14:37:54 -0700493 return true;
Jun Fange118ce92015-02-17 06:50:08 -0800494}
495
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
dsinclair521b7502016-11-02 13:02:28 -0700497 return document && (static_cast<CPDFXFA_Context*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700498}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800499#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700500
tsepezad2441e2016-10-24 10:19:11 -0700501class CMemFile final : public IFX_SeekableReadStream {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502 public:
503 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700504
Lei Zhang3884dba2015-10-19 17:27:53 -0700505 void Release() override { delete this; }
506 FX_FILESIZE GetSize() override { return m_size; }
tsepezf39074c2016-10-26 15:33:58 -0700507 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 if (offset < 0) {
tsepezf39074c2016-10-26 15:33:58 -0700509 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 }
tsepez4e597c82016-11-07 15:16:01 -0800511 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700512 newPos += offset;
weili47ca6922016-03-31 15:08:27 -0700513 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size) {
tsepezf39074c2016-10-26 15:33:58 -0700514 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515 }
516 FXSYS_memcpy(buffer, m_pBuf + offset, size);
tsepezf39074c2016-10-26 15:33:58 -0700517 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700519
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520 private:
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700521 ~CMemFile() override {}
522
Lei Zhang3884dba2015-10-19 17:27:53 -0700523 uint8_t* const m_pBuf;
524 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700525};
Lei Zhang3884dba2015-10-19 17:27:53 -0700526
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
528 int size,
529 FPDF_BYTESTRING password) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700530 CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
tsepez4540fba2016-08-16 11:12:21 -0700531 std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
532 pParser->SetPassword(password);
533
dsinclaircedaa552016-08-24 11:12:19 -0700534 std::unique_ptr<CPDF_Document> pDocument(
535 new CPDF_Document(std::move(pParser)));
tsepez4540fba2016-08-16 11:12:21 -0700536 CPDF_Parser::Error error =
dsinclaircedaa552016-08-24 11:12:19 -0700537 pDocument->GetParser()->StartParse(pMemFile, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800538 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800539 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700540 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 }
dsinclaircedaa552016-08-24 11:12:19 -0700542 CheckUnSupportError(pDocument.get(), error);
543 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700544}
545
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546DLLEXPORT FPDF_DOCUMENT STDCALL
547FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
548 FPDF_BYTESTRING password) {
Tom Sepezae51c812015-08-05 12:34:06 -0700549 CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
tsepez4540fba2016-08-16 11:12:21 -0700550 std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
551 pParser->SetPassword(password);
552
dsinclaircedaa552016-08-24 11:12:19 -0700553 std::unique_ptr<CPDF_Document> pDocument(
554 new CPDF_Document(std::move(pParser)));
555 CPDF_Parser::Error error =
556 pDocument->GetParser()->StartParse(pFile, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800557 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800558 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700559 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560 }
dsinclaircedaa552016-08-24 11:12:19 -0700561 CheckUnSupportError(pDocument.get(), error);
562 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700563}
564
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
566 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700567 if (!fileVersion)
tsepez4cf55152016-11-02 14:37:54 -0700568 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700569
Tom Sepez471a1032015-10-15 16:17:18 -0700570 *fileVersion = 0;
571 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
572 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700573 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700574
575 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576 if (!pParser)
tsepez4cf55152016-11-02 14:37:54 -0700577 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700578
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579 *fileVersion = pParser->GetFileVersion();
tsepez4cf55152016-11-02 14:37:54 -0700580 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700581}
582
tsepezc3255f52016-03-25 14:52:27 -0700583// jabdelmalek: changed return type from uint32_t to build on Linux (and match
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584// header).
585DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700586 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestig27ddf162016-05-23 15:06:59 -0700587 // https://bugs.chromium.org/p/pdfium/issues/detail?id=499
588 if (!pDoc) {
Tom Sepez51da0932015-11-25 16:05:49 -0800589#ifndef PDF_ENABLE_XFA
590 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800591#else // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700592 return 0xFFFFFFFF;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800593#endif // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700594 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700595
thestig27ddf162016-05-23 15:06:59 -0700596 return pDoc->GetUserPermissions();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700597}
598
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700600 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700601 if (!pDoc || !pDoc->GetParser())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700603
Tom Sepez471a1032015-10-15 16:17:18 -0700604 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
dsinclair38fd8442016-09-15 10:15:32 -0700605 return pDict ? pDict->GetIntegerFor("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700606}
607
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800609 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700610 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700611}
612
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
614 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800615 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
616 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700617 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800618
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700619 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700620 return nullptr;
621
Tom Sepez40e9ff32015-11-30 12:39:54 -0800622#ifdef PDF_ENABLE_XFA
dsinclair9f206f02016-09-20 12:17:42 -0700623 return pDoc->GetXFAPage(page_index);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800624#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800625 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800626 if (!pDict)
thestig5cc24652016-04-26 11:46:02 -0700627 return nullptr;
628
629 CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true);
630 pPage->ParseContent();
Tom Sepez51da0932015-11-25 16:05:49 -0800631 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800632#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700633}
634
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700635DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800636 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700637 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700638}
639
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800641 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700642 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700643}
644
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700645#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
647 FPDF_PAGE page,
648 int start_x,
649 int start_y,
650 int size_x,
651 int size_y,
652 int rotate,
653 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700654 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700655 if (!pPage)
656 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700657
weili9f515bc2016-07-24 08:08:24 -0700658 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700659 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700660
thestig84144e82016-09-27 15:06:01 -0700661 std::unique_ptr<CFX_DIBitmap> pBitmap;
662 const bool bNewBitmap =
663 pPage->BackgroundAlphaNeeded() || pPage->HasImageMask();
664 if (bNewBitmap) {
tsepez36eb4bd2016-10-03 15:24:27 -0700665 pBitmap = pdfium::MakeUnique<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700666 pBitmap->Create(size_x, size_y, FXDIB_Argb);
667 pBitmap->Clear(0x00ffffff);
thestigbefa4502016-05-26 20:15:19 -0700668 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
tsepez36eb4bd2016-10-03 15:24:27 -0700669 pContext->m_pDevice = pdfium::WrapUnique(pDevice);
thestig84144e82016-09-27 15:06:01 -0700670 pDevice->Attach(pBitmap.get(), false, nullptr, false);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800671 } else {
tsepez36eb4bd2016-10-03 15:24:27 -0700672 pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsDevice>(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800673 }
Bo Xud4e406e2014-08-13 11:03:19 -0700674
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700676 rotate, flags, true, nullptr);
Bo Xud4e406e2014-08-13 11:03:19 -0700677
thestig84144e82016-09-27 15:06:01 -0700678 if (bNewBitmap) {
679 CFX_WindowsDevice WinDC(dc);
680 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
tsepez36eb4bd2016-10-03 15:24:27 -0700681 std::unique_ptr<CFX_DIBitmap> pDst = pdfium::MakeUnique<CFX_DIBitmap>();
thestig84144e82016-09-27 15:06:01 -0700682 int pitch = pBitmap->GetPitch();
683 pDst->Create(size_x, size_y, FXDIB_Rgb32);
684 FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
685 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap.get(), 0, 0,
tsepez4cf55152016-11-02 14:37:54 -0700686 FXDIB_BLEND_NORMAL, nullptr, false, nullptr);
thestig84144e82016-09-27 15:06:01 -0700687 WinDC.StretchDIBits(pDst.get(), 0, 0, size_x, size_y);
688 } else {
689 WinDC.SetDIBits(pBitmap.get(), 0, 0);
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700690 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700691 }
Jun Fang1aeeceb2015-12-29 10:27:44 +0800692
tsepeze5cb0b12016-10-26 15:06:11 -0700693 pPage->SetRenderContext(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700694}
Lei Zhangae85f872016-02-19 14:57:07 -0800695#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700696
697DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
698 FPDF_PAGE page,
699 int start_x,
700 int start_y,
701 int size_x,
702 int size_y,
703 int rotate,
704 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700705 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700706 return;
tsepez1e2c5572016-05-25 14:58:09 -0700707
Tom Sepezdb0be962015-10-16 14:00:21 -0700708 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700709 if (!pPage)
710 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711
weili9f515bc2016-07-24 08:08:24 -0700712 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700713 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
thestigbefa4502016-05-26 20:15:19 -0700714 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
weili9f515bc2016-07-24 08:08:24 -0700715 pContext->m_pDevice.reset(pDevice);
thestigbefa4502016-05-26 20:15:19 -0700716 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
717 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718
719 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700720 rotate, flags, true, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721
caryclark687fbde2016-11-22 12:44:25 -0800722#ifdef _SKIA_SUPPORT_PATHS_
caryclark8f875502016-12-06 13:49:34 -0800723 pDevice->Flush();
caryclark687fbde2016-11-22 12:44:25 -0800724 pBitmap->UnPreMultiply();
725#endif
tsepeze5cb0b12016-10-26 15:06:11 -0700726 pPage->SetRenderContext(nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700727}
728
thestiga78ba602016-11-23 15:25:48 -0800729DLLEXPORT void STDCALL FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap,
730 FPDF_PAGE page,
731 const FS_MATRIX* matrix,
732 const FS_RECTF* clipping,
733 int flags) {
734 if (!bitmap)
735 return;
736
737 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
738 if (!pPage)
739 return;
740
741 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
742 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
743 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
744 pContext->m_pDevice.reset(pDevice);
745 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
746 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
747
748 CFX_Matrix transform_matrix = pPage->GetPageMatrix();
749 if (matrix) {
750 CFX_Matrix cmatrix;
751 cmatrix.a = matrix->a;
752 cmatrix.b = matrix->b;
753 cmatrix.c = matrix->c;
754 cmatrix.d = matrix->d;
755 cmatrix.e = matrix->e;
756 cmatrix.f = matrix->f;
757 transform_matrix.Concat(cmatrix);
758 }
759
760 CFX_FloatRect clipping_rect;
761 if (clipping) {
762 clipping_rect.left = clipping->left;
763 clipping_rect.bottom = clipping->bottom;
764 clipping_rect.right = clipping->right;
765 clipping_rect.top = clipping->top;
766 }
767 RenderPageImpl(pContext, pPage, transform_matrix, clipping_rect.ToFxRect(),
768 flags, true, nullptr);
769
770 pPage->SetRenderContext(nullptr);
771}
772
Cary Clark399be5b2016-03-14 16:51:29 -0400773#ifdef _SKIA_SUPPORT_
774DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page,
775 int size_x,
776 int size_y) {
777 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
778 if (!pPage)
779 return nullptr;
tsepez1e2c5572016-05-25 14:58:09 -0700780
weili9f515bc2016-07-24 08:08:24 -0700781 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700782 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
caryclarkd6e18872016-05-13 10:57:20 -0700783 CFX_FxgeDevice* skDevice = new CFX_FxgeDevice;
Cary Clark399be5b2016-03-14 16:51:29 -0400784 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
weili9f515bc2016-07-24 08:08:24 -0700785 pContext->m_pDevice.reset(skDevice);
tsepez4cf55152016-11-02 14:37:54 -0700786 FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, true,
tsepez1e2c5572016-05-25 14:58:09 -0700787 nullptr);
tsepeze5cb0b12016-10-26 15:06:11 -0700788 pPage->SetRenderContext(nullptr);
Cary Clark399be5b2016-03-14 16:51:29 -0400789 return recorder;
790}
791#endif
792
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700793DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
tsepez1e2c5572016-05-25 14:58:09 -0700794 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700795 if (!page)
796 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800797#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -0800798 pPage->Release();
799#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800800 CPDFSDK_PageView* pPageView =
tsepez1e2c5572016-05-25 14:58:09 -0700801 static_cast<CPDFSDK_PageView*>(pPage->GetView());
dsinclair1df1efa2016-09-07 09:55:37 -0700802 if (pPageView) {
dsinclairbcf46232016-10-03 13:02:27 -0700803 // We're already destroying the pageview, so bail early.
804 if (pPageView->IsBeingDestroyed())
805 return;
806
dsinclair1df1efa2016-09-07 09:55:37 -0700807 if (pPageView->IsLocked()) {
808 pPageView->TakePageOwnership();
809 return;
810 }
811
812 bool owned = pPageView->OwnsPage();
813 // This will delete the |pPageView| object. We must cleanup the PageView
814 // first because it will attempt to reset the View on the |pPage| during
815 // destruction.
dsinclair7cbe68e2016-10-12 11:56:23 -0700816 pPageView->GetFormFillEnv()->RemovePageView(pPage);
dsinclair1df1efa2016-09-07 09:55:37 -0700817 // If the page was owned then the pageview will have deleted the page.
818 if (owned)
819 return;
Tom Sepez51da0932015-11-25 16:05:49 -0800820 }
tsepez1e2c5572016-05-25 14:58:09 -0700821 delete pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800822#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700823}
824
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Jun Fangfc751362015-12-16 21:23:39 -0800826 delete UnderlyingFromFPDFDocument(document);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700827}
828
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
830 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700831}
832
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700833DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
834 int start_x,
835 int start_y,
836 int size_x,
837 int size_y,
838 int rotate,
839 int device_x,
840 int device_y,
841 double* page_x,
842 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -0800843 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700844 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800845 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800846#ifdef PDF_ENABLE_XFA
847 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
848 device_y, page_x, page_y);
849#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800850 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800851 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
852 rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800853 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800854 device2page.SetReverse(page2device);
Tom Sepez51da0932015-11-25 16:05:49 -0800855 FX_FLOAT page_x_f, page_y_f;
856 device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
857 page_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800858 *page_x = (page_x_f);
859 *page_y = (page_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800860#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700861}
862
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700863DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
864 int start_x,
865 int start_y,
866 int size_x,
867 int size_y,
868 int rotate,
869 double page_x,
870 double page_y,
871 int* device_x,
872 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700873 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800875 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700876 if (!pPage)
877 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800878#ifdef PDF_ENABLE_XFA
879 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
880 device_x, device_y);
881#else // PDF_ENABLE_XFA
Tom Sepez60d909e2015-12-10 15:34:55 -0800882 CFX_Matrix page2device;
Tom Sepez51da0932015-11-25 16:05:49 -0800883 pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
884 rotate);
Tom Sepez51da0932015-11-25 16:05:49 -0800885 FX_FLOAT device_x_f, device_y_f;
886 page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
887 device_y_f);
Tom Sepez51da0932015-11-25 16:05:49 -0800888 *device_x = FXSYS_round(device_x_f);
889 *device_y = FXSYS_round(device_y_f);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800890#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700891}
892
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
894 int height,
895 int alpha) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800896 std::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
thestig1cd352e2016-06-07 17:53:06 -0700898 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 }
900 return pBitmap.release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700901}
902
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
904 int height,
905 int format,
906 void* first_scan,
907 int stride) {
908 FXDIB_Format fx_format;
909 switch (format) {
910 case FPDFBitmap_Gray:
911 fx_format = FXDIB_8bppRgb;
912 break;
913 case FPDFBitmap_BGR:
914 fx_format = FXDIB_Rgb;
915 break;
916 case FPDFBitmap_BGRx:
917 fx_format = FXDIB_Rgb32;
918 break;
919 case FPDFBitmap_BGRA:
920 fx_format = FXDIB_Argb;
921 break;
922 default:
thestig1cd352e2016-06-07 17:53:06 -0700923 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700924 }
925 CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
926 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
927 return pBitmap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700928}
929
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
931 int left,
932 int top,
933 int width,
934 int height,
935 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800936 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 return;
thestigbefa4502016-05-26 20:15:19 -0700938
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700939 CFX_FxgeDevice device;
thestigbefa4502016-05-26 20:15:19 -0700940 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
941 device.Attach(pBitmap, false, nullptr, false);
942 if (!pBitmap->HasAlpha())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700943 color |= 0xFF000000;
944 FX_RECT rect(left, top, left + width, top + height);
945 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700946}
947
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700948DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700949 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700950}
951
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700953 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700954}
955
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700956DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700957 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958}
959
960DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700961 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700962}
963
964DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700965 delete CFXBitmapFromFPDFBitmap(bitmap);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966}
967
weili9f515bc2016-07-24 08:08:24 -0700968void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700969 FPDF_PAGE page,
970 int start_x,
971 int start_y,
972 int size_x,
973 int size_y,
974 int rotate,
975 int flags,
tsepez4cf55152016-11-02 14:37:54 -0700976 bool bNeedToRestore,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700978 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
979 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 return;
981
Tom Sepez60d909e2015-12-10 15:34:55 -0800982 CFX_Matrix matrix;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
thestiga78ba602016-11-23 15:25:48 -0800984 FX_RECT rect(start_x, start_y, start_x + size_x, start_y + size_y);
985 RenderPageImpl(pContext, pPage, matrix, rect, flags, bNeedToRestore, pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986}
987
988DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
989 int page_index,
990 double* width,
991 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -0800992 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
993 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700994 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700995
Tom Sepez40e9ff32015-11-30 12:39:54 -0800996#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997 int count = pDoc->GetPageCount();
998 if (page_index < 0 || page_index >= count)
tsepez4cf55152016-11-02 14:37:54 -0700999 return false;
dsinclair9f206f02016-09-20 12:17:42 -07001000 CPDFXFA_Page* pPage = pDoc->GetXFAPage(page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001001 if (!pPage)
tsepez4cf55152016-11-02 14:37:54 -07001002 return false;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001003 *width = pPage->GetPageWidth();
1004 *height = pPage->GetPageHeight();
1005#else // PDF_ENABLE_XFA
1006 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
1007 if (!pDict)
tsepez4cf55152016-11-02 14:37:54 -07001008 return false;
thestig5cc24652016-04-26 11:46:02 -07001009
1010 CPDF_Page page(pDoc, pDict, true);
Tom Sepez51da0932015-11-25 16:05:49 -08001011 *width = page.GetPageWidth();
1012 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001013#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014
tsepez4cf55152016-11-02 14:37:54 -07001015 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016}
1017
1018DLLEXPORT FPDF_BOOL STDCALL
1019FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001020 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001021 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001022 return true;
Tom Sepez471a1032015-10-15 16:17:18 -07001023 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001024 return viewRef.PrintScaling();
1025}
1026
1027DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001028 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029 if (!pDoc)
1030 return 1;
1031 CPDF_ViewerPreferences viewRef(pDoc);
1032 return viewRef.NumCopies();
1033}
1034
1035DLLEXPORT FPDF_PAGERANGE STDCALL
1036FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001037 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -07001039 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 CPDF_ViewerPreferences viewRef(pDoc);
1041 return viewRef.PrintPageRange();
1042}
1043
1044DLLEXPORT FPDF_DUPLEXTYPE STDCALL
1045FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001046 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -07001048 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 CPDF_ViewerPreferences viewRef(pDoc);
1050 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001051 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001052 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001053 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001054 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001055 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056 return DuplexFlipLongEdge;
1057 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001058}
1059
thestig04bebfe2016-11-04 16:07:25 -07001060DLLEXPORT unsigned long STDCALL FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document,
1061 FPDF_BYTESTRING key,
1062 char* buffer,
1063 unsigned long length) {
1064 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1065 if (!pDoc)
1066 return 0;
1067
1068 CPDF_ViewerPreferences viewRef(pDoc);
1069 CFX_ByteString bsVal;
1070 if (!viewRef.GenericName(key, &bsVal))
1071 return 0;
1072
1073 unsigned long dwStringLen = bsVal.GetLength() + 1;
1074 if (buffer && length >= dwStringLen)
1075 memcpy(buffer, bsVal.c_str(), dwStringLen);
1076 return dwStringLen;
1077}
1078
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001080 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1081 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001083
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1085 if (!pRoot)
1086 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001087
Lei Zhangd983b092015-12-14 16:58:33 -08001088 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001089 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
dsinclair38fd8442016-09-15 10:15:32 -07001090 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001091 if (pDest)
1092 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001093
1094 if (!count.IsValid())
1095 return 0;
1096
1097 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001098}
1099
1100DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1101 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001102 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001103 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104
Tom Sepez471a1032015-10-15 16:17:18 -07001105 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1106 if (!pDoc)
1107 return nullptr;
1108
Lei Zhangd983b092015-12-14 16:58:33 -08001109 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001110 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111}
1112
Tom Sepez51da0932015-11-25 16:05:49 -08001113#ifdef PDF_ENABLE_XFA
thestig77d148d2016-04-06 06:28:31 -07001114DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Init(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001115 if (!str)
1116 return -1;
1117
1118 FXSYS_memset(str, 0, sizeof(FPDF_BSTR));
1119 return 0;
1120}
1121
thestig77d148d2016-04-06 06:28:31 -07001122DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Set(FPDF_BSTR* str,
1123 FPDF_LPCSTR bstr,
1124 int length) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001125 if (!str)
1126 return -1;
1127 if (!bstr || !length)
1128 return -1;
1129 if (length == -1)
1130 length = FXSYS_strlen(bstr);
1131
1132 if (length == 0) {
1133 if (str->str) {
1134 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001135 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001136 }
1137 str->len = 0;
1138 return 0;
1139 }
1140
1141 if (str->str && str->len < length)
1142 str->str = FX_Realloc(char, str->str, length + 1);
1143 else if (!str->str)
1144 str->str = FX_Alloc(char, length + 1);
1145
1146 str->str[length] = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001147 FXSYS_memcpy(str->str, bstr, length);
1148 str->len = length;
1149
1150 return 0;
1151}
1152
thestig77d148d2016-04-06 06:28:31 -07001153DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Clear(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001154 if (!str)
1155 return -1;
1156
1157 if (str->str) {
1158 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001159 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 }
1161 str->len = 0;
1162 return 0;
1163}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001164#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165
1166DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1167 int index,
1168 void* buffer,
1169 long* buflen) {
1170 if (!buffer)
1171 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001172
1173 if (index < 0)
1174 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175
Tom Sepezbf59a072015-10-21 14:07:23 -07001176 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001177 if (!pDoc)
1178 return nullptr;
1179
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1181 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001182 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001183
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001184 CPDF_Object* pDestObj = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001185 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001186 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001187 int count = nameTree.GetCount();
1188 if (index >= count) {
dsinclair38fd8442016-09-15 10:15:32 -07001189 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001191 return nullptr;
1192
1193 pdfium::base::CheckedNumeric<int> checked_count = count;
1194 checked_count += pDest->GetCount();
1195 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1196 return nullptr;
1197
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001198 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001199 int i = 0;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001200 for (const auto& it : *pDest) {
1201 bsName = it.first;
tsepez0e606b52016-11-18 16:22:41 -08001202 pDestObj = it.second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001203 if (!pDestObj)
1204 continue;
1205 if (i == index)
1206 break;
1207 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001208 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001209 } else {
1210 pDestObj = nameTree.LookupValue(index, bsName);
1211 }
1212 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001213 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001214 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
dsinclair38fd8442016-09-15 10:15:32 -07001215 pDestObj = pDict->GetArrayFor("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001217 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001218 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001219 if (!pDestObj->IsArray())
1220 return nullptr;
1221
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 CFX_WideString wsName = PDF_DecodeText(bsName);
1223 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -07001224 int len = utf16Name.GetLength();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225 if (!buffer) {
1226 *buflen = len;
thestig9067fd62016-11-23 14:10:06 -08001227 } else if (len <= *buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001228 memcpy(buffer, utf16Name.c_str(), len);
1229 *buflen = len;
1230 } else {
1231 *buflen = -1;
1232 }
1233 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001234}