blob: e4618e3bbedadee5e7d34596ab49ee9f7684f434 [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"
Dan Sinclair00d47a62018-03-28 18:39:04 +000011#include "fpdfsdk/cpdfsdk_helpers.h"
dsinclair521b7502016-11-02 13:02:28 -070012#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070013#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
dsinclairf34518b2016-09-13 12:03:48 -070014#include "public/fpdf_formfill.h"
Lei Zhangdb3c6ce2018-05-17 02:01:42 +000015#include "third_party/base/compiler_specific.h"
tsepez36eb4bd2016-10-03 15:24:27 -070016#include "third_party/base/ptr_util.h"
Dan Sinclair80c48782017-03-23 12:11:20 -040017#include "xfa/fxfa/cxfa_ffdocview.h"
18#include "xfa/fxfa/cxfa_ffpageview.h"
Lei Zhang94293682016-01-27 18:27:56 -080019
dsinclair521b7502016-11-02 13:02:28 -070020CPDFXFA_Page::CPDFXFA_Page(CPDFXFA_Context* pContext, int page_index)
Henrique Nakashima7b094f82018-05-08 20:32:08 +000021 : m_pXFAPageView(nullptr), m_pContext(pContext), m_iPageIndex(page_index) {}
Lei Zhang94293682016-01-27 18:27:56 -080022
Tom Sepez9792f162017-05-16 14:11:30 -070023CPDFXFA_Page::~CPDFXFA_Page() {}
Lei Zhang94293682016-01-27 18:27:56 -080024
Tom Sepez101535f2018-06-12 13:36:05 +000025CPDF_Page* CPDFXFA_Page::AsPDFPage() {
26 return m_pPDFPage.Get();
27}
28
29CPDFXFA_Page* CPDFXFA_Page::AsXFAPage() {
30 return this;
31}
32
33CPDF_Document* CPDFXFA_Page::GetDocument() const {
34 return GetDocumentExtension()->GetPDFDoc();
35}
36
Henrique Nakashima7b094f82018-05-08 20:32:08 +000037bool CPDFXFA_Page::LoadPDFPage() {
38 if (!m_pContext)
tsepez4cf55152016-11-02 14:37:54 -070039 return false;
thestig9f3dbbc2016-04-13 13:18:21 -070040
Henrique Nakashima7b094f82018-05-08 20:32:08 +000041 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 Nakashima888af472018-06-07 19:43:42 +000049 if (!m_pPDFPage || m_pPDFPage->GetDict() != pDict) {
Tom Sepezf06ed6d2018-06-04 18:26:07 +000050 m_pPDFPage = pdfium::MakeRetain<CPDF_Page>(pPDFDoc, pDict, true);
51 m_pPDFPage->ParseContent();
52 }
Henrique Nakashima7b094f82018-05-08 20:32:08 +000053 return true;
54}
55
56bool CPDFXFA_Page::LoadXFAPageView() {
57 if (!m_pContext)
58 return false;
thestig9f3dbbc2016-04-13 13:18:21 -070059
dsinclair521b7502016-11-02 13:02:28 -070060 CXFA_FFDoc* pXFADoc = m_pContext->GetXFADoc();
Lei Zhang94293682016-01-27 18:27:56 -080061 if (!pXFADoc)
tsepez4cf55152016-11-02 14:37:54 -070062 return false;
Lei Zhang94293682016-01-27 18:27:56 -080063
dsinclair521b7502016-11-02 13:02:28 -070064 CXFA_FFDocView* pXFADocView = m_pContext->GetXFADocView();
Lei Zhang94293682016-01-27 18:27:56 -080065 if (!pXFADocView)
tsepez4cf55152016-11-02 14:37:54 -070066 return false;
Lei Zhang94293682016-01-27 18:27:56 -080067
dsinclairdf4bc592016-03-31 20:34:43 -070068 CXFA_FFPageView* pPageView = pXFADocView->GetPageView(m_iPageIndex);
Lei Zhang94293682016-01-27 18:27:56 -080069 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -070070 return false;
Lei Zhang94293682016-01-27 18:27:56 -080071
Lei Zhang94293682016-01-27 18:27:56 -080072 m_pXFAPageView = pPageView;
tsepez4cf55152016-11-02 14:37:54 -070073 return true;
Lei Zhang94293682016-01-27 18:27:56 -080074}
75
Henrique Nakashima7b094f82018-05-08 20:32:08 +000076bool 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
91bool CPDFXFA_Page::LoadPDFPage(CPDF_Dictionary* pageDict) {
92 if (!m_pContext || m_iPageIndex < 0 || !pageDict)
93 return false;
94
Tom Sepezf06ed6d2018-06-04 18:26:07 +000095 m_pPDFPage =
96 pdfium::MakeRetain<CPDF_Page>(m_pContext->GetPDFDoc(), pageDict, true);
97 m_pPDFPage->ParseContent();
Henrique Nakashima7b094f82018-05-08 20:32:08 +000098 return true;
99}
100
Tom Sepezccd94212018-05-03 13:53:02 +0000101CPDF_Document::Extension* CPDFXFA_Page::GetDocumentExtension() const {
102 return m_pContext.Get();
103}
104
Dan Sinclair05df0752017-03-14 14:43:42 -0400105float CPDFXFA_Page::GetPageWidth() const {
Lei Zhang94293682016-01-27 18:27:56 -0800106 if (!m_pPDFPage && !m_pXFAPageView)
107 return 0.0f;
108
Ryan Harrison854d71c2017-10-18 12:28:14 -0400109 switch (m_pContext->GetFormType()) {
110 case FormType::kNone:
111 case FormType::kAcroForm:
112 case FormType::kXFAForeground:
Lei Zhang94293682016-01-27 18:27:56 -0800113 if (m_pPDFPage)
114 return m_pPDFPage->GetPageWidth();
Lei Zhangdb3c6ce2018-05-17 02:01:42 +0000115 FALLTHROUGH;
Ryan Harrison854d71c2017-10-18 12:28:14 -0400116 case FormType::kXFAFull:
117 if (m_pXFAPageView)
118 return m_pXFAPageView->GetPageViewRect().width;
Henrique Nakashima067a44f2018-02-16 03:46:28 +0000119 break;
Lei Zhang94293682016-01-27 18:27:56 -0800120 }
121
122 return 0.0f;
123}
124
Dan Sinclair05df0752017-03-14 14:43:42 -0400125float CPDFXFA_Page::GetPageHeight() const {
Lei Zhang94293682016-01-27 18:27:56 -0800126 if (!m_pPDFPage && !m_pXFAPageView)
127 return 0.0f;
128
Ryan Harrison854d71c2017-10-18 12:28:14 -0400129 switch (m_pContext->GetFormType()) {
130 case FormType::kNone:
131 case FormType::kAcroForm:
132 case FormType::kXFAForeground:
Lei Zhang94293682016-01-27 18:27:56 -0800133 if (m_pPDFPage)
134 return m_pPDFPage->GetPageHeight();
Lei Zhangdb3c6ce2018-05-17 02:01:42 +0000135 FALLTHROUGH;
Ryan Harrison854d71c2017-10-18 12:28:14 -0400136 case FormType::kXFAFull:
Dan Sinclair1b08df12017-02-09 09:17:20 -0500137 if (m_pXFAPageView)
138 return m_pXFAPageView->GetPageViewRect().height;
Henrique Nakashima067a44f2018-02-16 03:46:28 +0000139 break;
Lei Zhang94293682016-01-27 18:27:56 -0800140 }
141
142 return 0.0f;
143}
144
Lei Zhang822886b2018-04-12 17:24:45 +0000145Optional<CFX_PointF> CPDFXFA_Page::DeviceToPage(
146 const FX_RECT& rect,
147 int rotate,
148 const CFX_PointF& device_point) const {
Lei Zhang94293682016-01-27 18:27:56 -0800149 if (!m_pPDFPage && !m_pXFAPageView)
Lei Zhang822886b2018-04-12 17:24:45 +0000150 return {};
Lei Zhang94293682016-01-27 18:27:56 -0800151
Lei Zhanga105fa12018-04-12 16:23:01 +0000152 CFX_PointF pos =
153 GetDisplayMatrix(rect, rotate).GetInverse().Transform(device_point);
Lei Zhang822886b2018-04-12 17:24:45 +0000154 return pos;
Lei Zhang94293682016-01-27 18:27:56 -0800155}
156
Lei Zhanga8db06a2018-04-12 17:53:15 +0000157Optional<CFX_PointF> CPDFXFA_Page::PageToDevice(
158 const FX_RECT& rect,
159 int rotate,
160 const CFX_PointF& page_point) const {
Lei Zhang94293682016-01-27 18:27:56 -0800161 if (!m_pPDFPage && !m_pXFAPageView)
Lei Zhang822886b2018-04-12 17:24:45 +0000162 return {};
Lei Zhang94293682016-01-27 18:27:56 -0800163
Lei Zhangc4242b22018-04-12 15:50:49 +0000164 CFX_Matrix page2device = GetDisplayMatrix(rect, rotate);
Lei Zhanga8db06a2018-04-12 17:53:15 +0000165 return page2device.Transform(page_point);
Lei Zhang94293682016-01-27 18:27:56 -0800166}
167
Lei Zhangc4242b22018-04-12 15:50:49 +0000168CFX_Matrix CPDFXFA_Page::GetDisplayMatrix(const FX_RECT& rect,
Dan Sinclair1b08df12017-02-09 09:17:20 -0500169 int iRotate) const {
Lei Zhang94293682016-01-27 18:27:56 -0800170 if (!m_pPDFPage && !m_pXFAPageView)
Dan Sinclair1b08df12017-02-09 09:17:20 -0500171 return CFX_Matrix();
Lei Zhang94293682016-01-27 18:27:56 -0800172
Ryan Harrison854d71c2017-10-18 12:28:14 -0400173 switch (m_pContext->GetFormType()) {
174 case FormType::kNone:
175 case FormType::kAcroForm:
176 case FormType::kXFAForeground:
Dan Sinclair1b08df12017-02-09 09:17:20 -0500177 if (m_pPDFPage)
Lei Zhangc4242b22018-04-12 15:50:49 +0000178 return m_pPDFPage->GetDisplayMatrix(rect, iRotate);
Lei Zhangdb3c6ce2018-05-17 02:01:42 +0000179 FALLTHROUGH;
Ryan Harrison854d71c2017-10-18 12:28:14 -0400180 case FormType::kXFAFull:
Lei Zhangc4242b22018-04-12 15:50:49 +0000181 if (m_pXFAPageView)
Lei Zhang7d865b62018-04-10 19:29:25 +0000182 return m_pXFAPageView->GetDisplayMatrix(rect, iRotate);
Henrique Nakashima067a44f2018-02-16 03:46:28 +0000183 break;
Lei Zhang94293682016-01-27 18:27:56 -0800184 }
Ryan Harrison854d71c2017-10-18 12:28:14 -0400185
Dan Sinclair1b08df12017-02-09 09:17:20 -0500186 return CFX_Matrix();
Lei Zhang94293682016-01-27 18:27:56 -0800187}