Xf.php 18 KB

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