3797a679285e10a828760705734040728f23a182.svn-base 11 KB

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