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 | 8a4e286 | 2016-09-29 13:43:30 -0700 | [diff] [blame] | 30 | #include "core/fxcodec/fx_codec.h" |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 31 | #include "core/fxcrt/fx_memory.h" |
| 32 | #include "core/fxcrt/fx_safe_types.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 | |
thestig | 25fa42f | 2016-05-25 21:39:46 -0700 | [diff] [blame] | 55 | #if _FXM_PLATFORM_ == _FXM_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 | |
| 83 | if (flags & FPDF_LCD_TEXT) |
| 84 | pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE; |
| 85 | else |
| 86 | pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE; |
| 87 | |
| 88 | if (flags & FPDF_NO_NATIVETEXT) |
| 89 | pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT; |
| 90 | if (flags & FPDF_RENDER_LIMITEDIMAGECACHE) |
| 91 | pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE; |
| 92 | if (flags & FPDF_RENDER_FORCEHALFTONE) |
| 93 | pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE; |
| 94 | #ifndef PDF_ENABLE_XFA |
| 95 | if (flags & FPDF_RENDER_NO_SMOOTHTEXT) |
| 96 | pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH; |
| 97 | if (flags & FPDF_RENDER_NO_SMOOTHIMAGE) |
| 98 | pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH; |
| 99 | if (flags & FPDF_RENDER_NO_SMOOTHPATH) |
| 100 | pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH; |
| 101 | #endif // PDF_ENABLE_XFA |
| 102 | |
| 103 | // Grayscale output |
Dan Sinclair | f55e72e | 2017-07-13 14:53:28 -0400 | [diff] [blame] | 104 | if (flags & FPDF_GRAYSCALE) |
| 105 | pContext->m_pOptions->m_ColorMode = CPDF_RenderOptions::kGray; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 106 | |
| 107 | const CPDF_OCContext::UsageType usage = |
| 108 | (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 109 | pContext->m_pOptions->m_pOCContext = |
Tom Sepez | 4cb82ee | 2017-05-22 15:15:30 -0700 | [diff] [blame] | 110 | pdfium::MakeRetain<CPDF_OCContext>(pPage->m_pDocument.Get(), usage); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 111 | |
| 112 | pContext->m_pDevice->SaveState(); |
| 113 | pContext->m_pDevice->SetClip_Rect(clipping_rect); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 114 | pContext->m_pContext = pdfium::MakeUnique<CPDF_RenderContext>(pPage); |
| 115 | pContext->m_pContext->AppendLayer(pPage, &matrix); |
| 116 | |
| 117 | if (flags & FPDF_ANNOT) { |
| 118 | pContext->m_pAnnots = pdfium::MakeUnique<CPDF_AnnotList>(pPage); |
| 119 | bool bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY; |
| 120 | pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext.get(), |
| 121 | bPrinting, &matrix, false, nullptr); |
| 122 | } |
| 123 | |
| 124 | pContext->m_pRenderer = pdfium::MakeUnique<CPDF_ProgressiveRenderer>( |
| 125 | pContext->m_pContext.get(), pContext->m_pDevice.get(), |
| 126 | pContext->m_pOptions.get()); |
| 127 | pContext->m_pRenderer->Start(pause); |
| 128 | if (bNeedToRestore) |
| 129 | pContext->m_pDevice->RestoreState(false); |
| 130 | } |
| 131 | |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 132 | class CPDF_CustomAccess final : public IFX_SeekableReadStream { |
| 133 | public: |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 134 | template <typename T, typename... Args> |
| 135 | friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args); |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 136 | |
| 137 | // IFX_SeekableReadStream |
| 138 | FX_FILESIZE GetSize() override; |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 139 | bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
| 140 | |
| 141 | private: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 142 | explicit CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess); |
| 143 | |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 144 | FPDF_FILEACCESS m_FileAccess; |
| 145 | }; |
| 146 | |
| 147 | CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) |
| 148 | : m_FileAccess(*pFileAccess) {} |
| 149 | |
| 150 | FX_FILESIZE CPDF_CustomAccess::GetSize() { |
| 151 | return m_FileAccess.m_FileLen; |
| 152 | } |
| 153 | |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 154 | bool CPDF_CustomAccess::ReadBlock(void* buffer, |
| 155 | FX_FILESIZE offset, |
| 156 | size_t size) { |
| 157 | if (offset < 0) |
| 158 | return false; |
| 159 | |
| 160 | FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size); |
| 161 | newPos += offset; |
| 162 | if (!newPos.IsValid() || |
| 163 | newPos.ValueOrDie() > static_cast<FX_FILESIZE>(m_FileAccess.m_FileLen)) { |
| 164 | return false; |
| 165 | } |
| 166 | return !!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 167 | static_cast<uint8_t*>(buffer), size); |
tsepez | bea0497 | 2016-12-01 13:54:42 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 170 | #ifdef PDF_ENABLE_XFA |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 171 | class CFPDF_FileStream : public IFX_SeekableStream { |
| 172 | public: |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 173 | template <typename T, typename... Args> |
| 174 | friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args); |
| 175 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 176 | ~CFPDF_FileStream() override; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 177 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 178 | // IFX_SeekableStream: |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 179 | FX_FILESIZE GetSize() override; |
| 180 | bool IsEOF() override; |
| 181 | FX_FILESIZE GetPosition() override; |
| 182 | bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
| 183 | size_t ReadBlock(void* buffer, size_t size) override; |
| 184 | bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; |
| 185 | bool Flush() override; |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 186 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 187 | void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; } |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 188 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 189 | protected: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 190 | explicit CFPDF_FileStream(FPDF_FILEHANDLER* pFS); |
| 191 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 192 | FPDF_FILEHANDLER* m_pFS; |
| 193 | FX_FILESIZE m_nCurPos; |
| 194 | }; |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 195 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) { |
| 197 | m_pFS = pFS; |
| 198 | m_nCurPos = 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 199 | } |
| 200 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 201 | CFPDF_FileStream::~CFPDF_FileStream() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | if (m_pFS && m_pFS->Release) |
| 203 | m_pFS->Release(m_pFS->clientData); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | FX_FILESIZE CFPDF_FileStream::GetSize() { |
| 207 | if (m_pFS && m_pFS->GetSize) |
| 208 | return (FX_FILESIZE)m_pFS->GetSize(m_pFS->clientData); |
| 209 | return 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 210 | } |
| 211 | |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 212 | bool CFPDF_FileStream::IsEOF() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | return m_nCurPos >= GetSize(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 214 | } |
| 215 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 216 | FX_FILESIZE CFPDF_FileStream::GetPosition() { |
| 217 | return m_nCurPos; |
| 218 | } |
| 219 | |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 220 | bool CFPDF_FileStream::ReadBlock(void* buffer, |
| 221 | FX_FILESIZE offset, |
| 222 | size_t size) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 223 | if (!buffer || !size || !m_pFS->ReadBlock) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 224 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 225 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 226 | if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, |
| 227 | (FPDF_DWORD)size) == 0) { |
| 228 | m_nCurPos = offset + size; |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 229 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 230 | } |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 231 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 234 | size_t CFPDF_FileStream::ReadBlock(void* buffer, size_t size) { |
| 235 | if (!buffer || !size || !m_pFS->ReadBlock) |
| 236 | return 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 237 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 238 | FX_FILESIZE nSize = GetSize(); |
| 239 | if (m_nCurPos >= nSize) |
| 240 | return 0; |
| 241 | FX_FILESIZE dwAvail = nSize - m_nCurPos; |
| 242 | if (dwAvail < (FX_FILESIZE)size) |
| 243 | size = (size_t)dwAvail; |
| 244 | if (m_pFS->ReadBlock(m_pFS->clientData, (FPDF_DWORD)m_nCurPos, buffer, |
| 245 | (FPDF_DWORD)size) == 0) { |
| 246 | m_nCurPos += size; |
| 247 | return size; |
| 248 | } |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 249 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 250 | return 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 251 | } |
| 252 | |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 253 | bool CFPDF_FileStream::WriteBlock(const void* buffer, |
| 254 | FX_FILESIZE offset, |
| 255 | size_t size) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 256 | if (!m_pFS || !m_pFS->WriteBlock) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 257 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 258 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 259 | if (m_pFS->WriteBlock(m_pFS->clientData, (FPDF_DWORD)offset, buffer, |
| 260 | (FPDF_DWORD)size) == 0) { |
| 261 | m_nCurPos = offset + size; |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 262 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 263 | } |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 264 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 265 | } |
| 266 | |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 267 | bool CFPDF_FileStream::Flush() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 268 | if (!m_pFS || !m_pFS->Flush) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 269 | return true; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 270 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 271 | return m_pFS->Flush(m_pFS->clientData) == 0; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 272 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 273 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 274 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 275 | } // namespace |
| 276 | |
| 277 | UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) { |
| 278 | return static_cast<UnderlyingDocumentType*>(doc); |
| 279 | } |
| 280 | |
| 281 | FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) { |
| 282 | return static_cast<FPDF_DOCUMENT>(doc); |
| 283 | } |
| 284 | |
| 285 | UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) { |
| 286 | return static_cast<UnderlyingPageType*>(page); |
| 287 | } |
| 288 | |
| 289 | CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) { |
| 290 | #ifdef PDF_ENABLE_XFA |
| 291 | return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr; |
| 292 | #else // PDF_ENABLE_XFA |
| 293 | return UnderlyingFromFPDFDocument(doc); |
| 294 | #endif // PDF_ENABLE_XFA |
| 295 | } |
| 296 | |
| 297 | FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) { |
| 298 | #ifdef PDF_ENABLE_XFA |
| 299 | return doc ? FPDFDocumentFromUnderlying( |
| 300 | new CPDFXFA_Context(pdfium::WrapUnique(doc))) |
| 301 | : nullptr; |
| 302 | #else // PDF_ENABLE_XFA |
| 303 | return FPDFDocumentFromUnderlying(doc); |
| 304 | #endif // PDF_ENABLE_XFA |
| 305 | } |
| 306 | |
| 307 | CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) { |
| 308 | #ifdef PDF_ENABLE_XFA |
| 309 | return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr; |
| 310 | #else // PDF_ENABLE_XFA |
| 311 | return UnderlyingFromFPDFPage(page); |
| 312 | #endif // PDF_ENABLE_XFA |
| 313 | } |
| 314 | |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 315 | CPDF_PathObject* CPDFPathObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) { |
| 316 | return static_cast<CPDF_PathObject*>(page_object); |
| 317 | } |
| 318 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 319 | CPDF_PageObject* CPDFPageObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) { |
| 320 | return static_cast<CPDF_PageObject*>(page_object); |
| 321 | } |
| 322 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 323 | CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) { |
| 324 | return static_cast<CFX_DIBitmap*>(bitmap); |
| 325 | } |
| 326 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 327 | CFX_RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream( |
| 328 | FPDF_FILEACCESS* pFileAccess) { |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 329 | return pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 330 | } |
| 331 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 332 | #ifdef PDF_ENABLE_XFA |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 333 | CFX_RetainPtr<IFX_SeekableStream> MakeSeekableStream( |
| 334 | FPDF_FILEHANDLER* pFilehandler) { |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 335 | return pdfium::MakeRetain<CFPDF_FileStream>(pFilehandler); |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 336 | } |
| 337 | #endif // PDF_ENABLE_XFA |
| 338 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 339 | // 0 bit: FPDF_POLICY_MACHINETIME_ACCESS |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 340 | static uint32_t foxit_sandbox_policy = 0xFFFFFFFF; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 341 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 342 | void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) { |
| 343 | switch (policy) { |
| 344 | case FPDF_POLICY_MACHINETIME_ACCESS: { |
| 345 | if (enable) |
| 346 | foxit_sandbox_policy |= 0x01; |
| 347 | else |
| 348 | foxit_sandbox_policy &= 0xFFFFFFFE; |
| 349 | } break; |
| 350 | default: |
| 351 | break; |
| 352 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 355 | FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) { |
| 356 | switch (policy) { |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 357 | case FPDF_POLICY_MACHINETIME_ACCESS: |
Lei Zhang | b0748bb | 2015-10-19 12:11:49 -0700 | [diff] [blame] | 358 | return !!(foxit_sandbox_policy & 0x01); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 359 | default: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 360 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 361 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 364 | DLLEXPORT void STDCALL FPDF_InitLibrary() { |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 365 | FPDF_InitLibraryWithConfig(nullptr); |
| 366 | } |
| 367 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 368 | DLLEXPORT void STDCALL |
| 369 | FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* cfg) { |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 370 | if (g_bLibraryInitialized) |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 371 | return; |
| 372 | |
Dan Sinclair | dbc3d3e | 2017-05-11 13:41:38 -0400 | [diff] [blame] | 373 | FXMEM_InitializePartitionAlloc(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 374 | |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 375 | CFX_GEModule* pModule = CFX_GEModule::Get(); |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 376 | pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr); |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 377 | |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 378 | CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get(); |
Lei Zhang | 76020fc | 2017-05-18 15:51:20 -0700 | [diff] [blame] | 379 | pModuleMgr->Init(); |
Tom Sepez | b4a2618 | 2017-03-01 12:15:00 -0800 | [diff] [blame] | 380 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 381 | #ifdef PDF_ENABLE_XFA |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 382 | FXJSE_Initialize(); |
| 383 | BC_Library_Init(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 384 | #endif // PDF_ENABLE_XFA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 385 | if (cfg && cfg->version >= 2) |
| 386 | IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate); |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 387 | |
| 388 | g_bLibraryInitialized = true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 391 | DLLEXPORT void STDCALL FPDF_DestroyLibrary() { |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 392 | if (!g_bLibraryInitialized) |
tsepez | 0275910 | 2016-12-01 08:29:25 -0800 | [diff] [blame] | 393 | return; |
| 394 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 395 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 5628fd7 | 2017-04-27 14:58:53 -0700 | [diff] [blame] | 396 | BC_Library_Destroy(); |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 397 | FXJSE_Finalize(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 398 | #endif // PDF_ENABLE_XFA |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 399 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 400 | CPDF_ModuleMgr::Destroy(); |
| 401 | CFX_GEModule::Destroy(); |
Tom Sepez | 2c28619 | 2015-06-18 12:47:11 -0700 | [diff] [blame] | 402 | |
thestig | 2d6dda1 | 2016-06-28 07:39:09 -0700 | [diff] [blame] | 403 | IJS_Runtime::Destroy(); |
Dan Sinclair | 830897a | 2017-05-11 14:36:10 -0400 | [diff] [blame] | 404 | |
| 405 | g_bLibraryInitialized = false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | #ifndef _WIN32 |
| 409 | int g_LastError; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 410 | void SetLastError(int err) { |
| 411 | g_LastError = err; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 414 | int GetLastError() { |
| 415 | return g_LastError; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 416 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 417 | #endif // _WIN32 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 418 | |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 419 | void ProcessParseError(CPDF_Parser::Error err) { |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 420 | uint32_t err_code = FPDF_ERR_SUCCESS; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 421 | // Translate FPDFAPI error code to FPDFVIEW error code |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 422 | switch (err) { |
| 423 | case CPDF_Parser::SUCCESS: |
| 424 | err_code = FPDF_ERR_SUCCESS; |
| 425 | break; |
| 426 | case CPDF_Parser::FILE_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 427 | err_code = FPDF_ERR_FILE; |
| 428 | break; |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 429 | case CPDF_Parser::FORMAT_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 430 | err_code = FPDF_ERR_FORMAT; |
| 431 | break; |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 432 | case CPDF_Parser::PASSWORD_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 433 | err_code = FPDF_ERR_PASSWORD; |
| 434 | break; |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 435 | case CPDF_Parser::HANDLER_ERROR: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 436 | err_code = FPDF_ERR_SECURITY; |
| 437 | break; |
| 438 | } |
| 439 | SetLastError(err_code); |
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 | DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy, |
| 443 | FPDF_BOOL enable) { |
| 444 | return FSDK_SetSandBoxPolicy(policy, enable); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 445 | } |
| 446 | |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 447 | #if defined(_WIN32) |
| 448 | #if defined(PDFIUM_PRINT_TEXT_WITH_GDI) |
thestig | fdb35ff | 2016-07-18 13:45:44 -0700 | [diff] [blame] | 449 | DLLEXPORT void STDCALL |
| 450 | FPDF_SetTypefaceAccessibleFunc(PDFiumEnsureTypefaceCharactersAccessible func) { |
| 451 | g_pdfium_typeface_accessible_func = func; |
| 452 | } |
| 453 | |
| 454 | DLLEXPORT void STDCALL FPDF_SetPrintTextWithGDI(FPDF_BOOL use_gdi) { |
| 455 | g_pdfium_print_text_with_gdi = !!use_gdi; |
| 456 | } |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 457 | #endif // PDFIUM_PRINT_TEXT_WITH_GDI |
| 458 | |
rbpotter | 38a01b9 | 2017-07-13 11:58:01 -0700 | [diff] [blame] | 459 | DLLEXPORT FPDF_BOOL STDCALL FPDF_SetPrintPostscriptLevel(int postscript_level) { |
| 460 | return postscript_level != 1 && FPDF_SetPrintMode(postscript_level); |
| 461 | } |
| 462 | |
rbpotter | e8468c4 | 2017-07-11 10:04:29 -0700 | [diff] [blame] | 463 | DLLEXPORT FPDF_BOOL STDCALL FPDF_SetPrintMode(int mode) { |
| 464 | if (mode < FPDF_PRINTMODE_EMF || mode > FPDF_PRINTMODE_POSTSCRIPT3) |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 465 | return FALSE; |
rbpotter | e8468c4 | 2017-07-11 10:04:29 -0700 | [diff] [blame] | 466 | g_pdfium_print_mode = mode; |
rbpotter | db76470 | 2017-01-12 10:31:43 -0800 | [diff] [blame] | 467 | return TRUE; |
| 468 | } |
| 469 | #endif // defined(_WIN32) |
thestig | fdb35ff | 2016-07-18 13:45:44 -0700 | [diff] [blame] | 470 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 471 | DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, |
| 472 | FPDF_BYTESTRING password) { |
Tom Sepez | e3166a8 | 2015-08-05 10:50:32 -0700 | [diff] [blame] | 473 | // NOTE: the creation of the file needs to be by the embedder on the |
| 474 | // other side of this API. |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 475 | CFX_RetainPtr<IFX_SeekableReadStream> pFileAccess = |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 476 | IFX_SeekableReadStream::CreateFromFilename((const char*)file_path); |
tsepez | 345d489 | 2016-11-30 15:10:55 -0800 | [diff] [blame] | 477 | if (!pFileAccess) |
Tom Sepez | e3166a8 | 2015-08-05 10:50:32 -0700 | [diff] [blame] | 478 | return nullptr; |
Tom Sepez | e3166a8 | 2015-08-05 10:50:32 -0700 | [diff] [blame] | 479 | |
tsepez | 345d489 | 2016-11-30 15:10:55 -0800 | [diff] [blame] | 480 | auto pParser = pdfium::MakeUnique<CPDF_Parser>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 481 | pParser->SetPassword(password); |
Bo Xu | d4e406e | 2014-08-13 11:03:19 -0700 | [diff] [blame] | 482 | |
tsepez | 345d489 | 2016-11-30 15:10:55 -0800 | [diff] [blame] | 483 | auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser)); |
dsinclair | 156de02 | 2016-08-24 11:58:24 -0700 | [diff] [blame] | 484 | CPDF_Parser::Error error = |
| 485 | pDocument->GetParser()->StartParse(pFileAccess, pDocument.get()); |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 486 | if (error != CPDF_Parser::SUCCESS) { |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 487 | ProcessParseError(error); |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 488 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 489 | } |
Dan Sinclair | cb377be | 2017-05-11 16:05:32 -0400 | [diff] [blame] | 490 | CheckUnSupportError(pDocument.get(), error); |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 491 | return FPDFDocumentFromCPDFDocument(pDocument.release()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 492 | } |
Jun Fang | e118ce9 | 2015-02-17 06:50:08 -0800 | [diff] [blame] | 493 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 494 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 495 | DLLEXPORT FPDF_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document, |
| 496 | int* docType) { |
| 497 | if (!document) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 498 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 499 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 500 | CPDF_Document* pdfDoc = |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 501 | (static_cast<CPDFXFA_Context*>(document))->GetPDFDoc(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 502 | if (!pdfDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 503 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 504 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 505 | CPDF_Dictionary* pRoot = pdfDoc->GetRoot(); |
| 506 | if (!pRoot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 507 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 508 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 509 | CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 510 | if (!pAcroForm) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 511 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 512 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 513 | CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 514 | if (!pXFA) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 515 | return false; |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 516 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 517 | bool bDynamicXFA = pRoot->GetBooleanFor("NeedsRendering", false); |
thestig | ded3634 | 2016-05-23 17:54:02 -0700 | [diff] [blame] | 518 | *docType = bDynamicXFA ? DOCTYPE_DYNAMIC_XFA : DOCTYPE_STATIC_XFA; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 519 | return true; |
Jun Fang | e118ce9 | 2015-02-17 06:50:08 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 522 | DLLEXPORT FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document) { |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 523 | return document && (static_cast<CPDFXFA_Context*>(document))->LoadXFADoc(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 524 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 525 | #endif // PDF_ENABLE_XFA |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 526 | |
tsepez | ad2441e | 2016-10-24 10:19:11 -0700 | [diff] [blame] | 527 | class CMemFile final : public IFX_SeekableReadStream { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 528 | public: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 529 | static CFX_RetainPtr<CMemFile> Create(uint8_t* pBuf, FX_FILESIZE size) { |
| 530 | return CFX_RetainPtr<CMemFile>(new CMemFile(pBuf, size)); |
| 531 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 532 | |
Lei Zhang | 3884dba | 2015-10-19 17:27:53 -0700 | [diff] [blame] | 533 | FX_FILESIZE GetSize() override { return m_size; } |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 534 | bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 535 | if (offset < 0) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 536 | return false; |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 537 | |
tsepez | 4e597c8 | 2016-11-07 15:16:01 -0800 | [diff] [blame] | 538 | FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE>(size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 539 | newPos += offset; |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 540 | if (!newPos.IsValid() || newPos.ValueOrDie() > m_size) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 541 | return false; |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 542 | |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 543 | memcpy(buffer, m_pBuf + offset, size); |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 544 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 545 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 546 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 547 | private: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 548 | 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] | 549 | |
Lei Zhang | 3884dba | 2015-10-19 17:27:53 -0700 | [diff] [blame] | 550 | uint8_t* const m_pBuf; |
| 551 | const FX_FILESIZE m_size; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 552 | }; |
Lei Zhang | 3884dba | 2015-10-19 17:27:53 -0700 | [diff] [blame] | 553 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 554 | DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf, |
| 555 | int size, |
| 556 | FPDF_BYTESTRING password) { |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 557 | CFX_RetainPtr<CMemFile> pMemFile = CMemFile::Create((uint8_t*)data_buf, size); |
| 558 | auto pParser = pdfium::MakeUnique<CPDF_Parser>(); |
tsepez | 4540fba | 2016-08-16 11:12:21 -0700 | [diff] [blame] | 559 | pParser->SetPassword(password); |
| 560 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 561 | auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser)); |
tsepez | 4540fba | 2016-08-16 11:12:21 -0700 | [diff] [blame] | 562 | CPDF_Parser::Error error = |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame] | 563 | pDocument->GetParser()->StartParse(pMemFile, pDocument.get()); |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 564 | if (error != CPDF_Parser::SUCCESS) { |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 565 | ProcessParseError(error); |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 566 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 567 | } |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame] | 568 | CheckUnSupportError(pDocument.get(), error); |
| 569 | return FPDFDocumentFromCPDFDocument(pDocument.release()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 572 | DLLEXPORT FPDF_DOCUMENT STDCALL |
| 573 | FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess, |
| 574 | FPDF_BYTESTRING password) { |
Tom Sepez | 40badde | 2017-05-01 13:21:39 -0700 | [diff] [blame] | 575 | auto pFile = pdfium::MakeRetain<CPDF_CustomAccess>(pFileAccess); |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 576 | auto pParser = pdfium::MakeUnique<CPDF_Parser>(); |
tsepez | 4540fba | 2016-08-16 11:12:21 -0700 | [diff] [blame] | 577 | pParser->SetPassword(password); |
| 578 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 579 | auto pDocument = pdfium::MakeUnique<CPDF_Document>(std::move(pParser)); |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame] | 580 | CPDF_Parser::Error error = |
| 581 | pDocument->GetParser()->StartParse(pFile, pDocument.get()); |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 582 | if (error != CPDF_Parser::SUCCESS) { |
Tom Sepez | f10ae63 | 2016-01-26 14:19:52 -0800 | [diff] [blame] | 583 | ProcessParseError(error); |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 584 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 585 | } |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame] | 586 | CheckUnSupportError(pDocument.get(), error); |
| 587 | return FPDFDocumentFromCPDFDocument(pDocument.release()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 590 | DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc, |
| 591 | int* fileVersion) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 592 | if (!fileVersion) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 593 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 594 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 595 | *fileVersion = 0; |
| 596 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc); |
| 597 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 598 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 599 | |
| 600 | CPDF_Parser* pParser = pDoc->GetParser(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 601 | if (!pParser) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 602 | return false; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 603 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 604 | *fileVersion = pParser->GetFileVersion(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 605 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 606 | } |
| 607 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 608 | // 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] | 609 | // header). |
| 610 | DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 611 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 612 | // https://bugs.chromium.org/p/pdfium/issues/detail?id=499 |
| 613 | if (!pDoc) { |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 614 | #ifndef PDF_ENABLE_XFA |
| 615 | return 0; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 616 | #else // PDF_ENABLE_XFA |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 617 | return 0xFFFFFFFF; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 618 | #endif // PDF_ENABLE_XFA |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 619 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 620 | |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 621 | return pDoc->GetUserPermissions(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 624 | DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 625 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
thestig | b8db511 | 2016-04-06 12:12:52 -0700 | [diff] [blame] | 626 | if (!pDoc || !pDoc->GetParser()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 627 | return -1; |
Bo Xu | c5cab02 | 2014-09-19 19:16:31 -0700 | [diff] [blame] | 628 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 629 | CPDF_Dictionary* pDict = pDoc->GetParser()->GetEncryptDict(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 630 | return pDict ? pDict->GetIntegerFor("R") : -1; |
Bo Xu | c5cab02 | 2014-09-19 19:16:31 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 633 | DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 634 | UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document); |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 635 | return pDoc ? pDoc->GetPageCount() : 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 638 | DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document, |
| 639 | int page_index) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 640 | UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document); |
| 641 | if (!pDoc) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 642 | return nullptr; |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 643 | |
Tom Sepez | bbe0e4d | 2015-10-20 15:41:40 -0700 | [diff] [blame] | 644 | if (page_index < 0 || page_index >= pDoc->GetPageCount()) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 645 | return nullptr; |
| 646 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 647 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 9792f16 | 2017-05-16 14:11:30 -0700 | [diff] [blame] | 648 | return pDoc->GetXFAPage(page_index).Leak(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 649 | #else // PDF_ENABLE_XFA |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 650 | CPDF_Dictionary* pDict = pDoc->GetPage(page_index); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 651 | if (!pDict) |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 652 | return nullptr; |
| 653 | |
| 654 | CPDF_Page* pPage = new CPDF_Page(pDoc, pDict, true); |
| 655 | pPage->ParseContent(); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 656 | return pPage; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 657 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 660 | DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 661 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 662 | return pPage ? pPage->GetPageWidth() : 0.0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 665 | DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 666 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 667 | return pPage ? pPage->GetPageHeight() : 0.0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 668 | } |
| 669 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 670 | #if defined(_WIN32) |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 671 | namespace { |
| 672 | |
| 673 | const double kEpsilonSize = 0.01f; |
| 674 | |
| 675 | void GetScaling(CPDF_Page* pPage, |
| 676 | int size_x, |
| 677 | int size_y, |
| 678 | int rotate, |
| 679 | double* scale_x, |
| 680 | double* scale_y) { |
| 681 | ASSERT(pPage); |
| 682 | ASSERT(scale_x); |
| 683 | ASSERT(scale_y); |
| 684 | double page_width = pPage->GetPageWidth(); |
| 685 | double page_height = pPage->GetPageHeight(); |
| 686 | if (page_width < kEpsilonSize || page_height < kEpsilonSize) |
| 687 | return; |
| 688 | |
| 689 | if (rotate % 2 == 0) { |
| 690 | *scale_x = size_x / page_width; |
| 691 | *scale_y = size_y / page_height; |
| 692 | } else { |
| 693 | *scale_x = size_y / page_width; |
| 694 | *scale_y = size_x / page_height; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | FX_RECT GetMaskDimensionsAndOffsets(CPDF_Page* pPage, |
| 699 | int start_x, |
| 700 | int start_y, |
| 701 | int size_x, |
| 702 | int size_y, |
| 703 | int rotate, |
| 704 | const CFX_FloatRect& mask_box) { |
| 705 | double scale_x = 0.0f; |
| 706 | double scale_y = 0.0f; |
| 707 | GetScaling(pPage, size_x, size_y, rotate, &scale_x, &scale_y); |
| 708 | if (scale_x < kEpsilonSize || scale_y < kEpsilonSize) |
| 709 | return FX_RECT(); |
| 710 | |
| 711 | // Compute sizes in page points. Round down to catch the entire bitmap. |
| 712 | int start_x_bm = static_cast<int>(mask_box.left * scale_x); |
| 713 | int start_y_bm = static_cast<int>(mask_box.bottom * scale_y); |
| 714 | int size_x_bm = static_cast<int>(mask_box.right * scale_x + 1.0f) - |
| 715 | static_cast<int>(mask_box.left * scale_x); |
| 716 | int size_y_bm = static_cast<int>(mask_box.top * scale_y + 1.0f) - |
| 717 | static_cast<int>(mask_box.bottom * scale_y); |
| 718 | |
| 719 | // Get page rotation |
rbpotter | ce8e51e | 2017-04-28 12:42:47 -0700 | [diff] [blame] | 720 | int page_rotation = pPage->GetPageRotation(); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 721 | |
| 722 | // Compute offsets |
| 723 | int offset_x = 0; |
| 724 | int offset_y = 0; |
| 725 | switch ((rotate + page_rotation) % 4) { |
| 726 | case 0: |
| 727 | offset_x = start_x_bm + start_x; |
| 728 | offset_y = start_y + size_y - size_y_bm - start_y_bm; |
| 729 | break; |
| 730 | case 1: |
| 731 | offset_x = start_y_bm + start_x; |
| 732 | offset_y = start_x_bm + start_y; |
| 733 | break; |
| 734 | case 2: |
| 735 | offset_x = start_x + size_x - size_x_bm - start_x_bm; |
| 736 | offset_y = start_y_bm + start_y; |
| 737 | break; |
| 738 | case 3: |
| 739 | offset_x = start_x + size_x - size_x_bm - start_y_bm; |
| 740 | offset_y = start_y + size_y - size_y_bm - start_x_bm; |
| 741 | break; |
| 742 | } |
| 743 | return FX_RECT(offset_x, offset_y, offset_x + size_x_bm, |
| 744 | offset_y + size_y_bm); |
| 745 | } |
| 746 | |
| 747 | // Get a bitmap of just the mask section defined by |mask_box| from a full page |
| 748 | // bitmap |pBitmap|. |
| 749 | CFX_RetainPtr<CFX_DIBitmap> GetMaskBitmap(CPDF_Page* pPage, |
| 750 | int start_x, |
| 751 | int start_y, |
| 752 | int size_x, |
| 753 | int size_y, |
| 754 | int rotate, |
| 755 | CFX_RetainPtr<CFX_DIBitmap>& pSrc, |
| 756 | const CFX_FloatRect& mask_box, |
| 757 | FX_RECT* bitmap_area) { |
| 758 | ASSERT(bitmap_area); |
| 759 | *bitmap_area = GetMaskDimensionsAndOffsets(pPage, start_x, start_y, size_x, |
| 760 | size_y, rotate, mask_box); |
| 761 | if (bitmap_area->IsEmpty()) |
| 762 | return nullptr; |
| 763 | |
| 764 | // Create a new bitmap to transfer part of the page bitmap to. |
| 765 | CFX_RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>(); |
| 766 | pDst->Create(bitmap_area->Width(), bitmap_area->Height(), FXDIB_Argb); |
| 767 | pDst->Clear(0x00ffffff); |
| 768 | pDst->TransferBitmap(0, 0, bitmap_area->Width(), bitmap_area->Height(), pSrc, |
| 769 | bitmap_area->left, bitmap_area->top); |
| 770 | return pDst; |
| 771 | } |
| 772 | |
| 773 | void RenderBitmap(CFX_RenderDevice* device, |
| 774 | const CFX_RetainPtr<CFX_DIBitmap>& pSrc, |
| 775 | const FX_RECT& mask_area) { |
| 776 | int size_x_bm = mask_area.Width(); |
| 777 | int size_y_bm = mask_area.Height(); |
| 778 | if (size_x_bm == 0 || size_y_bm == 0) |
| 779 | return; |
| 780 | |
| 781 | // Create a new bitmap from the old one |
| 782 | CFX_RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>(); |
| 783 | pDst->Create(size_x_bm, size_y_bm, FXDIB_Rgb32); |
| 784 | pDst->Clear(0xffffffff); |
| 785 | pDst->CompositeBitmap(0, 0, size_x_bm, size_y_bm, pSrc, 0, 0, |
| 786 | FXDIB_BLEND_NORMAL, nullptr, false); |
| 787 | |
| 788 | if (device->GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) { |
| 789 | device->StretchDIBitsWithFlagsAndBlend(pDst, mask_area.left, mask_area.top, |
| 790 | size_x_bm, size_y_bm, 0, |
| 791 | FXDIB_BLEND_NORMAL); |
| 792 | } else { |
| 793 | device->SetDIBits(pDst, mask_area.left, mask_area.top); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | } // namespace |
| 798 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 799 | DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc, |
| 800 | FPDF_PAGE page, |
| 801 | int start_x, |
| 802 | int start_y, |
| 803 | int size_x, |
| 804 | int size_y, |
| 805 | int rotate, |
| 806 | int flags) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 807 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 808 | if (!pPage) |
| 809 | return; |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 810 | pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>()); |
| 811 | CPDF_PageRenderContext* pContext = pPage->GetRenderContext(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 812 | |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 813 | CFX_RetainPtr<CFX_DIBitmap> pBitmap; |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 814 | // Don't render the full page to bitmap for a mask unless there are a lot |
| 815 | // of masks. Full page bitmaps result in large spool sizes, so they should |
| 816 | // only be used when necessary. For large numbers of masks, rendering each |
| 817 | // individually is inefficient and unlikely to significantly improve spool |
| 818 | // size. |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 819 | const bool bNewBitmap = |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 820 | pPage->BackgroundAlphaNeeded() || |
| 821 | (pPage->HasImageMask() && pPage->GetMaskBoundingBoxes().size() > 100); |
| 822 | const bool bHasMask = pPage->HasImageMask() && !bNewBitmap; |
| 823 | if (bNewBitmap || bHasMask) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 824 | pBitmap = pdfium::MakeRetain<CFX_DIBitmap>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 825 | pBitmap->Create(size_x, size_y, FXDIB_Argb); |
| 826 | pBitmap->Clear(0x00ffffff); |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 827 | CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice; |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 828 | pContext->m_pDevice = pdfium::WrapUnique(pDevice); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 829 | pDevice->Attach(pBitmap, false, nullptr, false); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 830 | if (bHasMask) { |
| 831 | pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>(); |
| 832 | pContext->m_pOptions->m_Flags |= RENDER_BREAKFORMASKS; |
| 833 | } |
Jun Fang | 1aeeceb | 2015-12-29 10:27:44 +0800 | [diff] [blame] | 834 | } else { |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 835 | pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc); |
Jun Fang | 1aeeceb | 2015-12-29 10:27:44 +0800 | [diff] [blame] | 836 | } |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 837 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 838 | FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 839 | rotate, flags, true, nullptr); |
Bo Xu | d4e406e | 2014-08-13 11:03:19 -0700 | [diff] [blame] | 840 | |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 841 | if (bHasMask) { |
| 842 | // Finish rendering the page to bitmap and copy the correct segments |
| 843 | // of the page to individual image mask bitmaps. |
| 844 | const std::vector<CFX_FloatRect>& mask_boxes = |
| 845 | pPage->GetMaskBoundingBoxes(); |
| 846 | std::vector<FX_RECT> bitmap_areas(mask_boxes.size()); |
| 847 | std::vector<CFX_RetainPtr<CFX_DIBitmap>> bitmaps(mask_boxes.size()); |
| 848 | for (size_t i = 0; i < mask_boxes.size(); i++) { |
| 849 | bitmaps[i] = |
| 850 | GetMaskBitmap(pPage, start_x, start_y, size_x, size_y, rotate, |
| 851 | pBitmap, mask_boxes[i], &bitmap_areas[i]); |
| 852 | pContext->m_pRenderer->Continue(nullptr); |
| 853 | } |
| 854 | |
| 855 | // Reset rendering context |
| 856 | pPage->SetRenderContext(nullptr); |
| 857 | |
| 858 | // Begin rendering to the printer. Add flag to indicate the renderer should |
| 859 | // pause after each image mask. |
| 860 | pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>()); |
| 861 | pContext = pPage->GetRenderContext(); |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 862 | pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsRenderDevice>(dc); |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 863 | pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>(); |
| 864 | pContext->m_pOptions->m_Flags |= RENDER_BREAKFORMASKS; |
| 865 | FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y, |
| 866 | rotate, flags, true, nullptr); |
| 867 | |
| 868 | // Render masks |
| 869 | for (size_t i = 0; i < mask_boxes.size(); i++) { |
| 870 | // Render the bitmap for the mask and free the bitmap. |
| 871 | if (bitmaps[i]) { // will be null if mask has zero area |
| 872 | RenderBitmap(pContext->m_pDevice.get(), bitmaps[i], bitmap_areas[i]); |
| 873 | } |
| 874 | // Render the next portion of page. |
| 875 | pContext->m_pRenderer->Continue(nullptr); |
| 876 | } |
| 877 | } else if (bNewBitmap) { |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 878 | CFX_WindowsRenderDevice WinDC(dc); |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 879 | if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 880 | auto pDst = pdfium::MakeRetain<CFX_DIBitmap>(); |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 881 | int pitch = pBitmap->GetPitch(); |
| 882 | pDst->Create(size_x, size_y, FXDIB_Rgb32); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 883 | memset(pDst->GetBuffer(), -1, pitch * size_y); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 884 | pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0, |
Nicolas Pena | 6a5c20c | 2017-04-07 14:12:31 -0400 | [diff] [blame] | 885 | FXDIB_BLEND_NORMAL, nullptr, false); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 886 | WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y); |
thestig | 84144e8 | 2016-09-27 15:06:01 -0700 | [diff] [blame] | 887 | } else { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 888 | WinDC.SetDIBits(pBitmap, 0, 0); |
Lei Zhang | 6d8b1c2 | 2015-06-19 17:26:17 -0700 | [diff] [blame] | 889 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 890 | } |
rbpotter | cec0180 | 2017-04-17 09:10:21 -0700 | [diff] [blame] | 891 | |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 892 | pPage->SetRenderContext(nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 893 | } |
Lei Zhang | ae85f87 | 2016-02-19 14:57:07 -0800 | [diff] [blame] | 894 | #endif // defined(_WIN32) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 895 | |
| 896 | DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap, |
| 897 | FPDF_PAGE page, |
| 898 | int start_x, |
| 899 | int start_y, |
| 900 | int size_x, |
| 901 | int size_y, |
| 902 | int rotate, |
| 903 | int flags) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 904 | if (!bitmap) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 905 | return; |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 906 | |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 907 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 908 | if (!pPage) |
| 909 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 910 | |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 911 | CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext; |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 912 | pPage->SetRenderContext(pdfium::WrapUnique(pContext)); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 913 | |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 914 | CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice; |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 915 | pContext->m_pDevice.reset(pDevice); |
dsinclair | 31b08d4 | 2017-03-28 15:47:47 +0000 | [diff] [blame] | 916 | |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 917 | CFX_RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap)); |
| 918 | pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 919 | FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 920 | rotate, flags, true, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 921 | |
caryclark | 687fbde | 2016-11-22 12:44:25 -0800 | [diff] [blame] | 922 | #ifdef _SKIA_SUPPORT_PATHS_ |
caryclark | 8f87550 | 2016-12-06 13:49:34 -0800 | [diff] [blame] | 923 | pDevice->Flush(); |
caryclark | 687fbde | 2016-11-22 12:44:25 -0800 | [diff] [blame] | 924 | pBitmap->UnPreMultiply(); |
| 925 | #endif |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 926 | pPage->SetRenderContext(nullptr); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 927 | } |
| 928 | |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 929 | DLLEXPORT void STDCALL FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap, |
| 930 | FPDF_PAGE page, |
| 931 | const FS_MATRIX* matrix, |
| 932 | const FS_RECTF* clipping, |
| 933 | int flags) { |
| 934 | if (!bitmap) |
| 935 | return; |
| 936 | |
| 937 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 938 | if (!pPage) |
| 939 | return; |
| 940 | |
| 941 | CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext; |
| 942 | pPage->SetRenderContext(pdfium::WrapUnique(pContext)); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 943 | |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 944 | CFX_DefaultRenderDevice* pDevice = new CFX_DefaultRenderDevice; |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 945 | pContext->m_pDevice.reset(pDevice); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 946 | |
| 947 | CFX_RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap)); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 948 | pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false); |
| 949 | |
| 950 | CFX_Matrix transform_matrix = pPage->GetPageMatrix(); |
| 951 | if (matrix) { |
Dan Sinclair | bba2a7c | 2017-02-07 16:36:39 -0500 | [diff] [blame] | 952 | transform_matrix.Concat(CFX_Matrix(matrix->a, matrix->b, matrix->c, |
| 953 | matrix->d, matrix->e, matrix->f)); |
thestig | a78ba60 | 2016-11-23 15:25:48 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | CFX_FloatRect clipping_rect; |
| 957 | if (clipping) { |
| 958 | clipping_rect.left = clipping->left; |
| 959 | clipping_rect.bottom = clipping->bottom; |
| 960 | clipping_rect.right = clipping->right; |
| 961 | clipping_rect.top = clipping->top; |
| 962 | } |
| 963 | RenderPageImpl(pContext, pPage, transform_matrix, clipping_rect.ToFxRect(), |
| 964 | flags, true, nullptr); |
| 965 | |
| 966 | pPage->SetRenderContext(nullptr); |
| 967 | } |
| 968 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 969 | #ifdef _SKIA_SUPPORT_ |
| 970 | DLLEXPORT FPDF_RECORDER STDCALL FPDF_RenderPageSkp(FPDF_PAGE page, |
| 971 | int size_x, |
| 972 | int size_y) { |
| 973 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 974 | if (!pPage) |
| 975 | return nullptr; |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 976 | |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 977 | CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext; |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 978 | pPage->SetRenderContext(pdfium::WrapUnique(pContext)); |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 979 | CFX_DefaultRenderDevice* skDevice = new CFX_DefaultRenderDevice; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 980 | FPDF_RECORDER recorder = skDevice->CreateRecorder(size_x, size_y); |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 981 | pContext->m_pDevice.reset(skDevice); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 982 | 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] | 983 | nullptr); |
tsepez | e5cb0b1 | 2016-10-26 15:06:11 -0700 | [diff] [blame] | 984 | pPage->SetRenderContext(nullptr); |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 985 | return recorder; |
| 986 | } |
| 987 | #endif |
| 988 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 989 | DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) { |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 990 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 991 | if (!page) |
| 992 | return; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 993 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 9792f16 | 2017-05-16 14:11:30 -0700 | [diff] [blame] | 994 | // Take it back across the API and throw it away. |
| 995 | CFX_RetainPtr<CPDFXFA_Page>().Unleak(pPage); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 996 | #else // PDF_ENABLE_XFA |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 997 | CPDFSDK_PageView* pPageView = |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 998 | static_cast<CPDFSDK_PageView*>(pPage->GetView()); |
dsinclair | 1df1efa | 2016-09-07 09:55:37 -0700 | [diff] [blame] | 999 | if (pPageView) { |
dsinclair | bcf4623 | 2016-10-03 13:02:27 -0700 | [diff] [blame] | 1000 | // We're already destroying the pageview, so bail early. |
| 1001 | if (pPageView->IsBeingDestroyed()) |
| 1002 | return; |
| 1003 | |
dsinclair | 1df1efa | 2016-09-07 09:55:37 -0700 | [diff] [blame] | 1004 | if (pPageView->IsLocked()) { |
| 1005 | pPageView->TakePageOwnership(); |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | bool owned = pPageView->OwnsPage(); |
| 1010 | // This will delete the |pPageView| object. We must cleanup the PageView |
| 1011 | // first because it will attempt to reset the View on the |pPage| during |
| 1012 | // destruction. |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 1013 | pPageView->GetFormFillEnv()->RemovePageView(pPage); |
dsinclair | 1df1efa | 2016-09-07 09:55:37 -0700 | [diff] [blame] | 1014 | // If the page was owned then the pageview will have deleted the page. |
| 1015 | if (owned) |
| 1016 | return; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1017 | } |
tsepez | 1e2c557 | 2016-05-25 14:58:09 -0700 | [diff] [blame] | 1018 | delete pPage; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1019 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1022 | DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) { |
Jun Fang | fc75136 | 2015-12-16 21:23:39 -0800 | [diff] [blame] | 1023 | delete UnderlyingFromFPDFDocument(document); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1026 | DLLEXPORT unsigned long STDCALL FPDF_GetLastError() { |
| 1027 | return GetLastError(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1030 | DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page, |
| 1031 | int start_x, |
| 1032 | int start_y, |
| 1033 | int size_x, |
| 1034 | int size_y, |
| 1035 | int rotate, |
| 1036 | int device_x, |
| 1037 | int device_y, |
| 1038 | double* page_x, |
| 1039 | double* page_y) { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 1040 | if (!page || !page_x || !page_y) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1041 | return; |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 1042 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1043 | #ifdef PDF_ENABLE_XFA |
| 1044 | pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x, |
| 1045 | device_y, page_x, page_y); |
| 1046 | #else // PDF_ENABLE_XFA |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1047 | CFX_Matrix page2device = |
| 1048 | pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate); |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1049 | |
Nicolas Pena | b21f174 | 2017-06-29 12:02:06 -0400 | [diff] [blame] | 1050 | CFX_PointF pos = page2device.GetInverse().Transform( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 1051 | CFX_PointF(static_cast<float>(device_x), static_cast<float>(device_y))); |
Dan Sinclair | afb4456 | 2017-02-09 13:07:43 -0500 | [diff] [blame] | 1052 | |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 1053 | *page_x = pos.x; |
| 1054 | *page_y = pos.y; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1055 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1058 | DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page, |
| 1059 | int start_x, |
| 1060 | int start_y, |
| 1061 | int size_x, |
| 1062 | int size_y, |
| 1063 | int rotate, |
| 1064 | double page_x, |
| 1065 | double page_y, |
| 1066 | int* device_x, |
| 1067 | int* device_y) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 1068 | if (!device_x || !device_y) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1069 | return; |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 1070 | UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page); |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 1071 | if (!pPage) |
| 1072 | return; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1073 | #ifdef PDF_ENABLE_XFA |
| 1074 | pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y, |
| 1075 | device_x, device_y); |
| 1076 | #else // PDF_ENABLE_XFA |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1077 | CFX_Matrix page2device = |
| 1078 | pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate); |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 1079 | CFX_PointF pos = page2device.Transform( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 1080 | CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y))); |
Dan Sinclair | afb4456 | 2017-02-09 13:07:43 -0500 | [diff] [blame] | 1081 | |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 1082 | *device_x = FXSYS_round(pos.x); |
| 1083 | *device_y = FXSYS_round(pos.y); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1084 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1087 | DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width, |
| 1088 | int height, |
| 1089 | int alpha) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1090 | auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>(); |
tsepez | 37b12ad | 2016-12-14 19:50:23 -0800 | [diff] [blame] | 1091 | if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1092 | return nullptr; |
tsepez | 37b12ad | 2016-12-14 19:50:23 -0800 | [diff] [blame] | 1093 | |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1094 | return pBitmap.Leak(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1097 | DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width, |
| 1098 | int height, |
| 1099 | int format, |
| 1100 | void* first_scan, |
| 1101 | int stride) { |
| 1102 | FXDIB_Format fx_format; |
| 1103 | switch (format) { |
| 1104 | case FPDFBitmap_Gray: |
| 1105 | fx_format = FXDIB_8bppRgb; |
| 1106 | break; |
| 1107 | case FPDFBitmap_BGR: |
| 1108 | fx_format = FXDIB_Rgb; |
| 1109 | break; |
| 1110 | case FPDFBitmap_BGRx: |
| 1111 | fx_format = FXDIB_Rgb32; |
| 1112 | break; |
| 1113 | case FPDFBitmap_BGRA: |
| 1114 | fx_format = FXDIB_Argb; |
| 1115 | break; |
| 1116 | default: |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1117 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1118 | } |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1119 | auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1120 | pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1121 | return pBitmap.Leak(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1124 | DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap, |
| 1125 | int left, |
| 1126 | int top, |
| 1127 | int width, |
| 1128 | int height, |
| 1129 | FPDF_DWORD color) { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 1130 | if (!bitmap) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1131 | return; |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1132 | |
Dan Sinclair | a5085d4 | 2017-05-11 16:26:50 -0400 | [diff] [blame] | 1133 | CFX_DefaultRenderDevice device; |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1134 | CFX_RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap)); |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1135 | device.Attach(pBitmap, false, nullptr, false); |
| 1136 | if (!pBitmap->HasAlpha()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1137 | color |= 0xFF000000; |
| 1138 | FX_RECT rect(left, top, left + width, top + height); |
| 1139 | device.FillRect(&rect, color); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1142 | DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1143 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetBuffer() : nullptr; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1146 | DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1147 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetWidth() : 0; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1150 | DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1151 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetHeight() : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) { |
thestig | befa450 | 2016-05-26 20:15:19 -0700 | [diff] [blame] | 1155 | return bitmap ? CFXBitmapFromFPDFBitmap(bitmap)->GetPitch() : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) { |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 1159 | CFX_RetainPtr<CFX_DIBitmap> destroyer; |
| 1160 | destroyer.Unleak(CFXBitmapFromFPDFBitmap(bitmap)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
weili | 9f515bc | 2016-07-24 08:08:24 -0700 | [diff] [blame] | 1163 | void FPDF_RenderPage_Retail(CPDF_PageRenderContext* pContext, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1164 | FPDF_PAGE page, |
| 1165 | int start_x, |
| 1166 | int start_y, |
| 1167 | int size_x, |
| 1168 | int size_y, |
| 1169 | int rotate, |
| 1170 | int flags, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1171 | bool bNeedToRestore, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1172 | IFSDK_PAUSE_Adapter* pause) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 1173 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 1174 | if (!pPage) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1175 | return; |
| 1176 | |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 1177 | RenderPageImpl(pContext, pPage, pPage->GetDisplayMatrix( |
| 1178 | start_x, start_y, size_x, size_y, rotate), |
| 1179 | FX_RECT(start_x, start_y, start_x + size_x, start_y + size_y), |
| 1180 | flags, bNeedToRestore, pause); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document, |
| 1184 | int page_index, |
| 1185 | double* width, |
| 1186 | double* height) { |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1187 | UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document); |
| 1188 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1189 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1190 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1191 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1192 | int count = pDoc->GetPageCount(); |
| 1193 | if (page_index < 0 || page_index >= count) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1194 | return false; |
Tom Sepez | 9792f16 | 2017-05-16 14:11:30 -0700 | [diff] [blame] | 1195 | CFX_RetainPtr<CPDFXFA_Page> pPage = pDoc->GetXFAPage(page_index); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1196 | if (!pPage) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1197 | return false; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1198 | *width = pPage->GetPageWidth(); |
| 1199 | *height = pPage->GetPageHeight(); |
| 1200 | #else // PDF_ENABLE_XFA |
| 1201 | CPDF_Dictionary* pDict = pDoc->GetPage(page_index); |
| 1202 | if (!pDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1203 | return false; |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 1204 | |
| 1205 | CPDF_Page page(pDoc, pDict, true); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1206 | *width = page.GetPageWidth(); |
| 1207 | *height = page.GetPageHeight(); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1208 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1209 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1210 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | DLLEXPORT FPDF_BOOL STDCALL |
| 1214 | FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1215 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1216 | if (!pDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 1217 | return true; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1218 | CPDF_ViewerPreferences viewRef(pDoc); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1219 | return viewRef.PrintScaling(); |
| 1220 | } |
| 1221 | |
| 1222 | DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1223 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1224 | if (!pDoc) |
| 1225 | return 1; |
| 1226 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1227 | return viewRef.NumCopies(); |
| 1228 | } |
| 1229 | |
| 1230 | DLLEXPORT FPDF_PAGERANGE STDCALL |
| 1231 | FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1232 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1233 | if (!pDoc) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1234 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1235 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1236 | return viewRef.PrintPageRange(); |
| 1237 | } |
| 1238 | |
| 1239 | DLLEXPORT FPDF_DUPLEXTYPE STDCALL |
| 1240 | FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1241 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1242 | if (!pDoc) |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1243 | return DuplexUndefined; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1244 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1245 | CFX_ByteString duplex = viewRef.Duplex(); |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1246 | if ("Simplex" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1247 | return Simplex; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1248 | if ("DuplexFlipShortEdge" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1249 | return DuplexFlipShortEdge; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1250 | if ("DuplexFlipLongEdge" == duplex) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1251 | return DuplexFlipLongEdge; |
| 1252 | return DuplexUndefined; |
Bo Xu | 9114e83 | 2014-07-14 13:22:47 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
thestig | 04bebfe | 2016-11-04 16:07:25 -0700 | [diff] [blame] | 1255 | DLLEXPORT unsigned long STDCALL FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document, |
| 1256 | FPDF_BYTESTRING key, |
| 1257 | char* buffer, |
| 1258 | unsigned long length) { |
| 1259 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1260 | if (!pDoc) |
| 1261 | return 0; |
| 1262 | |
| 1263 | CPDF_ViewerPreferences viewRef(pDoc); |
| 1264 | CFX_ByteString bsVal; |
| 1265 | if (!viewRef.GenericName(key, &bsVal)) |
| 1266 | return 0; |
| 1267 | |
| 1268 | unsigned long dwStringLen = bsVal.GetLength() + 1; |
| 1269 | if (buffer && length >= dwStringLen) |
| 1270 | memcpy(buffer, bsVal.c_str(), dwStringLen); |
| 1271 | return dwStringLen; |
| 1272 | } |
| 1273 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1274 | DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1275 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1276 | if (!pDoc) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1277 | return 0; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1278 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1279 | CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
| 1280 | if (!pRoot) |
| 1281 | return 0; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1282 | |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1283 | CPDF_NameTree nameTree(pDoc, "Dests"); |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1284 | pdfium::base::CheckedNumeric<FPDF_DWORD> count = nameTree.GetCount(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1285 | CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1286 | if (pDest) |
| 1287 | count += pDest->GetCount(); |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1288 | |
| 1289 | if (!count.IsValid()) |
| 1290 | return 0; |
| 1291 | |
| 1292 | return count.ValueOrDie(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document, |
| 1296 | FPDF_BYTESTRING name) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1297 | if (!name || name[0] == 0) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1298 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1299 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1300 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 1301 | if (!pDoc) |
| 1302 | return nullptr; |
| 1303 | |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1304 | CPDF_NameTree name_tree(pDoc, "Dests"); |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 1305 | return name_tree.LookupNamedDest(pDoc, name); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 1308 | #ifdef PDF_ENABLE_XFA |
thestig | 77d148d | 2016-04-06 06:28:31 -0700 | [diff] [blame] | 1309 | DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Init(FPDF_BSTR* str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1310 | if (!str) |
| 1311 | return -1; |
| 1312 | |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 1313 | memset(str, 0, sizeof(FPDF_BSTR)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1314 | return 0; |
| 1315 | } |
| 1316 | |
thestig | 77d148d | 2016-04-06 06:28:31 -0700 | [diff] [blame] | 1317 | DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Set(FPDF_BSTR* str, |
| 1318 | FPDF_LPCSTR bstr, |
| 1319 | int length) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1320 | if (!str) |
| 1321 | return -1; |
| 1322 | if (!bstr || !length) |
| 1323 | return -1; |
| 1324 | if (length == -1) |
| 1325 | length = FXSYS_strlen(bstr); |
| 1326 | |
| 1327 | if (length == 0) { |
| 1328 | if (str->str) { |
| 1329 | FX_Free(str->str); |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1330 | str->str = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1331 | } |
| 1332 | str->len = 0; |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | if (str->str && str->len < length) |
| 1337 | str->str = FX_Realloc(char, str->str, length + 1); |
| 1338 | else if (!str->str) |
| 1339 | str->str = FX_Alloc(char, length + 1); |
| 1340 | |
| 1341 | str->str[length] = 0; |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 1342 | memcpy(str->str, bstr, length); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1343 | str->len = length; |
| 1344 | |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
thestig | 77d148d | 2016-04-06 06:28:31 -0700 | [diff] [blame] | 1348 | DLLEXPORT FPDF_RESULT STDCALL FPDF_BStr_Clear(FPDF_BSTR* str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1349 | if (!str) |
| 1350 | return -1; |
| 1351 | |
| 1352 | if (str->str) { |
| 1353 | FX_Free(str->str); |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 1354 | str->str = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1355 | } |
| 1356 | str->len = 0; |
| 1357 | return 0; |
| 1358 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 1359 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1360 | |
| 1361 | DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, |
| 1362 | int index, |
| 1363 | void* buffer, |
| 1364 | long* buflen) { |
| 1365 | if (!buffer) |
| 1366 | *buflen = 0; |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1367 | |
| 1368 | if (index < 0) |
| 1369 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1370 | |
Tom Sepez | bf59a07 | 2015-10-21 14:07:23 -0700 | [diff] [blame] | 1371 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1372 | if (!pDoc) |
| 1373 | return nullptr; |
| 1374 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1375 | CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
| 1376 | if (!pRoot) |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 1377 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1378 | |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1379 | CPDF_Object* pDestObj = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1380 | CFX_ByteString bsName; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 1381 | CPDF_NameTree nameTree(pDoc, "Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1382 | int count = nameTree.GetCount(); |
| 1383 | if (index >= count) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1384 | CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1385 | if (!pDest) |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1386 | return nullptr; |
| 1387 | |
| 1388 | pdfium::base::CheckedNumeric<int> checked_count = count; |
| 1389 | checked_count += pDest->GetCount(); |
| 1390 | if (!checked_count.IsValid() || index >= checked_count.ValueOrDie()) |
| 1391 | return nullptr; |
| 1392 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1393 | index -= count; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1394 | int i = 0; |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 1395 | for (const auto& it : *pDest) { |
| 1396 | bsName = it.first; |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 1397 | pDestObj = it.second.get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1398 | if (!pDestObj) |
| 1399 | continue; |
| 1400 | if (i == index) |
| 1401 | break; |
| 1402 | i++; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1403 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1404 | } else { |
Tom Sepez | c4a2b75 | 2017-04-07 13:56:13 -0700 | [diff] [blame] | 1405 | pDestObj = nameTree.LookupValueAndName(index, &bsName); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1406 | } |
| 1407 | if (!pDestObj) |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1408 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 1409 | if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 1410 | pDestObj = pDict->GetArrayFor("D"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1411 | if (!pDestObj) |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1412 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1413 | } |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 1414 | if (!pDestObj->IsArray()) |
| 1415 | return nullptr; |
| 1416 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1417 | CFX_WideString wsName = PDF_DecodeText(bsName); |
| 1418 | CFX_ByteString utf16Name = wsName.UTF16LE_Encode(); |
weili | 47ca692 | 2016-03-31 15:08:27 -0700 | [diff] [blame] | 1419 | int len = utf16Name.GetLength(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1420 | if (!buffer) { |
| 1421 | *buflen = len; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 1422 | } else if (len <= *buflen) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1423 | memcpy(buffer, utf16Name.c_str(), len); |
| 1424 | *buflen = len; |
| 1425 | } else { |
| 1426 | *buflen = -1; |
| 1427 | } |
| 1428 | return (FPDF_DEST)pDestObj; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 1429 | } |