File Source for Xf.php

PHPExcel_Writer_Excel5

# Source for file Xf.php

Documentation is available at Xf.php

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

Documentation generated on Sun, 27 Feb 2011 16:37:29 -0800 by phpDocumentor 1.4.3