stylesheet.cls.php

来自「国外很不错的一个开源OA系统Group-Office」· PHP 代码 · 共 875 行 · 第 1/2 页

PHP
875
字号
<?php/** * DOMPDF - PHP5 HTML to PDF renderer * * File: $RCSfile: stylesheet.cls.php,v $ * Created on: 2004-06-01 * * 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.digitaljunkies.ca/dompdf * * @link http://www.digitaljunkies.ca/dompdf * @copyright 2004 Benj Carson * @author Benj Carson <benjcarson@digitaljunkies.ca> * @package dompdf * @version 0.5.1 *//* $Id: stylesheet.cls.php,v 1.2 2006/07/21 10:46:20 mschering Exp $ *//** * The location of the default built-in CSS file. * {@link Stylesheet::DEFAULT_STYLESHEET} */define('__DEFAULT_STYLESHEET', DOMPDF_LIB_DIR . DIRECTORY_SEPARATOR . "res" . DIRECTORY_SEPARATOR . "html.css");/** * The master stylesheet class * * The Stylesheet class is responsible for parsing stylesheets and style * tags/attributes.  It also acts as a registry of the individual Style * objects generated by the current set of loaded CSS files and style * elements. * * @see Style * @package dompdf */class Stylesheet {      /**   * the location of the default built-in CSS file.   *    */  const DEFAULT_STYLESHEET = __DEFAULT_STYLESHEET; // Hack: can't                                                   // concatenate stuff in                                                   // const declarations,                                                   // but I can do this?  // protected members  /**   *  array of currently defined styles   *  @var array   */  private $_styles;  /**   * base protocol of the document being parsed   *   * Used to handle relative urls.   *   * @var string    */  private $_protocol;  /**   * base hostname of the document being parsed   *   * Used to handle relative urls.   * @var string   */  private $_base_host;  /**   * base path of the document being parsed   *   * Used to handle relative urls.   * @var string   */  private $_base_path;    /**   * the style defined by @page rules    *   * @var Style   */  private $_page_style;  /**   * list of loaded files, used to prevent recursion   *   * @var array   */  private $_loaded_files;    /**   * accepted CSS media types    */  static $ACCEPTED_MEDIA_TYPES = array("all", "static", "visual",                                       "bitmap", "paged", "print");    /**   * The class constructor.   *   * The base protocol, host & path are initialized to those of   * the current script.   */  function __construct() {    $this->_styles = array();    $this->_loaded_files = array();    list($this->_protocol, $this->_base_host, $this->_base_path) = explode_url($_SERVER["SCRIPT_FILENAME"]);    $this->_page_style = null;  }  /**   * Set the base protocol   *   * @param string $proto   */  function set_protocol($proto) { $this->_protocol = $proto; }  /**   * Set the base host   *   * @param string $host   */  function set_host($host) { $this->_base_host = $host; }  /**   * Set the base path   *   * @param string $path   */  function set_base_path($path) { $this->_base_path = $path; }  /**   * Return the base protocol for this stylesheet   *   * @return string   */  function get_protocol() { return $this->_protocol; }  /**   * Return the base host for this stylesheet   *   * @return string   */  function get_host() { return $this->_base_host; }  /**   * Return the base path for this stylesheet   *   * @return string   */  function get_base_path() { return $this->_base_path; }    /**   * add a new Style object to the stylesheet   *   * add_style() adds a new Style object to the current stylesheet, or   * merges a new Style with an existing one.   *   * @param string $key   the Style's selector   * @param Style $style  the Style to be added   */  function add_style($key, Style $style) {    if (!is_string($key))      throw new DOMPDF_Exception("CSS rule must be keyed by a string.");    if ( isset($this->_styles[$key]) )      $this->_styles[$key]->merge($style);    else      $this->_styles[$key] = clone $style;  }  /**   * lookup a specifc Style object   *   * lookup() returns the Style specified by $key, or null if the Style is   * not found.   *   * @param string $key   the selector of the requested Style   * @return Style   */  function lookup($key) {    if ( !isset($this->_styles[$key]) )      return null;        return $this->_styles[$key];  }  /**   * create a new Style object associated with this stylesheet   *   * @param Style $parent The style of this style's parent in the DOM tree   * @return Style   */  function create_style($parent = null) {    return new Style($this, $parent);  }    /**   * load and parse a CSS string   *   * @param string $css   */  function load_css(&$css) { $this->_parse_css($css); }  /**   * load and parse a CSS file   *   * @param string $file   */  function load_css_file($file) {    global $_dompdf_warnings;        // Prevent circular references    if ( isset($this->_loaded_files[$file]) )      return;    $this->_loaded_files[$file] = true;    $parsed_url = explode_url($file);    list($this->_protocol, $this->_base_host, $this->_base_path, $filename) = $parsed_url;        if ( !DOMPDF_ENABLE_REMOTE &&         ($this->_protocol != "" && $this->_protocol != "file://") ) {      record_warnings(E_USER_WARNING, "Remote CSS file '$file' requested, but DOMPDF_ENABLE_REMOTE is false.", __FILE__, __LINE__);      return;     }        // Fix submitted by Nick Oostveen for aliased directory support:    if ( $this->_protocol == "" )      $file = $this->_base_path . $filename;    else      $file = build_url($this->_protocol, $this->_base_host, $this->_base_path, $filename);        set_error_handler("record_warnings");    $css = file_get_contents($file);    restore_error_handler();    if ( $css == "" ) {      record_warnings(E_USER_WARNING, "Unable to load css file $file", __FILE__, __LINE__);;      return;    }        $this->_parse_css($css);  }  /**   * @link http://www.w3.org/TR/CSS21/cascade.html#specificity}   *   * @param string $selector   * @return int   */  private function _specificity($selector) {    // http://www.w3.org/TR/CSS21/cascade.html#specificity    $a = ($selector === "!style attribute") ? 1 : 0;        $b = min(mb_substr_count($selector, "#"), 255);    $c = min(mb_substr_count($selector, ".") +             mb_substr_count($selector, ">") +             mb_substr_count($selector, "+"), 255);        $d = min(mb_substr_count($selector, " "), 255);    return ($a << 24) | ($b << 16) | ($c << 8) | ($d);  }  /**   * converts a CSS selector to an XPath query.   *   * @param string $selector   * @return string   */  private function _css_selector_to_xpath($selector) {    // Collapse white space and strip whitespace around delimiters//     $search = array("/\\s+/", "/\\s+([.>#+:])\\s+/");//     $replace = array(" ", "\\1");//     $selector = preg_replace($search, $replace, trim($selector));        // Initial query (non-absolute)    $query = "//";        // Parse the selector         //$s = preg_split("/([ :>.#+])/", $selector, -1, PREG_SPLIT_DELIM_CAPTURE);    $delimiters = array(" ", ">", ".", "#", "+", ":", "[");    // Add an implicit space at the beginning of the selector if there is no    // delimiter there already.    if ( !in_array($selector{0}, $delimiters) )      $selector = " $selector";    $tok = "";    $len = mb_strlen($selector);    $i = 0;                       while ( $i < $len ) {      $s = $selector{$i};      $i++;      // Eat characters up to the next delimiter      $tok = "";      while ($i < $len) {        if ( in_array($selector{$i}, $delimiters) )          break;        $tok .= $selector{$i++};      }      switch ($s) {              case " ":      case ">":        // All elements matching the next token that are direct children of        // the current token        $expr = $s == " " ? "descendant" : "child";        if ( mb_substr($query, -1, 1) != "/" )          $query .= "/";        if ( !$tok )          $tok = "*";                $query .= "$expr::$tok";        $tok = "";        break;      case ".":      case "#":        // All elements matching the current token with a class/id equal to        // the _next_ token.        $attr = $s == "." ? "class" : "id";        // empty class/id == *        if ( mb_substr($query, -1, 1) == "/" )          $query .= "*";        // Match multiple classes: $tok contains the current selected        // class.  Search for class attributes with class="$tok",        // class=".* $tok .*" and class=".* $tok"                // This doesn't work because libxml only supports XPath 1.0...        //$query .= "[matches(@$attr,\"^${tok}\$|^${tok}[ ]+|[ ]+${tok}\$|[ ]+${tok}[ ]+\")]";                // Query improvement by Michael Sheakoski <michael@mjsdigital.com>:        $query .= "[contains(concat(' ', @$attr, ' '), concat(' ', '$tok', ' '))]";        $tok = "";        break;      case "+":        // All sibling elements that folow the current token        if ( mb_substr($query, -1, 1) != "/" )          $query .= "/";        $query .= "following-sibling::$tok";        $tok = "";        break;      case ":":        // Pseudo-classes        switch ($tok) {        case "first-child":          break;        case "link":          $query .= "[@href]";          $tok = "";          break;        case "first-line":          break;        case "first-letter":          break;        case "before":          break;        case "after":          break;                }                break;              case "[":        // Attribute selectors.  All with an attribute matching the following token(s)        $attr_delimiters = array("=", "]", "~", "|");        $tok_len = mb_strlen($tok);        $j = 0;                $attr = "";        $op = "";        $value = "";                while ( $j < $tok_len ) {          if ( in_array($tok{$j}, $attr_delimiters) )            break;          $attr .= $tok{$j++};        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?