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##
30 * PHPExcel_Style_Alignment
33 * @package PHPExcel_Style
34 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
36 class PHPExcel_Style_Alignment
extends PHPExcel_Style_Supervisor
implements PHPExcel_IComparable
38 /* Horizontal alignment styles */
39 const HORIZONTAL_GENERAL
= 'general';
40 const HORIZONTAL_LEFT
= 'left';
41 const HORIZONTAL_RIGHT
= 'right';
42 const HORIZONTAL_CENTER
= 'center';
43 const HORIZONTAL_CENTER_CONTINUOUS
= 'centerContinuous';
44 const HORIZONTAL_JUSTIFY
= 'justify';
45 const HORIZONTAL_FILL
= 'fill';
46 const HORIZONTAL_DISTRIBUTED
= 'distributed'; // Excel2007 only
48 /* Vertical alignment styles */
49 const VERTICAL_BOTTOM
= 'bottom';
50 const VERTICAL_TOP
= 'top';
51 const VERTICAL_CENTER
= 'center';
52 const VERTICAL_JUSTIFY
= 'justify';
53 const VERTICAL_DISTRIBUTED
= 'distributed'; // Excel2007 only
56 const READORDER_CONTEXT
= 0;
57 const READORDER_LTR
= 1;
58 const READORDER_RTL
= 2;
61 * Horizontal alignment
65 protected $_horizontal = PHPExcel_Style_Alignment
::HORIZONTAL_GENERAL
;
72 protected $_vertical = PHPExcel_Style_Alignment
::VERTICAL_BOTTOM
;
79 protected $_textRotation = 0;
86 protected $_wrapText = FALSE;
93 protected $_shrinkToFit = FALSE;
96 * Indent - only possible with horizontal alignment left and right
100 protected $_indent = 0;
107 protected $_readorder = 0;
110 * Create a new PHPExcel_Style_Alignment
112 * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
113 * Leave this value at default unless you understand exactly what
114 * its ramifications are
115 * @param boolean $isConditional Flag indicating if this is a conditional style or not
116 * Leave this value at default unless you understand exactly what
117 * its ramifications are
119 public function __construct($isSupervisor = FALSE, $isConditional = FALSE)
122 parent
::__construct($isSupervisor);
124 if ($isConditional) {
125 $this->_horizontal
= NULL;
126 $this->_vertical
= NULL;
127 $this->_textRotation
= NULL;
132 * Get the shared style component for the currently active cell in currently active sheet.
133 * Only used for style supervisor
135 * @return PHPExcel_Style_Alignment
137 public function getSharedComponent()
139 return $this->_parent
->getSharedComponent()->getAlignment();
143 * Build style array from subcomponents
145 * @param array $array
148 public function getStyleArray($array)
150 return array('alignment' => $array);
154 * Apply styles from array
157 * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
159 * 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
160 * 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
167 * @param array $pStyles Array containing style information
168 * @throws PHPExcel_Exception
169 * @return PHPExcel_Style_Alignment
171 public function applyFromArray($pStyles = NULL) {
172 if (is_array($pStyles)) {
173 if ($this->_isSupervisor
) {
174 $this->getActiveSheet()->getStyle($this->getSelectedCells())
175 ->applyFromArray($this->getStyleArray($pStyles));
177 if (isset($pStyles['horizontal'])) {
178 $this->setHorizontal($pStyles['horizontal']);
180 if (isset($pStyles['vertical'])) {
181 $this->setVertical($pStyles['vertical']);
183 if (isset($pStyles['rotation'])) {
184 $this->setTextRotation($pStyles['rotation']);
186 if (isset($pStyles['wrap'])) {
187 $this->setWrapText($pStyles['wrap']);
189 if (isset($pStyles['shrinkToFit'])) {
190 $this->setShrinkToFit($pStyles['shrinkToFit']);
192 if (isset($pStyles['indent'])) {
193 $this->setIndent($pStyles['indent']);
195 if (isset($pStyles['readorder'])) {
196 $this->setReadorder($pStyles['readorder']);
200 throw new PHPExcel_Exception("Invalid style array passed.");
210 public function getHorizontal() {
211 if ($this->_isSupervisor
) {
212 return $this->getSharedComponent()->getHorizontal();
214 return $this->_horizontal
;
220 * @param string $pValue
221 * @return PHPExcel_Style_Alignment
223 public function setHorizontal($pValue = PHPExcel_Style_Alignment
::HORIZONTAL_GENERAL
) {
225 $pValue = PHPExcel_Style_Alignment
::HORIZONTAL_GENERAL
;
228 if ($this->_isSupervisor
) {
229 $styleArray = $this->getStyleArray(array('horizontal' => $pValue));
230 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
233 $this->_horizontal
= $pValue;
243 public function getVertical() {
244 if ($this->_isSupervisor
) {
245 return $this->getSharedComponent()->getVertical();
247 return $this->_vertical
;
253 * @param string $pValue
254 * @return PHPExcel_Style_Alignment
256 public function setVertical($pValue = PHPExcel_Style_Alignment
::VERTICAL_BOTTOM
) {
258 $pValue = PHPExcel_Style_Alignment
::VERTICAL_BOTTOM
;
261 if ($this->_isSupervisor
) {
262 $styleArray = $this->getStyleArray(array('vertical' => $pValue));
263 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
265 $this->_vertical
= $pValue;
275 public function getTextRotation() {
276 if ($this->_isSupervisor
) {
277 return $this->getSharedComponent()->getTextRotation();
279 return $this->_textRotation
;
286 * @throws PHPExcel_Exception
287 * @return PHPExcel_Style_Alignment
289 public function setTextRotation($pValue = 0) {
290 // Excel2007 value 255 => PHPExcel value -165
291 if ($pValue == 255) {
296 if ( ($pValue >= -90 && $pValue <= 90) ||
$pValue == -165 ) {
297 if ($this->_isSupervisor
) {
298 $styleArray = $this->getStyleArray(array('rotation' => $pValue));
299 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
301 $this->_textRotation
= $pValue;
304 throw new PHPExcel_Exception("Text rotation should be a value between -90 and 90.");
315 public function getWrapText() {
316 if ($this->_isSupervisor
) {
317 return $this->getSharedComponent()->getWrapText();
319 return $this->_wrapText
;
325 * @param boolean $pValue
326 * @return PHPExcel_Style_Alignment
328 public function setWrapText($pValue = FALSE) {
332 if ($this->_isSupervisor
) {
333 $styleArray = $this->getStyleArray(array('wrap' => $pValue));
334 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
336 $this->_wrapText
= $pValue;
346 public function getShrinkToFit() {
347 if ($this->_isSupervisor
) {
348 return $this->getSharedComponent()->getShrinkToFit();
350 return $this->_shrinkToFit
;
356 * @param boolean $pValue
357 * @return PHPExcel_Style_Alignment
359 public function setShrinkToFit($pValue = FALSE) {
363 if ($this->_isSupervisor
) {
364 $styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
365 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
367 $this->_shrinkToFit
= $pValue;
377 public function getIndent() {
378 if ($this->_isSupervisor
) {
379 return $this->getSharedComponent()->getIndent();
381 return $this->_indent
;
388 * @return PHPExcel_Style_Alignment
390 public function setIndent($pValue = 0) {
392 if ($this->getHorizontal() != self
::HORIZONTAL_GENERAL
&&
393 $this->getHorizontal() != self
::HORIZONTAL_LEFT
&&
394 $this->getHorizontal() != self
::HORIZONTAL_RIGHT
) {
395 $pValue = 0; // indent not supported
398 if ($this->_isSupervisor
) {
399 $styleArray = $this->getStyleArray(array('indent' => $pValue));
400 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
402 $this->_indent
= $pValue;
412 public function getReadorder() {
413 if ($this->_isSupervisor
) {
414 return $this->getSharedComponent()->getReadorder();
416 return $this->_readorder
;
423 * @return PHPExcel_Style_Alignment
425 public function setReadorder($pValue = 0) {
426 if ($pValue < 0 ||
$pValue > 2) {
429 if ($this->_isSupervisor
) {
430 $styleArray = $this->getStyleArray(array('readorder' => $pValue));
431 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
433 $this->_readorder
= $pValue;
441 * @return string Hash code
443 public function getHashCode() {
444 if ($this->_isSupervisor
) {
445 return $this->getSharedComponent()->getHashCode();
450 . $this->_textRotation
451 . ($this->_wrapText ?
't' : 'f')
452 . ($this->_shrinkToFit ?
't' : 'f')