blob: 5010c17ceb8e635b142c966c868e3c7dff2a66f7 [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"
Chris Palmere4b035b2017-03-26 15:48:34 -070036#include "third_party/base/allocator/partition_allocator/partition_alloc.h"
Lei Zhang8241df72015-11-06 14:38:48 -080037#include "third_party/base/numerics/safe_conversions_impl.h"
tsepez36eb4bd2016-10-03 15:24:27 -070038#include "third_party/base/ptr_util.h"
Bo Xufdc00a72014-10-28 23:03:33 -070039
Tom Sepez40e9ff32015-11-30 12:39:54 -080040#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070041#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070042#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
43#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
Dan Sinclaire7786682017-03-29 15:18:41 -040044#include "fxbarcode/BC_Library.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080045#include "public/fpdf_formfill.h"
46#endif // PDF_ENABLE_XFA
47
Tom Sepezb4a26182017-03-01 12:15:00 -080048#ifdef PDF_ENABLE_XFA_BMP
49#include "core/fxcodec/codec/ccodec_bmpmodule.h"
50#endif
51
52#ifdef PDF_ENABLE_XFA_GIF
53#include "core/fxcodec/codec/ccodec_gifmodule.h"
54#endif
55
56#ifdef PDF_ENABLE_XFA_PNG
57#include "core/fxcodec/codec/ccodec_pngmodule.h"
58#endif
59
60#ifdef PDF_ENABLE_XFA_TIFF
61#include "core/fxcodec/codec/ccodec_tiffmodule.h"
62#endif
63
thestig25fa42f2016-05-25 21:39:46 -070064#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
dsinclair74a34fc2016-09-29 16:41:42 -070065#include "core/fxge/cfx_windowsdevice.h"
thestig25fa42f2016-05-25 21:39:46 -070066#endif
67
thestiga78ba602016-11-23 15:25:48 -080068namespace {
69
tsepez02759102016-12-01 08:29:25 -080070// Also indicates whether library is currently initialized.
71CCodec_ModuleMgr* g_pCodecModule = nullptr;
72
thestiga78ba602016-11-23 15:25:48 -080073void RenderPageImpl(CPDF_PageRenderContext* pContext,
74 CPDF_Page* pPage,
75 const CFX_Matrix& matrix,
76 const FX_RECT& clipping_rect,
77 int flags,
78 bool bNeedToRestore,
79 IFSDK_PAUSE_Adapter* pause) {
80 if (!pContext->m_pOptions)
81 pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
82
83 if (flags & FPDF_LCD_TEXT)
84 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
85 else
86 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
87
88 if (flags & FPDF_NO_NATIVETEXT)
89 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
90 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
91 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
92 if (flags & FPDF_RENDER_FORCEHALFTONE)
93 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
94#ifndef PDF_ENABLE_XFA
95 if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
96 pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
97 if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
98 pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
99 if (flags & FPDF_RENDER_NO_SMOOTHPATH)
100 pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
101#endif // PDF_ENABLE_XFA
102
103 // Grayscale output
104 if (flags & FPDF_GRAYSCALE) {
105 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
106 pContext->m_pOptions->m_ForeColor = 0;
107 pContext->m_pOptions->m_BackColor = 0xffffff;
108 }
109
110 const CPDF_OCContext::UsageType usage =
111 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
112 pContext->m_pOptions->m_AddFlags = flags >> 8;
113 pContext->m_pOptions->m_pOCContext =
Tom Sepezf716f0b2017-01-30 10:04:07 -0800114 pdfium::MakeRetain<CPDF_OCContext>(pPage->m_pDocument, usage);
thestiga78ba602016-11-23 15:25:48 -0800115
116 pContext->m_pDevice->SaveState();
117 pContext->m_pDevice->SetClip_Rect(clipping_rect);
118
119 pContext->m_pContext = pdfium::MakeUnique<CPDF_RenderContext>(pPage);
120 pContext->m_pContext->AppendLayer(pPage, &matrix);
121
122 if (flags & FPDF_ANNOT) {
123 pContext->m_pAnnots = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
124 bool bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
125 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext.get(),
126 bPrinting, &matrix, false, nullptr);
127 }
128
129 pContext->m_pRenderer = pdfium::MakeUnique<CPDF_ProgressiveRenderer>(
130 pContext->m_pContext.get(), pContext->m_pDevice.get(),
131 pContext->m_pOptions.get());
132 pContext->m_pRenderer->Start(pause);
133 if (bNeedToRestore)
134 pContext->m_pDevice->RestoreState(false);
135}
136
tsepezbea04972016-12-01 13:54:42 -0800137class CPDF_CustomAccess final : public IFX_SeekableReadStream {
138 public:
tsepez833619b2016-12-07 09:21:17 -0800139 static CFX_RetainPtr<CPDF_CustomAccess> Create(FPDF_FILEACCESS* pFileAccess) {
140 return CFX_RetainPtr<CPDF_CustomAccess>(new CPDF_CustomAccess(pFileAccess));
141 }
tsepezbea04972016-12-01 13:54:42 -0800142
143 // IFX_SeekableReadStream
144 FX_FILESIZE GetSize() override;
tsepezbea04972016-12-01 13:54:42 -0800145 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
146
147 private:
tsepez833619b2016-12-07 09:21:17 -0800148 explicit CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess);
149
tsepezbea04972016-12-01 13:54:42 -0800150 FPDF_FILEACCESS m_FileAccess;
151};
152
153CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
154 : m_FileAccess(*pFileAccess) {}
155
156FX_FILESIZE CPDF_CustomAccess::GetSize() {
157 return m_FileAccess.m_FileLen;
158}
159
tsepezbea04972016-12-01 13:54:42 -0800160bool CPDF_CustomAccess::ReadBlock(void* buffer,
161 FX_FILESIZE offset,
162 size_t size) {
163 if (offset < 0)
164 return false;
165
166 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
167 newPos += offset;
168 if (!newPos.IsValid() ||
169 newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) {
170 return false;
171 }
172 return !!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,
173 reinterpret_cast<uint8_t*>(buffer), size);
174}
175
Tom Sepez40e9ff32015-11-30 12:39:54 -0800176#ifdef PDF_ENABLE_XFA
tsepezfa89a202016-12-02 09:48:30 -0800177class CFPDF_FileStream : public IFX_SeekableStream {
178 public:
tsepez833619b2016-12-07 09:21:17 -0800179 static CFX_RetainPtr<CFPDF_FileStream> Create(FPDF_FILEHANDLER* pFS) {
180 return CFX_RetainPtr<CFPDF_FileStream>(new CFPDF_FileStream(pFS));
181 }
182 ~CFPDF_FileStream() override;
Tom Sepez471a1032015-10-15 16:17:18 -0700183
tsepezfa89a202016-12-02 09:48:30 -0800184 // IFX_SeekableStream:
tsepezfa89a202016-12-02 09:48:30 -0800185 FX_FILESIZE GetSize() override;
186 bool IsEOF() override;
187 FX_FILESIZE GetPosition() override;
188 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
189 size_t ReadBlock(void* buffer, size_t size) override;
190 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override;
191 bool Flush() override;
Tom Sepezbf59a072015-10-21 14:07:23 -0700192
tsepezfa89a202016-12-02 09:48:30 -0800193 void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; }
Tom Sepezdb0be962015-10-16 14:00:21 -0700194
tsepezfa89a202016-12-02 09:48:30 -0800195 protected:
tsepez833619b2016-12-07 09:21:17 -0800196 explicit CFPDF_FileStream(FPDF_FILEHANDLER* pFS);
197
tsepezfa89a202016-12-02 09:48:30 -0800198 FPDF_FILEHANDLER* m_pFS;
199 FX_FILESIZE m_nCurPos;
200};
thestigbefa4502016-05-26 20:15:19 -0700201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
203 m_pFS = pFS;
204 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700205}
206
tsepez833619b2016-12-07 09:21:17 -0800207CFPDF_FileStream::~CFPDF_FileStream() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 if (m_pFS && m_pFS->Release)
209 m_pFS->Release(m_pFS->clientData);
Bo Xufdc00a72014-10-28 23:03:33 -0700210}
211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212FX_FILESIZE CFPDF_FileStream::GetSize() {
213 if (m_pFS && m_pFS->GetSize)
214 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
215 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700216}
217
tsepezf39074c2016-10-26 15:33:58 -0700218bool CFPDF_FileStream::IsEOF() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -0700220}
221
weili625ad662016-06-15 11:21:33 -0700222FX_FILESIZE CFPDF_FileStream::GetPosition() {
223 return m_nCurPos;
224}
225
tsepezf39074c2016-10-26 15:33:58 -0700226bool CFPDF_FileStream::ReadBlock(void* buffer,
227 FX_FILESIZE offset,
228 size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 if (!buffer || !size || !m_pFS->ReadBlock)
tsepezf39074c2016-10-26 15:33:58 -0700230 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700231
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
233 (FPDF_DWORD)size) == 0) {
234 m_nCurPos = offset + size;
tsepezf39074c2016-10-26 15:33:58 -0700235 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 }
tsepezf39074c2016-10-26 15:33:58 -0700237 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700238}
239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) {
241 if (!buffer || !size || !m_pFS->ReadBlock)
242 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 FX_FILESIZE nSize = GetSize();
245 if (m_nCurPos >= nSize)
246 return 0;
247 FX_FILESIZE dwAvail = nSize - m_nCurPos;
248 if (dwAvail < (FX_FILESIZE)size)
249 size = (size_t)dwAvail;
250 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
251 (FPDF_DWORD)size) == 0) {
252 m_nCurPos += size;
253 return size;
254 }
Bo Xufdc00a72014-10-28 23:03:33 -0700255
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700257}
258
tsepezf39074c2016-10-26 15:33:58 -0700259bool CFPDF_FileStream::WriteBlock(const void* buffer,
260 FX_FILESIZE offset,
261 size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 if (!m_pFS || !m_pFS->WriteBlock)
tsepezf39074c2016-10-26 15:33:58 -0700263 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
266 (FPDF_DWORD)size) == 0) {
267 m_nCurPos = offset + size;
tsepezf39074c2016-10-26 15:33:58 -0700268 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 }
tsepezf39074c2016-10-26 15:33:58 -0700270 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700271}
272
tsepezf39074c2016-10-26 15:33:58 -0700273bool CFPDF_FileStream::Flush() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 if (!m_pFS || !m_pFS->Flush)
tsepezf39074c2016-10-26 15:33:58 -0700275 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700278}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800279#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280
tsepezfa89a202016-12-02 09:48:30 -0800281} // namespace
282
283UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
284 return static_cast<UnderlyingDocumentType*>(doc);
285}
286
287FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
288 return static_cast<FPDF_DOCUMENT>(doc);
289}
290
291UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
292 return static_cast<UnderlyingPageType*>(page);
293}
294
295CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
296#ifdef PDF_ENABLE_XFA
297 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
298#else // PDF_ENABLE_XFA
299 return UnderlyingFromFPDFDocument(doc);
300#endif // PDF_ENABLE_XFA
301}
302
303FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
304#ifdef PDF_ENABLE_XFA
305 return doc ? FPDFDocumentFromUnderlying(
306 new CPDFXFA_Context(pdfium::WrapUnique(doc)))
307 : nullptr;
308#else // PDF_ENABLE_XFA
309 return FPDFDocumentFromUnderlying(doc);
310#endif // PDF_ENABLE_XFA
311}
312
313CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
314#ifdef PDF_ENABLE_XFA
315 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
316#else // PDF_ENABLE_XFA
317 return UnderlyingFromFPDFPage(page);
318#endif // PDF_ENABLE_XFA
319}
320
321CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
322 return static_cast<CFX_DIBitmap*>(bitmap);
323}
324
tsepez833619b2016-12-07 09:21:17 -0800325CFX_RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream(
326 FPDF_FILEACCESS* pFileAccess) {
327 return CPDF_CustomAccess::Create(pFileAccess);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700328}
329
tsepezfa89a202016-12-02 09:48:30 -0800330#ifdef PDF_ENABLE_XFA
tsepez833619b2016-12-07 09:21:17 -0800331CFX_RetainPtr<IFX_SeekableStream> MakeSeekableStream(
332 FPDF_FILEHANDLER* pFilehandler) {
333 return CFPDF_FileStream::Create(pFilehandler);
tsepezfa89a202016-12-02 09:48:30 -0800334}
335#endif // PDF_ENABLE_XFA
336
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
tsepezc3255f52016-03-25 14:52:27 -0700338static uint32_t foxit_sandbox_policy = 0xFFFFFFFF;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700339
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
341 switch (policy) {
342 case FPDF_POLICY_MACHINETIME_ACCESS: {
343 if (enable)
344 foxit_sandbox_policy |= 0x01;
345 else
346 foxit_sandbox_policy &= 0xFFFFFFFE;
347 } break;
348 default:
349 break;
350 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351}
352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
354 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700355 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700356 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 default:
tsepez4cf55152016-11-02 14:37:54 -0700358 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700360}
361
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362DLLEXPORT void STDCALL FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700363 FPDF_InitLibraryWithConfig(nullptr);
364}
365
Dan Sinclairf766ad22016-03-14 13:51:24 -0400366DLLEXPORT void STDCALL
367FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
tsepez02759102016-12-01 08:29:25 -0800368 if (g_pCodecModule)
369 return;
370
Chris Palmer2b797292017-04-06 14:45:39 -0700371 FXMEM_InitalizePartitionAlloc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 g_pCodecModule = new CCodec_ModuleMgr();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373
weili47228ac2016-07-20 10:35:31 -0700374 CFX_GEModule* pModule = CFX_GEModule::Get();
375 pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr, g_pCodecModule);
tsepez02759102016-12-01 08:29:25 -0800376
Tom Sepez1b246282015-11-25 15:15:31 -0800377 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
378 pModuleMgr->SetCodecModule(g_pCodecModule);
379 pModuleMgr->InitPageModule();
dsinclairab5b60e2016-06-23 08:18:36 -0700380 pModuleMgr->LoadEmbeddedGB1CMaps();
381 pModuleMgr->LoadEmbeddedJapan1CMaps();
382 pModuleMgr->LoadEmbeddedCNS1CMaps();
383 pModuleMgr->LoadEmbeddedKorea1CMaps();
dsinclaird647a6b2016-04-26 13:13:20 -0700384
Tom Sepezb4a26182017-03-01 12:15:00 -0800385#ifdef PDF_ENABLE_XFA_BMP
386 pModuleMgr->GetCodecModule()->SetBmpModule(
387 pdfium::MakeUnique<CCodec_BmpModule>());
388#endif
389
390#ifdef PDF_ENABLE_XFA_GIF
391 pModuleMgr->GetCodecModule()->SetGifModule(
392 pdfium::MakeUnique<CCodec_GifModule>());
393#endif
394
395#ifdef PDF_ENABLE_XFA_PNG
396 pModuleMgr->GetCodecModule()->SetPngModule(
397 pdfium::MakeUnique<CCodec_PngModule>());
398#endif
399
400#ifdef PDF_ENABLE_XFA_TIFF
401 pModuleMgr->GetCodecModule()->SetTiffModule(
402 pdfium::MakeUnique<CCodec_TiffModule>());
403#endif
404
Tom Sepez40e9ff32015-11-30 12:39:54 -0800405#ifdef PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700406 FXJSE_Initialize();
407 BC_Library_Init();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800408#endif // PDF_ENABLE_XFA
Tom Sepez452b4f32015-10-13 09:27:27 -0700409 if (cfg && cfg->version >= 2)
410 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411}
412
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
tsepez02759102016-12-01 08:29:25 -0800414 if (!g_pCodecModule)
415 return;
416
Tom Sepez51da0932015-11-25 16:05:49 -0800417#ifdef PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700418 BC_Library_Destory();
419 FXJSE_Finalize();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800420#endif // PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700421
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700422 CPDF_ModuleMgr::Destroy();
423 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700424
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 delete g_pCodecModule;
426 g_pCodecModule = nullptr;
thestig2d6dda12016-06-28 07:39:09 -0700427
428 IJS_Runtime::Destroy();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700429}
430
431#ifndef _WIN32
432int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433void SetLastError(int err) {
434 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435}
436
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437int GetLastError() {
438 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700439}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800440#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700441
Tom Sepezf10ae632016-01-26 14:19:52 -0800442void ProcessParseError(CPDF_Parser::Error err) {
tsepezc3255f52016-03-25 14:52:27 -0700443 uint32_t err_code = FPDF_ERR_SUCCESS;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800445 switch (err) {
446 case CPDF_Parser::SUCCESS:
447 err_code = FPDF_ERR_SUCCESS;
448 break;
449 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 err_code = FPDF_ERR_FILE;
451 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800452 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 err_code = FPDF_ERR_FORMAT;
454 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800455 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 err_code = FPDF_ERR_PASSWORD;
457 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800458 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 err_code = FPDF_ERR_SECURITY;
460 break;
461 }
462 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700463}
464
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
466 FPDF_BOOL enable) {
467 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700468}
469
rbpotterdb764702017-01-12 10:31:43 -0800470#if defined(_WIN32)
471#if defined(PDFIUM_PRINT_TEXT_WITH_GDI)
thestigfdb35ff2016-07-18 13:45:44 -0700472DLLEXPORT void STDCALL
473FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) {
474 g_pdfium_typeface_accessible_func = func;
475}
476
477DLLEXPORT void STDCALL FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) {
478 g_pdfium_print_text_with_gdi = !!use_gdi;
479}
rbpotterdb764702017-01-12 10:31:43 -0800480#endif // PDFIUM_PRINT_TEXT_WITH_GDI
481
482DLLEXPORT FPDF_BOOL STDCALL FPDF_SetPrintPostscriptLevel(int postscript_level) {
483 if (postscript_level != 0 && postscript_level != 2 && postscript_level != 3)
484 return FALSE;
485 g_pdfium_print_postscript_level = postscript_level;
486 return TRUE;
487}
488#endif // defined(_WIN32)
thestigfdb35ff2016-07-18 13:45:44 -0700489
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
491 FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700492 // NOTE: the creation of the file needs to be by the embedder on the
493 // other side of this API.
tsepez833619b2016-12-07 09:21:17 -0800494 CFX_RetainPtr<IFX_SeekableReadStream> pFileAccess =
Dan Sinclair812e96c2017-03-13 16:43:37 -0400495 IFX_SeekableReadStream::CreateFromFilename((const char*)file_path);
tsepez345d4892016-11-30 15:10:55 -0800496 if (!pFileAccess)
Tom Sepeze3166a82015-08-05 10:50:32 -0700497 return nullptr;
Tom Sepeze3166a82015-08-05 10:50:32 -0700498
tsepez345d4892016-11-30 15:10:55 -0800499 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500 pParser->SetPassword(password);
Bo Xud4e406e2014-08-13 11:03:19 -0700501
tsepez345d4892016-11-30 15:10:55 -0800502 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
dsinclair156de022016-08-24 11:58:24 -0700503 CPDF_Parser::Error error =
504 pDocument->GetParser()->StartParse(pFileAccess, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800505 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800506 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700507 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 }
dsinclair8837c912016-11-01 11:22:37 -0700509 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700510}
Jun Fange118ce92015-02-17 06:50:08 -0800511
Tom Sepez40e9ff32015-11-30 12:39:54 -0800512#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700513DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document,
514 int* docType) {
515 if (!document)
tsepez4cf55152016-11-02 14:37:54 -0700516 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800517
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518 CPDF_Document* pdfDoc =
dsinclair521b7502016-11-02 13:02:28 -0700519 (static_cast<CPDFXFA_Context*>(document))->GetPDFDoc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520 if (!pdfDoc)
tsepez4cf55152016-11-02 14:37:54 -0700521 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800522
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523 CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
524 if (!pRoot)
tsepez4cf55152016-11-02 14:37:54 -0700525 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800526
dsinclair38fd8442016-09-15 10:15:32 -0700527 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528 if (!pAcroForm)
tsepez4cf55152016-11-02 14:37:54 -0700529 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800530
dsinclair38fd8442016-09-15 10:15:32 -0700531 CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700532 if (!pXFA)
tsepez4cf55152016-11-02 14:37:54 -0700533 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800534
dsinclair38fd8442016-09-15 10:15:32 -0700535 bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false);
thestigded36342016-05-23 17:54:02 -0700536 *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA;
tsepez4cf55152016-11-02 14:37:54 -0700537 return true;
Jun Fange118ce92015-02-17 06:50:08 -0800538}
539
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700540DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) {
dsinclair521b7502016-11-02 13:02:28 -0700541 return document && (static_cast<CPDFXFA_Context*>(document))->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700542}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800543#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700544
tsepezad2441e2016-10-24 10:19:11 -0700545class CMemFile final : public IFX_SeekableReadStream {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 public:
tsepez833619b2016-12-07 09:21:17 -0800547 static CFX_RetainPtr<CMemFile> Create(uint8_t* pBuf, FX_FILESIZE size) {
548 return CFX_RetainPtr<CMemFile>(new CMemFile(pBuf, size));
549 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700550
Lei Zhang3884dba2015-10-19 17:27:53 -0700551 FX_FILESIZE GetSize() override { return m_size; }
tsepezf39074c2016-10-26 15:33:58 -0700552 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
tsepez833619b2016-12-07 09:21:17 -0800553 if (offset < 0)
tsepezf39074c2016-10-26 15:33:58 -0700554 return false;
tsepez833619b2016-12-07 09:21:17 -0800555
tsepez4e597c82016-11-07 15:16:01 -0800556 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557 newPos += offset;
tsepez833619b2016-12-07 09:21:17 -0800558 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size)
tsepezf39074c2016-10-26 15:33:58 -0700559 return false;
tsepez833619b2016-12-07 09:21:17 -0800560
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400561 memcpy(buffer, m_pBuf + offset, size);
tsepezf39074c2016-10-26 15:33:58 -0700562 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700564
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565 private:
tsepez833619b2016-12-07 09:21:17 -0800566 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700567
Lei Zhang3884dba2015-10-19 17:27:53 -0700568 uint8_t* const m_pBuf;
569 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700570};
Lei Zhang3884dba2015-10-19 17:27:53 -0700571
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
573 int size,
574 FPDF_BYTESTRING password) {
tsepez833619b2016-12-07 09:21:17 -0800575 CFX_RetainPtr<CMemFile> pMemFile = CMemFile::Create((uint8_t*)data_buf, size);
576 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
tsepez4540fba2016-08-16 11:12:21 -0700577 pParser->SetPassword(password);
578
tsepez833619b2016-12-07 09:21:17 -0800579 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
tsepez4540fba2016-08-16 11:12:21 -0700580 CPDF_Parser::Error error =
dsinclaircedaa552016-08-24 11:12:19 -0700581 pDocument->GetParser()->StartParse(pMemFile, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800582 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800583 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700584 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585 }
dsinclaircedaa552016-08-24 11:12:19 -0700586 CheckUnSupportError(pDocument.get(), error);
587 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700588}
589
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590DLLEXPORT FPDF_DOCUMENT STDCALL
591FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
592 FPDF_BYTESTRING password) {
tsepez833619b2016-12-07 09:21:17 -0800593 CFX_RetainPtr<CPDF_CustomAccess> pFile =
594 CPDF_CustomAccess::Create(pFileAccess);
595 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
tsepez4540fba2016-08-16 11:12:21 -0700596 pParser->SetPassword(password);
597
tsepez833619b2016-12-07 09:21:17 -0800598 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
dsinclaircedaa552016-08-24 11:12:19 -0700599 CPDF_Parser::Error error =
600 pDocument->GetParser()->StartParse(pFile, pDocument.get());
Tom Sepezf10ae632016-01-26 14:19:52 -0800601 if (error != CPDF_Parser::SUCCESS) {
Tom Sepezf10ae632016-01-26 14:19:52 -0800602 ProcessParseError(error);
thestig1cd352e2016-06-07 17:53:06 -0700603 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 }
dsinclaircedaa552016-08-24 11:12:19 -0700605 CheckUnSupportError(pDocument.get(), error);
606 return FPDFDocumentFromCPDFDocument(pDocument.release());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700607}
608
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
610 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700611 if (!fileVersion)
tsepez4cf55152016-11-02 14:37:54 -0700612 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700613
Tom Sepez471a1032015-10-15 16:17:18 -0700614 *fileVersion = 0;
615 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
616 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700617 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700618
619 CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700620 if (!pParser)
tsepez4cf55152016-11-02 14:37:54 -0700621 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700622
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700623 *fileVersion = pParser->GetFileVersion();
tsepez4cf55152016-11-02 14:37:54 -0700624 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700625}
626
tsepezc3255f52016-03-25 14:52:27 -0700627// jabdelmalek: changed return type from uint32_t to build on Linux (and match
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700628// header).
629DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700630 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestig27ddf162016-05-23 15:06:59 -0700631 // https://bugs.chromium.org/p/pdfium/issues/detail?id=499
632 if (!pDoc) {
Tom Sepez51da0932015-11-25 16:05:49 -0800633#ifndef PDF_ENABLE_XFA
634 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800635#else // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700636 return 0xFFFFFFFF;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800637#endif // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700638 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700639
thestig27ddf162016-05-23 15:06:59 -0700640 return pDoc->GetUserPermissions();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700641}
642
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700644 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700645 if (!pDoc || !pDoc->GetParser())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700647
Tom Sepez471a1032015-10-15 16:17:18 -0700648 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
dsinclair38fd8442016-09-15 10:15:32 -0700649 return pDict ? pDict->GetIntegerFor("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700650}
651
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800653 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700654 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700655}
656
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700657DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
658 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800659 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
660 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700661 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800662
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700663 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700664 return nullptr;
665
Tom Sepez40e9ff32015-11-30 12:39:54 -0800666#ifdef PDF_ENABLE_XFA
dsinclair9f206f02016-09-20 12:17:42 -0700667 return pDoc->GetXFAPage(page_index);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800668#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800669 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800670 if (!pDict)
thestig5cc24652016-04-26 11:46:02 -0700671 return nullptr;
672
673 CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true);
674 pPage->ParseContent();
Tom Sepez51da0932015-11-25 16:05:49 -0800675 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800676#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700677}
678
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800680 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700681 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700682}
683
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700684DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800685 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700686 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700687}
688
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700689#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
691 FPDF_PAGE page,
692 int start_x,
693 int start_y,
694 int size_x,
695 int size_y,
696 int rotate,
697 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700698 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700699 if (!pPage)
700 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700701
weili9f515bc2016-07-24 08:08:24 -0700702 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700703 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700704
Tom Sepezf0799fe2017-03-28 09:31:32 -0700705 CFX_RetainPtr<CFX_DIBitmap> pBitmap;
rbpotterdb764702017-01-12 10:31:43 -0800706 // TODO: This results in unnecessary rasterization of some PDFs due to
707 // HasImageMask() returning true. If any image on the page is a mask, the
708 // entire page gets rasterized and the spool size gets huge.
thestig84144e82016-09-27 15:06:01 -0700709 const bool bNewBitmap =
710 pPage->BackgroundAlphaNeeded() || pPage->HasImageMask();
711 if (bNewBitmap) {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700712 pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700713 pBitmap->Create(size_x, size_y, FXDIB_Argb);
714 pBitmap->Clear(0x00ffffff);
thestigbefa4502016-05-26 20:15:19 -0700715 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
tsepez36eb4bd2016-10-03 15:24:27 -0700716 pContext->m_pDevice = pdfium::WrapUnique(pDevice);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700717 pDevice->Attach(pBitmap, false, nullptr, false);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800718 } else {
tsepez36eb4bd2016-10-03 15:24:27 -0700719 pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsDevice>(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800720 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700722 rotate, flags, true, nullptr);
Bo Xud4e406e2014-08-13 11:03:19 -0700723
thestig84144e82016-09-27 15:06:01 -0700724 if (bNewBitmap) {
725 CFX_WindowsDevice WinDC(dc);
726 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700727 auto pDst = pdfium::MakeRetain<CFX_DIBitmap>();
thestig84144e82016-09-27 15:06:01 -0700728 int pitch = pBitmap->GetPitch();
729 pDst->Create(size_x, size_y, FXDIB_Rgb32);
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400730 memset(pDst->GetBuffer(), -1, pitch * size_y);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700731 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
Nicolas Pena6a5c20c2017-04-07 14:12:31 -0400732 FXDIB_BLEND_NORMAL, nullptr, false);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700733 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
thestig84144e82016-09-27 15:06:01 -0700734 } else {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700735 WinDC.SetDIBits(pBitmap, 0, 0);
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700736 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737 }
tsepeze5cb0b12016-10-26 15:06:11 -0700738 pPage->SetRenderContext(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700739}
Lei Zhangae85f872016-02-19 14:57:07 -0800740#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700741
742DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
743 FPDF_PAGE page,
744 int start_x,
745 int start_y,
746 int size_x,
747 int size_y,
748 int rotate,
749 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700750 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751 return;
tsepez1e2c5572016-05-25 14:58:09 -0700752
Tom Sepezdb0be962015-10-16 14:00:21 -0700753 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700754 if (!pPage)
755 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756
weili9f515bc2016-07-24 08:08:24 -0700757 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700758 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700759
thestigbefa4502016-05-26 20:15:19 -0700760 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
weili9f515bc2016-07-24 08:08:24 -0700761 pContext->m_pDevice.reset(pDevice);
dsinclair31b08d42017-03-28 15:47:47 +0000762
Tom Sepezf0799fe2017-03-28 09:31:32 -0700763 CFX_RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
764 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700765 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700766 rotate, flags, true, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767
caryclark687fbde2016-11-22 12:44:25 -0800768#ifdef _SKIA_SUPPORT_PATHS_
caryclark8f875502016-12-06 13:49:34 -0800769 pDevice->Flush();
caryclark687fbde2016-11-22 12:44:25 -0800770 pBitmap->UnPreMultiply();
771#endif
tsepeze5cb0b12016-10-26 15:06:11 -0700772 pPage->SetRenderContext(nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700773}
774
thestiga78ba602016-11-23 15:25:48 -0800775DLLEXPORT void STDCALL FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap,
776 FPDF_PAGE page,
777 const FS_MATRIX* matrix,
778 const FS_RECTF* clipping,
779 int flags) {
780 if (!bitmap)
781 return;
782
783 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
784 if (!pPage)
785 return;
786
787 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
788 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700789
thestiga78ba602016-11-23 15:25:48 -0800790 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
791 pContext->m_pDevice.reset(pDevice);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700792
793 CFX_RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
thestiga78ba602016-11-23 15:25:48 -0800794 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
795
796 CFX_Matrix transform_matrix = pPage->GetPageMatrix();
797 if (matrix) {
Dan Sinclairbba2a7c2017-02-07 16:36:39 -0500798 transform_matrix.Concat(CFX_Matrix(matrix->a, matrix->b, matrix->c,
799 matrix->d, matrix->e, matrix->f));
thestiga78ba602016-11-23 15:25:48 -0800800 }
801
802 CFX_FloatRect clipping_rect;
803 if (clipping) {
804 clipping_rect.left = clipping->left;
805 clipping_rect.bottom = clipping->bottom;
806 clipping_rect.right = clipping->right;
807 clipping_rect.top = clipping->top;
808 }
809 RenderPageImpl(pContext, pPage, transform_matrix, clipping_rect.ToFxRect(),
810 flags, true, nullptr);
811
812 pPage->SetRenderContext(nullptr);
813}
814
Cary Clark399be5b2016-03-14 16:51:29 -0400815#ifdef _SKIA_SUPPORT_
816DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page,
817 int size_x,
818 int size_y) {
819 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
820 if (!pPage)
821 return nullptr;
tsepez1e2c5572016-05-25 14:58:09 -0700822
weili9f515bc2016-07-24 08:08:24 -0700823 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700824 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
caryclarkd6e18872016-05-13 10:57:20 -0700825 CFX_FxgeDevice* skDevice = new CFX_FxgeDevice;
Cary Clark399be5b2016-03-14 16:51:29 -0400826 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
weili9f515bc2016-07-24 08:08:24 -0700827 pContext->m_pDevice.reset(skDevice);
tsepez4cf55152016-11-02 14:37:54 -0700828 FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, true,
tsepez1e2c5572016-05-25 14:58:09 -0700829 nullptr);
tsepeze5cb0b12016-10-26 15:06:11 -0700830 pPage->SetRenderContext(nullptr);
Cary Clark399be5b2016-03-14 16:51:29 -0400831 return recorder;
832}
833#endif
834
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
tsepez1e2c5572016-05-25 14:58:09 -0700836 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700837 if (!page)
838 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800839#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -0800840 pPage->Release();
841#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800842 CPDFSDK_PageView* pPageView =
tsepez1e2c5572016-05-25 14:58:09 -0700843 static_cast<CPDFSDK_PageView*>(pPage->GetView());
dsinclair1df1efa2016-09-07 09:55:37 -0700844 if (pPageView) {
dsinclairbcf46232016-10-03 13:02:27 -0700845 // We're already destroying the pageview, so bail early.
846 if (pPageView->IsBeingDestroyed())
847 return;
848
dsinclair1df1efa2016-09-07 09:55:37 -0700849 if (pPageView->IsLocked()) {
850 pPageView->TakePageOwnership();
851 return;
852 }
853
854 bool owned = pPageView->OwnsPage();
855 // This will delete the |pPageView| object. We must cleanup the PageView
856 // first because it will attempt to reset the View on the |pPage| during
857 // destruction.
dsinclair7cbe68e2016-10-12 11:56:23 -0700858 pPageView->GetFormFillEnv()->RemovePageView(pPage);
dsinclair1df1efa2016-09-07 09:55:37 -0700859 // If the page was owned then the pageview will have deleted the page.
860 if (owned)
861 return;
Tom Sepez51da0932015-11-25 16:05:49 -0800862 }
tsepez1e2c5572016-05-25 14:58:09 -0700863 delete pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800864#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700865}
866
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700867DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
Jun Fangfc751362015-12-16 21:23:39 -0800868 delete UnderlyingFromFPDFDocument(document);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700869}
870
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700871DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
872 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700873}
874
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
876 int start_x,
877 int start_y,
878 int size_x,
879 int size_y,
880 int rotate,
881 int device_x,
882 int device_y,
883 double* page_x,
884 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -0800885 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700886 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800887 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800888#ifdef PDF_ENABLE_XFA
889 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
890 device_y, page_x, page_y);
891#else // PDF_ENABLE_XFA
Dan Sinclair1b08df12017-02-09 09:17:20 -0500892 CFX_Matrix page2device =
893 pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
Tom Sepez60d909e2015-12-10 15:34:55 -0800894 CFX_Matrix device2page;
Tom Sepez51da0932015-11-25 16:05:49 -0800895 device2page.SetReverse(page2device);
Dan Sinclair1b08df12017-02-09 09:17:20 -0500896
Dan Sinclair05df0752017-03-14 14:43:42 -0400897 CFX_PointF pos = device2page.Transform(
898 CFX_PointF(static_cast<float>(device_x), static_cast<float>(device_y)));
Dan Sinclairafb44562017-02-09 13:07:43 -0500899
Dan Sinclaira0061af2017-02-23 09:25:17 -0500900 *page_x = pos.x;
901 *page_y = pos.y;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800902#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700903}
904
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
906 int start_x,
907 int start_y,
908 int size_x,
909 int size_y,
910 int rotate,
911 double page_x,
912 double page_y,
913 int* device_x,
914 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700915 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800917 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -0700918 if (!pPage)
919 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800920#ifdef PDF_ENABLE_XFA
921 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
922 device_x, device_y);
923#else // PDF_ENABLE_XFA
Dan Sinclair1b08df12017-02-09 09:17:20 -0500924 CFX_Matrix page2device =
925 pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
Dan Sinclaira0061af2017-02-23 09:25:17 -0500926 CFX_PointF pos = page2device.Transform(
Dan Sinclair05df0752017-03-14 14:43:42 -0400927 CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)));
Dan Sinclairafb44562017-02-09 13:07:43 -0500928
Dan Sinclaira0061af2017-02-23 09:25:17 -0500929 *device_x = FXSYS_round(pos.x);
930 *device_y = FXSYS_round(pos.y);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800931#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700932}
933
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
935 int height,
936 int alpha) {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700937 auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
tsepez37b12ad2016-12-14 19:50:23 -0800938 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32))
thestig1cd352e2016-06-07 17:53:06 -0700939 return nullptr;
tsepez37b12ad2016-12-14 19:50:23 -0800940
Tom Sepezf0799fe2017-03-28 09:31:32 -0700941 return pBitmap.Leak();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700942}
943
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
945 int height,
946 int format,
947 void* first_scan,
948 int stride) {
949 FXDIB_Format fx_format;
950 switch (format) {
951 case FPDFBitmap_Gray:
952 fx_format = FXDIB_8bppRgb;
953 break;
954 case FPDFBitmap_BGR:
955 fx_format = FXDIB_Rgb;
956 break;
957 case FPDFBitmap_BGRx:
958 fx_format = FXDIB_Rgb32;
959 break;
960 case FPDFBitmap_BGRA:
961 fx_format = FXDIB_Argb;
962 break;
963 default:
thestig1cd352e2016-06-07 17:53:06 -0700964 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965 }
Tom Sepezf0799fe2017-03-28 09:31:32 -0700966 auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700968 return pBitmap.Leak();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700969}
970
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700971DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
972 int left,
973 int top,
974 int width,
975 int height,
976 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -0800977 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978 return;
thestigbefa4502016-05-26 20:15:19 -0700979
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 CFX_FxgeDevice device;
Tom Sepezf0799fe2017-03-28 09:31:32 -0700981 CFX_RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
thestigbefa4502016-05-26 20:15:19 -0700982 device.Attach(pBitmap, false, nullptr, false);
983 if (!pBitmap->HasAlpha())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 color |= 0xFF000000;
985 FX_RECT rect(left, top, left + width, top + height);
986 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700987}
988
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700989DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700990 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -0700991}
992
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700994 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -0700995}
996
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -0700998 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999}
1000
1001DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001002 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003}
1004
1005DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
Tom Sepezf0799fe2017-03-28 09:31:32 -07001006 CFX_RetainPtr<CFX_DIBitmap> destroyer;
1007 destroyer.Unleak(CFXBitmapFromFPDFBitmap(bitmap));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008}
1009
weili9f515bc2016-07-24 08:08:24 -07001010void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011 FPDF_PAGE page,
1012 int start_x,
1013 int start_y,
1014 int size_x,
1015 int size_y,
1016 int rotate,
1017 int flags,
tsepez4cf55152016-11-02 14:37:54 -07001018 bool bNeedToRestore,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -07001020 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
1021 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 return;
1023
Dan Sinclair1b08df12017-02-09 09:17:20 -05001024 RenderPageImpl(pContext, pPage, pPage->GetDisplayMatrix(
1025 start_x, start_y, size_x, size_y, rotate),
1026 FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y),
1027 flags, bNeedToRestore, pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028}
1029
1030DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
1031 int page_index,
1032 double* width,
1033 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -08001034 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
1035 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001036 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037
Tom Sepez40e9ff32015-11-30 12:39:54 -08001038#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039 int count = pDoc->GetPageCount();
1040 if (page_index < 0 || page_index >= count)
tsepez4cf55152016-11-02 14:37:54 -07001041 return false;
dsinclair9f206f02016-09-20 12:17:42 -07001042 CPDFXFA_Page* pPage = pDoc->GetXFAPage(page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043 if (!pPage)
tsepez4cf55152016-11-02 14:37:54 -07001044 return false;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001045 *width = pPage->GetPageWidth();
1046 *height = pPage->GetPageHeight();
1047#else // PDF_ENABLE_XFA
1048 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
1049 if (!pDict)
tsepez4cf55152016-11-02 14:37:54 -07001050 return false;
thestig5cc24652016-04-26 11:46:02 -07001051
1052 CPDF_Page page(pDoc, pDict, true);
Tom Sepez51da0932015-11-25 16:05:49 -08001053 *width = page.GetPageWidth();
1054 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001055#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056
tsepez4cf55152016-11-02 14:37:54 -07001057 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058}
1059
1060DLLEXPORT FPDF_BOOL STDCALL
1061FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001062 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001064 return true;
Tom Sepez471a1032015-10-15 16:17:18 -07001065 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 return viewRef.PrintScaling();
1067}
1068
1069DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001070 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071 if (!pDoc)
1072 return 1;
1073 CPDF_ViewerPreferences viewRef(pDoc);
1074 return viewRef.NumCopies();
1075}
1076
1077DLLEXPORT FPDF_PAGERANGE STDCALL
1078FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001079 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -07001081 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082 CPDF_ViewerPreferences viewRef(pDoc);
1083 return viewRef.PrintPageRange();
1084}
1085
1086DLLEXPORT FPDF_DUPLEXTYPE STDCALL
1087FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001088 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001089 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -07001090 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001091 CPDF_ViewerPreferences viewRef(pDoc);
1092 CFX_ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001093 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001095 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001097 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001098 return DuplexFlipLongEdge;
1099 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001100}
1101
thestig04bebfe2016-11-04 16:07:25 -07001102DLLEXPORT unsigned long STDCALL FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document,
1103 FPDF_BYTESTRING key,
1104 char* buffer,
1105 unsigned long length) {
1106 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1107 if (!pDoc)
1108 return 0;
1109
1110 CPDF_ViewerPreferences viewRef(pDoc);
1111 CFX_ByteString bsVal;
1112 if (!viewRef.GenericName(key, &bsVal))
1113 return 0;
1114
1115 unsigned long dwStringLen = bsVal.GetLength() + 1;
1116 if (buffer && length >= dwStringLen)
1117 memcpy(buffer, bsVal.c_str(), dwStringLen);
1118 return dwStringLen;
1119}
1120
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001121DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001122 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1123 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001124 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001125
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1127 if (!pRoot)
1128 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001129
Lei Zhangd983b092015-12-14 16:58:33 -08001130 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001131 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
dsinclair38fd8442016-09-15 10:15:32 -07001132 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133 if (pDest)
1134 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001135
1136 if (!count.IsValid())
1137 return 0;
1138
1139 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001140}
1141
1142DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
1143 FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001144 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001145 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146
Tom Sepez471a1032015-10-15 16:17:18 -07001147 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1148 if (!pDoc)
1149 return nullptr;
1150
Lei Zhangd983b092015-12-14 16:58:33 -08001151 CPDF_NameTree name_tree(pDoc, "Dests");
Tom Sepez471a1032015-10-15 16:17:18 -07001152 return name_tree.LookupNamedDest(pDoc, name);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001153}
1154
Tom Sepez51da0932015-11-25 16:05:49 -08001155#ifdef PDF_ENABLE_XFA
thestig77d148d2016-04-06 06:28:31 -07001156DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Init(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 if (!str)
1158 return -1;
1159
Dan Sinclair1c5d0b42017-04-03 15:05:11 -04001160 memset(str, 0, sizeof(FPDF_BSTR));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001161 return 0;
1162}
1163
thestig77d148d2016-04-06 06:28:31 -07001164DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Set(FPDF_BSTR* str,
1165 FPDF_LPCSTR bstr,
1166 int length) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001167 if (!str)
1168 return -1;
1169 if (!bstr || !length)
1170 return -1;
1171 if (length == -1)
1172 length = FXSYS_strlen(bstr);
1173
1174 if (length == 0) {
1175 if (str->str) {
1176 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001177 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178 }
1179 str->len = 0;
1180 return 0;
1181 }
1182
1183 if (str->str && str->len < length)
1184 str->str = FX_Realloc(char, str->str, length + 1);
1185 else if (!str->str)
1186 str->str = FX_Alloc(char, length + 1);
1187
1188 str->str[length] = 0;
Dan Sinclair1c5d0b42017-04-03 15:05:11 -04001189 memcpy(str->str, bstr, length);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190 str->len = length;
1191
1192 return 0;
1193}
1194
thestig77d148d2016-04-06 06:28:31 -07001195DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Clear(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001196 if (!str)
1197 return -1;
1198
1199 if (str->str) {
1200 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001201 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202 }
1203 str->len = 0;
1204 return 0;
1205}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001206#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001207
1208DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
1209 int index,
1210 void* buffer,
1211 long* buflen) {
1212 if (!buffer)
1213 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001214
1215 if (index < 0)
1216 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001217
Tom Sepezbf59a072015-10-21 14:07:23 -07001218 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001219 if (!pDoc)
1220 return nullptr;
1221
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001222 CPDF_Dictionary* pRoot = pDoc->GetRoot();
1223 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001224 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001226 CPDF_Object* pDestObj = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001227 CFX_ByteString bsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001228 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 int count = nameTree.GetCount();
1230 if (index >= count) {
dsinclair38fd8442016-09-15 10:15:32 -07001231 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001232 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001233 return nullptr;
1234
1235 pdfium::base::CheckedNumeric<int> checked_count = count;
1236 checked_count += pDest->GetCount();
1237 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1238 return nullptr;
1239
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001240 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001241 int i = 0;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001242 for (const auto& it : *pDest) {
1243 bsName = it.first;
tsepez0e606b52016-11-18 16:22:41 -08001244 pDestObj = it.second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001245 if (!pDestObj)
1246 continue;
1247 if (i == index)
1248 break;
1249 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001250 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001251 } else {
Tom Sepezc4a2b752017-04-07 13:56:13 -07001252 pDestObj = nameTree.LookupValueAndName(index, &bsName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253 }
1254 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001255 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001256 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
dsinclair38fd8442016-09-15 10:15:32 -07001257 pDestObj = pDict->GetArrayFor("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001259 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001261 if (!pDestObj->IsArray())
1262 return nullptr;
1263
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264 CFX_WideString wsName = PDF_DecodeText(bsName);
1265 CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -07001266 int len = utf16Name.GetLength();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001267 if (!buffer) {
1268 *buflen = len;
thestig9067fd62016-11-23 14:10:06 -08001269 } else if (len <= *buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270 memcpy(buffer, utf16Name.c_str(), len);
1271 *buflen = len;
1272 } else {
1273 *buflen = -1;
1274 }
1275 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001276}