page_frame_reflower.cls.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: page_frame_reflower.cls.php,v $
  6. * Created on: 2004-06-16
  7. *
  8. * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library in the file LICENSE.LGPL; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. * 02111-1307 USA
  24. *
  25. * Alternatively, you may distribute this software under the terms of the
  26. * PHP License, version 3.0 or later. A copy of this license should have
  27. * been distributed with this file in the file LICENSE.PHP . If this is not
  28. * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
  29. *
  30. * The latest version of DOMPDF might be available at:
  31. * http://www.dompdf.com/
  32. *
  33. * @link http://www.dompdf.com/
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @package dompdf
  37. */
  38. /* $Id: page_frame_reflower.cls.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  39. /**
  40. * Reflows pages
  41. *
  42. * @access private
  43. * @package dompdf
  44. */
  45. class Page_Frame_Reflower extends Frame_Reflower {
  46. /**
  47. * Cache of the callbacks array
  48. *
  49. * @var array
  50. */
  51. private $_callbacks;
  52. /**
  53. * Cache of the canvas
  54. *
  55. * @var Canvas
  56. */
  57. private $_canvas;
  58. function __construct(Page_Frame_Decorator $frame) { parent::__construct($frame); }
  59. //........................................................................
  60. function reflow() {
  61. $style = $this->_frame->get_style();
  62. // Paged layout:
  63. // http://www.w3.org/TR/CSS21/page.html
  64. // Pages are only concerned with margins
  65. $cb = $this->_frame->get_containing_block();
  66. $left = $style->length_in_pt($style->margin_left, $cb["w"]);
  67. $right = $style->length_in_pt($style->margin_right, $cb["w"]);
  68. $top = $style->length_in_pt($style->margin_top, $cb["w"]);
  69. $bottom = $style->length_in_pt($style->margin_bottom, $cb["w"]);
  70. $content_x = $cb["x"] + $left;
  71. $content_y = $cb["y"] + $top;
  72. $content_width = $cb["w"] - $left - $right;
  73. $content_height = $cb["h"] - $top - $bottom;
  74. $prev_child = null;
  75. $child = $this->_frame->get_first_child();
  76. while ($child) {
  77. $child->set_containing_block($content_x, $content_y, $content_width, $content_height);
  78. // Check for begin reflow callback
  79. $this->_check_callbacks("begin_page_reflow", $child);
  80. $child->reflow();
  81. $next_child = $child->get_next_sibling();
  82. // Check for begin render callback
  83. $this->_check_callbacks("begin_page_render", $child);
  84. // Render the page
  85. $this->_frame->get_renderer()->render($child);
  86. // Check for end render callback
  87. $this->_check_callbacks("end_page_render", $child);
  88. if ( $next_child )
  89. {
  90. $this->_frame->next_page();
  91. }
  92. // Wait to dispose of all frames on the previous page
  93. // so callback will have access to them
  94. if ( $prev_child )
  95. {
  96. $prev_child->dispose(true);
  97. }
  98. $prev_child = $child;
  99. $child = $next_child;
  100. }
  101. // Dispose of previous page if it still exists
  102. if ( $prev_child )
  103. {
  104. $prev_child->dispose(true);
  105. }
  106. }
  107. //........................................................................
  108. /**
  109. * Check for callbacks that need to be performed when a given event
  110. * gets triggered on a page
  111. *
  112. * @param string $event the type of event
  113. * @param Frame $frame the frame that event is triggered on
  114. */
  115. protected function _check_callbacks($event, $frame) {
  116. if (!isset($this->_callbacks)) {
  117. $dompdf = $this->_frame->get_dompdf();
  118. $this->_callbacks = $dompdf->get_callbacks();
  119. $this->_canvas = $dompdf->get_canvas();
  120. }
  121. if (is_array($this->_callbacks) && isset($this->_callbacks[$event])) {
  122. $info = array(0 => $this->_canvas, "canvas" => $this->_canvas,
  123. 1 => $frame, "frame" => $frame);
  124. $fs = $this->_callbacks[$event];
  125. foreach ($fs as $f) {
  126. if (is_callable($f)) {
  127. if (is_array($f)) {
  128. $f[0]->$f[1]($info);
  129. } else {
  130. $f($info);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }