page_frame_reflower.cls.php 5.3 KB

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