John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdfview.h" |
| 8 | |
Lei Zhang | aa8bf7e | 2015-12-24 19:13:32 -0800 | [diff] [blame] | 9 | #include <memory> |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 10 | #include <utility> |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 11 | #include <vector> |
Lei Zhang | aa8bf7e | 2015-12-24 19:13:32 -0800 | [diff] [blame] | 12 | |
dsinclair | 39c62fd | 2016-09-29 12:49:17 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/cpdf_modulemgr.h" |
| 14 | #include "core/fpdfapi/cpdf_pagerendercontext.h" |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 15 | #include "core/fpdfapi/page/cpdf_image.h" |
| 16 | #include "core/fpdfapi/page/cpdf_imageobject.h" |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 17 | #include "core/fpdfapi/page/cpdf_page.h" |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 18 | #include "core/fpdfapi/page/cpdf_pageobject.h" |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 19 | #include "core/fpdfapi/page/cpdf_pathobject.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 20 | #include "core/fpdfapi/parser/cpdf_array.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 21 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 22 | #include "core/fpdfapi/parser/cpdf_document.h" |
| 23 | #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
dsinclair | 69d9c68 | 2016-10-04 12:18:35 -0700 | [diff] [blame] | 24 | #include "core/fpdfapi/render/cpdf_progressiverenderer.h" |
| 25 | #include "core/fpdfapi/render/cpdf_renderoptions.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 26 | #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" |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 30 | #include "core/fxcrt/fx_memory.h" |
| 31 | #include "core/fxcrt/fx_safe_types.h" |
Dan Sinclair | bcd1e70 | 2017-08-31 13:19:18 -0400 | [diff] [blame] | 32 | #include "core/fxcrt/fx_stream.h" |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 33 | #include "core/fxge/cfx_defaultrenderdevice.h" |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 34 | #include "core/fxge/cfx_gemodule.h" |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 35 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 36 | #include "fpdfsdk/cpdfsdk_pageview.h" |
| 37 | #include "fpdfsdk/fsdk_define.h" |
| 38 | #include "fpdfsdk/fsdk_pauseadapter.h" |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 39 | #include "fpdfsdk/javascript/ijs_runtime.h" |
rbpotter | e8468c4 | 2017-07-11 10:04:29 -0700 | [diff] [blame] | 40 | #include "public/fpdf_edit.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 41 | #include "public/fpdf_ext.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 42 | #include "public/fpdf_progressive.h" |
Chris Palmer | e4b035b | 2017-03-26 15:48:34 -0700 | [diff] [blame] | 43 | #include "third_party/base/allocator/partition_allocator/partition_alloc.h" |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 44 | #include "third_party/base/numerics/safe_conversions_impl.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 45 | #include "third_party/base/ptr_util.h" |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 46 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 47 | #ifdef PDF_ENABLE_XFA |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 48 | #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h" |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 49 | #include "fpdfsdk/fpdfxfa/cpdfxfa_page.h" |
| 50 | #include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h" |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 51 | #include "fxbarcode/BC_Library.h" |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 52 | #include "public/fpdf_formfill.h" |
| 53 | #endif // PDF_ENABLE_XFA |
| 54 | |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 55 | #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 56 | #include "core/fxge/cfx_windowsrenderdevice.h" |
rbpotter | e8468c4 | 2017-07-11 10:04:29 -0700 | [diff] [blame] | 57 | |
| 58 | // These checks are here because core/ and public/ cannot depend on each other. |
| 59 | static_assert(WindowsPrintMode::kModeEmf == FPDF_PRINTMODE_EMF, |
| 60 | "WindowsPrintMode::kModeEmf value mismatch"); |
| 61 | static_assert(WindowsPrintMode::kModeTextOnly == FPDF_PRINTMODE_TEXTONLY, |
| 62 | "WindowsPrintMode::kModeTextOnly value mismatch"); |
| 63 | static_assert(WindowsPrintMode::kModePostScript2 == FPDF_PRINTMODE_POSTSCRIPT2, |
| 64 | "WindowsPrintMode::kModePostScript2 value mismatch"); |
| 65 | static_assert(WindowsPrintMode::kModePostScript3 == FPDF_PRINTMODE_POSTSCRIPT3, |
| 66 | "WindowsPrintMode::kModePostScript3 value mismatch"); |
thestig | 25fa42f | 2016-05-25 21:39:46 -0700 | [diff] [blame] | 67 | #endif |
| 68 | |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 69 | namespace { |
| 70 | |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 71 | bool g_bLibraryInitialized = false; |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 72 | |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 73 | void 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 Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 83 | uint32_t option_flags = pContext->m_pOptions->GetFlags(); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 84 | if (flags & FPDF_LCD_TEXT) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 85 | option_flags |= RENDER_CLEARTYPE; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 86 | else |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 87 | option_flags &= ~RENDER_CLEARTYPE; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 88 | |
| 89 | if (flags & FPDF_NO_NATIVETEXT) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 90 | option_flags |= RENDER_NO_NATIVETEXT; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 91 | if (flags & FPDF_RENDER_LIMITEDIMAGECACHE) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 92 | option_flags |= RENDER_LIMITEDIMAGECACHE; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 93 | if (flags & FPDF_RENDER_FORCEHALFTONE) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 94 | option_flags |= RENDER_FORCE_HALFTONE; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 95 | #ifndef PDF_ENABLE_XFA |
| 96 | if (flags & FPDF_RENDER_NO_SMOOTHTEXT) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 97 | option_flags |= RENDER_NOTEXTSMOOTH; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 98 | if (flags & FPDF_RENDER_NO_SMOOTHIMAGE) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 99 | option_flags |= RENDER_NOIMAGESMOOTH; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 100 | if (flags & FPDF_RENDER_NO_SMOOTHPATH) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 101 | option_flags |= RENDER_NOPATHSMOOTH; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 102 | #endif // PDF_ENABLE_XFA |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 103 | pContext->m_pOptions->SetFlags(option_flags); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 104 | |
| 105 | // Grayscale output |
Dan Sinclair | f55e72e | 2017-07-13 14:53:28 -0400 | [diff] [blame] | 106 | if (flags & FPDF_GRAYSCALE) |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 107 | pContext->m_pOptions->SetColorMode(CPDF_RenderOptions::kGray); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 108 | |
| 109 | const CPDF_OCContext::UsageType usage = |
| 110 | (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View; |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 111 | pContext->m_pOptions->SetOCContext( |
| 112 | pdfium::MakeRetain<CPDF_OCContext>(pPage->m_pDocument.Get(), usage)); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 113 | |
| 114 | pContext->m_pDevice->SaveState(); |
| 115 | pContext->m_pDevice->SetClip_Rect(clipping_rect); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 116 | 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 | |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 134 | class CPDF_CustomAccess final : public IFX_SeekableReadStream { |
| 135 | public: |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 136 | template <typename T, typename... Args> |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 137 | friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 138 | |
| 139 | // IFX_SeekableReadStream |
| 140 | FX_FILESIZE GetSize() override; |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 141 | bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
| 142 | |
| 143 | private: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 144 | explicit CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess); |
| 145 | |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 146 | FPDF_FILEACCESS m_FileAccess; |
| 147 | }; |
| 148 | |
| 149 | CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) |
| 150 | : m_FileAccess(*pFileAccess) {} |
| 151 | |
| 152 | FX_FILESIZE CPDF_CustomAccess::GetSize() { |
| 153 | return m_FileAccess.m_FileLen; |
| 154 | } |
| 155 | |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 156 | bool 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 Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 169 | static_cast<uint8_t*>(buffer), size); |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 172 | #ifdef PDF_ENABLE_XFA |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 173 | class FPDF_FileHandlerContext : public IFX_SeekableStream { |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 174 | public: |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 175 | template <typename T, typename... Args> |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 176 | friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 177 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 178 | ~FPDF_FileHandlerContext() override; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 179 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 180 | // IFX_SeekableStream: |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 181 | 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 Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 188 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 189 | void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; } |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 190 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 191 | protected: |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 192 | explicit FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS); |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 193 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 194 | FPDF_FILEHANDLER* m_pFS; |
| 195 | FX_FILESIZE m_nCurPos; |
| 196 | }; |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 197 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 198 | FPDF_FileHandlerContext::FPDF_FileHandlerContext(FPDF_FILEHANDLER* pFS) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | m_pFS = pFS; |
| 200 | m_nCurPos = 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 203 | FPDF_FileHandlerContext::~FPDF_FileHandlerContext() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 204 | if (m_pFS && m_pFS->Release) |
| 205 | m_pFS->Release(m_pFS->clientData); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 208 | FX_FILESIZE FPDF_FileHandlerContext::GetSize() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | if (m_pFS && m_pFS->GetSize) |
| 210 | return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData); |
| 211 | return 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 214 | bool FPDF_FileHandlerContext::IsEOF() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 215 | return m_nCurPos >= GetSize(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 218 | FX_FILESIZE FPDF_FileHandlerContext::GetPosition() { |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 219 | return m_nCurPos; |
| 220 | } |
| 221 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 222 | bool FPDF_FileHandlerContext::ReadBlock(void* buffer, |
| 223 | FX_FILESIZE offset, |
| 224 | size_t size) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 225 | if (!buffer || !size || !m_pFS->ReadBlock) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 226 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 227 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 228 | if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, |
| 229 | (FPDF_DWORD)size) == 0) { |
| 230 | m_nCurPos = offset + size; |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 231 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 232 | } |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 233 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 236 | size_t FPDF_FileHandlerContext::ReadBlock(void* buffer, size_t size) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 237 | if (!buffer || !size || !m_pFS->ReadBlock) |
| 238 | return 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 239 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | 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 Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 245 | size = static_cast<size_t>(dwAvail); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 246 | 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 Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 251 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 252 | return 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 255 | bool FPDF_FileHandlerContext::WriteBlock(const void* buffer, |
| 256 | FX_FILESIZE offset, |
| 257 | size_t size) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | if (!m_pFS || !m_pFS->WriteBlock) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 259 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 260 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, |
| 262 | (FPDF_DWORD)size) == 0) { |
| 263 | m_nCurPos = offset + size; |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 264 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | } |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 266 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 269 | bool FPDF_FileHandlerContext::Flush() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 270 | if (!m_pFS || !m_pFS->Flush) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 271 | return true; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 272 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 273 | return m_pFS->Flush(m_pFS->clientData) == 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 274 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 275 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 276 | |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 277 | FPDF_DOCUMENT LoadDocumentImpl( |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 278 | const RetainPtr<IFX_SeekableReadStream>& pFileAccess, |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 279 | FPDF_BYTESTRING password) { |
Jane Liu | 8a1081f | 2017-08-22 16:28:05 -0400 | [diff] [blame] | 280 | if (!pFileAccess) { |
| 281 | ProcessParseError(CPDF_Parser::FILE_ERROR); |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 282 | return nullptr; |
Jane Liu | 8a1081f | 2017-08-22 16:28:05 -0400 | [diff] [blame] | 283 | } |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 284 | |
| 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 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 299 | } // namespace |
| 300 | |
| 301 | UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) { |
| 302 | return static_cast<UnderlyingDocumentType*>(doc); |
| 303 | } |
| 304 | |
| 305 | FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) { |
| 306 | return static_cast<FPDF_DOCUMENT>(doc); |
| 307 | } |
| 308 | |
| 309 | UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) { |
| 310 | return static_cast<UnderlyingPageType*>(page); |
| 311 | } |
| 312 | |
| 313 | CPDF_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 | |
| 321 | FPDF_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 | |
| 331 | CPDF_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 Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 339 | CPDF_PathObject* CPDFPathObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) { |
Lei Zhang | 038740c | 2017-09-13 13:22:54 -0700 | [diff] [blame] | 340 | auto* obj = CPDFPageObjectFromFPDFPageObject(page_object); |
| 341 | return obj ? obj->AsPath() : nullptr; |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 344 | CPDF_PageObject* CPDFPageObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) { |
| 345 | return static_cast<CPDF_PageObject*>(page_object); |
| 346 | } |
| 347 | |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 348 | CPDF_Object* CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment) { |
| 349 | return static_cast<CPDF_Object*>(attachment); |
| 350 | } |
| 351 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 352 | ByteString CFXByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string) { |
| 353 | return WideString::FromUTF16LE(wide_string, |
| 354 | WideString::WStringLength(wide_string)) |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 355 | .UTF8Encode(); |
| 356 | } |
| 357 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 358 | CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) { |
| 359 | return static_cast<CFX_DIBitmap*>(bitmap); |
| 360 | } |
| 361 | |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 362 | const FX_PATHPOINT* FXPathPointFromFPDFPathSegment(FPDF_PATHSEGMENT segment) { |
| 363 | return static_cast<const FX_PATHPOINT*>(segment); |
| 364 | } |
| 365 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 366 | unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text, |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 367 | void* buffer, |
| 368 | unsigned long buflen) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 369 | ByteString encoded_text = text.UTF16LE_Encode(); |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 370 | unsigned long len = encoded_text.GetLength(); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 371 | if (buffer && len <= buflen) |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 372 | memcpy(buffer, encoded_text.c_str(), len); |
| 373 | return len; |
| 374 | } |
| 375 | |
| 376 | unsigned 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 Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 388 | ByteString dummy_last_decoder; |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 389 | 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 Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 411 | return len; |
| 412 | } |
| 413 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 414 | RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream( |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 415 | FPDF_FILEACCESS* pFileAccess) { |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 416 | return pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 417 | } |
| 418 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 419 | #ifdef PDF_ENABLE_XFA |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 420 | RetainPtr<IFX_SeekableStream> MakeSeekableStream( |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 421 | FPDF_FILEHANDLER* pFilehandler) { |
Dan Sinclair | a7caeb5 | 2017-09-26 09:23:29 -0400 | [diff] [blame] | 422 | return pdfium::MakeRetain<FPDF_FileHandlerContext>(pFilehandler); |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 423 | } |
| 424 | #endif // PDF_ENABLE_XFA |
| 425 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 426 | // 0 bit: FPDF_POLICY_MACHINETIME_ACCESS |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 427 | static uint32_t foxit_sandbox_policy = 0xFFFFFFFF; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 428 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 429 | void 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 442 | FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) { |
| 443 | switch (policy) { |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 444 | case FPDF_POLICY_MACHINETIME_ACCESS: |
Lei Zhang | b0748bb | 2015-10-19 12:11:49 -0700 | [diff] [blame] | 445 | return !!(foxit_sandbox_policy & 0x01); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 446 | default: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 447 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 448 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 451 | FPDF_EXPORT void FPDF_CALLCONV FPDF_InitLibrary() { |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 452 | FPDF_InitLibraryWithConfig(nullptr); |
| 453 | } |
| 454 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 455 | FPDF_EXPORT void FPDF_CALLCONV |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 456 | FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) { |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 457 | if (g_bLibraryInitialized) |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 458 | return; |
| 459 | |
Dan Sinclair | dbc3d3e | 2017-05-11 13:41:38 -0400 | [diff] [blame] | 460 | FXMEM_InitializePartitionAlloc(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 461 | |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 462 | CFX_GEModule* pModule = CFX_GEModule::Get(); |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 463 | pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr); |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 464 | |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 465 | CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get(); |
Lei Zhang | 76020fc | 2017-05-18 15:51:20 -0700 | [diff] [blame] | 466 | pModuleMgr->Init(); |
Tom Sepez | b4a2618 | 2017-03-01 12:15:00 -0800 | [diff] [blame] | 467 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 468 | #ifdef PDF_ENABLE_XFA |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 469 | FXJSE_Initialize(); |
| 470 | BC_Library_Init(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 471 | #endif // PDF_ENABLE_XFA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 472 | if (cfg && cfg->version >= 2) |
| 473 | IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate); |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 474 | |
| 475 | g_bLibraryInitialized = true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 478 | FPDF_EXPORT void FPDF_CALLCONV FPDF_DestroyLibrary() { |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 479 | if (!g_bLibraryInitialized) |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 480 | return; |
| 481 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 482 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 5628fd7 | 2017-04-27 14:58:53 -0700 | [diff] [blame] | 483 | BC_Library_Destroy(); |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 484 | FXJSE_Finalize(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 485 | #endif // PDF_ENABLE_XFA |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 486 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 487 | CPDF_ModuleMgr::Destroy(); |
| 488 | CFX_GEModule::Destroy(); |
Tom Sepez | 2c28619 | 2015-06-18 12:47:11 -0700 | [diff] [blame] | 489 | |
thestig | 2d6dda1 | 2016-06-28 07:39:09 -0700 | [diff] [blame] | 490 | IJS_Runtime::Destroy(); |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 491 | |
| 492 | g_bLibraryInitialized = false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | #ifndef _WIN32 |
| 496 | int g_LastError; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 497 | void SetLastError(int err) { |
| 498 | g_LastError = err; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 501 | int GetLastError() { |
| 502 | return g_LastError; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 503 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 504 | #endif // _WIN32 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 505 | |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 506 | void ProcessParseError(CPDF_Parser::Error err) { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 507 | uint32_t err_code = FPDF_ERR_SUCCESS; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 508 | // Translate FPDFAPI error code to FPDFVIEW error code |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 509 | switch (err) { |
| 510 | case CPDF_Parser::SUCCESS: |
| 511 | err_code = FPDF_ERR_SUCCESS; |
| 512 | break; |
| 513 | case CPDF_Parser::FILE_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 514 | err_code = FPDF_ERR_FILE; |
| 515 | break; |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 516 | case CPDF_Parser::FORMAT_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 517 | err_code = FPDF_ERR_FORMAT; |
| 518 | break; |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 519 | case CPDF_Parser::PASSWORD_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 520 | err_code = FPDF_ERR_PASSWORD; |
| 521 | break; |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 522 | case CPDF_Parser::HANDLER_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 523 | err_code = FPDF_ERR_SECURITY; |
| 524 | break; |
| 525 | } |
| 526 | SetLastError(err_code); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 529 | FPDF_EXPORT void FPDF_CALLCONV FPDF_SetSandBoxPolicy(FPDF_DWORD policy, |
| 530 | FPDF_BOOL enable) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 531 | return FSDK_SetSandBoxPolicy(policy, enable); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 532 | } |
| 533 | |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 534 | #if defined(_WIN32) |
| 535 | #if defined(PDFIUM_PRINT_TEXT_WITH_GDI) |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 536 | FPDF_EXPORT void FPDF_CALLCONV |
thestig | fdb35ff | 2016-07-18 13:45:44 -0700 | [diff] [blame] | 537 | FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) { |
| 538 | g_pdfium_typeface_accessible_func = func; |
| 539 | } |
| 540 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 541 | FPDF_EXPORT void FPDF_CALLCONV FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) { |
thestig | fdb35ff | 2016-07-18 13:45:44 -0700 | [diff] [blame] | 542 | g_pdfium_print_text_with_gdi = !!use_gdi; |
| 543 | } |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 544 | #endif // PDFIUM_PRINT_TEXT_WITH_GDI |
| 545 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 546 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 547 | FPDF_SetPrintPostscriptLevel(int postscript_level) { |
rbpotter | 38a01b9 | 2017-07-13 11:58:01 -0700 | [diff] [blame] | 548 | return postscript_level != 1 && FPDF_SetPrintMode(postscript_level); |
| 549 | } |
| 550 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 551 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_SetPrintMode(int mode) { |
rbpotter | e8468c4 | 2017-07-11 10:04:29 -0700 | [diff] [blame] | 552 | if (mode < FPDF_PRINTMODE_EMF || mode > FPDF_PRINTMODE_POSTSCRIPT3) |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 553 | return FALSE; |
rbpotter | e8468c4 | 2017-07-11 10:04:29 -0700 | [diff] [blame] | 554 | g_pdfium_print_mode = mode; |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 555 | return TRUE; |
| 556 | } |
| 557 | #endif // defined(_WIN32) |
thestig | fdb35ff | 2016-07-18 13:45:44 -0700 | [diff] [blame] | 558 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 559 | FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV |
| 560 | FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BYTESTRING password) { |
Tom Sepez | e3166a8 | 2015-08-05 10:50:32 -0700 | [diff] [blame] | 561 | // NOTE: the creation of the file needs to be by the embedder on the |
| 562 | // other side of this API. |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 563 | return LoadDocumentImpl( |
| 564 | IFX_SeekableReadStream::CreateFromFilename((const char*)file_path), |
| 565 | password); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 566 | } |
Jun Fang | e118ce9 | 2015-02-17 06:50:08 -0800 | [diff] [blame] | 567 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 568 | #ifdef PDF_ENABLE_XFA |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 569 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_HasXFAField(FPDF_DOCUMENT document, |
| 570 | int* docType) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 571 | if (!document) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 572 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 573 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 574 | const CPDF_Document* pDoc = |
| 575 | static_cast<CPDFXFA_Context*>(document)->GetPDFDoc(); |
| 576 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 577 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 578 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 579 | const CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 580 | if (!pRoot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 581 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 582 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 583 | CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 584 | if (!pAcroForm) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 585 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 586 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 587 | CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 588 | if (!pXFA) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 589 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 590 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 591 | bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false); |
thestig | ded3634 | 2016-05-23 17:54:02 -0700 | [diff] [blame] | 592 | *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 593 | return true; |
Jun Fang | e118ce9 | 2015-02-17 06:50:08 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 596 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_LoadXFA(FPDF_DOCUMENT document) { |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 597 | return document && static_cast<CPDFXFA_Context*>(document)->LoadXFADoc(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 598 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 599 | #endif // PDF_ENABLE_XFA |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 600 | |
tsepez | ad2441e | 2016-10-24 10:19:11 -0700 | [diff] [blame] | 601 | class CMemFile final : public IFX_SeekableReadStream { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 602 | public: |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 603 | static RetainPtr<CMemFile> Create(uint8_t* pBuf, FX_FILESIZE size) { |
| 604 | return RetainPtr<CMemFile>(new CMemFile(pBuf, size)); |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 605 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 606 | |
Lei Zhang | 3884dba | 2015-10-19 17:27:53 -0700 | [diff] [blame] | 607 | FX_FILESIZE GetSize() override { return m_size; } |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 608 | bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 609 | if (offset < 0) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 610 | return false; |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 611 | |
tsepez | 4e597c8 | 2016-11-07 15:16:01 -0800 | [diff] [blame] | 612 | FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 613 | newPos += offset; |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 614 | if (!newPos.IsValid() || newPos.ValueOrDie() > m_size) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 615 | return false; |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 616 | |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 617 | memcpy(buffer, m_pBuf + offset, size); |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 618 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 619 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 620 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 621 | private: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 622 | CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {} |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 623 | |
Lei Zhang | 3884dba | 2015-10-19 17:27:53 -0700 | [diff] [blame] | 624 | uint8_t* const m_pBuf; |
| 625 | const FX_FILESIZE m_size; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 626 | }; |
Lei Zhang | 3884dba | 2015-10-19 17:27:53 -0700 | [diff] [blame] | 627 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 628 | FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV |
| 629 | FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password) { |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 630 | return LoadDocumentImpl(CMemFile::Create((uint8_t*)data_buf, size), password); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 633 | FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 634 | FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess, |
| 635 | FPDF_BYTESTRING password) { |
Artem Strygin | 3f7ff05 | 2017-07-27 22:16:13 +0300 | [diff] [blame] | 636 | return LoadDocumentImpl(pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess), |
| 637 | password); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 640 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_GetFileVersion(FPDF_DOCUMENT doc, |
| 641 | int* fileVersion) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 642 | if (!fileVersion) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 643 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 644 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 645 | *fileVersion = 0; |
| 646 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc); |
| 647 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 648 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 649 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 650 | const CPDF_Parser* pParser = pDoc->GetParser(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 651 | if (!pParser) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 652 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 653 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 654 | *fileVersion = pParser->GetFileVersion(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 655 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 656 | } |
| 657 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 658 | // jabdelmalek: changed return type from uint32_t to build on Linux (and match |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 659 | // header). |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 660 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 661 | FPDF_GetDocPermissions(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 662 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 663 | // https://bugs.chromium.org/p/pdfium/issues/detail?id=499 |
| 664 | if (!pDoc) { |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 665 | #ifndef PDF_ENABLE_XFA |
| 666 | return 0; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 667 | #else // PDF_ENABLE_XFA |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 668 | return 0xFFFFFFFF; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 669 | #endif // PDF_ENABLE_XFA |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 670 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 671 | |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 672 | return pDoc->GetUserPermissions(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 675 | FPDF_EXPORT int FPDF_CALLCONV |
| 676 | FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 677 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 678 | if (!pDoc || !pDoc->GetParser()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 679 | return -1; |
Bo Xu | c5cab02 | 2014-09-19 19:16:31 -0700 | [diff] [blame] | 680 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 681 | CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 682 | return pDict ? pDict->GetIntegerFor("R") : -1; |
Bo Xu | c5cab02 | 2014-09-19 19:16:31 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 685 | FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageCount(FPDF_DOCUMENT document) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 686 | UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document); |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 687 | return pDoc ? pDoc->GetPageCount() : 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 690 | FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDF_LoadPage(FPDF_DOCUMENT document, |
| 691 | int page_index) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 692 | UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document); |
| 693 | if (!pDoc) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 694 | return nullptr; |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 695 | |
Tom Sepez | bbe0e4d | 2015-10-20 15:41:40 -0700 | [diff] [blame] | 696 | if (page_index < 0 || page_index >= pDoc->GetPageCount()) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 697 | return nullptr; |
| 698 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 699 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 9792f16 | 2017-05-16 14:11:30 -0700 | [diff] [blame] | 700 | return pDoc->GetXFAPage(page_index).Leak(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 701 | #else // PDF_ENABLE_XFA |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 702 | CPDF_Dictionary* pDict = pDoc->GetPage(page_index); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 703 | if (!pDict) |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 704 | return nullptr; |
| 705 | |
| 706 | CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true); |
| 707 | pPage->ParseContent(); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 708 | return pPage; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 709 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 712 | FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageWidth(FPDF_PAGE page) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 713 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 714 | return pPage ? pPage->GetPageWidth() : 0.0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 717 | FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageHeight(FPDF_PAGE page) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 718 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 719 | return pPage ? pPage->GetPageHeight() : 0.0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 720 | } |
| 721 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 722 | #if defined(_WIN32) |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 723 | namespace { |
| 724 | |
| 725 | const double kEpsilonSize = 0.01f; |
| 726 | |
| 727 | void 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 | |
| 750 | FX_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 |
rbpotter | ce8e51e | 2017-04-28 12:42:47 -0700 | [diff] [blame] | 772 | int page_rotation = pPage->GetPageRotation(); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 773 | |
| 774 | // Compute offsets |
| 775 | int offset_x = 0; |
| 776 | int offset_y = 0; |
rbpotter | 8d7672e | 2017-08-16 16:45:44 -0700 | [diff] [blame] | 777 | if (size_x > size_y) |
| 778 | std::swap(size_x_bm, size_y_bm); |
| 779 | |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 780 | 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 Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 804 | RetainPtr<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) { |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 813 | 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 Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 820 | RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>(); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 821 | 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 | |
| 828 | void RenderBitmap(CFX_RenderDevice* device, |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 829 | const RetainPtr<CFX_DIBitmap>& pSrc, |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 830 | 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 Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 837 | RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>(); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 838 | 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) { |
rbpotter | 8d7672e | 2017-08-16 16:45:44 -0700 | [diff] [blame] | 844 | device->StretchDIBits(pDst, mask_area.left, mask_area.top, size_x_bm, |
| 845 | size_y_bm); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 846 | } else { |
| 847 | device->SetDIBits(pDst, mask_area.left, mask_area.top); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | } // namespace |
| 852 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 853 | FPDF_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 Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 861 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 862 | if (!pPage) |
| 863 | return; |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 864 | pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>()); |
| 865 | CPDF_PageRenderContext* pContext = pPage->GetRenderContext(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 866 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 867 | RetainPtr<CFX_DIBitmap> pBitmap; |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 868 | // 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 |
rbpotter | 8d7672e | 2017-08-16 16:45:44 -0700 | [diff] [blame] | 872 | // size. |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 873 | const bool bNewBitmap = |
rbpotter | 8d7672e | 2017-08-16 16:45:44 -0700 | [diff] [blame] | 874 | pPage->BackgroundAlphaNeeded() || |
| 875 | (pPage->HasImageMask() && pPage->GetMaskBoundingBoxes().size() > 100); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 876 | const bool bHasMask = pPage->HasImageMask() && !bNewBitmap; |
| 877 | if (bNewBitmap || bHasMask) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 878 | pBitmap = pdfium::MakeRetain<CFX_DIBitmap>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 879 | pBitmap->Create(size_x, size_y, FXDIB_Argb); |
| 880 | pBitmap->Clear(0x00ffffff); |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 881 | CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice; |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 882 | pContext->m_pDevice = pdfium::WrapUnique(pDevice); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 883 | pDevice->Attach(pBitmap, false, nullptr, false); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 884 | if (bHasMask) { |
| 885 | pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>(); |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 886 | uint32_t option_flags = pContext->m_pOptions->GetFlags(); |
| 887 | option_flags |= RENDER_BREAKFORMASKS; |
| 888 | pContext->m_pOptions->SetFlags(option_flags); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 889 | } |
Jun Fang | 1aeeceb | 2015-12-29 10:27:44 +0800 | [diff] [blame] | 890 | } else { |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 891 | pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc); |
Jun Fang | 1aeeceb | 2015-12-29 10:27:44 +0800 | [diff] [blame] | 892 | } |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 893 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 894 | FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 895 | rotate, flags, true, nullptr); |
Bo Xu | d4e406e | 2014-08-13 11:03:19 -0700 | [diff] [blame] | 896 | |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 897 | 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 Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 903 | std::vector<RetainPtr<CFX_DIBitmap>> bitmaps(mask_boxes.size()); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 904 | 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 Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 918 | pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 919 | pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>(); |
Dan Sinclair | 921fe6b | 2017-10-05 11:14:12 -0400 | [diff] [blame] | 920 | |
| 921 | uint32_t option_flags = pContext->m_pOptions->GetFlags(); |
| 922 | option_flags |= RENDER_BREAKFORMASKS; |
| 923 | pContext->m_pOptions->SetFlags(option_flags); |
| 924 | |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 925 | 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 Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 938 | CFX_WindowsRenderDevice WinDC(dc); |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 939 | if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 940 | auto pDst = pdfium::MakeRetain<CFX_DIBitmap>(); |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 941 | int pitch = pBitmap->GetPitch(); |
| 942 | pDst->Create(size_x, size_y, FXDIB_Rgb32); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 943 | memset(pDst->GetBuffer(), -1, pitch * size_y); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 944 | pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0, |
Nicolas Pena | 6a5c20c | 2017-04-07 14:12:31 -0400 | [diff] [blame] | 945 | FXDIB_BLEND_NORMAL, nullptr, false); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 946 | WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y); |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 947 | } else { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 948 | WinDC.SetDIBits(pBitmap, 0, 0); |
Lei Zhang | 6d8b1c2 | 2015-06-19 17:26:17 -0700 | [diff] [blame] | 949 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 950 | } |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 951 | |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 952 | pPage->SetRenderContext(nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 953 | } |
Lei Zhang | ae85f87 | 2016-02-19 14:57:07 -0800 | [diff] [blame] | 954 | #endif // defined(_WIN32) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 955 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 956 | FPDF_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 Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 964 | if (!bitmap) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 965 | return; |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 966 | |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 967 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 968 | if (!pPage) |
| 969 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 970 | |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 971 | CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext; |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 972 | pPage->SetRenderContext(pdfium::WrapUnique(pContext)); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 973 | |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 974 | CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice; |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 975 | pContext->m_pDevice.reset(pDevice); |
dsinclair | 31b08d4 | 2017-03-28 15:47:47 +0000 | [diff] [blame] | 976 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 977 | RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap)); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 978 | pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 979 | FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 980 | rotate, flags, true, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 981 | |
caryclark | 687fbde | 2016-11-22 12:44:25 -0800 | [diff] [blame] | 982 | #ifdef _SKIA_SUPPORT_PATHS_ |
Cary Clark | 364d18b | 2017-07-25 10:39:10 -0400 | [diff] [blame] | 983 | pDevice->Flush(true); |
caryclark | 687fbde | 2016-11-22 12:44:25 -0800 | [diff] [blame] | 984 | pBitmap->UnPreMultiply(); |
| 985 | #endif |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 986 | pPage->SetRenderContext(nullptr); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 989 | FPDF_EXPORT void FPDF_CALLCONV |
| 990 | FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap, |
| 991 | FPDF_PAGE page, |
| 992 | const FS_MATRIX* matrix, |
| 993 | const FS_RECTF* clipping, |
| 994 | int flags) { |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 995 | 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 Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1004 | |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 1005 | CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 1006 | pContext->m_pDevice.reset(pDevice); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1007 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 1008 | RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap)); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 1009 | pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false); |
| 1010 | |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 1011 | 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 Pena | 60bde10 | 2017-07-26 13:50:12 -0400 | [diff] [blame] | 1018 | FX_RECT clip_rect = clipping_rect.ToFxRect(); |
Nicolas Pena | 24b0733 | 2017-09-13 18:02:11 -0400 | [diff] [blame] | 1019 | 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); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 1026 | |
| 1027 | pPage->SetRenderContext(nullptr); |
| 1028 | } |
| 1029 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1030 | #ifdef _SKIA_SUPPORT_ |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1031 | FPDF_EXPORT FPDF_RECORDER FPDF_CALLCONV FPDF_RenderPageSkp(FPDF_PAGE page, |
| 1032 | int size_x, |
| 1033 | int size_y) { |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1034 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 1035 | if (!pPage) |
| 1036 | return nullptr; |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1037 | |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 1038 | CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext; |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 1039 | pPage->SetRenderContext(pdfium::WrapUnique(pContext)); |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 1040 | CFX_DefaultRenderDevice* skDevice = new CFX_DefaultRenderDevice; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1041 | FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y); |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 1042 | pContext->m_pDevice.reset(skDevice); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1043 | FPDF_RenderPage_Retail(pContext, page, 0, 0, size_x, size_y, 0, 0, true, |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1044 | nullptr); |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 1045 | pPage->SetRenderContext(nullptr); |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1046 | return recorder; |
| 1047 | } |
| 1048 | #endif |
| 1049 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1050 | FPDF_EXPORT void FPDF_CALLCONV FPDF_ClosePage(FPDF_PAGE page) { |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1051 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1052 | if (!page) |
| 1053 | return; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1054 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 9792f16 | 2017-05-16 14:11:30 -0700 | [diff] [blame] | 1055 | // Take it back across the API and throw it away. |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 1056 | RetainPtr<CPDFXFA_Page>().Unleak(pPage); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1057 | #else // PDF_ENABLE_XFA |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1058 | CPDFSDK_PageView* pPageView = |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1059 | static_cast<CPDFSDK_PageView*>(pPage->GetView()); |
dsinclair | 1df1efa | 2016-09-07 09:55:37 -0700 | [diff] [blame] | 1060 | if (pPageView) { |
dsinclair | bcf4623 | 2016-10-03 13:02:27 -0700 | [diff] [blame] | 1061 | // We're already destroying the pageview, so bail early. |
| 1062 | if (pPageView->IsBeingDestroyed()) |
| 1063 | return; |
| 1064 | |
dsinclair | 1df1efa | 2016-09-07 09:55:37 -0700 | [diff] [blame] | 1065 | 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. |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 1074 | pPageView->GetFormFillEnv()->RemovePageView(pPage); |
dsinclair | 1df1efa | 2016-09-07 09:55:37 -0700 | [diff] [blame] | 1075 | // If the page was owned then the pageview will have deleted the page. |
| 1076 | if (owned) |
| 1077 | return; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1078 | } |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1079 | delete pPage; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1080 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1083 | FPDF_EXPORT void FPDF_CALLCONV FPDF_CloseDocument(FPDF_DOCUMENT document) { |
Jun Fang | fc75136 | 2015-12-16 21:23:39 -0800 | [diff] [blame] | 1084 | delete UnderlyingFromFPDFDocument(document); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1087 | FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetLastError() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1088 | return GetLastError(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1091 | FPDF_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 Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 1101 | if (!page || !page_x || !page_y) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1102 | return; |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 1103 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1104 | #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 Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1108 | CFX_Matrix page2device = |
| 1109 | pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate); |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1110 | |
Nicolas Pena | b21f174 | 2017-06-29 12:02:06 -0400 | [diff] [blame] | 1111 | CFX_PointF pos = page2device.GetInverse().Transform( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 1112 | CFX_PointF(static_cast<float>(device_x), static_cast<float>(device_y))); |
Dan Sinclair | afb4456 | 2017-02-09 13:07:43 -0500 | [diff] [blame] | 1113 | |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 1114 | *page_x = pos.x; |
| 1115 | *page_y = pos.y; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1116 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1119 | FPDF_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 Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 1129 | if (!device_x || !device_y) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1130 | return; |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 1131 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 1132 | if (!pPage) |
| 1133 | return; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1134 | #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 Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1138 | CFX_Matrix page2device = |
| 1139 | pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate); |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 1140 | CFX_PointF pos = page2device.Transform( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 1141 | CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y))); |
Dan Sinclair | afb4456 | 2017-02-09 13:07:43 -0500 | [diff] [blame] | 1142 | |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 1143 | *device_x = FXSYS_round(pos.x); |
| 1144 | *device_y = FXSYS_round(pos.y); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1145 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1148 | FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_Create(int width, |
| 1149 | int height, |
| 1150 | int alpha) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1151 | auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>(); |
tsepez | 37b12ad | 2016-12-14 19:50:23 -0800 | [diff] [blame] | 1152 | if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1153 | return nullptr; |
tsepez | 37b12ad | 2016-12-14 19:50:23 -0800 | [diff] [blame] | 1154 | |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1155 | return pBitmap.Leak(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1158 | FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_CreateEx(int width, |
| 1159 | int height, |
| 1160 | int format, |
| 1161 | void* first_scan, |
| 1162 | int stride) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1163 | 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: |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1178 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1179 | } |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1180 | auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1181 | pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1182 | return pBitmap.Leak(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1185 | FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetFormat(FPDF_BITMAP bitmap) { |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 1186 | 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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1205 | FPDF_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 Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 1211 | if (!bitmap) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1212 | return; |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1213 | |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 1214 | CFX_DefaultRenderDevice device; |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 1215 | RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap)); |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1216 | device.Attach(pBitmap, false, nullptr, false); |
| 1217 | if (!pBitmap->HasAlpha()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1218 | color |= 0xFF000000; |
| 1219 | FX_RECT rect(left, top, left + width, top + height); |
| 1220 | device.FillRect(&rect, color); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1223 | FPDF_EXPORT void* FPDF_CALLCONV FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1224 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1227 | FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1228 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1231 | FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1232 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1235 | FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetStride(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1236 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1239 | FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_Destroy(FPDF_BITMAP bitmap) { |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 1240 | RetainPtr<CFX_DIBitmap> destroyer; |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1241 | destroyer.Unleak(CFXBitmapFromFPDFBitmap(bitmap)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 1244 | void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1245 | 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, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1252 | bool bNeedToRestore, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1253 | IFSDK_PAUSE_Adapter* pause) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 1254 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 1255 | if (!pPage) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1256 | return; |
| 1257 | |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1258 | 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1264 | FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document, |
| 1265 | int page_index, |
| 1266 | double* width, |
| 1267 | double* height) { |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1268 | UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document); |
| 1269 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1270 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1271 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1272 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1273 | int count = pDoc->GetPageCount(); |
| 1274 | if (page_index < 0 || page_index >= count) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1275 | return false; |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 1276 | RetainPtr<CPDFXFA_Page> pPage = pDoc->GetXFAPage(page_index); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1277 | if (!pPage) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1278 | return false; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1279 | *width = pPage->GetPageWidth(); |
| 1280 | *height = pPage->GetPageHeight(); |
| 1281 | #else // PDF_ENABLE_XFA |
| 1282 | CPDF_Dictionary* pDict = pDoc->GetPage(page_index); |
| 1283 | if (!pDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1284 | return false; |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 1285 | |
| 1286 | CPDF_Page page(pDoc, pDict, true); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1287 | *width = page.GetPageWidth(); |
| 1288 | *height = page.GetPageHeight(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1289 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1290 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1291 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1294 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1295 | FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1296 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1297 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1298 | return true; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1299 | CPDF_ViewerPreferences viewRef(pDoc); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1300 | return viewRef.PrintScaling(); |
| 1301 | } |
| 1302 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1303 | FPDF_EXPORT int FPDF_CALLCONV |
| 1304 | FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1305 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1306 | if (!pDoc) |
| 1307 | return 1; |
| 1308 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1309 | return viewRef.NumCopies(); |
| 1310 | } |
| 1311 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1312 | FPDF_EXPORT FPDF_PAGERANGE FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1313 | FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1314 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1315 | if (!pDoc) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1316 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1317 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1318 | return viewRef.PrintPageRange(); |
| 1319 | } |
| 1320 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1321 | FPDF_EXPORT FPDF_DUPLEXTYPE FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1322 | FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1323 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1324 | if (!pDoc) |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1325 | return DuplexUndefined; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1326 | CPDF_ViewerPreferences viewRef(pDoc); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1327 | ByteString duplex = viewRef.Duplex(); |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1328 | if ("Simplex" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1329 | return Simplex; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1330 | if ("DuplexFlipShortEdge" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1331 | return DuplexFlipShortEdge; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1332 | if ("DuplexFlipLongEdge" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1333 | return DuplexFlipLongEdge; |
| 1334 | return DuplexUndefined; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1337 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 1338 | FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document, |
| 1339 | FPDF_BYTESTRING key, |
| 1340 | char* buffer, |
| 1341 | unsigned long length) { |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 1342 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1343 | if (!pDoc) |
| 1344 | return 0; |
| 1345 | |
| 1346 | CPDF_ViewerPreferences viewRef(pDoc); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1347 | ByteString bsVal; |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 1348 | 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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1357 | FPDF_EXPORT FPDF_DWORD FPDF_CALLCONV |
| 1358 | FPDF_CountNamedDests(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1359 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1360 | if (!pDoc) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1361 | return 0; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1362 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 1363 | const CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1364 | if (!pRoot) |
| 1365 | return 0; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1366 | |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1367 | CPDF_NameTree nameTree(pDoc, "Dests"); |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1368 | pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1369 | CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1370 | if (pDest) |
| 1371 | count += pDest->GetCount(); |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1372 | |
| 1373 | if (!count.IsValid()) |
| 1374 | return 0; |
| 1375 | |
| 1376 | return count.ValueOrDie(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1379 | FPDF_EXPORT FPDF_DEST FPDF_CALLCONV |
| 1380 | FPDF_GetNamedDestByName(FPDF_DOCUMENT document, FPDF_BYTESTRING name) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1381 | if (!name || name[0] == 0) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1382 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1383 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1384 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1385 | if (!pDoc) |
| 1386 | return nullptr; |
| 1387 | |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1388 | CPDF_NameTree name_tree(pDoc, "Dests"); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1389 | return name_tree.LookupNamedDest(pDoc, PDF_DecodeText(ByteString(name))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1392 | #ifdef PDF_ENABLE_XFA |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1393 | FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Init(FPDF_BSTR* str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1394 | if (!str) |
| 1395 | return -1; |
| 1396 | |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 1397 | memset(str, 0, sizeof(FPDF_BSTR)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1398 | return 0; |
| 1399 | } |
| 1400 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1401 | FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Set(FPDF_BSTR* str, |
| 1402 | FPDF_LPCSTR bstr, |
| 1403 | int length) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1404 | if (!str) |
| 1405 | return -1; |
| 1406 | if (!bstr || !length) |
| 1407 | return -1; |
| 1408 | if (length == -1) |
Ryan Harrison | 8b1408e | 2017-09-27 11:07:51 -0400 | [diff] [blame] | 1409 | length = strlen(bstr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1410 | |
| 1411 | if (length == 0) { |
| 1412 | if (str->str) { |
| 1413 | FX_Free(str->str); |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1414 | str->str = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1415 | } |
| 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 Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 1426 | memcpy(str->str, bstr, length); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1427 | str->len = length; |
| 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1432 | FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Clear(FPDF_BSTR* str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1433 | if (!str) |
| 1434 | return -1; |
| 1435 | |
| 1436 | if (str->str) { |
| 1437 | FX_Free(str->str); |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1438 | str->str = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1439 | } |
| 1440 | str->len = 0; |
| 1441 | return 0; |
| 1442 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1443 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1444 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 1445 | FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDF_GetNamedDest(FPDF_DOCUMENT document, |
| 1446 | int index, |
| 1447 | void* buffer, |
| 1448 | long* buflen) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1449 | if (!buffer) |
| 1450 | *buflen = 0; |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1451 | |
| 1452 | if (index < 0) |
| 1453 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1454 | |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 1455 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1456 | if (!pDoc) |
| 1457 | return nullptr; |
| 1458 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 1459 | const CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1460 | if (!pRoot) |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1461 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1462 | |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1463 | CPDF_Object* pDestObj = nullptr; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1464 | WideString wsName; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1465 | CPDF_NameTree nameTree(pDoc, "Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1466 | int count = nameTree.GetCount(); |
| 1467 | if (index >= count) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1468 | CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1469 | if (!pDest) |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1470 | 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1477 | index -= count; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1478 | int i = 0; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1479 | ByteString bsName; |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1480 | for (const auto& it : *pDest) { |
| 1481 | bsName = it.first; |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 1482 | pDestObj = it.second.get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1483 | if (!pDestObj) |
| 1484 | continue; |
| 1485 | if (i == index) |
| 1486 | break; |
| 1487 | i++; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1488 | } |
Jane Liu | 67ccef7 | 2017-07-19 13:10:50 -0400 | [diff] [blame] | 1489 | wsName = PDF_DecodeText(bsName); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1490 | } else { |
Jane Liu | 67ccef7 | 2017-07-19 13:10:50 -0400 | [diff] [blame] | 1491 | pDestObj = nameTree.LookupValueAndName(index, &wsName); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1492 | } |
| 1493 | if (!pDestObj) |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1494 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 1495 | if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1496 | pDestObj = pDict->GetArrayFor("D"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1497 | if (!pDestObj) |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1498 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1499 | } |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1500 | if (!pDestObj->IsArray()) |
| 1501 | return nullptr; |
| 1502 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 1503 | ByteString utf16Name = wsName.UTF16LE_Encode(); |
weili | 47ca692 | 2016-03-31 15:08:27 -0700 | [diff] [blame] | 1504 | int len = utf16Name.GetLength(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1505 | if (!buffer) { |
| 1506 | *buflen = len; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 1507 | } else if (len <= *buflen) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1508 | memcpy(buffer, utf16Name.c_str(), len); |
| 1509 | *buflen = len; |
| 1510 | } else { |
| 1511 | *buflen = -1; |
| 1512 | } |
| 1513 | return (FPDF_DEST)pDestObj; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1514 | } |