renderer.cls.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: renderer.cls.php,v $
  6. * Created on: 2004-06-03
  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: renderer.cls.php 217 2010-03-11 23:03:57Z ryan.masten $ */
  39. /**
  40. * Concrete renderer
  41. *
  42. * Instantiates several specific renderers in order to render any given
  43. * frame.
  44. *
  45. * @access private
  46. * @package dompdf
  47. */
  48. class Renderer extends Abstract_Renderer {
  49. /**
  50. * Array of renderers for specific frame types
  51. *
  52. * @var array
  53. */
  54. protected $_renderers;
  55. /**
  56. * Cache of the callbacks array
  57. *
  58. * @var array
  59. */
  60. private $_callbacks;
  61. /**
  62. * Advance the canvas to the next page
  63. */
  64. function new_page() {
  65. $this->_canvas->new_page();
  66. }
  67. /**
  68. * Render frames recursively
  69. *
  70. * @param Frame $frame the frame to render
  71. */
  72. function render(Frame $frame) {
  73. global $_dompdf_debug;
  74. if ( $_dompdf_debug ) {
  75. echo $frame;
  76. flush();
  77. }
  78. $display = $frame->get_style()->display;
  79. switch ($display) {
  80. case "block":
  81. case "list-item":
  82. case "inline-block":
  83. case "table":
  84. case "table-row-group":
  85. case "table-header-group":
  86. case "table-footer-group":
  87. case "inline-table":
  88. $this->_render_frame("block", $frame);
  89. break;
  90. case "inline":
  91. if ( $frame->get_node()->nodeName === "#text" )
  92. $this->_render_frame("text", $frame);
  93. else
  94. $this->_render_frame("inline", $frame);
  95. break;
  96. case "table-cell":
  97. $this->_render_frame("table-cell", $frame);
  98. break;
  99. case "-dompdf-list-bullet":
  100. $this->_render_frame("list-bullet", $frame);
  101. break;
  102. case "-dompdf-image":
  103. $this->_render_frame("image", $frame);
  104. break;
  105. case "none":
  106. $node = $frame->get_node();
  107. if ( $node->nodeName === "script" ) {
  108. if ( $node->getAttribute("type") === "text/php" ||
  109. $node->getAttribute("language") === "php" ) {
  110. // Evaluate embedded php scripts
  111. $this->_render_frame("php", $frame);
  112. }
  113. elseif ( $node->getAttribute("type") === "text/javascript" ||
  114. $node->getAttribute("language") === "javascript" ) {
  115. // Insert JavaScript
  116. $this->_render_frame("javascript", $frame);
  117. }
  118. }
  119. // Don't render children, so skip to next iter
  120. return;
  121. default:
  122. break;
  123. }
  124. // Check for begin frame callback
  125. $this->_check_callbacks("begin_frame", $frame);
  126. foreach ($frame->get_children() as $child)
  127. $this->render($child);
  128. // Check for end frame callback
  129. $this->_check_callbacks("end_frame", $frame);
  130. }
  131. /**
  132. * Check for callbacks that need to be performed when a given event
  133. * gets triggered on a frame
  134. *
  135. * @param string $event the type of event
  136. * @param Frame $frame the frame that event is triggered on
  137. */
  138. protected function _check_callbacks($event, $frame) {
  139. if (!isset($this->_callbacks)) {
  140. $this->_callbacks = $this->_dompdf->get_callbacks();
  141. }
  142. if (is_array($this->_callbacks) && isset($this->_callbacks[$event])) {
  143. $info = array(0 => $this->_canvas, "canvas" => $this->_canvas,
  144. 1 => $frame, "frame" => $frame);
  145. $fs = $this->_callbacks[$event];
  146. foreach ($fs as $f) {
  147. if (is_callable($f)) {
  148. if (is_array($f)) {
  149. $f[0]->$f[1]($info);
  150. } else {
  151. $f($info);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * Render a single frame
  159. *
  160. * Creates Renderer objects on demand
  161. *
  162. * @param string $type type of renderer to use
  163. * @param Frame $frame the frame to render
  164. */
  165. protected function _render_frame($type, $frame) {
  166. if ( !isset($this->_renderers[$type]) ) {
  167. switch ($type) {
  168. case "block":
  169. $this->_renderers["block"] = new Block_Renderer($this->_dompdf);
  170. break;
  171. case "inline":
  172. $this->_renderers["inline"] = new Inline_Renderer($this->_dompdf);
  173. break;
  174. case "text":
  175. $this->_renderers["text"] = new Text_Renderer($this->_dompdf);
  176. break;
  177. case "image":
  178. $this->_renderers["image"] = new Image_Renderer($this->_dompdf);
  179. break;
  180. case "table-cell":
  181. $this->_renderers["table-cell"] = new Table_Cell_Renderer($this->_dompdf);
  182. break;
  183. case "list-bullet":
  184. $this->_renderers["list-bullet"] = new List_Bullet_Renderer($this->_dompdf);
  185. break;
  186. case "php":
  187. $this->_renderers["php"] = new PHP_Evaluator($this->_canvas);
  188. break;
  189. case "javascript":
  190. $this->_renderers["javascript"] = new Javascript_Embedder($this->_dompdf);
  191. break;
  192. }
  193. }
  194. $this->_renderers[$type]->render($frame);
  195. }
  196. }