PHPExcel_Style
[ class tree: PHPExcel_Style ] [ index: PHPExcel_Style ] [ all elements ]

Source for file Font.php

Documentation is available at Font.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2008 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Style
  23.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.3, 2008-08-25
  26.  */
  27.  
  28.  
  29. /** PHPExcel_Style_Color */
  30. require_once 'PHPExcel/Style/Color.php';
  31.  
  32. /** PHPExcel_IComparable */
  33. require_once 'PHPExcel/IComparable.php';
  34.  
  35.  
  36. /**
  37.  * PHPExcel_Style_Font
  38.  *
  39.  * @category   PHPExcel
  40.  * @package    PHPExcel_Style
  41.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  42.  */
  43. class PHPExcel_Style_Font implements PHPExcel_IComparable
  44. {
  45.     /* Underline types */
  46.     const UNDERLINE_NONE                    = 'none';
  47.     const UNDERLINE_DOUBLE                    = 'double';
  48.     const UNDERLINE_DOUBLEACCOUNTING        = 'doubleAccounting';
  49.     const UNDERLINE_SINGLE                    = 'single';
  50.     const UNDERLINE_SINGLEACCOUNTING        = 'singleAccounting';
  51.     
  52.     /**
  53.      * Name
  54.      *
  55.      * @var string 
  56.      */
  57.     private $_name;
  58.     
  59.     /**
  60.      * Bold
  61.      *
  62.      * @var boolean 
  63.      */
  64.     private $_bold;
  65.     
  66.     /**
  67.      * Italic
  68.      *
  69.      * @var boolean 
  70.      */
  71.     private $_italic;
  72.     
  73.     /**
  74.      * Underline
  75.      *
  76.      * @var string 
  77.      */
  78.     private $_underline;
  79.     
  80.     /**
  81.      * Striketrough
  82.      *
  83.      * @var boolean 
  84.      */
  85.     private $_striketrough;
  86.     
  87.     /**
  88.      * Foreground color
  89.      * 
  90.      * @var PHPExcel_Style_Color 
  91.      */
  92.     private $_color;    
  93.     
  94.     /**
  95.      * Parent Style
  96.      *
  97.      * @var PHPExcel_Style 
  98.      */
  99.      
  100.     private $_parent;
  101.     
  102.     /**
  103.      * Parent Borders
  104.      *
  105.      * @var _parentPropertyName string
  106.      */
  107.     private $_parentPropertyName;
  108.         
  109.     /**
  110.      * Create a new PHPExcel_Style_Font
  111.      */
  112.     public function __construct()
  113.     {
  114.         // Initialise values
  115.         $this->_name                = 'Calibri';
  116.         $this->_size                = 10;
  117.         $this->_bold                = false;
  118.         $this->_italic                = false;
  119.         $this->_underline            = PHPExcel_Style_Font::UNDERLINE_NONE;
  120.         $this->_striketrough        = false;
  121.         $this->_color                = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK);
  122.     }
  123.  
  124.     /**
  125.      * Property Prepare bind
  126.      *
  127.      * Configures this object for late binding as a property of a parent object
  128.      *     
  129.      * @param $parent 
  130.      * @param $parentPropertyName 
  131.      */
  132.     public function propertyPrepareBind($parent, $parentPropertyName)
  133.     {
  134.         // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  135.         // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  136.         $this->_parent                 = $parent;
  137.         $this->_parentPropertyName    = $parentPropertyName;
  138.     }
  139.  
  140.     /**
  141.      * Property Get Bound
  142.      *
  143.      * Returns the PHPExcel_Style_Font that is actual bound to PHPExcel_Style
  144.      *
  145.      * @return PHPExcel_Style_Font 
  146.      */
  147.     private function propertyGetBound() {
  148.         if(!isset($this->_parent))
  149.             return $this;                                                                // I am bound
  150.  
  151.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  152.             return $this->_parent->getFont();                                            // Another one is bound
  153.  
  154.         return $this;                                                                    // No one is bound yet
  155.     }
  156.     
  157.     /**
  158.      * Property Begin Bind
  159.      *
  160.      * If no PHPExcel_Style_Font has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  161.      *
  162.      * @return PHPExcel_Style_Font 
  163.      */
  164.     private function propertyBeginBind() {
  165.         if(!isset($this->_parent))
  166.             return $this;                                                                // I am already bound
  167.  
  168.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  169.             return $this->_parent->getFont();                                            // Another one is already bound
  170.             
  171.         $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName);        // Bind myself
  172.         $this->_parent = null;
  173.         
  174.         return $this;
  175.     }
  176.     
  177.     /**
  178.      * Apply styles from array
  179.      * 
  180.      * <code>
  181.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
  182.      *         array(
  183.      *             'name'      => 'Arial',
  184.      *             'bold'      => true,
  185.      *             'italic'    => false,
  186.      *             'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  187.      *             'strike'    => false,
  188.      *             'color'     => array(
  189.      *                 'rgb' => '808080'
  190.      *             )
  191.      *         )
  192.      * );
  193.      * </code>
  194.      * 
  195.      * @param    array    $pStyles    Array containing style information
  196.      * @throws    Exception
  197.      */
  198.     public function applyFromArray($pStyles = null) {
  199.         if (is_array($pStyles)) {
  200.             if (array_key_exists('name', $pStyles)) {
  201.                 $this->setName($pStyles['name']);
  202.             }
  203.             if (array_key_exists('bold', $pStyles)) {
  204.                 $this->setBold($pStyles['bold']);
  205.             }
  206.             if (array_key_exists('italic', $pStyles)) {
  207.                 $this->setItalic($pStyles['italic']);
  208.             }
  209.             if (array_key_exists('underline', $pStyles)) {
  210.                 $this->setUnderline($pStyles['underline']);
  211.             }
  212.             if (array_key_exists('strike', $pStyles)) {
  213.                 $this->setStriketrough($pStyles['strike']);
  214.             }
  215.             if (array_key_exists('color', $pStyles)) {
  216.                 $this->getColor()->applyFromArray($pStyles['color']);
  217.             }
  218.             if (array_key_exists('size', $pStyles)) {
  219.                 $this->setSize($pStyles['size']);
  220.             }
  221.         } else {
  222.             throw new Exception("Invalid style array passed.");
  223.         }
  224.     }
  225.     
  226.     /**
  227.      * Get Name
  228.      *
  229.      * @return string 
  230.      */
  231.     public function getName() {
  232.         return $this->propertyGetBound()->_name;
  233.     }
  234.     
  235.     /**
  236.      * Set Name
  237.      *
  238.      * @param string $pValue 
  239.      */
  240.     public function setName($pValue = 'Calibri') {
  241.            if ($pValue == '') {
  242.             $pValue = 'Calibri';
  243.         }
  244.         $this->propertyBeginBind()->_name = $pValue;
  245.     }
  246.     
  247.     /**
  248.      * Get Size
  249.      *
  250.      * @return double 
  251.      */
  252.     public function getSize() {
  253.         return $this->propertyGetBound()->_size;
  254.     }
  255.     
  256.     /**
  257.      * Set Size
  258.      *
  259.      * @param double $pValue 
  260.      */
  261.     public function setSize($pValue = 10) {
  262.         if ($pValue == '') {
  263.             $pValue = 10;
  264.         }
  265.         $this->propertyBeginBind()->_size = $pValue;
  266.     }
  267.     
  268.     /**
  269.      * Get Bold
  270.      *
  271.      * @return boolean 
  272.      */
  273.     public function getBold() {
  274.         return $this->propertyGetBound()->_bold;
  275.     }
  276.     
  277.     /**
  278.      * Set Bold
  279.      *
  280.      * @param boolean $pValue 
  281.      */
  282.     public function setBold($pValue = false) {
  283.         if ($pValue == '') {
  284.             $pValue = false;
  285.         }
  286.         $this->propertyBeginBind()->_bold = $pValue;
  287.     }
  288.     
  289.     /**
  290.      * Get Italic
  291.      *
  292.      * @return boolean 
  293.      */
  294.     public function getItalic() {
  295.         return $this->propertyGetBound()->_italic;
  296.     }
  297.     
  298.     /**
  299.      * Set Italic
  300.      *
  301.      * @param boolean $pValue 
  302.      */
  303.     public function setItalic($pValue = false) {
  304.         if ($pValue == '') {
  305.             $pValue = false;
  306.         }
  307.         $this->propertyBeginBind()->_italic = $pValue;
  308.     }
  309.     
  310.     /**
  311.      * Get Underline
  312.      *
  313.      * @return string 
  314.      */
  315.     public function getUnderline() {
  316.         return $this->propertyGetBound()->_underline;
  317.     }
  318.     
  319.     /**
  320.      * Set Underline
  321.      *
  322.      * @param string $pValue    PHPExcel_Style_Font underline type
  323.      */
  324.     public function setUnderline($pValue = PHPExcel_Style_Font::UNDERLINE_NONE) {
  325.         if ($pValue == '') {
  326.             $pValue = PHPExcel_Style_Font::UNDERLINE_NONE;
  327.         }
  328.         $this->propertyBeginBind()->_underline = $pValue;
  329.     }
  330.     
  331.     /**
  332.      * Get Striketrough
  333.      *
  334.      * @return boolean 
  335.      */
  336.     public function getStriketrough() {
  337.         return $this->propertyGetBound()->_striketrough;
  338.     }
  339.     
  340.     /**
  341.      * Set Striketrough
  342.      *
  343.      * @param boolean $pValue 
  344.      */
  345.     public function setStriketrough($pValue = false) {
  346.         if ($pValue == '') {
  347.             $pValue = false;
  348.         }
  349.         $this->propertyBeginBind()->_striketrough = $pValue;
  350.     }
  351.  
  352.     /**
  353.      * Get Color
  354.      *
  355.      * @return PHPExcel_Style_Color 
  356.      */
  357.     public function getColor() {
  358.         // It's a get but it may lead to a modified color which we won't detect but in which case we must bind.
  359.         // So bind as an assurance.
  360.         return $this->propertyBeginBind()->_color;
  361.     }
  362.     
  363.     /**
  364.      * Set Color
  365.      *
  366.      * @param     PHPExcel_Style_Color $pValue 
  367.      * @throws     Exception
  368.      */
  369.     public function setColor(PHPExcel_Style_Color $pValue = null) {
  370.            $this->propertyBeginBind()->_color = $pValue;
  371.     }
  372.  
  373.     /**
  374.      * Get hash code
  375.      *
  376.      * @return string    Hash code
  377.      */    
  378.     public function getHashCode() {
  379.         $property = $this->propertyGetBound();
  380.         return md5(
  381.               $property->_name
  382.             . $property->_size
  383.             . ($property->_bold ? 't' : 'f')
  384.             . ($property->_italic ? 't' : 'f')
  385.             . $property->_underline
  386.             . ($property->_striketrough ? 't' : 'f')
  387.             . $property->_color->getHashCode()
  388.             . __CLASS__
  389.         );
  390.     }
  391.         
  392.     /**
  393.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  394.      */
  395.     public function __clone() {
  396.         $vars = get_object_vars($this);
  397.         foreach ($vars as $key => $value) {
  398.             if (is_object($value)) {
  399.                 $this->$key = clone $value;
  400.             } else {
  401.                 $this->$key = $value;
  402.             }
  403.         }
  404.     }
  405. }

Documentation generated on Mon, 25 Aug 2008 13:38:35 +0200 by phpDocumentor 1.4.1