PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/wi6857-memory/Classes/PHPExcel/Style/Alignment.php

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