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 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 25 | CPDF_Page* CPDFXFA_Page::AsPDFPage() { |
| 26 | return m_pPDFPage.Get(); |
| 27 | } |
| 28 | |
| 29 | CPDFXFA_Page* CPDFXFA_Page::AsXFAPage() { |
| 30 | return this; |
| 31 | } |
| 32 | |
| 33 | CPDF_Document* CPDFXFA_Page::GetDocument() const { |
| 34 | return GetDocumentExtension()->GetPDFDoc(); |
| 35 | } |
| 36 | |
Henrique Nakashima | 7b094f8 | 2018-05-08 20:32:08 +0000 | [diff] [blame] | 37 | bool CPDFXFA_Page::LoadPDFPage() { |
| 38 | if (!m_pContext) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 39 | return false; |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 40 | |
Henrique Nakashima | 7b094f8 | 2018-05-08 20:32:08 +0000 | [diff] [blame] | 41 | CPDF_Document* pPDFDoc = m_pContext->GetPDFDoc(); |
| 42 | if (!pPDFDoc) |
| 43 | return false; |
| 44 | |
| 45 | CPDF_Dictionary* pDict = pPDFDoc->GetPageDictionary(m_iPageIndex); |
| 46 | if (!pDict) |
| 47 | return false; |
| 48 | |
Henrique Nakashima | 888af47 | 2018-06-07 19:43:42 +0000 | [diff] [blame] | 49 | if (!m_pPDFPage || m_pPDFPage->GetDict() != pDict) { |
Tom Sepez | f06ed6d | 2018-06-04 18:26:07 +0000 | [diff] [blame] | 50 | m_pPDFPage = pdfium::MakeRetain<CPDF_Page>(pPDFDoc, pDict, true); |
| 51 | m_pPDFPage->ParseContent(); |
| 52 | } |
Henrique Nakashima | 7b094f8 | 2018-05-08 20:32:08 +0000 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | |
| 56 | bool CPDFXFA_Page::LoadXFAPageView() { |
| 57 | if (!m_pContext) |
| 58 | return false; |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 59 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 60 | CXFA_FFDoc* pXFADoc = m_pContext->GetXFADoc(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 61 | if (!pXFADoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 62 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 63 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 64 | CXFA_FFDocView* pXFADocView = m_pContext->GetXFADocView(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 65 | if (!pXFADocView) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 66 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 67 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 68 | CXFA_FFPageView* pPageView = pXFADocView->GetPageView(m_iPageIndex); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 69 | if (!pPageView) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 70 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 71 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 72 | m_pXFAPageView = pPageView; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 73 | return true; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Henrique Nakashima | 7b094f8 | 2018-05-08 20:32:08 +0000 | [diff] [blame] | 76 | bool CPDFXFA_Page::LoadPage() { |
| 77 | if (!m_pContext || m_iPageIndex < 0) |
| 78 | return false; |
| 79 | |
| 80 | switch (m_pContext->GetFormType()) { |
| 81 | case FormType::kNone: |
| 82 | case FormType::kAcroForm: |
| 83 | case FormType::kXFAForeground: |
| 84 | return LoadPDFPage(); |
| 85 | case FormType::kXFAFull: |
| 86 | return LoadXFAPageView(); |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | bool CPDFXFA_Page::LoadPDFPage(CPDF_Dictionary* pageDict) { |
| 92 | if (!m_pContext || m_iPageIndex < 0 || !pageDict) |
| 93 | return false; |
| 94 | |
Tom Sepez | f06ed6d | 2018-06-04 18:26:07 +0000 | [diff] [blame] | 95 | m_pPDFPage = |
| 96 | pdfium::MakeRetain<CPDF_Page>(m_pContext->GetPDFDoc(), pageDict, true); |
| 97 | m_pPDFPage->ParseContent(); |
Henrique Nakashima | 7b094f8 | 2018-05-08 20:32:08 +0000 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
Tom Sepez | ccd9421 | 2018-05-03 13:53:02 +0000 | [diff] [blame] | 101 | CPDF_Document::Extension* CPDFXFA_Page::GetDocumentExtension() const { |
| 102 | return m_pContext.Get(); |
| 103 | } |
| 104 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 105 | float CPDFXFA_Page::GetPageWidth() const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 106 | if (!m_pPDFPage && !m_pXFAPageView) |
| 107 | return 0.0f; |
| 108 | |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 109 | switch (m_pContext->GetFormType()) { |
| 110 | case FormType::kNone: |
| 111 | case FormType::kAcroForm: |
| 112 | case FormType::kXFAForeground: |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 113 | if (m_pPDFPage) |
| 114 | return m_pPDFPage->GetPageWidth(); |
Lei Zhang | db3c6ce | 2018-05-17 02:01:42 +0000 | [diff] [blame] | 115 | FALLTHROUGH; |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 116 | case FormType::kXFAFull: |
| 117 | if (m_pXFAPageView) |
| 118 | return m_pXFAPageView->GetPageViewRect().width; |
Henrique Nakashima | 067a44f | 2018-02-16 03:46:28 +0000 | [diff] [blame] | 119 | break; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return 0.0f; |
| 123 | } |
| 124 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 125 | float CPDFXFA_Page::GetPageHeight() const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 126 | if (!m_pPDFPage && !m_pXFAPageView) |
| 127 | return 0.0f; |
| 128 | |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 129 | switch (m_pContext->GetFormType()) { |
| 130 | case FormType::kNone: |
| 131 | case FormType::kAcroForm: |
| 132 | case FormType::kXFAForeground: |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 133 | if (m_pPDFPage) |
| 134 | return m_pPDFPage->GetPageHeight(); |
Lei Zhang | db3c6ce | 2018-05-17 02:01:42 +0000 | [diff] [blame] | 135 | FALLTHROUGH; |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 136 | case FormType::kXFAFull: |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 137 | if (m_pXFAPageView) |
| 138 | return m_pXFAPageView->GetPageViewRect().height; |
Henrique Nakashima | 067a44f | 2018-02-16 03:46:28 +0000 | [diff] [blame] | 139 | break; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return 0.0f; |
| 143 | } |
| 144 | |
Lei Zhang | 822886b | 2018-04-12 17:24:45 +0000 | [diff] [blame] | 145 | Optional<CFX_PointF> CPDFXFA_Page::DeviceToPage( |
| 146 | const FX_RECT& rect, |
| 147 | int rotate, |
| 148 | const CFX_PointF& device_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 | a105fa1 | 2018-04-12 16:23:01 +0000 | [diff] [blame] | 152 | CFX_PointF pos = |
| 153 | GetDisplayMatrix(rect, rotate).GetInverse().Transform(device_point); |
Lei Zhang | 822886b | 2018-04-12 17:24:45 +0000 | [diff] [blame] | 154 | return pos; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Lei Zhang | a8db06a | 2018-04-12 17:53:15 +0000 | [diff] [blame] | 157 | Optional<CFX_PointF> CPDFXFA_Page::PageToDevice( |
| 158 | const FX_RECT& rect, |
| 159 | int rotate, |
| 160 | const CFX_PointF& page_point) const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 161 | if (!m_pPDFPage && !m_pXFAPageView) |
Lei Zhang | 822886b | 2018-04-12 17:24:45 +0000 | [diff] [blame] | 162 | return {}; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 163 | |
Lei Zhang | c4242b2 | 2018-04-12 15:50:49 +0000 | [diff] [blame] | 164 | CFX_Matrix page2device = GetDisplayMatrix(rect, rotate); |
Lei Zhang | a8db06a | 2018-04-12 17:53:15 +0000 | [diff] [blame] | 165 | return page2device.Transform(page_point); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Lei Zhang | c4242b2 | 2018-04-12 15:50:49 +0000 | [diff] [blame] | 168 | CFX_Matrix CPDFXFA_Page::GetDisplayMatrix(const FX_RECT& rect, |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 169 | int iRotate) const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 170 | if (!m_pPDFPage && !m_pXFAPageView) |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 171 | return CFX_Matrix(); |
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 | switch (m_pContext->GetFormType()) { |
| 174 | case FormType::kNone: |
| 175 | case FormType::kAcroForm: |
| 176 | case FormType::kXFAForeground: |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 177 | if (m_pPDFPage) |
Lei Zhang | c4242b2 | 2018-04-12 15:50:49 +0000 | [diff] [blame] | 178 | return m_pPDFPage->GetDisplayMatrix(rect, iRotate); |
Lei Zhang | db3c6ce | 2018-05-17 02:01:42 +0000 | [diff] [blame] | 179 | FALLTHROUGH; |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 180 | case FormType::kXFAFull: |
Lei Zhang | c4242b2 | 2018-04-12 15:50:49 +0000 | [diff] [blame] | 181 | if (m_pXFAPageView) |
Lei Zhang | 7d865b6 | 2018-04-10 19:29:25 +0000 | [diff] [blame] | 182 | return m_pXFAPageView->GetDisplayMatrix(rect, iRotate); |
Henrique Nakashima | 067a44f | 2018-02-16 03:46:28 +0000 | [diff] [blame] | 183 | break; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 184 | } |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 185 | |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 186 | return CFX_Matrix(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 187 | } |