LineBox.php
7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* @package dompdf
* @link http://dompdf.github.com/
* @author Fabien Ménager <fabien.menager@gmail.com>
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
namespace Dompdf;
use Dompdf\Frame;
use Dompdf\FrameDecorator\Block;
use Dompdf\FrameDecorator\Page;
/**
* The line box class
*
* This class represents a line box
* http://www.w3.org/TR/CSS2/visuren.html#line-box
*
* @package dompdf
*/
class LineBox
{
/**
* @var Block
*/
protected $_block_frame;
/**
* @var Frame[]
*/
protected $_frames = array();
/**
* @var integer
*/
public $wc = 0;
/**
* @var float
*/
public $y = null;
/**
* @var float
*/
public $w = 0.0;
/**
* @var float
*/
public $h = 0.0;
/**
* @var float
*/
public $left = 0.0;
/**
* @var float
*/
public $right = 0.0;
/**
* @var Frame
*/
public $tallest_frame = null;
/**
* @var bool[]
*/
public $floating_blocks = array();
/**
* @var bool
*/
public $br = false;
/**
* Class constructor
*
* @param Block $frame the Block containing this line
* @param int $y
*/
public function __construct(Block $frame, $y = 0)
{
$this->_block_frame = $frame;
$this->_frames = array();
$this->y = $y;
$this->get_float_offsets();
}
/**
* Returns the floating elements inside the first floating parent
*
* @param Page $root
*
* @return Frame[]
*/
public function get_floats_inside(Page $root)
{
$floating_frames = $root->get_floating_frames();
if (count($floating_frames) == 0) {
return $floating_frames;
}
// Find nearest floating element
$p = $this->_block_frame;
while ($p->get_style()->float === "none") {
$parent = $p->get_parent();
if (!$parent) {
break;
}
$p = $parent;
}
if ($p == $root) {
return $floating_frames;
}
$parent = $p;
$childs = array();
foreach ($floating_frames as $_floating) {
$p = $_floating->get_parent();
while (($p = $p->get_parent()) && $p !== $parent) ;
if ($p) {
$childs[] = $p;
}
}
return $childs;
}
/**
*
*/
public function get_float_offsets()
{
static $anti_infinite_loop = 10000; // FIXME smelly hack
$reflower = $this->_block_frame->get_reflower();
if (!$reflower) {
return;
}
$cb_w = null;
$block = $this->_block_frame;
$root = $block->get_root();
if (!$root) {
return;
}
$style = $this->_block_frame->get_style();
$floating_frames = $this->get_floats_inside($root);
$inside_left_floating_width = 0;
$inside_right_floating_width = 0;
$outside_left_floating_width = 0;
$outside_right_floating_width = 0;
foreach ($floating_frames as $child_key => $floating_frame) {
$floating_frame_parent = $floating_frame->get_parent();
$id = $floating_frame->get_id();
if (isset($this->floating_blocks[$id])) {
continue;
}
$float = $floating_frame->get_style()->float;
$floating_width = $floating_frame->get_margin_width();
if (!$cb_w) {
$cb_w = $floating_frame->get_containing_block("w");
}
$line_w = $this->get_width();
if (!$floating_frame->_float_next_line && ($cb_w <= $line_w + $floating_width) && ($cb_w > $line_w)) {
$floating_frame->_float_next_line = true;
continue;
}
// If the child is still shifted by the floating element
if ($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")
) {
if ($float === "left") {
if ($floating_frame_parent === $this->_block_frame) {
$inside_left_floating_width += $floating_width;
} else {
$outside_left_floating_width += $floating_width;
}
} elseif ($float === "right") {
if ($floating_frame_parent === $this->_block_frame) {
$inside_right_floating_width += $floating_width;
} else {
$outside_right_floating_width += $floating_width;
}
}
$this->floating_blocks[$id] = true;
} // else, the floating element won't shift anymore
else {
$root->remove_floating_frame($child_key);
}
}
$this->left += $inside_left_floating_width;
if ($outside_left_floating_width > (float)$style->length_in_pt($style->margin_left) + (float)$style->length_in_pt($style->padding_left)) {
$this->left += $outside_left_floating_width - (float)$style->length_in_pt($style->margin_left) - (float)$style->length_in_pt($style->padding_left);
}
$this->right += $inside_right_floating_width;
if ($outside_right_floating_width > (float)$style->length_in_pt($style->margin_left) + (float)$style->length_in_pt($style->padding_right)) {
$this->right += $outside_right_floating_width - (float)$style->length_in_pt($style->margin_right) - (float)$style->length_in_pt($style->padding_right);
}
}
/**
* @return float
*/
public function get_width()
{
return $this->left + $this->w + $this->right;
}
/**
* @return Block
*/
public function get_block_frame()
{
return $this->_block_frame;
}
/**
* @return Frame[]
*/
function &get_frames()
{
return $this->_frames;
}
/**
* @param Frame $frame
*/
public function add_frame(Frame $frame)
{
$this->_frames[] = $frame;
}
/**
* Recalculate LineBox width based on the contained frames total width.
*
* @return float
*/
public function recalculate_width()
{
$width = 0;
foreach ($this->get_frames() as $frame) {
$width += $frame->calculate_auto_width();
}
return $this->w = $width;
}
/**
* @return string
*/
public function __toString()
{
$props = array("wc", "y", "w", "h", "left", "right", "br");
$s = "";
foreach ($props as $prop) {
$s .= "$prop: " . $this->$prop . "\n";
}
$s .= count($this->_frames) . " frames\n";
return $s;
}
/*function __get($prop) {
if (!isset($this->{"_$prop"})) return;
return $this->{"_$prop"};
}*/
}
/*
class LineBoxList implements Iterator {
private $_p = 0;
private $_lines = array();
}
*/