<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">&lt;?php
/**
 * DOMPDF - PHP5 HTML to PDF renderer
 *
 * File: $RCSfile: attribute_translator.cls.php,v $
 * Created on: 2004-09-13
 *
 * Copyright (c) 2004 - Benj Carson &lt;benjcarson@digitaljunkies.ca&gt;
 *
 * 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 &lt;benjcarson@digitaljunkies.ca&gt;
 * @package dompdf

 */

/* $Id: attribute_translator.cls.php 219 2010-03-11 23:18:31Z ryan.masten $ */

/**
 * Translates HTML 4.0 attributes into CSS rules
 *
 * @access private
 * @package dompdf
 */
class Attribute_Translator {
  
  // Munged data originally from
  // http://www.w3.org/TR/REC-html40/index/attributes.html
  //
  // thank you var_export() :D
  static private $__ATTRIBUTE_LOOKUP =
    array (//'caption' =&gt; array ( 'align' =&gt; '', ),
           'img_inner' =&gt;  // img tags actually end up wrapping img_inner elements
           array ('align' =&gt; array('bottom' =&gt; 'vertical-align: baseline;',
                                   'middle' =&gt; 'vertical-align: middle;',
                                   'top' =&gt; 'vertical-align: top;',
                                   'left' =&gt; 'float: left;',
                                   'right' =&gt; 'float: right;'),
                  'border' =&gt; 'border-width: %0.2f px;',
                  'height' =&gt; 'height: %s px;',
                  'hspace' =&gt; 'padding-left: %1$0.2f px; padding-right: %1$0.2f px;',
                  'vspace' =&gt; 'padding-top: %1$0.2f px; padding-bottom: %1$0.2f px;',
                  'width' =&gt; 'width: %s px;',
                  ),
           'table' =&gt;
           array ("align" =&gt; array('left' =&gt; 'margin-left: 0; margin-right: auto;',
                        'center' =&gt; 'margin-left: auto; margin-right: auto;',
                        'right' =&gt; 'margin-left: auto; margin-right: 0;'
                        ),
                  'bgcolor' =&gt; 'background-color: %s;',
                  'border' =&gt; '!set_table_border',
                  'cellpadding' =&gt; '!set_table_cellpadding',
                  'cellspacing' =&gt; 'border-spacing: %0.2f; border-collapse: separate;',
                  'frame' =&gt; array('void' =&gt; 'border-style: none;',
                                   'above' =&gt; 'border-top-style: solid;',
                                   'below' =&gt; 'border-bottom-style: solid;',
                                   'hsides' =&gt; 'border-left-style: solid; border-right-style: solid;',
                                   'vsides' =&gt; 'border-top-style: solid; border-bottom-style: solid;',
                                   'lhs' =&gt; 'border-left-style: solid;',
                                   'rhs' =&gt; 'border-right-style: solid;',
                                   'box' =&gt; 'border-style: solid;',
                                   'border' =&gt; 'border-style: solid;'),
                  'rules' =&gt; '!set_table_rules',
                  'width' =&gt; 'width: %s;',
                  ),
           'hr' =&gt;
           array (
                  'align' =&gt; '!set_hr_align', // Need to grab width to set 'left' &amp; 'right' correctly
                  'noshade' =&gt; 'border-style: solid;',
                  'size' =&gt; 'border-width: %0.2f px;',
                  'width' =&gt; 'width: %s;',
                  ),
           'div' =&gt;
           array (
                  'align' =&gt; 'text-align: %s;',
                  ),
           'h1' =&gt;
           array (
                  'align' =&gt; 'text-align: %s;',
                  ),
           'h2' =&gt;
           array (
                  'align' =&gt; 'text-align: %s;',
                  ),
           'h3' =&gt;
           array (
                  'align' =&gt; 'text-align: %s;',
                  ),
           'h4' =&gt;
           array (
                  'align' =&gt; 'text-align: %s;',
                  ),
           'h5' =&gt;
           array (
                  'align' =&gt; 'text-align: %s;',
                  ),
           'h6' =&gt;
           array (
                  'align' =&gt; 'text-align: %s;',
                  ),
           'p' =&gt;
           array (
                  'align' =&gt; 'text-align: %s;',
                  ),
//            'col' =&gt;
//            array (
//                   'align' =&gt; '',
//                   'valign' =&gt; '',
//                   ),
//            'colgroup' =&gt;
//            array (
//                   'align' =&gt; '',
//                   'valign' =&gt; '',
//                   ),
           'tbody' =&gt;
           array (
                  'align' =&gt; '!set_table_row_align',
                  'valign' =&gt; '!set_table_row_valign',
                  ),
           'td' =&gt;
           array (
                  'align' =&gt; 'text-align: %s;',
                  'bgcolor' =&gt; 'background-color: %s;',
                  'height' =&gt; 'height: %s;',
                  'nowrap' =&gt; 'white-space: nowrap;',
                  'valign' =&gt; 'vertical-align: %s;',
                  'width' =&gt; 'width: %s;',
                  ),
           'tfoot' =&gt;
           array (
                  'align' =&gt; '!set_table_row_align',
                  'valign' =&gt; '!set_table_row_valign',
                  ),
           'th' =&gt;
           array (
                  'align' =&gt; 'text-align: %s;',
                  'bgcolor' =&gt; 'background-color: %s;',
                  'height' =&gt; 'height: %s;',
                  'nowrap' =&gt; 'white-space: nowrap;',
                  'valign' =&gt; 'vertical-align: %s;',
                  'width' =&gt; 'width: %s;',
                  ),
           'thead' =&gt;
           array (
                  'align' =&gt; '!set_table_row_align',
                  'valign' =&gt; '!set_table_row_valign',
                  ),
           'tr' =&gt;
           array (
                  'align' =&gt; '!set_table_row_align',
                  'bgcolor' =&gt; '!set_table_row_bgcolor',
                  'valign' =&gt; '!set_table_row_valign',
                  ),
           'body' =&gt;
           array (
                  'background' =&gt; 'background-image: url(%s);',
                  'bgcolor' =&gt; 'background-color: %s;',
                  'link' =&gt; '!set_body_link',
                  'text' =&gt; 'color: %s;',
                  ),
           'br' =&gt;
           array (
                  'clear' =&gt; 'clear: %s;',
                  ),
           'basefont' =&gt;
           array (
                  'color' =&gt; 'color: %s;',
                  'face' =&gt; 'font-family: %s;',
                  'size' =&gt; '!set_basefont_size',
                  ),
           'font' =&gt;
           array (
                  'color' =&gt; 'color: %s;',
                  'face' =&gt; 'font-family: %s;',
                  'size' =&gt; '!set_font_size',
                  ),
           'dir' =&gt;
           array (
                  'compact' =&gt; 'margin: 0.5em 0;',
                  ),
           'dl' =&gt;
           array (
                  'compact' =&gt; 'margin: 0.5em 0;',
                  ),
           'menu' =&gt;
           array (
                  'compact' =&gt; 'margin: 0.5em 0;',
                  ),
           'ol' =&gt;
           array (
                  'compact' =&gt; 'margin: 0.5em 0;',
                  'start' =&gt; 'counter-reset: -dompdf-default-counter %d;',
                  'type' =&gt; 'list-style-type: %s;',
                  ),
           'ul' =&gt;
           array (
                  'compact' =&gt; 'margin: 0.5em 0;',
                  'type' =&gt; 'list-style-type: %s;',
                  ),
           'li' =&gt;
           array (
                  'type' =&gt; 'list-style-type: %s;',
                  'value' =&gt; 'counter-reset: -dompdf-default-counter %d;',
                  ),
           'pre' =&gt;
           array (
                  'width' =&gt; 'width: %s;',
                  ),
           );

  
  static protected $_last_basefont_size = 3;
  static protected $_font_size_lookup = array(1=&gt;"xx-small",
                                              2=&gt;"x-small",
                                              3=&gt;"medium",
                                              4=&gt;"large",
                                              5=&gt;"x-large",
                                              6=&gt;"xx-large",
                                              7=&gt;"300%");
  
  
  static function translate_attributes($frame) {
    $node = $frame-&gt;get_node();
    $tag = $node-&gt;tagName;

    if ( !isset(self::$__ATTRIBUTE_LOOKUP[$tag]) )
      return;

    $valid_attrs = self::$__ATTRIBUTE_LOOKUP[$tag];
    $attrs = $node-&gt;attributes;
    $style = rtrim($node-&gt;getAttribute("style"), "; ");
    if ( $style != "" )
      $style .= ";";

    foreach ($attrs as $attr =&gt; $attr_node ) {
      if ( !isset($valid_attrs[$attr]) )
        continue;

      $value = $attr_node-&gt;value;

      $target = $valid_attrs[$attr];
      
      // Look up $value in $target, if $target is an array:
      if ( is_array($target) ) {

        if ( isset($target[$value]) )
          $style .= " " . self::_resolve_target($node, $target[$value], $value);

      } else {
        // otherwise use target directly
        $style .= " " . self::_resolve_target($node, $target, $value);
      }
    }
    if ( !is_null($style) ) {
      $style = ltrim($style);
      $node-&gt;setAttribute("style", $style);
    }
    
  }

