PageRenderTime 35ms CodeModel.GetById 14ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/PHPExcel/Style/Font.php

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