blob: 98a5b32d96e95e9a3091c65821acaa19f73e8c38 [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>
rbpottercec01802017-04-17 09:10:21 -070011#include <vector>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080012
dsinclair39c62fd2016-09-29 12:49:17 -070013#include "core/fpdfapi/cpdf_modulemgr.h"
14#include "core/fpdfapi/cpdf_pagerendercontext.h"
rbpottercec01802017-04-17 09:10:21 -070015#include "core/fpdfapi/page/cpdf_image.h"
16#include "core/fpdfapi/page/cpdf_imageobject.h"
dsinclair41872fa2016-10-04 11:29:35 -070017#include "core/fpdfapi/page/cpdf_page.h"
rbpottercec01802017-04-17 09:10:21 -070018#include "core/fpdfapi/page/cpdf_pageobject.h"
Jane Liu3b057432017-06-19 10:44:01 -040019#include "core/fpdfapi/page/cpdf_pathobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070020#include "core/fpdfapi/parser/cpdf_array.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040021#include "core/fpdfapi/parser/cpdf_dictionary.h"
dsinclair488b7ad2016-10-04 11:55:50 -070022#include "core/fpdfapi/parser/cpdf_document.h"
23#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair69d9c682016-10-04 12:18:35 -070024#include "core/fpdfapi/render/cpdf_progressiverenderer.h"
25#include "core/fpdfapi/render/cpdf_renderoptions.h"
dsinclair1727aee2016-09-29 13:12:56 -070026#include "core/fpdfdoc/cpdf_annotlist.h"
27#include "core/fpdfdoc/cpdf_nametree.h"
28#include "core/fpdfdoc/cpdf_occontext.h"
29#include "core/fpdfdoc/cpdf_viewerpreferences.h"
dsinclaira52ab742016-09-29 13:59:29 -070030#include "core/fxcrt/fx_memory.h"
31#include "core/fxcrt/fx_safe_types.h"
Dan Sinclairbcd1e702017-08-31 13:19:18 -040032#include "core/fxcrt/fx_stream.h"
Dan Sinclaira5085d42017-05-11 16:26:50 -040033#include "core/fxge/cfx_defaultrenderdevice.h"
dsinclair74a34fc2016-09-29 16:41:42 -070034#include "core/fxge/cfx_gemodule.h"
dsinclairb402b172016-10-11 09:26:32 -070035#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070036#include "fpdfsdk/cpdfsdk_pageview.h"
37#include "fpdfsdk/fsdk_define.h"
38#include "fpdfsdk/fsdk_pauseadapter.h"
dsinclair64376be2016-03-31 20:03:24 -070039#include "fpdfsdk/javascript/ijs_runtime.h"
rbpottere8468c42017-07-11 10:04:29 -070040#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080041#include "public/fpdf_ext.h"
Ryan Harrisona74a6272017-10-16 10:57:48 -040042#include "public/fpdf_formfill.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080043#include "public/fpdf_progressive.h"
Chris Palmere4b035b2017-03-26 15:48:34 -070044#include "third_party/base/allocator/partition_allocator/partition_alloc.h"
Lei Zhang8241df72015-11-06 14:38:48 -080045#include "third_party/base/numerics/safe_conversions_impl.h"
tsepez36eb4bd2016-10-03 15:24:27 -070046#include "third_party/base/ptr_util.h"
Bo Xufdc00a72014-10-28 23:03:33 -070047
Tom Sepez40e9ff32015-11-30 12:39:54 -080048#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070049#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070050#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
51#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
Dan Sinclaire7786682017-03-29 15:18:41 -040052#include "fxbarcode/BC_Library.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080053#endif // PDF_ENABLE_XFA
54
Dan Sinclair698aed72017-09-26 16:24:49 -040055#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclaira5085d42017-05-11 16:26:50 -040056#include "core/fxge/cfx_windowsrenderdevice.h"
rbpottere8468c42017-07-11 10:04:29 -070057
58// These checks are here because core/ and public/ cannot depend on each other.
59static_assert(WindowsPrintMode::kModeEmf == FPDF_PRINTMODE_EMF,
60 "WindowsPrintMode::kModeEmf value mismatch");
61static_assert(WindowsPrintMode::kModeTextOnly == FPDF_PRINTMODE_TEXTONLY,
62 "WindowsPrintMode::kModeTextOnly value mismatch");
63static_assert(WindowsPrintMode::kModePostScript2 == FPDF_PRINTMODE_POSTSCRIPT2,
64 "WindowsPrintMode::kModePostScript2 value mismatch");
65static_assert(WindowsPrintMode::kModePostScript3 == FPDF_PRINTMODE_POSTSCRIPT3,
66 "WindowsPrintMode::kModePostScript3 value mismatch");
thestig25fa42f2016-05-25 21:39:46 -070067#endif
68
thestiga78ba602016-11-23 15:25:48 -080069namespace {
70
Dan Sinclair830897a2017-05-11 14:36:10 -040071bool g_bLibraryInitialized = false;
tsepez02759102016-12-01 08:29:25 -080072
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
Dan Sinclair921fe6b2017-10-05 11:14:12 -040083 uint32_t option_flags = pContext->m_pOptions->GetFlags();
thestiga78ba602016-11-23 15:25:48 -080084 if (flags & FPDF_LCD_TEXT)
Dan Sinclair921fe6b2017-10-05 11:14:12 -040085 option_flags |= RENDER_CLEARTYPE;
thestiga78ba602016-11-23 15:25:48 -080086 else
Dan Sinclair921fe6b2017-10-05 11:14:12 -040087 option_flags &= ~RENDER_CLEARTYPE;
thestiga78ba602016-11-23 15:25:48 -080088
89 if (flags & FPDF_NO_NATIVETEXT)
Dan Sinclair921fe6b2017-10-05 11:14:12 -040090 option_flags |= RENDER_NO_NATIVETEXT;
thestiga78ba602016-11-23 15:25:48 -080091 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
Dan Sinclair921fe6b2017-10-05 11:14:12 -040092 option_flags |= RENDER_LIMITEDIMAGECACHE;
thestiga78ba602016-11-23 15:25:48 -080093 if (flags & FPDF_RENDER_FORCEHALFTONE)
Dan Sinclair921fe6b2017-10-05 11:14:12 -040094 option_flags |= RENDER_FORCE_HALFTONE;
thestiga78ba602016-11-23 15:25:48 -080095#ifndef PDF_ENABLE_XFA
96 if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
Dan Sinclair921fe6b2017-10-05 11:14:12 -040097 option_flags |= RENDER_NOTEXTSMOOTH;
thestiga78ba602016-11-23 15:25:48 -080098 if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
Dan Sinclair921fe6b2017-10-05 11:14:12 -040099 option_flags |= RENDER_NOIMAGESMOOTH;
thestiga78ba602016-11-23 15:25:48 -0800100 if (flags & FPDF_RENDER_NO_SMOOTHPATH)
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400101 option_flags |= RENDER_NOPATHSMOOTH;
thestiga78ba602016-11-23 15:25:48 -0800102#endif // PDF_ENABLE_XFA
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400103 pContext->m_pOptions->SetFlags(option_flags);
thestiga78ba602016-11-23 15:25:48 -0800104
105 // Grayscale output
Dan Sinclairf55e72e2017-07-13 14:53:28 -0400106 if (flags & FPDF_GRAYSCALE)
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400107 pContext->m_pOptions->SetColorMode(CPDF_RenderOptions::kGray);
thestiga78ba602016-11-23 15:25:48 -0800108
109 const CPDF_OCContext::UsageType usage =
110 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400111 pContext->m_pOptions->SetOCContext(
112 pdfium::MakeRetain<CPDF_OCContext>(pPage->m_pDocument.Get(), usage));
thestiga78ba602016-11-23 15:25:48 -0800113
114 pContext->m_pDevice->SaveState();
115 pContext->m_pDevice->SetClip_Rect(clipping_rect);
thestiga78ba602016-11-23 15:25:48 -0800116 pContext->m_pContext = pdfium::MakeUnique<CPDF_RenderContext>(pPage);
117 pContext->m_pContext->AppendLayer(pPage, &matrix);
118
119 if (flags & FPDF_ANNOT) {
120 pContext->m_pAnnots = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
121 bool bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
122 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext.get(),
123 bPrinting, &matrix, false, nullptr);
124 }
125
126 pContext->m_pRenderer = pdfium::MakeUnique<CPDF_ProgressiveRenderer>(
127 pContext->m_pContext.get(), pContext->m_pDevice.get(),
128 pContext->m_pOptions.get());
129 pContext->m_pRenderer->Start(pause);
130 if (bNeedToRestore)
131 pContext->m_pDevice->RestoreState(false);
132}
133
tsepezbea04972016-12-01 13:54:42 -0800134class CPDF_CustomAccess final : public IFX_SeekableReadStream {
135 public:
Tom Sepez40badde2017-05-01 13:21:39 -0700136 template <typename T, typename... Args>
Dan Sinclair0b950422017-09-21 15:49:49 -0400137 friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
tsepezbea04972016-12-01 13:54:42 -0800138
139 // IFX_SeekableReadStream
140 FX_FILESIZE GetSize() override;
tsepezbea04972016-12-01 13:54:42 -0800141 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
142
143 private:
tsepez833619b2016-12-07 09:21:17 -0800144 explicit CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess);
145
tsepezbea04972016-12-01 13:54:42 -0800146 FPDF_FILEACCESS m_FileAccess;
147};
148
149CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
150 : m_FileAccess(*pFileAccess) {}
151
152FX_FILESIZE CPDF_CustomAccess::GetSize() {
153 return m_FileAccess.m_FileLen;
154}
155
tsepezbea04972016-12-01 13:54:42 -0800156bool CPDF_CustomAccess::ReadBlock(void* buffer,
157 FX_FILESIZE offset,
158 size_t size) {
159 if (offset < 0)
160 return false;
161
162 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
163 newPos += offset;
164 if (!newPos.IsValid() ||
165 newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) {
166 return false;
167 }
168 return !!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,
Nicolas Pena46abb662017-05-17 17:23:22 -0400169 static_cast<uint8_t*>(buffer), size);
tsepezbea04972016-12-01 13:54:42 -0800170}
171
Tom Sepez40e9ff32015-11-30 12:39:54 -0800172#ifdef PDF_ENABLE_XFA
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400173class FPDF_FileHandlerContext : public IFX_SeekableStream {
tsepezfa89a202016-12-02 09:48:30 -0800174 public:
Tom Sepez40badde2017-05-01 13:21:39 -0700175 template <typename T, typename... Args>
Dan Sinclair0b950422017-09-21 15:49:49 -0400176 friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
Tom Sepez40badde2017-05-01 13:21:39 -0700177
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400178 ~FPDF_FileHandlerContext() override;
Tom Sepez471a1032015-10-15 16:17:18 -0700179
tsepezfa89a202016-12-02 09:48:30 -0800180 // IFX_SeekableStream:
tsepezfa89a202016-12-02 09:48:30 -0800181 FX_FILESIZE GetSize() override;
182 bool IsEOF() override;
183 FX_FILESIZE GetPosition() override;
184 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
185 size_t ReadBlock(void* buffer, size_t size) override;
186 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override;
187 bool Flush() override;
Tom Sepezbf59a072015-10-21 14:07:23 -0700188
tsepezfa89a202016-12-02 09:48:30 -0800189 void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; }
Tom Sepezdb0be962015-10-16 14:00:21 -0700190
tsepezfa89a202016-12-02 09:48:30 -0800191 protected:
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400192 explicit FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS);
tsepez833619b2016-12-07 09:21:17 -0800193
tsepezfa89a202016-12-02 09:48:30 -0800194 FPDF_FILEHANDLER* m_pFS;
195 FX_FILESIZE m_nCurPos;
196};
thestigbefa4502016-05-26 20:15:19 -0700197
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400198FPDF_FileHandlerContext::FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 m_pFS = pFS;
200 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700201}
202
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400203FPDF_FileHandlerContext::~FPDF_FileHandlerContext() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 if (m_pFS && m_pFS->Release)
205 m_pFS->Release(m_pFS->clientData);
Bo Xufdc00a72014-10-28 23:03:33 -0700206}
207
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400208FX_FILESIZE FPDF_FileHandlerContext::GetSize() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 if (m_pFS && m_pFS->GetSize)
210 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
211 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700212}
213
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400214bool FPDF_FileHandlerContext::IsEOF() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -0700216}
217
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400218FX_FILESIZE FPDF_FileHandlerContext::GetPosition() {
weili625ad662016-06-15 11:21:33 -0700219 return m_nCurPos;
220}
221
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400222bool FPDF_FileHandlerContext::ReadBlock(void* buffer,
223 FX_FILESIZE offset,
224 size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 if (!buffer || !size || !m_pFS->ReadBlock)
tsepezf39074c2016-10-26 15:33:58 -0700226 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
229 (FPDF_DWORD)size) == 0) {
230 m_nCurPos = offset + size;
tsepezf39074c2016-10-26 15:33:58 -0700231 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 }
tsepezf39074c2016-10-26 15:33:58 -0700233 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700234}
235
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400236size_t FPDF_FileHandlerContext::ReadBlock(void* buffer, size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 if (!buffer || !size || !m_pFS->ReadBlock)
238 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 FX_FILESIZE nSize = GetSize();
241 if (m_nCurPos >= nSize)
242 return 0;
243 FX_FILESIZE dwAvail = nSize - m_nCurPos;
244 if (dwAvail < (FX_FILESIZE)size)
Ryan Harrison875e98c2017-09-27 10:53:11 -0400245 size = static_cast<size_t>(dwAvail);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
247 (FPDF_DWORD)size) == 0) {
248 m_nCurPos += size;
249 return size;
250 }
Bo Xufdc00a72014-10-28 23:03:33 -0700251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700253}
254
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400255bool FPDF_FileHandlerContext::WriteBlock(const void* buffer,
256 FX_FILESIZE offset,
257 size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 if (!m_pFS || !m_pFS->WriteBlock)
tsepezf39074c2016-10-26 15:33:58 -0700259 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
262 (FPDF_DWORD)size) == 0) {
263 m_nCurPos = offset + size;
tsepezf39074c2016-10-26 15:33:58 -0700264 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 }
tsepezf39074c2016-10-26 15:33:58 -0700266 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700267}
268
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400269bool FPDF_FileHandlerContext::Flush() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 if (!m_pFS || !m_pFS->Flush)
tsepezf39074c2016-10-26 15:33:58 -0700271 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700274}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800275#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276
Artem Strygin3f7ff052017-07-27 22:16:13 +0300277FPDF_DOCUMENT LoadDocumentImpl(
Dan Sinclair0b950422017-09-21 15:49:49 -0400278 const RetainPtr<IFX_SeekableReadStream>& pFileAccess,
Artem Strygin3f7ff052017-07-27 22:16:13 +0300279 FPDF_BYTESTRING password) {
Jane Liu8a1081f2017-08-22 16:28:05 -0400280 if (!pFileAccess) {
281 ProcessParseError(CPDF_Parser::FILE_ERROR);
Artem Strygin3f7ff052017-07-27 22:16:13 +0300282 return nullptr;
Jane Liu8a1081f2017-08-22 16:28:05 -0400283 }
Artem Strygin3f7ff052017-07-27 22:16:13 +0300284
285 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
286 pParser->SetPassword(password);
287
288 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
289 CPDF_Parser::Error error =
290 pDocument->GetParser()->StartParse(pFileAccess, pDocument.get());
291 if (error != CPDF_Parser::SUCCESS) {
292 ProcessParseError(error);
293 return nullptr;
294 }
295 CheckUnSupportError(pDocument.get(), error);
296 return FPDFDocumentFromCPDFDocument(pDocument.release());
297}
298
tsepezfa89a202016-12-02 09:48:30 -0800299} // namespace
300
301UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
302 return static_cast<UnderlyingDocumentType*>(doc);
303}
304
305FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
306 return static_cast<FPDF_DOCUMENT>(doc);
307}
308
309UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
310 return static_cast<UnderlyingPageType*>(page);
311}
312
313CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
314#ifdef PDF_ENABLE_XFA
315 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
316#else // PDF_ENABLE_XFA
317 return UnderlyingFromFPDFDocument(doc);
318#endif // PDF_ENABLE_XFA
319}
320
321FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
322#ifdef PDF_ENABLE_XFA
323 return doc ? FPDFDocumentFromUnderlying(
324 new CPDFXFA_Context(pdfium::WrapUnique(doc)))
325 : nullptr;
326#else // PDF_ENABLE_XFA
327 return FPDFDocumentFromUnderlying(doc);
328#endif // PDF_ENABLE_XFA
329}
330
331CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
332#ifdef PDF_ENABLE_XFA
333 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
334#else // PDF_ENABLE_XFA
335 return UnderlyingFromFPDFPage(page);
336#endif // PDF_ENABLE_XFA
337}
338
Jane Liu3b057432017-06-19 10:44:01 -0400339CPDF_PathObject* CPDFPathObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) {
Lei Zhang038740c2017-09-13 13:22:54 -0700340 auto* obj = CPDFPageObjectFromFPDFPageObject(page_object);
341 return obj ? obj->AsPath() : nullptr;
Jane Liu3b057432017-06-19 10:44:01 -0400342}
343
Jane Liubaa7ff42017-06-29 19:18:23 -0400344CPDF_PageObject* CPDFPageObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) {
345 return static_cast<CPDF_PageObject*>(page_object);
346}
347
Jane Liu18ae06d2017-07-18 10:15:16 -0400348CPDF_Object* CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment) {
349 return static_cast<CPDF_Object*>(attachment);
350}
351
Ryan Harrison275e2602017-09-18 14:23:18 -0400352ByteString CFXByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string) {
353 return WideString::FromUTF16LE(wide_string,
354 WideString::WStringLength(wide_string))
Jane Liu18ae06d2017-07-18 10:15:16 -0400355 .UTF8Encode();
356}
357
tsepezfa89a202016-12-02 09:48:30 -0800358CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
359 return static_cast<CFX_DIBitmap*>(bitmap);
360}
361
Miklos Vajna36eed872017-09-20 22:52:43 +0200362const FX_PATHPOINT* FXPathPointFromFPDFPathSegment(FPDF_PATHSEGMENT segment) {
363 return static_cast<const FX_PATHPOINT*>(segment);
364}
365
Ryan Harrison275e2602017-09-18 14:23:18 -0400366unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text,
Jane Liu18ae06d2017-07-18 10:15:16 -0400367 void* buffer,
368 unsigned long buflen) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400369 ByteString encoded_text = text.UTF16LE_Encode();
Jane Liu548334e2017-08-03 16:33:40 -0400370 unsigned long len = encoded_text.GetLength();
Jane Liu18ae06d2017-07-18 10:15:16 -0400371 if (buffer && len <= buflen)
Jane Liu548334e2017-08-03 16:33:40 -0400372 memcpy(buffer, encoded_text.c_str(), len);
373 return len;
374}
375
376unsigned long DecodeStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream,
377 void* buffer,
378 unsigned long buflen) {
379 ASSERT(stream);
380 uint8_t* data = stream->GetRawData();
381 uint32_t len = stream->GetRawSize();
382 CPDF_Dictionary* dict = stream->GetDict();
383 CPDF_Object* decoder = dict ? dict->GetDirectObjectFor("Filter") : nullptr;
384 if (decoder && (decoder->IsArray() || decoder->IsName())) {
385 // Decode the stream if one or more stream filters are specified.
386 uint8_t* decoded_data = nullptr;
387 uint32_t decoded_len = 0;
Ryan Harrison275e2602017-09-18 14:23:18 -0400388 ByteString dummy_last_decoder;
Jane Liu548334e2017-08-03 16:33:40 -0400389 CPDF_Dictionary* dummy_last_param;
390 if (PDF_DataDecode(data, len, dict, dict->GetIntegerFor("DL"), false,
391 &decoded_data, &decoded_len, &dummy_last_decoder,
392 &dummy_last_param)) {
393 if (buffer && buflen >= decoded_len)
394 memcpy(buffer, decoded_data, decoded_len);
395
396 // Free the buffer for the decoded data if it was allocated by
397 // PDF_DataDecode(). Note that for images with a single image-specific
398 // filter, |decoded_data| is directly assigned to be |data|, so
399 // |decoded_data| does not need to be freed.
400 if (decoded_data != data)
401 FX_Free(decoded_data);
402
403 return decoded_len;
404 }
405 }
406 // Copy the raw data and return its length if there is no valid filter
407 // specified or if decoding failed.
408 if (buffer && buflen >= len)
409 memcpy(buffer, data, len);
410
Jane Liu18ae06d2017-07-18 10:15:16 -0400411 return len;
412}
413
Dan Sinclair0b950422017-09-21 15:49:49 -0400414RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream(
tsepez833619b2016-12-07 09:21:17 -0800415 FPDF_FILEACCESS* pFileAccess) {
Tom Sepez40badde2017-05-01 13:21:39 -0700416 return pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700417}
418
tsepezfa89a202016-12-02 09:48:30 -0800419#ifdef PDF_ENABLE_XFA
Dan Sinclair0b950422017-09-21 15:49:49 -0400420RetainPtr<IFX_SeekableStream> MakeSeekableStream(
tsepez833619b2016-12-07 09:21:17 -0800421 FPDF_FILEHANDLER* pFilehandler) {
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400422 return pdfium::MakeRetain<FPDF_FileHandlerContext>(pFilehandler);
tsepezfa89a202016-12-02 09:48:30 -0800423}
424#endif // PDF_ENABLE_XFA
425
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
tsepezc3255f52016-03-25 14:52:27 -0700427static uint32_t foxit_sandbox_policy = 0xFFFFFFFF;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
430 switch (policy) {
431 case FPDF_POLICY_MACHINETIME_ACCESS: {
432 if (enable)
433 foxit_sandbox_policy |= 0x01;
434 else
435 foxit_sandbox_policy &= 0xFFFFFFFE;
436 } break;
437 default:
438 break;
439 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700440}
441
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
443 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700444 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700445 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 default:
tsepez4cf55152016-11-02 14:37:54 -0700447 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700449}
450
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400451FPDF_EXPORT void FPDF_CALLCONV FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700452 FPDF_InitLibraryWithConfig(nullptr);
453}
454
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400455FPDF_EXPORT void FPDF_CALLCONV
Dan Sinclairf766ad22016-03-14 13:51:24 -0400456FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
Dan Sinclair830897a2017-05-11 14:36:10 -0400457 if (g_bLibraryInitialized)
tsepez02759102016-12-01 08:29:25 -0800458 return;
459
Dan Sinclairdbc3d3e2017-05-11 13:41:38 -0400460 FXMEM_InitializePartitionAlloc();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700461
weili47228ac2016-07-20 10:35:31 -0700462 CFX_GEModule* pModule = CFX_GEModule::Get();
Dan Sinclair830897a2017-05-11 14:36:10 -0400463 pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr);
tsepez02759102016-12-01 08:29:25 -0800464
Tom Sepez1b246282015-11-25 15:15:31 -0800465 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
Lei Zhang76020fc2017-05-18 15:51:20 -0700466 pModuleMgr->Init();
Tom Sepezb4a26182017-03-01 12:15:00 -0800467
Tom Sepez40e9ff32015-11-30 12:39:54 -0800468#ifdef PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700469 FXJSE_Initialize();
470 BC_Library_Init();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800471#endif // PDF_ENABLE_XFA
Tom Sepez452b4f32015-10-13 09:27:27 -0700472 if (cfg && cfg->version >= 2)
473 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
Dan Sinclair830897a2017-05-11 14:36:10 -0400474
475 g_bLibraryInitialized = true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700476}
477
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400478FPDF_EXPORT void FPDF_CALLCONV FPDF_DestroyLibrary() {
Dan Sinclair830897a2017-05-11 14:36:10 -0400479 if (!g_bLibraryInitialized)
tsepez02759102016-12-01 08:29:25 -0800480 return;
481
Tom Sepez51da0932015-11-25 16:05:49 -0800482#ifdef PDF_ENABLE_XFA
Tom Sepez5628fd72017-04-27 14:58:53 -0700483 BC_Library_Destroy();
dsinclair8837c912016-11-01 11:22:37 -0700484 FXJSE_Finalize();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800485#endif // PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700486
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487 CPDF_ModuleMgr::Destroy();
488 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700489
thestig2d6dda12016-06-28 07:39:09 -0700490 IJS_Runtime::Destroy();
Dan Sinclair830897a2017-05-11 14:36:10 -0400491
492 g_bLibraryInitialized = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700493}
494
495#ifndef _WIN32
496int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497void SetLastError(int err) {
498 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700499}
500
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700501int GetLastError() {
502 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700503}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800504#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700505
Tom Sepezf10ae632016-01-26 14:19:52 -0800506void ProcessParseError(CPDF_Parser::Error err) {
tsepezc3255f52016-03-25 14:52:27 -0700507 uint32_t err_code = FPDF_ERR_SUCCESS;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800509 switch (err) {
510 case CPDF_Parser::SUCCESS:
511 err_code = FPDF_ERR_SUCCESS;
512 break;
513 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514 err_code = FPDF_ERR_FILE;
515 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800516 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517 err_code = FPDF_ERR_FORMAT;
518 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800519 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520 err_code = FPDF_ERR_PASSWORD;
521 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800522 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523 err_code = FPDF_ERR_SECURITY;
524 break;
525 }
526 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700527}
528
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400529FPDF_EXPORT void FPDF_CALLCONV FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
530 FPDF_BOOL enable) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700531 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700532}
533
rbpotterdb764702017-01-12 10:31:43 -0800534#if defined(_WIN32)
535#if defined(PDFIUM_PRINT_TEXT_WITH_GDI)
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400536FPDF_EXPORT void FPDF_CALLCONV
thestigfdb35ff2016-07-18 13:45:44 -0700537FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) {
538 g_pdfium_typeface_accessible_func = func;
539}
540
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400541FPDF_EXPORT void FPDF_CALLCONV FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) {
thestigfdb35ff2016-07-18 13:45:44 -0700542 g_pdfium_print_text_with_gdi = !!use_gdi;
543}
rbpotterdb764702017-01-12 10:31:43 -0800544#endif // PDFIUM_PRINT_TEXT_WITH_GDI
545
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400546FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
547FPDF_SetPrintPostscriptLevel(int postscript_level) {
rbpotter38a01b92017-07-13 11:58:01 -0700548 return postscript_level != 1 && FPDF_SetPrintMode(postscript_level);
549}
550
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400551FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_SetPrintMode(int mode) {
rbpottere8468c42017-07-11 10:04:29 -0700552 if (mode < FPDF_PRINTMODE_EMF || mode > FPDF_PRINTMODE_POSTSCRIPT3)
rbpotterdb764702017-01-12 10:31:43 -0800553 return FALSE;
rbpottere8468c42017-07-11 10:04:29 -0700554 g_pdfium_print_mode = mode;
rbpotterdb764702017-01-12 10:31:43 -0800555 return TRUE;
556}
557#endif // defined(_WIN32)
thestigfdb35ff2016-07-18 13:45:44 -0700558
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400559FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
560FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700561 // NOTE: the creation of the file needs to be by the embedder on the
562 // other side of this API.
Artem Strygin3f7ff052017-07-27 22:16:13 +0300563 return LoadDocumentImpl(
564 IFX_SeekableReadStream::CreateFromFilename((const char*)file_path),
565 password);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700566}
Jun Fange118ce92015-02-17 06:50:08 -0800567
Ryan Harrison854d71c2017-10-18 12:28:14 -0400568FPDF_EXPORT int FPDF_CALLCONV FPDF_GetFormType(FPDF_DOCUMENT document) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 if (!document)
tsepez4cf55152016-11-02 14:37:54 -0700570 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800571
Ryan Harrisona74a6272017-10-16 10:57:48 -0400572 const CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Lei Zhang01581062017-08-30 14:19:26 -0700573 if (!pDoc)
Ryan Harrison854d71c2017-10-18 12:28:14 -0400574 return FORMTYPE_NONE;
JUN FANG827a1722015-03-05 13:39:21 -0800575
Lei Zhang01581062017-08-30 14:19:26 -0700576 const CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700577 if (!pRoot)
Ryan Harrison854d71c2017-10-18 12:28:14 -0400578 return FORMTYPE_NONE;
JUN FANG827a1722015-03-05 13:39:21 -0800579
dsinclair38fd8442016-09-15 10:15:32 -0700580 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581 if (!pAcroForm)
Ryan Harrison854d71c2017-10-18 12:28:14 -0400582 return FORMTYPE_NONE;
JUN FANG827a1722015-03-05 13:39:21 -0800583
dsinclair38fd8442016-09-15 10:15:32 -0700584 CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585 if (!pXFA)
Ryan Harrison854d71c2017-10-18 12:28:14 -0400586 return FORMTYPE_ACRO_FORM;
JUN FANG827a1722015-03-05 13:39:21 -0800587
Ryan Harrison854d71c2017-10-18 12:28:14 -0400588 bool needsRendering = pRoot->GetBooleanFor("NeedsRendering", false);
589 return needsRendering ? FORMTYPE_XFA_FULL : FORMTYPE_XFA_FOREGROUND;
Jun Fange118ce92015-02-17 06:50:08 -0800590}
591
Ryan Harrisona74a6272017-10-16 10:57:48 -0400592#ifdef PDF_ENABLE_XFA
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400593FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_LoadXFA(FPDF_DOCUMENT document) {
Lei Zhang01581062017-08-30 14:19:26 -0700594 return document && static_cast<CPDFXFA_Context*>(document)->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700595}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800596#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700597
tsepezad2441e2016-10-24 10:19:11 -0700598class CMemFile final : public IFX_SeekableReadStream {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 public:
Dan Sinclair0b950422017-09-21 15:49:49 -0400600 static RetainPtr<CMemFile> Create(uint8_t* pBuf, FX_FILESIZE size) {
601 return RetainPtr<CMemFile>(new CMemFile(pBuf, size));
tsepez833619b2016-12-07 09:21:17 -0800602 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700603
Lei Zhang3884dba2015-10-19 17:27:53 -0700604 FX_FILESIZE GetSize() override { return m_size; }
tsepezf39074c2016-10-26 15:33:58 -0700605 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
tsepez833619b2016-12-07 09:21:17 -0800606 if (offset < 0)
tsepezf39074c2016-10-26 15:33:58 -0700607 return false;
tsepez833619b2016-12-07 09:21:17 -0800608
tsepez4e597c82016-11-07 15:16:01 -0800609 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700610 newPos += offset;
tsepez833619b2016-12-07 09:21:17 -0800611 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size)
tsepezf39074c2016-10-26 15:33:58 -0700612 return false;
tsepez833619b2016-12-07 09:21:17 -0800613
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400614 memcpy(buffer, m_pBuf + offset, size);
tsepezf39074c2016-10-26 15:33:58 -0700615 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700617
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 private:
tsepez833619b2016-12-07 09:21:17 -0800619 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700620
Lei Zhang3884dba2015-10-19 17:27:53 -0700621 uint8_t* const m_pBuf;
622 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700623};
Lei Zhang3884dba2015-10-19 17:27:53 -0700624
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400625FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
626FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password) {
Artem Strygin3f7ff052017-07-27 22:16:13 +0300627 return LoadDocumentImpl(CMemFile::Create((uint8_t*)data_buf, size), password);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700628}
629
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400630FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700631FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
632 FPDF_BYTESTRING password) {
Artem Strygin3f7ff052017-07-27 22:16:13 +0300633 return LoadDocumentImpl(pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess),
634 password);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700635}
636
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400637FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_GetFileVersion(FPDF_DOCUMENT doc,
638 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700639 if (!fileVersion)
tsepez4cf55152016-11-02 14:37:54 -0700640 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700641
Tom Sepez471a1032015-10-15 16:17:18 -0700642 *fileVersion = 0;
643 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
644 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700645 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700646
Lei Zhang01581062017-08-30 14:19:26 -0700647 const CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648 if (!pParser)
tsepez4cf55152016-11-02 14:37:54 -0700649 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700650
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651 *fileVersion = pParser->GetFileVersion();
tsepez4cf55152016-11-02 14:37:54 -0700652 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700653}
654
tsepezc3255f52016-03-25 14:52:27 -0700655// jabdelmalek: changed return type from uint32_t to build on Linux (and match
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656// header).
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400657FPDF_EXPORT unsigned long FPDF_CALLCONV
658FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700659 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestig27ddf162016-05-23 15:06:59 -0700660 // https://bugs.chromium.org/p/pdfium/issues/detail?id=499
661 if (!pDoc) {
Tom Sepez51da0932015-11-25 16:05:49 -0800662#ifndef PDF_ENABLE_XFA
663 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800664#else // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700665 return 0xFFFFFFFF;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800666#endif // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700667 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700668
thestig27ddf162016-05-23 15:06:59 -0700669 return pDoc->GetUserPermissions();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700670}
671
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400672FPDF_EXPORT int FPDF_CALLCONV
673FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700674 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700675 if (!pDoc || !pDoc->GetParser())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700676 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700677
Tom Sepez471a1032015-10-15 16:17:18 -0700678 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
dsinclair38fd8442016-09-15 10:15:32 -0700679 return pDict ? pDict->GetIntegerFor("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700680}
681
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400682FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800683 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700684 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700685}
686
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400687FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDF_LoadPage(FPDF_DOCUMENT document,
688 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800689 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
690 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700691 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800692
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700693 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700694 return nullptr;
695
Tom Sepez40e9ff32015-11-30 12:39:54 -0800696#ifdef PDF_ENABLE_XFA
Tom Sepez9792f162017-05-16 14:11:30 -0700697 return pDoc->GetXFAPage(page_index).Leak();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800698#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800699 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800700 if (!pDict)
thestig5cc24652016-04-26 11:46:02 -0700701 return nullptr;
702
703 CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true);
704 pPage->ParseContent();
Tom Sepez51da0932015-11-25 16:05:49 -0800705 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800706#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700707}
708
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400709FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800710 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700711 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700712}
713
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400714FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800715 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700716 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700717}
718
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700719#if defined(_WIN32)
rbpottercec01802017-04-17 09:10:21 -0700720namespace {
721
722const double kEpsilonSize = 0.01f;
723
724void GetScaling(CPDF_Page* pPage,
725 int size_x,
726 int size_y,
727 int rotate,
728 double* scale_x,
729 double* scale_y) {
730 ASSERT(pPage);
731 ASSERT(scale_x);
732 ASSERT(scale_y);
733 double page_width = pPage->GetPageWidth();
734 double page_height = pPage->GetPageHeight();
735 if (page_width < kEpsilonSize || page_height < kEpsilonSize)
736 return;
737
738 if (rotate % 2 == 0) {
739 *scale_x = size_x / page_width;
740 *scale_y = size_y / page_height;
741 } else {
742 *scale_x = size_y / page_width;
743 *scale_y = size_x / page_height;
744 }
745}
746
747FX_RECT GetMaskDimensionsAndOffsets(CPDF_Page* pPage,
748 int start_x,
749 int start_y,
750 int size_x,
751 int size_y,
752 int rotate,
753 const CFX_FloatRect& mask_box) {
754 double scale_x = 0.0f;
755 double scale_y = 0.0f;
756 GetScaling(pPage, size_x, size_y, rotate, &scale_x, &scale_y);
757 if (scale_x < kEpsilonSize || scale_y < kEpsilonSize)
758 return FX_RECT();
759
760 // Compute sizes in page points. Round down to catch the entire bitmap.
761 int start_x_bm = static_cast<int>(mask_box.left * scale_x);
762 int start_y_bm = static_cast<int>(mask_box.bottom * scale_y);
763 int size_x_bm = static_cast<int>(mask_box.right * scale_x + 1.0f) -
764 static_cast<int>(mask_box.left * scale_x);
765 int size_y_bm = static_cast<int>(mask_box.top * scale_y + 1.0f) -
766 static_cast<int>(mask_box.bottom * scale_y);
767
768 // Get page rotation
rbpotterce8e51e2017-04-28 12:42:47 -0700769 int page_rotation = pPage->GetPageRotation();
rbpottercec01802017-04-17 09:10:21 -0700770
771 // Compute offsets
772 int offset_x = 0;
773 int offset_y = 0;
rbpotter8d7672e2017-08-16 16:45:44 -0700774 if (size_x > size_y)
775 std::swap(size_x_bm, size_y_bm);
776
rbpottercec01802017-04-17 09:10:21 -0700777 switch ((rotate + page_rotation) % 4) {
778 case 0:
779 offset_x = start_x_bm + start_x;
780 offset_y = start_y + size_y - size_y_bm - start_y_bm;
781 break;
782 case 1:
783 offset_x = start_y_bm + start_x;
784 offset_y = start_x_bm + start_y;
785 break;
786 case 2:
787 offset_x = start_x + size_x - size_x_bm - start_x_bm;
788 offset_y = start_y_bm + start_y;
789 break;
790 case 3:
791 offset_x = start_x + size_x - size_x_bm - start_y_bm;
792 offset_y = start_y + size_y - size_y_bm - start_x_bm;
793 break;
794 }
795 return FX_RECT(offset_x, offset_y, offset_x + size_x_bm,
796 offset_y + size_y_bm);
797}
798
799// Get a bitmap of just the mask section defined by |mask_box| from a full page
800// bitmap |pBitmap|.
Dan Sinclair0b950422017-09-21 15:49:49 -0400801RetainPtr<CFX_DIBitmap> GetMaskBitmap(CPDF_Page* pPage,
802 int start_x,
803 int start_y,
804 int size_x,
805 int size_y,
806 int rotate,
807 RetainPtr<CFX_DIBitmap>& pSrc,
808 const CFX_FloatRect& mask_box,
809 FX_RECT* bitmap_area) {
rbpottercec01802017-04-17 09:10:21 -0700810 ASSERT(bitmap_area);
811 *bitmap_area = GetMaskDimensionsAndOffsets(pPage, start_x, start_y, size_x,
812 size_y, rotate, mask_box);
813 if (bitmap_area->IsEmpty())
814 return nullptr;
815
816 // Create a new bitmap to transfer part of the page bitmap to.
Dan Sinclair0b950422017-09-21 15:49:49 -0400817 RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>();
rbpottercec01802017-04-17 09:10:21 -0700818 pDst->Create(bitmap_area->Width(), bitmap_area->Height(), FXDIB_Argb);
819 pDst->Clear(0x00ffffff);
820 pDst->TransferBitmap(0, 0, bitmap_area->Width(), bitmap_area->Height(), pSrc,
821 bitmap_area->left, bitmap_area->top);
822 return pDst;
823}
824
825void RenderBitmap(CFX_RenderDevice* device,
Dan Sinclair0b950422017-09-21 15:49:49 -0400826 const RetainPtr<CFX_DIBitmap>& pSrc,
rbpottercec01802017-04-17 09:10:21 -0700827 const FX_RECT& mask_area) {
828 int size_x_bm = mask_area.Width();
829 int size_y_bm = mask_area.Height();
830 if (size_x_bm == 0 || size_y_bm == 0)
831 return;
832
833 // Create a new bitmap from the old one
Dan Sinclair0b950422017-09-21 15:49:49 -0400834 RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>();
rbpottercec01802017-04-17 09:10:21 -0700835 pDst->Create(size_x_bm, size_y_bm, FXDIB_Rgb32);
836 pDst->Clear(0xffffffff);
837 pDst->CompositeBitmap(0, 0, size_x_bm, size_y_bm, pSrc, 0, 0,
838 FXDIB_BLEND_NORMAL, nullptr, false);
839
840 if (device->GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
rbpotter8d7672e2017-08-16 16:45:44 -0700841 device->StretchDIBits(pDst, mask_area.left, mask_area.top, size_x_bm,
842 size_y_bm);
rbpottercec01802017-04-17 09:10:21 -0700843 } else {
844 device->SetDIBits(pDst, mask_area.left, mask_area.top);
845 }
846}
847
848} // namespace
849
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400850FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage(HDC dc,
851 FPDF_PAGE page,
852 int start_x,
853 int start_y,
854 int size_x,
855 int size_y,
856 int rotate,
857 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700858 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859 if (!pPage)
860 return;
rbpottercec01802017-04-17 09:10:21 -0700861 pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>());
862 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700863
Dan Sinclair0b950422017-09-21 15:49:49 -0400864 RetainPtr<CFX_DIBitmap> pBitmap;
rbpottercec01802017-04-17 09:10:21 -0700865 // Don't render the full page to bitmap for a mask unless there are a lot
866 // of masks. Full page bitmaps result in large spool sizes, so they should
867 // only be used when necessary. For large numbers of masks, rendering each
868 // individually is inefficient and unlikely to significantly improve spool
rbpotter8d7672e2017-08-16 16:45:44 -0700869 // size.
thestig84144e82016-09-27 15:06:01 -0700870 const bool bNewBitmap =
rbpotter8d7672e2017-08-16 16:45:44 -0700871 pPage->BackgroundAlphaNeeded() ||
872 (pPage->HasImageMask() && pPage->GetMaskBoundingBoxes().size() > 100);
rbpottercec01802017-04-17 09:10:21 -0700873 const bool bHasMask = pPage->HasImageMask() && !bNewBitmap;
874 if (bNewBitmap || bHasMask) {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700875 pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700876 pBitmap->Create(size_x, size_y, FXDIB_Argb);
877 pBitmap->Clear(0x00ffffff);
Dan Sinclaira5085d42017-05-11 16:26:50 -0400878 CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice;
tsepez36eb4bd2016-10-03 15:24:27 -0700879 pContext->m_pDevice = pdfium::WrapUnique(pDevice);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700880 pDevice->Attach(pBitmap, false, nullptr, false);
rbpottercec01802017-04-17 09:10:21 -0700881 if (bHasMask) {
882 pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400883 uint32_t option_flags = pContext->m_pOptions->GetFlags();
884 option_flags |= RENDER_BREAKFORMASKS;
885 pContext->m_pOptions->SetFlags(option_flags);
rbpottercec01802017-04-17 09:10:21 -0700886 }
Jun Fang1aeeceb2015-12-29 10:27:44 +0800887 } else {
Dan Sinclaira5085d42017-05-11 16:26:50 -0400888 pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800889 }
rbpottercec01802017-04-17 09:10:21 -0700890
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700892 rotate, flags, true, nullptr);
Bo Xud4e406e2014-08-13 11:03:19 -0700893
rbpottercec01802017-04-17 09:10:21 -0700894 if (bHasMask) {
895 // Finish rendering the page to bitmap and copy the correct segments
896 // of the page to individual image mask bitmaps.
897 const std::vector<CFX_FloatRect>& mask_boxes =
898 pPage->GetMaskBoundingBoxes();
899 std::vector<FX_RECT> bitmap_areas(mask_boxes.size());
Dan Sinclair0b950422017-09-21 15:49:49 -0400900 std::vector<RetainPtr<CFX_DIBitmap>> bitmaps(mask_boxes.size());
rbpottercec01802017-04-17 09:10:21 -0700901 for (size_t i = 0; i < mask_boxes.size(); i++) {
902 bitmaps[i] =
903 GetMaskBitmap(pPage, start_x, start_y, size_x, size_y, rotate,
904 pBitmap, mask_boxes[i], &bitmap_areas[i]);
905 pContext->m_pRenderer->Continue(nullptr);
906 }
907
908 // Reset rendering context
909 pPage->SetRenderContext(nullptr);
910
911 // Begin rendering to the printer. Add flag to indicate the renderer should
912 // pause after each image mask.
913 pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>());
914 pContext = pPage->GetRenderContext();
Dan Sinclaira5085d42017-05-11 16:26:50 -0400915 pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc);
rbpottercec01802017-04-17 09:10:21 -0700916 pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400917
918 uint32_t option_flags = pContext->m_pOptions->GetFlags();
919 option_flags |= RENDER_BREAKFORMASKS;
920 pContext->m_pOptions->SetFlags(option_flags);
921
rbpottercec01802017-04-17 09:10:21 -0700922 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
923 rotate, flags, true, nullptr);
924
925 // Render masks
926 for (size_t i = 0; i < mask_boxes.size(); i++) {
927 // Render the bitmap for the mask and free the bitmap.
928 if (bitmaps[i]) { // will be null if mask has zero area
929 RenderBitmap(pContext->m_pDevice.get(), bitmaps[i], bitmap_areas[i]);
930 }
931 // Render the next portion of page.
932 pContext->m_pRenderer->Continue(nullptr);
933 }
934 } else if (bNewBitmap) {
Dan Sinclaira5085d42017-05-11 16:26:50 -0400935 CFX_WindowsRenderDevice WinDC(dc);
thestig84144e82016-09-27 15:06:01 -0700936 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700937 auto pDst = pdfium::MakeRetain<CFX_DIBitmap>();
thestig84144e82016-09-27 15:06:01 -0700938 int pitch = pBitmap->GetPitch();
939 pDst->Create(size_x, size_y, FXDIB_Rgb32);
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400940 memset(pDst->GetBuffer(), -1, pitch * size_y);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700941 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
Nicolas Pena6a5c20c2017-04-07 14:12:31 -0400942 FXDIB_BLEND_NORMAL, nullptr, false);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700943 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
thestig84144e82016-09-27 15:06:01 -0700944 } else {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700945 WinDC.SetDIBits(pBitmap, 0, 0);
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700946 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 }
rbpottercec01802017-04-17 09:10:21 -0700948
tsepeze5cb0b12016-10-26 15:06:11 -0700949 pPage->SetRenderContext(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700950}
Lei Zhangae85f872016-02-19 14:57:07 -0800951#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400953FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
954 FPDF_PAGE page,
955 int start_x,
956 int start_y,
957 int size_x,
958 int size_y,
959 int rotate,
960 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700961 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700962 return;
tsepez1e2c5572016-05-25 14:58:09 -0700963
Tom Sepezdb0be962015-10-16 14:00:21 -0700964 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965 if (!pPage)
966 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967
weili9f515bc2016-07-24 08:08:24 -0700968 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700969 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700970
Dan Sinclaira5085d42017-05-11 16:26:50 -0400971 CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice;
weili9f515bc2016-07-24 08:08:24 -0700972 pContext->m_pDevice.reset(pDevice);
dsinclair31b08d42017-03-28 15:47:47 +0000973
Dan Sinclair0b950422017-09-21 15:49:49 -0400974 RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700975 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700976 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700977 rotate, flags, true, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978
caryclark687fbde2016-11-22 12:44:25 -0800979#ifdef _SKIA_SUPPORT_PATHS_
Cary Clark364d18b2017-07-25 10:39:10 -0400980 pDevice->Flush(true);
caryclark687fbde2016-11-22 12:44:25 -0800981 pBitmap->UnPreMultiply();
982#endif
tsepeze5cb0b12016-10-26 15:06:11 -0700983 pPage->SetRenderContext(nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700984}
985
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400986FPDF_EXPORT void FPDF_CALLCONV
987FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap,
988 FPDF_PAGE page,
989 const FS_MATRIX* matrix,
990 const FS_RECTF* clipping,
991 int flags) {
thestiga78ba602016-11-23 15:25:48 -0800992 if (!bitmap)
993 return;
994
995 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
996 if (!pPage)
997 return;
998
999 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
1000 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Tom Sepezf0799fe2017-03-28 09:31:32 -07001001
Dan Sinclaira5085d42017-05-11 16:26:50 -04001002 CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice;
thestiga78ba602016-11-23 15:25:48 -08001003 pContext->m_pDevice.reset(pDevice);
Tom Sepezf0799fe2017-03-28 09:31:32 -07001004
Dan Sinclair0b950422017-09-21 15:49:49 -04001005 RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
thestiga78ba602016-11-23 15:25:48 -08001006 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
1007
thestiga78ba602016-11-23 15:25:48 -08001008 CFX_FloatRect clipping_rect;
1009 if (clipping) {
1010 clipping_rect.left = clipping->left;
1011 clipping_rect.bottom = clipping->bottom;
1012 clipping_rect.right = clipping->right;
1013 clipping_rect.top = clipping->top;
1014 }
Nicolas Pena60bde102017-07-26 13:50:12 -04001015 FX_RECT clip_rect = clipping_rect.ToFxRect();
Nicolas Pena24b07332017-09-13 18:02:11 -04001016 RenderPageImpl(
1017 pContext, pPage,
1018 pPage->GetDisplayMatrixWithTransformation(
1019 clip_rect.left, clip_rect.top, clip_rect.Width(), clip_rect.Height(),
1020 CFX_Matrix(matrix->a, matrix->b, matrix->c, matrix->d, matrix->e,
1021 matrix->f)),
1022 clip_rect, flags, true, nullptr);
thestiga78ba602016-11-23 15:25:48 -08001023
1024 pPage->SetRenderContext(nullptr);
1025}
1026
Cary Clark399be5b2016-03-14 16:51:29 -04001027#ifdef _SKIA_SUPPORT_
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001028FPDF_EXPORT FPDF_RECORDER FPDF_CALLCONV FPDF_RenderPageSkp(FPDF_PAGE page,
1029 int size_x,
1030 int size_y) {
Cary Clark399be5b2016-03-14 16:51:29 -04001031 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
1032 if (!pPage)
1033 return nullptr;
tsepez1e2c5572016-05-25 14:58:09 -07001034
weili9f515bc2016-07-24 08:08:24 -07001035 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -07001036 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Dan Sinclaira5085d42017-05-11 16:26:50 -04001037 CFX_DefaultRenderDevice* skDevice = new CFX_DefaultRenderDevice;
Cary Clark399be5b2016-03-14 16:51:29 -04001038 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
weili9f515bc2016-07-24 08:08:24 -07001039 pContext->m_pDevice.reset(skDevice);
tsepez4cf55152016-11-02 14:37:54 -07001040 FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, true,
tsepez1e2c5572016-05-25 14:58:09 -07001041 nullptr);
tsepeze5cb0b12016-10-26 15:06:11 -07001042 pPage->SetRenderContext(nullptr);
Cary Clark399be5b2016-03-14 16:51:29 -04001043 return recorder;
1044}
1045#endif
1046
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001047FPDF_EXPORT void FPDF_CALLCONV FPDF_ClosePage(FPDF_PAGE page) {
tsepez1e2c5572016-05-25 14:58:09 -07001048 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 if (!page)
1050 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001051#ifdef PDF_ENABLE_XFA
Tom Sepez9792f162017-05-16 14:11:30 -07001052 // Take it back across the API and throw it away.
Dan Sinclair0b950422017-09-21 15:49:49 -04001053 RetainPtr<CPDFXFA_Page>().Unleak(pPage);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001054#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -08001055 CPDFSDK_PageView* pPageView =
tsepez1e2c5572016-05-25 14:58:09 -07001056 static_cast<CPDFSDK_PageView*>(pPage->GetView());
dsinclair1df1efa2016-09-07 09:55:37 -07001057 if (pPageView) {
dsinclairbcf46232016-10-03 13:02:27 -07001058 // We're already destroying the pageview, so bail early.
1059 if (pPageView->IsBeingDestroyed())
1060 return;
1061
dsinclair1df1efa2016-09-07 09:55:37 -07001062 if (pPageView->IsLocked()) {
1063 pPageView->TakePageOwnership();
1064 return;
1065 }
1066
1067 bool owned = pPageView->OwnsPage();
1068 // This will delete the |pPageView| object. We must cleanup the PageView
1069 // first because it will attempt to reset the View on the |pPage| during
1070 // destruction.
dsinclair7cbe68e2016-10-12 11:56:23 -07001071 pPageView->GetFormFillEnv()->RemovePageView(pPage);
dsinclair1df1efa2016-09-07 09:55:37 -07001072 // If the page was owned then the pageview will have deleted the page.
1073 if (owned)
1074 return;
Tom Sepez51da0932015-11-25 16:05:49 -08001075 }
tsepez1e2c5572016-05-25 14:58:09 -07001076 delete pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001077#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001078}
1079
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001080FPDF_EXPORT void FPDF_CALLCONV FPDF_CloseDocument(FPDF_DOCUMENT document) {
Jun Fangfc751362015-12-16 21:23:39 -08001081 delete UnderlyingFromFPDFDocument(document);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001082}
1083
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001084FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetLastError() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001085 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001086}
1087
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001088FPDF_EXPORT void FPDF_CALLCONV FPDF_DeviceToPage(FPDF_PAGE page,
1089 int start_x,
1090 int start_y,
1091 int size_x,
1092 int size_y,
1093 int rotate,
1094 int device_x,
1095 int device_y,
1096 double* page_x,
1097 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -08001098 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001099 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001100 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001101#ifdef PDF_ENABLE_XFA
1102 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
1103 device_y, page_x, page_y);
1104#else // PDF_ENABLE_XFA
Dan Sinclair1b08df12017-02-09 09:17:20 -05001105 CFX_Matrix page2device =
1106 pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
Dan Sinclair1b08df12017-02-09 09:17:20 -05001107
Nicolas Penab21f1742017-06-29 12:02:06 -04001108 CFX_PointF pos = page2device.GetInverse().Transform(
Dan Sinclair05df0752017-03-14 14:43:42 -04001109 CFX_PointF(static_cast<float>(device_x), static_cast<float>(device_y)));
Dan Sinclairafb44562017-02-09 13:07:43 -05001110
Dan Sinclaira0061af2017-02-23 09:25:17 -05001111 *page_x = pos.x;
1112 *page_y = pos.y;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001113#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001114}
1115
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001116FPDF_EXPORT void FPDF_CALLCONV FPDF_PageToDevice(FPDF_PAGE page,
1117 int start_x,
1118 int start_y,
1119 int size_x,
1120 int size_y,
1121 int rotate,
1122 double page_x,
1123 double page_y,
1124 int* device_x,
1125 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -07001126 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001127 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001128 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -07001129 if (!pPage)
1130 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001131#ifdef PDF_ENABLE_XFA
1132 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
1133 device_x, device_y);
1134#else // PDF_ENABLE_XFA
Dan Sinclair1b08df12017-02-09 09:17:20 -05001135 CFX_Matrix page2device =
1136 pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
Dan Sinclaira0061af2017-02-23 09:25:17 -05001137 CFX_PointF pos = page2device.Transform(
Dan Sinclair05df0752017-03-14 14:43:42 -04001138 CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)));
Dan Sinclairafb44562017-02-09 13:07:43 -05001139
Dan Sinclaira0061af2017-02-23 09:25:17 -05001140 *device_x = FXSYS_round(pos.x);
1141 *device_y = FXSYS_round(pos.y);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001142#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001143}
1144
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001145FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_Create(int width,
1146 int height,
1147 int alpha) {
Tom Sepezf0799fe2017-03-28 09:31:32 -07001148 auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
tsepez37b12ad2016-12-14 19:50:23 -08001149 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32))
thestig1cd352e2016-06-07 17:53:06 -07001150 return nullptr;
tsepez37b12ad2016-12-14 19:50:23 -08001151
Tom Sepezf0799fe2017-03-28 09:31:32 -07001152 return pBitmap.Leak();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001153}
1154
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001155FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_CreateEx(int width,
1156 int height,
1157 int format,
1158 void* first_scan,
1159 int stride) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001160 FXDIB_Format fx_format;
1161 switch (format) {
1162 case FPDFBitmap_Gray:
1163 fx_format = FXDIB_8bppRgb;
1164 break;
1165 case FPDFBitmap_BGR:
1166 fx_format = FXDIB_Rgb;
1167 break;
1168 case FPDFBitmap_BGRx:
1169 fx_format = FXDIB_Rgb32;
1170 break;
1171 case FPDFBitmap_BGRA:
1172 fx_format = FXDIB_Argb;
1173 break;
1174 default:
thestig1cd352e2016-06-07 17:53:06 -07001175 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001176 }
Tom Sepezf0799fe2017-03-28 09:31:32 -07001177 auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
Tom Sepezf0799fe2017-03-28 09:31:32 -07001179 return pBitmap.Leak();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001180}
1181
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001182FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetFormat(FPDF_BITMAP bitmap) {
Jane Liu28fb7ba2017-08-02 21:45:57 -04001183 if (!bitmap)
1184 return FPDFBitmap_Unknown;
1185
1186 FXDIB_Format format = CFXBitmapFromFPDFBitmap(bitmap)->GetFormat();
1187 switch (format) {
1188 case FXDIB_8bppRgb:
1189 case FXDIB_8bppMask:
1190 return FPDFBitmap_Gray;
1191 case FXDIB_Rgb:
1192 return FPDFBitmap_BGR;
1193 case FXDIB_Rgb32:
1194 return FPDFBitmap_BGRx;
1195 case FXDIB_Argb:
1196 return FPDFBitmap_BGRA;
1197 default:
1198 return FPDFBitmap_Unknown;
1199 }
1200}
1201
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001202FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
1203 int left,
1204 int top,
1205 int width,
1206 int height,
1207 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -08001208 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001209 return;
thestigbefa4502016-05-26 20:15:19 -07001210
Dan Sinclaira5085d42017-05-11 16:26:50 -04001211 CFX_DefaultRenderDevice device;
Dan Sinclair0b950422017-09-21 15:49:49 -04001212 RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
thestigbefa4502016-05-26 20:15:19 -07001213 device.Attach(pBitmap, false, nullptr, false);
1214 if (!pBitmap->HasAlpha())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001215 color |= 0xFF000000;
1216 FX_RECT rect(left, top, left + width, top + height);
1217 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001218}
1219
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001220FPDF_EXPORT void* FPDF_CALLCONV FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001221 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -07001222}
1223
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001224FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001225 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -07001226}
1227
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001228FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001229 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001230}
1231
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001232FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001233 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234}
1235
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001236FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
Dan Sinclair0b950422017-09-21 15:49:49 -04001237 RetainPtr<CFX_DIBitmap> destroyer;
Tom Sepezf0799fe2017-03-28 09:31:32 -07001238 destroyer.Unleak(CFXBitmapFromFPDFBitmap(bitmap));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001239}
1240
weili9f515bc2016-07-24 08:08:24 -07001241void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001242 FPDF_PAGE page,
1243 int start_x,
1244 int start_y,
1245 int size_x,
1246 int size_y,
1247 int rotate,
1248 int flags,
tsepez4cf55152016-11-02 14:37:54 -07001249 bool bNeedToRestore,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001250 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -07001251 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
1252 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253 return;
1254
Dan Sinclair1b08df12017-02-09 09:17:20 -05001255 RenderPageImpl(pContext, pPage, pPage->GetDisplayMatrix(
1256 start_x, start_y, size_x, size_y, rotate),
1257 FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y),
1258 flags, bNeedToRestore, pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001259}
1260
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001261FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
1262 int page_index,
1263 double* width,
1264 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -08001265 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
1266 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001267 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001268
Tom Sepez40e9ff32015-11-30 12:39:54 -08001269#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001270 int count = pDoc->GetPageCount();
1271 if (page_index < 0 || page_index >= count)
tsepez4cf55152016-11-02 14:37:54 -07001272 return false;
Dan Sinclair0b950422017-09-21 15:49:49 -04001273 RetainPtr<CPDFXFA_Page> pPage = pDoc->GetXFAPage(page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001274 if (!pPage)
tsepez4cf55152016-11-02 14:37:54 -07001275 return false;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001276 *width = pPage->GetPageWidth();
1277 *height = pPage->GetPageHeight();
1278#else // PDF_ENABLE_XFA
1279 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
1280 if (!pDict)
tsepez4cf55152016-11-02 14:37:54 -07001281 return false;
thestig5cc24652016-04-26 11:46:02 -07001282
1283 CPDF_Page page(pDoc, pDict, true);
Tom Sepez51da0932015-11-25 16:05:49 -08001284 *width = page.GetPageWidth();
1285 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001286#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001287
tsepez4cf55152016-11-02 14:37:54 -07001288 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289}
1290
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001291FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001293 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001294 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001295 return true;
Tom Sepez471a1032015-10-15 16:17:18 -07001296 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297 return viewRef.PrintScaling();
1298}
1299
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001300FPDF_EXPORT int FPDF_CALLCONV
1301FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001302 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303 if (!pDoc)
1304 return 1;
1305 CPDF_ViewerPreferences viewRef(pDoc);
1306 return viewRef.NumCopies();
1307}
1308
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001309FPDF_EXPORT FPDF_PAGERANGE FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001310FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001311 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001312 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -07001313 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314 CPDF_ViewerPreferences viewRef(pDoc);
1315 return viewRef.PrintPageRange();
1316}
1317
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001318FPDF_EXPORT FPDF_DUPLEXTYPE FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001319FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001320 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -07001322 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001323 CPDF_ViewerPreferences viewRef(pDoc);
Ryan Harrison275e2602017-09-18 14:23:18 -04001324 ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001325 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001327 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001328 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001329 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001330 return DuplexFlipLongEdge;
1331 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001332}
1333
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001334FPDF_EXPORT unsigned long FPDF_CALLCONV
1335FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document,
1336 FPDF_BYTESTRING key,
1337 char* buffer,
1338 unsigned long length) {
thestig04bebfe2016-11-04 16:07:25 -07001339 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1340 if (!pDoc)
1341 return 0;
1342
1343 CPDF_ViewerPreferences viewRef(pDoc);
Ryan Harrison275e2602017-09-18 14:23:18 -04001344 ByteString bsVal;
thestig04bebfe2016-11-04 16:07:25 -07001345 if (!viewRef.GenericName(key, &bsVal))
1346 return 0;
1347
1348 unsigned long dwStringLen = bsVal.GetLength() + 1;
1349 if (buffer && length >= dwStringLen)
1350 memcpy(buffer, bsVal.c_str(), dwStringLen);
1351 return dwStringLen;
1352}
1353
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001354FPDF_EXPORT FPDF_DWORD FPDF_CALLCONV
1355FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001356 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1357 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001359
Lei Zhang01581062017-08-30 14:19:26 -07001360 const CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001361 if (!pRoot)
1362 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001363
Lei Zhangd983b092015-12-14 16:58:33 -08001364 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001365 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
dsinclair38fd8442016-09-15 10:15:32 -07001366 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001367 if (pDest)
1368 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001369
1370 if (!count.IsValid())
1371 return 0;
1372
1373 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001374}
1375
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001376FPDF_EXPORT FPDF_DEST FPDF_CALLCONV
1377FPDF_GetNamedDestByName(FPDF_DOCUMENT document, FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001379 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380
Tom Sepez471a1032015-10-15 16:17:18 -07001381 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1382 if (!pDoc)
1383 return nullptr;
1384
Lei Zhangd983b092015-12-14 16:58:33 -08001385 CPDF_NameTree name_tree(pDoc, "Dests");
Ryan Harrison275e2602017-09-18 14:23:18 -04001386 return name_tree.LookupNamedDest(pDoc, PDF_DecodeText(ByteString(name)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387}
1388
Tom Sepez51da0932015-11-25 16:05:49 -08001389#ifdef PDF_ENABLE_XFA
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001390FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Init(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001391 if (!str)
1392 return -1;
1393
Dan Sinclair1c5d0b42017-04-03 15:05:11 -04001394 memset(str, 0, sizeof(FPDF_BSTR));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001395 return 0;
1396}
1397
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001398FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Set(FPDF_BSTR* str,
1399 FPDF_LPCSTR bstr,
1400 int length) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001401 if (!str)
1402 return -1;
1403 if (!bstr || !length)
1404 return -1;
1405 if (length == -1)
Ryan Harrison8b1408e2017-09-27 11:07:51 -04001406 length = strlen(bstr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407
1408 if (length == 0) {
1409 if (str->str) {
1410 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001411 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001412 }
1413 str->len = 0;
1414 return 0;
1415 }
1416
1417 if (str->str && str->len < length)
1418 str->str = FX_Realloc(char, str->str, length + 1);
1419 else if (!str->str)
1420 str->str = FX_Alloc(char, length + 1);
1421
1422 str->str[length] = 0;
Dan Sinclair1c5d0b42017-04-03 15:05:11 -04001423 memcpy(str->str, bstr, length);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001424 str->len = length;
1425
1426 return 0;
1427}
1428
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001429FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Clear(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001430 if (!str)
1431 return -1;
1432
1433 if (str->str) {
1434 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001435 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001436 }
1437 str->len = 0;
1438 return 0;
1439}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001440#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001441
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001442FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDF_GetNamedDest(FPDF_DOCUMENT document,
1443 int index,
1444 void* buffer,
1445 long* buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001446 if (!buffer)
1447 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001448
1449 if (index < 0)
1450 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001451
Tom Sepezbf59a072015-10-21 14:07:23 -07001452 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001453 if (!pDoc)
1454 return nullptr;
1455
Lei Zhang01581062017-08-30 14:19:26 -07001456 const CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001457 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001458 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001459
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001460 CPDF_Object* pDestObj = nullptr;
Ryan Harrison275e2602017-09-18 14:23:18 -04001461 WideString wsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001462 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001463 int count = nameTree.GetCount();
1464 if (index >= count) {
dsinclair38fd8442016-09-15 10:15:32 -07001465 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001466 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001467 return nullptr;
1468
1469 pdfium::base::CheckedNumeric<int> checked_count = count;
1470 checked_count += pDest->GetCount();
1471 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1472 return nullptr;
1473
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001475 int i = 0;
Ryan Harrison275e2602017-09-18 14:23:18 -04001476 ByteString bsName;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001477 for (const auto& it : *pDest) {
1478 bsName = it.first;
tsepez0e606b52016-11-18 16:22:41 -08001479 pDestObj = it.second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480 if (!pDestObj)
1481 continue;
1482 if (i == index)
1483 break;
1484 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001485 }
Jane Liu67ccef72017-07-19 13:10:50 -04001486 wsName = PDF_DecodeText(bsName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001487 } else {
Jane Liu67ccef72017-07-19 13:10:50 -04001488 pDestObj = nameTree.LookupValueAndName(index, &wsName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001489 }
1490 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001491 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001492 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
dsinclair38fd8442016-09-15 10:15:32 -07001493 pDestObj = pDict->GetArrayFor("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001494 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001495 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001496 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001497 if (!pDestObj->IsArray())
1498 return nullptr;
1499
Ryan Harrison275e2602017-09-18 14:23:18 -04001500 ByteString utf16Name = wsName.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -07001501 int len = utf16Name.GetLength();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001502 if (!buffer) {
1503 *buflen = len;
thestig9067fd62016-11-23 14:10:06 -08001504 } else if (len <= *buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505 memcpy(buffer, utf16Name.c_str(), len);
1506 *buflen = len;
1507 } else {
1508 *buflen = -1;
1509 }
1510 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001511}