1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* ElementBox class. |
6
|
|
|
* |
7
|
|
|
* @package YetiForcePDF\Layout |
8
|
|
|
* |
9
|
|
|
* @copyright YetiForce Sp. z o.o |
10
|
|
|
* @license MIT |
11
|
|
|
* @author Rafal Pospiech <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace YetiForcePDF\Layout; |
15
|
|
|
|
16
|
|
|
use YetiForcePDF\Html\Element; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ElementBox. |
20
|
|
|
*/ |
21
|
|
|
class ElementBox extends Box |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Element |
25
|
|
|
*/ |
26
|
|
|
protected $element; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get element. |
30
|
|
|
* |
31
|
|
|
* @return Element |
32
|
|
|
*/ |
33
|
|
|
public function getElement() |
34
|
|
|
{ |
35
|
|
|
return $this->element; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set element. |
40
|
|
|
* |
41
|
|
|
* @param Element $element |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function setElement(Element $element) |
46
|
|
|
{ |
47
|
|
|
$this->element = $element; |
48
|
|
|
$element->setBox($this); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get boxes by tag name. |
55
|
|
|
* |
56
|
|
|
* @param string $tagName |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getBoxesByTagName(string $tagName) |
61
|
|
|
{ |
62
|
|
|
$boxes = []; |
63
|
|
|
$allChildren = []; |
64
|
|
|
$this->getAllChildren($allChildren); |
65
|
|
|
foreach ($allChildren as $child) { |
66
|
|
|
if ($child instanceof self && $child->getElement() && $child->getElement()->getDOMElement()) { |
67
|
|
|
if (isset($child->getElement()->getDOMElement()->tagName)) { |
68
|
|
|
$elementTagName = $child->getElement()->getDOMElement()->tagName; |
69
|
|
|
if ($elementTagName && strtolower($elementTagName) === strtolower($tagName)) { |
70
|
|
|
$boxes[] = $child; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $boxes; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Fix tables - iterate through cells and insert missing one. |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function fixTables() |
85
|
|
|
{ |
86
|
|
|
$tables = $this->getBoxesByType('TableWrapperBox'); |
87
|
|
|
foreach ($tables as $tableWrapperBox) { |
88
|
|
|
$tableBox = $tableWrapperBox->getFirstChild(); |
89
|
|
|
$rowGroups = $tableBox->getChildren(); |
90
|
|
|
foreach ($rowGroups as $rowGroup) { |
91
|
|
|
// wrap rows with row groups |
92
|
|
|
if ($rowGroup instanceof TableRowBox) { |
93
|
|
|
if (!isset($wrapRowGroup)) { |
94
|
|
|
$wrapRowGroup = $tableBox->removeChild($tableBox->createRowGroupBox()); |
95
|
|
|
$tableBox->insertBefore($wrapRowGroup, $rowGroup); |
96
|
|
|
} |
97
|
|
|
$wrapRowGroup->appendChild($tableBox->removeChild($rowGroup)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
unset($wrapRowGroup); |
101
|
|
|
$rowGroups = $tableBox->getChildren(); |
102
|
|
|
if (empty($rowGroups)) { |
103
|
|
|
$rowGroup = $tableBox->createRowGroupBox(); |
104
|
|
|
$row = $rowGroup->createRowBox(); |
105
|
|
|
$column = $row->createColumnBox(); |
106
|
|
|
$column->createCellBox(); |
107
|
|
|
} else { |
108
|
|
|
$columnsCount = 0; |
109
|
|
|
foreach ($rowGroups as $rowGroup) { |
110
|
|
|
if (!$rowGroup->hasChildren()) { |
111
|
|
|
$row = $rowGroup->createRowBox(); |
112
|
|
|
$column = $row->createColumnBox(); |
113
|
|
|
$column->createCellBox(); |
114
|
|
|
} |
115
|
|
|
foreach ($rowGroup->getChildren() as $rowIndex => $row) { |
116
|
|
|
$columns = $row->getChildren(); |
117
|
|
|
$columnsCount = max($columnsCount, \count($columns)); |
118
|
|
|
foreach ($columns as $columnIndex => $column) { |
119
|
|
|
if ($column->getRowSpan() > 1) { |
120
|
|
|
$rowSpans = $column->getRowSpan(); |
121
|
|
|
for ($i = 1; $i < $rowSpans; ++$i) { |
122
|
|
|
$nextRow = $rowGroup->getChildren()[$rowIndex + $i]; |
123
|
|
|
$rowChildren = $nextRow->getChildren(); |
124
|
|
|
$insertColumn = $nextRow->removeChild($nextRow->createColumnBox()); |
125
|
|
|
if (isset($rowChildren[$columnIndex])) { |
126
|
|
|
$before = $rowChildren[$columnIndex]; |
127
|
|
|
$nextRow->insertBefore($insertColumn, $before); |
128
|
|
|
} else { |
129
|
|
|
$nextRow->appendChild($insertColumn); |
130
|
|
|
} |
131
|
|
|
$insertColumn->setStyle(clone $column->getStyle()); |
132
|
|
|
$insertColumn->getStyle()->setBox($insertColumn); |
133
|
|
|
$insertCell = $insertColumn->createCellBox(); |
134
|
|
|
$insertCell->setStyle(clone $column->getFirstChild()->getStyle()); |
135
|
|
|
$insertCell->getStyle()->setBox($insertCell); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
foreach ($rowGroup->getChildren() as $row) { |
141
|
|
|
$columns = $row->getChildren(); |
142
|
|
|
$missing = $columnsCount - \count($columns); |
143
|
|
|
for ($i = 0; $i < $missing; ++$i) { |
144
|
|
|
$column = $row->createColumnBox(); |
145
|
|
|
$column->createCellBox(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
// fix row spans |
149
|
|
|
$rowSpans = []; |
150
|
|
|
$rowSpansUp = []; |
151
|
|
|
foreach ($rowGroup->getChildren() as $row) { |
152
|
|
|
foreach ($row->getChildren() as $columnIndex => $column) { |
153
|
|
|
if ($column->getRowSpan() > 1) { |
154
|
|
|
$rowSpans[$columnIndex] = $column->getRowSpan(); |
155
|
|
|
$rowSpansUp[$columnIndex] = 0; |
156
|
|
|
$column->setRowSpanUp(0); |
157
|
|
|
$row->setRowSpanUp(max($row->getRowSpanUp(), 0)); |
158
|
|
|
$row->setRowSpan(max($row->getRowSpan(), $column->getRowSpan())); |
159
|
|
|
} else { |
160
|
|
|
if (isset($rowSpans[$columnIndex]) && $rowSpans[$columnIndex] > 1) { |
161
|
|
|
if ($rowSpansUp[$columnIndex] < $rowSpans[$columnIndex]) { |
162
|
|
|
++$rowSpansUp[$columnIndex]; |
163
|
|
|
$column->setRowSpanUp($rowSpansUp[$columnIndex]); |
164
|
|
|
$row->setRowSpanUp(max($row->getRowSpanUp(), $rowSpansUp[$columnIndex])); |
165
|
|
|
$row->setRowSpan(max($row->getRowSpan(), $column->getRowSpan())); |
166
|
|
|
} else { |
167
|
|
|
$rowSpansUp[$columnIndex] = 0; |
168
|
|
|
$rowSpans[$columnIndex] = 1; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Span all rows. |
183
|
|
|
* |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function spanAllRows() |
187
|
|
|
{ |
188
|
|
|
$tablesBoxes = $this->getBoxesByType('TableBox'); |
189
|
|
|
foreach ($tablesBoxes as $tableBox) { |
190
|
|
|
$tableBox->spanRows(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Build tree. |
198
|
|
|
* |
199
|
|
|
* @param $parentBlock |
200
|
|
|
* |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function buildTree($parentBlock = null) |
204
|
|
|
{ |
205
|
|
|
if ($this->getElement()) { |
206
|
|
|
$domElement = $this->getElement()->getDOMElement(); |
207
|
|
|
} else { |
208
|
|
|
// tablebox doesn't have element so we can get it from table wrapper (parent box) |
209
|
|
|
$domElement = $this->getParent()->getElement()->getDOMElement(); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
if ($domElement->hasChildNodes()) { |
212
|
|
|
foreach ($domElement->childNodes as $childDomElement) { |
213
|
|
|
if ($childDomElement instanceof \DOMComment) { |
214
|
|
|
continue; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$element = (new Element()) |
218
|
|
|
->setDocument($this->document) |
219
|
|
|
->setDOMElement($childDomElement) |
220
|
|
|
->init(); |
221
|
|
|
$style = (new \YetiForcePDF\Style\Style()) |
222
|
|
|
->setDocument($this->document) |
223
|
|
|
->setElement($element); |
224
|
|
|
if ($childDomElement instanceof \DOMElement) { |
225
|
|
|
if ($childDomElement->hasAttribute('style')) { |
226
|
|
|
// for now only basic style is used - from current element only (with defaults) |
227
|
|
|
$style->setContent($childDomElement->getAttribute('style')); |
228
|
|
|
} elseif ('style' === $childDomElement->nodeName) { |
229
|
|
|
$style->parseCss($childDomElement->nodeValue); |
230
|
|
|
} |
231
|
|
|
$element->attachClasses(); |
232
|
|
|
} |
233
|
|
|
$style = $style->parseInline(); |
234
|
|
|
$display = $style->getRules('display'); |
235
|
|
|
switch ($display) { |
236
|
|
|
case 'block': |
237
|
|
|
if ($childDomElement->hasAttribute('data-header')) { |
238
|
|
|
$this->appendHeaderBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
239
|
|
|
} elseif ($childDomElement->hasAttribute('data-footer')) { |
240
|
|
|
$this->appendFooterBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
241
|
|
|
} elseif ($childDomElement->hasAttribute('data-watermark')) { |
242
|
|
|
$this->appendWatermarkBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
243
|
|
|
} elseif ($childDomElement->hasAttribute('data-font')) { |
244
|
|
|
$this->appendFontBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
245
|
|
|
} elseif ($childDomElement->hasAttribute('data-barcode')) { |
246
|
|
|
$this->appendBarcodeBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
247
|
|
|
} else { |
248
|
|
|
$this->appendBlockBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
break; |
252
|
|
|
case 'table': |
253
|
|
|
$tableWrapper = $this->appendTableWrapperBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
254
|
|
|
$tableWrapper->appendTableBox($childDomElement, $element, $style, $parentBlock); |
255
|
|
|
|
256
|
|
|
break; |
257
|
|
|
case 'table-row-group': |
258
|
|
|
case 'table-header-group': |
259
|
|
|
case 'table-footer-group': |
260
|
|
|
$this->appendTableRowGroupBox($childDomElement, $element, $style, $parentBlock, $display); |
|
|
|
|
261
|
|
|
|
262
|
|
|
break; |
263
|
|
|
case 'table-row': |
264
|
|
|
$this->appendTableRowBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
265
|
|
|
|
266
|
|
|
break; |
267
|
|
|
case 'table-cell': |
268
|
|
|
$this->appendTableCellBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
269
|
|
|
|
270
|
|
|
break; |
271
|
|
|
case 'inline': |
272
|
|
|
$inline = $this->appendInlineBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
273
|
|
|
if (isset($inline) && $childDomElement instanceof \DOMText) { |
274
|
|
|
$inline->setAnonymous(true)->appendText($childDomElement, null, null, $parentBlock); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
break; |
278
|
|
|
case 'inline-block': |
279
|
|
|
$this->appendInlineBlockBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
280
|
|
|
|
281
|
|
|
break; |
282
|
|
|
case 'none': |
283
|
|
|
if ('style' === $childDomElement->nodeName) { |
284
|
|
|
$this->appendStyleBox($childDomElement, $element, $style, $parentBlock); |
|
|
|
|
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
break; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return $this; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|