PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / dompdf / dompdf / src / LineBox.php

LineBox.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor_prefixed/dompdf/dompdf/src/LineBox.php

352 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package dompdf
5 * @link https://github.com/dompdf/dompdf
6 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7 */
8 namespace WCPOS\Vendor\Dompdf;
9
10 use WCPOS\Vendor\Dompdf\FrameDecorator\AbstractFrameDecorator;
11 use WCPOS\Vendor\Dompdf\FrameDecorator\Block;
12 use WCPOS\Vendor\Dompdf\FrameDecorator\ListBullet;
13 use WCPOS\Vendor\Dompdf\FrameDecorator\Page;
14 use WCPOS\Vendor\Dompdf\FrameReflower\Text as TextFrameReflower;
15 use WCPOS\Vendor\Dompdf\Positioner\Inline as InlinePositioner;
16 use Iterator;
17 /**
18 * The line box class
19 *
20 * This class represents a line box
21 * http://www.w3.org/TR/CSS2/visuren.html#line-box
22 *
23 * @package dompdf
24 */
25 class LineBox
26 {
27 /**
28 * @var Block
29 */
30 protected $_block_frame;
31 /**
32 * @var AbstractFrameDecorator[]
33 */
34 protected $_frames = [];
35 /**
36 * @var ListBullet[]
37 */
38 protected $list_markers = [];
39 /**
40 * @var int
41 */
42 public $wc = 0;
43 /**
44 * @var float
45 */
46 public $y = 0.0;
47 /**
48 * @var float
49 */
50 public $w = 0.0;
51 /**
52 * @var float
53 */
54 public $h = 0.0;
55 /**
56 * @var float
57 */
58 public $left = 0.0;
59 /**
60 * @var float
61 */
62 public $right = 0.0;
63 /**
64 * @var AbstractFrameDecorator
65 */
66 public $tallest_frame = null;
67 /**
68 * @var bool[]
69 */
70 public $floating_blocks = [];
71 /**
72 * @var bool
73 */
74 public $br = \false;
75 /**
76 * Whether the line box contains any inline-positioned frames.
77 *
78 * @var bool
79 */
80 public $inline = \false;
81 /**
82 * @var int
83 */
84 public static $max_float_reflows = 10000;
85 /**
86 * FIXME smelly hack, used by the get_float_offsets method.
87 *
88 * @var int
89 */
90 private static $float_offset_anti_infinite_loop = 10000;
91 /**
92 * @param Block $frame the Block containing this line
93 * @param float $y
94 */
95 public function __construct(Block $frame, float $y = 0.0)
96 {
97 $this->_block_frame = $frame;
98 $this->_frames = [];
99 $this->y = $y;
100 $this->get_float_offsets();
101 }
102 /**
103 * Returns the floating elements inside the first floating parent
104 *
105 * @param Page $root
106 *
107 * @return Frame[]
108 */
109 public function get_floats_inside(Page $root) : array
110 {
111 $floating_frames = $root->get_floating_frames();
112 if (\count($floating_frames) == 0) {
113 return $floating_frames;
114 }
115 // Find nearest floating element
116 $p = $this->_block_frame;
117 while ($p->get_style()->float === "none") {
118 $parent = $p->get_parent();
119 if (!$parent) {
120 break;
121 }
122 $p = $parent;
123 }
124 if ($p == $root) {
125 return $floating_frames;
126 }
127 $parent = $p;
128 $childs = [];
129 foreach ($floating_frames as $_floating) {
130 $p = $_floating->get_parent();
131 while (($p = $p->get_parent()) && $p !== $parent) {
132 }
133 if ($p) {
134 $childs[] = $p;
135 }
136 }
137 return $childs;
138 }
139 /**
140 * Resets the anti-infinite-loop counter for the get_float_offsets method.
141 */
142 public static function reset_float_reflow_limit() : void
143 {
144 self::$float_offset_anti_infinite_loop = self::$max_float_reflows;
145 }
146 public function get_float_offsets() : void
147 {
148 $reflower = $this->_block_frame->get_reflower();
149 if (!$reflower) {
150 return;
151 }
152 $cb_w = null;
153 $block = $this->_block_frame;
154 $root = $block->get_root();
155 if (!$root) {
156 return;
157 }
158 $style = $this->_block_frame->get_style();
159 $floating_frames = $this->get_floats_inside($root);
160 $inside_left_floating_width = 0;
161 $inside_right_floating_width = 0;
162 $outside_left_floating_width = 0;
163 $outside_right_floating_width = 0;
164 foreach ($floating_frames as $child_key => $floating_frame) {
165 $floating_frame_parent = $floating_frame->get_parent();
166 $id = $floating_frame->get_id();
167 if (isset($this->floating_blocks[$id])) {
168 continue;
169 }
170 $float = $floating_frame->get_style()->float;
171 $floating_width = $floating_frame->get_margin_width();
172 if (!$cb_w) {
173 $cb_w = $floating_frame->get_containing_block("w");
174 }
175 $line_w = $this->get_width();
176 if (!$floating_frame->_float_next_line && $cb_w <= $line_w + $floating_width && $cb_w > $line_w) {
177 $floating_frame->_float_next_line = \true;
178 continue;
179 }
180 // If the child is still shifted by the floating element
181 if (self::$float_offset_anti_infinite_loop-- > 0 && $floating_frame->get_position("y") + $floating_frame->get_margin_height() >= $this->y && $block->get_position("x") + $block->get_margin_width() >= $floating_frame->get_position("x")) {
182 if ($float === "left") {
183 if ($floating_frame_parent === $this->_block_frame) {
184 $inside_left_floating_width += $floating_width;
185 } else {
186 $outside_left_floating_width += $floating_width;
187 }
188 } elseif ($float === "right") {
189 if ($floating_frame_parent === $this->_block_frame) {
190 $inside_right_floating_width += $floating_width;
191 } else {
192 $outside_right_floating_width += $floating_width;
193 }
194 }
195 $this->floating_blocks[$id] = \true;
196 } else {
197 $root->remove_floating_frame($child_key);
198 }
199 }
200 $this->left += $inside_left_floating_width;
201 if ($outside_left_floating_width > 0 && $outside_left_floating_width > (float) $style->length_in_pt($style->margin_left) + (float) $style->length_in_pt($style->padding_left)) {
202 $this->left += $outside_left_floating_width - (float) $style->length_in_pt($style->margin_left) - (float) $style->length_in_pt($style->padding_left);
203 }
204 $this->right += $inside_right_floating_width;
205 if ($outside_right_floating_width > 0 && $outside_right_floating_width > (float) $style->length_in_pt($style->margin_left) + (float) $style->length_in_pt($style->padding_right)) {
206 $this->right += $outside_right_floating_width - (float) $style->length_in_pt($style->margin_right) - (float) $style->length_in_pt($style->padding_right);
207 }
208 }
209 /**
210 * @return float
211 */
212 public function get_width() : float
213 {
214 return $this->left + $this->w + $this->right;
215 }
216 /**
217 * @return Block
218 */
219 public function get_block_frame() : Block
220 {
221 return $this->_block_frame;
222 }
223 /**
224 * @return AbstractFrameDecorator[]
225 */
226 public function &get_frames() : array
227 {
228 return $this->_frames;
229 }
230 /**
231 * @return bool
232 */
233 public function is_empty() : bool
234 {
235 return $this->_frames === [];
236 }
237 /**
238 * @param AbstractFrameDecorator $frame
239 */
240 public function add_frame(Frame $frame) : void
241 {
242 $this->_frames[] = $frame;
243 if ($frame->get_positioner() instanceof InlinePositioner) {
244 $this->inline = \true;
245 }
246 }
247 /**
248 * Remove the frame at the given index and all following frames from the
249 * line.
250 *
251 * @param int $index
252 */
253 public function remove_frames(int $index) : void
254 {
255 $lastIndex = \count($this->_frames) - 1;
256 if ($index < 0 || $index > $lastIndex) {
257 return;
258 }
259 for ($i = $lastIndex; $i >= $index; $i--) {
260 $f = $this->_frames[$i];
261 unset($this->_frames[$i]);
262 $this->w -= $f->get_margin_width();
263 }
264 // Reset array indices
265 $this->_frames = \array_values($this->_frames);
266 // Recalculate the height of the line
267 $h = 0.0;
268 $this->inline = \false;
269 foreach ($this->_frames as $f) {
270 $h = \max($h, $f->get_margin_height());
271 if ($f->get_positioner() instanceof InlinePositioner) {
272 $this->inline = \true;
273 }
274 }
275 $this->h = $h;
276 }
277 /**
278 * Get the `outside` positioned list markers to be vertically aligned with
279 * the line box.
280 *
281 * @return ListBullet[]
282 */
283 public function get_list_markers() : array
284 {
285 return $this->list_markers;
286 }
287 /**
288 * Add a list marker to the line box.
289 *
290 * The list marker is only added for the purpose of vertical alignment, it
291 * is not actually added to the list of frames of the line box.
292 */
293 public function add_list_marker(ListBullet $marker) : void
294 {
295 $this->list_markers[] = $marker;
296 }
297 /**
298 * An iterator of all list markers and inline positioned frames of the line
299 * box.
300 *
301 * @return Iterator<AbstractFrameDecorator>
302 */
303 public function frames_to_align() : Iterator
304 {
305 yield from $this->list_markers;
306 foreach ($this->_frames as $frame) {
307 if ($frame->get_positioner() instanceof InlinePositioner) {
308 (yield $frame);
309 }
310 }
311 }
312 /**
313 * Trim trailing whitespace from the line.
314 */
315 public function trim_trailing_ws() : void
316 {
317 $lastIndex = \count($this->_frames) - 1;
318 if ($lastIndex < 0) {
319 return;
320 }
321 $lastFrame = $this->_frames[$lastIndex];
322 $reflower = $lastFrame->get_reflower();
323 if ($reflower instanceof TextFrameReflower && !$lastFrame->is_pre()) {
324 $reflower->trim_trailing_ws();
325 $this->recalculate_width();
326 }
327 }
328 /**
329 * Recalculate LineBox width based on the contained frames total width.
330 *
331 * @return float
332 */
333 public function recalculate_width() : float
334 {
335 $width = 0.0;
336 foreach ($this->_frames as $frame) {
337 $width += $frame->get_margin_width();
338 }
339 return $this->w = $width;
340 }
341 public function __toString() : string
342 {
343 $props = ["wc", "y", "w", "h", "left", "right", "br"];
344 $s = "";
345 foreach ($props as $prop) {
346 $s .= "{$prop}: " . $this->{$prop} . "\n";
347 }
348 $s .= \count($this->_frames) . " frames\n";
349 return $s;
350 }
351 }
352