PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/pdf/pipeline.class.php

http://simpleinvoices.googlecode.com/
PHP | 1209 lines | 860 code | 238 blank | 111 comment | 71 complexity | 31c10b324d216eaf863c54b9269da7fb MD5 | raw file
   1<?php
   2
   3require_once(HTML2PS_DIR.'utils_array.php');
   4require_once(HTML2PS_DIR.'utils_graphic.php');
   5require_once(HTML2PS_DIR.'utils_url.php');
   6require_once(HTML2PS_DIR.'utils_text.php');
   7require_once(HTML2PS_DIR.'utils_units.php');
   8require_once(HTML2PS_DIR.'utils_number.php');
   9
  10require_once(HTML2PS_DIR.'value.color.php');
  11
  12require_once(HTML2PS_DIR.'config.parse.php');
  13
  14require_once(HTML2PS_DIR.'flow_context.class.inc.php');
  15require_once(HTML2PS_DIR.'flow_viewport.class.inc.php');
  16
  17require_once(HTML2PS_DIR.'output._interface.class.php');
  18require_once(HTML2PS_DIR.'output._generic.class.php');
  19require_once(HTML2PS_DIR.'output._generic.pdf.class.php');
  20require_once(HTML2PS_DIR.'output._generic.ps.class.php');
  21require_once(HTML2PS_DIR.'output.pdflib.old.class.php');
  22require_once(HTML2PS_DIR.'output.pdflib.1.6.class.php');
  23require_once(HTML2PS_DIR.'output.fpdf.class.php');
  24require_once(HTML2PS_DIR.'output.fastps.class.php');
  25require_once(HTML2PS_DIR.'output.fastps.l2.class.php');
  26require_once(HTML2PS_DIR.'output.png.class.php');
  27// require_once(HTML2PS_DIR.'output.pcl.class.php');
  28
  29require_once(HTML2PS_DIR.'stubs.common.inc.php');
  30
  31require_once(HTML2PS_DIR.'media.layout.inc.php');
  32
  33require_once(HTML2PS_DIR.'box.php');
  34require_once(HTML2PS_DIR.'box.generic.php');
  35require_once(HTML2PS_DIR.'box.generic.formatted.php');
  36require_once(HTML2PS_DIR.'box.container.php');
  37require_once(HTML2PS_DIR.'box.generic.inline.php');
  38require_once(HTML2PS_DIR.'box.inline.php');
  39require_once(HTML2PS_DIR.'box.inline.control.php');
  40
  41require_once(HTML2PS_DIR.'font.class.php');
  42require_once(HTML2PS_DIR.'font_factory.class.php');
  43
  44require_once(HTML2PS_DIR.'box.br.php');
  45require_once(HTML2PS_DIR.'box.block.php');
  46require_once(HTML2PS_DIR.'box.page.php');
  47require_once(HTML2PS_DIR.'box.page.margin.class.php');
  48require_once(HTML2PS_DIR.'box.body.php');
  49require_once(HTML2PS_DIR.'box.block.inline.php');
  50require_once(HTML2PS_DIR.'box.button.php');
  51require_once(HTML2PS_DIR.'box.button.submit.php');
  52require_once(HTML2PS_DIR.'box.button.reset.php');
  53require_once(HTML2PS_DIR.'box.checkbutton.php');
  54require_once(HTML2PS_DIR.'box.form.php');
  55require_once(HTML2PS_DIR.'box.frame.php');
  56require_once(HTML2PS_DIR.'box.iframe.php');
  57require_once(HTML2PS_DIR.'box.input.text.php');
  58require_once(HTML2PS_DIR.'box.input.textarea.php');
  59require_once(HTML2PS_DIR.'box.input.password.php');
  60require_once(HTML2PS_DIR.'box.legend.php');
  61require_once(HTML2PS_DIR.'box.list-item.php');
  62require_once(HTML2PS_DIR.'box.null.php');
  63require_once(HTML2PS_DIR.'box.radiobutton.php');
  64require_once(HTML2PS_DIR.'box.select.php');
  65require_once(HTML2PS_DIR.'box.table.php');
  66require_once(HTML2PS_DIR.'box.table.cell.php');
  67require_once(HTML2PS_DIR.'box.table.cell.fake.php');
  68require_once(HTML2PS_DIR.'box.table.row.php');
  69require_once(HTML2PS_DIR.'box.table.section.php');
  70
  71require_once(HTML2PS_DIR.'box.text.php');
  72require_once(HTML2PS_DIR.'box.text.string.php');
  73require_once(HTML2PS_DIR.'box.field.pageno.php');
  74require_once(HTML2PS_DIR.'box.field.pages.php');
  75
  76require_once(HTML2PS_DIR.'box.whitespace.php');
  77
  78require_once(HTML2PS_DIR.'box.img.php'); // Inherited from the text box!
  79require_once(HTML2PS_DIR.'box.input.img.php');
  80
  81require_once(HTML2PS_DIR.'box.utils.text-align.inc.php');
  82
  83require_once(HTML2PS_DIR.'manager.encoding.php');
  84
  85require_once(HTML2PS_DIR.'ps.unicode.inc.php');
  86require_once(HTML2PS_DIR.'ps.utils.inc.php');
  87require_once(HTML2PS_DIR.'ps.whitespace.inc.php');
  88
  89require_once(HTML2PS_DIR.'ps.image.encoder.inc.php');
  90require_once(HTML2PS_DIR.'ps.image.encoder.simple.inc.php');
  91require_once(HTML2PS_DIR.'ps.l2.image.encoder.stream.inc.php');
  92require_once(HTML2PS_DIR.'ps.l3.image.encoder.stream.inc.php');
  93
  94require_once(HTML2PS_DIR.'tag.body.inc.php');
  95require_once(HTML2PS_DIR.'tag.font.inc.php');
  96require_once(HTML2PS_DIR.'tag.frame.inc.php');
  97require_once(HTML2PS_DIR.'tag.input.inc.php');
  98require_once(HTML2PS_DIR.'tag.img.inc.php');
  99require_once(HTML2PS_DIR.'tag.select.inc.php');
 100require_once(HTML2PS_DIR.'tag.span.inc.php');
 101require_once(HTML2PS_DIR.'tag.table.inc.php');
 102require_once(HTML2PS_DIR.'tag.td.inc.php');
 103require_once(HTML2PS_DIR.'tag.utils.inc.php');
 104
 105require_once(HTML2PS_DIR.'tree.navigation.inc.php');
 106
 107require_once(HTML2PS_DIR.'html.attrs.inc.php');
 108
 109require_once(HTML2PS_DIR.'xhtml.autoclose.inc.php');
 110require_once(HTML2PS_DIR.'xhtml.utils.inc.php');
 111require_once(HTML2PS_DIR.'xhtml.tables.inc.php');
 112require_once(HTML2PS_DIR.'xhtml.p.inc.php');
 113require_once(HTML2PS_DIR.'xhtml.lists.inc.php');
 114require_once(HTML2PS_DIR.'xhtml.deflist.inc.php');
 115require_once(HTML2PS_DIR.'xhtml.script.inc.php');
 116require_once(HTML2PS_DIR.'xhtml.entities.inc.php');
 117require_once(HTML2PS_DIR.'xhtml.comments.inc.php');
 118require_once(HTML2PS_DIR.'xhtml.style.inc.php');
 119require_once(HTML2PS_DIR.'xhtml.selects.inc.php');
 120
 121require_once(HTML2PS_DIR.'background.image.php');
 122require_once(HTML2PS_DIR.'background.position.php');
 123
 124require_once(HTML2PS_DIR.'list-style.image.php');
 125
 126require_once(HTML2PS_DIR.'height.php');
 127require_once(HTML2PS_DIR.'width.php');
 128
 129require_once(HTML2PS_DIR.'css.counter.php');
 130require_once(HTML2PS_DIR.'css.counter.collection.php');
 131
 132require_once(HTML2PS_DIR.'css.colors.inc.php');
 133
 134require_once(HTML2PS_DIR.'css.constants.inc.php');
 135require_once(HTML2PS_DIR.'css.inc.php');
 136require_once(HTML2PS_DIR.'css.state.class.php');
 137require_once(HTML2PS_DIR.'css.cache.class.php');
 138require_once(HTML2PS_DIR.'css.property.handler.class.php');
 139require_once(HTML2PS_DIR.'css.property.stringset.class.php');
 140require_once(HTML2PS_DIR.'css.property.sub.class.php');
 141require_once(HTML2PS_DIR.'css.property.sub.field.class.php');
 142require_once(HTML2PS_DIR.'css.utils.inc.php');
 143require_once(HTML2PS_DIR.'css.parse.inc.php');
 144require_once(HTML2PS_DIR.'css.parse.media.inc.php');
 145
 146require_once(HTML2PS_DIR.'css.background.attachment.inc.php');
 147require_once(HTML2PS_DIR.'css.background.color.inc.php');
 148require_once(HTML2PS_DIR.'css.background.image.inc.php');
 149require_once(HTML2PS_DIR.'css.background.repeat.inc.php');
 150require_once(HTML2PS_DIR.'css.background.position.inc.php');
 151require_once(HTML2PS_DIR.'css.background.inc.php');
 152
 153require_once(HTML2PS_DIR.'css.border.inc.php');
 154require_once(HTML2PS_DIR.'css.border.style.inc.php');
 155require_once(HTML2PS_DIR.'css.border.collapse.inc.php');
 156require_once(HTML2PS_DIR.'css.bottom.inc.php');
 157require_once(HTML2PS_DIR.'css.clear.inc.php');
 158require_once(HTML2PS_DIR.'css.color.inc.php');
 159require_once(HTML2PS_DIR.'css.direction.inc.php');
 160require_once(HTML2PS_DIR.'css.html2ps.html.content.inc.php');
 161require_once(HTML2PS_DIR.'css.html2ps.pseudoelements.inc.php');
 162require_once(HTML2PS_DIR.'css.html2ps.pixels.php');
 163require_once(HTML2PS_DIR.'css.content.inc.php');
 164require_once(HTML2PS_DIR.'css.display.inc.php');
 165require_once(HTML2PS_DIR.'css.float.inc.php');
 166require_once(HTML2PS_DIR.'css.font.inc.php');
 167require_once(HTML2PS_DIR.'css.height.inc.php');
 168require_once(HTML2PS_DIR.'css.min-height.inc.php');
 169require_once(HTML2PS_DIR.'css.max-height.inc.php');
 170require_once(HTML2PS_DIR.'css.left.inc.php');
 171require_once(HTML2PS_DIR.'css.letter-spacing.inc.php');
 172
 173require_once(HTML2PS_DIR.'css.list-style-image.inc.php');
 174require_once(HTML2PS_DIR.'css.list-style-position.inc.php');
 175require_once(HTML2PS_DIR.'css.list-style-type.inc.php');
 176require_once(HTML2PS_DIR.'css.list-style.inc.php');
 177
 178require_once(HTML2PS_DIR.'css.margin.inc.php');
 179require_once(HTML2PS_DIR.'css.overflow.inc.php');
 180require_once(HTML2PS_DIR.'css.padding.inc.php');
 181
 182require_once(HTML2PS_DIR.'css.page.inc.php');
 183require_once(HTML2PS_DIR.'css.page-break.inc.php');
 184require_once(HTML2PS_DIR.'css.page-break-after.inc.php');
 185require_once(HTML2PS_DIR.'css.page-break-before.inc.php');
 186require_once(HTML2PS_DIR.'css.page-break-inside.inc.php');
 187require_once(HTML2PS_DIR.'css.orphans.inc.php');
 188require_once(HTML2PS_DIR.'css.size.inc.php');
 189require_once(HTML2PS_DIR.'css.widows.inc.php');
 190
 191require_once(HTML2PS_DIR.'css.position.inc.php');
 192require_once(HTML2PS_DIR.'css.right.inc.php');
 193require_once(HTML2PS_DIR.'css.property.declaration.php');
 194require_once(HTML2PS_DIR.'css.rules.inc.php');
 195require_once(HTML2PS_DIR.'css.ruleset.class.php');
 196require_once(HTML2PS_DIR.'css.selectors.inc.php');
 197require_once(HTML2PS_DIR.'css.table-layout.inc.php');
 198require_once(HTML2PS_DIR.'css.text-align.inc.php');
 199require_once(HTML2PS_DIR.'css.text-decoration.inc.php');
 200require_once(HTML2PS_DIR.'css.text-transform.inc.php');
 201require_once(HTML2PS_DIR.'css.text-indent.inc.php');
 202require_once(HTML2PS_DIR.'css.top.inc.php');
 203require_once(HTML2PS_DIR.'css.vertical-align.inc.php');
 204require_once(HTML2PS_DIR.'css.visibility.inc.php');
 205require_once(HTML2PS_DIR.'css.white-space.inc.php');
 206require_once(HTML2PS_DIR.'css.width.inc.php');
 207require_once(HTML2PS_DIR.'css.word-spacing.inc.php');
 208require_once(HTML2PS_DIR.'css.z-index.inc.php');
 209
 210require_once(HTML2PS_DIR.'css.pseudo.add.margin.inc.php');
 211require_once(HTML2PS_DIR.'css.pseudo.align.inc.php');
 212require_once(HTML2PS_DIR.'css.pseudo.cellspacing.inc.php');
 213require_once(HTML2PS_DIR.'css.pseudo.cellpadding.inc.php');
 214require_once(HTML2PS_DIR.'css.pseudo.form.action.inc.php');
 215require_once(HTML2PS_DIR.'css.pseudo.form.radiogroup.inc.php');
 216require_once(HTML2PS_DIR.'css.pseudo.link.destination.inc.php');
 217require_once(HTML2PS_DIR.'css.pseudo.link.target.inc.php');
 218require_once(HTML2PS_DIR.'css.pseudo.listcounter.inc.php');
 219require_once(HTML2PS_DIR.'css.pseudo.localalign.inc.php');
 220require_once(HTML2PS_DIR.'css.pseudo.nowrap.inc.php');
 221require_once(HTML2PS_DIR.'css.pseudo.table.border.inc.php');
 222
 223// After all CSS utilities and constants have been initialized, load the default (precomiled) CSS stylesheet
 224require_once(HTML2PS_DIR.'converter.class.php');
 225require_once(HTML2PS_DIR.'treebuilder.class.php');
 226require_once(HTML2PS_DIR.'image.class.php');
 227
 228require_once(HTML2PS_DIR.'fetched_data._interface.class.php');
 229require_once(HTML2PS_DIR.'fetched_data._html.class.php');
 230require_once(HTML2PS_DIR.'fetched_data.url.class.php');
 231require_once(HTML2PS_DIR.'fetched_data.file.class.php');
 232
 233require_once(HTML2PS_DIR.'filter.data._interface.class.php');
 234require_once(HTML2PS_DIR.'filter.data.doctype.class.php');
 235
 236require_once(HTML2PS_DIR.'filter.data.utf8.class.php');
 237require_once(HTML2PS_DIR.'filter.data.ucs2.class.php');
 238
 239require_once(HTML2PS_DIR.'filter.data.html2xhtml.class.php');
 240require_once(HTML2PS_DIR.'filter.data.xhtml2xhtml.class.php');
 241
 242require_once(HTML2PS_DIR.'parser._interface.class.php');
 243require_once(HTML2PS_DIR.'parser.xhtml.class.php');
 244
 245require_once(HTML2PS_DIR.'filter.pre._interface.class.php');
 246require_once(HTML2PS_DIR.'filter.pre.fields.class.php');
 247require_once(HTML2PS_DIR.'filter.pre.headfoot.class.php');
 248require_once(HTML2PS_DIR.'filter.pre.footnotes.class.php');
 249require_once(HTML2PS_DIR.'filter.pre.height-constraint.class.php');
 250
 251require_once(HTML2PS_DIR.'layout._interface.class.php');
 252require_once(HTML2PS_DIR.'layout.default.class.php');
 253require_once(HTML2PS_DIR.'layout.page.breaks.php');
 254
 255require_once(HTML2PS_DIR.'filter.post._interface.class.php');
 256require_once(HTML2PS_DIR.'filter.post.positioned.class.php');
 257require_once(HTML2PS_DIR.'filter.post.postponed.class.php');
 258
 259require_once(HTML2PS_DIR.'filter.output._interface.class.php');
 260require_once(HTML2PS_DIR.'filter.output.ps2pdf.class.php');
 261require_once(HTML2PS_DIR.'filter.output.gzip.class.php');
 262
 263require_once(HTML2PS_DIR.'destination._interface.class.php');
 264require_once(HTML2PS_DIR.'destination._http.class.php');
 265require_once(HTML2PS_DIR.'destination.browser.class.php');
 266require_once(HTML2PS_DIR.'destination.download.class.php');
 267require_once(HTML2PS_DIR.'destination.file.class.php');
 268
 269require_once(HTML2PS_DIR.'xml.validation.inc.php');
 270
 271require_once(HTML2PS_DIR.'content_type.class.php');
 272require_once(HTML2PS_DIR.'dispatcher.class.php');
 273require_once(HTML2PS_DIR.'observer.class.php');
 274
 275require_once(HTML2PS_DIR.'strategy.page.break.simple.php');
 276require_once(HTML2PS_DIR.'strategy.page.break.smart.php');
 277
 278require_once(HTML2PS_DIR.'strategy.link.rendering.normal.php');
 279require_once(HTML2PS_DIR.'strategy.position.absolute.php');
 280require_once(HTML2PS_DIR.'strategy.width.absolute.positioned.php');
 281require_once(HTML2PS_DIR.'autofix.url.php');
 282
 283require_once(HTML2PS_DIR.'fetcher._interface.class.php');
 284require_once(HTML2PS_DIR.'features/_factory.php');
 285
 286class Pipeline {
 287  var $fetchers;
 288  var $data_filters;
 289  var $error_message;
 290  var $parser;
 291  var $pre_tree_filters;
 292  var $layout_engine;
 293  var $post_tree_filters;
 294  var $output_driver;
 295  var $output_filters;
 296  var $destination;
 297
 298  var $_base_url;
 299
 300  var $_page_at_rules;
 301  var $_counters;
 302  var $_footnotes;
 303
 304  var $_cssState;
 305  var $_css;
 306  var $_defaultCSS;
 307
 308  var $_dispatcher;
 309
 310  var $_current_page_name;
 311
 312  var $_page_break_strategy;
 313
 314  function Pipeline() {
 315    $this->_counters = array();
 316    $this->_footnotes = array();
 317
 318    $this->_base_url = array('');
 319    $this->_reset_page_at_rules();
 320
 321    $this->pre_tree_filters = array();
 322
 323    $this->_dispatcher =& new Dispatcher();
 324
 325    $this->_dispatcher->add_event('before-page-heights');
 326    $this->_dispatcher->add_event('before-page');
 327    $this->_dispatcher->add_event('after-page');
 328    $this->_dispatcher->add_event('before-batch-item');
 329    $this->_dispatcher->add_event('after-batch-item');
 330    $this->_dispatcher->add_event('after-parse');
 331    $this->_dispatcher->add_event('before-document');
 332    $this->_dispatcher->add_event('after-document');
 333    $this->_dispatcher->add_event('before-batch');
 334    $this->_dispatcher->add_event('after-batch');
 335
 336    $this->_page_break_strategy = new StrategyPageBreakSimple();
 337  }
 338
 339  function add_feature($feature_name, $params = array()) {
 340    $feature_object =& FeatureFactory::get($feature_name);
 341    if (is_null($feature_object)) {
 342      die(sprintf('No feature "%s" found', $feature_name));
 343    };
 344
 345    $feature_object->install($this, $params);
 346  }
 347
 348  function add_fetcher(&$fetcher) {
 349    array_unshift($this->fetchers, $fetcher);
 350  }
 351
 352  function calculate_page_heights(&$media, &$box) {
 353    return $this->_page_break_strategy->run($this, $media, $box);
 354  }
 355
 356  function clear_box_id_map() {
 357    $GLOBALS['__html_box_id_map'] = array();
 358  }
 359
 360  function close() {
 361    $this->_dispatcher->fire('after-batch', array('pipeline' => &$this));
 362
 363    $this->output_driver->close();
 364    $this->_output();
 365    $this->output_driver->release();   
 366
 367    // Non HTML-specific cleanup
 368    //
 369    Image::clear_cache();
 370  }
 371
 372  function configure($options) {
 373    $defaults = array('compress'      => false,
 374                      'cssmedia'      => 'screen',
 375                      'debugbox'      => false,
 376                      'debugnoclip'   => false,
 377                      'draw_page_border' => false,
 378                      'encoding'      => '',
 379                      'html2xhtml'    => true,
 380                      'imagequality_workaround' => false,
 381                      'landscape'     => false,
 382                      'margins'       => array('left' => 30,
 383                                               'right' => 15,
 384                                               'top' => 15,
 385                                               'bottom' => 15),
 386                      'media'         => 'A4',
 387                      'method'        => 'fpdf',
 388                      'mode'          => 'html',
 389                      'output'        => 0,
 390                      'pagewidth'     => 800,
 391                      'pdfversion'    => "1.2",
 392                      'ps2pdf'        => false,
 393                      'pslevel'       => 3,
 394                      'renderfields'  => false,
 395                      'renderforms'   => false,
 396                      'renderimages'  => true,
 397                      'renderlinks'   => false,
 398                      'scalepoints'   => true,
 399                      'smartpagebreak' => true,
 400                      'transparency_workaround' => false
 401                      );
 402
 403    // As a reminder: If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.
 404    $GLOBALS['g_config'] = array_merge($defaults, $options);
 405
 406    // Note that CSS media names should be case-insensitive
 407    $GLOBALS['g_config']['cssmedia'] = strtolower($GLOBALS['g_config']['cssmedia']);
 408
 409    if ($GLOBALS['g_config']['smartpagebreak']) {
 410      $this->_page_break_strategy = new StrategyPageBreakSmart();
 411    } else {
 412      $this->_page_break_strategy = new StrategyPageBreakSimple();
 413    };
 414  }
 415
 416  function _addFootnote(&$note_call) {
 417    $this->_footnotes[] =& $note_call;
 418  }
 419
 420//   function _fillContent($content) {
 421//     $filled = "";
 422
 423//     while (preg_match("/^.*?('.*?'|\".*?\"|counter\(.*?\))(.*)$/", $content, $matches)) {
 424//       $data    = $matches[1];
 425//       $content = $matches[2];
 426      
 427//       if ($data{0} != '\'' && $data{0} != '"') {
 428//         $filled .= $this->_fillContentCounter($data);
 429//       } else {
 430//         $filled .= $this->_fillContentString($data);
 431//       };
 432//     };
 433
 434//     return $filled;
 435//   }
 436
 437//   function _fillContentString($content) {
 438//     $unescaped_content = css_process_escapes($content);
 439//     $unquoted_content = css_remove_value_quotes($unescaped_content);
 440//     return $unquoted_content;
 441//   }
 442
 443//   function _fillContentCounter($content) {
 444//     preg_match("/counter\((.*?)\)/", $content, $matches);
 445//     return $this->_getCounter($matches[1]);
 446//   }
 447
 448  function &get_counters() {
 449    $counter_collection =& new CSSCounterCollection();
 450
 451    foreach ($this->_counters as $counter_name => $counter_value) {
 452      $counter =& new CSSCounter($counter_name);
 453      $counter->set($counter_value);
 454      $counter_collection->add($counter);
 455    };
 456
 457    return $counter_collection;
 458  }
 459
 460  function &get_dispatcher() {
 461    return $this->_dispatcher;
 462  }
 463
 464  function _getCounter($counter) {
 465    if (isset($this->_counters[$counter])) { 
 466      return $this->_counters[$counter];
 467    };
 468
 469    /**
 470     * CSS  2.1:   Counters  that  are   not  in  the  scope   of  any
 471     * 'counter-reset',  are assumed  to have  been  reset to  0 by  a
 472     * 'counter-reset' on the root element.
 473     */
 474    return 0;
 475  }
 476
 477  function _resetCounter($counter, $value) {
 478    $this->_counters[$counter] = $value;
 479  }
 480
 481  function _incrementCounter($counter, $value) {
 482    $this->_counters[$counter] += $value;
 483  }
 484
 485  function add_at_rule_page($at_rule) {
 486    $selector =& $at_rule->getSelector();
 487    $type = $selector->get_type();
 488    $this->_page_at_rules[$type][] = $at_rule;
 489  }
 490
 491  function _reset_page_at_rules() {
 492    $this->_page_at_rules = array(CSS_PAGE_SELECTOR_ALL   => array(),
 493                                  CSS_PAGE_SELECTOR_FIRST => array(),
 494                                  CSS_PAGE_SELECTOR_LEFT  => array(),
 495                                  CSS_PAGE_SELECTOR_RIGHT => array(),
 496                                  CSS_PAGE_SELECTOR_NAMED => array());
 497  }
 498
 499  function &getDefaultCSS() {
 500    return $this->_defaultCSS;
 501  }
 502
 503  function &getCurrentCSS() {
 504    return $this->_css[0];
 505  }
 506
 507  function &getCurrentCSSState() {
 508    return $this->_cssState[0];
 509  }
 510
 511  function pushCSS() {
 512    array_unshift($this->_css, new CSSRuleset());
 513  }
 514
 515  function popCSS() {
 516    array_shift($this->_css);
 517  }
 518
 519  /**
 520   * Note that different pages  may define different margin boxes (for
 521   * example,  left and right  pages may  have different  headers). In
 522   * this  case, we  should  process  @page rules  in  order of  their
 523   * specificity (no selector  < :left / :right <  :first) and extract
 524   * margin boxes to be drawn
 525   *
 526   * @param $page_no Integer current page index (1-based)
 527   * @param $media 
 528   */
 529  function render_margin_boxes($page_no, &$media) {
 530    $boxes =& $this->reflow_margin_boxes($page_no, $media);
 531
 532    foreach ($boxes as $selector => $box) {
 533      $boxes[$selector]->show($this->output_driver);
 534    };
 535
 536    // Memleak fix
 537    for ($i=0, $size = count($boxes); $i < $size; $i++) {
 538      $boxes[$i]->destroy();
 539    };
 540    unset($boxes);
 541  }
 542
 543  function get_page_media($page_no, &$media) {
 544    $page_rules =& $this->get_page_rules($page_no);
 545    $size_landscape = $page_rules->getPropertyValue(CSS_SIZE);
 546    if (!is_null($size_landscape)) {
 547      $media->set_width($size_landscape['size']['width']);
 548      $media->set_height($size_landscape['size']['height']);
 549      $media->set_landscape($size_landscape['landscape']);
 550    };
 551
 552    $margins = $page_rules->getPropertyValue(CSS_MARGIN);
 553    if (!is_null($margins)) {
 554      $media->margins['left'] = $margins->left->calc(mm2pt($media->get_width())) / mm2pt(1) / pt2pt(1);
 555      $media->margins['right'] = $margins->right->calc(mm2pt($media->get_width())) / mm2pt(1) / pt2pt(1);
 556      $media->margins['top'] = $margins->top->calc(mm2pt($media->get_height())) / mm2pt(1) / pt2pt(1);
 557      $media->margins['bottom'] = $margins->bottom->calc(mm2pt($media->get_height())) / mm2pt(1) / pt2pt(1);
 558    };
 559
 560    $left_margin = $page_rules->getPropertyValue(CSS_MARGIN_LEFT);
 561    if (!is_null($left_margin)) {
 562      $media->margins['left'] = $left_margin->calc(mm2pt($media->get_width())) / mm2pt(1) / pt2pt(1);
 563    };
 564
 565    $right_margin = $page_rules->getPropertyValue(CSS_MARGIN_RIGHT);
 566    if (!is_null($right_margin)) {
 567      $media->margins['right'] = $right_margin->calc(mm2pt($media->get_width())) / mm2pt(1) / pt2pt(1);
 568    };
 569
 570    $top_margin = $page_rules->getPropertyValue(CSS_MARGIN_TOP);
 571    if (!is_null($top_margin)) {
 572      $media->margins['top'] = $top_margin->calc(mm2pt($media->get_height())) / mm2pt(1) / pt2pt(1);
 573    };
 574
 575    $bottom_margin = $page_rules->getPropertyValue(CSS_MARGIN_BOTTOM);
 576    if (!is_null($bottom_margin)) {
 577      $media->margins['bottom'] = $bottom_margin->calc(mm2pt($media->get_height())) / mm2pt(1) / pt2pt(1);
 578    };
 579
 580    $pixels = $page_rules->getPropertyValue(CSS_HTML2PS_PIXELS);
 581    if (!is_null($pixels)) {
 582      $media->set_pixels($pixels);
 583    };
 584  }
 585
 586  function &get_page_rules($page_no) {
 587    $collection =& new CSSPropertyCollection();
 588
 589    foreach ($this->_page_at_rules[CSS_PAGE_SELECTOR_ALL] as $rule) {
 590      $collection->merge($rule->css);
 591    };
 592
 593    /**
 594     * Check which one of :right/:left selector is applicable (assuming that first page matches :right)
 595     */
 596    if ($page_no % 2 == 0) {
 597      foreach ($this->_page_at_rules[CSS_PAGE_SELECTOR_LEFT] as $rule) {
 598        $collection->merge($rule->css);
 599      };
 600    } else {
 601      foreach ($this->_page_at_rules[CSS_PAGE_SELECTOR_RIGHT] as $rule) {
 602        $collection->merge($rule->css);
 603      };
 604    };
 605
 606    if ($page_no == 1) {
 607      foreach ($this->_page_at_rules[CSS_PAGE_SELECTOR_FIRST] as $rule) {
 608        $collection->merge($rule->css);
 609      };
 610    };
 611
 612    return $collection;
 613  }
 614
 615  function &reflow_page_box($page_no, &$media) {
 616    $rules =& $this->get_page_rules($page_no);
 617    $box =& BoxPage::create($this, $rules);
 618    $box->reflow($media);
 619    return $box;
 620  }
 621
 622  function render_page_box($page_no, &$media) {
 623    $box =& $this->reflow_page_box($page_no, $media);
 624    $box->show($this->output_driver);
 625    $box->destroy();
 626    unset($box);
 627  }
 628
 629  function &reflow_margin_boxes($page_no, &$media) {
 630    $at_rules = $this->_getMarginBoxes($page_no, $media);
 631
 632    $boxes = array();
 633    foreach ($at_rules as $at_rule) {
 634      $selector = $at_rule->getSelector();
 635      $boxes[$selector] =& BoxPageMargin::create($this, $at_rule);
 636    };
 637
 638    foreach ($boxes as $selector => $box) {
 639      $linebox_started     = false;
 640      $previous_whitespace = false;
 641      $boxes[$selector]->reflow_whitespace($linebox_started, $previous_whitespace);
 642      $boxes[$selector]->reflow_text($this->output_driver);
 643    };
 644
 645    foreach ($boxes as $selector => $box) {
 646      $boxes[$selector]->reflow($this->output_driver, 
 647                                $media,
 648                                $boxes);
 649    };
 650
 651    return $boxes;
 652  }
 653
 654  /**
 655   * Note that "+" operation on arrays will preserve existing elements; thus
 656   * we need to process @page rules in order of decreasing specificity
 657   *
 658   */
 659  function _getMarginBoxes($page_no, $media) {
 660    $applicable_margin_boxes = array();
 661
 662    /**
 663     * Check if :first selector is applicable
 664     */
 665    if ($page_no == 1) {
 666      foreach ($this->_page_at_rules[CSS_PAGE_SELECTOR_FIRST] as $rule) {
 667        $applicable_margin_boxes = $applicable_margin_boxes + $rule->getAtRuleMarginBoxes();
 668      };
 669    };
 670
 671    /**
 672     * Check which one of :right/:left selector is applicable (assuming that first page matches :right)
 673     */
 674    if ($page_no % 2 == 0) {
 675      foreach ($this->_page_at_rules[CSS_PAGE_SELECTOR_LEFT] as $rule) {
 676        $applicable_margin_boxes = $applicable_margin_boxes +  $rule->getAtRuleMarginBoxes();
 677      };
 678    } else {
 679      foreach ($this->_page_at_rules[CSS_PAGE_SELECTOR_RIGHT] as $rule) {
 680        $applicable_margin_boxes = $applicable_margin_boxes +  $rule->getAtRuleMarginBoxes();
 681      };
 682    };
 683
 684    /**
 685     * Extract margin boxes from plain @page rules
 686     */
 687    foreach ($this->_page_at_rules[CSS_PAGE_SELECTOR_ALL] as $rule) {
 688      $applicable_margin_boxes = $applicable_margin_boxes + $rule->getAtRuleMarginBoxes();
 689    };
 690
 691    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP])) { 
 692      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP] =& 
 693        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_TOP,$this); 
 694    };
 695
 696    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_LEFT_CORNER])) { 
 697      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_LEFT_CORNER] =& 
 698        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_TOP_LEFT_CORNER,$this); 
 699    };
 700
 701    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_LEFT])) { 
 702      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_LEFT] =& 
 703        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_TOP_LEFT,$this); 
 704    };
 705
 706    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_CENTER])) { 
 707      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_CENTER] =& 
 708        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_TOP_CENTER,$this); 
 709    };
 710
 711    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT])) { 
 712      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT] =& 
 713        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT,$this); 
 714    };
 715
 716    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT_CORNER])) { 
 717      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT_CORNER] =& 
 718        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT_CORNER,$this); 
 719    };
 720
 721    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM])) { 
 722      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM] =& 
 723        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_BOTTOM,$this); 
 724    };
 725
 726    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT_CORNER])) { 
 727      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT_CORNER] =& 
 728        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT_CORNER,$this); 
 729    };
 730
 731    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT])) { 
 732      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT] =& 
 733        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT,$this); 
 734    };
 735
 736    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_CENTER])) { 
 737      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_CENTER] =& 
 738        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_BOTTOM_CENTER,$this); 
 739    };
 740
 741    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT])) { 
 742      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT] =& 
 743        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT,$this); 
 744    };
 745
 746    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT_CORNER])) { 
 747      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT_CORNER] =& 
 748        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT_CORNER,$this); 
 749    };
 750
 751    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_TOP])) { 
 752      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_TOP] =& 
 753        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_LEFT_TOP,$this); 
 754    };
 755
 756    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_MIDDLE])) { 
 757      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_MIDDLE] =& 
 758        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_LEFT_MIDDLE,$this); 
 759    };
 760
 761    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_BOTTOM])) { 
 762      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_LEFT_BOTTOM] =& 
 763        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_LEFT_BOTTOM,$this); 
 764    };
 765
 766    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_RIGHT_TOP])) { 
 767      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_RIGHT_TOP] =& 
 768        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_RIGHT_TOP,$this); 
 769    };
 770
 771    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_RIGHT_MIDDLE])) { 
 772      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_RIGHT_MIDDLE] =& 
 773        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_RIGHT_MIDDLE,$this); 
 774    };
 775
 776    if (!isset($applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_RIGHT_BOTTOM])) { 
 777      $applicable_margin_boxes[CSS_MARGIN_BOX_SELECTOR_RIGHT_BOTTOM] =&
 778        new CSSAtRuleMarginBox(CSS_MARGIN_BOX_SELECTOR_RIGHT_BOTTOM,$this); 
 779    };
 780
 781    return $applicable_margin_boxes;
 782  }
 783
 784  function _process_item($data_id, &$media, $offset=0) {    
 785    $this->_dispatcher->fire('before-batch-item', array('pipeline' => &$this));
 786
 787    $box =& $this->_layout_item($data_id, $media, $offset, $context, $postponed_filter);
 788
 789    if (is_null($box)) {
 790      error_log(sprintf(_('Could not fetch: %s'), (string)$data_id));
 791      return true;
 792    };
 793
 794    $this->_show_item($box, $offset, $context, $media, $postponed_filter);
 795
 796    // Clear CSS for this item 
 797    $this->popCSS();
 798    $this->_defaultCSS = null;
 799
 800    // Memory leak fix: caused by circular references?
 801    $box->destroy();
 802
 803    $this->_dispatcher->fire('after-batch-item', array('pipeline' => &$this));
 804    return true;
 805  }
 806
 807  function _show_item(&$box, $offset, &$context, &$media, &$postponed_filter) {
 808    $context->sort_absolute_positioned_by_z_index();
 809
 810    $this->_dispatcher->fire('before-page-heights', array('pipeline' => &$this, 
 811                                                          'document' => &$box,
 812                                                          'media' => &$media));
 813
 814    // Make batch-processing offset
 815    $page_heights = $this->calculate_page_heights($media, $box);
 816    $box->offset(0, $offset);
 817
 818    $box->reflow_anchors($this->output_driver, $this->output_driver->anchors, $page_heights);
 819    
 820    $this->_dispatcher->fire('before-document', array('pipeline' => &$this, 
 821                                                      'document' => &$box,
 822                                                      'page-heights' => &$page_heights,
 823                                                      'media' => &$media));
 824
 825    $expected_pages = count($page_heights);
 826    $this->output_driver->set_expected_pages($expected_pages);
 827    $this->_resetCounter('pages', $expected_pages);
 828    $this->_resetCounter('page',  0);
 829
 830    // Output PDF pages using chosen PDF driver
 831    for ($i=0; $i<$expected_pages; $i++) {
 832      $this->get_page_media(1, $media);
 833
 834      $this->output_driver->update_media($media);
 835      $this->_setupScales($media);
 836
 837      $current_page_offset = $i == 0 ? 0 : $page_heights[$i-1];
 838      $current_page_height = $page_heights[$i];
 839
 840      $this->output_driver->next_page($current_page_offset);
 841
 842      // Preparen list of postponed (floating and relative-positioned) boxes for the current page
 843      $postponed_filter->process($box, null, $this);
 844
 845      $this->_resetCounter('footnote', 0);
 846      $this->_incrementCounter('page', 1);
 847
 848      $this->output_driver->save();
 849
 850      /**
 851       * Note that margin boxes should be rendered before 'setup_clip', as it will trim all
 852       * content rendered outside the 'main' page area
 853       */
 854      $this->render_margin_boxes($i+1, $media);
 855      $this->render_page_box($i+1, $media);
 856
 857      $this->output_driver->setPageHeight($current_page_height);
 858      $this->output_driver->setup_clip();
 859
 860      $this->_dispatcher->fire('before-page', array('pipeline' => &$this, 
 861                                                    'document' => &$box, 
 862                                                    'pageno' => $i));
 863
 864      if (is_null($box->show($this->output_driver))) { 
 865        error_log('Pipeline::_process_item: output routine failed');
 866        return null; 
 867      };
 868
 869      /**
 870       * Show postponed boxes - relative and floating boxes, as they should be 
 871       * shown over boxes on the same layer
 872       */
 873      $this->output_driver->show_postponed();
 874
 875      $this->renderAbsolutePositioned($context);
 876      $this->output_driver->restore();
 877      $this->renderFixedPositioned($context);
 878      $this->renderFootnotes();
 879
 880      global $g_config;
 881      if ($g_config['draw_page_border']) { 
 882        $this->output_driver->draw_page_border(); 
 883      };
 884
 885      $this->_dispatcher->fire('after-page', array('pipeline' => &$this, 
 886                                                   'document' => &$box, 
 887                                                   'pageno' => $i));
 888    };
 889
 890    $this->_dispatcher->fire('after-document', array('pipeline' => &$this, 
 891                                                     'document' => &$box));
 892  }
 893
 894  function _output() {
 895    $temporary_output_filename = $this->output_driver->get_filename();
 896
 897    for ($i=0; $i<count($this->output_filters); $i++) {
 898      $temporary_output_filename = $this->output_filters[$i]->process($temporary_output_filename);
 899    };
 900
 901    // Determine the content type of the result
 902    $content_type = null;
 903    $i = count($this->output_filters)-1;
 904    while (($i >= 0) && (is_null($content_type))) {
 905      $content_type = $this->output_filters[$i]->content_type();
 906      $i--;
 907    };
 908
 909    if (is_null($content_type)) {
 910      $content_type = $this->output_driver->content_type();
 911    };
 912
 913    $this->destination->process($temporary_output_filename, $content_type);
 914    unlink($temporary_output_filename);
 915  }
 916
 917  function set_destination(&$destination) {
 918    $this->destination =& $destination;
 919  }
 920
 921  function set_output_driver(&$output_driver) {
 922    $this->output_driver =& $output_driver;
 923  }
 924
 925  function &fetch($data_id) {
 926    if (count($this->fetchers) == 0) { 
 927      ob_start();
 928      include(HTML2PS_DIR.'/templates/error._no_fetchers.tpl');
 929      $this->error_message = ob_get_contents();
 930      ob_end_clean();
 931
 932      return null; 
 933    };
 934
 935    // Fetch data
 936    for ($i=0; $i<count($this->fetchers); $i++) {
 937      $data = $this->fetchers[$i]->get_data($data_id);
 938
 939      if ($data != null) {
 940        $this->push_base_url($this->fetchers[$i]->get_base_url());
 941        return $data;
 942      };
 943    };
 944
 945    if (defined('DEBUG_MODE')) {
 946      error_log(sprintf('Could not fetch %s', $data_id));
 947    };
 948
 949    $null = null;
 950    return $null;
 951  }
 952  
 953  function process($data_id, &$media) {
 954    return $this->process_batch(array($data_id), $media);
 955  }
 956
 957  function _setupScales(&$media) {
 958    global $g_config;
 959    global $g_px_scale;
 960    global $g_pt_scale;
 961
 962    $g_px_scale = floor(mm2pt($media->width() - $media->margins['left'] - $media->margins['right'])) / $media->pixels;
 963
 964    if ($g_config['scalepoints']) {
 965      $g_pt_scale = $g_px_scale * 1.33; // This is a magic number, just don't touch it, or everything will explode!
 966    } else {
 967      $g_pt_scale = 1.0;
 968    };
 969  }
 970
 971  /**
 972   * Processes an set of URLs ot once; every URL is rendered on the separate page and 
 973   * merged to one PDF file.
 974   *
 975   * Note: to reduce peak memory requirement, URLs are processed one-after-one.
 976   *
 977   * @param Array $data_id_array Array of page identifiers to be processed (usually URLs or files paths)
 978   * @param Media $media Object describing the media to render for (size, margins, orientaiton & resolution)
 979   */
 980  function process_batch($data_id_array, &$media) {
 981    $this->clear_box_id_map();
 982
 983    // Save and disable magic_quotes_runtime
 984    $mq_runtime = get_magic_quotes_runtime();
 985    set_magic_quotes_runtime(0);
 986
 987    $this->_prepare($media);
 988
 989    $this->_dispatcher->fire('before-batch', array('pipeline' => &$this));
 990
 991    $i = 0;
 992    $offset = 0;
 993    foreach ($data_id_array as $data_id) {      
 994      $this->_process_item($data_id, $media, $offset);
 995
 996      $i++;
 997      $offset = $this->output_driver->offset;
 998    };
 999
1000    $this->close();
1001
1002    // Restore magic_quotes_runtime setting
1003    set_magic_quotes_runtime($mq_runtime);
1004
1005    return true;
1006  }
1007
1008  function error_message() {
1009    $message = file_get_contents(HTML2PS_DIR.'/templates/error._header.tpl');
1010
1011    $message .= $this->error_message;
1012
1013    for ($i=0; $i<count($this->fetchers); $i++) {
1014      $message .= $this->fetchers[$i]->error_message();
1015    };
1016
1017    $message .= $this->output_driver->error_message();
1018    
1019    $message .= file_get_contents(HTML2PS_DIR.'/templates/error._footer.tpl');
1020    return $message;
1021  }
1022
1023  function push_base_url($url) {
1024    array_unshift($this->_base_url, $url);
1025  }
1026
1027  function pop_base_url() {
1028    array_shift($this->_base_url);
1029  }
1030
1031  function get_base_url() {
1032    return $this->_base_url[0];
1033  }
1034
1035  function &get_output_driver() {
1036    return $this->output_driver;
1037  }  
1038
1039  function guess_url($src) {
1040    return guess_url($src, $this->get_base_url());
1041  }
1042
1043  function renderFootnotes() {
1044    /**
1045     * Render every footnote defined (note-call element is visible) on a current page
1046     */
1047
1048    $footnote_y = $this->output_driver->getFootnoteTop() - FOOTNOTE_LINE_TOP_GAP - FOOTNOTE_LINE_BOTTOM_GAP;
1049    $footnote_x = $this->output_driver->getPageLeft();
1050    $footnotes_found = false;
1051
1052    foreach ($this->_footnotes as $footnote) {
1053      // Note that footnote area for current page have been already defined,
1054      // as show_foonote is called after note-call boxes were placed.
1055      if ($this->output_driver->contains($footnote->_note_call_box)) { 
1056        $footnotes_found = true;
1057        $footnote_y = $footnote->show_footnote($this->output_driver, 
1058                                               $footnote_x,
1059                                               $footnote_y);
1060        $footnote_y -= FOOTNOTE_GAP;
1061      };
1062    };
1063
1064    /**
1065     * Draw thin line separating footnotes from page content
1066     */
1067    if ($footnotes_found) {
1068      $this->output_driver->setrgbcolor(0,0,0);
1069      $this->output_driver->moveto($this->output_driver->getPageLeft(),
1070                                   $this->output_driver->getFootnoteTop() - FOOTNOTE_LINE_TOP_GAP);
1071      $this->output_driver->lineto($this->output_driver->getPageLeft() + $this->output_driver->getPageWidth()*FOOTNOTE_LINE_PERCENT/100,
1072                                   $this->output_driver->getFootnoteTop() - FOOTNOTE_LINE_TOP_GAP);
1073      $this->output_driver->stroke();
1074    };
1075  }
1076
1077  function renderAbsolutePositioned(&$context) {
1078    for ($j=0, $size = count($context->absolute_positioned); $j<$size; $j++) {
1079      $current_box =& $context->absolute_positioned[$j];
1080      if ($current_box->getCSSProperty(CSS_VISIBILITY) === VISIBILITY_VISIBLE) {
1081        $this->output_driver->save();
1082        $current_box->_setupClip($this->output_driver);
1083        if (is_null($current_box->show($this->output_driver))) {
1084          return null;
1085        };
1086        $this->output_driver->restore();
1087      };
1088    };
1089    $this->output_driver->show_postponed_in_absolute();
1090  }
1091
1092  function renderFixedPositioned(&$context) {
1093    for ($j=0, $size = count($context->fixed_positioned); $j<$size; $j++) {
1094      $current_box =& $context->fixed_positioned[$j];
1095      if ($current_box->getCSSProperty(CSS_VISIBILITY) === VISIBILITY_VISIBLE) {
1096        $this->output_driver->save();
1097        $current_box->_setupClip($this->output_driver);
1098        if (is_null($current_box->show_fixed($this->output_driver))) { 
1099          return null;
1100        };
1101        $this->output_driver->restore();
1102      };
1103    };
1104    $this->output_driver->show_postponed_in_fixed();
1105  }
1106
1107  function _prepare(&$media) {
1108    $this->_setupScales($media);
1109    $GLOBALS['g_media'] =& $media;
1110    $this->output_driver->reset($media);
1111  }
1112
1113  function &_layout_item($data_id, &$media, $offset, &$context, &$postponed_filter) {
1114    $this->_reset_page_at_rules();
1115
1116    $css_cache = CSSCache::get();
1117    $this->_defaultCSS = $css_cache->compile('resource://default.css', 
1118                                             file_get_contents(HTML2PS_DIR.'/default.css'));
1119    $this->_css = array();
1120    $this->pushCSS();
1121
1122    $this->_cssState = array(new CSSState(CSS::get()));
1123
1124    $font = $this->_cssState[0]->getProperty(CSS_FONT);
1125    $font->units2pt(0);
1126    $this->_cssState[0]->setProperty(CSS_FONT, $font);
1127
1128    $data = $this->fetch($data_id);
1129
1130    if (is_null($data)) { 
1131      $dummy = null;
1132      return $dummy;
1133    };
1134
1135    // Run raw data filters
1136    for ($i=0; $i<count($this->data_filters); $i++) {
1137      $data = $this->data_filters[$i]->process($data);
1138    };
1139
1140    // Parse the raw data
1141    $box =& $this->parser->process($data->get_content(), $this, $media);
1142
1143    $this->_dispatcher->fire('after-parse', array('pipeline' => &$this,
1144                                                  'document' => &$box,
1145                                                  'media' => $media));
1146
1147    /**
1148     * Run obligatory tree filters
1149     */
1150
1151    /**
1152     * height-constraint processing filter;
1153     */
1154    $filter = new PreTreeFilterHeightConstraint();
1155    $filter->process($box, $data, $this);
1156
1157    /**
1158     * Footnote support filter
1159     */
1160    $filter = new PreTreeFilterFootnotes();
1161    $filter->process($box, $data, $this);
1162
1163    // Run pre-layout tree filters
1164    for ($i=0, $size = count($this->pre_tree_filters); $i < $size; $i++) {
1165      $this->pre_tree_filters[$i]->process($box, $data, $this);
1166    };
1167
1168    $context = new FlowContext;
1169
1170    /**
1171     * Extract absolute/fixed positioned boxes
1172     */
1173    $positioned_filter = new PostTreeFilterPositioned($context);
1174    $positioned_filter->process($box, null, $this);
1175
1176    $postponed_filter = new PostTreeFilterPostponed($this->output_driver);
1177    $postponed_filter->process($box, null, $this);
1178
1179    $this->output_driver->prepare();
1180
1181    $status = $this->layout_engine->process($box, $media, $this->output_driver, $context);
1182    if (is_null($status)) { 
1183      error_log('Pipeline::_process_item: layout routine failed');
1184      $dummy = null;
1185      return $dummy;
1186    };
1187
1188    // Run post-layout tree filters
1189    for ($i=0; $i<count($this->post_tree_filters); $i++) {
1190      $this->post_tree_filters[$i]->process($box);
1191    };
1192
1193    return $box;
1194  }
1195
1196  function &getDispatcher() {
1197    return $this->_dispatcher;
1198  }
1199
1200  function get_current_page_name() {
1201    return $this->_current_page_name;
1202  }
1203
1204  function set_current_page_name($name) {
1205    $this->_current_page_name = $name;
1206  }
1207}
1208
1209?>