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

Documentation generated on Tue, 01 Jun 2010 17:03:50 +0200 by phpDocumentor 1.4.3