PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/phpexcel/PHPExcel/Style/Alignment.php

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