PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.7.2/Classes/PHPExcel/Style/Alignment.php

#
PHP | 501 lines | 234 code | 43 blank | 224 comment | 54 complexity | fa797e01249f049b9595da488dc1d277 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. }
  34. /** PHPExcel_IComparable */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel/IComparable.php';
  36. /**
  37. * PHPExcel_Style_Alignment
  38. *
  39. * @category PHPExcel
  40. * @package PHPExcel_Style
  41. * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  42. */
  43. class PHPExcel_Style_Alignment implements PHPExcel_IComparable
  44. {
  45. /* Horizontal alignment styles */
  46. const HORIZONTAL_GENERAL = 'general';
  47. const HORIZONTAL_LEFT = 'left';
  48. const HORIZONTAL_RIGHT = 'right';
  49. const HORIZONTAL_CENTER = 'center';
  50. const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
  51. const HORIZONTAL_JUSTIFY = 'justify';
  52. /* Vertical alignment styles */
  53. const VERTICAL_BOTTOM = 'bottom';
  54. const VERTICAL_TOP = 'top';
  55. const VERTICAL_CENTER = 'center';
  56. const VERTICAL_JUSTIFY = 'justify';
  57. /**
  58. * Horizontal
  59. *
  60. * @var string
  61. */
  62. private $_horizontal;
  63. /**
  64. * Vertical
  65. *
  66. * @var string
  67. */
  68. private $_vertical;
  69. /**
  70. * Text rotation
  71. *
  72. * @var int
  73. */
  74. private $_textRotation;
  75. /**
  76. * Wrap text
  77. *
  78. * @var boolean
  79. */
  80. private $_wrapText;
  81. /**
  82. * Shrink to fit
  83. *
  84. * @var boolean
  85. */
  86. private $_shrinkToFit;
  87. /**
  88. * Indent - only possible with horizontal alignment left and right
  89. *
  90. * @var int
  91. */
  92. private $_indent;
  93. /**
  94. * Parent Borders
  95. *
  96. * @var _parentPropertyName string
  97. */
  98. private $_parentPropertyName;
  99. /**
  100. * Supervisor?
  101. *
  102. * @var boolean
  103. */
  104. private $_isSupervisor;
  105. /**
  106. * Parent. Only used for supervisor
  107. *
  108. * @var PHPExcel_Style
  109. */
  110. private $_parent;
  111. /**
  112. * Create a new PHPExcel_Style_Alignment
  113. */
  114. public function __construct($isSupervisor = false)
  115. {
  116. // Supervisor?
  117. $this->_isSupervisor = $isSupervisor;
  118. // Initialise values
  119. $this->_horizontal = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  120. $this->_vertical = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  121. $this->_textRotation = 0;
  122. $this->_wrapText = false;
  123. $this->_shrinkToFit = false;
  124. $this->_indent = 0;
  125. }
  126. /**
  127. * Bind parent. Only used for supervisor
  128. *
  129. * @param PHPExcel $parent
  130. * @return PHPExcel_Style_Alignment
  131. */
  132. public function bindParent($parent)
  133. {
  134. $this->_parent = $parent;
  135. return $this;
  136. }
  137. /**
  138. * Is this a supervisor or a real style component?
  139. *
  140. * @return boolean
  141. */
  142. public function getIsSupervisor()
  143. {
  144. return $this->_isSupervisor;
  145. }
  146. /**
  147. * Get the shared style component for the currently active cell in currently active sheet.
  148. * Only used for style supervisor
  149. *
  150. * @return PHPExcel_Style_Alignment
  151. */
  152. public function getSharedComponent()
  153. {
  154. return $this->_parent->getSharedComponent()->getAlignment();
  155. }
  156. /**
  157. * Get the currently active sheet. Only used for supervisor
  158. *
  159. * @return PHPExcel_Worksheet
  160. */
  161. public function getActiveSheet()
  162. {
  163. return $this->_parent->getActiveSheet();
  164. }
  165. /**
  166. * Get the currently active cell coordinate in currently active sheet.
  167. * Only used for supervisor
  168. *
  169. * @return string E.g. 'A1'
  170. */
  171. public function getSelectedCells()
  172. {
  173. return $this->getActiveSheet()->getSelectedCells();
  174. }
  175. /**
  176. * Get the currently active cell coordinate in currently active sheet.
  177. * Only used for supervisor
  178. *
  179. * @return string E.g. 'A1'
  180. */
  181. public function getActiveCell()
  182. {
  183. return $this->getActiveSheet()->getActiveCell();
  184. }
  185. /**
  186. * Build style array from subcomponents
  187. *
  188. * @param array $array
  189. * @return array
  190. */
  191. public function getStyleArray($array)
  192. {
  193. return array('alignment' => $array);
  194. }
  195. /**
  196. * Apply styles from array
  197. *
  198. * <code>
  199. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  200. * array(
  201. * 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  202. * 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  203. * 'rotation' => 0,
  204. * 'wrap' => true
  205. * )
  206. * );
  207. * </code>
  208. *
  209. * @param array $pStyles Array containing style information
  210. * @throws Exception
  211. * @return PHPExcel_Style_Alignment
  212. */
  213. public function applyFromArray($pStyles = null) {
  214. if (is_array($pStyles)) {
  215. if ($this->_isSupervisor) {
  216. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  217. } else {
  218. if (array_key_exists('horizontal', $pStyles)) {
  219. $this->setHorizontal($pStyles['horizontal']);
  220. }
  221. if (array_key_exists('vertical', $pStyles)) {
  222. $this->setVertical($pStyles['vertical']);
  223. }
  224. if (array_key_exists('rotation', $pStyles)) {
  225. $this->setTextRotation($pStyles['rotation']);
  226. }
  227. if (array_key_exists('wrap', $pStyles)) {
  228. $this->setWrapText($pStyles['wrap']);
  229. }
  230. if (array_key_exists('shrinkToFit', $pStyles)) {
  231. $this->setShrinkToFit($pStyles['shrinkToFit']);
  232. }
  233. if (array_key_exists('indent', $pStyles)) {
  234. $this->setIndent($pStyles['indent']);
  235. }
  236. }
  237. } else {
  238. throw new Exception("Invalid style array passed.");
  239. }
  240. return $this;
  241. }
  242. /**
  243. * Get Horizontal
  244. *
  245. * @return string
  246. */
  247. public function getHorizontal() {
  248. if ($this->_isSupervisor) {
  249. return $this->getSharedComponent()->getHorizontal();
  250. }
  251. return $this->_horizontal;
  252. }
  253. /**
  254. * Set Horizontal
  255. *
  256. * @param string $pValue
  257. * @return PHPExcel_Style_Alignment
  258. */
  259. public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL) {
  260. if ($pValue == '') {
  261. $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  262. }
  263. if ($this->_isSupervisor) {
  264. $styleArray = $this->getStyleArray(array('horizontal' => $pValue));
  265. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  266. }
  267. else {
  268. $this->_horizontal = $pValue;
  269. }
  270. return $this;
  271. }
  272. /**
  273. * Get Vertical
  274. *
  275. * @return string
  276. */
  277. public function getVertical() {
  278. if ($this->_isSupervisor) {
  279. return $this->getSharedComponent()->getVertical();
  280. }
  281. return $this->_vertical;
  282. }
  283. /**
  284. * Set Vertical
  285. *
  286. * @param string $pValue
  287. * @return PHPExcel_Style_Alignment
  288. */
  289. public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM) {
  290. if ($pValue == '') {
  291. $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  292. }
  293. if ($this->_isSupervisor) {
  294. $styleArray = $this->getStyleArray(array('vertical' => $pValue));
  295. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  296. } else {
  297. $this->_vertical = $pValue;
  298. }
  299. return $this;
  300. }
  301. /**
  302. * Get TextRotation
  303. *
  304. * @return int
  305. */
  306. public function getTextRotation() {
  307. if ($this->_isSupervisor) {
  308. return $this->getSharedComponent()->getTextRotation();
  309. }
  310. return $this->_textRotation;
  311. }
  312. /**
  313. * Set TextRotation
  314. *
  315. * @param int $pValue
  316. * @throws Exception
  317. * @return PHPExcel_Style_Alignment
  318. */
  319. public function setTextRotation($pValue = 0) {
  320. // Excel2007 value 255 => PHPExcel value -165
  321. if ($pValue == 255) {
  322. $pValue = -165;
  323. }
  324. // Set rotation
  325. if ( ($pValue >= -90 && $pValue <= 90) || $pValue == -165 ) {
  326. if ($this->_isSupervisor) {
  327. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  328. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  329. } else {
  330. $this->_textRotation = $pValue;
  331. }
  332. } else {
  333. throw new Exception("Text rotation should be a value between -90 and 90.");
  334. }
  335. return $this;
  336. }
  337. /**
  338. * Get Wrap Text
  339. *
  340. * @return boolean
  341. */
  342. public function getWrapText() {
  343. if ($this->_isSupervisor) {
  344. return $this->getSharedComponent()->getWrapText();
  345. }
  346. return $this->_wrapText;
  347. }
  348. /**
  349. * Set Wrap Text
  350. *
  351. * @param boolean $pValue
  352. * @return PHPExcel_Style_Alignment
  353. */
  354. public function setWrapText($pValue = false) {
  355. if ($pValue == '') {
  356. $pValue = false;
  357. }
  358. if ($this->_isSupervisor) {
  359. $styleArray = $this->getStyleArray(array('wrap' => $pValue));
  360. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  361. } else {
  362. $this->_wrapText = $pValue;
  363. }
  364. return $this;
  365. }
  366. /**
  367. * Get Shrink to fit
  368. *
  369. * @return boolean
  370. */
  371. public function getShrinkToFit() {
  372. if ($this->_isSupervisor) {
  373. return $this->getSharedComponent()->getShrinkToFit();
  374. }
  375. return $this->_shrinkToFit;
  376. }
  377. /**
  378. * Set Shrink to fit
  379. *
  380. * @param boolean $pValue
  381. * @return PHPExcel_Style_Alignment
  382. */
  383. public function setShrinkToFit($pValue = false) {
  384. if ($pValue == '') {
  385. $pValue = false;
  386. }
  387. if ($this->_isSupervisor) {
  388. $styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
  389. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  390. } else {
  391. $this->_shrinkToFit = $pValue;
  392. }
  393. return $this;
  394. }
  395. /**
  396. * Get indent
  397. *
  398. * @return int
  399. */
  400. public function getIndent() {
  401. if ($this->_isSupervisor) {
  402. return $this->getSharedComponent()->getIndent();
  403. }
  404. return $this->_indent;
  405. }
  406. /**
  407. * Set indent
  408. *
  409. * @param int $pValue
  410. * @return PHPExcel_Style_Alignment
  411. */
  412. public function setIndent($pValue = 0) {
  413. if ($pValue > 0) {
  414. if ($this->getHorizontal() != self::HORIZONTAL_GENERAL && $this->getHorizontal() != self::HORIZONTAL_LEFT && $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
  415. $pValue = 0; // indent not supported
  416. }
  417. }
  418. if ($this->_isSupervisor) {
  419. $styleArray = $this->getStyleArray(array('indent' => $pValue));
  420. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  421. } else {
  422. $this->_indent = $pValue;
  423. }
  424. return $this;
  425. }
  426. /**
  427. * Get hash code
  428. *
  429. * @return string Hash code
  430. */
  431. public function getHashCode() {
  432. if ($this->_isSupervisor) {
  433. return $this->getSharedComponent()->getHashCode();
  434. }
  435. return md5(
  436. $this->_horizontal
  437. . $this->_vertical
  438. . $this->_textRotation
  439. . ($this->_wrapText ? 't' : 'f')
  440. . ($this->_shrinkToFit ? 't' : 'f')
  441. . $this->_indent
  442. . __CLASS__
  443. );
  444. }
  445. /**
  446. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  447. */
  448. public function __clone() {
  449. $vars = get_object_vars($this);
  450. foreach ($vars as $key => $value) {
  451. if (is_object($value)) {
  452. $this->$key = clone $value;
  453. } else {
  454. $this->$key = $value;
  455. }
  456. }
  457. }
  458. }