  static protected function _resolve_target($node, $target, $value) {
    if ( $target[0] === "!" ) {
      // Function call
      $func = "_" . mb_substr($target, 1);
      return self::$func($node, $value);
    }
    
    return $value ? sprintf($target, $value) : "";
  }

  //.....................................................................

  static protected function _set_table_cellpadding($node, $value) {

    $td_list = $node-&gt;getElementsByTagName("td");
    foreach ($td_list as $td) {
      $style = rtrim($td-&gt;getAttribute("style"), ";");
      $style .= "; padding: $value" . "px;";
      $style = ltrim($style, ";");
      $td-&gt;setAttribute("style", $style);
    }
    return null;
  }

  static protected function _set_table_border($node, $value) {
    $td_list = $node-&gt;getElementsByTagName("td");
    foreach ($td_list as $td) {
      $style = $td-&gt;getAttribute("style");
      if ( strpos($style, "border") !== false )
        continue;
      $style = rtrim($style, ";");
      $style .= "; border-width: $value" . "px; border-style: ridge;";
      $style = ltrim($style, ";");
      $td-&gt;setAttribute("style", $style);
    }
    $th_list = $node-&gt;getElementsByTagName("th");
    foreach ($th_list as $th) {
      $style = $th-&gt;getAttribute("style");
      if ( strpos($style, "border") !== false )
        continue;
      $style = rtrim($style, ";");
      $style .= "; border-width: $value" . "px; border-style: ridge;";
      $style = ltrim($style, ";");
      $th-&gt;setAttribute("style", $style);
    }
    
    return null;
  }

