5 * Copyright (c) 2006 - 2014 PHPExcel
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.
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.
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
22 * @package PHPExcel_Style
23 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25 * @version ##VERSION##, ##DATE##
33 * @package PHPExcel_Style
34 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
36 class PHPExcel_Style_Font
extends PHPExcel_Style_Supervisor
implements PHPExcel_IComparable
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';
50 protected $_name = 'Calibri';
57 protected $_size = 11;
64 protected $_bold = FALSE;
71 protected $_italic = FALSE;
78 protected $_superScript = FALSE;
85 protected $_subScript = FALSE;
92 protected $_underline = self
::UNDERLINE_NONE
;
99 protected $_strikethrough = FALSE;
104 * @var PHPExcel_Style_Color
109 * Create a new PHPExcel_Style_Font
111 * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
112 * Leave this value at default unless you understand exactly what
113 * its ramifications are
114 * @param boolean $isConditional Flag indicating if this is a conditional style or not
115 * Leave this value at default unless you understand exactly what
116 * its ramifications are
118 public function __construct($isSupervisor = FALSE, $isConditional = FALSE)
121 parent
::__construct($isSupervisor);
124 if ($isConditional) {
128 $this->_italic
= NULL;
129 $this->_superScript
= NULL;
130 $this->_subScript
= NULL;
131 $this->_underline
= NULL;
132 $this->_strikethrough
= NULL;
133 $this->_color
= new PHPExcel_Style_Color(PHPExcel_Style_Color
::COLOR_BLACK
, $isSupervisor, $isConditional);
135 $this->_color
= new PHPExcel_Style_Color(PHPExcel_Style_Color
::COLOR_BLACK
, $isSupervisor);
137 // bind parent if we are a supervisor
139 $this->_color
->bindParent($this, '_color');
144 * Get the shared style component for the currently active cell in currently active sheet.
145 * Only used for style supervisor
147 * @return PHPExcel_Style_Font
149 public function getSharedComponent()
151 return $this->_parent
->getSharedComponent()->getFont();
155 * Build style array from subcomponents
157 * @param array $array
160 public function getStyleArray($array)
162 return array('font' => $array);
166 * Apply styles from array
169 * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
174 * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
183 * @param array $pStyles Array containing style information
184 * @throws PHPExcel_Exception
185 * @return PHPExcel_Style_Font
187 public function applyFromArray($pStyles = null) {
188 if (is_array($pStyles)) {
189 if ($this->_isSupervisor
) {
190 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
192 if (array_key_exists('name', $pStyles)) {
193 $this->setName($pStyles['name']);
195 if (array_key_exists('bold', $pStyles)) {
196 $this->setBold($pStyles['bold']);
198 if (array_key_exists('italic', $pStyles)) {
199 $this->setItalic($pStyles['italic']);
201 if (array_key_exists('superScript', $pStyles)) {
202 $this->setSuperScript($pStyles['superScript']);
204 if (array_key_exists('subScript', $pStyles)) {
205 $this->setSubScript($pStyles['subScript']);
207 if (array_key_exists('underline', $pStyles)) {
208 $this->setUnderline($pStyles['underline']);
210 if (array_key_exists('strike', $pStyles)) {
211 $this->setStrikethrough($pStyles['strike']);
213 if (array_key_exists('color', $pStyles)) {
214 $this->getColor()->applyFromArray($pStyles['color']);
216 if (array_key_exists('size', $pStyles)) {
217 $this->setSize($pStyles['size']);
221 throw new PHPExcel_Exception("Invalid style array passed.");
231 public function getName() {
232 if ($this->_isSupervisor
) {
233 return $this->getSharedComponent()->getName();
241 * @param string $pValue
242 * @return PHPExcel_Style_Font
244 public function setName($pValue = 'Calibri') {
248 if ($this->_isSupervisor
) {
249 $styleArray = $this->getStyleArray(array('name' => $pValue));
250 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
252 $this->_name
= $pValue;
262 public function getSize() {
263 if ($this->_isSupervisor
) {
264 return $this->getSharedComponent()->getSize();
272 * @param double $pValue
273 * @return PHPExcel_Style_Font
275 public function setSize($pValue = 10) {
279 if ($this->_isSupervisor
) {
280 $styleArray = $this->getStyleArray(array('size' => $pValue));
281 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
283 $this->_size
= $pValue;
293 public function getBold() {
294 if ($this->_isSupervisor
) {
295 return $this->getSharedComponent()->getBold();
303 * @param boolean $pValue
304 * @return PHPExcel_Style_Font
306 public function setBold($pValue = false) {
310 if ($this->_isSupervisor
) {
311 $styleArray = $this->getStyleArray(array('bold' => $pValue));
312 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
314 $this->_bold
= $pValue;
324 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) {
341 if ($this->_isSupervisor
) {
342 $styleArray = $this->getStyleArray(array('italic' => $pValue));
343 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
345 $this->_italic
= $pValue;
355 public function getSuperScript() {
356 if ($this->_isSupervisor
) {
357 return $this->getSharedComponent()->getSuperScript();
359 return $this->_superScript
;
365 * @param boolean $pValue
366 * @return PHPExcel_Style_Font
368 public function setSuperScript($pValue = false) {
372 if ($this->_isSupervisor
) {
373 $styleArray = $this->getStyleArray(array('superScript' => $pValue));
374 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
376 $this->_superScript
= $pValue;
377 $this->_subScript
= !$pValue;
387 public function getSubScript() {
388 if ($this->_isSupervisor
) {
389 return $this->getSharedComponent()->getSubScript();
391 return $this->_subScript
;
397 * @param boolean $pValue
398 * @return PHPExcel_Style_Font
400 public function setSubScript($pValue = false) {
404 if ($this->_isSupervisor
) {
405 $styleArray = $this->getStyleArray(array('subScript' => $pValue));
406 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
408 $this->_subScript
= $pValue;
409 $this->_superScript
= !$pValue;
419 public function getUnderline() {
420 if ($this->_isSupervisor
) {
421 return $this->getSharedComponent()->getUnderline();
423 return $this->_underline
;
429 * @param string|boolean $pValue PHPExcel_Style_Font underline type
430 * If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE,
431 * false equates to UNDERLINE_NONE
432 * @return PHPExcel_Style_Font
434 public function setUnderline($pValue = self
::UNDERLINE_NONE
) {
435 if (is_bool($pValue)) {
436 $pValue = ($pValue) ? self
::UNDERLINE_SINGLE
: self
::UNDERLINE_NONE
;
437 } elseif ($pValue == '') {
438 $pValue = self
::UNDERLINE_NONE
;
440 if ($this->_isSupervisor
) {
441 $styleArray = $this->getStyleArray(array('underline' => $pValue));
442 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
444 $this->_underline
= $pValue;
454 public function getStrikethrough() {
455 if ($this->_isSupervisor
) {
456 return $this->getSharedComponent()->getStrikethrough();
458 return $this->_strikethrough
;
464 * @param boolean $pValue
465 * @return PHPExcel_Style_Font
467 public function setStrikethrough($pValue = false) {
471 if ($this->_isSupervisor
) {
472 $styleArray = $this->getStyleArray(array('strike' => $pValue));
473 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
475 $this->_strikethrough
= $pValue;
483 * @return PHPExcel_Style_Color
485 public function getColor() {
486 return $this->_color
;
492 * @param PHPExcel_Style_Color $pValue
493 * @throws PHPExcel_Exception
494 * @return PHPExcel_Style_Font
496 public function setColor(PHPExcel_Style_Color
$pValue = null) {
497 // make sure parameter is a real color and not a supervisor
498 $color = $pValue->getIsSupervisor() ?
$pValue->getSharedComponent() : $pValue;
500 if ($this->_isSupervisor
) {
501 $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
502 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
504 $this->_color
= $color;
512 * @return string Hash code
514 public function getHashCode() {
515 if ($this->_isSupervisor
) {
516 return $this->getSharedComponent()->getHashCode();
521 . ($this->_bold ?
't' : 'f')
522 . ($this->_italic ?
't' : 'f')
523 . ($this->_superScript ?
't' : 'f')
524 . ($this->_subScript ?
't' : 'f')
526 . ($this->_strikethrough ?
't' : 'f')
527 . $this->_color
->getHashCode()