blob: 8011295f962b3afdb5cc0ffe0300ce34b6c379d7 [file] [log] [blame]
Lei Zhang94293682016-01-27 18:27:56 -08001// 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
dsinclair4d29e782016-10-04 14:02:47 -07007#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -04008
dsinclair41872fa2016-10-04 11:29:35 -07009#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070010#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair521b7502016-11-02 13:02:28 -070011#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070012#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
dsinclair114e46a2016-09-29 17:18:21 -070013#include "fpdfsdk/fsdk_define.h"
dsinclairf34518b2016-09-13 12:03:48 -070014#include "public/fpdf_formfill.h"
tsepez36eb4bd2016-10-03 15:24:27 -070015#include "third_party/base/ptr_util.h"
dsinclair5b493092016-09-29 20:20:24 -070016#include "xfa/fxfa/xfa_ffdocview.h"
17#include "xfa/fxfa/xfa_ffpageview.h"
Lei Zhang94293682016-01-27 18:27:56 -080018
dsinclair521b7502016-11-02 13:02:28 -070019CPDFXFA_Page::CPDFXFA_Page(CPDFXFA_Context* pContext, int page_index)
thestig9f3dbbc2016-04-13 13:18:21 -070020 : m_pXFAPageView(nullptr),
dsinclair521b7502016-11-02 13:02:28 -070021 m_pContext(pContext),
thestig9f3dbbc2016-04-13 13:18:21 -070022 m_iPageIndex(page_index),
Lei Zhang94293682016-01-27 18:27:56 -080023 m_iRef(1) {}
24
tsepez44d83f32016-05-16 14:10:22 -070025CPDFXFA_Page::~CPDFXFA_Page() {
dsinclair521b7502016-11-02 13:02:28 -070026 if (m_pContext)
27 m_pContext->RemovePage(this);
Lei Zhang94293682016-01-27 18:27:56 -080028}
29
tsepez4cf55152016-11-02 14:37:54 -070030bool CPDFXFA_Page::LoadPDFPage() {
dsinclair521b7502016-11-02 13:02:28 -070031 if (!m_pContext)
tsepez4cf55152016-11-02 14:37:54 -070032 return false;
thestig9f3dbbc2016-04-13 13:18:21 -070033
dsinclair521b7502016-11-02 13:02:28 -070034 CPDF_Document* pPDFDoc = m_pContext->GetPDFDoc();
thestig9f3dbbc2016-04-13 13:18:21 -070035 if (!pPDFDoc)
tsepez4cf55152016-11-02 14:37:54 -070036 return false;
Lei Zhang94293682016-01-27 18:27:56 -080037
thestig9f3dbbc2016-04-13 13:18:21 -070038 CPDF_Dictionary* pDict = pPDFDoc->GetPage(m_iPageIndex);
39 if (!pDict)
tsepez4cf55152016-11-02 14:37:54 -070040 return false;
Lei Zhang94293682016-01-27 18:27:56 -080041
thestig9f3dbbc2016-04-13 13:18:21 -070042 if (!m_pPDFPage || m_pPDFPage->m_pFormDict != pDict) {
tsepez36eb4bd2016-10-03 15:24:27 -070043 m_pPDFPage = pdfium::MakeUnique<CPDF_Page>(pPDFDoc, pDict, true);
thestig5cc24652016-04-26 11:46:02 -070044 m_pPDFPage->ParseContent();
Lei Zhang94293682016-01-27 18:27:56 -080045 }
tsepez4cf55152016-11-02 14:37:54 -070046 return true;
Lei Zhang94293682016-01-27 18:27:56 -080047}
48
tsepez4cf55152016-11-02 14:37:54 -070049bool CPDFXFA_Page::LoadXFAPageView() {
dsinclair521b7502016-11-02 13:02:28 -070050 if (!m_pContext)
tsepez4cf55152016-11-02 14:37:54 -070051 return false;
thestig9f3dbbc2016-04-13 13:18:21 -070052
dsinclair521b7502016-11-02 13:02:28 -070053 CXFA_FFDoc* pXFADoc = m_pContext->GetXFADoc();
Lei Zhang94293682016-01-27 18:27:56 -080054 if (!pXFADoc)
tsepez4cf55152016-11-02 14:37:54 -070055 return false;
Lei Zhang94293682016-01-27 18:27:56 -080056
dsinclair521b7502016-11-02 13:02:28 -070057 CXFA_FFDocView* pXFADocView = m_pContext->GetXFADocView();
Lei Zhang94293682016-01-27 18:27:56 -080058 if (!pXFADocView)
tsepez4cf55152016-11-02 14:37:54 -070059 return false;
Lei Zhang94293682016-01-27 18:27:56 -080060
dsinclairdf4bc592016-03-31 20:34:43 -070061 CXFA_FFPageView* pPageView = pXFADocView->GetPageView(m_iPageIndex);
Lei Zhang94293682016-01-27 18:27:56 -080062 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -070063 return false;
Lei Zhang94293682016-01-27 18:27:56 -080064
Lei Zhang94293682016-01-27 18:27:56 -080065 m_pXFAPageView = pPageView;
tsepez4cf55152016-11-02 14:37:54 -070066 return true;
Lei Zhang94293682016-01-27 18:27:56 -080067}
68
tsepez4cf55152016-11-02 14:37:54 -070069bool CPDFXFA_Page::LoadPage() {
dsinclair521b7502016-11-02 13:02:28 -070070 if (!m_pContext || m_iPageIndex < 0)
tsepez4cf55152016-11-02 14:37:54 -070071 return false;
Lei Zhang94293682016-01-27 18:27:56 -080072
dsinclair521b7502016-11-02 13:02:28 -070073 int iDocType = m_pContext->GetDocType();
Lei Zhang94293682016-01-27 18:27:56 -080074 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:
tsepez4cf55152016-11-02 14:37:54 -070083 return false;
Lei Zhang94293682016-01-27 18:27:56 -080084 }
Lei Zhang94293682016-01-27 18:27:56 -080085}
86
tsepez4cf55152016-11-02 14:37:54 -070087bool CPDFXFA_Page::LoadPDFPage(CPDF_Dictionary* pageDict) {
dsinclair521b7502016-11-02 13:02:28 -070088 if (!m_pContext || m_iPageIndex < 0 || !pageDict)
tsepez4cf55152016-11-02 14:37:54 -070089 return false;
Lei Zhang94293682016-01-27 18:27:56 -080090
thestigd4c34f22016-09-28 17:04:51 -070091 m_pPDFPage =
dsinclair521b7502016-11-02 13:02:28 -070092 pdfium::MakeUnique<CPDF_Page>(m_pContext->GetPDFDoc(), pageDict, true);
thestig5cc24652016-04-26 11:46:02 -070093 m_pPDFPage->ParseContent();
tsepez4cf55152016-11-02 14:37:54 -070094 return true;
Lei Zhang94293682016-01-27 18:27:56 -080095}
96
thestig9f3dbbc2016-04-13 13:18:21 -070097FX_FLOAT CPDFXFA_Page::GetPageWidth() const {
Lei Zhang94293682016-01-27 18:27:56 -080098 if (!m_pPDFPage && !m_pXFAPageView)
99 return 0.0f;
100
dsinclair521b7502016-11-02 13:02:28 -0700101 int nDocType = m_pContext->GetDocType();
Lei Zhang94293682016-01-27 18:27:56 -0800102 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
thestig9f3dbbc2016-04-13 13:18:21 -0700122FX_FLOAT CPDFXFA_Page::GetPageHeight() const {
Lei Zhang94293682016-01-27 18:27:56 -0800123 if (!m_pPDFPage && !m_pXFAPageView)
124 return 0.0f;
125
dsinclair521b7502016-11-02 13:02:28 -0700126 int nDocType = m_pContext->GetDocType();
Lei Zhang94293682016-01-27 18:27:56 -0800127 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
147void 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 Zhang94293682016-01-27 18:27:56 -0800156 if (!m_pPDFPage && !m_pXFAPageView)
157 return;
158
Lei Zhang94293682016-01-27 18:27:56 -0800159 FX_FLOAT page_x_f, page_y_f;
160
Dan Sinclairbba2a7c2017-02-07 16:36:39 -0500161 CFX_Matrix page2device;
Lei Zhang94293682016-01-27 18:27:56 -0800162 GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate);
163
Dan Sinclairbba2a7c2017-02-07 16:36:39 -0500164 CFX_Matrix device2page;
Lei Zhang94293682016-01-27 18:27:56 -0800165 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
173void 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 Zhang94293682016-01-27 18:27:56 -0800185 FX_FLOAT device_x_f, device_y_f;
186
Dan Sinclairbba2a7c2017-02-07 16:36:39 -0500187 CFX_Matrix page2device;
Lei Zhang94293682016-01-27 18:27:56 -0800188 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
197void CPDFXFA_Page::GetDisplayMatrix(CFX_Matrix& matrix,
198 int xPos,
199 int yPos,
200 int xSize,
201 int ySize,
202 int iRotate) const {
Lei Zhang94293682016-01-27 18:27:56 -0800203 if (!m_pPDFPage && !m_pXFAPageView)
204 return;
205
dsinclair521b7502016-11-02 13:02:28 -0700206 int nDocType = m_pContext->GetDocType();
Lei Zhang94293682016-01-27 18:27:56 -0800207 switch (nDocType) {
208 case DOCTYPE_DYNAMIC_XFA: {
209 if (m_pXFAPageView) {
Dan Sinclairbba2a7c2017-02-07 16:36:39 -0500210 CFX_Rect rect(xPos, yPos, xSize, ySize);
Lei Zhang94293682016-01-27 18:27:56 -0800211 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}