Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cjx_layoutpseudomodel.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
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 "fxjs/xfa/cjx_layoutpseudomodel.h"
8
9#include <set>
10#include <utility>
11
12#include "core/fxcrt/fx_coordinates.h"
13#include "fxjs/fxv8.h"
14#include "fxjs/js_resources.h"
15#include "fxjs/xfa/cfxjse_class.h"
16#include "fxjs/xfa/cfxjse_engine.h"
17#include "third_party/base/containers/contains.h"
18#include "v8/include/cppgc/allocation.h"
19#include "v8/include/v8-object.h"
20#include "xfa/fxfa/cxfa_ffnotify.h"
21#include "xfa/fxfa/layout/cxfa_contentlayoutitem.h"
22#include "xfa/fxfa/layout/cxfa_layoutitem.h"
23#include "xfa/fxfa/layout/cxfa_layoutprocessor.h"
24#include "xfa/fxfa/layout/cxfa_traversestrategy_layoutitem.h"
25#include "xfa/fxfa/layout/cxfa_viewlayoutitem.h"
26#include "xfa/fxfa/parser/cscript_layoutpseudomodel.h"
27#include "xfa/fxfa/parser/cxfa_arraynodelist.h"
28#include "xfa/fxfa/parser/cxfa_document.h"
29#include "xfa/fxfa/parser/cxfa_form.h"
30#include "xfa/fxfa/parser/cxfa_measurement.h"
31#include "xfa/fxfa/parser/cxfa_node.h"
32#include "xfa/fxfa/parser/cxfa_nodeiteratortemplate.h"
33
34const CJX_MethodSpec CJX_LayoutPseudoModel::MethodSpecs[] = {
35 {"absPage", absPage_static},
36 {"absPageCount", absPageCount_static},
37 {"absPageCountInBatch", absPageCountInBatch_static},
38 {"absPageInBatch", absPageInBatch_static},
39 {"absPageSpan", absPageSpan_static},
40 {"h", h_static},
41 {"page", page_static},
42 {"pageContent", pageContent_static},
43 {"pageCount", pageCount_static},
44 {"pageSpan", pageSpan_static},
45 {"relayout", relayout_static},
46 {"relayoutPageArea", relayoutPageArea_static},
47 {"sheet", sheet_static},
48 {"sheetCount", sheetCount_static},
49 {"sheetCountInBatch", sheetCountInBatch_static},
50 {"sheetInBatch", sheetInBatch_static},
51 {"w", w_static},
52 {"x", x_static},
53 {"y", y_static}};
54
55CJX_LayoutPseudoModel::CJX_LayoutPseudoModel(CScript_LayoutPseudoModel* model)
56 : CJX_Object(model) {
57 DefineMethods(MethodSpecs);
58}
59
60CJX_LayoutPseudoModel::~CJX_LayoutPseudoModel() = default;
61
62bool CJX_LayoutPseudoModel::DynamicTypeIs(TypeTag eType) const {
63 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
64}
65
66void CJX_LayoutPseudoModel::ready(v8::Isolate* pIsolate,
67 v8::Local<v8::Value>* pValue,
68 bool bSetting,
69 XFA_Attribute eAttribute) {
70 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
71 if (!pNotify)
72 return;
73 if (bSetting) {
74 ThrowException(pIsolate,
75 WideString::FromASCII("Unable to set ready value."));
76 return;
77 }
78
80 const bool bReady = iStatus != CXFA_FFDocView::LayoutStatus::kNone &&
82 *pValue = fxv8::NewBooleanHelper(pIsolate, bReady);
83}
84
85CJS_Result CJX_LayoutPseudoModel::DoHWXYInternal(
86 CFXJSE_Engine* runtime,
87 pdfium::span<v8::Local<v8::Value>> params,
88 HWXY layoutModel) {
89 if (params.empty() || params.size() > 3)
91
92 CXFA_Node* pNode = ToNode(runtime->ToXFAObject(params[0]));
93 if (!pNode)
95
96 WideString unit = WideString::FromASCII("pt");
97 if (params.size() >= 2) {
98 WideString tmp_unit = runtime->ToWideString(params[1]);
99 if (!tmp_unit.IsEmpty())
100 unit = std::move(tmp_unit);
101 }
102 int32_t iIndex = params.size() >= 3 ? runtime->ToInt32(params[2]) : 0;
103 auto* pDocLayout = CXFA_LayoutProcessor::FromDocument(GetDocument());
104 CXFA_ContentLayoutItem* pLayoutItem =
105 ToContentLayoutItem(pDocLayout->GetLayoutItem(pNode));
106 if (!pLayoutItem)
108
109 while (iIndex > 0 && pLayoutItem) {
110 pLayoutItem = pLayoutItem->GetNext();
111 --iIndex;
112 }
113
114 if (!pLayoutItem)
115 return CJS_Result::Success(runtime->NewNumber(0.0));
116
117 CXFA_Measurement measure;
118 CFX_RectF rtRect = pLayoutItem->GetRelativeRect();
119 switch (layoutModel) {
120 case HWXY::kH:
121 measure.Set(rtRect.height, XFA_Unit::Pt);
122 break;
123 case HWXY::kW:
124 measure.Set(rtRect.width, XFA_Unit::Pt);
125 break;
126 case HWXY::kX:
127 measure.Set(rtRect.left, XFA_Unit::Pt);
128 break;
129 case HWXY::kY:
130 measure.Set(rtRect.top, XFA_Unit::Pt);
131 break;
132 }
133
134 XFA_Unit eUnit = CXFA_Measurement::GetUnitFromString(unit.AsStringView());
135 if (eUnit == XFA_Unit::Unknown)
137
138 float fValue = measure.ToUnit(eUnit);
140 runtime->NewNumber(FXSYS_roundf(fValue * 1000) / 1000.0f));
141}
142
143CJS_Result CJX_LayoutPseudoModel::h(CFXJSE_Engine* runtime,
144 pdfium::span<v8::Local<v8::Value>> params) {
145 return DoHWXYInternal(runtime, params, HWXY::kH);
146}
147
148CJS_Result CJX_LayoutPseudoModel::w(CFXJSE_Engine* runtime,
149 pdfium::span<v8::Local<v8::Value>> params) {
150 return DoHWXYInternal(runtime, params, HWXY::kW);
151}
152
153CJS_Result CJX_LayoutPseudoModel::x(CFXJSE_Engine* runtime,
154 pdfium::span<v8::Local<v8::Value>> params) {
155 return DoHWXYInternal(runtime, params, HWXY::kX);
156}
157
158CJS_Result CJX_LayoutPseudoModel::y(CFXJSE_Engine* runtime,
159 pdfium::span<v8::Local<v8::Value>> params) {
160 return DoHWXYInternal(runtime, params, HWXY::kY);
161}
162
163CJS_Result CJX_LayoutPseudoModel::AllPageCount(CFXJSE_Engine* runtime) {
164 auto* pDocLayout = CXFA_LayoutProcessor::FromDocument(GetDocument());
165 return CJS_Result::Success(runtime->NewNumber(pDocLayout->CountPages()));
166}
167
168CJS_Result CJX_LayoutPseudoModel::NumberedPageCount(CFXJSE_Engine* runtime) {
169 auto* pDocLayout = CXFA_LayoutProcessor::FromDocument(GetDocument());
170 int32_t iPageCount = 0;
171 int32_t iPageNum = pDocLayout->CountPages();
172 for (int32_t i = 0; i < iPageNum; i++) {
173 CXFA_ViewLayoutItem* pLayoutPage = pDocLayout->GetPage(i);
174 if (!pLayoutPage)
175 continue;
176
177 CXFA_Node* pMasterPage = pLayoutPage->GetMasterPage();
178 if (pMasterPage->JSObject()->GetInteger(XFA_Attribute::Numbered))
179 iPageCount++;
180 }
181 return CJS_Result::Success(runtime->NewNumber(iPageCount));
182}
183
184CJS_Result CJX_LayoutPseudoModel::pageCount(
185 CFXJSE_Engine* runtime,
186 pdfium::span<v8::Local<v8::Value>> params) {
187 return NumberedPageCount(runtime);
188}
189
190CJS_Result CJX_LayoutPseudoModel::pageSpan(
191 CFXJSE_Engine* runtime,
192 pdfium::span<v8::Local<v8::Value>> params) {
193 if (params.size() != 1)
195
196 CXFA_Node* pNode = ToNode(runtime->ToXFAObject(params[0]));
197 if (!pNode)
199
200 auto* pDocLayout = CXFA_LayoutProcessor::FromDocument(GetDocument());
201 CXFA_ContentLayoutItem* pLayoutItem =
202 ToContentLayoutItem(pDocLayout->GetLayoutItem(pNode));
203 if (!pLayoutItem)
204 return CJS_Result::Success(runtime->NewNumber(-1));
205
206 int32_t iLast = pLayoutItem->GetLast()->GetPage()->GetPageIndex();
207 int32_t iFirst = pLayoutItem->GetFirst()->GetPage()->GetPageIndex();
208 int32_t iPageSpan = iLast - iFirst + 1;
209 return CJS_Result::Success(runtime->NewNumber(iPageSpan));
210}
211
212CJS_Result CJX_LayoutPseudoModel::page(
213 CFXJSE_Engine* runtime,
214 pdfium::span<v8::Local<v8::Value>> params) {
215 return PageInternals(runtime, params, false);
216}
217
218std::vector<CXFA_Node*> CJX_LayoutPseudoModel::GetObjArray(
219 CXFA_LayoutProcessor* pDocLayout,
220 int32_t iPageNo,
221 const WideString& wsType,
222 bool bOnPageArea) {
223 CXFA_ViewLayoutItem* pLayoutPage = pDocLayout->GetPage(iPageNo);
224 if (!pLayoutPage)
225 return std::vector<CXFA_Node*>();
226
227 std::vector<CXFA_Node*> retArray;
228 if (wsType.EqualsASCII("pageArea")) {
229 if (pLayoutPage->GetFormNode())
230 retArray.push_back(pLayoutPage->GetFormNode());
231 return retArray;
232 }
233 if (wsType.EqualsASCII("contentArea")) {
234 for (CXFA_LayoutItem* pItem = pLayoutPage->GetFirstChild(); pItem;
235 pItem = pItem->GetNextSibling()) {
236 if (pItem->GetFormNode()->GetElementType() == XFA_Element::ContentArea)
237 retArray.push_back(pItem->GetFormNode());
238 }
239 return retArray;
240 }
241 std::set<CXFA_Node*> formItems;
242 if (wsType.IsEmpty()) {
243 if (pLayoutPage->GetFormNode())
244 retArray.push_back(pLayoutPage->GetFormNode());
245
246 for (CXFA_LayoutItem* pItem = pLayoutPage->GetFirstChild(); pItem;
247 pItem = pItem->GetNextSibling()) {
248 if (pItem->GetFormNode()->GetElementType() == XFA_Element::ContentArea) {
249 retArray.push_back(pItem->GetFormNode());
250 if (!bOnPageArea) {
251 CXFA_LayoutItemIterator iterator(pItem->GetFirstChild());
252 for (CXFA_LayoutItem* pChild = iterator.GetCurrent(); pChild;
253 pChild = iterator.MoveToNext()) {
254 CXFA_ContentLayoutItem* pItemChild = pChild->AsContentLayoutItem();
255 if (!pItemChild)
256 continue;
257
258 XFA_Element eType = pItemChild->GetFormNode()->GetElementType();
259 if (eType != XFA_Element::Field && eType != XFA_Element::Draw &&
260 eType != XFA_Element::Subform && eType != XFA_Element::Area) {
261 continue;
262 }
263 if (pdfium::Contains(formItems, pItemChild->GetFormNode()))
264 continue;
265
266 formItems.insert(pItemChild->GetFormNode());
267 retArray.push_back(pItemChild->GetFormNode());
268 }
269 }
270 } else {
271 if (bOnPageArea) {
272 CXFA_LayoutItemIterator iterator(pItem);
273 for (CXFA_LayoutItem* pChild = iterator.GetCurrent(); pChild;
274 pChild = iterator.MoveToNext()) {
275 CXFA_ContentLayoutItem* pItemChild = pChild->AsContentLayoutItem();
276 if (!pItemChild)
277 continue;
278
279 XFA_Element eType = pItemChild->GetFormNode()->GetElementType();
280 if (eType != XFA_Element::Field && eType != XFA_Element::Draw &&
281 eType != XFA_Element::Subform && eType != XFA_Element::Area) {
282 continue;
283 }
284 if (pdfium::Contains(formItems, pItemChild->GetFormNode()))
285 continue;
286
287 formItems.insert(pItemChild->GetFormNode());
288 retArray.push_back(pItemChild->GetFormNode());
289 }
290 }
291 }
292 }
293 return retArray;
294 }
295
297 if (wsType.EqualsASCII("field"))
298 eType = XFA_Element::Field;
299 else if (wsType.EqualsASCII("draw"))
300 eType = XFA_Element::Draw;
301 else if (wsType.EqualsASCII("subform"))
302 eType = XFA_Element::Subform;
303 else if (wsType.EqualsASCII("area"))
304 eType = XFA_Element::Area;
305
306 if (eType != XFA_Element::Unknown) {
307 for (CXFA_LayoutItem* pItem = pLayoutPage->GetFirstChild(); pItem;
308 pItem = pItem->GetNextSibling()) {
309 if (pItem->GetFormNode()->GetElementType() == XFA_Element::ContentArea) {
310 if (!bOnPageArea) {
311 CXFA_LayoutItemIterator iterator(pItem->GetFirstChild());
312 for (CXFA_LayoutItem* pChild = iterator.GetCurrent(); pChild;
313 pChild = iterator.MoveToNext()) {
314 CXFA_ContentLayoutItem* pItemChild = pChild->AsContentLayoutItem();
315 if (!pItemChild)
316 continue;
317 if (pItemChild->GetFormNode()->GetElementType() != eType)
318 continue;
319 if (pdfium::Contains(formItems, pItemChild->GetFormNode()))
320 continue;
321
322 formItems.insert(pItemChild->GetFormNode());
323 retArray.push_back(pItemChild->GetFormNode());
324 }
325 }
326 } else {
327 if (bOnPageArea) {
328 CXFA_LayoutItemIterator iterator(pItem);
329 for (CXFA_LayoutItem* pChild = iterator.GetCurrent(); pChild;
330 pChild = iterator.MoveToNext()) {
331 CXFA_ContentLayoutItem* pItemChild = pChild->AsContentLayoutItem();
332 if (!pItemChild)
333 continue;
334 if (pItemChild->GetFormNode()->GetElementType() != eType)
335 continue;
336 if (pdfium::Contains(formItems, pItemChild->GetFormNode()))
337 continue;
338
339 formItems.insert(pItemChild->GetFormNode());
340 retArray.push_back(pItemChild->GetFormNode());
341 }
342 }
343 }
344 }
345 }
346 return retArray;
347}
348
349CJS_Result CJX_LayoutPseudoModel::pageContent(
350 CFXJSE_Engine* runtime,
351 pdfium::span<v8::Local<v8::Value>> params) {
352 if (params.empty() || params.size() > 3)
354
355 int32_t iIndex = 0;
356 if (params.size() >= 1)
357 iIndex = runtime->ToInt32(params[0]);
358
359 WideString wsType;
360 if (params.size() >= 2)
361 wsType = runtime->ToWideString(params[1]);
362
363 bool bOnPageArea = false;
364 if (params.size() >= 3)
365 bOnPageArea = runtime->ToBoolean(params[2]);
366
367 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
368 if (!pNotify)
370
371 CXFA_Document* pDoc = GetDocument();
372 auto* pDocLayout = CXFA_LayoutProcessor::FromDocument(pDoc);
373 auto* pArrayNodeList = cppgc::MakeGarbageCollected<CXFA_ArrayNodeList>(
374 pDoc->GetHeap()->GetAllocationHandle(), pDoc);
375 pDoc->GetNodeOwner()->PersistList(pArrayNodeList);
376 pArrayNodeList->SetArrayNodeList(
377 GetObjArray(pDocLayout, iIndex, wsType, bOnPageArea));
378 return CJS_Result::Success(runtime->NewNormalXFAObject(pArrayNodeList));
379}
380
381CJS_Result CJX_LayoutPseudoModel::absPageCount(
382 CFXJSE_Engine* runtime,
383 pdfium::span<v8::Local<v8::Value>> params) {
384 return AllPageCount(runtime);
385}
386
387CJS_Result CJX_LayoutPseudoModel::absPageCountInBatch(
388 CFXJSE_Engine* runtime,
389 pdfium::span<v8::Local<v8::Value>> params) {
390 return CJS_Result::Success(runtime->NewNumber(0));
391}
392
393CJS_Result CJX_LayoutPseudoModel::sheetCountInBatch(
394 CFXJSE_Engine* runtime,
395 pdfium::span<v8::Local<v8::Value>> params) {
396 return CJS_Result::Success(runtime->NewNumber(0));
397}
398
399CJS_Result CJX_LayoutPseudoModel::relayout(
400 CFXJSE_Engine* runtime,
401 pdfium::span<v8::Local<v8::Value>> params) {
402 CXFA_Node* pRootNode = GetDocument()->GetRoot();
403 auto* pLayoutProcessor = GetDocument()->GetLayoutProcessor();
404 CXFA_Form* pFormRoot =
405 pRootNode->GetFirstChildByClass<CXFA_Form>(XFA_Element::Form);
406 if (pFormRoot) {
407 if (pFormRoot->GetFirstChild())
408 pLayoutProcessor->SetHasChangedContainer();
409 }
410 pLayoutProcessor->SetForceRelayout();
412}
413
414CJS_Result CJX_LayoutPseudoModel::absPageSpan(
415 CFXJSE_Engine* runtime,
416 pdfium::span<v8::Local<v8::Value>> params) {
417 return pageSpan(runtime, params);
418}
419
420CJS_Result CJX_LayoutPseudoModel::absPageInBatch(
421 CFXJSE_Engine* runtime,
422 pdfium::span<v8::Local<v8::Value>> params) {
423 if (params.size() != 1)
425
426 return CJS_Result::Success(runtime->NewNumber(0));
427}
428
429CJS_Result CJX_LayoutPseudoModel::sheetInBatch(
430 CFXJSE_Engine* runtime,
431 pdfium::span<v8::Local<v8::Value>> params) {
432 if (params.size() != 1)
434
435 return CJS_Result::Success(runtime->NewNumber(0));
436}
437
438CJS_Result CJX_LayoutPseudoModel::sheet(
439 CFXJSE_Engine* runtime,
440 pdfium::span<v8::Local<v8::Value>> params) {
441 return PageInternals(runtime, params, true);
442}
443
444CJS_Result CJX_LayoutPseudoModel::relayoutPageArea(
445 CFXJSE_Engine* runtime,
446 pdfium::span<v8::Local<v8::Value>> params) {
448}
449
450CJS_Result CJX_LayoutPseudoModel::sheetCount(
451 CFXJSE_Engine* runtime,
452 pdfium::span<v8::Local<v8::Value>> params) {
453 return AllPageCount(runtime);
454}
455
456CJS_Result CJX_LayoutPseudoModel::absPage(
457 CFXJSE_Engine* runtime,
458 pdfium::span<v8::Local<v8::Value>> params) {
459 return PageInternals(runtime, params, true);
460}
461
462CJS_Result CJX_LayoutPseudoModel::PageInternals(
463 CFXJSE_Engine* runtime,
464 pdfium::span<v8::Local<v8::Value>> params,
465 bool bAbsPage) {
466 if (params.size() != 1)
468
469 CXFA_Node* pNode = ToNode(runtime->ToXFAObject(params[0]));
470 if (!pNode)
471 return CJS_Result::Success(runtime->NewNumber(0));
472
473 auto* pDocLayout = CXFA_LayoutProcessor::FromDocument(GetDocument());
474 CXFA_ContentLayoutItem* pLayoutItem =
475 ToContentLayoutItem(pDocLayout->GetLayoutItem(pNode));
476 if (!pLayoutItem)
477 return CJS_Result::Success(runtime->NewNumber(-1));
478
479 int32_t iPage = pLayoutItem->GetFirst()->GetPage()->GetPageIndex();
480 return CJS_Result::Success(runtime->NewNumber(bAbsPage ? iPage : iPage + 1));
481}
friend class EventParamScope
static CJS_Result Success()
Definition cjs_result.h:27
static CJS_Result Failure(JSMessage id)
Definition cjs_result.h:34
bool DynamicTypeIs(TypeTag eType) const override
~CJX_LayoutPseudoModel() override
void DefineMethods(pdfium::span< const CJX_MethodSpec > methods)
CXFA_Document * GetDocument() const
virtual bool DynamicTypeIs(TypeTag eType) const
void ThrowException(v8::Isolate *pIsolate, const WideString &str) const
CXFA_ContentLayoutItem * GetNext() const
CXFA_ContentLayoutItem * GetLast()
CXFA_ContentLayoutItem * GetFirst()
CXFA_NodeOwner * GetNodeOwner()
CXFA_FFDocView::LayoutStatus GetLayoutStatus()
const CXFA_ViewLayoutItem * GetPage() const
CXFA_ContentLayoutItem * AsContentLayoutItem()
CXFA_Node * GetFormNode() const
CXFA_ViewLayoutItem * GetPage(int32_t index) const
static CXFA_LayoutProcessor * FromDocument(const CXFA_Document *pXFADoc)
void Set(float fValue, XFA_Unit eUnit)
float ToUnit(XFA_Unit eUnit) const
static XFA_Unit GetUnitFromString(WideStringView wsUnit)
void PersistList(CXFA_List *list)
XFA_Element GetElementType() const
Definition cxfa_object.h:91
CXFA_Node * GetMasterPage() const
bool IsEmpty() const
Definition widestring.h:118
static WideString FromASCII(ByteStringView str)
bool EqualsASCII(ByteStringView that) const
Definition widestring.h:216
int FXSYS_roundf(float f)
Definition fx_system.cpp:92
XFA_Unit
Definition fxfa_basic.h:91
XFA_Attribute
Definition fxfa_basic.h:67
XFA_Element
Definition fxfa_basic.h:75
JSMessage