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" |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 11 | #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h" |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 12 | #include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 13 | #include "fpdfsdk/fsdk_define.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 14 | #include "public/fpdf_formfill.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 15 | #include "third_party/base/ptr_util.h" |
Dan Sinclair | 80c4878 | 2017-03-23 12:11:20 -0400 | [diff] [blame^] | 16 | #include "xfa/fxfa/cxfa_ffdocview.h" |
| 17 | #include "xfa/fxfa/cxfa_ffpageview.h" |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 18 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 19 | CPDFXFA_Page::CPDFXFA_Page(CPDFXFA_Context* pContext, int page_index) |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 20 | : m_pXFAPageView(nullptr), |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 21 | m_pContext(pContext), |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 22 | m_iPageIndex(page_index), |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 23 | m_iRef(1) {} |
| 24 | |
tsepez | 44d83f3 | 2016-05-16 14:10:22 -0700 | [diff] [blame] | 25 | CPDFXFA_Page::~CPDFXFA_Page() { |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 26 | if (m_pContext) |
| 27 | m_pContext->RemovePage(this); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 28 | } |
| 29 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 30 | bool CPDFXFA_Page::LoadPDFPage() { |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 31 | if (!m_pContext) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 32 | return false; |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 33 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 34 | CPDF_Document* pPDFDoc = m_pContext->GetPDFDoc(); |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 35 | if (!pPDFDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 36 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 37 | |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 38 | CPDF_Dictionary* pDict = pPDFDoc->GetPage(m_iPageIndex); |
| 39 | if (!pDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 40 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 41 | |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 42 | if (!m_pPDFPage || m_pPDFPage->m_pFormDict != pDict) { |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 43 | m_pPDFPage = pdfium::MakeUnique<CPDF_Page>(pPDFDoc, pDict, true); |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 44 | m_pPDFPage->ParseContent(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 45 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 46 | return true; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 47 | } |
| 48 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 49 | bool CPDFXFA_Page::LoadXFAPageView() { |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 50 | if (!m_pContext) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 51 | return false; |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 52 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 53 | CXFA_FFDoc* pXFADoc = m_pContext->GetXFADoc(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 54 | if (!pXFADoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 55 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 56 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 57 | CXFA_FFDocView* pXFADocView = m_pContext->GetXFADocView(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 58 | if (!pXFADocView) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 59 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 60 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 61 | CXFA_FFPageView* pPageView = pXFADocView->GetPageView(m_iPageIndex); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 62 | if (!pPageView) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 63 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 64 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 65 | m_pXFAPageView = pPageView; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 66 | return true; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 67 | } |
| 68 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 69 | bool CPDFXFA_Page::LoadPage() { |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 70 | if (!m_pContext || m_iPageIndex < 0) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 71 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 72 | |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 73 | switch (m_pContext->GetDocType()) { |
| 74 | case XFA_DocType::PDF: |
| 75 | case XFA_DocType::Static: |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 76 | return LoadPDFPage(); |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 77 | case XFA_DocType::Dynamic: |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 78 | return LoadXFAPageView(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 79 | default: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 80 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 81 | } |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 82 | } |
| 83 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 84 | bool CPDFXFA_Page::LoadPDFPage(CPDF_Dictionary* pageDict) { |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 85 | if (!m_pContext || m_iPageIndex < 0 || !pageDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 86 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 87 | |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 88 | m_pPDFPage = |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 89 | pdfium::MakeUnique<CPDF_Page>(m_pContext->GetPDFDoc(), pageDict, true); |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 90 | m_pPDFPage->ParseContent(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 91 | return true; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 94 | float CPDFXFA_Page::GetPageWidth() const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 95 | if (!m_pPDFPage && !m_pXFAPageView) |
| 96 | return 0.0f; |
| 97 | |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 98 | switch (m_pContext->GetDocType()) { |
| 99 | case XFA_DocType::Dynamic: { |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 100 | if (m_pXFAPageView) |
| 101 | return m_pXFAPageView->GetPageViewRect().width; |
| 102 | break; |
| 103 | } |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 104 | case XFA_DocType::Static: |
| 105 | case XFA_DocType::PDF: { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 106 | if (m_pPDFPage) |
| 107 | return m_pPDFPage->GetPageWidth(); |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 108 | break; |
| 109 | } |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 110 | default: |
| 111 | return 0.0f; |
| 112 | } |
| 113 | |
| 114 | return 0.0f; |
| 115 | } |
| 116 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 117 | float CPDFXFA_Page::GetPageHeight() const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 118 | if (!m_pPDFPage && !m_pXFAPageView) |
| 119 | return 0.0f; |
| 120 | |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 121 | switch (m_pContext->GetDocType()) { |
| 122 | case XFA_DocType::PDF: |
| 123 | case XFA_DocType::Static: { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 124 | if (m_pPDFPage) |
| 125 | return m_pPDFPage->GetPageHeight(); |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 126 | break; |
| 127 | } |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 128 | case XFA_DocType::Dynamic: { |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 129 | if (m_pXFAPageView) |
| 130 | return m_pXFAPageView->GetPageViewRect().height; |
| 131 | break; |
| 132 | } |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 133 | default: |
| 134 | return 0.0f; |
| 135 | } |
| 136 | |
| 137 | return 0.0f; |
| 138 | } |
| 139 | |
| 140 | void CPDFXFA_Page::DeviceToPage(int start_x, |
| 141 | int start_y, |
| 142 | int size_x, |
| 143 | int size_y, |
| 144 | int rotate, |
| 145 | int device_x, |
| 146 | int device_y, |
| 147 | double* page_x, |
| 148 | double* page_y) { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 149 | if (!m_pPDFPage && !m_pXFAPageView) |
| 150 | return; |
| 151 | |
Dan Sinclair | bba2a7c | 2017-02-07 16:36:39 -0500 | [diff] [blame] | 152 | CFX_Matrix device2page; |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 153 | device2page.SetReverse( |
| 154 | GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate)); |
Dan Sinclair | afb4456 | 2017-02-09 13:07:43 -0500 | [diff] [blame] | 155 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 156 | CFX_PointF pos = device2page.Transform( |
| 157 | CFX_PointF(static_cast<float>(device_x), static_cast<float>(device_y))); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 158 | |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 159 | *page_x = pos.x; |
| 160 | *page_y = pos.y; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void CPDFXFA_Page::PageToDevice(int start_x, |
| 164 | int start_y, |
| 165 | int size_x, |
| 166 | int size_y, |
| 167 | int rotate, |
| 168 | double page_x, |
| 169 | double page_y, |
| 170 | int* device_x, |
| 171 | int* device_y) { |
| 172 | if (!m_pPDFPage && !m_pXFAPageView) |
| 173 | return; |
| 174 | |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 175 | CFX_Matrix page2device = |
| 176 | GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate); |
Dan Sinclair | afb4456 | 2017-02-09 13:07:43 -0500 | [diff] [blame] | 177 | |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 178 | CFX_PointF pos = page2device.Transform( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 179 | CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y))); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 180 | |
Dan Sinclair | a0061af | 2017-02-23 09:25:17 -0500 | [diff] [blame] | 181 | *device_x = FXSYS_round(pos.x); |
| 182 | *device_y = FXSYS_round(pos.y); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 185 | CFX_Matrix CPDFXFA_Page::GetDisplayMatrix(int xPos, |
| 186 | int yPos, |
| 187 | int xSize, |
| 188 | int ySize, |
| 189 | int iRotate) const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 190 | if (!m_pPDFPage && !m_pXFAPageView) |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 191 | return CFX_Matrix(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 192 | |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 193 | switch (m_pContext->GetDocType()) { |
| 194 | case XFA_DocType::Dynamic: { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 195 | if (m_pXFAPageView) { |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 196 | return m_pXFAPageView->GetDisplayMatrix( |
| 197 | CFX_Rect(xPos, yPos, xSize, ySize), iRotate); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 198 | } |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 199 | break; |
| 200 | } |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 201 | case XFA_DocType::PDF: |
| 202 | case XFA_DocType::Static: { |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 203 | if (m_pPDFPage) |
| 204 | return m_pPDFPage->GetDisplayMatrix(xPos, yPos, xSize, ySize, iRotate); |
| 205 | break; |
| 206 | } |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 207 | default: |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 208 | return CFX_Matrix(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 209 | } |
Dan Sinclair | 1b08df1 | 2017-02-09 09:17:20 -0500 | [diff] [blame] | 210 | return CFX_Matrix(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 211 | } |