  static protected function _set_table_cellspacing($node, $value) {
    $style = rtrim($td-&gt;getAttribute($style), ";");

    if ( $value == 0 )
      $style .= "; border-collapse: collapse;";
      
    else
      $style = "; border-collapse: separate;";
      
    return ltrim($style, ";");
  }
  
  static protected function _set_table_rules($node, $value) {
    $new_style = "; border-collapse: collapse;";
    switch ($value) {
    case "none":
      $new_style .= "border-style: none;";
      break;

    case "groups":
      // FIXME: unsupported
      return;

    case "rows":
      $new_style .= "border-style: solid none solid none; border-width: 1px; ";
      break;

    case "cols":
      $new_style .= "border-style: none solid none solid; border-width: 1px; ";
      break;

    case "all":
      $new_style .= "border-style: solid; border-width: 1px; ";
      break;
      
    default:
      // Invalid value
      return null;
    }

    $td_list = $node-&gt;getElementsByTagName("td");
    
    foreach ($td_list as $td) {
      $style = $td-&gt;getAttribute("style");
      $style .= $new_style;
      $td-&gt;setAttribute("style", $style);
    }
    return null;
  }

  static protected function _set_hr_align($node, $value) {

    $style = rtrim($node-&gt;getAttribute("style"),";");
    $width = $node-&gt;getAttribute("width");
    if ( $width == "" )
      $width = "100%";

    $remainder = 100 - (double)rtrim($width, "% ");
    
    switch ($value) {
    case "left":
      $style .= "; margin-right: $remainder %;";
      break;

    case "right":
      $style .= "; margin-left: $remainder %;";
      break;

    case "center":
      $style .= "; margin-left: auto; margin-right: auto;";
      break;

    default:
      return null;
    }
    return ltrim($style, "; ");
    
  }

  static protected function _set_table_row_align($node, $value) {

    $td_list = $node-&gt;getElementsByTagName("td");

    foreach ($td_list as $td) {
      $style = rtrim($td-&gt;getAttribute("style"), ";");
      $style .= "; text-align: $value;";
      $style = ltrim($style, "; ");
      $td-&gt;setAttribute("style", $style);
    }

    return null;
  }

  static protected function _set_table_row_valign($node, $value) {

    $td_list = $node-&gt;getElementsByTagName("td");

    foreach ($td_list as $td) {
      $style = rtrim($td-&gt;getAttribute("style"), ";");
      $style .= "; vertical-align: $value;";
      $style = ltrim($style, "; ");
      $td-&gt;setAttribute("style", $style);
    }

    return null;
  }

  static protected function _set_table_row_bgcolor($node, $value) {

    $td_list = $node-&gt;getElementsByTagName("td");

    foreach ($td_list as $td) {
      $style = rtrim($td-&gt;getAttribute("style"), ";");
      $style .= "; background-color: $value;";
      $style = ltrim($style, "; ");
      $td-&gt;setAttribute("style", $style);
    }

    return null;
  }

  static protected function _set_body_link($node, $value) {

    $a_list = $node-&gt;getElementsByTagName("a");

    foreach ($a_list as $a) {
      $style = rtrim($a-&gt;getAttribute("style"), ";");
      $style .= "; color: $value;";
      $style = ltrim($style, "; ");
      $a-&gt;setAttribute("style", $style);
    }

    return null;
  }

  static protected function _set_basefont_size($node, $value) {
    // FIXME: ? we don't actually set the font size of anything here, just
    // the base size for later modification by &lt;font&gt; tags.
    self::$_last_basefont_size = $value;
    return null;
  }
  
  static protected function _set_font_size($node, $value) {
    $style = $node-&gt;getAttribute("style");

    if ( $value[0] === "-" || $value[0] === "+" )
      $value = self::$_last_basefont_size + (int)$value;

    if ( isset(self::$_font_size_lookup[$value]) )
      $style .= "; font-size: " . self::$_font_size_lookup[$value] . ";";
    else
      $style .= "; font-size: $value;";

    return ltrim($style, "; ");
    
  }

}
</pre></body></html>