Alignment.php 9.7 KB

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