blob: e5ab9d357f9ca940075c178942d36ce9bac88a8b [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"
Dan Sinclair80c48782017-03-23 12:11:20 -040016#include "xfa/fxfa/cxfa_ffdocview.h"
17#include "xfa/fxfa/cxfa_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
Dan Sinclaircdba7472017-03-23 09:17:10 -040073 switch (m_pContext->GetDocType()) {
74 case XFA_DocType::PDF:
75 case XFA_DocType::Static:
Lei Zhang94293682016-01-27 18:27:56 -080076 return LoadPDFPage();
Dan Sinclaircdba7472017-03-23 09:17:10 -040077 case XFA_DocType::Dynamic:
Lei Zhang94293682016-01-27 18:27:56 -080078 return LoadXFAPageView();
Lei Zhang94293682016-01-27 18:27:56 -080079 default:
tsepez4cf55152016-11-02 14:37:54 -070080 return false;
Lei Zhang94293682016-01-27 18:27:56 -080081 }
Lei Zhang94293682016-01-27 18:27:56 -080082}
83
tsepez4cf55152016-11-02 14:37:54 -070084bool CPDFXFA_Page::LoadPDFPage(CPDF_Dictionary* pageDict) {
dsinclair521b7502016-11-02 13:02:28 -070085 if (!m_pContext || m_iPageIndex < 0 || !pageDict)
tsepez4cf55152016-11-02 14:37:54 -070086 return false;
Lei Zhang94293682016-01-27 18:27:56 -080087
thestigd4c34f22016-09-28 17:04:51 -070088 m_pPDFPage =
dsinclair521b7502016-11-02 13:02:28 -070089 pdfium::MakeUnique<CPDF_Page>(m_pContext->GetPDFDoc(), pageDict, true);
thestig5cc24652016-04-26 11:46:02 -070090 m_pPDFPage->ParseContent();
tsepez4cf55152016-11-02 14:37:54 -070091 return true;
Lei Zhang94293682016-01-27 18:27:56 -080092}
93
Dan Sinclair05df0752017-03-14 14:43:42 -040094float CPDFXFA_Page::GetPageWidth() const {
Lei Zhang94293682016-01-27 18:27:56 -080095 if (!m_pPDFPage && !m_pXFAPageView)
96 return 0.0f;
97
Dan Sinclaircdba7472017-03-23 09:17:10 -040098 switch (m_pContext->GetDocType()) {
99 case XFA_DocType::Dynamic: {
Dan Sinclair1b08df12017-02-09 09:17:20 -0500100 if (m_pXFAPageView)
101 return m_pXFAPageView->GetPageViewRect().width;
102 break;
103 }
Dan Sinclaircdba7472017-03-23 09:17:10 -0400104 case XFA_DocType::Static:
105 case XFA_DocType::PDF: {
Lei Zhang94293682016-01-27 18:27:56 -0800106 if (m_pPDFPage)
107 return m_pPDFPage->GetPageWidth();
Dan Sinclair1b08df12017-02-09 09:17:20 -0500108 break;
109 }
Lei Zhang94293682016-01-27 18:27:56 -0800110 default:
111 return 0.0f;
112 }
113
114 return 0.0f;
115}
116
Dan Sinclair05df0752017-03-14 14:43:42 -0400117float CPDFXFA_Page::GetPageHeight() const {
Lei Zhang94293682016-01-27 18:27:56 -0800118 if (!m_pPDFPage && !m_pXFAPageView)
119 return 0.0f;
120
Dan Sinclaircdba7472017-03-23 09:17:10 -0400121 switch (m_pContext->GetDocType()) {
122 case XFA_DocType::PDF:
123 case XFA_DocType::Static: {
Lei Zhang94293682016-01-27 18:27:56 -0800124 if (m_pPDFPage)
125 return m_pPDFPage->GetPageHeight();
Dan Sinclair1b08df12017-02-09 09:17:20 -0500126 break;
127 }
Dan Sinclaircdba7472017-03-23 09:17:10 -0400128 case XFA_DocType::Dynamic: {
Dan Sinclair1b08df12017-02-09 09:17:20 -0500129 if (m_pXFAPageView)
130 return m_pXFAPageView->GetPageViewRect().height;
131 break;
132 }
Lei Zhang94293682016-01-27 18:27:56 -0800133 default:
134 return 0.0f;
135 }
136
137 return 0.0f;
138}
139
140void 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 Zhang94293682016-01-27 18:27:56 -0800149 if (!m_pPDFPage && !m_pXFAPageView)
150 return;
151
Dan Sinclairbba2a7c2017-02-07 16:36:39 -0500152 CFX_Matrix device2page;
Dan Sinclair1b08df12017-02-09 09:17:20 -0500153 device2page.SetReverse(
154 GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate));
Dan Sinclairafb44562017-02-09 13:07:43 -0500155
Dan Sinclair05df0752017-03-14 14:43:42 -0400156 CFX_PointF pos = device2page.Transform(
157 CFX_PointF(static_cast<float>(device_x), static_cast<float>(device_y)));
Lei Zhang94293682016-01-27 18:27:56 -0800158
Dan Sinclaira0061af2017-02-23 09:25:17 -0500159 *page_x = pos.x;
160 *page_y = pos.y;
Lei Zhang94293682016-01-27 18:27:56 -0800161}
162
163void 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 Sinclair1b08df12017-02-09 09:17:20 -0500175 CFX_Matrix page2device =
176 GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
Dan Sinclairafb44562017-02-09 13:07:43 -0500177
Dan Sinclaira0061af2017-02-23 09:25:17 -0500178 CFX_PointF pos = page2device.Transform(
Dan Sinclair05df0752017-03-14 14:43:42 -0400179 CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)));
Lei Zhang94293682016-01-27 18:27:56 -0800180
Dan Sinclaira0061af2017-02-23 09:25:17 -0500181 *device_x = FXSYS_round(pos.x);
182 *device_y = FXSYS_round(pos.y);
Lei Zhang94293682016-01-27 18:27:56 -0800183}
184
Dan Sinclair1b08df12017-02-09 09:17:20 -0500185CFX_Matrix CPDFXFA_Page::GetDisplayMatrix(int xPos,
186 int yPos,
187 int xSize,
188 int ySize,
189 int iRotate) const {
Lei Zhang94293682016-01-27 18:27:56 -0800190 if (!m_pPDFPage && !m_pXFAPageView)
Dan Sinclair1b08df12017-02-09 09:17:20 -0500191 return CFX_Matrix();
Lei Zhang94293682016-01-27 18:27:56 -0800192
Dan Sinclaircdba7472017-03-23 09:17:10 -0400193 switch (m_pContext->GetDocType()) {
194 case XFA_DocType::Dynamic: {
Lei Zhang94293682016-01-27 18:27:56 -0800195 if (m_pXFAPageView) {
Dan Sinclair1b08df12017-02-09 09:17:20 -0500196 return m_pXFAPageView->GetDisplayMatrix(
197 CFX_Rect(xPos, yPos, xSize, ySize), iRotate);
Lei Zhang94293682016-01-27 18:27:56 -0800198 }
Dan Sinclair1b08df12017-02-09 09:17:20 -0500199 break;
200 }
Dan Sinclaircdba7472017-03-23 09:17:10 -0400201 case XFA_DocType::PDF:
202 case XFA_DocType::Static: {
Dan Sinclair1b08df12017-02-09 09:17:20 -0500203 if (m_pPDFPage)
204 return m_pPDFPage->GetDisplayMatrix(xPos, yPos, xSize, ySize, iRotate);
205 break;
206 }
Lei Zhang94293682016-01-27 18:27:56 -0800207 default:
Dan Sinclair1b08df12017-02-09 09:17:20 -0500208 return CFX_Matrix();
Lei Zhang94293682016-01-27 18:27:56 -0800209 }
Dan Sinclair1b08df12017-02-09 09:17:20 -0500210 return CFX_Matrix();
Lei Zhang94293682016-01-27 18:27:56 -0800211}