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.7.0, 2009-08-10
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35. }
  36.  
  37. /** PHPExcel_Style_Color */
  38. require_once PHPEXCEL_ROOT 'PHPExcel/Style/Color.php';
  39.  
  40. /** PHPExcel_IComparable */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/IComparable.php';
  42.  
  43.  
  44. /**
  45.  * PHPExcel_Style_Font
  46.  *
  47.  * @category   PHPExcel
  48.  * @package    PHPExcel_Style
  49.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  50.  */
  51. class PHPExcel_Style_Font implements PHPExcel_IComparable
  52. {
  53.     /* Underline types */
  54.     const UNDERLINE_NONE                    'none';
  55.     const UNDERLINE_DOUBLE                    'double';
  56.     const UNDERLINE_DOUBLEACCOUNTING        'doubleAccounting';
  57.     const UNDERLINE_SINGLE                    'single';
  58.     const UNDERLINE_SINGLEACCOUNTING        'singleAccounting';
  59.     
  60.     /**
  61.      * Name
  62.      *
  63.      * @var string 
  64.      */
  65.     private $_name;
  66.     
  67.     /**
  68.      * Bold
  69.      *
  70.      * @var boolean 
  71.      */
  72.     private $_bold;
  73.     
  74.     /**
  75.      * Italic
  76.      *
  77.      * @var boolean 
  78.      */
  79.     private $_italic;
  80.     
  81.     /**
  82.      * Superscript
  83.      *
  84.      * @var boolean 
  85.      */
  86.     private $_superScript;
  87.     
  88.     /**
  89.      * Subscript
  90.      *
  91.      * @var boolean 
  92.      */
  93.     private $_subScript;
  94.     
  95.     /**
  96.      * Underline
  97.      *
  98.      * @var string 
  99.      */
  100.     private $_underline;
  101.     
  102.     /**
  103.      * Strikethrough
  104.      *
  105.      * @var boolean 
  106.      */
  107.     private $_strikethrough;
  108.     
  109.     /**
  110.      * Foreground color
  111.      * 
  112.      * @var PHPExcel_Style_Color 
  113.      */
  114.     private $_color;    
  115.     
  116.     /**
  117.      * Parent Borders
  118.      *
  119.      * @var _parentPropertyName string
  120.      */
  121.     private $_parentPropertyName;
  122.  
  123.     /**
  124.      * Supervisor?
  125.      *
  126.      * @var boolean 
  127.      */
  128.     private $_isSupervisor;
  129.  
  130.     /**
  131.      * Parent. Only used for supervisor
  132.      *
  133.      * @var PHPExcel_Style 
  134.      */
  135.     private $_parent;
  136.  
  137.     /**
  138.      * Create a new PHPExcel_Style_Font
  139.      */
  140.     public function __construct($isSupervisor false)
  141.     {
  142.         // Supervisor?
  143.         $this->_isSupervisor = $isSupervisor;
  144.  
  145.         // Initialise values
  146.         $this->_name                = 'Calibri';
  147.         $this->_size                11;
  148.         $this->_bold                = false;
  149.         $this->_italic                = false;
  150.         $this->_superScript            = false;
  151.         $this->_subScript            = false;
  152.         $this->_underline            = PHPExcel_Style_Font::UNDERLINE_NONE;
  153.         $this->_strikethrough        = false;
  154.         $this->_color                = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK$isSupervisor);
  155.  
  156.         // bind parent if we are a supervisor
  157.         if ($isSupervisor{
  158.             $this->_color->bindParent($this'_color');
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Bind parent. Only used for supervisor
  164.      *
  165.      * @param PHPExcel_Style $parent 
  166.      * @return PHPExcel_Style_Font 
  167.      */
  168.     public function bindParent($parent)
  169.     {
  170.         $this->_parent = $parent;
  171.     }
  172.  
  173.     /**
  174.      * Is this a supervisor or a real style component?
  175.      *
  176.      * @return boolean 
  177.      */
  178.     public function getIsSupervisor()
  179.     {
  180.         return $this->_isSupervisor;
  181.     }
  182.  
  183.     /**
  184.      * Get the shared style component for the currently active cell in currently active sheet.
  185.      * Only used for style supervisor
  186.      *
  187.      * @return PHPExcel_Style_Font 
  188.      */
  189.     public function getSharedComponent()
  190.     {
  191.         return $this->_parent->getSharedComponent()->getFont();
  192.     }
  193.  
  194.     /**
  195.      * Get the currently active sheet. Only used for supervisor
  196.      *
  197.      * @return PHPExcel_Worksheet 
  198.      */
  199.     public function getActiveSheet()
  200.     {
  201.         return $this->_parent->getActiveSheet();
  202.     }
  203.  
  204.     /**
  205.      * Get the currently active cell coordinate in currently active sheet.
  206.      * Only used for supervisor
  207.      *
  208.      * @return string E.g. 'A1'
  209.      */
  210.     public function getXSelectedCells()
  211.     {
  212.         return $this->getActiveSheet()->getXSelectedCells();
  213.     }
  214.  
  215.     /**
  216.      * Get the currently active cell coordinate in currently active sheet.
  217.      * Only used for supervisor
  218.      *
  219.      * @return string E.g. 'A1'
  220.      */
  221.     public function getXActiveCell()
  222.     {
  223.         return $this->getActiveSheet()->getXActiveCell();
  224.     }
  225.  
  226.     /**
  227.      * Build style array from subcomponents
  228.      *
  229.      * @param array $array 
  230.      * @return array 
  231.      */
  232.     public function getStyleArray($array)
  233.     {
  234.         return array('font' => $array);
  235.     }
  236.  
  237.     /**
  238.      * Apply styles from array
  239.      * 
  240.      * <code>
  241.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
  242.      *         array(
  243.      *             'name'      => 'Arial',
  244.      *             'bold'      => true,
  245.      *             'italic'    => false,
  246.      *             'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  247.      *             'strike'    => false,
  248.      *             'color'     => array(
  249.      *                 'rgb' => '808080'
  250.      *             )
  251.      *         )
  252.      * );
  253.      * </code>
  254.      * 
  255.      * @param    array    $pStyles    Array containing style information
  256.      * @throws    Exception
  257.      * @return PHPExcel_Style_Font 
  258.      */
  259.     public function applyFromArray($pStyles null{
  260.         if (is_array($pStyles)) {
  261.             if ($this->_isSupervisor{
  262.                 $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  263.             else {
  264.                 if (array_key_exists('name'$pStyles)) {
  265.                     $this->setName($pStyles['name']);
  266.                 }
  267.                 if (array_key_exists('bold'$pStyles)) {
  268.                     $this->setBold($pStyles['bold']);
  269.                 }
  270.                 if (array_key_exists('italic'$pStyles)) {
  271.                     $this->setItalic($pStyles['italic']);
  272.                 }
  273.                 if (array_key_exists('superScript'$pStyles)) {
  274.                     $this->setSuperScript($pStyles['superScript']);
  275.                 }
  276.                 if (array_key_exists('subScript'$pStyles)) {
  277.                     $this->setSubScript($pStyles['subScript']);
  278.                 }
  279.                 if (array_key_exists('underline'$pStyles)) {
  280.                     $this->setUnderline($pStyles['underline']);
  281.                 }
  282.                 if (array_key_exists('strike'$pStyles)) {
  283.                     $this->setStrikethrough($pStyles['strike']);
  284.                 }
  285.                 if (array_key_exists('color'$pStyles)) {
  286.                     $this->getColor()->applyFromArray($pStyles['color']);
  287.                 }
  288.                 if (array_key_exists('size'$pStyles)) {
  289.                     $this->setSize($pStyles['size']);
  290.                 }
  291.             }
  292.         else {
  293.             throw new Exception("Invalid style array passed.");
  294.         }
  295.         return $this;
  296.     }
  297.     
  298.     /**
  299.      * Get Name
  300.      *
  301.      * @return string 
  302.      */
  303.     public function getName({
  304.         if ($this->_isSupervisor{
  305.             return $this->getSharedComponent()->getName();
  306.         }
  307.         return $this->_name;
  308.     }
  309.     
  310.     /**
  311.      * Set Name
  312.      *
  313.      * @param string $pValue 
  314.      * @return PHPExcel_Style_Font 
  315.      */
  316.     public function setName($pValue 'Calibri'{
  317.            if ($pValue == ''{
  318.             $pValue 'Calibri';
  319.         }
  320.         if ($this->_isSupervisor{
  321.             $styleArray $this->getStyleArray(array('name' => $pValue));
  322.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  323.         else {
  324.             $this->_name = $pValue;
  325.         }
  326.         return $this;
  327.     }
  328.     
  329.     /**
  330.      * Get Size
  331.      *
  332.      * @return double 
  333.      */
  334.     public function getSize({
  335.         if ($this->_isSupervisor{
  336.             return $this->getSharedComponent()->getSize();
  337.         }
  338.         return $this->_size;
  339.     }
  340.     
  341.     /**
  342.      * Set Size
  343.      *
  344.      * @param double $pValue 
  345.      * @return PHPExcel_Style_Font 
  346.      */
  347.     public function setSize($pValue 10{
  348.         if ($pValue == ''{
  349.             $pValue 10;
  350.         }
  351.         if ($this->_isSupervisor{
  352.             $styleArray $this->getStyleArray(array('size' => $pValue));
  353.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  354.         else {
  355.             $this->_size $pValue;
  356.         }
  357.         return $this;
  358.     }
  359.     
  360.     /**
  361.      * Get Bold
  362.      *
  363.      * @return boolean 
  364.      */
  365.     public function getBold({
  366.         if ($this->_isSupervisor{
  367.             return $this->getSharedComponent()->getBold();
  368.         }
  369.         return $this->_bold;
  370.     }
  371.     
  372.     /**
  373.      * Set Bold
  374.      *
  375.      * @param boolean $pValue 
  376.      * @return PHPExcel_Style_Font 
  377.      */
  378.     public function setBold($pValue false{
  379.         if ($pValue == ''{
  380.             $pValue false;
  381.         }
  382.         if ($this->_isSupervisor{
  383.             $styleArray $this->getStyleArray(array('bold' => $pValue));
  384.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  385.         else {
  386.             $this->_bold = $pValue;
  387.         }
  388.         return $this;
  389.     }
  390.     
  391.     /**
  392.      * Get Italic
  393.      *
  394.      * @return boolean 
  395.      */
  396.     public function getItalic({
  397.         if ($this->_isSupervisor{
  398.             return $this->getSharedComponent()->getItalic();
  399.         }
  400.         return $this->_italic;
  401.     }
  402.     
  403.     /**
  404.      * Set Italic
  405.      *
  406.      * @param boolean $pValue 
  407.      * @return PHPExcel_Style_Font 
  408.      */
  409.     public function setItalic($pValue false{
  410.         if ($pValue == ''{
  411.             $pValue false;
  412.         }
  413.         if ($this->_isSupervisor{
  414.             $styleArray $this->getStyleArray(array('italic' => $pValue));
  415.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  416.         else {
  417.             $this->_italic = $pValue;
  418.         }
  419.         return $this;
  420.     }
  421.     
  422.     /**
  423.      * Get SuperScript
  424.      *
  425.      * @return boolean 
  426.      */
  427.     public function getSuperScript({
  428.         if ($this->_isSupervisor{
  429.             return $this->getSharedComponent()->getSuperScript();
  430.         }
  431.         return $this->_superScript;
  432.     }
  433.     
  434.     /**
  435.      * Set SuperScript
  436.      *
  437.      * @param boolean $pValue 
  438.      * @return PHPExcel_Style_Font 
  439.      */
  440.     public function setSuperScript($pValue false{
  441.         if ($pValue == ''{
  442.             $pValue false;
  443.         }
  444.         if ($this->_isSupervisor{
  445.             $styleArray $this->getStyleArray(array('superScript' => $pValue));
  446.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  447.         else {
  448.             $this->_superScript = $pValue;
  449.             $this->_subScript = !$pValue;
  450.         }
  451.         return $this;
  452.     }
  453.     
  454.         /**
  455.      * Get SubScript
  456.      *
  457.      * @return boolean 
  458.      */
  459.     public function getSubScript({
  460.         if ($this->_isSupervisor{
  461.             return $this->getSharedComponent()->getSubScript();
  462.         }
  463.         return $this->_subScript;
  464.     }
  465.     
  466.     /**
  467.      * Set SubScript
  468.      *
  469.      * @param boolean $pValue 
  470.      * @return PHPExcel_Style_Font 
  471.      */
  472.     public function setSubScript($pValue false{
  473.         if ($pValue == ''{
  474.             $pValue false;
  475.         }
  476.         if ($this->_isSupervisor{
  477.             $styleArray $this->getStyleArray(array('subScript' => $pValue));
  478.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  479.         else {
  480.             $this->_subScript = $pValue;
  481.             $this->_superScript = !$pValue;
  482.         }
  483.         return $this;
  484.     }
  485.     
  486.     /**
  487.      * Get Underline
  488.      *
  489.      * @return string 
  490.      */
  491.     public function getUnderline({
  492.         if ($this->_isSupervisor{
  493.             return $this->getSharedComponent()->getUnderline();
  494.         }
  495.         return $this->_underline;
  496.     }
  497.     
  498.     /**
  499.      * Set Underline
  500.      *
  501.      * @param string $pValue    PHPExcel_Style_Font underline type
  502.      * @return PHPExcel_Style_Font 
  503.      */
  504.     public function setUnderline($pValue PHPExcel_Style_Font::UNDERLINE_NONE{
  505.         if ($pValue == ''{
  506.             $pValue PHPExcel_Style_Font::UNDERLINE_NONE;
  507.         }
  508.         if ($this->_isSupervisor{
  509.             $styleArray $this->getStyleArray(array('underline' => $pValue));
  510.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  511.         else {
  512.             $this->_underline = $pValue;
  513.         }
  514.         return $this;
  515.     }
  516.     
  517.     /**
  518.      * Get Striketrough
  519.      *
  520.      * @deprecated Use getStrikethrough() instead.
  521.      * @return boolean 
  522.      */
  523.     public function getStriketrough({
  524.         return $this->getStrikethrough();
  525.     }
  526.     
  527.     /**
  528.      * Set Striketrough
  529.      *
  530.      * @deprecated Use setStrikethrough() instead.
  531.      * @param boolean $pValue 
  532.      * @return PHPExcel_Style_Font 
  533.      */
  534.     public function setStriketrough($pValue false{
  535.         return $this->setStrikethrough($pValue);
  536.     }
  537.     
  538.     /**
  539.      * Get Strikethrough
  540.      *
  541.      * @return boolean 
  542.      */
  543.     public function getStrikethrough({
  544.         if ($this->_isSupervisor{
  545.             return $this->getSharedComponent()->getStrikethrough();
  546.         }
  547.         return $this->_strikethrough;
  548.     }
  549.     
  550.     /**
  551.      * Set Strikethrough
  552.      *
  553.      * @param boolean $pValue 
  554.      * @return PHPExcel_Style_Font 
  555.      */
  556.     public function setStrikethrough($pValue false{
  557.         if ($pValue == ''{
  558.             $pValue false;
  559.         }
  560.         if ($this->_isSupervisor{
  561.             $styleArray $this->getStyleArray(array('strike' => $pValue));
  562.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  563.         else {
  564.             $this->_strikethrough = $pValue;
  565.         }
  566.         return $this;
  567.     }
  568.  
  569.     /**
  570.      * Get Color
  571.      *
  572.      * @return PHPExcel_Style_Color 
  573.      */
  574.     public function getColor({
  575.         return $this->_color;
  576.     }
  577.     
  578.     /**
  579.      * Set Color
  580.      *
  581.      * @param     PHPExcel_Style_Color $pValue 
  582.      * @throws     Exception
  583.      * @return PHPExcel_Style_Font 
  584.      */
  585.     public function setColor(PHPExcel_Style_Color $pValue null{
  586.         // make sure parameter is a real color and not a supervisor
  587.         $color $pValue->getIsSupervisor($pValue->getSharedComponent($pValue;
  588.         
  589.         if ($this->_isSupervisor{
  590.             $styleArray $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
  591.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  592.         else {
  593.             $this->_color = $color;
  594.         }
  595.         return $this;
  596.     }
  597.  
  598.     /**
  599.      * Get hash code
  600.      *
  601.      * @return string    Hash code
  602.      */    
  603.     public function getHashCode({
  604.         if ($this->_isSupervisor{
  605.             return $this->getSharedComponent()->getHashCode();
  606.         }
  607.         return md5(
  608.               $this->_name
  609.             . $this->_size
  610.             . ($this->_bold ? 't' 'f')
  611.             . ($this->_italic ? 't' 'f')
  612.             . ($this->_superScript ? 't' 'f')
  613.             . ($this->_subScript ? 't' 'f')
  614.             . $this->_underline
  615.             . ($this->_strikethrough ? 't' 'f')
  616.             . $this->_color->getHashCode()
  617.             . __CLASS__
  618.         );
  619.     }
  620.     
  621.     /**
  622.      * Hash index
  623.      *
  624.      * @var string 
  625.      */
  626.     private $_hashIndex;
  627.     
  628.     /**
  629.      * Get hash index
  630.      * 
  631.      * Note that this index may vary during script execution! Only reliable moment is
  632.      * while doing a write of a workbook and when changes are not allowed.
  633.      *
  634.      * @return string    Hash index
  635.      */
  636.     public function getHashIndex({
  637.         return $this->_hashIndex;
  638.     }
  639.     
  640.     /**
  641.      * Set hash index
  642.      * 
  643.      * Note that this index may vary during script execution! Only reliable moment is
  644.      * while doing a write of a workbook and when changes are not allowed.
  645.      *
  646.      * @param string    $value    Hash index
  647.      */
  648.     public function setHashIndex($value{
  649.         $this->_hashIndex = $value;
  650.     }
  651.         
  652.     /**
  653.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  654.      */
  655.     public function __clone({
  656.         $vars get_object_vars($this);
  657.         foreach ($vars as $key => $value{
  658.             if (is_object($value)) {
  659.                 $this->$key clone $value;
  660.             else {
  661.                 $this->$key $value;
  662.             }
  663.         }
  664.     }
  665. }

Documentation generated on Mon, 10 Aug 2009 08:04:19 +0200 by phpDocumentor 1.4.1