blob: 170f17ea7a1f43524e23d2661d46ce027836192a [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
Henrique Nakashima7b094f82018-05-08 20:32:08 +000025bool CPDFXFA_Page::LoadPDFPage() {
26 if (!m_pContext)
tsepez4cf55152016-11-02 14:37:54 -070027 return false;
thestig9f3dbbc2016-04-13 13:18:21 -070028
Henrique Nakashima7b094f82018-05-08 20:32:08 +000029 CPDF_Document* pPDFDoc = m_pContext->GetPDFDoc();
30 if (!pPDFDoc)
31 return false;
32
33 CPDF_Dictionary* pDict = pPDFDoc->GetPageDictionary(m_iPageIndex);
34 if (!pDict)
35 return false;
36
37 if (!m_pPDFPage || m_pPDFPage->GetFormDict() != pDict) {
38 m_pPDFPage = pdfium::MakeUnique<CPDF_Page>(pPDFDoc, pDict, true);
thestig5cc24652016-04-26 11:46:02 -070039 m_pPDFPage->ParseContent();
Lei Zhang94293682016-01-27 18:27:56 -080040 }
Henrique Nakashima7b094f82018-05-08 20:32:08 +000041 return true;
42}
43
44bool CPDFXFA_Page::LoadXFAPageView() {
45 if (!m_pContext)
46 return false;
thestig9f3dbbc2016-04-13 13:18:21 -070047
dsinclair521b7502016-11-02 13:02:28 -070048 CXFA_FFDoc* pXFADoc = m_pContext->GetXFADoc();
Lei Zhang94293682016-01-27 18:27:56 -080049 if (!pXFADoc)
tsepez4cf55152016-11-02 14:37:54 -070050 return false;
Lei Zhang94293682016-01-27 18:27:56 -080051
dsinclair521b7502016-11-02 13:02:28 -070052 CXFA_FFDocView* pXFADocView = m_pContext->GetXFADocView();
Lei Zhang94293682016-01-27 18:27:56 -080053 if (!pXFADocView)
tsepez4cf55152016-11-02 14:37:54 -070054 return false;
Lei Zhang94293682016-01-27 18:27:56 -080055
dsinclairdf4bc592016-03-31 20:34:43 -070056 CXFA_FFPageView* pPageView = pXFADocView->GetPageView(m_iPageIndex);
Lei Zhang94293682016-01-27 18:27:56 -080057 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -070058 return false;
Lei Zhang94293682016-01-27 18:27:56 -080059
Lei Zhang94293682016-01-27 18:27:56 -080060 m_pXFAPageView = pPageView;
tsepez4cf55152016-11-02 14:37:54 -070061 return true;
Lei Zhang94293682016-01-27 18:27:56 -080062}
63
Henrique Nakashima7b094f82018-05-08 20:32:08 +000064bool CPDFXFA_Page::LoadPage() {
65 if (!m_pContext || m_iPageIndex < 0)
66 return false;
67
68 switch (m_pContext->GetFormType()) {
69 case FormType::kNone:
70 case FormType::kAcroForm:
71 case FormType::kXFAForeground:
72 return LoadPDFPage();
73 case FormType::kXFAFull:
74 return LoadXFAPageView();
75 }
76 return false;
77}
78
79bool CPDFXFA_Page::LoadPDFPage(CPDF_Dictionary* pageDict) {
80 if (!m_pContext || m_iPageIndex < 0 || !pageDict)
81 return false;
82
83 m_pPDFPage =
84 pdfium::MakeUnique<CPDF_Page>(m_pContext->GetPDFDoc(), pageDict, true);
85 m_pPDFPage->ParseContent();
86 return true;
87}
88
Tom Sepezccd94212018-05-03 13:53:02 +000089CPDF_Document::Extension* CPDFXFA_Page::GetDocumentExtension() const {
90 return m_pContext.Get();
91}
92
Dan Sinclair05df0752017-03-14 14:43:42 -040093float CPDFXFA_Page::GetPageWidth() const {
Lei Zhang94293682016-01-27 18:27:56 -080094 if (!m_pPDFPage && !m_pXFAPageView)
95 return 0.0f;
96
Ryan Harrison854d71c2017-10-18 12:28:14 -040097 switch (m_pContext->GetFormType()) {
98 case FormType::kNone:
99 case FormType::kAcroForm:
100 case FormType::kXFAForeground:
Lei Zhang94293682016-01-27 18:27:56 -0800101 if (m_pPDFPage)
102 return m_pPDFPage->GetPageWidth();
Lei Zhangdb3c6ce2018-05-17 02:01:42 +0000103 FALLTHROUGH;
Ryan Harrison854d71c2017-10-18 12:28:14 -0400104 case FormType::kXFAFull:
105 if (m_pXFAPageView)
106 return m_pXFAPageView->GetPageViewRect().width;
Henrique Nakashima067a44f2018-02-16 03:46:28 +0000107 break;
Lei Zhang94293682016-01-27 18:27:56 -0800108 }
109
110 return 0.0f;
111}
112
Dan Sinclair05df0752017-03-14 14:43:42 -0400113float CPDFXFA_Page::GetPageHeight() const {
Lei Zhang94293682016-01-27 18:27:56 -0800114 if (!m_pPDFPage && !m_pXFAPageView)
115 return 0.0f;
116
Ryan Harrison854d71c2017-10-18 12:28:14 -0400117 switch (m_pContext->GetFormType()) {
118 case FormType::kNone:
119 case FormType::kAcroForm:
120 case FormType::kXFAForeground:
Lei Zhang94293682016-01-27 18:27:56 -0800121 if (m_pPDFPage)
122 return m_pPDFPage->GetPageHeight();
Lei Zhangdb3c6ce2018-05-17 02:01:42 +0000123 FALLTHROUGH;
Ryan Harrison854d71c2017-10-18 12:28:14 -0400124 case FormType::kXFAFull:
Dan Sinclair1b08df12017-02-09 09:17:20 -0500125 if (m_pXFAPageView)
126 return m_pXFAPageView->GetPageViewRect().height;
Henrique Nakashima067a44f2018-02-16 03:46:28 +0000127 break;
Lei Zhang94293682016-01-27 18:27:56 -0800128 }
129
130 return 0.0f;
131}
132
Lei Zhang822886b2018-04-12 17:24:45 +0000133Optional<CFX_PointF> CPDFXFA_Page::DeviceToPage(
134 const FX_RECT& rect,
135 int rotate,
136 const CFX_PointF& device_point) const {
Lei Zhang94293682016-01-27 18:27:56 -0800137 if (!m_pPDFPage && !m_pXFAPageView)
Lei Zhang822886b2018-04-12 17:24:45 +0000138 return {};
Lei Zhang94293682016-01-27 18:27:56 -0800139
Lei Zhanga105fa12018-04-12 16:23:01 +0000140 CFX_PointF pos =
141 GetDisplayMatrix(rect, rotate).GetInverse().Transform(device_point);
Lei Zhang822886b2018-04-12 17:24:45 +0000142 return pos;
Lei Zhang94293682016-01-27 18:27:56 -0800143}
144
Lei Zhanga8db06a2018-04-12 17:53:15 +0000145Optional<CFX_PointF> CPDFXFA_Page::PageToDevice(
146 const FX_RECT& rect,
147 int rotate,
148 const CFX_PointF& page_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 Zhangc4242b22018-04-12 15:50:49 +0000152 CFX_Matrix page2device = GetDisplayMatrix(rect, rotate);
Lei Zhanga8db06a2018-04-12 17:53:15 +0000153 return page2device.Transform(page_point);
Lei Zhang94293682016-01-27 18:27:56 -0800154}
155
Lei Zhangc4242b22018-04-12 15:50:49 +0000156CFX_Matrix CPDFXFA_Page::GetDisplayMatrix(const FX_RECT& rect,
Dan Sinclair1b08df12017-02-09 09:17:20 -0500157 int iRotate) const {
Lei Zhang94293682016-01-27 18:27:56 -0800158 if (!m_pPDFPage && !m_pXFAPageView)
Dan Sinclair1b08df12017-02-09 09:17:20 -0500159 return CFX_Matrix();
Lei Zhang94293682016-01-27 18:27:56 -0800160
Ryan Harrison854d71c2017-10-18 12:28:14 -0400161 switch (m_pContext->GetFormType()) {
162 case FormType::kNone:
163 case FormType::kAcroForm:
164 case FormType::kXFAForeground:
Dan Sinclair1b08df12017-02-09 09:17:20 -0500165 if (m_pPDFPage)
Lei Zhangc4242b22018-04-12 15:50:49 +0000166 return m_pPDFPage->GetDisplayMatrix(rect, iRotate);
Lei Zhangdb3c6ce2018-05-17 02:01:42 +0000167 FALLTHROUGH;
Ryan Harrison854d71c2017-10-18 12:28:14 -0400168 case FormType::kXFAFull:
Lei Zhangc4242b22018-04-12 15:50:49 +0000169 if (m_pXFAPageView)
Lei Zhang7d865b62018-04-10 19:29:25 +0000170 return m_pXFAPageView->GetDisplayMatrix(rect, iRotate);
Henrique Nakashima067a44f2018-02-16 03:46:28 +0000171 break;
Lei Zhang94293682016-01-27 18:27:56 -0800172 }
Ryan Harrison854d71c2017-10-18 12:28:14 -0400173
Dan Sinclair1b08df12017-02-09 09:17:20 -0500174 return CFX_Matrix();
Lei Zhang94293682016-01-27 18:27:56 -0800175}