Alignment.php 9.9 KB

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