-
Notifications
You must be signed in to change notification settings - Fork 2
/
box.page.margin.class.php
476 lines (387 loc) · 19.3 KB
/
box.page.margin.class.php
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
/**
* @abstract
*/
class BoxPageMargin extends GenericContainerBox {
/**
* @param $at_rule CSSAtRuleMarginBox At-rule object describing margin box to be created
* @return Object Object of concrete BoxPageMargin descendant type
*/
function &create(&$pipeline, $at_rule) {
switch ($at_rule->getSelector()) {
case CSS_MARGIN_BOX_SELECTOR_TOP:
$box =& new BoxPageMarginTop($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_TOP_LEFT_CORNER:
$box =& new BoxPageMarginTopLeftCorner($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_TOP_LEFT:
$box =& new BoxPageMarginTopLeft($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_TOP_CENTER:
$box =& new BoxPageMarginTopCenter($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT:
$box =& new BoxPageMarginTopRight($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT_CORNER:
$box =& new BoxPageMarginTopRightCorner($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_BOTTOM:
$box =& new BoxPageMarginBottom($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT_CORNER:
$box =& new BoxPageMarginBottomLeftCorner($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT:
$box =& new BoxPageMarginBottomLeft($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_BOTTOM_CENTER:
$box =& new BoxPageMarginBottomCenter($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT:
$box =& new BoxPageMarginBottomRight($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT_CORNER:
$box =& new BoxPageMarginBottomRightCorner($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_LEFT_TOP:
$box =& new BoxPageMarginLeftTop($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_LEFT_MIDDLE:
$box =& new BoxPageMarginLeftMiddle($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_LEFT_BOTTOM:
$box =& new BoxPageMarginLeftBottom($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_RIGHT_TOP:
$box =& new BoxPageMarginRightTop($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_RIGHT_MIDDLE:
$box =& new BoxPageMarginRightMiddle($pipeline, $at_rule);
break;
case CSS_MARGIN_BOX_SELECTOR_RIGHT_BOTTOM:
$box =& new BoxPageMarginRightBottom($pipeline, $at_rule);
break;
default:
trigger_error("Unknown selector type", E_USER_ERROR);
};
return $box;
}
function BoxPageMargin(&$pipeline, $at_rule) {
$state =& $pipeline->get_current_css_state();
$state->pushDefaultState();
$root = null;
$at_rule->css->apply($root, $state, $pipeline);
$this->GenericContainerBox();
$this->readCSS($state);
$state->pushDefaultstate();
/**
* Check whether 'content' or '-html2ps-html-content' properties had been defined
* (if both properties are defined, -html2ps-html-content takes precedence)
*/
$raw_html_content =& $at_rule->get_css_property(CSS_HTML2PS_HTML_CONTENT);
$html_content = $raw_html_content->render($pipeline->get_counters());
if ($html_content !== '') {
// We should wrap html_content in DIV tag,
// as we treat only the very first box of the resulting DOM tree as margin box content
$html_content = html2xhtml("<div>".$html_content."</div>");
$tree = TreeBuilder::build($html_content);
$tree_root = traverse_dom_tree_pdf($tree);
$body_box =& create_pdf_box($tree_root, $pipeline);
$box =& $body_box->content[0];
} else {
$raw_content =& $at_rule->get_css_property(CSS_CONTENT);
$content = $raw_content->render($pipeline->get_counters());
$box =& InlineBox::create_from_text($content,
WHITESPACE_PRE_LINE,
$pipeline);
}
$this->add_child($box);
$state->popState();
$state->popState();
}
function get_cell_baseline() {
return 0;
}
function reflow(&$driver, &$media, $boxes) {
$context = new FlowContext;
$this->_position($media, $boxes, $context);
$this->setCSSProperty(CSS_WIDTH, new WCConstant($this->get_width()));
$this->put_height_constraint(new HCConstraint(array($this->height, false),
null,
null));
$this->reflow_content($context);
/**
* Apply vertical-align (behave like table cell)
*/
$va = CSSVerticalAlign::value2pdf($this->get_css_property(CSS_VERTICAL_ALIGN));
$va->apply_cell($this,$this->get_full_height(),0);
}
function show(&$driver) {
$this->offset(0, $driver->offset);
$this->show_fixed($driver);
}
function _calc_sizes($full_width, $left, $center, $right) {
$context = new FlowContext;
$left_width = $left->get_max_width($context);
$center_width = $center->get_max_width($context);
$right_width = $right->get_max_width($context);
$calculated_left_width = 0;
$calculated_center_width = 0;
$calculated_right_width = 0;
if ($center_width > 0) {
$calculated_center_width = $full_width * $center_width / ($center_width + 2*max($left_width, $right_width));
$calculated_left_width = ($full_width - $calculated_center_width) / 2;
$calculated_right_width = $calculated_left_width;
} elseif ($left_width == 0 && $right_width == 0) {
$calculated_center_width = 0;
$calculated_left_width = 0;
$calculated_right_width = 0;
} elseif ($left_width == 0) {
$calculated_center_width = 0;
$calculated_left_width = 0;
$calculated_right_width = $full_width;
} elseif ($right_width == 0) {
$calculated_center_width = 0;
$calculated_left_width = $full_width;
$calculated_right_width = 0;
} else {
$calculated_center_width = 0;
$calculated_left_width = $full_width * $left_width / ($left_width + $right_width);
$calculated_right_width = $full_width - $calculated_left_width;
};
return array($calculated_left_width,
$calculated_center_width,
$calculated_right_width);
}
}
class BoxPageMarginTop extends BoxPageMargin {
function _position($media, $boxes, $context) {
$this->put_left($this->get_extra_left() + 0);
$this->put_top(-$this->get_extra_top() +mm2pt($media->height()));
$this->put_full_width(mm2pt($media->width()));
$this->put_full_height(mm2pt($media->margins['top']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginTopLeftCorner extends BoxPageMargin {
function _position($media, $boxes, $context) {
$this->put_left($this->get_extra_left() + 0);
$this->put_top(-$this->get_extra_top() +mm2pt($media->height()));
$this->put_full_width(mm2pt($media->margins['left']));
$this->put_full_height(mm2pt($media->margins['top']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginTopLeft extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_width()),
$boxes[CSS_MARGIN_BOX_SELECTOR_TOP_LEFT],
$boxes[CSS_MARGIN_BOX_SELECTOR_TOP_CENTER],
$boxes[CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT]);
$this->put_left($this->get_extra_left() + mm2pt($media->margins['left']));
$this->put_top(-$this->get_extra_top() +mm2pt($media->height()));
$this->put_full_width($left);
$this->put_full_height(mm2pt($media->margins['top']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginTopCenter extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_width()),
$boxes[CSS_MARGIN_BOX_SELECTOR_TOP_LEFT],
$boxes[CSS_MARGIN_BOX_SELECTOR_TOP_CENTER],
$boxes[CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT]);
$this->put_left($this->get_extra_left() + mm2pt($media->margins['left']) + $left);
$this->put_top(-$this->get_extra_top() +mm2pt($media->height()));
$this->put_full_width($center);
$this->put_full_height(mm2pt($media->margins['top']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginTopRight extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_width()),
$boxes[CSS_MARGIN_BOX_SELECTOR_TOP_LEFT],
$boxes[CSS_MARGIN_BOX_SELECTOR_TOP_CENTER],
$boxes[CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT]);
$this->put_left($this->get_extra_left() + mm2pt($media->margins['left']) + $left + $center);
$this->put_top(-$this->get_extra_top() +mm2pt($media->height()));
$this->put_full_width($right);
$this->put_full_height(mm2pt($media->margins['top']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginTopRightCorner extends BoxPageMargin {
function _position($media, $boxes, $context) {
$this->put_left($this->get_extra_left() + mm2pt($media->width() - $media->margins['right']));
$this->put_top(-$this->get_extra_top() +mm2pt($media->height()));
$this->put_full_width(mm2pt($media->margins['right']));
$this->put_full_height(mm2pt($media->margins['top']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginBottomLeftCorner extends BoxPageMargin {
function _position($media, $boxes, $context) {
$this->put_left($this->get_extra_left() + 0);
$this->put_top(-$this->get_extra_top() +mm2pt($media->margins['bottom']));
$this->put_full_width(mm2pt($media->margins['left']));
$this->put_full_height(mm2pt($media->margins['bottom']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginBottomLeft extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_width()),
$boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT],
$boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_CENTER],
$boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT]);
$this->put_left($this->get_extra_left() + mm2pt($media->margins['left']));
$this->put_top(-$this->get_extra_top() +mm2pt($media->margins['bottom']));
$this->put_full_width($left);
$this->put_full_height(mm2pt($media->margins['bottom']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginBottomCenter extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_width()),
$boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT],
$boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_CENTER],
$boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT]);
$this->put_left($this->get_extra_left() + mm2pt($media->margins['left']) + $left);
$this->put_top(-$this->get_extra_top() +mm2pt($media->margins['bottom']));
$this->put_full_width($center);
$this->put_full_height(mm2pt($media->margins['bottom']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginBottomRight extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_width()),
$boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT],
$boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_CENTER],
$boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT]);
$this->put_left($this->get_extra_left() + mm2pt($media->margins['left']) + $left + $center);
$this->put_top(-$this->get_extra_top() +mm2pt($media->margins['bottom']));
$this->put_full_width($right);
$this->put_full_height(mm2pt($media->margins['bottom']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginBottomRightCorner extends BoxPageMargin {
function _position($media, $boxes, $context) {
$this->put_left($this->get_extra_left() + mm2pt($media->width() - $media->margins['right']));
$this->put_top(-$this->get_extra_top() +mm2pt($media->margins['bottom']));
$this->put_full_width(mm2pt($media->margins['right']));
$this->put_full_height(mm2pt($media->margins['top']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginBottom extends BoxPageMargin {
function _position($media, $boxes, $context) {
$this->put_left($this->get_extra_left() + 0);
$this->put_top(-$this->get_extra_top() +mm2pt($media->margins['bottom']));
$this->put_full_width(mm2pt($media->width()));
$this->put_full_height(mm2pt($media->margins['bottom']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginLeftTop extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_height()),
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_TOP],
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_MIDDLE],
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_BOTTOM]);
$this->put_left($this->get_extra_left() + 0);
$this->put_top(-$this->get_extra_top() +mm2pt($media->height() - $media->margins['top']));
$this->put_full_height($left);
$this->put_full_width(mm2pt($media->margins['left']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginLeftMiddle extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_height()),
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_TOP],
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_MIDDLE],
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_BOTTOM]);
$this->put_left($this->get_extra_left() + 0);
$this->put_top(-$this->get_extra_top() +mm2pt($media->height() - $media->margins['top']) - $left);
$this->put_full_height($center);
$this->put_full_width(mm2pt($media->margins['left']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginLeftBottom extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_height()),
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_TOP],
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_MIDDLE],
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_BOTTOM]);
$this->put_left($this->get_extra_left() + 0);
$this->put_top(-$this->get_extra_top() +mm2pt($media->height() - $media->margins['top']) - $left - $center);
$this->put_full_height($right);
$this->put_full_width(mm2pt($media->margins['left']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginRightTop extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_height()),
$boxes[CSS_MARGIN_BOX_SELECTOR_RIGHT_TOP],
$boxes[CSS_MARGIN_BOX_SELECTOR_RIGHT_MIDDLE],
$boxes[CSS_MARGIN_BOX_SELECTOR_RIGHT_BOTTOM]);
$this->put_left($this->get_extra_left() + mm2pt($media->width() - $media->margins['right']));
$this->put_top(-$this->get_extra_top() +mm2pt($media->height() - $media->margins['top']));
$this->put_full_height($left);
$this->put_full_width(mm2pt($media->margins['right']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginRightMiddle extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_height()),
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_TOP],
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_MIDDLE],
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_BOTTOM]);
$this->put_left($this->get_extra_left() + mm2pt($media->width() - $media->margins['right']));
$this->put_top(-$this->get_extra_top() +mm2pt($media->height() - $media->margins['top']) - $left);
$this->put_full_height($center);
$this->put_full_width(mm2pt($media->margins['right']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
class BoxPageMarginRightBottom extends BoxPageMargin {
function _position($media, $boxes, $context) {
list($left, $center, $right) = $this->_calc_sizes(mm2pt($media->real_height()),
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_TOP],
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_MIDDLE],
$boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_BOTTOM]);
$this->put_left($this->get_extra_left() + mm2pt($media->width() - $media->margins['right']));
$this->put_top(-$this->get_extra_top() + mm2pt($media->height() - $media->margins['top']) - $left - $center);
$this->put_full_height($right);
$this->put_full_width(mm2pt($media->margins['right']));
$this->_current_x = $this->get_left();
$this->_current_y = $this->get_top();
}
}
?>