1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64: 65: 66: 67: 68: 69: 70:
71: class PHPExcel_Writer_Excel5_Xf
72: {
73: 74: 75: 76: 77:
78: private $_isStyleXf;
79:
80: 81: 82: 83:
84: private $_fontIndex;
85:
86: 87: 88: 89:
90: public $_numberFormatIndex;
91:
92: 93: 94: 95:
96: public $_text_justlast;
97:
98: 99: 100: 101:
102: public $_fg_color;
103:
104: 105: 106: 107:
108: public $_bg_color;
109:
110: 111: 112: 113:
114: public $_bottom_color;
115:
116: 117: 118: 119:
120: public $_top_color;
121:
122: 123: 124: 125:
126: public $_left_color;
127:
128: 129: 130: 131:
132: public $_right_color;
133:
134: 135: 136: 137: 138: 139:
140: public function __construct(PHPExcel_Style $style = null)
141: {
142: $this->_isStyleXf = false;
143: $this->_fontIndex = 0;
144:
145: $this->_numberFormatIndex = 0;
146:
147: $this->_text_justlast = 0;
148:
149: $this->_fg_color = 0x40;
150: $this->_bg_color = 0x41;
151:
152: $this->_diag = 0;
153:
154: $this->_bottom_color = 0x40;
155: $this->_top_color = 0x40;
156: $this->_left_color = 0x40;
157: $this->_right_color = 0x40;
158: $this->_diag_color = 0x40;
159: $this->_style = $style;
160:
161: }
162:
163:
164: 165: 166: 167: 168:
169: function writeXf()
170: {
171:
172: if ($this->_isStyleXf) {
173: $style = 0xFFF5;
174: } else {
175: $style = self::_mapLocked($this->_style->getProtection()->getLocked());
176: $style |= self::_mapHidden($this->_style->getProtection()->getHidden()) << 1;
177: }
178:
179:
180: $atr_num = ($this->_numberFormatIndex != 0)?1:0;
181: $atr_fnt = ($this->_fontIndex != 0)?1:0;
182: $atr_alc = ((int) $this->_style->getAlignment()->getWrapText()) ? 1 : 0;
183: $atr_bdr = (self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) ||
184: self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
185: self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
186: self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0;
187: $atr_pat = (($this->_fg_color != 0x40) ||
188: ($this->_bg_color != 0x41) ||
189: self::_mapFillType($this->_style->getFill()->getFillType()))?1:0;
190: $atr_prot = self::_mapLocked($this->_style->getProtection()->getLocked())
191: | self::_mapHidden($this->_style->getProtection()->getHidden());
192:
193:
194: if (self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) {
195: $this->_bottom_color = 0;
196: }
197: if (self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) {
198: $this->_top_color = 0;
199: }
200: if (self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) {
201: $this->_right_color = 0;
202: }
203: if (self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) {
204: $this->_left_color = 0;
205: }
206: if (self::_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) == 0) {
207: $this->_diag_color = 0;
208: }
209:
210: $record = 0x00E0;
211: $length = 0x0014;
212:
213: $ifnt = $this->_fontIndex;
214: $ifmt = $this->_numberFormatIndex;
215:
216: $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal());
217: $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
218: $align |= self::_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
219: $align |= $this->_text_justlast << 7;
220:
221: $used_attrib = $atr_num << 2;
222: $used_attrib |= $atr_fnt << 3;
223: $used_attrib |= $atr_alc << 4;
224: $used_attrib |= $atr_bdr << 5;
225: $used_attrib |= $atr_pat << 6;
226: $used_attrib |= $atr_prot << 7;
227:
228: $icv = $this->_fg_color;
229: $icv |= $this->_bg_color << 7;
230:
231: $border1 = self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle());
232: $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4;
233: $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8;
234: $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12;
235: $border1 |= $this->_left_color << 16;
236: $border1 |= $this->_right_color << 23;
237:
238: $diagonalDirection = $this->_style->getBorders()->getDiagonalDirection();
239: $diag_tl_to_rb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
240: || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_DOWN;
241: $diag_tr_to_lb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
242: || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_UP;
243: $border1 |= $diag_tl_to_rb << 30;
244: $border1 |= $diag_tr_to_lb << 31;
245:
246: $border2 = $this->_top_color;
247: $border2 |= $this->_bottom_color << 7;
248: $border2 |= $this->_diag_color << 14;
249: $border2 |= self::_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21;
250: $border2 |= self::_mapFillType($this->_style->getFill()->getFillType()) << 26;
251:
252: $header = pack("vv", $record, $length);
253:
254:
255: $biff8_options = $this->_style->getAlignment()->getIndent();
256: $biff8_options |= (int) $this->_style->getAlignment()->getShrinkToFit() << 4;
257:
258: $data = pack("vvvC", $ifnt, $ifmt, $style, $align);
259: $data .= pack("CCC"
260: , self::_mapTextRotation($this->_style->getAlignment()->getTextRotation())
261: , $biff8_options
262: , $used_attrib
263: );
264: $data .= pack("VVv", $border1, $border2, $icv);
265:
266: return($header . $data);
267: }
268:
269: 270: 271: 272: 273:
274: public function setIsStyleXf($value)
275: {
276: $this->_isStyleXf = $value;
277: }
278:
279: 280: 281: 282: 283: 284:
285: function setBottomColor($colorIndex)
286: {
287: $this->_bottom_color = $colorIndex;
288: }
289:
290: 291: 292: 293: 294: 295:
296: function setTopColor($colorIndex)
297: {
298: $this->_top_color = $colorIndex;
299: }
300:
301: 302: 303: 304: 305: 306:
307: function setLeftColor($colorIndex)
308: {
309: $this->_left_color = $colorIndex;
310: }
311:
312: 313: 314: 315: 316: 317:
318: function setRightColor($colorIndex)
319: {
320: $this->_right_color = $colorIndex;
321: }
322:
323: 324: 325: 326: 327: 328:
329: function setDiagColor($colorIndex)
330: {
331: $this->_diag_color = $colorIndex;
332: }
333:
334:
335: 336: 337: 338: 339: 340:
341: function setFgColor($colorIndex)
342: {
343: $this->_fg_color = $colorIndex;
344: }
345:
346: 347: 348: 349: 350: 351:
352: function setBgColor($colorIndex)
353: {
354: $this->_bg_color = $colorIndex;
355: }
356:
357: 358: 359: 360: 361: 362: 363:
364: function setNumberFormatIndex($numberFormatIndex)
365: {
366: $this->_numberFormatIndex = $numberFormatIndex;
367: }
368:
369: 370: 371: 372: 373:
374: public function setFontIndex($value)
375: {
376: $this->_fontIndex = $value;
377: }
378:
379: 380: 381: 382: 383:
384: private static $_mapBorderStyle = array ( PHPExcel_Style_Border::BORDER_NONE => 0x00,
385: PHPExcel_Style_Border::BORDER_THIN => 0x01,
386: PHPExcel_Style_Border::BORDER_MEDIUM => 0x02,
387: PHPExcel_Style_Border::BORDER_DASHED => 0x03,
388: PHPExcel_Style_Border::BORDER_DOTTED => 0x04,
389: PHPExcel_Style_Border::BORDER_THICK => 0x05,
390: PHPExcel_Style_Border::BORDER_DOUBLE => 0x06,
391: PHPExcel_Style_Border::BORDER_HAIR => 0x07,
392: PHPExcel_Style_Border::BORDER_MEDIUMDASHED => 0x08,
393: PHPExcel_Style_Border::BORDER_DASHDOT => 0x09,
394: PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT => 0x0A,
395: PHPExcel_Style_Border::BORDER_DASHDOTDOT => 0x0B,
396: PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT => 0x0C,
397: PHPExcel_Style_Border::BORDER_SLANTDASHDOT => 0x0D,
398: );
399:
400: 401: 402: 403: 404: 405:
406: private static function _mapBorderStyle($borderStyle) {
407: if (isset(self::$_mapBorderStyle[$borderStyle]))
408: return self::$_mapBorderStyle[$borderStyle];
409: return 0x00;
410: }
411:
412: 413: 414: 415: 416:
417: private static $_mapFillType = array( PHPExcel_Style_Fill::FILL_NONE => 0x00,
418: PHPExcel_Style_Fill::FILL_SOLID => 0x01,
419: PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY => 0x02,
420: PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY => 0x03,
421: PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY => 0x04,
422: PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL => 0x05,
423: PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL => 0x06,
424: PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN => 0x07,
425: PHPExcel_Style_Fill::FILL_PATTERN_DARKUP => 0x08,
426: PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID => 0x09,
427: PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS => 0x0A,
428: PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL => 0x0B,
429: PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL => 0x0C,
430: PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN => 0x0D,
431: PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP => 0x0E,
432: PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID => 0x0F,
433: PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS => 0x10,
434: PHPExcel_Style_Fill::FILL_PATTERN_GRAY125 => 0x11,
435: PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625 => 0x12,
436: PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR => 0x00,
437: PHPExcel_Style_Fill::FILL_GRADIENT_PATH => 0x00,
438: );
439: 440: 441: 442: 443: 444:
445: private static function _mapFillType($fillType) {
446: if (isset(self::$_mapFillType[$fillType]))
447: return self::$_mapFillType[$fillType];
448: return 0x00;
449: }
450:
451: 452: 453: 454: 455:
456: private static $_mapHAlign = array( PHPExcel_Style_Alignment::HORIZONTAL_GENERAL => 0,
457: PHPExcel_Style_Alignment::HORIZONTAL_LEFT => 1,
458: PHPExcel_Style_Alignment::HORIZONTAL_CENTER => 2,
459: PHPExcel_Style_Alignment::HORIZONTAL_RIGHT => 3,
460: PHPExcel_Style_Alignment::HORIZONTAL_FILL => 4,
461: PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY => 5,
462: PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS => 6,
463: );
464: 465: 466: 467: 468: 469:
470: private function _mapHAlign($hAlign)
471: {
472: if (isset(self::$_mapHAlign[$hAlign]))
473: return self::$_mapHAlign[$hAlign];
474: return 0;
475: }
476:
477: 478: 479: 480: 481:
482: private static $_mapVAlign = array( PHPExcel_Style_Alignment::VERTICAL_TOP => 0,
483: PHPExcel_Style_Alignment::VERTICAL_CENTER => 1,
484: PHPExcel_Style_Alignment::VERTICAL_BOTTOM => 2,
485: PHPExcel_Style_Alignment::VERTICAL_JUSTIFY => 3,
486: );
487: 488: 489: 490: 491: 492:
493: private static function _mapVAlign($vAlign) {
494: if (isset(self::$_mapVAlign[$vAlign]))
495: return self::$_mapVAlign[$vAlign];
496: return 2;
497: }
498:
499: 500: 501: 502: 503: 504:
505: private static function _mapTextRotation($textRotation) {
506: if ($textRotation >= 0) {
507: return $textRotation;
508: }
509: if ($textRotation == -165) {
510: return 255;
511: }
512: if ($textRotation < 0) {
513: return 90 - $textRotation;
514: }
515: }
516:
517: 518: 519: 520: 521: 522:
523: private static function _mapLocked($locked) {
524: switch ($locked) {
525: case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 1;
526: case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1;
527: case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED: return 0;
528: default: return 1;
529: }
530: }
531:
532: 533: 534: 535: 536: 537:
538: private static function _mapHidden($hidden) {
539: switch ($hidden) {
540: case PHPExcel_Style_Protection::PROTECTION_INHERIT: return 0;
541: case PHPExcel_Style_Protection::PROTECTION_PROTECTED: return 1;
542: case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED: return 0;
543: default: return 0;
544: }
545: }
546:
547: }
548: