OLD | NEW |
| (Empty) |
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 | |
7 #include "core/include/fpdfapi/cpdf_document.h" | |
8 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h" | |
9 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h" | |
10 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_util.h" | |
11 #include "fpdfsdk/include/fsdk_define.h" | |
12 #include "fpdfsdk/include/fsdk_mgr.h" | |
13 | |
14 CPDFXFA_Page::CPDFXFA_Page(CPDFXFA_Document* pDoc, int page_index) | |
15 : m_pPDFPage(NULL), | |
16 m_pXFAPageView(NULL), | |
17 m_iPageIndex(page_index), | |
18 m_pDocument(pDoc), | |
19 m_iRef(1) {} | |
20 | |
21 CPDFXFA_Page::~CPDFXFA_Page() { | |
22 if (m_pPDFPage) | |
23 delete m_pPDFPage; | |
24 m_pPDFPage = NULL; | |
25 m_pXFAPageView = NULL; | |
26 } | |
27 | |
28 void CPDFXFA_Page::Release() { | |
29 m_iRef--; | |
30 if (m_iRef > 0) | |
31 return; | |
32 | |
33 if (m_pDocument) | |
34 m_pDocument->RemovePage(this); | |
35 | |
36 delete this; | |
37 } | |
38 | |
39 FX_BOOL CPDFXFA_Page::LoadPDFPage() { | |
40 if (!m_pDocument) | |
41 return FALSE; | |
42 CPDF_Document* pPDFDoc = m_pDocument->GetPDFDoc(); | |
43 if (pPDFDoc) { | |
44 CPDF_Dictionary* pDict = pPDFDoc->GetPage(m_iPageIndex); | |
45 if (pDict == NULL) | |
46 return FALSE; | |
47 if (m_pPDFPage) { | |
48 if (m_pPDFPage->m_pFormDict == pDict) | |
49 return TRUE; | |
50 | |
51 delete m_pPDFPage; | |
52 m_pPDFPage = NULL; | |
53 } | |
54 | |
55 m_pPDFPage = new CPDF_Page; | |
56 m_pPDFPage->Load(pPDFDoc, pDict); | |
57 m_pPDFPage->ParseContent(nullptr); | |
58 return TRUE; | |
59 } | |
60 | |
61 return FALSE; | |
62 } | |
63 | |
64 FX_BOOL CPDFXFA_Page::LoadXFAPageView() { | |
65 if (!m_pDocument) | |
66 return FALSE; | |
67 IXFA_Doc* pXFADoc = m_pDocument->GetXFADoc(); | |
68 if (!pXFADoc) | |
69 return FALSE; | |
70 | |
71 IXFA_DocView* pXFADocView = m_pDocument->GetXFADocView(); | |
72 if (!pXFADocView) | |
73 return FALSE; | |
74 | |
75 IXFA_PageView* pPageView = pXFADocView->GetPageView(m_iPageIndex); | |
76 if (!pPageView) | |
77 return FALSE; | |
78 | |
79 if (m_pXFAPageView == pPageView) | |
80 return TRUE; | |
81 | |
82 m_pXFAPageView = pPageView; | |
83 (void)m_pXFAPageView->LoadPageView(nullptr); | |
84 return TRUE; | |
85 } | |
86 | |
87 FX_BOOL CPDFXFA_Page::LoadPage() { | |
88 if (!m_pDocument || m_iPageIndex < 0) | |
89 return FALSE; | |
90 | |
91 int iDocType = m_pDocument->GetDocType(); | |
92 switch (iDocType) { | |
93 case DOCTYPE_PDF: | |
94 case DOCTYPE_STATIC_XFA: { | |
95 return LoadPDFPage(); | |
96 } | |
97 case DOCTYPE_DYNAMIC_XFA: { | |
98 return LoadXFAPageView(); | |
99 } | |
100 default: | |
101 return FALSE; | |
102 } | |
103 | |
104 return FALSE; | |
105 } | |
106 | |
107 FX_BOOL CPDFXFA_Page::LoadPDFPage(CPDF_Dictionary* pageDict) { | |
108 if (!m_pDocument || m_iPageIndex < 0 || !pageDict) | |
109 return FALSE; | |
110 | |
111 if (m_pPDFPage) | |
112 delete m_pPDFPage; | |
113 | |
114 m_pPDFPage = new CPDF_Page(); | |
115 m_pPDFPage->Load(m_pDocument->GetPDFDoc(), pageDict); | |
116 m_pPDFPage->ParseContent(nullptr); | |
117 | |
118 return TRUE; | |
119 } | |
120 | |
121 FX_FLOAT CPDFXFA_Page::GetPageWidth() { | |
122 if (!m_pPDFPage && !m_pXFAPageView) | |
123 return 0.0f; | |
124 | |
125 int nDocType = m_pDocument->GetDocType(); | |
126 switch (nDocType) { | |
127 case DOCTYPE_DYNAMIC_XFA: { | |
128 if (m_pXFAPageView) { | |
129 CFX_RectF rect; | |
130 m_pXFAPageView->GetPageViewRect(rect); | |
131 return rect.width; | |
132 } | |
133 } break; | |
134 case DOCTYPE_STATIC_XFA: | |
135 case DOCTYPE_PDF: { | |
136 if (m_pPDFPage) | |
137 return m_pPDFPage->GetPageWidth(); | |
138 } break; | |
139 default: | |
140 return 0.0f; | |
141 } | |
142 | |
143 return 0.0f; | |
144 } | |
145 | |
146 FX_FLOAT CPDFXFA_Page::GetPageHeight() { | |
147 if (!m_pPDFPage && !m_pXFAPageView) | |
148 return 0.0f; | |
149 | |
150 int nDocType = m_pDocument->GetDocType(); | |
151 switch (nDocType) { | |
152 case DOCTYPE_PDF: | |
153 case DOCTYPE_STATIC_XFA: { | |
154 if (m_pPDFPage) | |
155 return m_pPDFPage->GetPageHeight(); | |
156 } break; | |
157 case DOCTYPE_DYNAMIC_XFA: { | |
158 if (m_pXFAPageView) { | |
159 CFX_RectF rect; | |
160 m_pXFAPageView->GetPageViewRect(rect); | |
161 return rect.height; | |
162 } | |
163 } break; | |
164 default: | |
165 return 0.0f; | |
166 } | |
167 | |
168 return 0.0f; | |
169 } | |
170 | |
171 void CPDFXFA_Page::DeviceToPage(int start_x, | |
172 int start_y, | |
173 int size_x, | |
174 int size_y, | |
175 int rotate, | |
176 int device_x, | |
177 int device_y, | |
178 double* page_x, | |
179 double* page_y) { | |
180 if (!m_pPDFPage && !m_pXFAPageView) | |
181 return; | |
182 | |
183 CFX_Matrix page2device; | |
184 CFX_Matrix device2page; | |
185 FX_FLOAT page_x_f, page_y_f; | |
186 | |
187 GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate); | |
188 | |
189 device2page.SetReverse(page2device); | |
190 device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f, | |
191 page_y_f); | |
192 | |
193 *page_x = (page_x_f); | |
194 *page_y = (page_y_f); | |
195 } | |
196 | |
197 void CPDFXFA_Page::PageToDevice(int start_x, | |
198 int start_y, | |
199 int size_x, | |
200 int size_y, | |
201 int rotate, | |
202 double page_x, | |
203 double page_y, | |
204 int* device_x, | |
205 int* device_y) { | |
206 if (!m_pPDFPage && !m_pXFAPageView) | |
207 return; | |
208 | |
209 CFX_Matrix page2device; | |
210 FX_FLOAT device_x_f, device_y_f; | |
211 | |
212 GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate); | |
213 | |
214 page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f, | |
215 device_y_f); | |
216 | |
217 *device_x = FXSYS_round(device_x_f); | |
218 *device_y = FXSYS_round(device_y_f); | |
219 } | |
220 | |
221 void CPDFXFA_Page::GetDisplayMatrix(CFX_Matrix& matrix, | |
222 int xPos, | |
223 int yPos, | |
224 int xSize, | |
225 int ySize, | |
226 int iRotate) const { | |
227 if (!m_pPDFPage && !m_pXFAPageView) | |
228 return; | |
229 | |
230 int nDocType = m_pDocument->GetDocType(); | |
231 switch (nDocType) { | |
232 case DOCTYPE_DYNAMIC_XFA: { | |
233 if (m_pXFAPageView) { | |
234 CFX_Rect rect; | |
235 rect.Set(xPos, yPos, xSize, ySize); | |
236 m_pXFAPageView->GetDisplayMatrix(matrix, rect, iRotate); | |
237 } | |
238 } break; | |
239 case DOCTYPE_PDF: | |
240 case DOCTYPE_STATIC_XFA: { | |
241 if (m_pPDFPage) { | |
242 m_pPDFPage->GetDisplayMatrix(matrix, xPos, yPos, xSize, ySize, iRotate); | |
243 } | |
244 } break; | |
245 default: | |
246 return; | |
247 } | |
248 } | |
OLD | NEW |