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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.5, 2009-01-05
  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 - 2009 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.      * Superscript
  75.      *
  76.      * @var boolean 
  77.      */
  78.     private $_superScript;
  79.     
  80.     /**
  81.      * Subscript
  82.      *
  83.      * @var boolean 
  84.      */
  85.     private $_subScript;
  86.     
  87.     /**
  88.      * Underline
  89.      *
  90.      * @var string 
  91.      */
  92.     private $_underline;
  93.     
  94.     /**
  95.      * Striketrough
  96.      *
  97.      * @var boolean 
  98.      */
  99.     private $_striketrough;
  100.     
  101.     /**
  102.      * Foreground color
  103.      * 
  104.      * @var PHPExcel_Style_Color 
  105.      */
  106.     private $_color;    
  107.     
  108.     /**
  109.      * Parent Style
  110.      *
  111.      * @var PHPExcel_Style 
  112.      */
  113.      
  114.     private $_parent;
  115.     
  116.     /**
  117.      * Parent Borders
  118.      *
  119.      * @var _parentPropertyName string
  120.      */
  121.     private $_parentPropertyName;
  122.         
  123.     /**
  124.      * Create a new PHPExcel_Style_Font
  125.      */
  126.     public function __construct()
  127.     {
  128.         // Initialise values
  129.         $this->_name                = 'Calibri';
  130.         $this->_size                10;
  131.         $this->_bold                = false;
  132.         $this->_italic                = false;
  133.         $this->_superScript            = false;
  134.         $this->_subScript            = false;
  135.         $this->_underline            = PHPExcel_Style_Font::UNDERLINE_NONE;
  136.         $this->_striketrough        = false;
  137.         $this->_color                = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK);
  138.     }
  139.  
  140.     /**
  141.      * Property Prepare bind
  142.      *
  143.      * Configures this object for late binding as a property of a parent object
  144.      *     
  145.      * @param $parent 
  146.      * @param $parentPropertyName 
  147.      */
  148.     public function propertyPrepareBind($parent$parentPropertyName)
  149.     {
  150.         // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  151.         // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  152.         $this->_parent                 = $parent;
  153.         $this->_parentPropertyName    = $parentPropertyName;
  154.     }
  155.  
  156.     /**
  157.      * Property Get Bound
  158.      *
  159.      * Returns the PHPExcel_Style_Font that is actual bound to PHPExcel_Style
  160.      *
  161.      * @return PHPExcel_Style_Font 
  162.      */
  163.     private function propertyGetBound({
  164.         if(!isset($this->_parent))
  165.             return $this;                                                                // I am bound
  166.  
  167.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  168.             return $this->_parent->getFont();                                            // Another one is bound
  169.  
  170.         return $this;                                                                    // No one is bound yet
  171.     }
  172.     
  173.     /**
  174.      * Property Begin Bind
  175.      *
  176.      * If no PHPExcel_Style_Font has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  177.      *
  178.      * @return PHPExcel_Style_Font 
  179.      */
  180.     private function propertyBeginBind({
  181.         if(!isset($this->_parent))
  182.             return $this;                                                                // I am already bound
  183.  
  184.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  185.             return $this->_parent->getFont();                                            // Another one is already bound
  186.             
  187.         $this->_parent->propertyCompleteBind($this$this->_parentPropertyName);        // Bind myself
  188.         $this->_parent = null;
  189.         
  190.         return $this;
  191.     }
  192.     
  193.     /**
  194.      * Apply styles from array
  195.      * 
  196.      * <code>
  197.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
  198.      *         array(
  199.      *             'name'      => 'Arial',
  200.      *             'bold'      => true,
  201.      *             'italic'    => false,
  202.      *             'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  203.      *             'strike'    => false,
  204.      *             'color'     => array(
  205.      *                 'rgb' => '808080'
  206.      *             )
  207.      *         )
  208.      * );
  209.      * </code>
  210.      * 
  211.      * @param    array    $pStyles    Array containing style information
  212.      * @throws    Exception
  213.      */
  214.     public function applyFromArray($pStyles null{
  215.         if (is_array($pStyles)) {
  216.             if (array_key_exists('name'$pStyles)) {
  217.                 $this->setName($pStyles['name']);
  218.             }
  219.             if (array_key_exists('bold'$pStyles)) {
  220.                 $this->setBold($pStyles['bold']);
  221.             }
  222.             if (array_key_exists('italic'$pStyles)) {
  223.                 $this->setItalic($pStyles['italic']);
  224.             }
  225.             if (array_key_exists('superScript'$pStyles)) {
  226.                 $this->setSuperScript($pStyles['superScript']);
  227.             }
  228.             if (array_key_exists('subScript'$pStyles)) {
  229.                 $this->setSubScript($pStyles['subScript']);
  230.             }
  231.             if (array_key_exists('underline'$pStyles)) {
  232.                 $this->setUnderline($pStyles['underline']);
  233.             }
  234.             if (array_key_exists('strike'$pStyles)) {
  235.                 $this->setStriketrough($pStyles['strike']);
  236.             }
  237.             if (array_key_exists('color'$pStyles)) {
  238.                 $this->getColor()->applyFromArray($pStyles['color']);
  239.             }
  240.             if (array_key_exists('size'$pStyles)) {
  241.                 $this->setSize($pStyles['size']);
  242.             }
  243.         else {
  244.             throw new Exception("Invalid style array passed.");
  245.         }
  246.     }
  247.     
  248.     /**
  249.      * Get Name
  250.      *
  251.      * @return string 
  252.      */
  253.     public function getName({
  254.         return $this->propertyGetBound()->_name;
  255.     }
  256.     
  257.     /**
  258.      * Set Name
  259.      *
  260.      * @param string $pValue 
  261.      */
  262.     public function setName($pValue 'Calibri'{
  263.            if ($pValue == ''{
  264.             $pValue 'Calibri';
  265.         }
  266.         $this->propertyBeginBind()->_name $pValue;
  267.     }
  268.     
  269.     /**
  270.      * Get Size
  271.      *
  272.      * @return double 
  273.      */
  274.     public function getSize({
  275.         return $this->propertyGetBound()->_size;
  276.     }
  277.     
  278.     /**
  279.      * Set Size
  280.      *
  281.      * @param double $pValue 
  282.      */
  283.     public function setSize($pValue 10{
  284.         if ($pValue == ''{
  285.             $pValue 10;
  286.         }
  287.         $this->propertyBeginBind()->_size $pValue;
  288.     }
  289.     
  290.     /**
  291.      * Get Bold
  292.      *
  293.      * @return boolean 
  294.      */
  295.     public function getBold({
  296.         return $this->propertyGetBound()->_bold;
  297.     }
  298.     
  299.     /**
  300.      * Set Bold
  301.      *
  302.      * @param boolean $pValue 
  303.      */
  304.     public function setBold($pValue false{
  305.         if ($pValue == ''{
  306.             $pValue false;
  307.         }
  308.         $this->propertyBeginBind()->_bold $pValue;
  309.     }
  310.     
  311.     /**
  312.      * Get Italic
  313.      *
  314.      * @return boolean 
  315.      */
  316.     public function getItalic({
  317.         return $this->propertyGetBound()->_italic;
  318.     }
  319.     
  320.     /**
  321.      * Set Italic
  322.      *
  323.      * @param boolean $pValue 
  324.      */
  325.     public function setItalic($pValue false{
  326.         if ($pValue == ''{
  327.             $pValue false;
  328.         }
  329.         $this->propertyBeginBind()->_italic $pValue;
  330.     }
  331.     
  332.     /**
  333.      * Get SuperScript
  334.      *
  335.      * @return boolean 
  336.      */
  337.     public function getSuperScript({
  338.         return $this->propertyGetBound()->_superScript;
  339.     }
  340.     
  341.     /**
  342.      * Set SuperScript
  343.      *
  344.      * @param boolean $pValue 
  345.      */
  346.     public function setSuperScript($pValue false{
  347.         if ($pValue == ''{
  348.             $pValue false;
  349.         }
  350.         $this->propertyBeginBind()->_superScript $pValue;
  351.         $this->propertyBeginBind()->_subScript !$pValue;
  352.     }
  353.     
  354.         /**
  355.      * Get SubScript
  356.      *
  357.      * @return boolean 
  358.      */
  359.     public function getSubScript({
  360.         return $this->propertyGetBound()->_subScript;
  361.     }
  362.     
  363.     /**
  364.      * Set SubScript
  365.      *
  366.      * @param boolean $pValue 
  367.      */
  368.     public function setSubScript($pValue false{
  369.         if ($pValue == ''{
  370.             $pValue false;
  371.         }
  372.         $this->propertyBeginBind()->_subScript $pValue;
  373.         $this->propertyBeginBind()->_superScript !$pValue;
  374.     }
  375.     
  376.     /**
  377.      * Get Underline
  378.      *
  379.      * @return string 
  380.      */
  381.     public function getUnderline({
  382.         return $this->propertyGetBound()->_underline;
  383.     }
  384.     
  385.     /**
  386.      * Set Underline
  387.      *
  388.      * @param string $pValue    PHPExcel_Style_Font underline type
  389.      */
  390.     public function setUnderline($pValue PHPExcel_Style_Font::UNDERLINE_NONE{
  391.         if ($pValue == ''{
  392.             $pValue PHPExcel_Style_Font::UNDERLINE_NONE;
  393.         }
  394.         $this->propertyBeginBind()->_underline $pValue;
  395.     }
  396.     
  397.     /**
  398.      * Get Striketrough
  399.      *
  400.      * @return boolean 
  401.      */
  402.     public function getStriketrough({
  403.         return $this->propertyGetBound()->_striketrough;
  404.     }
  405.     
  406.     /**
  407.      * Set Striketrough
  408.      *
  409.      * @param boolean $pValue 
  410.      */
  411.     public function setStriketrough($pValue false{
  412.         if ($pValue == ''{
  413.             $pValue false;
  414.         }
  415.         $this->propertyBeginBind()->_striketrough $pValue;
  416.     }
  417.  
  418.     /**
  419.      * Get Color
  420.      *
  421.      * @return PHPExcel_Style_Color 
  422.      */
  423.     public function getColor({
  424.         // It's a get but it may lead to a modified color which we won't detect but in which case we must bind.
  425.         // So bind as an assurance.
  426.         return $this->propertyBeginBind()->_color;
  427.     }
  428.     
  429.     /**
  430.      * Set Color
  431.      *
  432.      * @param     PHPExcel_Style_Color $pValue 
  433.      * @throws     Exception
  434.      */
  435.     public function setColor(PHPExcel_Style_Color $pValue null{
  436.            $this->propertyBeginBind()->_color $pValue;
  437.     }
  438.  
  439.     /**
  440.      * Get hash code
  441.      *
  442.      * @return string    Hash code
  443.      */    
  444.     public function getHashCode({
  445.         $property $this->propertyGetBound();
  446.         return md5(
  447.               $property->_name
  448.             . $property->_size
  449.             . ($property->_bold 't' 'f')
  450.             . ($property->_italic 't' 'f')
  451.             . ($property->_superScript 't' 'f')
  452.             . ($property->_subScript 't' 'f')
  453.             . $property->_underline
  454.             . ($property->_striketrough 't' 'f')
  455.             . $property->_color->getHashCode()
  456.             . __CLASS__
  457.         );
  458.     }
  459.     
  460.     /**
  461.      * Hash index
  462.      *
  463.      * @var string 
  464.      */
  465.     private $_hashIndex;
  466.     
  467.     /**
  468.      * Get hash index
  469.      * 
  470.      * Note that this index may vary during script execution! Only reliable moment is
  471.      * while doing a write of a workbook and when changes are not allowed.
  472.      *
  473.      * @return string    Hash index
  474.      */
  475.     public function getHashIndex({
  476.         return $this->_hashIndex;
  477.     }
  478.     
  479.     /**
  480.      * Set hash index
  481.      * 
  482.      * Note that this index may vary during script execution! Only reliable moment is
  483.      * while doing a write of a workbook and when changes are not allowed.
  484.      *
  485.      * @param string    $value    Hash index
  486.      */
  487.     public function setHashIndex($value{
  488.         $this->_hashIndex = $value;
  489.     }
  490.         
  491.     /**
  492.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  493.      */
  494.     public function __clone({
  495.         $vars get_object_vars($this);
  496.         foreach ($vars as $key => $value{
  497.             if (is_object($value)) {
  498.                 $this->$key clone $value;
  499.             else {
  500.                 $this->$key $value;
  501.             }
  502.         }
  503.     }
  504. }

Documentation generated on Mon, 05 Jan 2009 20:37:14 +0100 by phpDocumentor 1.4.1