Alignment.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /**
  3. * PHPExcel_Style_Alignment
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 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. class PHPExcel_Style_Alignment extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  28. {
  29. /* Horizontal alignment styles */
  30. const HORIZONTAL_GENERAL = 'general';
  31. const HORIZONTAL_LEFT = 'left';
  32. const HORIZONTAL_RIGHT = 'right';
  33. const HORIZONTAL_CENTER = 'center';
  34. const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
  35. const HORIZONTAL_JUSTIFY = 'justify';
  36. const HORIZONTAL_FILL = 'fill';
  37. const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only
  38. /* Vertical alignment styles */
  39. const VERTICAL_BOTTOM = 'bottom';
  40. const VERTICAL_TOP = 'top';
  41. const VERTICAL_CENTER = 'center';
  42. const VERTICAL_JUSTIFY = 'justify';
  43. const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only
  44. /* Read order */
  45. const READORDER_CONTEXT = 0;
  46. const READORDER_LTR = 1;
  47. const READORDER_RTL = 2;
  48. /**
  49. * Horizontal alignment
  50. *
  51. * @var string
  52. */
  53. protected $horizontal = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  54. /**
  55. * Vertical alignment
  56. *
  57. * @var string
  58. */
  59. protected $vertical = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  60. /**
  61. * Text rotation
  62. *
  63. * @var integer
  64. */
  65. protected $textRotation = 0;
  66. /**
  67. * Wrap text
  68. *
  69. * @var boolean
  70. */
  71. protected $wrapText = false;
  72. /**
  73. * Shrink to fit
  74. *
  75. * @var boolean
  76. */
  77. protected $shrinkToFit = false;
  78. /**
  79. * Indent - only possible with horizontal alignment left and right
  80. *
  81. * @var integer
  82. */
  83. protected $indent = 0;
  84. /**
  85. * Read order
  86. *
  87. * @var integer
  88. */
  89. protected $readorder = 0;
  90. /**
  91. * Create a new PHPExcel_Style_Alignment
  92. *
  93. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  94. * Leave this value at default unless you understand exactly what
  95. * its ramifications are
  96. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  97. * Leave this value at default unless you understand exactly what
  98. * its ramifications are
  99. */
  100. public function __construct($isSupervisor = false, $isConditional = false)
  101. {
  102. // Supervisor?
  103. parent::__construct($isSupervisor);
  104. if ($isConditional) {
  105. $this->horizontal = null;
  106. $this->vertical = null;
  107. $this->textRotation = null;
  108. }
  109. }
  110. /**
  111. * Get the shared style component for the currently active cell in currently active sheet.
  112. * Only used for style supervisor
  113. *
  114. * @return PHPExcel_Style_Alignment
  115. */
  116. public function getSharedComponent()
  117. {
  118. return $this->parent->getSharedComponent()->getAlignment();
  119. }
  120. /**
  121. * Build style array from subcomponents
  122. *
  123. * @param array $array
  124. * @return array
  125. */
  126. public function getStyleArray($array)
  127. {
  128. return array('alignment' => $array);
  129. }
  130. /**
  131. * Apply styles from array
  132. *
  133. * <code>
  134. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  135. * array(
  136. * 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  137. * 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  138. * 'rotation' => 0,
  139. * 'wrap' => TRUE
  140. * )
  141. * );
  142. * </code>
  143. *
  144. * @param array $pStyles Array containing style information
  145. * @throws PHPExcel_Exception
  146. * @return PHPExcel_Style_Alignment
  147. */
  148. public function applyFromArray($pStyles = null)
  149. {
  150. if (is_array($pStyles)) {
  151. if ($this->isSupervisor) {
  152. $this->getActiveSheet()->getStyle($this->getSelectedCells())
  153. ->applyFromArray($this->getStyleArray($pStyles));
  154. } else {
  155. if (isset($pStyles['horizontal'])) {
  156. $this->setHorizontal($pStyles['horizontal']);
  157. }
  158. if (isset($pStyles['vertical'])) {
  159. $this->setVertical($pStyles['vertical']);
  160. }
  161. if (isset($pStyles['rotation'])) {
  162. $this->setTextRotation($pStyles['rotation']);
  163. }
  164. if (isset($pStyles['wrap'])) {
  165. $this->setWrapText($pStyles['wrap']);
  166. }
  167. if (isset($pStyles['shrinkToFit'])) {
  168. $this->setShrinkToFit($pStyles['shrinkToFit']);
  169. }
  170. if (isset($pStyles['indent'])) {
  171. $this->setIndent($pStyles['indent']);
  172. }
  173. if (isset($pStyles['readorder'])) {
  174. $this->setReadorder($pStyles['readorder']);
  175. }
  176. }
  177. } else {
  178. throw new PHPExcel_Exception("Invalid style array passed.");
  179. }
  180. return $this;
  181. }
  182. /**
  183. * Get Horizontal
  184. *
  185. * @return string
  186. */
  187. public function getHorizontal()
  188. {
  189. if ($this->isSupervisor) {
  190. return $this->getSharedComponent()->getHorizontal();
  191. }
  192. return $this->horizontal;
  193. }
  194. /**
  195. * Set Horizontal
  196. *
  197. * @param string $pValue
  198. * @return PHPExcel_Style_Alignment
  199. */
  200. public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL)
  201. {
  202. if ($pValue == '') {
  203. $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  204. }
  205. if ($this->isSupervisor) {
  206. $styleArray = $this->getStyleArray(array('horizontal' => $pValue));
  207. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  208. } else {
  209. $this->horizontal = $pValue;
  210. }
  211. return $this;
  212. }
  213. /**
  214. * Get Vertical
  215. *
  216. * @return string
  217. */
  218. public function getVertical()
  219. {
  220. if ($this->isSupervisor) {
  221. return $this->getSharedComponent()->getVertical();
  222. }
  223. return $this->vertical;
  224. }
  225. /**
  226. * Set Vertical
  227. *
  228. * @param string $pValue
  229. * @return PHPExcel_Style_Alignment
  230. */
  231. public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM)
  232. {
  233. if ($pValue == '') {
  234. $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  235. }
  236. if ($this->isSupervisor) {
  237. $styleArray = $this->getStyleArray(array('vertical' => $pValue));
  238. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  239. } else {
  240. $this->vertical = $pValue;
  241. }
  242. return $this;
  243. }
  244. /**
  245. * Get TextRotation
  246. *
  247. * @return int
  248. */
  249. public function getTextRotation()
  250. {
  251. if ($this->isSupervisor) {
  252. return $this->getSharedComponent()->getTextRotation();
  253. }
  254. return $this->textRotation;
  255. }
  256. /**
  257. * Set TextRotation
  258. *
  259. * @param int $pValue
  260. * @throws PHPExcel_Exception
  261. * @return PHPExcel_Style_Alignment
  262. */
  263. public function setTextRotation($pValue = 0)
  264. {
  265. // Excel2007 value 255 => PHPExcel value -165
  266. if ($pValue == 255) {
  267. $pValue = -165;
  268. }
  269. // Set rotation
  270. if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
  271. if ($this->isSupervisor) {
  272. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  273. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  274. } else {
  275. $this->textRotation = $pValue;
  276. }
  277. } else {
  278. throw new PHPExcel_Exception("Text rotation should be a value between -90 and 90.");
  279. }
  280. return $this;
  281. }
  282. /**
  283. * Get Wrap Text
  284. *
  285. * @return boolean
  286. */
  287. public function getWrapText()
  288. {
  289. if ($this->isSupervisor) {
  290. return $this->getSharedComponent()->getWrapText();
  291. }
  292. return $this->wrapText;
  293. }
  294. /**
  295. * Set Wrap Text
  296. *
  297. * @param boolean $pValue
  298. * @return PHPExcel_Style_Alignment
  299. */
  300. public function setWrapText($pValue = false)
  301. {
  302. if ($pValue == '') {
  303. $pValue = false;
  304. }
  305. if ($this->isSupervisor) {
  306. $styleArray = $this->getStyleArray(array('wrap' => $pValue));
  307. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  308. } else {
  309. $this->wrapText = $pValue;
  310. }
  311. return $this;
  312. }
  313. /**
  314. * Get Shrink to fit
  315. *
  316. * @return boolean
  317. */
  318. public function getShrinkToFit()
  319. {
  320. if ($this->isSupervisor) {
  321. return $this->getSharedComponent()->getShrinkToFit();
  322. }
  323. return $this->shrinkToFit;
  324. }
  325. /**
  326. * Set Shrink to fit
  327. *
  328. * @param boolean $pValue
  329. * @return PHPExcel_Style_Alignment
  330. */
  331. public function setShrinkToFit($pValue = false)
  332. {
  333. if ($pValue == '') {
  334. $pValue = false;
  335. }
  336. if ($this->isSupervisor) {
  337. $styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
  338. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  339. } else {
  340. $this->shrinkToFit = $pValue;
  341. }
  342. return $this;
  343. }
  344. /**
  345. * Get indent
  346. *
  347. * @return int
  348. */
  349. public function getIndent()
  350. {
  351. if ($this->isSupervisor) {
  352. return $this->getSharedComponent()->getIndent();
  353. }
  354. return $this->indent;
  355. }
  356. /**
  357. * Set indent
  358. *
  359. * @param int $pValue
  360. * @return PHPExcel_Style_Alignment
  361. */
  362. public function setIndent($pValue = 0)
  363. {
  364. if ($pValue > 0) {
  365. if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
  366. $this->getHorizontal() != self::HORIZONTAL_LEFT &&
  367. $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
  368. $pValue = 0; // indent not supported
  369. }
  370. }
  371. if ($this->isSupervisor) {
  372. $styleArray = $this->getStyleArray(array('indent' => $pValue));
  373. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  374. } else {
  375. $this->indent = $pValue;
  376. }
  377. return $this;
  378. }
  379. /**
  380. * Get read order
  381. *
  382. * @return integer
  383. */
  384. public function getReadorder()
  385. {
  386. if ($this->isSupervisor) {
  387. return $this->getSharedComponent()->getReadorder();
  388. }
  389. return $this->readorder;
  390. }
  391. /**
  392. * Set read order
  393. *
  394. * @param int $pValue
  395. * @return PHPExcel_Style_Alignment
  396. */
  397. public function setReadorder($pValue = 0)
  398. {
  399. if ($pValue < 0 || $pValue > 2) {
  400. $pValue = 0;
  401. }
  402. if ($this->isSupervisor) {
  403. $styleArray = $this->getStyleArray(array('readorder' => $pValue));
  404. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  405. } else {
  406. $this->readorder = $pValue;
  407. }
  408. return $this;
  409. }
  410. /**
  411. * Get hash code
  412. *
  413. * @return string Hash code
  414. */
  415. public function getHashCode()
  416. {
  417. if ($this->isSupervisor) {
  418. return $this->getSharedComponent()->getHashCode();
  419. }
  420. return md5(
  421. $this->horizontal .
  422. $this->vertical .
  423. $this->textRotation .
  424. ($this->wrapText ? 't' : 'f') .
  425. ($this->shrinkToFit ? 't' : 'f') .
  426. $this->indent .
  427. $this->readorder .
  428. __CLASS__
  429. );
  430. }
  431. }