Blame view
sources/3rdparty/phpdocx/pdf/include/renderer.cls.php
7.77 KB
|
31b7f2792
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
<?php
/**
* DOMPDF - PHP5 HTML to PDF renderer
*
* File: $RCSfile: renderer.cls.php,v $
* Created on: 2004-06-03
*
* Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library in the file LICENSE.LGPL; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
* Alternatively, you may distribute this software under the terms of the
* PHP License, version 3.0 or later. A copy of this license should have
* been distributed with this file in the file LICENSE.PHP . If this is not
* the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
*
* The latest version of DOMPDF might be available at:
* http://www.dompdf.com/
*
* @link http://www.dompdf.com/
* @copyright 2004 Benj Carson
* @author Benj Carson <benjcarson@digitaljunkies.ca>
* @package dompdf
*/
/* $Id: renderer.cls.php 351 2011-01-19 20:27:02Z fabien.menager $ */
/**
* Concrete renderer
*
* Instantiates several specific renderers in order to render any given
* frame.
*
* @access private
* @package dompdf
*/
class Renderer extends Abstract_Renderer {
/**
* Array of renderers for specific frame types
*
* @var array
*/
protected $_renderers;
/**
* Cache of the callbacks array
*
* @var array
*/
private $_callbacks;
/**
* Class destructor
*/
function __destruct() {
clear_object($this);
}
/**
* Advance the canvas to the next page
*/
function new_page() {
$this->_canvas->new_page();
}
/**
* Render frames recursively
*
* @param Frame $frame the frame to render
*/
function render(Frame $frame) {
global $_dompdf_debug;
if ( $_dompdf_debug ) {
echo $frame;
flush();
}
$style = $frame->get_style();
$display = $style->display;
// Starts the CSS transformation
if ( $style->transform && is_array($style->transform) ) {
$this->_canvas->save();
list($x, $y, $w, $h) = $frame->get_padding_box();
$origin = $style->transform_origin;
foreach($style->transform as $transform) {
list($function, $values) = $transform;
if ( $function === "matrix" ) {
$function = "transform";
}
$values = array_map("floatval", $values);
$values[] = $x + $style->length_in_pt($origin[0], $style->width);
$values[] = $y + $style->length_in_pt($origin[1], $style->height);
call_user_func_array(array($this->_canvas, $function), $values);
}
}
switch ($display) {
case "block":
case "list-item":
case "inline-block":
case "table":
case "table-row-group":
case "table-header-group":
case "table-footer-group":
case "inline-table":
$this->_render_frame("block", $frame);
break;
case "inline":
if ( $frame->get_node()->nodeName === "#text" )
$this->_render_frame("text", $frame);
else
$this->_render_frame("inline", $frame);
break;
case "table-cell":
$this->_render_frame("table-cell", $frame);
break;
case "-dompdf-list-bullet":
$this->_render_frame("list-bullet", $frame);
break;
case "-dompdf-image":
$this->_render_frame("image", $frame);
break;
case "none":
$node = $frame->get_node();
if ( $node->nodeName === "script" ) {
if ( $node->getAttribute("type") === "text/php" ||
$node->getAttribute("language") === "php" ) {
// Evaluate embedded php scripts
$this->_render_frame("php", $frame);
}
elseif ( $node->getAttribute("type") === "text/javascript" ||
$node->getAttribute("language") === "javascript" ) {
// Insert JavaScript
$this->_render_frame("javascript", $frame);
}
}
// Don't render children, so skip to next iter
return;
default:
break;
}
// Check for begin frame callback
$this->_check_callbacks("begin_frame", $frame);
// Starts the overflow: hidden box
if ( $style->overflow === "hidden" ) {
list($x, $y, $w, $h) = $frame->get_padding_box();
$this->_canvas->clipping_rectangle($x, $y, $w, $h);
}
$page = $frame->get_root()->get_reflower();
foreach ($frame->get_children() as $child) {
$child_style = $child->get_style();
// Stacking context
if ( $child_style->z_index !== false && ($child_style->z_index !== "auto" || in_array($child_style->position, Style::$POSITIONNED_TYPES)) ) {
$z_index = ($child_style->z_index === "auto") ? 0 : intval($child_style->z_index);
$page->add_frame_to_stacking_context($child, $z_index);
$child_style->z_index = false;
}
else {
$this->render($child);
}
}
// Ends the overflow: hidden box
if ( $style->overflow === "hidden" ) {
$this->_canvas->clipping_end();
}
if ( $style->transform && is_array($style->transform) ) {
$this->_canvas->restore();
}
// Check for end frame callback
$this->_check_callbacks("end_frame", $frame);
}
/**
* Check for callbacks that need to be performed when a given event
* gets triggered on a frame
*
* @param string $event the type of event
* @param Frame $frame the frame that event is triggered on
*/
protected function _check_callbacks($event, $frame) {
if (!isset($this->_callbacks)) {
$this->_callbacks = $this->_dompdf->get_callbacks();
}
if (is_array($this->_callbacks) && isset($this->_callbacks[$event])) {
$info = array(0 => $this->_canvas, "canvas" => $this->_canvas,
1 => $frame, "frame" => $frame);
$fs = $this->_callbacks[$event];
foreach ($fs as $f) {
if (is_callable($f)) {
if (is_array($f)) {
$f[0]->$f[1]($info);
} else {
$f($info);
}
}
}
}
}
/**
* Render a single frame
*
* Creates Renderer objects on demand
*
* @param string $type type of renderer to use
* @param Frame $frame the frame to render
*/
protected function _render_frame($type, $frame) {
if ( !isset($this->_renderers[$type]) ) {
switch ($type) {
case "block":
$this->_renderers[$type] = new Block_Renderer($this->_dompdf);
break;
case "inline":
$this->_renderers[$type] = new Inline_Renderer($this->_dompdf);
break;
case "text":
$this->_renderers[$type] = new Text_Renderer($this->_dompdf);
break;
case "image":
$this->_renderers[$type] = new Image_Renderer($this->_dompdf);
break;
case "table-cell":
$this->_renderers[$type] = new Table_Cell_Renderer($this->_dompdf);
break;
case "list-bullet":
$this->_renderers[$type] = new List_Bullet_Renderer($this->_dompdf);
break;
case "php":
$this->_renderers[$type] = new PHP_Evaluator($this->_canvas);
break;
case "javascript":
$this->_renderers[$type] = new Javascript_Embedder($this->_dompdf);
break;
}
}
$this->_renderers[$type]->render($frame);
}
}
|