Xf.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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_Writer_Excel5
  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. // 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  67. */
  68. class PHPExcel_Writer_Excel5_Xf
  69. {
  70. /**
  71. * Style XF or a cell XF ?
  72. *
  73. * @var boolean
  74. */
  75. private $_isStyleXf;
  76. /**
  77. * Index to the FONT record. Index 4 does not exist
  78. * @var integer
  79. */
  80. private $_fontIndex;
  81. /**
  82. * An index (2 bytes) to a FORMAT record (number format).
  83. * @var integer
  84. */
  85. public $_numberFormatIndex;
  86. /**
  87. * 1 bit, apparently not used.
  88. * @var integer
  89. */
  90. public $_text_justlast;
  91. /**
  92. * The cell's foreground color.
  93. * @var integer
  94. */
  95. public $_fg_color;
  96. /**
  97. * The cell's background color.
  98. * @var integer
  99. */
  100. public $_bg_color;
  101. /**
  102. * Color of the bottom border of the cell.
  103. * @var integer
  104. */
  105. public $_bottom_color;
  106. /**
  107. * Color of the top border of the cell.
  108. * @var integer
  109. */
  110. public $_top_color;
  111. /**
  112. * Color of the left border of the cell.
  113. * @var integer
  114. */
  115. public $_left_color;
  116. /**
  117. * Color of the right border of the cell.
  118. * @var integer
  119. */
  120. public $_right_color;
  121. /**
  122. * Constructor
  123. *
  124. * @access public
  125. * @param PHPExcel_Style The XF format
  126. */
  127. public function __construct(PHPExcel_Style $style = null)
  128. {
  129. $this->_isStyleXf = false;
  130. $this->_fontIndex = 0;
  131. $this->_numberFormatIndex = 0;
  132. $this->_text_justlast = 0;
  133. $this->_fg_color = 0x40;
  134. $this->_bg_color = 0x41;
  135. $this->_diag = 0;
  136. $this->_bottom_color = 0x40;
  137. $this->_top_color = 0x40;
  138. $this->_left_color = 0x40;
  139. $this->_right_color = 0x40;
  140. $this->_diag_color = 0x40;
  141. $this->_style = $style;
  142. }
  143. /**
  144. * Generate an Excel BIFF XF record (style or cell).
  145. *
  146. * @return string The XF record
  147. */
  148. function writeXf()
  149. {
  150. // Set the type of the XF record and some of the attributes.
  151. if ($this->_isStyleXf) {
  152. $style = 0xFFF5;
  153. } else {
  154. $style = self::_mapLocked($this->_style->getProtection()->getLocked());
  155. $style |= self::_mapHidden($this->_style->getProtection()->getHidden()) << 1;
  156. }
  157. // Flags to indicate if attributes have been set.
  158. $atr_num = ($this->_numberFormatIndex != 0)?1:0;
  159. $atr_fnt = ($this->_fontIndex != 0)?1:0;
  160. $atr_alc = ((int) $this->_style->getAlignment()->getWrapText()) ? 1 : 0;
  161. $atr_bdr = (self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) ||
  162. self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
  163. self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
  164. self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0;
  165. $atr_pat = (($this->_fg_color != 0x40) ||
  166. ($this->_bg_color != 0x41) ||
  167. self::_mapFillType($this->_style->getFill()->getFillType()))?1:0;
  168. $atr_prot = self::_mapLocked($this->_style->getProtection()->getLocked())
  169. | self::_mapHidden($this->_style->getProtection()->getHidden());
  170. // Zero the default border colour if the border has not been set.
  171. if (self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) {
  172. $this->_bottom_color = 0;
  173. }
  174. if (self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) {
  175. $this->_top_color = 0;
  176. }
  177. if (self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) {
  178. $this->_right_color = 0;
  179. }
  180. if (self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) {
  181. $this->_left_color = 0;
  182. }
  183. if (self::_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) == 0) {
  184. $this->_diag_color = 0;
  185. }
  186. $record = 0x00E0; // Record identifier
  187. $length = 0x0014; // Number of bytes to follow
  188. $ifnt = $this->_fontIndex; // Index to FONT record
  189. $ifmt = $this->_numberFormatIndex; // Index to FORMAT record
  190. $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
  191. $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
  192. $align |= self::_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
  193. $align |= $this->_text_justlast << 7;
  194. $used_attrib = $atr_num << 2;
  195. $used_attrib |= $atr_fnt << 3;
  196. $used_attrib |= $atr_alc << 4;
  197. $used_attrib |= $atr_bdr << 5;
  198. $used_attrib |= $atr_pat << 6;
  199. $used_attrib |= $atr_prot << 7;
  200. $icv = $this->_fg_color; // fg and bg pattern colors
  201. $icv |= $this->_bg_color << 7;
  202. $border1 = self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color
  203. $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4;
  204. $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8;
  205. $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12;
  206. $border1 |= $this->_left_color << 16;
  207. $border1 |= $this->_right_color << 23;
  208. $diagonalDirection = $this->_style->getBorders()->getDiagonalDirection();
  209. $diag_tl_to_rb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
  210. || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_DOWN;
  211. $diag_tr_to_lb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
  212. || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_UP;
  213. $border1 |= $diag_tl_to_rb << 30;
  214. $border1 |= $diag_tr_to_lb << 31;
  215. $border2 = $this->_top_color; // Border color
  216. $border2 |= $this->_bottom_color << 7;
  217. $border2 |= $this->_diag_color << 14;
  218. $border2 |= self::_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21;
  219. $border2 |= self::_mapFillType($this->_style->getFill()->getFillType()) << 26;
  220. $header = pack("vv", $record, $length);
  221. //BIFF8 options: identation, shrinkToFit and text direction
  222. $biff8_options = $this->_style->getAlignment()->getIndent();
  223. $biff8_options |= (int) $this->_style->getAlignment()->getShrinkToFit() << 4;
  224. $data = pack("vvvC", $ifnt, $ifmt, $style, $align);
  225. $data .= pack("CCC"
  226. , self::_mapTextRotation($this->_style->getAlignment()->getTextRotation())
  227. , $biff8_options
  228. , $used_attrib
  229. );
  230. $data .= pack("VVv", $border1, $border2, $icv);
  231. return($header . $data);
  232. }
  233. /**
  234. * Is this a style XF ?
  235. *
  236. * @param boolean $value
  237. */
  238. public function setIsStyleXf($value)
  239. {
  240. $this->_isStyleXf = $value;
  241. }
  242. /**
  243. * Sets the cell's bottom border color
  244. *
  245. * @access public
  246. * @param int $colorIndex Color index
  247. */
  248. function setBottomColor($colorIndex)
  249. {
  250. $this->_bottom_color = $colorIndex;
  251. }
  252. /**
  253. * Sets the cell's top border color
  254. *
  255. * @access public
  256. * @param int $colorIndex Color index
  257. */
  258. function setTopColor($colorIndex)
  259. {
  260. $this->_top_color = $colorIndex;
  261. }
  262. /**
  263. * Sets the cell's left border color
  264. *
  265. * @access public
  266. * @param int $colorIndex Color index
  267. */
  268. function setLeftColor($colorIndex)
  269. {
  270. $this->_left_color = $colorIndex;
  271. }
  272. /**
  273. * Sets the cell's right border color
  274. *
  275. * @access public
  276. * @param int $colorIndex Color index
  277. */
  278. function setRightColor($colorIndex)
  279. {
  280. $this->_right_color = $colorIndex;
  281. }
  282. /**
  283. * Sets the cell's diagonal border color
  284. *
  285. * @access public
  286. * @param int $colorIndex Color index
  287. */
  288. function setDiagColor($colorIndex)
  289. {
  290. $this->_diag_color = $colorIndex;
  291. }
  292. /**
  293. * Sets the cell's foreground color
  294. *
  295. * @access public
  296. * @param int $colorIndex Color index
  297. */
  298. function setFgColor($colorIndex)
  299. {
  300. $this->_fg_color = $colorIndex;
  301. }
  302. /**
  303. * Sets the cell's background color
  304. *
  305. * @access public
  306. * @param int $colorIndex Color index
  307. */
  308. function setBgColor($colorIndex)
  309. {
  310. $this->_bg_color = $colorIndex;
  311. }
  312. /**
  313. * Sets the index to the number format record
  314. * It can be date, time, currency, etc...
  315. *
  316. * @access public
  317. * @param integer $numberFormatIndex Index to format record
  318. */
  319. function setNumberFormatIndex($numberFormatIndex)
  320. {
  321. $this->_numberFormatIndex = $numberFormatIndex;
  322. }
  323. /**
  324. * Set the font index.
  325. *
  326. * @param int $value Font index, note that value 4 does not exist
  327. */
  328. public function setFontIndex($value)
  329. {
  330. $this->_fontIndex = $value;
  331. }
  332. /**
  333. * Map of BIFF2-BIFF8 codes for border styles
  334. * @static array of int
  335. *
  336. */
  337. private static $_mapBorderStyle = array ( PHPExcel_Style_Border::BORDER_NONE => 0x00,
  338. PHPExcel_Style_Border::BORDER_THIN => 0x01,
  339. PHPExcel_Style_Border::BORDER_MEDIUM => 0x02,
  340. PHPExcel_Style_Border::BORDER_DASHED => 0x03,
  341. PHPExcel_Style_Border::BORDER_DOTTED => 0x04,
  342. PHPExcel_Style_Border::BORDER_THICK => 0x05,
  343. PHPExcel_Style_Border::BORDER_DOUBLE => 0x06,
  344. PHPExcel_Style_Border::BORDER_HAIR => 0x07,
  345. PHPExcel_Style_Border::BORDER_MEDIUMDASHED => 0x08,
  346. PHPExcel_Style_Border::BORDER_DASHDOT => 0x09,
  347. PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT => 0x0A,
  348. PHPExcel_Style_Border::BORDER_DASHDOTDOT => 0x0B,
  349. PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT => 0x0C,
  350. PHPExcel_Style_Border::BORDER_SLANTDASHDOT => 0x0D,
  351. );
  352. /**
  353. * Map border style
  354. *
  355. * @param string $borderStyle
  356. * @return int
  357. */
  358. private static function _mapBorderStyle($borderStyle) {
  359. if (isset(self::$_mapBorderStyle[$borderStyle]))
  360. return self::$_mapBorderStyle[$borderStyle];
  361. return 0x00;
  362. }
  363. /**
  364. * Map of BIFF2-BIFF8 codes for fill types
  365. * @static array of int
  366. *
  367. */
  368. private static $_mapFillType = array( PHPExcel_Style_Fill::FILL_NONE => 0x00,
  369. PHPExcel_Style_Fill::FILL_SOLID => 0x01,
  370. PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY => 0x02,
  371. PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY => 0x03,
  372. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY => 0x04,
  373. PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL => 0x05,
  374. PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL => 0x06,
  375. PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN => 0x07,
  376. PHPExcel_Style_Fill::FILL_PATTERN_DARKUP => 0x08,
  377. PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID => 0x09,
  378. PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS => 0x0A,
  379. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL => 0x0B,
  380. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL => 0x0C,
  381. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN => 0x0D,
  382. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP => 0x0E,
  383. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID => 0x0F,
  384. PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS => 0x10,
  385. PHPExcel_Style_Fill::FILL_PATTERN_GRAY125 => 0x11,
  386. PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625 => 0x12,
  387. PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR => 0x00, // does not exist in BIFF8
  388. PHPExcel_Style_Fill::FILL_GRADIENT_PATH => 0x00, // does not exist in BIFF8
  389. );
  390. /**
  391. * Map fill type
  392. *
  393. * @param string $fillType
  394. * @return int
  395. */
  396. private static function _mapFillType($fillType) {
  397. if (isset(self::$_mapFillType[$fillType]))
  398. return self::$_mapFillType[$fillType];
  399. return 0x00;
  400. }
  401. /**
  402. * Map of BIFF2-BIFF8 codes for horizontal alignment
  403. * @static array of int
  404. *
  405. */
  406. private static $_mapHAlign = array( PHPExcel_Style_Alignment::HORIZONTAL_GENERAL => 0,
  407. PHPExcel_Style_Alignment::HORIZONTAL_LEFT => 1,
  408. PHPExcel_Style_Alignment::HORIZONTAL_CENTER => 2,
  409. PHPExcel_Style_Alignment::HORIZONTAL_RIGHT => 3,
  410. PHPExcel_Style_Alignment::HORIZONTAL_FILL => 4,
  411. PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY => 5,
  412. PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS => 6,
  413. );
  414. /**
  415. * Map to BIFF2-BIFF8 codes for horizontal alignment
  416. *
  417. * @param string $hAlign
  418. * @return int
  419. */
  420. private function _mapHAlign($hAlign)
  421. {
  422. if (isset(self::$_mapHAlign[$hAlign]))
  423. return self::$_mapHAlign[$hAlign];
  424. return 0;
  425. }
  426. /**
  427. * Map of BIFF2-BIFF8 codes for vertical alignment
  428. * @static array of int
  429. *
  430. */
  431. private static $_mapVAlign = array( PHPExcel_Style_Alignment::VERTICAL_TOP => 0,
  432. PHPExcel_Style_Alignment::VERTICAL_CENTER => 1,
  433. PHPExcel_Style_Alignment::VERTICAL_BOTTOM => 2,
  434. PHPExcel_Style_Alignment::VERTICAL_JUSTIFY => 3,
  435. );
  436. /**
  437. * Map to BIFF2-BIFF8 codes for vertical alignment
  438. *
  439. * @param string $vAlign
  440. * @return int
  441. */
  442. private static function _mapVAlign($vAlign) {
  443. if (isset(self::$_mapVAlign[$vAlign]))
  444. return self::$_mapVAlign[$vAlign];
  445. return 2;
  446. }
  447. /**
  448. * Map to BIFF8 codes for text rotation angle
  449. *
  450. * @param int $textRotation
  451. * @return int
  452. */
  453. private static function _mapTextRotation($textRotation) {
  454. if ($textRotation >= 0) {
  455. return $textRotation;
  456. }
  457. if ($textRotation == -165) {
  458. return 255;
  459. }
  460. if ($textRotation < 0) {
  461. return 90 - $textRotation;
  462. }
  463. }
  464. /**
  465. * Map locked
  466. *
  467. * @param string
  468. * @return int
  469. */
  470. private static function _mapLocked($locked) {
  471. switch ($locked) {
  472. case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 1;
  473. case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1;
  474. case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED: return 0;
  475. default: return 1;
  476. }
  477. }
  478. /**
  479. * Map hidden
  480. *
  481. * @param string
  482. * @return int
  483. */
  484. private static function _mapHidden($hidden) {
  485. switch ($hidden) {
  486. case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 0;
  487. case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1;
  488. case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED: return 0;
  489. default: return 0;
  490. }
  491. }
  492. }