/vendor/dompdf/dompdf/src/FrameReflower/Page.php

https://gitlab.com/ealexis.t/kiosco · PHP · 199 lines · 117 code · 34 blank · 48 comment · 27 complexity · 24cd82b4999cb81b704580f14df77891 MD5 · raw file

  1. <?php
  2. /**
  3. * @package dompdf
  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
  8. */
  9. namespace Dompdf\FrameReflower;
  10. use Dompdf\Frame;
  11. use Dompdf\FrameDecorator\Block as BlockFrameDecorator;
  12. use Dompdf\FrameDecorator\Page as PageFrameDecorator;
  13. /**
  14. * Reflows pages
  15. *
  16. * @package dompdf
  17. */
  18. class Page extends AbstractFrameReflower
  19. {
  20. /**
  21. * Cache of the callbacks array
  22. *
  23. * @var array
  24. */
  25. private $_callbacks;
  26. /**
  27. * Cache of the canvas
  28. *
  29. * @var \Dompdf\Canvas
  30. */
  31. private $_canvas;
  32. function __construct(PageFrameDecorator $frame)
  33. {
  34. parent::__construct($frame);
  35. }
  36. function apply_page_style(Frame $frame, $page_number)
  37. {
  38. $style = $frame->get_style();
  39. $page_styles = $style->get_stylesheet()->get_page_styles();
  40. // http://www.w3.org/TR/CSS21/page.html#page-selectors
  41. if (count($page_styles) > 1) {
  42. $odd = $page_number % 2 == 1;
  43. $first = $page_number == 1;
  44. $style = clone $page_styles["base"];
  45. // FIXME RTL
  46. if ($odd && isset($page_styles[":right"])) {
  47. $style->merge($page_styles[":right"]);
  48. }
  49. if ($odd && isset($page_styles[":odd"])) {
  50. $style->merge($page_styles[":odd"]);
  51. }
  52. // FIXME RTL
  53. if (!$odd && isset($page_styles[":left"])) {
  54. $style->merge($page_styles[":left"]);
  55. }
  56. if (!$odd && isset($page_styles[":even"])) {
  57. $style->merge($page_styles[":even"]);
  58. }
  59. if ($first && isset($page_styles[":first"])) {
  60. $style->merge($page_styles[":first"]);
  61. }
  62. $frame->set_style($style);
  63. }
  64. }
  65. //........................................................................
  66. /**
  67. * Paged layout:
  68. * http://www.w3.org/TR/CSS21/page.html
  69. */
  70. function reflow(BlockFrameDecorator $block = null)
  71. {
  72. $fixed_children = array();
  73. $prev_child = null;
  74. $child = $this->_frame->get_first_child();
  75. $current_page = 0;
  76. while ($child) {
  77. $this->apply_page_style($this->_frame, $current_page + 1);
  78. $style = $this->_frame->get_style();
  79. // Pages are only concerned with margins
  80. $cb = $this->_frame->get_containing_block();
  81. $left = $style->length_in_pt($style->margin_left, $cb["w"]);
  82. $right = $style->length_in_pt($style->margin_right, $cb["w"]);
  83. $top = $style->length_in_pt($style->margin_top, $cb["h"]);
  84. $bottom = $style->length_in_pt($style->margin_bottom, $cb["h"]);
  85. $content_x = $cb["x"] + $left;
  86. $content_y = $cb["y"] + $top;
  87. $content_width = $cb["w"] - $left - $right;
  88. $content_height = $cb["h"] - $top - $bottom;
  89. // Only if it's the first page, we save the nodes with a fixed position
  90. if ($current_page == 0) {
  91. $children = $child->get_children();
  92. foreach ($children as $onechild) {
  93. if ($onechild->get_style()->position === "fixed") {
  94. $fixed_children[] = $onechild->deep_copy();
  95. }
  96. }
  97. $fixed_children = array_reverse($fixed_children);
  98. }
  99. $child->set_containing_block($content_x, $content_y, $content_width, $content_height);
  100. // Check for begin reflow callback
  101. $this->_check_callbacks("begin_page_reflow", $child);
  102. //Insert a copy of each node which have a fixed position
  103. if ($current_page >= 1) {
  104. foreach ($fixed_children as $fixed_child) {
  105. $child->insert_child_before($fixed_child->deep_copy(), $child->get_first_child());
  106. }
  107. }
  108. $child->reflow();
  109. $next_child = $child->get_next_sibling();
  110. // Check for begin render callback
  111. $this->_check_callbacks("begin_page_render", $child);
  112. // Render the page
  113. $this->_frame->get_renderer()->render($child);
  114. // Check for end render callback
  115. $this->_check_callbacks("end_page_render", $child);
  116. if ($next_child) {
  117. $this->_frame->next_page();
  118. }
  119. // Wait to dispose of all frames on the previous page
  120. // so callback will have access to them
  121. if ($prev_child) {
  122. $prev_child->dispose(true);
  123. }
  124. $prev_child = $child;
  125. $child = $next_child;
  126. $current_page++;
  127. }
  128. // Dispose of previous page if it still exists
  129. if ($prev_child) {
  130. $prev_child->dispose(true);
  131. }
  132. }
  133. //........................................................................
  134. /**
  135. * Check for callbacks that need to be performed when a given event
  136. * gets triggered on a page
  137. *
  138. * @param string $event the type of event
  139. * @param Frame $frame the frame that event is triggered on
  140. */
  141. protected function _check_callbacks($event, $frame)
  142. {
  143. if (!isset($this->_callbacks)) {
  144. $dompdf = $this->_frame->get_dompdf();
  145. $this->_callbacks = $dompdf->get_callbacks();
  146. $this->_canvas = $dompdf->get_canvas();
  147. }
  148. if (is_array($this->_callbacks) && isset($this->_callbacks[$event])) {
  149. $info = array(
  150. 0 => $this->_canvas, "canvas" => $this->_canvas,
  151. 1 => $frame, "frame" => $frame,
  152. );
  153. $fs = $this->_callbacks[$event];
  154. foreach ($fs as $f) {
  155. if (is_callable($f)) {
  156. if (is_array($f)) {
  157. $f[0]->{$f[1]}($info);
  158. } else {
  159. $f($info);
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }