blob: bbd60bdd5929d913358cb9d5623623c10748a5c0 [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
83 if (flags & FPDF_LCD_TEXT)
84 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
85 else
86 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
87
88 if (flags & FPDF_NO_NATIVETEXT)
89 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
90 if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
91 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
92 if (flags & FPDF_RENDER_FORCEHALFTONE)
93 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
94#ifndef PDF_ENABLE_XFA
95 if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
96 pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
97 if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
98 pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
99 if (flags & FPDF_RENDER_NO_SMOOTHPATH)
100 pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
101#endif // PDF_ENABLE_XFA
102
103 // Grayscale output
Dan Sinclairf55e72e2017-07-13 14:53:28 -0400104 if (flags & FPDF_GRAYSCALE)
105 pContext->m_pOptions->m_ColorMode = CPDF_RenderOptions::kGray;
thestiga78ba602016-11-23 15:25:48 -0800106
107 const CPDF_OCContext::UsageType usage =
108 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
thestiga78ba602016-11-23 15:25:48 -0800109 pContext->m_pOptions->m_pOCContext =
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700110 pdfium::MakeRetain<CPDF_OCContext>(pPage->m_pDocument.Get(), usage);
thestiga78ba602016-11-23 15:25:48 -0800111
112 pContext->m_pDevice->SaveState();
113 pContext->m_pDevice->SetClip_Rect(clipping_rect);
thestiga78ba602016-11-23 15:25:48 -0800114 pContext->m_pContext = pdfium::MakeUnique<CPDF_RenderContext>(pPage);
115 pContext->m_pContext->AppendLayer(pPage, &matrix);
116
117 if (flags & FPDF_ANNOT) {
118 pContext->m_pAnnots = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
119 bool bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
120 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext.get(),
121 bPrinting, &matrix, false, nullptr);
122 }
123
124 pContext->m_pRenderer = pdfium::MakeUnique<CPDF_ProgressiveRenderer>(
125 pContext->m_pContext.get(), pContext->m_pDevice.get(),
126 pContext->m_pOptions.get());
127 pContext->m_pRenderer->Start(pause);
128 if (bNeedToRestore)
129 pContext->m_pDevice->RestoreState(false);
130}
131
tsepezbea04972016-12-01 13:54:42 -0800132class CPDF_CustomAccess final : public IFX_SeekableReadStream {
133 public:
Tom Sepez40badde2017-05-01 13:21:39 -0700134 template <typename T, typename... Args>
Dan Sinclair0b950422017-09-21 15:49:49 -0400135 friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
tsepezbea04972016-12-01 13:54:42 -0800136
137 // IFX_SeekableReadStream
138 FX_FILESIZE GetSize() override;
tsepezbea04972016-12-01 13:54:42 -0800139 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
140
141 private:
tsepez833619b2016-12-07 09:21:17 -0800142 explicit CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess);
143
tsepezbea04972016-12-01 13:54:42 -0800144 FPDF_FILEACCESS m_FileAccess;
145};
146
147CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
148 : m_FileAccess(*pFileAccess) {}
149
150FX_FILESIZE CPDF_CustomAccess::GetSize() {
151 return m_FileAccess.m_FileLen;
152}
153
tsepezbea04972016-12-01 13:54:42 -0800154bool CPDF_CustomAccess::ReadBlock(void* buffer,
155 FX_FILESIZE offset,
156 size_t size) {
157 if (offset < 0)
158 return false;
159
160 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
161 newPos += offset;
162 if (!newPos.IsValid() ||
163 newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) {
164 return false;
165 }
166 return !!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,
Nicolas Pena46abb662017-05-17 17:23:22 -0400167 static_cast<uint8_t*>(buffer), size);
tsepezbea04972016-12-01 13:54:42 -0800168}
169
Tom Sepez40e9ff32015-11-30 12:39:54 -0800170#ifdef PDF_ENABLE_XFA
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400171class FPDF_FileHandlerContext : public IFX_SeekableStream {
tsepezfa89a202016-12-02 09:48:30 -0800172 public:
Tom Sepez40badde2017-05-01 13:21:39 -0700173 template <typename T, typename... Args>
Dan Sinclair0b950422017-09-21 15:49:49 -0400174 friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
Tom Sepez40badde2017-05-01 13:21:39 -0700175
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400176 ~FPDF_FileHandlerContext() override;
Tom Sepez471a1032015-10-15 16:17:18 -0700177
tsepezfa89a202016-12-02 09:48:30 -0800178 // IFX_SeekableStream:
tsepezfa89a202016-12-02 09:48:30 -0800179 FX_FILESIZE GetSize() override;
180 bool IsEOF() override;
181 FX_FILESIZE GetPosition() override;
182 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
183 size_t ReadBlock(void* buffer, size_t size) override;
184 bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override;
185 bool Flush() override;
Tom Sepezbf59a072015-10-21 14:07:23 -0700186
tsepezfa89a202016-12-02 09:48:30 -0800187 void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; }
Tom Sepezdb0be962015-10-16 14:00:21 -0700188
tsepezfa89a202016-12-02 09:48:30 -0800189 protected:
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400190 explicit FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS);
tsepez833619b2016-12-07 09:21:17 -0800191
tsepezfa89a202016-12-02 09:48:30 -0800192 FPDF_FILEHANDLER* m_pFS;
193 FX_FILESIZE m_nCurPos;
194};
thestigbefa4502016-05-26 20:15:19 -0700195
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400196FPDF_FileHandlerContext::FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 m_pFS = pFS;
198 m_nCurPos = 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700199}
200
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400201FPDF_FileHandlerContext::~FPDF_FileHandlerContext() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 if (m_pFS && m_pFS->Release)
203 m_pFS->Release(m_pFS->clientData);
Bo Xufdc00a72014-10-28 23:03:33 -0700204}
205
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400206FX_FILESIZE FPDF_FileHandlerContext::GetSize() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 if (m_pFS && m_pFS->GetSize)
208 return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData);
209 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700210}
211
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400212bool FPDF_FileHandlerContext::IsEOF() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 return m_nCurPos >= GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -0700214}
215
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400216FX_FILESIZE FPDF_FileHandlerContext::GetPosition() {
weili625ad662016-06-15 11:21:33 -0700217 return m_nCurPos;
218}
219
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400220bool FPDF_FileHandlerContext::ReadBlock(void* buffer,
221 FX_FILESIZE offset,
222 size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 if (!buffer || !size || !m_pFS->ReadBlock)
tsepezf39074c2016-10-26 15:33:58 -0700224 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700225
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
227 (FPDF_DWORD)size) == 0) {
228 m_nCurPos = offset + size;
tsepezf39074c2016-10-26 15:33:58 -0700229 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 }
tsepezf39074c2016-10-26 15:33:58 -0700231 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700232}
233
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400234size_t FPDF_FileHandlerContext::ReadBlock(void* buffer, size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 if (!buffer || !size || !m_pFS->ReadBlock)
236 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 FX_FILESIZE nSize = GetSize();
239 if (m_nCurPos >= nSize)
240 return 0;
241 FX_FILESIZE dwAvail = nSize - m_nCurPos;
242 if (dwAvail < (FX_FILESIZE)size)
Ryan Harrison875e98c2017-09-27 10:53:11 -0400243 size = static_cast<size_t>(dwAvail);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer,
245 (FPDF_DWORD)size) == 0) {
246 m_nCurPos += size;
247 return size;
248 }
Bo Xufdc00a72014-10-28 23:03:33 -0700249
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 return 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700251}
252
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400253bool FPDF_FileHandlerContext::WriteBlock(const void* buffer,
254 FX_FILESIZE offset,
255 size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 if (!m_pFS || !m_pFS->WriteBlock)
tsepezf39074c2016-10-26 15:33:58 -0700257 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700258
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer,
260 (FPDF_DWORD)size) == 0) {
261 m_nCurPos = offset + size;
tsepezf39074c2016-10-26 15:33:58 -0700262 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 }
tsepezf39074c2016-10-26 15:33:58 -0700264 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700265}
266
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400267bool FPDF_FileHandlerContext::Flush() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 if (!m_pFS || !m_pFS->Flush)
tsepezf39074c2016-10-26 15:33:58 -0700269 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 return m_pFS->Flush(m_pFS->clientData) == 0;
Bo Xufdc00a72014-10-28 23:03:33 -0700272}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800273#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274
Artem Strygin3f7ff052017-07-27 22:16:13 +0300275FPDF_DOCUMENT LoadDocumentImpl(
Dan Sinclair0b950422017-09-21 15:49:49 -0400276 const RetainPtr<IFX_SeekableReadStream>& pFileAccess,
Artem Strygin3f7ff052017-07-27 22:16:13 +0300277 FPDF_BYTESTRING password) {
Jane Liu8a1081f2017-08-22 16:28:05 -0400278 if (!pFileAccess) {
279 ProcessParseError(CPDF_Parser::FILE_ERROR);
Artem Strygin3f7ff052017-07-27 22:16:13 +0300280 return nullptr;
Jane Liu8a1081f2017-08-22 16:28:05 -0400281 }
Artem Strygin3f7ff052017-07-27 22:16:13 +0300282
283 auto pParser = pdfium::MakeUnique<CPDF_Parser>();
284 pParser->SetPassword(password);
285
286 auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser));
287 CPDF_Parser::Error error =
288 pDocument->GetParser()->StartParse(pFileAccess, pDocument.get());
289 if (error != CPDF_Parser::SUCCESS) {
290 ProcessParseError(error);
291 return nullptr;
292 }
293 CheckUnSupportError(pDocument.get(), error);
294 return FPDFDocumentFromCPDFDocument(pDocument.release());
295}
296
tsepezfa89a202016-12-02 09:48:30 -0800297} // namespace
298
299UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
300 return static_cast<UnderlyingDocumentType*>(doc);
301}
302
303FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
304 return static_cast<FPDF_DOCUMENT>(doc);
305}
306
307UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
308 return static_cast<UnderlyingPageType*>(page);
309}
310
311CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
312#ifdef PDF_ENABLE_XFA
313 return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
314#else // PDF_ENABLE_XFA
315 return UnderlyingFromFPDFDocument(doc);
316#endif // PDF_ENABLE_XFA
317}
318
319FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
320#ifdef PDF_ENABLE_XFA
321 return doc ? FPDFDocumentFromUnderlying(
322 new CPDFXFA_Context(pdfium::WrapUnique(doc)))
323 : nullptr;
324#else // PDF_ENABLE_XFA
325 return FPDFDocumentFromUnderlying(doc);
326#endif // PDF_ENABLE_XFA
327}
328
329CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
330#ifdef PDF_ENABLE_XFA
331 return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
332#else // PDF_ENABLE_XFA
333 return UnderlyingFromFPDFPage(page);
334#endif // PDF_ENABLE_XFA
335}
336
Jane Liu3b057432017-06-19 10:44:01 -0400337CPDF_PathObject* CPDFPathObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) {
Lei Zhang038740c2017-09-13 13:22:54 -0700338 auto* obj = CPDFPageObjectFromFPDFPageObject(page_object);
339 return obj ? obj->AsPath() : nullptr;
Jane Liu3b057432017-06-19 10:44:01 -0400340}
341
Jane Liubaa7ff42017-06-29 19:18:23 -0400342CPDF_PageObject* CPDFPageObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) {
343 return static_cast<CPDF_PageObject*>(page_object);
344}
345
Jane Liu18ae06d2017-07-18 10:15:16 -0400346CPDF_Object* CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment) {
347 return static_cast<CPDF_Object*>(attachment);
348}
349
Ryan Harrison275e2602017-09-18 14:23:18 -0400350ByteString CFXByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string) {
351 return WideString::FromUTF16LE(wide_string,
352 WideString::WStringLength(wide_string))
Jane Liu18ae06d2017-07-18 10:15:16 -0400353 .UTF8Encode();
354}
355
tsepezfa89a202016-12-02 09:48:30 -0800356CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
357 return static_cast<CFX_DIBitmap*>(bitmap);
358}
359
Miklos Vajna36eed872017-09-20 22:52:43 +0200360const FX_PATHPOINT* FXPathPointFromFPDFPathSegment(FPDF_PATHSEGMENT segment) {
361 return static_cast<const FX_PATHPOINT*>(segment);
362}
363
Ryan Harrison275e2602017-09-18 14:23:18 -0400364unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text,
Jane Liu18ae06d2017-07-18 10:15:16 -0400365 void* buffer,
366 unsigned long buflen) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400367 ByteString encoded_text = text.UTF16LE_Encode();
Jane Liu548334e2017-08-03 16:33:40 -0400368 unsigned long len = encoded_text.GetLength();
Jane Liu18ae06d2017-07-18 10:15:16 -0400369 if (buffer && len <= buflen)
Jane Liu548334e2017-08-03 16:33:40 -0400370 memcpy(buffer, encoded_text.c_str(), len);
371 return len;
372}
373
374unsigned long DecodeStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream,
375 void* buffer,
376 unsigned long buflen) {
377 ASSERT(stream);
378 uint8_t* data = stream->GetRawData();
379 uint32_t len = stream->GetRawSize();
380 CPDF_Dictionary* dict = stream->GetDict();
381 CPDF_Object* decoder = dict ? dict->GetDirectObjectFor("Filter") : nullptr;
382 if (decoder && (decoder->IsArray() || decoder->IsName())) {
383 // Decode the stream if one or more stream filters are specified.
384 uint8_t* decoded_data = nullptr;
385 uint32_t decoded_len = 0;
Ryan Harrison275e2602017-09-18 14:23:18 -0400386 ByteString dummy_last_decoder;
Jane Liu548334e2017-08-03 16:33:40 -0400387 CPDF_Dictionary* dummy_last_param;
388 if (PDF_DataDecode(data, len, dict, dict->GetIntegerFor("DL"), false,
389 &decoded_data, &decoded_len, &dummy_last_decoder,
390 &dummy_last_param)) {
391 if (buffer && buflen >= decoded_len)
392 memcpy(buffer, decoded_data, decoded_len);
393
394 // Free the buffer for the decoded data if it was allocated by
395 // PDF_DataDecode(). Note that for images with a single image-specific
396 // filter, |decoded_data| is directly assigned to be |data|, so
397 // |decoded_data| does not need to be freed.
398 if (decoded_data != data)
399 FX_Free(decoded_data);
400
401 return decoded_len;
402 }
403 }
404 // Copy the raw data and return its length if there is no valid filter
405 // specified or if decoding failed.
406 if (buffer && buflen >= len)
407 memcpy(buffer, data, len);
408
Jane Liu18ae06d2017-07-18 10:15:16 -0400409 return len;
410}
411
Dan Sinclair0b950422017-09-21 15:49:49 -0400412RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream(
tsepez833619b2016-12-07 09:21:17 -0800413 FPDF_FILEACCESS* pFileAccess) {
Tom Sepez40badde2017-05-01 13:21:39 -0700414 return pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700415}
416
tsepezfa89a202016-12-02 09:48:30 -0800417#ifdef PDF_ENABLE_XFA
Dan Sinclair0b950422017-09-21 15:49:49 -0400418RetainPtr<IFX_SeekableStream> MakeSeekableStream(
tsepez833619b2016-12-07 09:21:17 -0800419 FPDF_FILEHANDLER* pFilehandler) {
Dan Sinclaira7caeb52017-09-26 09:23:29 -0400420 return pdfium::MakeRetain<FPDF_FileHandlerContext>(pFilehandler);
tsepezfa89a202016-12-02 09:48:30 -0800421}
422#endif // PDF_ENABLE_XFA
423
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
tsepezc3255f52016-03-25 14:52:27 -0700425static uint32_t foxit_sandbox_policy = 0xFFFFFFFF;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700426
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
428 switch (policy) {
429 case FPDF_POLICY_MACHINETIME_ACCESS: {
430 if (enable)
431 foxit_sandbox_policy |= 0x01;
432 else
433 foxit_sandbox_policy &= 0xFFFFFFFE;
434 } break;
435 default:
436 break;
437 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700438}
439
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
441 switch (policy) {
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700442 case FPDF_POLICY_MACHINETIME_ACCESS:
Lei Zhangb0748bb2015-10-19 12:11:49 -0700443 return !!(foxit_sandbox_policy & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 default:
tsepez4cf55152016-11-02 14:37:54 -0700445 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700447}
448
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400449FPDF_EXPORT void FPDF_CALLCONV FPDF_InitLibrary() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700450 FPDF_InitLibraryWithConfig(nullptr);
451}
452
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400453FPDF_EXPORT void FPDF_CALLCONV
Dan Sinclairf766ad22016-03-14 13:51:24 -0400454FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) {
Dan Sinclair830897a2017-05-11 14:36:10 -0400455 if (g_bLibraryInitialized)
tsepez02759102016-12-01 08:29:25 -0800456 return;
457
Dan Sinclairdbc3d3e2017-05-11 13:41:38 -0400458 FXMEM_InitializePartitionAlloc();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700459
weili47228ac2016-07-20 10:35:31 -0700460 CFX_GEModule* pModule = CFX_GEModule::Get();
Dan Sinclair830897a2017-05-11 14:36:10 -0400461 pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr);
tsepez02759102016-12-01 08:29:25 -0800462
Tom Sepez1b246282015-11-25 15:15:31 -0800463 CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
Lei Zhang76020fc2017-05-18 15:51:20 -0700464 pModuleMgr->Init();
Tom Sepezb4a26182017-03-01 12:15:00 -0800465
Tom Sepez40e9ff32015-11-30 12:39:54 -0800466#ifdef PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700467 FXJSE_Initialize();
468 BC_Library_Init();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800469#endif // PDF_ENABLE_XFA
Tom Sepez452b4f32015-10-13 09:27:27 -0700470 if (cfg && cfg->version >= 2)
471 IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
Dan Sinclair830897a2017-05-11 14:36:10 -0400472
473 g_bLibraryInitialized = true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700474}
475
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400476FPDF_EXPORT void FPDF_CALLCONV FPDF_DestroyLibrary() {
Dan Sinclair830897a2017-05-11 14:36:10 -0400477 if (!g_bLibraryInitialized)
tsepez02759102016-12-01 08:29:25 -0800478 return;
479
Tom Sepez51da0932015-11-25 16:05:49 -0800480#ifdef PDF_ENABLE_XFA
Tom Sepez5628fd72017-04-27 14:58:53 -0700481 BC_Library_Destroy();
dsinclair8837c912016-11-01 11:22:37 -0700482 FXJSE_Finalize();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800483#endif // PDF_ENABLE_XFA
dsinclair8837c912016-11-01 11:22:37 -0700484
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485 CPDF_ModuleMgr::Destroy();
486 CFX_GEModule::Destroy();
Tom Sepez2c286192015-06-18 12:47:11 -0700487
thestig2d6dda12016-06-28 07:39:09 -0700488 IJS_Runtime::Destroy();
Dan Sinclair830897a2017-05-11 14:36:10 -0400489
490 g_bLibraryInitialized = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700491}
492
493#ifndef _WIN32
494int g_LastError;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495void SetLastError(int err) {
496 g_LastError = err;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700497}
498
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700499int GetLastError() {
500 return g_LastError;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700501}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800502#endif // _WIN32
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700503
Tom Sepezf10ae632016-01-26 14:19:52 -0800504void ProcessParseError(CPDF_Parser::Error err) {
tsepezc3255f52016-03-25 14:52:27 -0700505 uint32_t err_code = FPDF_ERR_SUCCESS;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506 // Translate FPDFAPI error code to FPDFVIEW error code
Tom Sepezf10ae632016-01-26 14:19:52 -0800507 switch (err) {
508 case CPDF_Parser::SUCCESS:
509 err_code = FPDF_ERR_SUCCESS;
510 break;
511 case CPDF_Parser::FILE_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700512 err_code = FPDF_ERR_FILE;
513 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800514 case CPDF_Parser::FORMAT_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515 err_code = FPDF_ERR_FORMAT;
516 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800517 case CPDF_Parser::PASSWORD_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518 err_code = FPDF_ERR_PASSWORD;
519 break;
Tom Sepezf10ae632016-01-26 14:19:52 -0800520 case CPDF_Parser::HANDLER_ERROR:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521 err_code = FPDF_ERR_SECURITY;
522 break;
523 }
524 SetLastError(err_code);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700525}
526
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400527FPDF_EXPORT void FPDF_CALLCONV FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
528 FPDF_BOOL enable) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700529 return FSDK_SetSandBoxPolicy(policy, enable);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700530}
531
rbpotterdb764702017-01-12 10:31:43 -0800532#if defined(_WIN32)
533#if defined(PDFIUM_PRINT_TEXT_WITH_GDI)
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400534FPDF_EXPORT void FPDF_CALLCONV
thestigfdb35ff2016-07-18 13:45:44 -0700535FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) {
536 g_pdfium_typeface_accessible_func = func;
537}
538
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400539FPDF_EXPORT void FPDF_CALLCONV FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) {
thestigfdb35ff2016-07-18 13:45:44 -0700540 g_pdfium_print_text_with_gdi = !!use_gdi;
541}
rbpotterdb764702017-01-12 10:31:43 -0800542#endif // PDFIUM_PRINT_TEXT_WITH_GDI
543
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400544FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
545FPDF_SetPrintPostscriptLevel(int postscript_level) {
rbpotter38a01b92017-07-13 11:58:01 -0700546 return postscript_level != 1 && FPDF_SetPrintMode(postscript_level);
547}
548
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400549FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_SetPrintMode(int mode) {
rbpottere8468c42017-07-11 10:04:29 -0700550 if (mode < FPDF_PRINTMODE_EMF || mode > FPDF_PRINTMODE_POSTSCRIPT3)
rbpotterdb764702017-01-12 10:31:43 -0800551 return FALSE;
rbpottere8468c42017-07-11 10:04:29 -0700552 g_pdfium_print_mode = mode;
rbpotterdb764702017-01-12 10:31:43 -0800553 return TRUE;
554}
555#endif // defined(_WIN32)
thestigfdb35ff2016-07-18 13:45:44 -0700556
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400557FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
558FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BYTESTRING password) {
Tom Sepeze3166a82015-08-05 10:50:32 -0700559 // NOTE: the creation of the file needs to be by the embedder on the
560 // other side of this API.
Artem Strygin3f7ff052017-07-27 22:16:13 +0300561 return LoadDocumentImpl(
562 IFX_SeekableReadStream::CreateFromFilename((const char*)file_path),
563 password);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700564}
Jun Fange118ce92015-02-17 06:50:08 -0800565
Tom Sepez40e9ff32015-11-30 12:39:54 -0800566#ifdef PDF_ENABLE_XFA
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400567FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_HasXFAField(FPDF_DOCUMENT document,
568 int* docType) {
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
Lei Zhang01581062017-08-30 14:19:26 -0700572 const CPDF_Document* pDoc =
573 static_cast<CPDFXFA_Context*>(document)->GetPDFDoc();
574 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700575 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800576
Lei Zhang01581062017-08-30 14:19:26 -0700577 const CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700578 if (!pRoot)
tsepez4cf55152016-11-02 14:37:54 -0700579 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800580
dsinclair38fd8442016-09-15 10:15:32 -0700581 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 if (!pAcroForm)
tsepez4cf55152016-11-02 14:37:54 -0700583 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800584
dsinclair38fd8442016-09-15 10:15:32 -0700585 CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 if (!pXFA)
tsepez4cf55152016-11-02 14:37:54 -0700587 return false;
JUN FANG827a1722015-03-05 13:39:21 -0800588
dsinclair38fd8442016-09-15 10:15:32 -0700589 bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false);
thestigded36342016-05-23 17:54:02 -0700590 *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA;
tsepez4cf55152016-11-02 14:37:54 -0700591 return true;
Jun Fange118ce92015-02-17 06:50:08 -0800592}
593
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400594FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_LoadXFA(FPDF_DOCUMENT document) {
Lei Zhang01581062017-08-30 14:19:26 -0700595 return document && static_cast<CPDFXFA_Context*>(document)->LoadXFADoc();
Bo Xufdc00a72014-10-28 23:03:33 -0700596}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800597#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700598
tsepezad2441e2016-10-24 10:19:11 -0700599class CMemFile final : public IFX_SeekableReadStream {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600 public:
Dan Sinclair0b950422017-09-21 15:49:49 -0400601 static RetainPtr<CMemFile> Create(uint8_t* pBuf, FX_FILESIZE size) {
602 return RetainPtr<CMemFile>(new CMemFile(pBuf, size));
tsepez833619b2016-12-07 09:21:17 -0800603 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700604
Lei Zhang3884dba2015-10-19 17:27:53 -0700605 FX_FILESIZE GetSize() override { return m_size; }
tsepezf39074c2016-10-26 15:33:58 -0700606 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
tsepez833619b2016-12-07 09:21:17 -0800607 if (offset < 0)
tsepezf39074c2016-10-26 15:33:58 -0700608 return false;
tsepez833619b2016-12-07 09:21:17 -0800609
tsepez4e597c82016-11-07 15:16:01 -0800610 FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 newPos += offset;
tsepez833619b2016-12-07 09:21:17 -0800612 if (!newPos.IsValid() || newPos.ValueOrDie() > m_size)
tsepezf39074c2016-10-26 15:33:58 -0700613 return false;
tsepez833619b2016-12-07 09:21:17 -0800614
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400615 memcpy(buffer, m_pBuf + offset, size);
tsepezf39074c2016-10-26 15:33:58 -0700616 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700618
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 private:
tsepez833619b2016-12-07 09:21:17 -0800620 CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -0700621
Lei Zhang3884dba2015-10-19 17:27:53 -0700622 uint8_t* const m_pBuf;
623 const FX_FILESIZE m_size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700624};
Lei Zhang3884dba2015-10-19 17:27:53 -0700625
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400626FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
627FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password) {
Artem Strygin3f7ff052017-07-27 22:16:13 +0300628 return LoadDocumentImpl(CMemFile::Create((uint8_t*)data_buf, size), password);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700629}
630
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400631FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
633 FPDF_BYTESTRING password) {
Artem Strygin3f7ff052017-07-27 22:16:13 +0300634 return LoadDocumentImpl(pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess),
635 password);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700636}
637
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400638FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_GetFileVersion(FPDF_DOCUMENT doc,
639 int* fileVersion) {
Tom Sepez471a1032015-10-15 16:17:18 -0700640 if (!fileVersion)
tsepez4cf55152016-11-02 14:37:54 -0700641 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700642
Tom Sepez471a1032015-10-15 16:17:18 -0700643 *fileVersion = 0;
644 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc);
645 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -0700646 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700647
Lei Zhang01581062017-08-30 14:19:26 -0700648 const CPDF_Parser* pParser = pDoc->GetParser();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700649 if (!pParser)
tsepez4cf55152016-11-02 14:37:54 -0700650 return false;
Tom Sepez471a1032015-10-15 16:17:18 -0700651
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652 *fileVersion = pParser->GetFileVersion();
tsepez4cf55152016-11-02 14:37:54 -0700653 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700654}
655
tsepezc3255f52016-03-25 14:52:27 -0700656// jabdelmalek: changed return type from uint32_t to build on Linux (and match
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700657// header).
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400658FPDF_EXPORT unsigned long FPDF_CALLCONV
659FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700660 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestig27ddf162016-05-23 15:06:59 -0700661 // https://bugs.chromium.org/p/pdfium/issues/detail?id=499
662 if (!pDoc) {
Tom Sepez51da0932015-11-25 16:05:49 -0800663#ifndef PDF_ENABLE_XFA
664 return 0;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800665#else // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700666 return 0xFFFFFFFF;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800667#endif // PDF_ENABLE_XFA
thestig27ddf162016-05-23 15:06:59 -0700668 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700669
thestig27ddf162016-05-23 15:06:59 -0700670 return pDoc->GetUserPermissions();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700671}
672
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400673FPDF_EXPORT int FPDF_CALLCONV
674FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -0700675 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
thestigb8db5112016-04-06 12:12:52 -0700676 if (!pDoc || !pDoc->GetParser())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677 return -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700678
Tom Sepez471a1032015-10-15 16:17:18 -0700679 CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict();
dsinclair38fd8442016-09-15 10:15:32 -0700680 return pDict ? pDict->GetIntegerFor("R") : -1;
Bo Xuc5cab022014-09-19 19:16:31 -0700681}
682
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400683FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageCount(FPDF_DOCUMENT document) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800684 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700685 return pDoc ? pDoc->GetPageCount() : 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700686}
687
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400688FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDF_LoadPage(FPDF_DOCUMENT document,
689 int page_index) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800690 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
691 if (!pDoc)
Tom Sepez471a1032015-10-15 16:17:18 -0700692 return nullptr;
Tom Sepez1b246282015-11-25 15:15:31 -0800693
Tom Sepezbbe0e4d2015-10-20 15:41:40 -0700694 if (page_index < 0 || page_index >= pDoc->GetPageCount())
Tom Sepez471a1032015-10-15 16:17:18 -0700695 return nullptr;
696
Tom Sepez40e9ff32015-11-30 12:39:54 -0800697#ifdef PDF_ENABLE_XFA
Tom Sepez9792f162017-05-16 14:11:30 -0700698 return pDoc->GetXFAPage(page_index).Leak();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800699#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800700 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
Lei Zhang412e9082015-12-14 18:34:00 -0800701 if (!pDict)
thestig5cc24652016-04-26 11:46:02 -0700702 return nullptr;
703
704 CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true);
705 pPage->ParseContent();
Tom Sepez51da0932015-11-25 16:05:49 -0800706 return pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800707#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700708}
709
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400710FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageWidth(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800711 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700712 return pPage ? pPage->GetPageWidth() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700713}
714
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400715FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageHeight(FPDF_PAGE page) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800716 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezbf59a072015-10-21 14:07:23 -0700717 return pPage ? pPage->GetPageHeight() : 0.0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700718}
719
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700720#if defined(_WIN32)
rbpottercec01802017-04-17 09:10:21 -0700721namespace {
722
723const double kEpsilonSize = 0.01f;
724
725void GetScaling(CPDF_Page* pPage,
726 int size_x,
727 int size_y,
728 int rotate,
729 double* scale_x,
730 double* scale_y) {
731 ASSERT(pPage);
732 ASSERT(scale_x);
733 ASSERT(scale_y);
734 double page_width = pPage->GetPageWidth();
735 double page_height = pPage->GetPageHeight();
736 if (page_width < kEpsilonSize || page_height < kEpsilonSize)
737 return;
738
739 if (rotate % 2 == 0) {
740 *scale_x = size_x / page_width;
741 *scale_y = size_y / page_height;
742 } else {
743 *scale_x = size_y / page_width;
744 *scale_y = size_x / page_height;
745 }
746}
747
748FX_RECT GetMaskDimensionsAndOffsets(CPDF_Page* pPage,
749 int start_x,
750 int start_y,
751 int size_x,
752 int size_y,
753 int rotate,
754 const CFX_FloatRect& mask_box) {
755 double scale_x = 0.0f;
756 double scale_y = 0.0f;
757 GetScaling(pPage, size_x, size_y, rotate, &scale_x, &scale_y);
758 if (scale_x < kEpsilonSize || scale_y < kEpsilonSize)
759 return FX_RECT();
760
761 // Compute sizes in page points. Round down to catch the entire bitmap.
762 int start_x_bm = static_cast<int>(mask_box.left * scale_x);
763 int start_y_bm = static_cast<int>(mask_box.bottom * scale_y);
764 int size_x_bm = static_cast<int>(mask_box.right * scale_x + 1.0f) -
765 static_cast<int>(mask_box.left * scale_x);
766 int size_y_bm = static_cast<int>(mask_box.top * scale_y + 1.0f) -
767 static_cast<int>(mask_box.bottom * scale_y);
768
769 // Get page rotation
rbpotterce8e51e2017-04-28 12:42:47 -0700770 int page_rotation = pPage->GetPageRotation();
rbpottercec01802017-04-17 09:10:21 -0700771
772 // Compute offsets
773 int offset_x = 0;
774 int offset_y = 0;
rbpotter8d7672e2017-08-16 16:45:44 -0700775 if (size_x > size_y)
776 std::swap(size_x_bm, size_y_bm);
777
rbpottercec01802017-04-17 09:10:21 -0700778 switch ((rotate + page_rotation) % 4) {
779 case 0:
780 offset_x = start_x_bm + start_x;
781 offset_y = start_y + size_y - size_y_bm - start_y_bm;
782 break;
783 case 1:
784 offset_x = start_y_bm + start_x;
785 offset_y = start_x_bm + start_y;
786 break;
787 case 2:
788 offset_x = start_x + size_x - size_x_bm - start_x_bm;
789 offset_y = start_y_bm + start_y;
790 break;
791 case 3:
792 offset_x = start_x + size_x - size_x_bm - start_y_bm;
793 offset_y = start_y + size_y - size_y_bm - start_x_bm;
794 break;
795 }
796 return FX_RECT(offset_x, offset_y, offset_x + size_x_bm,
797 offset_y + size_y_bm);
798}
799
800// Get a bitmap of just the mask section defined by |mask_box| from a full page
801// bitmap |pBitmap|.
Dan Sinclair0b950422017-09-21 15:49:49 -0400802RetainPtr<CFX_DIBitmap> GetMaskBitmap(CPDF_Page* pPage,
803 int start_x,
804 int start_y,
805 int size_x,
806 int size_y,
807 int rotate,
808 RetainPtr<CFX_DIBitmap>& pSrc,
809 const CFX_FloatRect& mask_box,
810 FX_RECT* bitmap_area) {
rbpottercec01802017-04-17 09:10:21 -0700811 ASSERT(bitmap_area);
812 *bitmap_area = GetMaskDimensionsAndOffsets(pPage, start_x, start_y, size_x,
813 size_y, rotate, mask_box);
814 if (bitmap_area->IsEmpty())
815 return nullptr;
816
817 // Create a new bitmap to transfer part of the page bitmap to.
Dan Sinclair0b950422017-09-21 15:49:49 -0400818 RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>();
rbpottercec01802017-04-17 09:10:21 -0700819 pDst->Create(bitmap_area->Width(), bitmap_area->Height(), FXDIB_Argb);
820 pDst->Clear(0x00ffffff);
821 pDst->TransferBitmap(0, 0, bitmap_area->Width(), bitmap_area->Height(), pSrc,
822 bitmap_area->left, bitmap_area->top);
823 return pDst;
824}
825
826void RenderBitmap(CFX_RenderDevice* device,
Dan Sinclair0b950422017-09-21 15:49:49 -0400827 const RetainPtr<CFX_DIBitmap>& pSrc,
rbpottercec01802017-04-17 09:10:21 -0700828 const FX_RECT& mask_area) {
829 int size_x_bm = mask_area.Width();
830 int size_y_bm = mask_area.Height();
831 if (size_x_bm == 0 || size_y_bm == 0)
832 return;
833
834 // Create a new bitmap from the old one
Dan Sinclair0b950422017-09-21 15:49:49 -0400835 RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>();
rbpottercec01802017-04-17 09:10:21 -0700836 pDst->Create(size_x_bm, size_y_bm, FXDIB_Rgb32);
837 pDst->Clear(0xffffffff);
838 pDst->CompositeBitmap(0, 0, size_x_bm, size_y_bm, pSrc, 0, 0,
839 FXDIB_BLEND_NORMAL, nullptr, false);
840
841 if (device->GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
rbpotter8d7672e2017-08-16 16:45:44 -0700842 device->StretchDIBits(pDst, mask_area.left, mask_area.top, size_x_bm,
843 size_y_bm);
rbpottercec01802017-04-17 09:10:21 -0700844 } else {
845 device->SetDIBits(pDst, mask_area.left, mask_area.top);
846 }
847}
848
849} // namespace
850
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400851FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage(HDC dc,
852 FPDF_PAGE page,
853 int start_x,
854 int start_y,
855 int size_x,
856 int size_y,
857 int rotate,
858 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700859 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860 if (!pPage)
861 return;
rbpottercec01802017-04-17 09:10:21 -0700862 pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>());
863 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700864
Dan Sinclair0b950422017-09-21 15:49:49 -0400865 RetainPtr<CFX_DIBitmap> pBitmap;
rbpottercec01802017-04-17 09:10:21 -0700866 // Don't render the full page to bitmap for a mask unless there are a lot
867 // of masks. Full page bitmaps result in large spool sizes, so they should
868 // only be used when necessary. For large numbers of masks, rendering each
869 // individually is inefficient and unlikely to significantly improve spool
rbpotter8d7672e2017-08-16 16:45:44 -0700870 // size.
thestig84144e82016-09-27 15:06:01 -0700871 const bool bNewBitmap =
rbpotter8d7672e2017-08-16 16:45:44 -0700872 pPage->BackgroundAlphaNeeded() ||
873 (pPage->HasImageMask() && pPage->GetMaskBoundingBoxes().size() > 100);
rbpottercec01802017-04-17 09:10:21 -0700874 const bool bHasMask = pPage->HasImageMask() && !bNewBitmap;
875 if (bNewBitmap || bHasMask) {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700876 pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877 pBitmap->Create(size_x, size_y, FXDIB_Argb);
878 pBitmap->Clear(0x00ffffff);
Dan Sinclaira5085d42017-05-11 16:26:50 -0400879 CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice;
tsepez36eb4bd2016-10-03 15:24:27 -0700880 pContext->m_pDevice = pdfium::WrapUnique(pDevice);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700881 pDevice->Attach(pBitmap, false, nullptr, false);
rbpottercec01802017-04-17 09:10:21 -0700882 if (bHasMask) {
883 pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
884 pContext->m_pOptions->m_Flags |= RENDER_BREAKFORMASKS;
885 }
Jun Fang1aeeceb2015-12-29 10:27:44 +0800886 } else {
Dan Sinclaira5085d42017-05-11 16:26:50 -0400887 pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc);
Jun Fang1aeeceb2015-12-29 10:27:44 +0800888 }
rbpottercec01802017-04-17 09:10:21 -0700889
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700891 rotate, flags, true, nullptr);
Bo Xud4e406e2014-08-13 11:03:19 -0700892
rbpottercec01802017-04-17 09:10:21 -0700893 if (bHasMask) {
894 // Finish rendering the page to bitmap and copy the correct segments
895 // of the page to individual image mask bitmaps.
896 const std::vector<CFX_FloatRect>& mask_boxes =
897 pPage->GetMaskBoundingBoxes();
898 std::vector<FX_RECT> bitmap_areas(mask_boxes.size());
Dan Sinclair0b950422017-09-21 15:49:49 -0400899 std::vector<RetainPtr<CFX_DIBitmap>> bitmaps(mask_boxes.size());
rbpottercec01802017-04-17 09:10:21 -0700900 for (size_t i = 0; i < mask_boxes.size(); i++) {
901 bitmaps[i] =
902 GetMaskBitmap(pPage, start_x, start_y, size_x, size_y, rotate,
903 pBitmap, mask_boxes[i], &bitmap_areas[i]);
904 pContext->m_pRenderer->Continue(nullptr);
905 }
906
907 // Reset rendering context
908 pPage->SetRenderContext(nullptr);
909
910 // Begin rendering to the printer. Add flag to indicate the renderer should
911 // pause after each image mask.
912 pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>());
913 pContext = pPage->GetRenderContext();
Dan Sinclaira5085d42017-05-11 16:26:50 -0400914 pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc);
rbpottercec01802017-04-17 09:10:21 -0700915 pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
916 pContext->m_pOptions->m_Flags |= RENDER_BREAKFORMASKS;
917 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
918 rotate, flags, true, nullptr);
919
920 // Render masks
921 for (size_t i = 0; i < mask_boxes.size(); i++) {
922 // Render the bitmap for the mask and free the bitmap.
923 if (bitmaps[i]) { // will be null if mask has zero area
924 RenderBitmap(pContext->m_pDevice.get(), bitmaps[i], bitmap_areas[i]);
925 }
926 // Render the next portion of page.
927 pContext->m_pRenderer->Continue(nullptr);
928 }
929 } else if (bNewBitmap) {
Dan Sinclaira5085d42017-05-11 16:26:50 -0400930 CFX_WindowsRenderDevice WinDC(dc);
thestig84144e82016-09-27 15:06:01 -0700931 if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700932 auto pDst = pdfium::MakeRetain<CFX_DIBitmap>();
thestig84144e82016-09-27 15:06:01 -0700933 int pitch = pBitmap->GetPitch();
934 pDst->Create(size_x, size_y, FXDIB_Rgb32);
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400935 memset(pDst->GetBuffer(), -1, pitch * size_y);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700936 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
Nicolas Pena6a5c20c2017-04-07 14:12:31 -0400937 FXDIB_BLEND_NORMAL, nullptr, false);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700938 WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
thestig84144e82016-09-27 15:06:01 -0700939 } else {
Tom Sepezf0799fe2017-03-28 09:31:32 -0700940 WinDC.SetDIBits(pBitmap, 0, 0);
Lei Zhang6d8b1c22015-06-19 17:26:17 -0700941 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700942 }
rbpottercec01802017-04-17 09:10:21 -0700943
tsepeze5cb0b12016-10-26 15:06:11 -0700944 pPage->SetRenderContext(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945}
Lei Zhangae85f872016-02-19 14:57:07 -0800946#endif // defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400948FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
949 FPDF_PAGE page,
950 int start_x,
951 int start_y,
952 int size_x,
953 int size_y,
954 int rotate,
955 int flags) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700956 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 return;
tsepez1e2c5572016-05-25 14:58:09 -0700958
Tom Sepezdb0be962015-10-16 14:00:21 -0700959 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 if (!pPage)
961 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700962
weili9f515bc2016-07-24 08:08:24 -0700963 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -0700964 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700965
Dan Sinclaira5085d42017-05-11 16:26:50 -0400966 CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice;
weili9f515bc2016-07-24 08:08:24 -0700967 pContext->m_pDevice.reset(pDevice);
dsinclair31b08d42017-03-28 15:47:47 +0000968
Dan Sinclair0b950422017-09-21 15:49:49 -0400969 RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700970 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700971 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -0700972 rotate, flags, true, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973
caryclark687fbde2016-11-22 12:44:25 -0800974#ifdef _SKIA_SUPPORT_PATHS_
Cary Clark364d18b2017-07-25 10:39:10 -0400975 pDevice->Flush(true);
caryclark687fbde2016-11-22 12:44:25 -0800976 pBitmap->UnPreMultiply();
977#endif
tsepeze5cb0b12016-10-26 15:06:11 -0700978 pPage->SetRenderContext(nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700979}
980
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400981FPDF_EXPORT void FPDF_CALLCONV
982FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap,
983 FPDF_PAGE page,
984 const FS_MATRIX* matrix,
985 const FS_RECTF* clipping,
986 int flags) {
thestiga78ba602016-11-23 15:25:48 -0800987 if (!bitmap)
988 return;
989
990 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
991 if (!pPage)
992 return;
993
994 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
995 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700996
Dan Sinclaira5085d42017-05-11 16:26:50 -0400997 CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice;
thestiga78ba602016-11-23 15:25:48 -0800998 pContext->m_pDevice.reset(pDevice);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700999
Dan Sinclair0b950422017-09-21 15:49:49 -04001000 RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
thestiga78ba602016-11-23 15:25:48 -08001001 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
1002
thestiga78ba602016-11-23 15:25:48 -08001003 CFX_FloatRect clipping_rect;
1004 if (clipping) {
1005 clipping_rect.left = clipping->left;
1006 clipping_rect.bottom = clipping->bottom;
1007 clipping_rect.right = clipping->right;
1008 clipping_rect.top = clipping->top;
1009 }
Nicolas Pena60bde102017-07-26 13:50:12 -04001010 FX_RECT clip_rect = clipping_rect.ToFxRect();
Nicolas Pena24b07332017-09-13 18:02:11 -04001011 RenderPageImpl(
1012 pContext, pPage,
1013 pPage->GetDisplayMatrixWithTransformation(
1014 clip_rect.left, clip_rect.top, clip_rect.Width(), clip_rect.Height(),
1015 CFX_Matrix(matrix->a, matrix->b, matrix->c, matrix->d, matrix->e,
1016 matrix->f)),
1017 clip_rect, flags, true, nullptr);
thestiga78ba602016-11-23 15:25:48 -08001018
1019 pPage->SetRenderContext(nullptr);
1020}
1021
Cary Clark399be5b2016-03-14 16:51:29 -04001022#ifdef _SKIA_SUPPORT_
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001023FPDF_EXPORT FPDF_RECORDER FPDF_CALLCONV FPDF_RenderPageSkp(FPDF_PAGE page,
1024 int size_x,
1025 int size_y) {
Cary Clark399be5b2016-03-14 16:51:29 -04001026 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
1027 if (!pPage)
1028 return nullptr;
tsepez1e2c5572016-05-25 14:58:09 -07001029
weili9f515bc2016-07-24 08:08:24 -07001030 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -07001031 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
Dan Sinclaira5085d42017-05-11 16:26:50 -04001032 CFX_DefaultRenderDevice* skDevice = new CFX_DefaultRenderDevice;
Cary Clark399be5b2016-03-14 16:51:29 -04001033 FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y);
weili9f515bc2016-07-24 08:08:24 -07001034 pContext->m_pDevice.reset(skDevice);
tsepez4cf55152016-11-02 14:37:54 -07001035 FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, true,
tsepez1e2c5572016-05-25 14:58:09 -07001036 nullptr);
tsepeze5cb0b12016-10-26 15:06:11 -07001037 pPage->SetRenderContext(nullptr);
Cary Clark399be5b2016-03-14 16:51:29 -04001038 return recorder;
1039}
1040#endif
1041
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001042FPDF_EXPORT void FPDF_CALLCONV FPDF_ClosePage(FPDF_PAGE page) {
tsepez1e2c5572016-05-25 14:58:09 -07001043 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044 if (!page)
1045 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001046#ifdef PDF_ENABLE_XFA
Tom Sepez9792f162017-05-16 14:11:30 -07001047 // Take it back across the API and throw it away.
Dan Sinclair0b950422017-09-21 15:49:49 -04001048 RetainPtr<CPDFXFA_Page>().Unleak(pPage);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001049#else // PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -08001050 CPDFSDK_PageView* pPageView =
tsepez1e2c5572016-05-25 14:58:09 -07001051 static_cast<CPDFSDK_PageView*>(pPage->GetView());
dsinclair1df1efa2016-09-07 09:55:37 -07001052 if (pPageView) {
dsinclairbcf46232016-10-03 13:02:27 -07001053 // We're already destroying the pageview, so bail early.
1054 if (pPageView->IsBeingDestroyed())
1055 return;
1056
dsinclair1df1efa2016-09-07 09:55:37 -07001057 if (pPageView->IsLocked()) {
1058 pPageView->TakePageOwnership();
1059 return;
1060 }
1061
1062 bool owned = pPageView->OwnsPage();
1063 // This will delete the |pPageView| object. We must cleanup the PageView
1064 // first because it will attempt to reset the View on the |pPage| during
1065 // destruction.
dsinclair7cbe68e2016-10-12 11:56:23 -07001066 pPageView->GetFormFillEnv()->RemovePageView(pPage);
dsinclair1df1efa2016-09-07 09:55:37 -07001067 // If the page was owned then the pageview will have deleted the page.
1068 if (owned)
1069 return;
Tom Sepez51da0932015-11-25 16:05:49 -08001070 }
tsepez1e2c5572016-05-25 14:58:09 -07001071 delete pPage;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001072#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001073}
1074
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001075FPDF_EXPORT void FPDF_CALLCONV FPDF_CloseDocument(FPDF_DOCUMENT document) {
Jun Fangfc751362015-12-16 21:23:39 -08001076 delete UnderlyingFromFPDFDocument(document);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001077}
1078
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001079FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetLastError() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080 return GetLastError();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001081}
1082
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001083FPDF_EXPORT void FPDF_CALLCONV FPDF_DeviceToPage(FPDF_PAGE page,
1084 int start_x,
1085 int start_y,
1086 int size_x,
1087 int size_y,
1088 int rotate,
1089 int device_x,
1090 int device_y,
1091 double* page_x,
1092 double* page_y) {
Lei Zhang412e9082015-12-14 18:34:00 -08001093 if (!page || !page_x || !page_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001095 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001096#ifdef PDF_ENABLE_XFA
1097 pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
1098 device_y, page_x, page_y);
1099#else // PDF_ENABLE_XFA
Dan Sinclair1b08df12017-02-09 09:17:20 -05001100 CFX_Matrix page2device =
1101 pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
Dan Sinclair1b08df12017-02-09 09:17:20 -05001102
Nicolas Penab21f1742017-06-29 12:02:06 -04001103 CFX_PointF pos = page2device.GetInverse().Transform(
Dan Sinclair05df0752017-03-14 14:43:42 -04001104 CFX_PointF(static_cast<float>(device_x), static_cast<float>(device_y)));
Dan Sinclairafb44562017-02-09 13:07:43 -05001105
Dan Sinclaira0061af2017-02-23 09:25:17 -05001106 *page_x = pos.x;
1107 *page_y = pos.y;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001108#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001109}
1110
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001111FPDF_EXPORT void FPDF_CALLCONV FPDF_PageToDevice(FPDF_PAGE page,
1112 int start_x,
1113 int start_y,
1114 int size_x,
1115 int size_y,
1116 int rotate,
1117 double page_x,
1118 double page_y,
1119 int* device_x,
1120 int* device_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -07001121 if (!device_x || !device_y)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001122 return;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001123 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
Tom Sepezdb0be962015-10-16 14:00:21 -07001124 if (!pPage)
1125 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001126#ifdef PDF_ENABLE_XFA
1127 pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
1128 device_x, device_y);
1129#else // PDF_ENABLE_XFA
Dan Sinclair1b08df12017-02-09 09:17:20 -05001130 CFX_Matrix page2device =
1131 pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
Dan Sinclaira0061af2017-02-23 09:25:17 -05001132 CFX_PointF pos = page2device.Transform(
Dan Sinclair05df0752017-03-14 14:43:42 -04001133 CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)));
Dan Sinclairafb44562017-02-09 13:07:43 -05001134
Dan Sinclaira0061af2017-02-23 09:25:17 -05001135 *device_x = FXSYS_round(pos.x);
1136 *device_y = FXSYS_round(pos.y);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001137#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001138}
1139
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001140FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_Create(int width,
1141 int height,
1142 int alpha) {
Tom Sepezf0799fe2017-03-28 09:31:32 -07001143 auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
tsepez37b12ad2016-12-14 19:50:23 -08001144 if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32))
thestig1cd352e2016-06-07 17:53:06 -07001145 return nullptr;
tsepez37b12ad2016-12-14 19:50:23 -08001146
Tom Sepezf0799fe2017-03-28 09:31:32 -07001147 return pBitmap.Leak();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001148}
1149
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001150FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_CreateEx(int width,
1151 int height,
1152 int format,
1153 void* first_scan,
1154 int stride) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001155 FXDIB_Format fx_format;
1156 switch (format) {
1157 case FPDFBitmap_Gray:
1158 fx_format = FXDIB_8bppRgb;
1159 break;
1160 case FPDFBitmap_BGR:
1161 fx_format = FXDIB_Rgb;
1162 break;
1163 case FPDFBitmap_BGRx:
1164 fx_format = FXDIB_Rgb32;
1165 break;
1166 case FPDFBitmap_BGRA:
1167 fx_format = FXDIB_Argb;
1168 break;
1169 default:
thestig1cd352e2016-06-07 17:53:06 -07001170 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171 }
Tom Sepezf0799fe2017-03-28 09:31:32 -07001172 auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173 pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
Tom Sepezf0799fe2017-03-28 09:31:32 -07001174 return pBitmap.Leak();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001175}
1176
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001177FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetFormat(FPDF_BITMAP bitmap) {
Jane Liu28fb7ba2017-08-02 21:45:57 -04001178 if (!bitmap)
1179 return FPDFBitmap_Unknown;
1180
1181 FXDIB_Format format = CFXBitmapFromFPDFBitmap(bitmap)->GetFormat();
1182 switch (format) {
1183 case FXDIB_8bppRgb:
1184 case FXDIB_8bppMask:
1185 return FPDFBitmap_Gray;
1186 case FXDIB_Rgb:
1187 return FPDFBitmap_BGR;
1188 case FXDIB_Rgb32:
1189 return FPDFBitmap_BGRx;
1190 case FXDIB_Argb:
1191 return FPDFBitmap_BGRA;
1192 default:
1193 return FPDFBitmap_Unknown;
1194 }
1195}
1196
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001197FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
1198 int left,
1199 int top,
1200 int width,
1201 int height,
1202 FPDF_DWORD color) {
Lei Zhang412e9082015-12-14 18:34:00 -08001203 if (!bitmap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001204 return;
thestigbefa4502016-05-26 20:15:19 -07001205
Dan Sinclaira5085d42017-05-11 16:26:50 -04001206 CFX_DefaultRenderDevice device;
Dan Sinclair0b950422017-09-21 15:49:49 -04001207 RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
thestigbefa4502016-05-26 20:15:19 -07001208 device.Attach(pBitmap, false, nullptr, false);
1209 if (!pBitmap->HasAlpha())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210 color |= 0xFF000000;
1211 FX_RECT rect(left, top, left + width, top + height);
1212 device.FillRect(&rect, color);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001213}
1214
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001215FPDF_EXPORT void* FPDF_CALLCONV FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001216 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr;
Bo Xu9114e832014-07-14 13:22:47 -07001217}
1218
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001219FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001220 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0;
Bo Xu9114e832014-07-14 13:22:47 -07001221}
1222
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001223FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001224 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225}
1226
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001227FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
thestigbefa4502016-05-26 20:15:19 -07001228 return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229}
1230
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001231FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
Dan Sinclair0b950422017-09-21 15:49:49 -04001232 RetainPtr<CFX_DIBitmap> destroyer;
Tom Sepezf0799fe2017-03-28 09:31:32 -07001233 destroyer.Unleak(CFXBitmapFromFPDFBitmap(bitmap));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001234}
1235
weili9f515bc2016-07-24 08:08:24 -07001236void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001237 FPDF_PAGE page,
1238 int start_x,
1239 int start_y,
1240 int size_x,
1241 int size_y,
1242 int rotate,
1243 int flags,
tsepez4cf55152016-11-02 14:37:54 -07001244 bool bNeedToRestore,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001245 IFSDK_PAUSE_Adapter* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -07001246 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
1247 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001248 return;
1249
Dan Sinclair1b08df12017-02-09 09:17:20 -05001250 RenderPageImpl(pContext, pPage, pPage->GetDisplayMatrix(
1251 start_x, start_y, size_x, size_y, rotate),
1252 FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y),
1253 flags, bNeedToRestore, pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254}
1255
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001256FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
1257 int page_index,
1258 double* width,
1259 double* height) {
Tom Sepez540c4362015-11-24 13:33:57 -08001260 UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
1261 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001262 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001263
Tom Sepez40e9ff32015-11-30 12:39:54 -08001264#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 int count = pDoc->GetPageCount();
1266 if (page_index < 0 || page_index >= count)
tsepez4cf55152016-11-02 14:37:54 -07001267 return false;
Dan Sinclair0b950422017-09-21 15:49:49 -04001268 RetainPtr<CPDFXFA_Page> pPage = pDoc->GetXFAPage(page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001269 if (!pPage)
tsepez4cf55152016-11-02 14:37:54 -07001270 return false;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001271 *width = pPage->GetPageWidth();
1272 *height = pPage->GetPageHeight();
1273#else // PDF_ENABLE_XFA
1274 CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
1275 if (!pDict)
tsepez4cf55152016-11-02 14:37:54 -07001276 return false;
thestig5cc24652016-04-26 11:46:02 -07001277
1278 CPDF_Page page(pDoc, pDict, true);
Tom Sepez51da0932015-11-25 16:05:49 -08001279 *width = page.GetPageWidth();
1280 *height = page.GetPageHeight();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001281#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001282
tsepez4cf55152016-11-02 14:37:54 -07001283 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001284}
1285
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001286FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001287FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001288 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001289 if (!pDoc)
tsepez4cf55152016-11-02 14:37:54 -07001290 return true;
Tom Sepez471a1032015-10-15 16:17:18 -07001291 CPDF_ViewerPreferences viewRef(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292 return viewRef.PrintScaling();
1293}
1294
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001295FPDF_EXPORT int FPDF_CALLCONV
1296FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001297 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001298 if (!pDoc)
1299 return 1;
1300 CPDF_ViewerPreferences viewRef(pDoc);
1301 return viewRef.NumCopies();
1302}
1303
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001304FPDF_EXPORT FPDF_PAGERANGE FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001306 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001307 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -07001308 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001309 CPDF_ViewerPreferences viewRef(pDoc);
1310 return viewRef.PrintPageRange();
1311}
1312
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001313FPDF_EXPORT FPDF_DUPLEXTYPE FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001314FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001315 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001316 if (!pDoc)
Bo Xu9114e832014-07-14 13:22:47 -07001317 return DuplexUndefined;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318 CPDF_ViewerPreferences viewRef(pDoc);
Ryan Harrison275e2602017-09-18 14:23:18 -04001319 ByteString duplex = viewRef.Duplex();
Lei Zhangd983b092015-12-14 16:58:33 -08001320 if ("Simplex" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001321 return Simplex;
Lei Zhangd983b092015-12-14 16:58:33 -08001322 if ("DuplexFlipShortEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001323 return DuplexFlipShortEdge;
Lei Zhangd983b092015-12-14 16:58:33 -08001324 if ("DuplexFlipLongEdge" == duplex)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001325 return DuplexFlipLongEdge;
1326 return DuplexUndefined;
Bo Xu9114e832014-07-14 13:22:47 -07001327}
1328
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001329FPDF_EXPORT unsigned long FPDF_CALLCONV
1330FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document,
1331 FPDF_BYTESTRING key,
1332 char* buffer,
1333 unsigned long length) {
thestig04bebfe2016-11-04 16:07:25 -07001334 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1335 if (!pDoc)
1336 return 0;
1337
1338 CPDF_ViewerPreferences viewRef(pDoc);
Ryan Harrison275e2602017-09-18 14:23:18 -04001339 ByteString bsVal;
thestig04bebfe2016-11-04 16:07:25 -07001340 if (!viewRef.GenericName(key, &bsVal))
1341 return 0;
1342
1343 unsigned long dwStringLen = bsVal.GetLength() + 1;
1344 if (buffer && length >= dwStringLen)
1345 memcpy(buffer, bsVal.c_str(), dwStringLen);
1346 return dwStringLen;
1347}
1348
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001349FPDF_EXPORT FPDF_DWORD FPDF_CALLCONV
1350FPDF_CountNamedDests(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -07001351 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1352 if (!pDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001353 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001354
Lei Zhang01581062017-08-30 14:19:26 -07001355 const CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001356 if (!pRoot)
1357 return 0;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001358
Lei Zhangd983b092015-12-14 16:58:33 -08001359 CPDF_NameTree nameTree(pDoc, "Dests");
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001360 pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount();
dsinclair38fd8442016-09-15 10:15:32 -07001361 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362 if (pDest)
1363 count += pDest->GetCount();
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001364
1365 if (!count.IsValid())
1366 return 0;
1367
1368 return count.ValueOrDie();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001369}
1370
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001371FPDF_EXPORT FPDF_DEST FPDF_CALLCONV
1372FPDF_GetNamedDestByName(FPDF_DOCUMENT document, FPDF_BYTESTRING name) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373 if (!name || name[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -07001374 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375
Tom Sepez471a1032015-10-15 16:17:18 -07001376 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
1377 if (!pDoc)
1378 return nullptr;
1379
Lei Zhangd983b092015-12-14 16:58:33 -08001380 CPDF_NameTree name_tree(pDoc, "Dests");
Ryan Harrison275e2602017-09-18 14:23:18 -04001381 return name_tree.LookupNamedDest(pDoc, PDF_DecodeText(ByteString(name)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001382}
1383
Tom Sepez51da0932015-11-25 16:05:49 -08001384#ifdef PDF_ENABLE_XFA
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001385FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Init(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001386 if (!str)
1387 return -1;
1388
Dan Sinclair1c5d0b42017-04-03 15:05:11 -04001389 memset(str, 0, sizeof(FPDF_BSTR));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001390 return 0;
1391}
1392
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001393FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Set(FPDF_BSTR* str,
1394 FPDF_LPCSTR bstr,
1395 int length) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396 if (!str)
1397 return -1;
1398 if (!bstr || !length)
1399 return -1;
1400 if (length == -1)
1401 length = FXSYS_strlen(bstr);
1402
1403 if (length == 0) {
1404 if (str->str) {
1405 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001406 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407 }
1408 str->len = 0;
1409 return 0;
1410 }
1411
1412 if (str->str && str->len < length)
1413 str->str = FX_Realloc(char, str->str, length + 1);
1414 else if (!str->str)
1415 str->str = FX_Alloc(char, length + 1);
1416
1417 str->str[length] = 0;
Dan Sinclair1c5d0b42017-04-03 15:05:11 -04001418 memcpy(str->str, bstr, length);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001419 str->len = length;
1420
1421 return 0;
1422}
1423
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001424FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Clear(FPDF_BSTR* str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001425 if (!str)
1426 return -1;
1427
1428 if (str->str) {
1429 FX_Free(str->str);
thestig1cd352e2016-06-07 17:53:06 -07001430 str->str = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431 }
1432 str->len = 0;
1433 return 0;
1434}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001435#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001436
Dan Sinclair00d2ad12017-08-10 14:13:02 -04001437FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDF_GetNamedDest(FPDF_DOCUMENT document,
1438 int index,
1439 void* buffer,
1440 long* buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001441 if (!buffer)
1442 *buflen = 0;
Tom Sepez540c4362015-11-24 13:33:57 -08001443
1444 if (index < 0)
1445 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001446
Tom Sepezbf59a072015-10-21 14:07:23 -07001447 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -08001448 if (!pDoc)
1449 return nullptr;
1450
Lei Zhang01581062017-08-30 14:19:26 -07001451 const CPDF_Dictionary* pRoot = pDoc->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001452 if (!pRoot)
Tom Sepez540c4362015-11-24 13:33:57 -08001453 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001454
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001455 CPDF_Object* pDestObj = nullptr;
Ryan Harrison275e2602017-09-18 14:23:18 -04001456 WideString wsName;
Lei Zhangd983b092015-12-14 16:58:33 -08001457 CPDF_NameTree nameTree(pDoc, "Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001458 int count = nameTree.GetCount();
1459 if (index >= count) {
dsinclair38fd8442016-09-15 10:15:32 -07001460 CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001461 if (!pDest)
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001462 return nullptr;
1463
1464 pdfium::base::CheckedNumeric<int> checked_count = count;
1465 checked_count += pDest->GetCount();
1466 if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
1467 return nullptr;
1468
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001469 index -= count;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001470 int i = 0;
Ryan Harrison275e2602017-09-18 14:23:18 -04001471 ByteString bsName;
Oliver Chang3f1c71f2016-01-11 08:45:31 -08001472 for (const auto& it : *pDest) {
1473 bsName = it.first;
tsepez0e606b52016-11-18 16:22:41 -08001474 pDestObj = it.second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001475 if (!pDestObj)
1476 continue;
1477 if (i == index)
1478 break;
1479 i++;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001480 }
Jane Liu67ccef72017-07-19 13:10:50 -04001481 wsName = PDF_DecodeText(bsName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001482 } else {
Jane Liu67ccef72017-07-19 13:10:50 -04001483 pDestObj = nameTree.LookupValueAndName(index, &wsName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001484 }
1485 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001486 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -04001487 if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
dsinclair38fd8442016-09-15 10:15:32 -07001488 pDestObj = pDict->GetArrayFor("D");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001489 if (!pDestObj)
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001490 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491 }
Dan Sinclair2b11dc12015-10-22 15:02:06 -04001492 if (!pDestObj->IsArray())
1493 return nullptr;
1494
Ryan Harrison275e2602017-09-18 14:23:18 -04001495 ByteString utf16Name = wsName.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -07001496 int len = utf16Name.GetLength();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001497 if (!buffer) {
1498 *buflen = len;
thestig9067fd62016-11-23 14:10:06 -08001499 } else if (len <= *buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001500 memcpy(buffer, utf16Name.c_str(), len);
1501 *buflen = len;
1502 } else {
1503 *buflen = -1;
1504 }
1505 return (FPDF_DEST)pDestObj;
Bo Xu4d62b6b2015-01-10 22:52:59 -08001506}