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

Documentation generated on Sun, 27 Feb 2011 16:31:40 -0800 by phpDocumentor 1.4.3