blob: af1d0db1afac31d83ddad363502e07477e3842f5 [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"
Lei Zhangb4e7f302015-11-06 15:52:32 -080042#include "public/fpdf_progressive.h"
Chris Palmere4b035b2017-03-26 15:48:34 -070043#include "third_party/base/allocator/partition_allocator/partition_alloc.h"
Lei Zhang8241df72015-11-06 14:38:48 -080044#include "third_party/base/numerics/safe_conversions_impl.h"
tsepez36eb4bd2016-10-03 15:24:27 -070045#include "third_party/base/ptr_util.h"
Bo Xufdc00a72014-10-28 23:03:33 -070046
Tom Sepez40e9ff32015-11-30 12:39:54 -080047#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070048#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070049#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
50#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
Dan Sinclaire7786682017-03-29 15:18:41 -040051#include "fxbarcode/BC_Library.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080052#include "public/fpdf_formfill.h"
53#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
Tom Sepez40e9ff32015-11-30 12:39:54 -0800568#ifdef PDF_ENABLE_XFA
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400569FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_HasXFAField(FPDF_DOCUMENT document,
570 int* docType) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700571 if (!document)
tsepez4cf55152016-11-02 14:37:54 -0700572 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800573
Lei Zhang01581062017-08-30 14:19:26 -0700574 const CPDF_Document* pDoc =
575 static_cast<CPDFXFA_Context*>(document)->GetPDFDoc();
576 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700577 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800578
Lei Zhang01581062017-08-30 14:19:26 -0700579 const CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580 if (!pRoot)
tsepez4cf55152016-11-02 14:37:54 -0700581 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800582
dsinclair38fd8442016-09-15 10:15:32 -0700583 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 if (!pAcroForm)
tsepez4cf55152016-11-02 14:37:54 -0700585 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800586
dsinclair38fd8442016-09-15 10:15:32 -0700587 CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588 if (!pXFA)
tsepez4cf55152016-11-02 14:37:54 -0700589 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800590
dsinclair38fd8442016-09-15 10:15:32 -0700591 bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false);
thestigded36342016-05-23 17:54:02 -0700592 *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA;
tsepez4cf55152016-11-02 14:37:54 -0700593 return true;
Jun Fange118ce92015-02-17 06:50:08 -0800594}
595
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400596FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_LoadXFA(FPDF_DOCUMENT document) {
Lei Zhang01581062017-08-30 14:19:26 -0700597 return document && static_cast<CPDFXFA_Context*>(document)->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700598}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800599#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700600
tsepezad2441e2016-10-24 10:19:11 -0700601class CMemFile final : public IFX_SeekableReadStream {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602 public:
Dan Sinclair0b950422017-09-21 15:49:49 -0400603 static RetainPtr<CMemFile> Create(uint8_t* pBuf, FX_FILESIZE size) {
604 return RetainPtr<CMemFile>(new CMemFile(pBuf, size));
tsepez833619b2016-12-07 09:21:17 -0800605 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700606
Lei Zhang3884dba2015-10-19 17:27:53 -0700607 FX_FILESIZE GetSize() override { return m_size; }
tsepezf39074c2016-10-26 15:33:58 -0700608 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
tsepez833619b2016-12-07 09:21:17 -0800609 if (offset < 0)
tsepezf39074c2016-10-26 15:33:58 -0700610 return false;
tsepez833619b2016-12-07 09:21:17 -0800611
tsepez4e597c82016-11-07 15:16:01 -0800612 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613 newPos += offset;
tsepez833619b2016-12-07 09:21:17 -0800614 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size)
tsepezf39074c2016-10-26 15:33:58 -0700615 return false;
tsepez833619b2016-12-07 09:21:17 -0800616
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400617 memcpy(buffer, m_pBuf + offset, size);
tsepezf39074c2016-10-26 15:33:58 -0700618 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700620
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621 private:
tsepez833619b2016-12-07 09:21:17 -0800622 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700623
Lei Zhang3884dba2015-10-19 17:27:53 -0700624 uint8_t* const m_pBuf;
625 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700626};
Lei Zhang3884dba2015-10-19 17:27:53 -0700627
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400628FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
629FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password) {
Artem Strygin3f7ff052017-07-27 22:16:13 +0300630 return LoadDocumentImpl(CMemFile::Create((uint8_t*)data_buf, size), password);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700631}
632
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400633FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
635 FPDF_BYTESTRING password) {
Artem Strygin3f7ff052017-07-27 22:16:13 +0300636 return LoadDocumentImpl(pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess),
637 password);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700638}
639
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400640FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_GetFileVersion(FPDF_DOCUMENT doc,
641 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700642 if (!fileVersion)
tsepez4cf55152016-11-02 14:37:54 -0700643 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700644
Tom Sepez471a1032015-10-15 16:17:18 -0700645 *fileVersion = 0;
646 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
647 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700648 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700649
Lei Zhang01581062017-08-30 14:19:26 -0700650 const CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651 if (!pParser)
tsepez4cf55152016-11-02 14:37:54 -0700652 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700653
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700654 *fileVersion = pParser->GetFileVersion();
tsepez4cf55152016-11-02 14:37:54 -0700655 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700656}
657
tsepezc3255f52016-03-25 14:52:27 -0700658// jabdelmalek: changed return type from uint32_t to build on Linux (and match
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700659// header).
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400660FPDF_EXPORT unsigned long FPDF_CALLCONV
661FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700662 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestig27ddf162016-05-23 15:06:59 -0700663 // https://bugs.chromium.org/p/pdfium/issues/detail?id=499
664 if (!pDoc) {
Tom Sepez51da0932015-11-25 16:05:49 -0800665#ifndef PDF_ENABLE_XFA
666 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800667#else // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700668 return 0xFFFFFFFF;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800669#endif // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700670 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700671
thestig27ddf162016-05-23 15:06:59 -0700672 return pDoc->GetUserPermissions();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700673}
674
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400675FPDF_EXPORT int FPDF_CALLCONV
676FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700677 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700678 if (!pDoc || !pDoc->GetParser())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700680
Tom Sepez471a1032015-10-15 16:17:18 -0700681 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
dsinclair38fd8442016-09-15 10:15:32 -0700682 return pDict ? pDict->GetIntegerFor("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700683}
684
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400685FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800686 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700687 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700688}
689
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400690FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDF_LoadPage(FPDF_DOCUMENT document,
691 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800692 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
693 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700694 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800695
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700696 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700697 return nullptr;
698
Tom Sepez40e9ff32015-11-30 12:39:54 -0800699#ifdef PDF_ENABLE_XFA
Tom Sepez9792f162017-05-16 14:11:30 -0700700 return pDoc->GetXFAPage(page_index).Leak();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800701#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800702 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800703 if (!pDict)
thestig5cc24652016-04-26 11:46:02 -0700704 return nullptr;
705
706 CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true);
707 pPage->ParseContent();
Tom Sepez51da0932015-11-25 16:05:49 -0800708 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800709#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700710}
711
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400712FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800713 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700714 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700715}
716
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400717FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800718 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700719 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700720}
721
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700722#if defined(_WIN32)
rbpottercec01802017-04-17 09:10:21 -0700723namespace {
724
725const double kEpsilonSize = 0.01f;
726
727void GetScaling(CPDF_Page* pPage,
728 int size_x,
729 int size_y,
730 int rotate,
731 double* scale_x,
732 double* scale_y) {
733 ASSERT(pPage);
734 ASSERT(scale_x);
735 ASSERT(scale_y);
736 double page_width = pPage->GetPageWidth();
737 double page_height = pPage->GetPageHeight();
738 if (page_width < kEpsilonSize || page_height < kEpsilonSize)
739 return;
740
741 if (rotate % 2 == 0) {
742 *scale_x = size_x / page_width;
743 *scale_y = size_y / page_height;
744 } else {
745 *scale_x = size_y / page_width;
746 *scale_y = size_x / page_height;
747 }
748}
749
750FX_RECT GetMaskDimensionsAndOffsets(CPDF_Page* pPage,
751 int start_x,
752 int start_y,
753 int size_x,
754 int size_y,
755 int rotate,
756 const CFX_FloatRect& mask_box) {
757 double scale_x = 0.0f;
758 double scale_y = 0.0f;
759 GetScaling(pPage, size_x, size_y, rotate, &scale_x, &scale_y);
760 if (scale_x < kEpsilonSize || scale_y < kEpsilonSize)
761 return FX_RECT();
762
763 // Compute sizes in page points. Round down to catch the entire bitmap.
764 int start_x_bm = static_cast<int>(mask_box.left * scale_x);
765 int start_y_bm = static_cast<int>(mask_box.bottom * scale_y);
766 int size_x_bm = static_cast<int>(mask_box.right * scale_x + 1.0f) -
767 static_cast<int>(mask_box.left * scale_x);
768 int size_y_bm = static_cast<int>(mask_box.top * scale_y + 1.0f) -
769 static_cast<int>(mask_box.bottom * scale_y);
770
771 // Get page rotation
rbpotterce8e51e2017-04-28 12:42:47 -0700772 int page_rotation = pPage->GetPageRotation();
rbpottercec01802017-04-17 09:10:21 -0700773
774 // Compute offsets
775 int offset_x = 0;
776 int offset_y = 0;
rbpotter8d7672e2017-08-16 16:45:44 -0700777 if (size_x > size_y)
778 std::swap(size_x_bm, size_y_bm);
779
rbpottercec01802017-04-17 09:10:21 -0700780 switch ((rotate + page_rotation) % 4) {
781 case 0:
782 offset_x = start_x_bm + start_x;
783 offset_y = start_y + size_y - size_y_bm - start_y_bm;
784 break;
785 case 1:
786 offset_x = start_y_bm + start_x;
787 offset_y = start_x_bm + start_y;
788 break;
789 case 2:
790 offset_x = start_x + size_x - size_x_bm - start_x_bm;
791 offset_y = start_y_bm + start_y;
792 break;
793 case 3:
794 offset_x = start_x + size_x - size_x_bm - start_y_bm;
795 offset_y = start_y + size_y - size_y_bm - start_x_bm;
796 break;
797 }
798 return FX_RECT(offset_x, offset_y, offset_x + size_x_bm,
799 offset_y + size_y_bm);
800}
801
802// Get a bitmap of just the mask section defined by |mask_box| from a full page
803// bitmap |pBitmap|.
Dan Sinclair0b950422017-09-21 15:49:49 -0400804RetainPtr<CFX_DIBitmap> GetMaskBitmap(CPDF_Page* pPage,
805 int start_x,
806 int start_y,
807 int size_x,
808 int size_y,
809 int rotate,
810 RetainPtr<CFX_DIBitmap>& pSrc,
811 const CFX_FloatRect& mask_box,
812 FX_RECT* bitmap_area) {
rbpottercec01802017-04-17 09:10:21 -0700813 ASSERT(bitmap_area);
814 *bitmap_area = GetMaskDimensionsAndOffsets(pPage, start_x, start_y, size_x,
815 size_y, rotate, mask_box);
816 if (bitmap_area->IsEmpty())
817 return nullptr;
818
819 // Create a new bitmap to transfer part of the page bitmap to.
Dan Sinclair0b950422017-09-21 15:49:49 -0400820 RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>();
rbpottercec01802017-04-17 09:10:21 -0700821 pDst->Create(bitmap_area->Width(), bitmap_area->Height(), FXDIB_Argb);
822 pDst->Clear(0x00ffffff);
823 pDst->TransferBitmap(0, 0, bitmap_area->Width(), bitmap_area->Height(), pSrc,
824 bitmap_area->left, bitmap_area->top);
825 return pDst;
826}
827
828void RenderBitmap(CFX_RenderDevice* device,
Dan Sinclair0b950422017-09-21 15:49:49 -0400829 const RetainPtr<CFX_DIBitmap>& pSrc,
rbpottercec01802017-04-17 09:10:21 -0700830 const FX_RECT& mask_area) {
831 int size_x_bm = mask_area.Width();
832 int size_y_bm = mask_area.Height();
833 if (size_x_bm == 0 || size_y_bm == 0)
834 return;
835
836 // Create a new bitmap from the old one
Dan Sinclair0b950422017-09-21 15:49:49 -0400837 RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>();
rbpottercec01802017-04-17 09:10:21 -0700838 pDst->Create(size_x_bm, size_y_bm, FXDIB_Rgb32);
839 pDst->Clear(0xffffffff);
840 pDst->CompositeBitmap(0, 0, size_x_bm, size_y_bm, pSrc, 0, 0,
841 FXDIB_BLEND_NORMAL, nullptr, false);
842
843 if (device->GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
rbpotter8d7672e2017-08-16 16:45:44 -0700844 device->StretchDIBits(pDst, mask_area.left, mask_area.top, size_x_bm,
845 size_y_bm);
rbpottercec01802017-04-17 09:10:21 -0700846 } else {
847 device->SetDIBits(pDst, mask_area.left, mask_area.top);
848 }
849}
850
851} // namespace
852
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400853FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage(HDC dc,
854 FPDF_PAGE page,
855 int start_x,
856 int start_y,
857 int size_x,
858 int size_y,
859 int rotate,
860 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700861 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700862 if (!pPage)
863 return;
rbpottercec01802017-04-17 09:10:21 -0700864 pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>());
865 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700866
Dan Sinclair0b950422017-09-21 15:49:49 -0400867 RetainPtr<CFX_DIBitmap> pBitmap;
rbpottercec01802017-04-17 09:10:21 -0700868 // Don't render the full page to bitmap for a mask unless there are a lot
869 // of masks. Full page bitmaps result in large spool sizes, so they should
870 // only be used when necessary. For large numbers of masks, rendering each
871 // individually is inefficient and unlikely to significantly improve spool
rbpotter8d7672e2017-08-16 16:45:44 -0700872 // size.
thestig84144e82016-09-27 15:06:01 -0700873 const bool bNewBitmap =
rbpotter8d7672e2017-08-16 16:45:44 -0700874 pPage->BackgroundAlphaNeeded() ||
875 (pPage->HasImageMask() && pPage->GetMaskBoundingBoxes().size() > 100);
rbpottercec01802017-04-17 09:10:21 -0700876 const bool bHasMask = pPage->HasImageMask() && !bNewBitmap;
877 if (bNewBitmap || bHasMask) {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700878 pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879 pBitmap->Create(size_x, size_y, FXDIB_Argb);
880 pBitmap->Clear(0x00ffffff);
Dan Sinclaira5085d42017-05-11 16:26:50 -0400881 CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice;
tsepez36eb4bd2016-10-03 15:24:27 -0700882 pContext->m_pDevice = pdfium::WrapUnique(pDevice);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700883 pDevice->Attach(pBitmap, false, nullptr, false);
rbpottercec01802017-04-17 09:10:21 -0700884 if (bHasMask) {
885 pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400886 uint32_t option_flags = pContext->m_pOptions->GetFlags();
887 option_flags |= RENDER_BREAKFORMASKS;
888 pContext->m_pOptions->SetFlags(option_flags);
rbpottercec01802017-04-17 09:10:21 -0700889 }
Jun Fang1aeeceb2015-12-29 10:27:44 +0800890 } else {
Dan Sinclaira5085d42017-05-11 16:26:50 -0400891 pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800892 }
rbpottercec01802017-04-17 09:10:21 -0700893
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700895 rotate, flags, true, nullptr);
Bo Xud4e406e2014-08-13 11:03:19 -0700896
rbpottercec01802017-04-17 09:10:21 -0700897 if (bHasMask) {
898 // Finish rendering the page to bitmap and copy the correct segments
899 // of the page to individual image mask bitmaps.
900 const std::vector<CFX_FloatRect>& mask_boxes =
901 pPage->GetMaskBoundingBoxes();
902 std::vector<FX_RECT> bitmap_areas(mask_boxes.size());
Dan Sinclair0b950422017-09-21 15:49:49 -0400903 std::vector<RetainPtr<CFX_DIBitmap>> bitmaps(mask_boxes.size());
rbpottercec01802017-04-17 09:10:21 -0700904 for (size_t i = 0; i < mask_boxes.size(); i++) {
905 bitmaps[i] =
906 GetMaskBitmap(pPage, start_x, start_y, size_x, size_y, rotate,
907 pBitmap, mask_boxes[i], &bitmap_areas[i]);
908 pContext->m_pRenderer->Continue(nullptr);
909 }
910
911 // Reset rendering context
912 pPage->SetRenderContext(nullptr);
913
914 // Begin rendering to the printer. Add flag to indicate the renderer should
915 // pause after each image mask.
916 pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>());
917 pContext = pPage->GetRenderContext();
Dan Sinclaira5085d42017-05-11 16:26:50 -0400918 pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc);
rbpottercec01802017-04-17 09:10:21 -0700919 pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400920
921 uint32_t option_flags = pContext->m_pOptions->GetFlags();
922 option_flags |= RENDER_BREAKFORMASKS;
923 pContext->m_pOptions->SetFlags(option_flags);
924
rbpottercec01802017-04-17 09:10:21 -0700925 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
926 rotate, flags, true, nullptr);
927
928 // Render masks
929 for (size_t i = 0; i < mask_boxes.size(); i++) {
930 // Render the bitmap for the mask and free the bitmap.
931 if (bitmaps[i]) { // will be null if mask has zero area
932 RenderBitmap(pContext->m_pDevice.get(), bitmaps[i], bitmap_areas[i]);
933 }
934 // Render the next portion of page.
935 pContext->m_pRenderer->Continue(nullptr);
936 }
937 } else if (bNewBitmap) {
Dan Sinclaira5085d42017-05-11 16:26:50 -0400938 CFX_WindowsRenderDevice WinDC(dc);
thestig84144e82016-09-27 15:06:01 -0700939 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700940 auto pDst = pdfium::MakeRetain<CFX_DIBitmap>();
thestig84144e82016-09-27 15:06:01 -0700941 int pitch = pBitmap->GetPitch();
942 pDst->Create(size_x, size_y, FXDIB_Rgb32);
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400943 memset(pDst->GetBuffer(), -1, pitch * size_y);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700944 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
Nicolas Pena6a5c20c2017-04-07 14:12:31 -0400945 FXDIB_BLEND_NORMAL, nullptr, false);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700946 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
thestig84144e82016-09-27 15:06:01 -0700947 } else {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700948 WinDC.SetDIBits(pBitmap, 0, 0);
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700949 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700950 }
rbpottercec01802017-04-17 09:10:21 -0700951
tsepeze5cb0b12016-10-26 15:06:11 -0700952 pPage->SetRenderContext(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953}
Lei Zhangae85f872016-02-19 14:57:07 -0800954#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700955
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400956FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
957 FPDF_PAGE page,
958 int start_x,
959 int start_y,
960 int size_x,
961 int size_y,
962 int rotate,
963 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700964 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965 return;
tsepez1e2c5572016-05-25 14:58:09 -0700966
Tom Sepezdb0be962015-10-16 14:00:21 -0700967 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700968 if (!pPage)
969 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970
weili9f515bc2016-07-24 08:08:24 -0700971 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700972 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700973
Dan Sinclaira5085d42017-05-11 16:26:50 -0400974 CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice;
weili9f515bc2016-07-24 08:08:24 -0700975 pContext->m_pDevice.reset(pDevice);
dsinclair31b08d42017-03-28 15:47:47 +0000976
Dan Sinclair0b950422017-09-21 15:49:49 -0400977 RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700978 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700979 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700980 rotate, flags, true, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700981
caryclark687fbde2016-11-22 12:44:25 -0800982#ifdef _SKIA_SUPPORT_PATHS_
Cary Clark364d18b2017-07-25 10:39:10 -0400983 pDevice->Flush(true);
caryclark687fbde2016-11-22 12:44:25 -0800984 pBitmap->UnPreMultiply();
985#endif
tsepeze5cb0b12016-10-26 15:06:11 -0700986 pPage->SetRenderContext(nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700987}
988
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400989FPDF_EXPORT void FPDF_CALLCONV
990FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap,
991 FPDF_PAGE page,
992 const FS_MATRIX* matrix,
993 const FS_RECTF* clipping,
994 int flags) {
thestiga78ba602016-11-23 15:25:48 -0800995 if (!bitmap)
996 return;
997
998 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
999 if (!pPage)
1000 return;
1001
1002 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
1003 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Tom Sepezf0799fe2017-03-28 09:31:32 -07001004
Dan Sinclaira5085d42017-05-11 16:26:50 -04001005 CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice;
thestiga78ba602016-11-23 15:25:48 -08001006 pContext->m_pDevice.reset(pDevice);
Tom Sepezf0799fe2017-03-28 09:31:32 -07001007
Dan Sinclair0b950422017-09-21 15:49:49 -04001008 RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
thestiga78ba602016-11-23 15:25:48 -08001009 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
1010
thestiga78ba602016-11-23 15:25:48 -08001011 CFX_FloatRect clipping_rect;
1012 if (clipping) {
1013 clipping_rect.left = clipping->left;
1014 clipping_rect.bottom = clipping->bottom;
1015 clipping_rect.right = clipping->right;
1016 clipping_rect.top = clipping->top;
1017 }
Nicolas Pena60bde102017-07-26 13:50:12 -04001018 FX_RECT clip_rect = clipping_rect.ToFxRect();
Nicolas Pena24b07332017-09-13 18:02:11 -04001019 RenderPageImpl(
1020 pContext, pPage,
1021 pPage->GetDisplayMatrixWithTransformation(
1022 clip_rect.left, clip_rect.top, clip_rect.Width(), clip_rect.Height(),
1023 CFX_Matrix(matrix->a, matrix->b, matrix->c, matrix->d, matrix->e,
1024 matrix->f)),
1025 clip_rect, flags, true, nullptr);
thestiga78ba602016-11-23 15:25:48 -08001026
1027 pPage->SetRenderContext(nullptr);
1028}
1029
Cary Clark399be5b2016-03-14 16:51:29 -04001030#ifdef _SKIA_SUPPORT_
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001031FPDF_EXPORT FPDF_RECORDER FPDF_CALLCONV FPDF_RenderPageSkp(FPDF_PAGE page,
1032 int size_x,
1033 int size_y) {
Cary Clark399be5b2016-03-14 16:51:29 -04001034 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
1035 if (!pPage)
1036 return nullptr;
tsepez1e2c5572016-05-25 14:58:09 -07001037
weili9f515bc2016-07-24 08:08:24 -07001038 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -07001039 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Dan Sinclaira5085d42017-05-11 16:26:50 -04001040 CFX_DefaultRenderDevice* skDevice = new CFX_DefaultRenderDevice;
Cary Clark399be5b2016-03-14 16:51:29 -04001041 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
weili9f515bc2016-07-24 08:08:24 -07001042 pContext->m_pDevice.reset(skDevice);
tsepez4cf55152016-11-02 14:37:54 -07001043 FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, true,
tsepez1e2c5572016-05-25 14:58:09 -07001044 nullptr);
tsepeze5cb0b12016-10-26 15:06:11 -07001045 pPage->SetRenderContext(nullptr);
Cary Clark399be5b2016-03-14 16:51:29 -04001046 return recorder;
1047}
1048#endif
1049
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001050FPDF_EXPORT void FPDF_CALLCONV FPDF_ClosePage(FPDF_PAGE page) {
tsepez1e2c5572016-05-25 14:58:09 -07001051 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001052 if (!page)
1053 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001054#ifdef PDF_ENABLE_XFA
Tom Sepez9792f162017-05-16 14:11:30 -07001055 // Take it back across the API and throw it away.
Dan Sinclair0b950422017-09-21 15:49:49 -04001056 RetainPtr<CPDFXFA_Page>().Unleak(pPage);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001057#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -08001058 CPDFSDK_PageView* pPageView =
tsepez1e2c5572016-05-25 14:58:09 -07001059 static_cast<CPDFSDK_PageView*>(pPage->GetView());
dsinclair1df1efa2016-09-07 09:55:37 -07001060 if (pPageView) {
dsinclairbcf46232016-10-03 13:02:27 -07001061 // We're already destroying the pageview, so bail early.
1062 if (pPageView->IsBeingDestroyed())
1063 return;
1064
dsinclair1df1efa2016-09-07 09:55:37 -07001065 if (pPageView->IsLocked()) {
1066 pPageView->TakePageOwnership();
1067 return;
1068 }
1069
1070 bool owned = pPageView->OwnsPage();
1071 // This will delete the |pPageView| object. We must cleanup the PageView
1072 // first because it will attempt to reset the View on the |pPage| during
1073 // destruction.
dsinclair7cbe68e2016-10-12 11:56:23 -07001074 pPageView->GetFormFillEnv()->RemovePageView(pPage);
dsinclair1df1efa2016-09-07 09:55:37 -07001075 // If the page was owned then the pageview will have deleted the page.
1076 if (owned)
1077 return;
Tom Sepez51da0932015-11-25 16:05:49 -08001078 }
tsepez1e2c5572016-05-25 14:58:09 -07001079 delete pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001080#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001081}
1082
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001083FPDF_EXPORT void FPDF_CALLCONV FPDF_CloseDocument(FPDF_DOCUMENT document) {
Jun Fangfc751362015-12-16 21:23:39 -08001084 delete UnderlyingFromFPDFDocument(document);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001085}
1086
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001087FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetLastError() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001089}
1090
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001091FPDF_EXPORT void FPDF_CALLCONV FPDF_DeviceToPage(FPDF_PAGE page,
1092 int start_x,
1093 int start_y,
1094 int size_x,
1095 int size_y,
1096 int rotate,
1097 int device_x,
1098 int device_y,
1099 double* page_x,
1100 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -08001101 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001102 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001103 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001104#ifdef PDF_ENABLE_XFA
1105 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
1106 device_y, page_x, page_y);
1107#else // PDF_ENABLE_XFA
Dan Sinclair1b08df12017-02-09 09:17:20 -05001108 CFX_Matrix page2device =
1109 pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
Dan Sinclair1b08df12017-02-09 09:17:20 -05001110
Nicolas Penab21f1742017-06-29 12:02:06 -04001111 CFX_PointF pos = page2device.GetInverse().Transform(
Dan Sinclair05df0752017-03-14 14:43:42 -04001112 CFX_PointF(static_cast<float>(device_x), static_cast<float>(device_y)));
Dan Sinclairafb44562017-02-09 13:07:43 -05001113
Dan Sinclaira0061af2017-02-23 09:25:17 -05001114 *page_x = pos.x;
1115 *page_y = pos.y;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001116#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001117}
1118
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001119FPDF_EXPORT void FPDF_CALLCONV FPDF_PageToDevice(FPDF_PAGE page,
1120 int start_x,
1121 int start_y,
1122 int size_x,
1123 int size_y,
1124 int rotate,
1125 double page_x,
1126 double page_y,
1127 int* device_x,
1128 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -07001129 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001130 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001131 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -07001132 if (!pPage)
1133 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001134#ifdef PDF_ENABLE_XFA
1135 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
1136 device_x, device_y);
1137#else // PDF_ENABLE_XFA
Dan Sinclair1b08df12017-02-09 09:17:20 -05001138 CFX_Matrix page2device =
1139 pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
Dan Sinclaira0061af2017-02-23 09:25:17 -05001140 CFX_PointF pos = page2device.Transform(
Dan Sinclair05df0752017-03-14 14:43:42 -04001141 CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)));
Dan Sinclairafb44562017-02-09 13:07:43 -05001142
Dan Sinclaira0061af2017-02-23 09:25:17 -05001143 *device_x = FXSYS_round(pos.x);
1144 *device_y = FXSYS_round(pos.y);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001145#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001146}
1147
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001148FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_Create(int width,
1149 int height,
1150 int alpha) {
Tom Sepezf0799fe2017-03-28 09:31:32 -07001151 auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
tsepez37b12ad2016-12-14 19:50:23 -08001152 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32))
thestig1cd352e2016-06-07 17:53:06 -07001153 return nullptr;
tsepez37b12ad2016-12-14 19:50:23 -08001154
Tom Sepezf0799fe2017-03-28 09:31:32 -07001155 return pBitmap.Leak();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001156}
1157
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001158FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_CreateEx(int width,
1159 int height,
1160 int format,
1161 void* first_scan,
1162 int stride) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 FXDIB_Format fx_format;
1164 switch (format) {
1165 case FPDFBitmap_Gray:
1166 fx_format = FXDIB_8bppRgb;
1167 break;
1168 case FPDFBitmap_BGR:
1169 fx_format = FXDIB_Rgb;
1170 break;
1171 case FPDFBitmap_BGRx:
1172 fx_format = FXDIB_Rgb32;
1173 break;
1174 case FPDFBitmap_BGRA:
1175 fx_format = FXDIB_Argb;
1176 break;
1177 default:
thestig1cd352e2016-06-07 17:53:06 -07001178 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179 }
Tom Sepezf0799fe2017-03-28 09:31:32 -07001180 auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001181 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
Tom Sepezf0799fe2017-03-28 09:31:32 -07001182 return pBitmap.Leak();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001183}
1184
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001185FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetFormat(FPDF_BITMAP bitmap) {
Jane Liu28fb7ba2017-08-02 21:45:57 -04001186 if (!bitmap)
1187 return FPDFBitmap_Unknown;
1188
1189 FXDIB_Format format = CFXBitmapFromFPDFBitmap(bitmap)->GetFormat();
1190 switch (format) {
1191 case FXDIB_8bppRgb:
1192 case FXDIB_8bppMask:
1193 return FPDFBitmap_Gray;
1194 case FXDIB_Rgb:
1195 return FPDFBitmap_BGR;
1196 case FXDIB_Rgb32:
1197 return FPDFBitmap_BGRx;
1198 case FXDIB_Argb:
1199 return FPDFBitmap_BGRA;
1200 default:
1201 return FPDFBitmap_Unknown;
1202 }
1203}
1204
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001205FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
1206 int left,
1207 int top,
1208 int width,
1209 int height,
1210 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -08001211 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 return;
thestigbefa4502016-05-26 20:15:19 -07001213
Dan Sinclaira5085d42017-05-11 16:26:50 -04001214 CFX_DefaultRenderDevice device;
Dan Sinclair0b950422017-09-21 15:49:49 -04001215 RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
thestigbefa4502016-05-26 20:15:19 -07001216 device.Attach(pBitmap, false, nullptr, false);
1217 if (!pBitmap->HasAlpha())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001218 color |= 0xFF000000;
1219 FX_RECT rect(left, top, left + width, top + height);
1220 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001221}
1222
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001223FPDF_EXPORT void* FPDF_CALLCONV FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001224 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -07001225}
1226
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001227FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001228 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -07001229}
1230
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001231FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001232 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001233}
1234
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001235FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001236 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237}
1238
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001239FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
Dan Sinclair0b950422017-09-21 15:49:49 -04001240 RetainPtr<CFX_DIBitmap> destroyer;
Tom Sepezf0799fe2017-03-28 09:31:32 -07001241 destroyer.Unleak(CFXBitmapFromFPDFBitmap(bitmap));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001242}
1243
weili9f515bc2016-07-24 08:08:24 -07001244void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001245 FPDF_PAGE page,
1246 int start_x,
1247 int start_y,
1248 int size_x,
1249 int size_y,
1250 int rotate,
1251 int flags,
tsepez4cf55152016-11-02 14:37:54 -07001252 bool bNeedToRestore,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -07001254 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
1255 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256 return;
1257
Dan Sinclair1b08df12017-02-09 09:17:20 -05001258 RenderPageImpl(pContext, pPage, pPage->GetDisplayMatrix(
1259 start_x, start_y, size_x, size_y, rotate),
1260 FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y),
1261 flags, bNeedToRestore, pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001262}
1263
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001264FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
1265 int page_index,
1266 double* width,
1267 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -08001268 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
1269 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001270 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001271
Tom Sepez40e9ff32015-11-30 12:39:54 -08001272#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001273 int count = pDoc->GetPageCount();
1274 if (page_index < 0 || page_index >= count)
tsepez4cf55152016-11-02 14:37:54 -07001275 return false;
Dan Sinclair0b950422017-09-21 15:49:49 -04001276 RetainPtr<CPDFXFA_Page> pPage = pDoc->GetXFAPage(page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277 if (!pPage)
tsepez4cf55152016-11-02 14:37:54 -07001278 return false;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001279 *width = pPage->GetPageWidth();
1280 *height = pPage->GetPageHeight();
1281#else // PDF_ENABLE_XFA
1282 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
1283 if (!pDict)
tsepez4cf55152016-11-02 14:37:54 -07001284 return false;
thestig5cc24652016-04-26 11:46:02 -07001285
1286 CPDF_Page page(pDoc, pDict, true);
Tom Sepez51da0932015-11-25 16:05:49 -08001287 *width = page.GetPageWidth();
1288 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001289#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001290
tsepez4cf55152016-11-02 14:37:54 -07001291 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292}
1293
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001294FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001295FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001296 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001297 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001298 return true;
Tom Sepez471a1032015-10-15 16:17:18 -07001299 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001300 return viewRef.PrintScaling();
1301}
1302
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001303FPDF_EXPORT int FPDF_CALLCONV
1304FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001305 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001306 if (!pDoc)
1307 return 1;
1308 CPDF_ViewerPreferences viewRef(pDoc);
1309 return viewRef.NumCopies();
1310}
1311
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001312FPDF_EXPORT FPDF_PAGERANGE FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001313FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001314 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001315 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -07001316 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001317 CPDF_ViewerPreferences viewRef(pDoc);
1318 return viewRef.PrintPageRange();
1319}
1320
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001321FPDF_EXPORT FPDF_DUPLEXTYPE FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001322FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001323 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001324 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -07001325 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001326 CPDF_ViewerPreferences viewRef(pDoc);
Ryan Harrison275e2602017-09-18 14:23:18 -04001327 ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001328 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001329 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001330 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001331 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001332 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001333 return DuplexFlipLongEdge;
1334 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001335}
1336
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001337FPDF_EXPORT unsigned long FPDF_CALLCONV
1338FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document,
1339 FPDF_BYTESTRING key,
1340 char* buffer,
1341 unsigned long length) {
thestig04bebfe2016-11-04 16:07:25 -07001342 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1343 if (!pDoc)
1344 return 0;
1345
1346 CPDF_ViewerPreferences viewRef(pDoc);
Ryan Harrison275e2602017-09-18 14:23:18 -04001347 ByteString bsVal;
thestig04bebfe2016-11-04 16:07:25 -07001348 if (!viewRef.GenericName(key, &bsVal))
1349 return 0;
1350
1351 unsigned long dwStringLen = bsVal.GetLength() + 1;
1352 if (buffer && length >= dwStringLen)
1353 memcpy(buffer, bsVal.c_str(), dwStringLen);
1354 return dwStringLen;
1355}
1356
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001357FPDF_EXPORT FPDF_DWORD FPDF_CALLCONV
1358FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001359 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1360 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001361 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001362
Lei Zhang01581062017-08-30 14:19:26 -07001363 const CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001364 if (!pRoot)
1365 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001366
Lei Zhangd983b092015-12-14 16:58:33 -08001367 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001368 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
dsinclair38fd8442016-09-15 10:15:32 -07001369 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001370 if (pDest)
1371 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001372
1373 if (!count.IsValid())
1374 return 0;
1375
1376 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377}
1378
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001379FPDF_EXPORT FPDF_DEST FPDF_CALLCONV
1380FPDF_GetNamedDestByName(FPDF_DOCUMENT document, FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001381 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001382 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001383
Tom Sepez471a1032015-10-15 16:17:18 -07001384 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1385 if (!pDoc)
1386 return nullptr;
1387
Lei Zhangd983b092015-12-14 16:58:33 -08001388 CPDF_NameTree name_tree(pDoc, "Dests");
Ryan Harrison275e2602017-09-18 14:23:18 -04001389 return name_tree.LookupNamedDest(pDoc, PDF_DecodeText(ByteString(name)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001390}
1391
Tom Sepez51da0932015-11-25 16:05:49 -08001392#ifdef PDF_ENABLE_XFA
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001393FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Init(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001394 if (!str)
1395 return -1;
1396
Dan Sinclair1c5d0b42017-04-03 15:05:11 -04001397 memset(str, 0, sizeof(FPDF_BSTR));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001398 return 0;
1399}
1400
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001401FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Set(FPDF_BSTR* str,
1402 FPDF_LPCSTR bstr,
1403 int length) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001404 if (!str)
1405 return -1;
1406 if (!bstr || !length)
1407 return -1;
1408 if (length == -1)
Ryan Harrison8b1408e2017-09-27 11:07:51 -04001409 length = strlen(bstr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001410
1411 if (length == 0) {
1412 if (str->str) {
1413 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001414 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001415 }
1416 str->len = 0;
1417 return 0;
1418 }
1419
1420 if (str->str && str->len < length)
1421 str->str = FX_Realloc(char, str->str, length + 1);
1422 else if (!str->str)
1423 str->str = FX_Alloc(char, length + 1);
1424
1425 str->str[length] = 0;
Dan Sinclair1c5d0b42017-04-03 15:05:11 -04001426 memcpy(str->str, bstr, length);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001427 str->len = length;
1428
1429 return 0;
1430}
1431
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001432FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Clear(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001433 if (!str)
1434 return -1;
1435
1436 if (str->str) {
1437 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001438 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001439 }
1440 str->len = 0;
1441 return 0;
1442}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001443#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001444
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001445FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDF_GetNamedDest(FPDF_DOCUMENT document,
1446 int index,
1447 void* buffer,
1448 long* buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001449 if (!buffer)
1450 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001451
1452 if (index < 0)
1453 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001454
Tom Sepezbf59a072015-10-21 14:07:23 -07001455 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001456 if (!pDoc)
1457 return nullptr;
1458
Lei Zhang01581062017-08-30 14:19:26 -07001459 const CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001460 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001461 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001462
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001463 CPDF_Object* pDestObj = nullptr;
Ryan Harrison275e2602017-09-18 14:23:18 -04001464 WideString wsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001465 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001466 int count = nameTree.GetCount();
1467 if (index >= count) {
dsinclair38fd8442016-09-15 10:15:32 -07001468 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001469 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001470 return nullptr;
1471
1472 pdfium::base::CheckedNumeric<int> checked_count = count;
1473 checked_count += pDest->GetCount();
1474 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1475 return nullptr;
1476
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001477 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001478 int i = 0;
Ryan Harrison275e2602017-09-18 14:23:18 -04001479 ByteString bsName;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001480 for (const auto& it : *pDest) {
1481 bsName = it.first;
tsepez0e606b52016-11-18 16:22:41 -08001482 pDestObj = it.second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001483 if (!pDestObj)
1484 continue;
1485 if (i == index)
1486 break;
1487 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001488 }
Jane Liu67ccef72017-07-19 13:10:50 -04001489 wsName = PDF_DecodeText(bsName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001490 } else {
Jane Liu67ccef72017-07-19 13:10:50 -04001491 pDestObj = nameTree.LookupValueAndName(index, &wsName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492 }
1493 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001494 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001495 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
dsinclair38fd8442016-09-15 10:15:32 -07001496 pDestObj = pDict->GetArrayFor("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001497 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001498 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001500 if (!pDestObj->IsArray())
1501 return nullptr;
1502
Ryan Harrison275e2602017-09-18 14:23:18 -04001503 ByteString utf16Name = wsName.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -07001504 int len = utf16Name.GetLength();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001505 if (!buffer) {
1506 *buflen = len;
thestig9067fd62016-11-23 14:10:06 -08001507 } else if (len <= *buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001508 memcpy(buffer, utf16Name.c_str(), len);
1509 *buflen = len;
1510 } else {
1511 *buflen = -1;
1512 }
1513 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001514}