Xf.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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_Writer_Excel5
  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. // Original file header of PEAR::Spreadsheet_Excel_Writer_Format (used as the base for this class):
  28. // -----------------------------------------------------------------------------------------
  29. // /*
  30. // * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
  31. // *
  32. // * The majority of this is _NOT_ my code. I simply ported it from the
  33. // * PERL Spreadsheet::WriteExcel module.
  34. // *
  35. // * The author of the Spreadsheet::WriteExcel module is John McNamara
  36. // * <jmcnamara@cpan.org>
  37. // *
  38. // * I _DO_ maintain this code, and John McNamara has nothing to do with the
  39. // * porting of this code to PHP. Any questions directly related to this
  40. // * class library should be directed to me.
  41. // *
  42. // * License Information:
  43. // *
  44. // * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
  45. // * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
  46. // *
  47. // * This library is free software; you can redistribute it and/or
  48. // * modify it under the terms of the GNU Lesser General Public
  49. // * License as published by the Free Software Foundation; either
  50. // * version 2.1 of the License, or (at your option) any later version.
  51. // *
  52. // * This library is distributed in the hope that it will be useful,
  53. // * but WITHOUT ANY WARRANTY; without even the implied warranty of
  54. // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  55. // * Lesser General Public License for more details.
  56. // *
  57. // * You should have received a copy of the GNU Lesser General Public
  58. // * License along with this library; if not, write to the Free Software
  59. // * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  60. // */
  61. /**
  62. * PHPExcel_Writer_Excel5_Xf
  63. *
  64. * @category PHPExcel
  65. * @package PHPExcel_Writer_Excel5
  66. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  67. */
  68. class PHPExcel_Writer_Excel5_Xf
  69. {
  70. /**
  71. * BIFF version
  72. *
  73. * @var int
  74. */
  75. private $_BIFFVersion;
  76. /**
  77. * Style XF or a cell XF ?
  78. *
  79. * @var boolean
  80. */
  81. private $_isStyleXf;
  82. /**
  83. * Index to the FONT record. Index 4 does not exist
  84. * @var integer
  85. */
  86. private $_fontIndex;
  87. /**
  88. * An index (2 bytes) to a FORMAT record (number format).
  89. * @var integer
  90. */
  91. public $_numberFormatIndex;
  92. /**
  93. * 1 bit, apparently not used.
  94. * @var integer
  95. */
  96. public $_text_justlast;
  97. /**
  98. * The cell's foreground color.
  99. * @var integer
  100. */
  101. public $_fg_color;
  102. /**
  103. * The cell's background color.
  104. * @var integer
  105. */
  106. public $_bg_color;
  107. /**
  108. * Color of the bottom border of the cell.
  109. * @var integer
  110. */
  111. public $_bottom_color;
  112. /**
  113. * Color of the top border of the cell.
  114. * @var integer
  115. */
  116. public $_top_color;
  117. /**
  118. * Color of the left border of the cell.
  119. * @var integer
  120. */
  121. public $_left_color;
  122. /**
  123. * Color of the right border of the cell.
  124. * @var integer
  125. */
  126. public $_right_color;
  127. /**
  128. * Constructor
  129. *
  130. * @access private
  131. * @param integer $index the XF index for the format.
  132. * @param PHPExcel_Style
  133. */
  134. public function __construct(PHPExcel_Style $style = null)
  135. {
  136. $this->_isStyleXf = false;
  137. $this->_BIFFVersion = 0x0600;
  138. $this->_fontIndex = 0;
  139. $this->_numberFormatIndex = 0;
  140. $this->_text_justlast = 0;
  141. $this->_fg_color = 0x40;
  142. $this->_bg_color = 0x41;
  143. $this->_diag = 0;
  144. $this->_bottom_color = 0x40;
  145. $this->_top_color = 0x40;
  146. $this->_left_color = 0x40;
  147. $this->_right_color = 0x40;
  148. $this->_diag_color = 0x40;
  149. $this->_style = $style;
  150. }
  151. /**
  152. * Generate an Excel BIFF XF record (style or cell).
  153. *
  154. * @param string $style The type of the XF record ('style' or 'cell').
  155. * @return string The XF record
  156. */
  157. function writeXf()
  158. {
  159. // Set the type of the XF record and some of the attributes.
  160. if ($this->_isStyleXf) {
  161. $style = 0xFFF5;
  162. } else {
  163. $style = $this->_mapLocked($this->_style->getProtection()->getLocked());
  164. $style |= $this->_mapHidden($this->_style->getProtection()->getHidden()) << 1;
  165. }
  166. // Flags to indicate if attributes have been set.
  167. $atr_num = ($this->_numberFormatIndex != 0)?1:0;
  168. $atr_fnt = ($this->_fontIndex != 0)?1:0;
  169. $atr_alc = ((int) $this->_style->getAlignment()->getWrapText())?1:0;
  170. $atr_bdr = ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) ||
  171. $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
  172. $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
  173. $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0;
  174. $atr_pat = (($this->_fg_color != 0x40) ||
  175. ($this->_bg_color != 0x41) ||
  176. $this->_mapFillType($this->_style->getFill()->getFillType()))?1:0;
  177. $atr_prot = $this->_mapLocked($this->_style->getProtection()->getLocked())
  178. | $this->_mapHidden($this->_style->getProtection()->getHidden());
  179. // Zero the default border colour if the border has not been set.
  180. if ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) {
  181. $this->_bottom_color = 0;
  182. }
  183. if ($this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) {
  184. $this->_top_color = 0;
  185. }
  186. if ($this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) {
  187. $this->_right_color = 0;
  188. }
  189. if ($this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) {
  190. $this->_left_color = 0;
  191. }
  192. if ($this->_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) == 0) {
  193. $this->_diag_color = 0;
  194. }
  195. $record = 0x00E0; // Record identifier
  196. if ($this->_BIFFVersion == 0x0500) {
  197. $length = 0x0010; // Number of bytes to follow
  198. }
  199. if ($this->_BIFFVersion == 0x0600) {
  200. $length = 0x0014;
  201. }
  202. $ifnt = $this->_fontIndex; // Index to FONT record
  203. $ifmt = $this->_numberFormatIndex; // Index to FORMAT record
  204. if ($this->_BIFFVersion == 0x0500) {
  205. $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
  206. $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
  207. $align |= $this->_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
  208. $align |= $this->_text_justlast << 7;
  209. $align |= 0 << 8; // rotation
  210. $align |= $atr_num << 10;
  211. $align |= $atr_fnt << 11;
  212. $align |= $atr_alc << 12;
  213. $align |= $atr_bdr << 13;
  214. $align |= $atr_pat << 14;
  215. $align |= $atr_prot << 15;
  216. $icv = $this->_fg_color; // fg and bg pattern colors
  217. $icv |= $this->_bg_color << 7;
  218. $fill = $this->_mapFillType($this->_style->getFill()->getFillType()); // Fill and border line style
  219. $fill |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 6;
  220. $fill |= $this->_bottom_color << 9;
  221. $border1 = $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()); // Border line style and color
  222. $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) << 3;
  223. $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 6;
  224. $border1 |= $this->_top_color << 9;
  225. $border2 = $this->_left_color; // Border color
  226. $border2 |= $this->_right_color << 7;
  227. $header = pack("vv", $record, $length);
  228. $data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
  229. $icv, $fill,
  230. $border1, $border2);
  231. } elseif ($this->_BIFFVersion == 0x0600) {
  232. $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
  233. $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
  234. $align |= $this->_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
  235. $align |= $this->_text_justlast << 7;
  236. $used_attrib = $atr_num << 2;
  237. $used_attrib |= $atr_fnt << 3;
  238. $used_attrib |= $atr_alc << 4;
  239. $used_attrib |= $atr_bdr << 5;
  240. $used_attrib |= $atr_pat << 6;
  241. $used_attrib |= $atr_prot << 7;
  242. $icv = $this->_fg_color; // fg and bg pattern colors
  243. $icv |= $this->_bg_color << 7;
  244. $border1 = $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color
  245. $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4;
  246. $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8;
  247. $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12;
  248. $border1 |= $this->_left_color << 16;
  249. $border1 |= $this->_right_color << 23;
  250. $diagonalDirection = $this->_style->getBorders()->getDiagonalDirection();
  251. $diag_tl_to_rb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
  252. || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_DOWN;
  253. $diag_tr_to_lb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
  254. || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_UP;
  255. $border1 |= $diag_tl_to_rb << 30;
  256. $border1 |= $diag_tr_to_lb << 31;
  257. $border2 = $this->_top_color; // Border color
  258. $border2 |= $this->_bottom_color << 7;
  259. $border2 |= $this->_diag_color << 14;
  260. $border2 |= $this->_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21;
  261. $border2 |= $this->_mapFillType($this->_style->getFill()->getFillType()) << 26;
  262. $header = pack("vv", $record, $length);
  263. //BIFF8 options: identation, shrinkToFit and text direction
  264. $biff8_options = $this->_style->getAlignment()->getIndent();
  265. $biff8_options |= (int) $this->_style->getAlignment()->getShrinkToFit() << 4;
  266. $data = pack("vvvC", $ifnt, $ifmt, $style, $align);
  267. $data .= pack("CCC"
  268. , $this->_mapTextRotation($this->_style->getAlignment()->getTextRotation())
  269. , $biff8_options
  270. , $used_attrib
  271. );
  272. $data .= pack("VVv", $border1, $border2, $icv);
  273. }
  274. return($header . $data);
  275. }
  276. /**
  277. * Set BIFF version
  278. *
  279. * @param int $BIFFVersion
  280. */
  281. public function setBIFFVersion($BIFFVersion)
  282. {
  283. $this->_BIFFVersion = $BIFFVersion;
  284. }
  285. /**
  286. * Is this a style XF ?
  287. *
  288. * @param boolean $value
  289. */
  290. public function setIsStyleXf($value)
  291. {
  292. $this->_isStyleXf = $value;
  293. }
  294. /**
  295. * Sets the cell's bottom border color
  296. *
  297. * @access public
  298. * @param int $colorIndex Color index
  299. */
  300. function setBottomColor($colorIndex)
  301. {
  302. $this->_bottom_color = $colorIndex;
  303. }
  304. /**
  305. * Sets the cell's top border color
  306. *
  307. * @access public
  308. * @param int $colorIndex Color index
  309. */
  310. function setTopColor($colorIndex)
  311. {
  312. $this->_top_color = $colorIndex;
  313. }
  314. /**
  315. * Sets the cell's left border color
  316. *
  317. * @access public
  318. * @param int $colorIndex Color index
  319. */
  320. function setLeftColor($colorIndex)
  321. {
  322. $this->_left_color = $colorIndex;
  323. }
  324. /**
  325. * Sets the cell's right border color
  326. *
  327. * @access public
  328. * @param int $colorIndex Color index
  329. */
  330. function setRightColor($colorIndex)
  331. {
  332. $this->_right_color = $colorIndex;
  333. }
  334. /**
  335. * Sets the cell's diagonal border color
  336. *
  337. * @access public
  338. * @param int $colorIndex Color index
  339. */
  340. function setDiagColor($colorIndex)
  341. {
  342. $this->_diag_color = $colorIndex;
  343. }
  344. /**
  345. * Sets the cell's foreground color
  346. *
  347. * @access public
  348. * @param int $colorIndex Color index
  349. */
  350. function setFgColor($colorIndex)
  351. {
  352. $this->_fg_color = $colorIndex;
  353. }
  354. /**
  355. * Sets the cell's background color
  356. *
  357. * @access public
  358. * @param int $colorIndex Color index
  359. */
  360. function setBgColor($colorIndex)
  361. {
  362. $this->_bg_color = $colorIndex;
  363. }
  364. /**
  365. * Sets the index to the number format record
  366. * It can be date, time, currency, etc...
  367. *
  368. * @access public
  369. * @param integer $numberFormatIndex Index to format record
  370. */
  371. function setNumberFormatIndex($numberFormatIndex)
  372. {
  373. $this->_numberFormatIndex = $numberFormatIndex;
  374. }
  375. /**
  376. * Set the font index.
  377. *
  378. * @param int $value Font index, note that value 4 does not exist
  379. */
  380. public function setFontIndex($value)
  381. {
  382. $this->_fontIndex = $value;
  383. }
  384. /**
  385. * Map border style
  386. */
  387. private function _mapBorderStyle($borderStyle) {
  388. switch ($borderStyle) {
  389. case PHPExcel_Style_Border::BORDER_NONE: return 0x00;
  390. case PHPExcel_Style_Border::BORDER_THIN; return 0x01;
  391. case PHPExcel_Style_Border::BORDER_MEDIUM; return 0x02;
  392. case PHPExcel_Style_Border::BORDER_DASHED; return 0x03;
  393. case PHPExcel_Style_Border::BORDER_DOTTED; return 0x04;
  394. case PHPExcel_Style_Border::BORDER_THICK; return 0x05;
  395. case PHPExcel_Style_Border::BORDER_DOUBLE; return 0x06;
  396. case PHPExcel_Style_Border::BORDER_HAIR; return 0x07;
  397. case PHPExcel_Style_Border::BORDER_MEDIUMDASHED; return 0x08;
  398. case PHPExcel_Style_Border::BORDER_DASHDOT; return 0x09;
  399. case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT; return 0x0A;
  400. case PHPExcel_Style_Border::BORDER_DASHDOTDOT; return 0x0B;
  401. case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT; return 0x0C;
  402. case PHPExcel_Style_Border::BORDER_SLANTDASHDOT; return 0x0D;
  403. default: return 0x00;
  404. }
  405. }
  406. /**
  407. * Map fill type
  408. */
  409. private function _mapFillType($fillType) {
  410. switch ($fillType) {
  411. case PHPExcel_Style_Fill::FILL_NONE: return 0x00;
  412. case PHPExcel_Style_Fill::FILL_SOLID: return 0x01;
  413. case PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY: return 0x02;
  414. case PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY: return 0x03;
  415. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY: return 0x04;
  416. case PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL: return 0x05;
  417. case PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL: return 0x06;
  418. case PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN: return 0x07;
  419. case PHPExcel_Style_Fill::FILL_PATTERN_DARKUP: return 0x08;
  420. case PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID: return 0x09;
  421. case PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS: return 0x0A;
  422. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL: return 0x0B;
  423. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL: return 0x0C;
  424. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN: return 0x0D;
  425. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP: return 0x0E;
  426. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID: return 0x0F;
  427. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS: return 0x10;
  428. case PHPExcel_Style_Fill::FILL_PATTERN_GRAY125: return 0x11;
  429. case PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625: return 0x12;
  430. case PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR: // does not exist in BIFF8
  431. case PHPExcel_Style_Fill::FILL_GRADIENT_PATH: // does not exist in BIFF8
  432. default: return 0x00;
  433. }
  434. }
  435. /**
  436. * Map to BIFF2-BIFF8 codes for horizontal alignment
  437. *
  438. * @param string $hAlign
  439. * @return int
  440. */
  441. private function _mapHAlign($hAlign)
  442. {
  443. switch ($hAlign) {
  444. case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL: return 0;
  445. case PHPExcel_Style_Alignment::HORIZONTAL_LEFT: return 1;
  446. case PHPExcel_Style_Alignment::HORIZONTAL_CENTER: return 2;
  447. case PHPExcel_Style_Alignment::HORIZONTAL_RIGHT: return 3;
  448. case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY: return 5;
  449. case PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS: return 6;
  450. default: return 0;
  451. }
  452. }
  453. /**
  454. * Map to BIFF2-BIFF8 codes for vertical alignment
  455. *
  456. * @param string $vAlign
  457. * @return int
  458. */
  459. private function _mapVAlign($vAlign) {
  460. switch ($vAlign) {
  461. case PHPExcel_Style_Alignment::VERTICAL_TOP: return 0;
  462. case PHPExcel_Style_Alignment::VERTICAL_CENTER: return 1;
  463. case PHPExcel_Style_Alignment::VERTICAL_BOTTOM: return 2;
  464. case PHPExcel_Style_Alignment::VERTICAL_JUSTIFY: return 3;
  465. default: return 2;
  466. }
  467. }
  468. /**
  469. * Map to BIFF8 codes for text rotation angle
  470. *
  471. * @param int $textRotation
  472. * @return int
  473. */
  474. private function _mapTextRotation($textRotation) {
  475. if ($textRotation >= 0) {
  476. return $textRotation;
  477. }
  478. if ($textRotation == -165) {
  479. return 255;
  480. }
  481. if ($textRotation < 0) {
  482. return 90 - $textRotation;
  483. }
  484. }
  485. /**
  486. * Map locked
  487. *
  488. * @param string
  489. * @return int
  490. */
  491. private function _mapLocked($locked) {
  492. switch ($locked) {
  493. case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 1;
  494. case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1;
  495. case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED: return 0;
  496. default: return 1;
  497. }
  498. }
  499. /**
  500. * Map hidden
  501. *
  502. * @param string
  503. * @return int
  504. */
  505. private function _mapHidden($hidden) {
  506. switch ($hidden) {
  507. case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 0;
  508. case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1;
  509. case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED: return 0;
  510. default: return 0;
  511. }
  512. }
  513. }