source: sourcecode/application/libraries/PHPExcel/Writer/Excel5/Xf.php @ 1

Last change on this file since 1 was 1, checked in by dungnv, 11 years ago
File size: 16.6 KB
Line 
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
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// */
62
63
64/**
65 * PHPExcel_Writer_Excel5_Xf
66 *
67 * @category   PHPExcel
68 * @package    PHPExcel_Writer_Excel5
69 * @copyright  Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
70 */
71class PHPExcel_Writer_Excel5_Xf
72{
73    /**
74         * Style XF or a cell XF ?
75         *
76         * @var boolean
77         */
78        private $_isStyleXf;
79
80        /**
81         * Index to the FONT record. Index 4 does not exist
82         * @var integer
83         */
84        private $_fontIndex;
85
86        /**
87         * An index (2 bytes) to a FORMAT record (number format).
88         * @var integer
89         */
90        public $_numberFormatIndex;
91
92        /**
93         * 1 bit, apparently not used.
94         * @var integer
95         */
96        public $_text_justlast;
97
98        /**
99         * The cell's foreground color.
100         * @var integer
101         */
102        public $_fg_color;
103
104        /**
105         * The cell's background color.
106         * @var integer
107         */
108        public $_bg_color;
109
110        /**
111         * Color of the bottom border of the cell.
112         * @var integer
113         */
114        public $_bottom_color;
115
116        /**
117         * Color of the top border of the cell.
118         * @var integer
119         */
120        public $_top_color;
121
122        /**
123        * Color of the left border of the cell.
124        * @var integer
125        */
126        public $_left_color;
127
128        /**
129         * Color of the right border of the cell.
130         * @var integer
131         */
132        public $_right_color;
133
134        /**
135         * Constructor
136         *
137         * @access public
138         * @param PHPExcel_Style        The XF format
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         * Generate an Excel BIFF XF record (style or cell).
166         *
167         * @return string The XF record
168         */
169        function writeXf()
170        {
171                // Set the type of the XF record and some of the attributes.
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                // Flags to indicate if attributes have been set.
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                // Zero the default border colour if the border has not been set.
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;              // Record identifier
211                $length         = 0x0014;              // Number of bytes to follow
212
213                $ifnt           = $this->_fontIndex;   // Index to FONT record
214                $ifmt           = $this->_numberFormatIndex;  // Index to FORMAT record
215
216                $align          = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal());       // Alignment
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;      // fg and bg pattern colors
229                $icv           |= $this->_bg_color      << 7;
230
231                $border1        = self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle());          // Border line style and color
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;    // Border 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                //BIFF8 options: identation, shrinkToFit and  text direction
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         * Is this a style XF ?
271         *
272         * @param boolean $value
273         */
274        public function setIsStyleXf($value)
275        {
276                $this->_isStyleXf = $value;
277        }
278
279        /**
280         * Sets the cell's bottom border color
281         *
282         * @access public
283         * @param int $colorIndex Color index
284         */
285        function setBottomColor($colorIndex)
286        {
287                $this->_bottom_color = $colorIndex;
288        }
289
290        /**
291         * Sets the cell's top border color
292         *
293         * @access public
294         * @param int $colorIndex Color index
295         */
296        function setTopColor($colorIndex)
297        {
298                $this->_top_color = $colorIndex;
299        }
300
301        /**
302         * Sets the cell's left border color
303         *
304         * @access public
305         * @param int $colorIndex Color index
306         */
307        function setLeftColor($colorIndex)
308        {
309                $this->_left_color = $colorIndex;
310        }
311
312        /**
313         * Sets the cell's right border color
314         *
315         * @access public
316         * @param int $colorIndex Color index
317         */
318        function setRightColor($colorIndex)
319        {
320                $this->_right_color = $colorIndex;
321        }
322
323        /**
324         * Sets the cell's diagonal border color
325         *
326         * @access public
327         * @param int $colorIndex Color index
328         */
329        function setDiagColor($colorIndex)
330        {
331                $this->_diag_color = $colorIndex;
332        }
333
334
335        /**
336         * Sets the cell's foreground color
337         *
338         * @access public
339         * @param int $colorIndex Color index
340         */
341        function setFgColor($colorIndex)
342        {
343                $this->_fg_color = $colorIndex;
344        }
345
346        /**
347         * Sets the cell's background color
348         *
349         * @access public
350         * @param int $colorIndex Color index
351         */
352        function setBgColor($colorIndex)
353        {
354                $this->_bg_color = $colorIndex;
355        }
356
357        /**
358         * Sets the index to the number format record
359         * It can be date, time, currency, etc...
360         *
361         * @access public
362         * @param integer $numberFormatIndex Index to format record
363         */
364        function setNumberFormatIndex($numberFormatIndex)
365        {
366                $this->_numberFormatIndex = $numberFormatIndex;
367        }
368
369        /**
370         * Set the font index.
371         *
372         * @param int $value Font index, note that value 4 does not exist
373         */
374        public function setFontIndex($value)
375        {
376                $this->_fontIndex = $value;
377        }
378
379        /**
380         * Map of BIFF2-BIFF8 codes for border styles
381         * @static      array of int
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         * Map border style
402         *
403         * @param string $borderStyle
404         * @return int
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         * Map of BIFF2-BIFF8 codes for fill types
414         * @static      array of int
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,        // does not exist in BIFF8
437                                                                                  PHPExcel_Style_Fill::FILL_GRADIENT_PATH                       => 0x00,        // does not exist in BIFF8
438                                                                                );
439        /**
440         * Map fill type
441         *
442         * @param string $fillType
443         * @return int
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         * Map of BIFF2-BIFF8 codes for horizontal alignment
453         * @static      array of int
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         * Map to BIFF2-BIFF8 codes for horizontal alignment
466         *
467         * @param string $hAlign
468         * @return int
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         * Map of BIFF2-BIFF8 codes for vertical alignment
479         * @static      array of int
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         * Map to BIFF2-BIFF8 codes for vertical alignment
489         *
490         * @param string $vAlign
491         * @return int
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         * Map to BIFF8 codes for text rotation angle
501         *
502         * @param int $textRotation
503         * @return int
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         * Map locked
519         *
520         * @param string
521         * @return int
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         * Map hidden
534         *
535         * @param string
536         * @return int
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}
Note: See TracBrowser for help on using the repository browser.