6 * Copyright (c) 2006 - 2015 PHPExcel
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.
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.
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
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##
28 class PHPExcel_Style_Font extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
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';
42 protected $name = 'Calibri';
56 protected $bold = false;
63 protected $italic = false;
70 protected $superScript = false;
77 protected $subScript = false;
84 protected $underline = self::UNDERLINE_NONE;
91 protected $strikethrough = false;
96 * @var PHPExcel_Style_Color
101 * Create a new PHPExcel_Style_Font
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
110 public function __construct($isSupervisor = false, $isConditional = false)
113 parent::__construct($isSupervisor);
116 if ($isConditional) {
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);
127 $this->color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
129 // bind parent if we are a supervisor
131 $this->color->bindParent($this, 'color');
136 * Get the shared style component for the currently active cell in currently active sheet.
137 * Only used for style supervisor
139 * @return PHPExcel_Style_Font
141 public function getSharedComponent()
143 return $this->parent->getSharedComponent()->getFont();
147 * Build style array from subcomponents
149 * @param array $array
152 public function getStyleArray($array)
154 return array('font' => $array);
158 * Apply styles from array
161 * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
166 * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
175 * @param array $pStyles Array containing style information
176 * @throws PHPExcel_Exception
177 * @return PHPExcel_Style_Font
179 public function applyFromArray($pStyles = null)
181 if (is_array($pStyles)) {
182 if ($this->isSupervisor) {
183 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
185 if (array_key_exists('name', $pStyles)) {
186 $this->setName($pStyles['name']);
188 if (array_key_exists('bold', $pStyles)) {
189 $this->setBold($pStyles['bold']);
191 if (array_key_exists('italic', $pStyles)) {
192 $this->setItalic($pStyles['italic']);
194 if (array_key_exists('superScript', $pStyles)) {
195 $this->setSuperScript($pStyles['superScript']);
197 if (array_key_exists('subScript', $pStyles)) {
198 $this->setSubScript($pStyles['subScript']);
200 if (array_key_exists('underline', $pStyles)) {
201 $this->setUnderline($pStyles['underline']);
203 if (array_key_exists('strike', $pStyles)) {
204 $this->setStrikethrough($pStyles['strike']);
206 if (array_key_exists('color', $pStyles)) {
207 $this->getColor()->applyFromArray($pStyles['color']);
209 if (array_key_exists('size', $pStyles)) {
210 $this->setSize($pStyles['size']);
214 throw new PHPExcel_Exception("Invalid style array passed.");
224 public function getName()
226 if ($this->isSupervisor) {
227 return $this->getSharedComponent()->getName();
235 * @param string $pValue
236 * @return PHPExcel_Style_Font
238 public function setName($pValue = 'Calibri')
243 if ($this->isSupervisor) {
244 $styleArray = $this->getStyleArray(array('name' => $pValue));
245 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
247 $this->name = $pValue;
257 public function getSize()
259 if ($this->isSupervisor) {
260 return $this->getSharedComponent()->getSize();
268 * @param double $pValue
269 * @return PHPExcel_Style_Font
271 public function setSize($pValue = 10)
276 if ($this->isSupervisor) {
277 $styleArray = $this->getStyleArray(array('size' => $pValue));
278 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
280 $this->size = $pValue;
290 public function getBold()
292 if ($this->isSupervisor) {
293 return $this->getSharedComponent()->getBold();
301 * @param boolean $pValue
302 * @return PHPExcel_Style_Font
304 public function setBold($pValue = false)
309 if ($this->isSupervisor) {
310 $styleArray = $this->getStyleArray(array('bold' => $pValue));
311 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
313 $this->bold = $pValue;
323 public function getItalic()
325 if ($this->isSupervisor) {
326 return $this->getSharedComponent()->getItalic();
328 return $this->italic;
334 * @param boolean $pValue
335 * @return PHPExcel_Style_Font
337 public function setItalic($pValue = false)
342 if ($this->isSupervisor) {
343 $styleArray = $this->getStyleArray(array('italic' => $pValue));
344 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
346 $this->italic = $pValue;
356 public function getSuperScript()
358 if ($this->isSupervisor) {
359 return $this->getSharedComponent()->getSuperScript();
361 return $this->superScript;
367 * @param boolean $pValue
368 * @return PHPExcel_Style_Font
370 public function setSuperScript($pValue = false)
375 if ($this->isSupervisor) {
376 $styleArray = $this->getStyleArray(array('superScript' => $pValue));
377 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
379 $this->superScript = $pValue;
380 $this->subScript = !$pValue;
390 public function getSubScript()
392 if ($this->isSupervisor) {
393 return $this->getSharedComponent()->getSubScript();
395 return $this->subScript;
401 * @param boolean $pValue
402 * @return PHPExcel_Style_Font
404 public function setSubScript($pValue = false)
409 if ($this->isSupervisor) {
410 $styleArray = $this->getStyleArray(array('subScript' => $pValue));
411 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
413 $this->subScript = $pValue;
414 $this->superScript = !$pValue;
424 public function getUnderline()
426 if ($this->isSupervisor) {
427 return $this->getSharedComponent()->getUnderline();
429 return $this->underline;
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
440 public function setUnderline($pValue = self::UNDERLINE_NONE)
442 if (is_bool($pValue)) {
443 $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
444 } elseif ($pValue == '') {
445 $pValue = self::UNDERLINE_NONE;
447 if ($this->isSupervisor) {
448 $styleArray = $this->getStyleArray(array('underline' => $pValue));
449 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
451 $this->underline = $pValue;
461 public function getStrikethrough()
463 if ($this->isSupervisor) {
464 return $this->getSharedComponent()->getStrikethrough();
466 return $this->strikethrough;
472 * @param boolean $pValue
473 * @return PHPExcel_Style_Font
475 public function setStrikethrough($pValue = false)
480 if ($this->isSupervisor) {
481 $styleArray = $this->getStyleArray(array('strike' => $pValue));
482 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
484 $this->strikethrough = $pValue;
492 * @return PHPExcel_Style_Color
494 public function getColor()
502 * @param PHPExcel_Style_Color $pValue
503 * @throws PHPExcel_Exception
504 * @return PHPExcel_Style_Font
506 public function setColor(PHPExcel_Style_Color $pValue = null)
508 // make sure parameter is a real color and not a supervisor
509 $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
511 if ($this->isSupervisor) {
512 $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
513 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
515 $this->color = $color;
523 * @return string Hash code
525 public function getHashCode()
527 if ($this->isSupervisor) {
528 return $this->getSharedComponent()->getHashCode();
533 ($this->bold ? 't' : 'f') .
534 ($this->italic ? 't' : 'f') .
535 ($this->superScript ? 't' : 'f') .
536 ($this->subScript ? 't' : 'f') .
538 ($this->strikethrough ? 't' : 'f') .
539 $this->color->getHashCode() .