[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Style/ -> Font.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_Style_Font
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_Style
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_Style_Font extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  29  {
  30      /* Underline types */
  31      const UNDERLINE_NONE             = 'none';
  32      const UNDERLINE_DOUBLE           = 'double';
  33      const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting';
  34      const UNDERLINE_SINGLE           = 'single';
  35      const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting';
  36  
  37      /**
  38       * Font Name
  39       *
  40       * @var string
  41       */
  42      protected $name = 'Calibri';
  43  
  44      /**
  45       * Font Size
  46       *
  47       * @var float
  48       */
  49      protected $size = 11;
  50  
  51      /**
  52       * Bold
  53       *
  54       * @var boolean
  55       */
  56      protected $bold = false;
  57  
  58      /**
  59       * Italic
  60       *
  61       * @var boolean
  62       */
  63      protected $italic = false;
  64  
  65      /**
  66       * Superscript
  67       *
  68       * @var boolean
  69       */
  70      protected $superScript = false;
  71  
  72      /**
  73       * Subscript
  74       *
  75       * @var boolean
  76       */
  77      protected $subScript = false;
  78  
  79      /**
  80       * Underline
  81       *
  82       * @var string
  83       */
  84      protected $underline = self::UNDERLINE_NONE;
  85  
  86      /**
  87       * Strikethrough
  88       *
  89       * @var boolean
  90       */
  91      protected $strikethrough = false;
  92  
  93      /**
  94       * Foreground color
  95       *
  96       * @var PHPExcel_Style_Color
  97       */
  98      protected $color;
  99  
 100      /**
 101       * Create a new PHPExcel_Style_Font
 102       *
 103       * @param    boolean    $isSupervisor    Flag indicating if this is a supervisor or not
 104       *                                    Leave this value at default unless you understand exactly what
 105       *                                        its ramifications are
 106       * @param    boolean    $isConditional    Flag indicating if this is a conditional style or not
 107       *                                    Leave this value at default unless you understand exactly what
 108       *                                        its ramifications are
 109       */
 110      public function __construct($isSupervisor = false, $isConditional = false)
 111      {
 112          // Supervisor?
 113          parent::__construct($isSupervisor);
 114  
 115          // Initialise values
 116          if ($isConditional) {
 117              $this->name = null;
 118              $this->size = null;
 119              $this->bold = null;
 120              $this->italic = null;
 121              $this->superScript = null;
 122              $this->subScript = null;
 123              $this->underline = null;
 124              $this->strikethrough = null;
 125              $this->color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor, $isConditional);
 126          } else {
 127              $this->color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
 128          }
 129          // bind parent if we are a supervisor
 130          if ($isSupervisor) {
 131              $this->color->bindParent($this, 'color');
 132          }
 133      }
 134  
 135      /**
 136       * Get the shared style component for the currently active cell in currently active sheet.
 137       * Only used for style supervisor
 138       *
 139       * @return PHPExcel_Style_Font
 140       */
 141      public function getSharedComponent()
 142      {
 143          return $this->parent->getSharedComponent()->getFont();
 144      }
 145  
 146      /**
 147       * Build style array from subcomponents
 148       *
 149       * @param array $array
 150       * @return array
 151       */
 152      public function getStyleArray($array)
 153      {
 154          return array('font' => $array);
 155      }
 156  
 157      /**
 158       * Apply styles from array
 159       *
 160       * <code>
 161       * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
 162       *        array(
 163       *            'name'        => 'Arial',
 164       *            'bold'        => TRUE,
 165       *            'italic'    => FALSE,
 166       *            'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
 167       *            'strike'    => FALSE,
 168       *            'color'        => array(
 169       *                'rgb' => '808080'
 170       *            )
 171       *        )
 172       * );
 173       * </code>
 174       *
 175       * @param    array    $pStyles    Array containing style information
 176       * @throws    PHPExcel_Exception
 177       * @return PHPExcel_Style_Font
 178       */
 179      public function applyFromArray($pStyles = null)
 180      {
 181          if (is_array($pStyles)) {
 182              if ($this->isSupervisor) {
 183                  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
 184              } else {
 185                  if (array_key_exists('name', $pStyles)) {
 186                      $this->setName($pStyles['name']);
 187                  }
 188                  if (array_key_exists('bold', $pStyles)) {
 189                      $this->setBold($pStyles['bold']);
 190                  }
 191                  if (array_key_exists('italic', $pStyles)) {
 192                      $this->setItalic($pStyles['italic']);
 193                  }
 194                  if (array_key_exists('superScript', $pStyles)) {
 195                      $this->setSuperScript($pStyles['superScript']);
 196                  }
 197                  if (array_key_exists('subScript', $pStyles)) {
 198                      $this->setSubScript($pStyles['subScript']);
 199                  }
 200                  if (array_key_exists('underline', $pStyles)) {
 201                      $this->setUnderline($pStyles['underline']);
 202                  }
 203                  if (array_key_exists('strike', $pStyles)) {
 204                      $this->setStrikethrough($pStyles['strike']);
 205                  }
 206                  if (array_key_exists('color', $pStyles)) {
 207                      $this->getColor()->applyFromArray($pStyles['color']);
 208                  }
 209                  if (array_key_exists('size', $pStyles)) {
 210                      $this->setSize($pStyles['size']);
 211                  }
 212              }
 213          } else {
 214              throw new PHPExcel_Exception("Invalid style array passed.");
 215          }
 216          return $this;
 217      }
 218  
 219      /**
 220       * Get Name
 221       *
 222       * @return string
 223       */
 224      public function getName()
 225      {
 226          if ($this->isSupervisor) {
 227              return $this->getSharedComponent()->getName();
 228          }
 229          return $this->name;
 230      }
 231  
 232      /**
 233       * Set Name
 234       *
 235       * @param string $pValue
 236       * @return PHPExcel_Style_Font
 237       */
 238      public function setName($pValue = 'Calibri')
 239      {
 240          if ($pValue == '') {
 241              $pValue = 'Calibri';
 242          }
 243          if ($this->isSupervisor) {
 244              $styleArray = $this->getStyleArray(array('name' => $pValue));
 245              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 246          } else {
 247              $this->name = $pValue;
 248          }
 249          return $this;
 250      }
 251  
 252      /**
 253       * Get Size
 254       *
 255       * @return double
 256       */
 257      public function getSize()
 258      {
 259          if ($this->isSupervisor) {
 260              return $this->getSharedComponent()->getSize();
 261          }
 262          return $this->size;
 263      }
 264  
 265      /**
 266       * Set Size
 267       *
 268       * @param double $pValue
 269       * @return PHPExcel_Style_Font
 270       */
 271      public function setSize($pValue = 10)
 272      {
 273          if ($pValue == '') {
 274              $pValue = 10;
 275          }
 276          if ($this->isSupervisor) {
 277              $styleArray = $this->getStyleArray(array('size' => $pValue));
 278              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 279          } else {
 280              $this->size = $pValue;
 281          }
 282          return $this;
 283      }
 284  
 285      /**
 286       * Get Bold
 287       *
 288       * @return boolean
 289       */
 290      public function getBold()
 291      {
 292          if ($this->isSupervisor) {
 293              return $this->getSharedComponent()->getBold();
 294          }
 295          return $this->bold;
 296      }
 297  
 298      /**
 299       * Set Bold
 300       *
 301       * @param boolean $pValue
 302       * @return PHPExcel_Style_Font
 303       */
 304      public function setBold($pValue = false)
 305      {
 306          if ($pValue == '') {
 307              $pValue = false;
 308          }
 309          if ($this->isSupervisor) {
 310              $styleArray = $this->getStyleArray(array('bold' => $pValue));
 311              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 312          } else {
 313              $this->bold = $pValue;
 314          }
 315          return $this;
 316      }
 317  
 318      /**
 319       * Get Italic
 320       *
 321       * @return boolean
 322       */
 323      public function getItalic()
 324      {
 325          if ($this->isSupervisor) {
 326              return $this->getSharedComponent()->getItalic();
 327          }
 328          return $this->italic;
 329      }
 330  
 331      /**
 332       * Set Italic
 333       *
 334       * @param boolean $pValue
 335       * @return PHPExcel_Style_Font
 336       */
 337      public function setItalic($pValue = false)
 338      {
 339          if ($pValue == '') {
 340              $pValue = false;
 341          }
 342          if ($this->isSupervisor) {
 343              $styleArray = $this->getStyleArray(array('italic' => $pValue));
 344              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 345          } else {
 346              $this->italic = $pValue;
 347          }
 348          return $this;
 349      }
 350  
 351      /**
 352       * Get SuperScript
 353       *
 354       * @return boolean
 355       */
 356      public function getSuperScript()
 357      {
 358          if ($this->isSupervisor) {
 359              return $this->getSharedComponent()->getSuperScript();
 360          }
 361          return $this->superScript;
 362      }
 363  
 364      /**
 365       * Set SuperScript
 366       *
 367       * @param boolean $pValue
 368       * @return PHPExcel_Style_Font
 369       */
 370      public function setSuperScript($pValue = false)
 371      {
 372          if ($pValue == '') {
 373              $pValue = false;
 374          }
 375          if ($this->isSupervisor) {
 376              $styleArray = $this->getStyleArray(array('superScript' => $pValue));
 377              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 378          } else {
 379              $this->superScript = $pValue;
 380              $this->subScript = !$pValue;
 381          }
 382          return $this;
 383      }
 384  
 385          /**
 386       * Get SubScript
 387       *
 388       * @return boolean
 389       */
 390      public function getSubScript()
 391      {
 392          if ($this->isSupervisor) {
 393              return $this->getSharedComponent()->getSubScript();
 394          }
 395          return $this->subScript;
 396      }
 397  
 398      /**
 399       * Set SubScript
 400       *
 401       * @param boolean $pValue
 402       * @return PHPExcel_Style_Font
 403       */
 404      public function setSubScript($pValue = false)
 405      {
 406          if ($pValue == '') {
 407              $pValue = false;
 408          }
 409          if ($this->isSupervisor) {
 410              $styleArray = $this->getStyleArray(array('subScript' => $pValue));
 411              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 412          } else {
 413              $this->subScript = $pValue;
 414              $this->superScript = !$pValue;
 415          }
 416          return $this;
 417      }
 418  
 419      /**
 420       * Get Underline
 421       *
 422       * @return string
 423       */
 424      public function getUnderline()
 425      {
 426          if ($this->isSupervisor) {
 427              return $this->getSharedComponent()->getUnderline();
 428          }
 429          return $this->underline;
 430      }
 431  
 432      /**
 433       * Set Underline
 434       *
 435       * @param string|boolean $pValue    PHPExcel_Style_Font underline type
 436       *                                    If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE,
 437       *                                        false equates to UNDERLINE_NONE
 438       * @return PHPExcel_Style_Font
 439       */
 440      public function setUnderline($pValue = self::UNDERLINE_NONE)
 441      {
 442          if (is_bool($pValue)) {
 443              $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
 444          } elseif ($pValue == '') {
 445              $pValue = self::UNDERLINE_NONE;
 446          }
 447          if ($this->isSupervisor) {
 448              $styleArray = $this->getStyleArray(array('underline' => $pValue));
 449              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 450          } else {
 451              $this->underline = $pValue;
 452          }
 453          return $this;
 454      }
 455  
 456      /**
 457       * Get Strikethrough
 458       *
 459       * @return boolean
 460       */
 461      public function getStrikethrough()
 462      {
 463          if ($this->isSupervisor) {
 464              return $this->getSharedComponent()->getStrikethrough();
 465          }
 466          return $this->strikethrough;
 467      }
 468  
 469      /**
 470       * Set Strikethrough
 471       *
 472       * @param boolean $pValue
 473       * @return PHPExcel_Style_Font
 474       */
 475      public function setStrikethrough($pValue = false)
 476      {
 477          if ($pValue == '') {
 478              $pValue = false;
 479          }
 480          if ($this->isSupervisor) {
 481              $styleArray = $this->getStyleArray(array('strike' => $pValue));
 482              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 483          } else {
 484              $this->strikethrough = $pValue;
 485          }
 486          return $this;
 487      }
 488  
 489      /**
 490       * Get Color
 491       *
 492       * @return PHPExcel_Style_Color
 493       */
 494      public function getColor()
 495      {
 496          return $this->color;
 497      }
 498  
 499      /**
 500       * Set Color
 501       *
 502       * @param    PHPExcel_Style_Color $pValue
 503       * @throws    PHPExcel_Exception
 504       * @return PHPExcel_Style_Font
 505       */
 506      public function setColor(PHPExcel_Style_Color $pValue = null)
 507      {
 508          // make sure parameter is a real color and not a supervisor
 509          $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
 510  
 511          if ($this->isSupervisor) {
 512              $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
 513              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 514          } else {
 515              $this->color = $color;
 516          }
 517          return $this;
 518      }
 519  
 520      /**
 521       * Get hash code
 522       *
 523       * @return string    Hash code
 524       */
 525      public function getHashCode()
 526      {
 527          if ($this->isSupervisor) {
 528              return $this->getSharedComponent()->getHashCode();
 529          }
 530          return md5(
 531              $this->name .
 532              $this->size .
 533              ($this->bold ? 't' : 'f') .
 534              ($this->italic ? 't' : 'f') .
 535              ($this->superScript ? 't' : 'f') .
 536              ($this->subScript ? 't' : 'f') .
 537              $this->underline .
 538              ($this->strikethrough ? 't' : 'f') .
 539              $this->color->getHashCode() .
 540              __CLASS__
 541          );
 542      }
 543  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1