Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [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. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/fpdfxfa/cpdfxfa_page.h" |
Dan Sinclair | aa403d3 | 2016-03-15 14:57:22 -0400 | [diff] [blame] | 8 | |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 9 | #include "core/fpdfapi/page/cpdf_page.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 10 | #include "core/fpdfapi/parser/cpdf_document.h" |
Dan Sinclair | 00d47a6 | 2018-03-28 18:39:04 +0000 | [diff] [blame] | 11 | #include "fpdfsdk/cpdfsdk_helpers.h" |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 12 | #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h" |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 13 | #include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 14 | #include "public/fpdf_formfill.h" |
Lei Zhang | db3c6ce | 2018-05-17 02:01:42 +0000 | [diff] [blame^] | 15 | #include "third_party/base/compiler_specific.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 16 | #include "third_party/base/ptr_util.h" |
Dan Sinclair | 80c4878 | 2017-03-23 12:11:20 -0400 | [diff] [blame] | 17 | #include "xfa/fxfa/cxfa_ffdocview.h" |
| 18 | #include "xfa/fxfa/cxfa_ffpageview.h" |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 19 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 20 | CPDFXFA_Page::CPDFXFA_Page(CPDFXFA_Context* pContext, int page_index) |
Henrique Nakashima | 7b094f8 | 2018-05-08 20:32:08 +0000 | [diff] [blame] | 21 | : m_pXFAPageView(nullptr), m_pContext(pContext), m_iPageIndex(page_index) {} |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 22 | |
Tom Sepez | 9792f16 | 2017-05-16 14:11:30 -0700 | [diff] [blame] | 23 | CPDFXFA_Page::~CPDFXFA_Page() {} |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 24 | |
Henrique Nakashima | 7b094f8 | 2018-05-08 20:32:08 +0000 | [diff] [blame] | 25 | bool CPDFXFA_Page::LoadPDFPage() { |
| 26 | if (!m_pContext) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 27 | return false; |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 28 | |
Henrique Nakashima | 7b094f8 | 2018-05-08 20:32:08 +0000 | [diff] [blame] | 29 | CPDF_Document* pPDFDoc = m_pContext->GetPDFDoc(); |
| 30 | if (!pPDFDoc) |
| 31 | return false; |
| 32 | |
| 33 | CPDF_Dictionary* pDict = pPDFDoc->GetPageDictionary(m_iPageIndex); |
| 34 | if (!pDict) |
| 35 | return false; |
| 36 | |
| 37 | if (!m_pPDFPage || m_pPDFPage->GetFormDict() != pDict) { |
| 38 | m_pPDFPage = pdfium::MakeUnique<CPDF_Page>(pPDFDoc, pDict, true); |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 39 | m_pPDFPage->ParseContent(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 40 | } |
Henrique Nakashima | 7b094f8 | 2018-05-08 20:32:08 +0000 | [diff] [blame] | 41 | return true; |
| 42 | } |
| 43 | |
| 44 | bool CPDFXFA_Page::LoadXFAPageView() { |
| 45 | if (!m_pContext) |
| 46 | return false; |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 47 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 48 | CXFA_FFDoc* pXFADoc = m_pContext->GetXFADoc(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 49 | if (!pXFADoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 50 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 51 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 52 | CXFA_FFDocView* pXFADocView = m_pContext->GetXFADocView(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 53 | if (!pXFADocView) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 54 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 55 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 56 | CXFA_FFPageView* pPageView = pXFADocView->GetPageView(m_iPageIndex); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 57 | if (!pPageView) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 58 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 59 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 60 | m_pXFAPageView = pPageView; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 61 | return true; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Henrique Nakashima | 7b094f8 | 2018-05-08 20:32:08 +0000 | [diff] [blame] | 64 | bool CPDFXFA_Page::LoadPage() { |
| 65 | if (!m_pContext || m_iPageIndex < 0) |
| 66 | return false; |
| 67 | |
| 68 | switch (m_pContext->GetFormType()) { |
| 69 | case FormType::kNone: |
| 70 | case FormType::kAcroForm: |
| 71 | case FormType::kXFAForeground: |
| 72 | return LoadPDFPage(); |
| 73 | case FormType::kXFAFull: |
| 74 | return LoadXFAPageView(); |
| 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | bool CPDFXFA_Page::LoadPDFPage(CPDF_Dictionary* pageDict) { |
| 80 | if (!m_pContext || m_iPageIndex < 0 || !pageDict) |
| 81 | return false; |
| 82 | |
| 83 | m_pPDFPage = |
| 84 | pdfium::MakeUnique<CPDF_Page>(m_pContext->GetPDFDoc(), pageDict, true); |
| 85 | m_pPDFPage->ParseContent(); |
| 86 | return true; |
| 87 | } |
| 88 | |
Tom Sepez | ccd9421 | 2018-05-03 13:53:02 +0000 | [diff] [blame] | 89 | CPDF_Document::Extension* CPDFXFA_Page::GetDocumentExtension() const { |
| 90 | return m_pContext.Get(); |
| 91 | } |
| 92 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 93 | float CPDFXFA_Page::GetPageWidth() const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 94 | if (!m_pPDFPage && !m_pXFAPageView) |
| 95 | return 0.0f; |
| 96 | |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 97 | switch (m_pContext->GetFormType()) { |
| 98 | case FormType::kNone: |
| 99 | case FormType::kAcroForm: |
| 100 | case FormType::kXFAForeground: |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 101 | if (m_pPDFPage) |
| 102 | return m_pPDFPage->GetPageWidth(); |
Lei Zhang | db3c6ce | 2018-05-17 02:01:42 +0000 | [diff] [blame^] | 103 | FALLTHROUGH; |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 104 | case FormType::kXFAFull: |
| 105 | if (m_pXFAPageView) |
| 106 | return m_pXFAPageView->GetPageViewRect().width; |
Henrique Nakashima | 067a44f | 2018-02-16 03:46:28 +0000 | [diff] [blame] | 107 | break; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return 0.0f; |
| 111 | } |
| 112 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 113 | float CPDFXFA_Page::GetPageHeight() const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 114 | if (!m_pPDFPage && !m_pXFAPageView) |
| 115 | return 0.0f; |
| 116 | |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 117 | switch (m_pContext->GetFormType()) { |
| 118 | case FormType::kNone: |
| 119 | case FormType::kAcroForm: |
| 120 | case FormType::kXFAForeground: |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 121 | if (m_pPDFPage) |
| 122 | return m_pPDFPage->GetPageHeight(); |
Lei Zhang | db3c6ce | 2018-05-17 02:01:42 +0000 | [diff] [blame^] | 123 | FALLTHROUGH; |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 124 | case FormType::kXFAFull: |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 125 | if (m_pXFAPageView) |
| 126 | return m_pXFAPageView->GetPageViewRect().height; |
Henrique Nakashima | 067a44f | 2018-02-16 03:46:28 +0000 | [diff] [blame] | 127 | break; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return 0.0f; |
| 131 | } |
| 132 | |
Lei Zhang | 822886b | 2018-04-12 17:24:45 +0000 | [diff] [blame] | 133 | Optional<CFX_PointF> CPDFXFA_Page::DeviceToPage( |
| 134 | const FX_RECT& rect, |
| 135 | int rotate, |
| 136 | const CFX_PointF& device_point) const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 137 | if (!m_pPDFPage && !m_pXFAPageView) |
Lei Zhang | 822886b | 2018-04-12 17:24:45 +0000 | [diff] [blame] | 138 | return {}; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 139 | |
Lei Zhang | a105fa1 | 2018-04-12 16:23:01 +0000 | [diff] [blame] | 140 | CFX_PointF pos = |
| 141 | GetDisplayMatrix(rect, rotate).GetInverse().Transform(device_point); |
Lei Zhang | 822886b | 2018-04-12 17:24:45 +0000 | [diff] [blame] | 142 | return pos; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Lei Zhang | a8db06a | 2018-04-12 17:53:15 +0000 | [diff] [blame] | 145 | Optional<CFX_PointF> CPDFXFA_Page::PageToDevice( |
| 146 | const FX_RECT& rect, |
| 147 | int rotate, |
| 148 | const CFX_PointF& page_point) const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 149 | if (!m_pPDFPage && !m_pXFAPageView) |
Lei Zhang | 822886b | 2018-04-12 17:24:45 +0000 | [diff] [blame] | 150 | return {}; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 151 | |
Lei Zhang | c4242b2 | 2018-04-12 15:50:49 +0000 | [diff] [blame] | 152 | CFX_Matrix page2device = GetDisplayMatrix(rect, rotate); |
Lei Zhang | a8db06a | 2018-04-12 17:53:15 +0000 | [diff] [blame] | 153 | return page2device.Transform(page_point); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Lei Zhang | c4242b2 | 2018-04-12 15:50:49 +0000 | [diff] [blame] | 156 | CFX_Matrix CPDFXFA_Page::GetDisplayMatrix(const FX_RECT& rect, |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 157 | int iRotate) const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 158 | if (!m_pPDFPage && !m_pXFAPageView) |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 159 | return CFX_Matrix(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 160 | |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 161 | switch (m_pContext->GetFormType()) { |
| 162 | case FormType::kNone: |
| 163 | case FormType::kAcroForm: |
| 164 | case FormType::kXFAForeground: |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 165 | if (m_pPDFPage) |
Lei Zhang | c4242b2 | 2018-04-12 15:50:49 +0000 | [diff] [blame] | 166 | return m_pPDFPage->GetDisplayMatrix(rect, iRotate); |
Lei Zhang | db3c6ce | 2018-05-17 02:01:42 +0000 | [diff] [blame^] | 167 | FALLTHROUGH; |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 168 | case FormType::kXFAFull: |
Lei Zhang | c4242b2 | 2018-04-12 15:50:49 +0000 | [diff] [blame] | 169 | if (m_pXFAPageView) |
Lei Zhang | 7d865b6 | 2018-04-10 19:29:25 +0000 | [diff] [blame] | 170 | return m_pXFAPageView->GetDisplayMatrix(rect, iRotate); |
Henrique Nakashima | 067a44f | 2018-02-16 03:46:28 +0000 | [diff] [blame] | 171 | break; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 172 | } |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 173 | |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 174 | return CFX_Matrix(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 175 | } |