4 * @link http://dompdf.github.com/
5 * @author Benj Carson <benjcarson@digitaljunkies.ca>
6 * @author Fabien Ménager <fabien.menager@gmail.com>
7 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
9 namespace Dompdf\FrameReflower
;
12 use Dompdf\FrameDecorator\Block
as BlockFrameDecorator
;
13 use Dompdf\FrameDecorator\Page
as PageFrameDecorator
;
20 class Page
extends AbstractFrameReflower
24 * Cache of the callbacks array
39 * @param PageFrameDecorator $frame
41 function __construct(PageFrameDecorator
$frame)
43 parent
::__construct($frame);
50 function apply_page_style(Frame
$frame, $page_number)
52 $style = $frame->get_style();
53 $page_styles = $style->get_stylesheet()->get_page_styles();
55 // http://www.w3.org/TR/CSS21/page.html#page-selectors
56 if (count($page_styles) > 1) {
57 $odd = $page_number %
2 == 1;
58 $first = $page_number == 1;
60 $style = clone $page_styles["base"];
63 if ($odd && isset($page_styles[":right"])) {
64 $style->merge($page_styles[":right"]);
67 if ($odd && isset($page_styles[":odd"])) {
68 $style->merge($page_styles[":odd"]);
72 if (!$odd && isset($page_styles[":left"])) {
73 $style->merge($page_styles[":left"]);
76 if (!$odd && isset($page_styles[":even"])) {
77 $style->merge($page_styles[":even"]);
80 if ($first && isset($page_styles[":first"])) {
81 $style->merge($page_styles[":first"]);
84 $frame->set_style($style);
90 * http://www.w3.org/TR/CSS21/page.html
92 * @param BlockFrameDecorator|null $block
94 function reflow(BlockFrameDecorator
$block = null)
96 $fixed_children = array();
98 $child = $this->_frame
->get_first_child();
102 $this->apply_page_style($this->_frame
, $current_page +
1);
104 $style = $this->_frame
->get_style();
106 // Pages are only concerned with margins
107 $cb = $this->_frame
->get_containing_block();
108 $left = (float)$style->length_in_pt($style->margin_left
, $cb["w"]);
109 $right = (float)$style->length_in_pt($style->margin_right
, $cb["w"]);
110 $top = (float)$style->length_in_pt($style->margin_top
, $cb["h"]);
111 $bottom = (float)$style->length_in_pt($style->margin_bottom
, $cb["h"]);
113 $content_x = $cb["x"] +
$left;
114 $content_y = $cb["y"] +
$top;
115 $content_width = $cb["w"] - $left - $right;
116 $content_height = $cb["h"] - $top - $bottom;
118 // Only if it's the first page, we save the nodes with a fixed position
119 if ($current_page == 0) {
120 $children = $child->get_children();
121 foreach ($children as $onechild) {
122 if ($onechild->get_style()->position
=== "fixed") {
123 $fixed_children[] = $onechild->deep_copy();
126 $fixed_children = array_reverse($fixed_children);
129 $child->set_containing_block($content_x, $content_y, $content_width, $content_height);
131 // Check for begin reflow callback
132 $this->_check_callbacks("begin_page_reflow", $child);
134 //Insert a copy of each node which have a fixed position
135 if ($current_page >= 1) {
136 foreach ($fixed_children as $fixed_child) {
137 $child->insert_child_before($fixed_child->deep_copy(), $child->get_first_child());
142 $next_child = $child->get_next_sibling();
144 // Check for begin render callback
145 $this->_check_callbacks("begin_page_render", $child);
148 $this->_frame
->get_renderer()->render($child);
150 // Check for end render callback
151 $this->_check_callbacks("end_page_render", $child);
154 $this->_frame
->next_page();
157 // Wait to dispose of all frames on the previous page
158 // so callback will have access to them
160 $prev_child->dispose(true);
162 $prev_child = $child;
163 $child = $next_child;
167 // Dispose of previous page if it still exists
169 $prev_child->dispose(true);
174 * Check for callbacks that need to be performed when a given event
175 * gets triggered on a page
177 * @param string $event the type of event
178 * @param Frame $frame the frame that event is triggered on
180 protected function _check_callbacks($event, $frame)
182 if (!isset($this->_callbacks
)) {
183 $dompdf = $this->_frame
->get_dompdf();
184 $this->_callbacks
= $dompdf->get_callbacks();
185 $this->_canvas
= $dompdf->get_canvas();
188 if (is_array($this->_callbacks
) && isset($this->_callbacks
[$event])) {
190 0 => $this->_canvas
, "canvas" => $this->_canvas
,
191 1 => $frame, "frame" => $frame,
193 $fs = $this->_callbacks
[$event];
194 foreach ($fs as $f) {
195 if (is_callable($f)) {
197 $f[0]->{$f[1]}($info);