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" |
dsinclair | 5b49309 | 2016-09-29 20:20:24 -0700 | [diff] [blame] | 16 | #include "xfa/fxfa/xfa_ffdocview.h" |
| 17 | #include "xfa/fxfa/xfa_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 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 73 | int iDocType = m_pContext->GetDocType(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 74 | switch (iDocType) { |
| 75 | case DOCTYPE_PDF: |
| 76 | case DOCTYPE_STATIC_XFA: { |
| 77 | return LoadPDFPage(); |
| 78 | } |
| 79 | case DOCTYPE_DYNAMIC_XFA: { |
| 80 | return LoadXFAPageView(); |
| 81 | } |
| 82 | default: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 83 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 84 | } |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 85 | } |
| 86 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 87 | bool CPDFXFA_Page::LoadPDFPage(CPDF_Dictionary* pageDict) { |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 88 | if (!m_pContext || m_iPageIndex < 0 || !pageDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 89 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 90 | |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 91 | m_pPDFPage = |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 92 | pdfium::MakeUnique<CPDF_Page>(m_pContext->GetPDFDoc(), pageDict, true); |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 93 | m_pPDFPage->ParseContent(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 94 | return true; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 95 | } |
| 96 | |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 97 | FX_FLOAT CPDFXFA_Page::GetPageWidth() const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 98 | if (!m_pPDFPage && !m_pXFAPageView) |
| 99 | return 0.0f; |
| 100 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 101 | int nDocType = m_pContext->GetDocType(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 102 | switch (nDocType) { |
| 103 | case DOCTYPE_DYNAMIC_XFA: { |
| 104 | if (m_pXFAPageView) { |
| 105 | CFX_RectF rect; |
| 106 | m_pXFAPageView->GetPageViewRect(rect); |
| 107 | return rect.width; |
| 108 | } |
| 109 | } break; |
| 110 | case DOCTYPE_STATIC_XFA: |
| 111 | case DOCTYPE_PDF: { |
| 112 | if (m_pPDFPage) |
| 113 | return m_pPDFPage->GetPageWidth(); |
| 114 | } break; |
| 115 | default: |
| 116 | return 0.0f; |
| 117 | } |
| 118 | |
| 119 | return 0.0f; |
| 120 | } |
| 121 | |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 122 | FX_FLOAT CPDFXFA_Page::GetPageHeight() const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 123 | if (!m_pPDFPage && !m_pXFAPageView) |
| 124 | return 0.0f; |
| 125 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 126 | int nDocType = m_pContext->GetDocType(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 127 | switch (nDocType) { |
| 128 | case DOCTYPE_PDF: |
| 129 | case DOCTYPE_STATIC_XFA: { |
| 130 | if (m_pPDFPage) |
| 131 | return m_pPDFPage->GetPageHeight(); |
| 132 | } break; |
| 133 | case DOCTYPE_DYNAMIC_XFA: { |
| 134 | if (m_pXFAPageView) { |
| 135 | CFX_RectF rect; |
| 136 | m_pXFAPageView->GetPageViewRect(rect); |
| 137 | return rect.height; |
| 138 | } |
| 139 | } break; |
| 140 | default: |
| 141 | return 0.0f; |
| 142 | } |
| 143 | |
| 144 | return 0.0f; |
| 145 | } |
| 146 | |
| 147 | void CPDFXFA_Page::DeviceToPage(int start_x, |
| 148 | int start_y, |
| 149 | int size_x, |
| 150 | int size_y, |
| 151 | int rotate, |
| 152 | int device_x, |
| 153 | int device_y, |
| 154 | double* page_x, |
| 155 | double* page_y) { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 156 | if (!m_pPDFPage && !m_pXFAPageView) |
| 157 | return; |
| 158 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 159 | FX_FLOAT page_x_f, page_y_f; |
| 160 | |
Dan Sinclair | bba2a7c | 2017-02-07 16:36:39 -0500 | [diff] [blame^] | 161 | CFX_Matrix page2device; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 162 | GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate); |
| 163 | |
Dan Sinclair | bba2a7c | 2017-02-07 16:36:39 -0500 | [diff] [blame^] | 164 | CFX_Matrix device2page; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 165 | device2page.SetReverse(page2device); |
| 166 | device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f, |
| 167 | page_y_f); |
| 168 | |
| 169 | *page_x = (page_x_f); |
| 170 | *page_y = (page_y_f); |
| 171 | } |
| 172 | |
| 173 | void CPDFXFA_Page::PageToDevice(int start_x, |
| 174 | int start_y, |
| 175 | int size_x, |
| 176 | int size_y, |
| 177 | int rotate, |
| 178 | double page_x, |
| 179 | double page_y, |
| 180 | int* device_x, |
| 181 | int* device_y) { |
| 182 | if (!m_pPDFPage && !m_pXFAPageView) |
| 183 | return; |
| 184 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 185 | FX_FLOAT device_x_f, device_y_f; |
| 186 | |
Dan Sinclair | bba2a7c | 2017-02-07 16:36:39 -0500 | [diff] [blame^] | 187 | CFX_Matrix page2device; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 188 | GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate); |
| 189 | |
| 190 | page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f, |
| 191 | device_y_f); |
| 192 | |
| 193 | *device_x = FXSYS_round(device_x_f); |
| 194 | *device_y = FXSYS_round(device_y_f); |
| 195 | } |
| 196 | |
| 197 | void CPDFXFA_Page::GetDisplayMatrix(CFX_Matrix& matrix, |
| 198 | int xPos, |
| 199 | int yPos, |
| 200 | int xSize, |
| 201 | int ySize, |
| 202 | int iRotate) const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 203 | if (!m_pPDFPage && !m_pXFAPageView) |
| 204 | return; |
| 205 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 206 | int nDocType = m_pContext->GetDocType(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 207 | switch (nDocType) { |
| 208 | case DOCTYPE_DYNAMIC_XFA: { |
| 209 | if (m_pXFAPageView) { |
Dan Sinclair | bba2a7c | 2017-02-07 16:36:39 -0500 | [diff] [blame^] | 210 | CFX_Rect rect(xPos, yPos, xSize, ySize); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 211 | m_pXFAPageView->GetDisplayMatrix(matrix, rect, iRotate); |
| 212 | } |
| 213 | } break; |
| 214 | case DOCTYPE_PDF: |
| 215 | case DOCTYPE_STATIC_XFA: { |
| 216 | if (m_pPDFPage) { |
| 217 | m_pPDFPage->GetDisplayMatrix(matrix, xPos, yPos, xSize, ySize, iRotate); |
| 218 | } |
| 219 | } break; |
| 220 | default: |
| 221 | return; |
| 222 | } |
| 223 | } |