3 * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
5 * The majority of this is _NOT_ my code. I simply ported it from the
6 * PERL Spreadsheet::WriteExcel module.
8 * The author of the Spreadsheet::WriteExcel module is John McNamara
11 * I _DO_ maintain this code, and John McNamara has nothing to do with the
12 * porting of this code to PHP. Any questions directly related to this
13 * class library should be directed to me.
15 * License Information:
17 * Spreadsheet::WriteExcel: A library for generating Excel Spreadsheets
18 * Copyright (C) 2002 Xavier Noguer xnoguer@rezebra.com
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 * Class for generating Excel XF records (formats)
38 * @author Xavier Noguer <xnoguer@rezebra.com>
39 * @package Spreadsheet_WriteExcel
48 * @param integer $index the XF index for the format.
49 * @param array $properties array with properties to be set on initialization.
51 function Format($index = 0,$properties = array())
53 $this->xf_index
= $index;
55 $this->font_index
= 0;
56 $this->font
= 'Arial';
60 $this->color
= 0x7FFF;
61 $this->_underline
= 0;
62 $this->font_strikeout
= 0;
63 $this->font_outline
= 0;
64 $this->font_shadow
= 0;
65 $this->font_script
= 0;
66 $this->font_family
= 0;
67 $this->font_charset
= 0;
69 $this->_num_format
= 0;
74 $this->_text_h_align
= 0;
75 $this->_text_wrap
= 0;
76 $this->text_v_align
= 2;
77 $this->text_justlast
= 0;
80 $this->fg_color
= 0x40;
81 $this->bg_color
= 0x41;
90 $this->bottom_color
= 0x40;
91 $this->top_color
= 0x40;
92 $this->left_color
= 0x40;
93 $this->right_color
= 0x40;
95 // Set properties passed to Workbook::add_format()
96 foreach($properties as $property => $value)
98 if(method_exists($this,"set_$property"))
100 $aux = 'set_'.$property;
107 * Generate an Excel BIFF XF record (style or cell).
109 * @param string $style The type of the XF record ('style' or 'cell').
110 * @return string The XF record
112 function get_xf($style)
114 // Set the type of the XF record and some of the attributes.
115 if ($style == "style") {
119 $style = $this->locked
;
120 $style |
= $this->hidden
<< 1;
123 // Flags to indicate if attributes have been set.
124 $atr_num = ($this->_num_format
!= 0)?
1:0;
125 $atr_fnt = ($this->font_index
!= 0)?
1:0;
126 $atr_alc = ($this->_text_wrap
)?
1:0;
127 $atr_bdr = ($this->bottom ||
131 $atr_pat = (($this->fg_color
!= 0x40) ||
132 ($this->bg_color
!= 0x41) ||
136 // Zero the default border colour if the border has not been set.
137 if ($this->bottom
== 0) {
138 $this->bottom_color
= 0;
140 if ($this->top
== 0) {
141 $this->top_color
= 0;
143 if ($this->right
== 0) {
144 $this->right_color
= 0;
146 if ($this->left
== 0) {
147 $this->left_color
= 0;
150 $record = 0x00E0; // Record identifier
151 $length = 0x0010; // Number of bytes to follow
153 $ifnt = $this->font_index
; // Index to FONT record
154 $ifmt = $this->_num_format
; // Index to FORMAT record
156 $align = $this->_text_h_align
; // Alignment
157 $align |
= $this->_text_wrap
<< 3;
158 $align |
= $this->text_v_align
<< 4;
159 $align |
= $this->text_justlast
<< 7;
160 $align |
= $this->rotation
<< 8;
161 $align |
= $atr_num << 10;
162 $align |
= $atr_fnt << 11;
163 $align |
= $atr_alc << 12;
164 $align |
= $atr_bdr << 13;
165 $align |
= $atr_pat << 14;
166 $align |
= $atr_prot << 15;
168 $icv = $this->fg_color
; // fg and bg pattern colors
169 $icv |
= $this->bg_color
<< 7;
171 $fill = $this->pattern
; // Fill and border line style
172 $fill |
= $this->bottom
<< 6;
173 $fill |
= $this->bottom_color
<< 9;
175 $border1 = $this->top
; // Border line style and color
176 $border1 |
= $this->left
<< 3;
177 $border1 |
= $this->right
<< 6;
178 $border1 |
= $this->top_color
<< 9;
180 $border2 = $this->left_color
; // Border color
181 $border2 |
= $this->right_color
<< 7;
183 $header = pack("vv", $record, $length);
184 $data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
187 return($header.$data);
191 * Generate an Excel BIFF FONT record.
193 * @see Workbook::_store_all_fonts()
194 * @return string The FONT record
198 $dyHeight = $this->size
* 20; // Height of font (1/20 of a point)
199 $icv = $this->color
; // Index to color palette
200 $bls = $this->bold
; // Bold style
201 $sss = $this->font_script
; // Superscript/subscript
202 $uls = $this->_underline
; // Underline
203 $bFamily = $this->font_family
; // Font family
204 $bCharSet = $this->font_charset
; // Character set
205 $rgch = $this->font
; // Font name
207 $cch = strlen($rgch); // Length of font name
208 $record = 0x31; // Record identifier
209 $length = 0x0F +
$cch; // Record length
210 $reserved = 0x00; // Reserved
211 $grbit = 0x00; // Font attributes
212 if ($this->_italic
) {
215 if ($this->font_strikeout
) {
218 if ($this->font_outline
) {
221 if ($this->font_shadow
) {
225 $header = pack("vv", $record, $length);
226 $data = pack("vvvvvCCCCC", $dyHeight, $grbit, $icv, $bls,
227 $sss, $uls, $bFamily,
228 $bCharSet, $reserved, $cch);
229 return($header . $data. $this->font
);
233 * Returns a unique hash key for a font. Used by Workbook->_store_all_fonts()
235 * The elements that form the key are arranged to increase the probability of
236 * generating a unique key. Elements that hold a large range of numbers
237 * (eg. _color) are placed between two binary elements such as _italic
239 * @return string A key for this font
241 function get_font_key()
243 $key = "$this->font$this->size";
244 $key .= "$this->font_script$this->_underline";
245 $key .= "$this->font_strikeout$this->bold$this->font_outline";
246 $key .= "$this->font_family$this->font_charset";
247 $key .= "$this->font_shadow$this->color$this->_italic";
248 $key = str_replace(" ","_",$key);
253 * Returns the index used by Worksheet->_XF()
255 * @return integer The index for the XF record
257 function get_xf_index()
259 return($this->xf_index
);
263 * Used in conjunction with the set_xxx_color methods to convert a color
264 * string into a number. Color range is 0..63 but we will restrict it
265 * to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
267 * @param string $name_color name of the color (i.e.: 'blue', 'red', etc..). Optional.
268 * @return integer The color index
270 function _get_color($name_color = '')
293 // Return the default color, 0x7FFF, if undef,
294 if($name_color == '') {
298 // or the color string converted to an integer,
299 if(isset($colors[$name_color])) {
300 return($colors[$name_color]);
303 // or the default color if string is unrecognised,
304 if(preg_match("/\D/",$name_color)) {
308 // or an index < 8 mapped into the correct range,
309 if($name_color < 8) {
310 return($name_color +
8);
313 // or the default color if arg is outside range,
314 if($name_color > 63) {
318 // or an integer in the valid range
323 * Set cell alignment.
326 * @param string $location alignment for the cell ('left', 'right', etc...).
328 function set_align($location)
330 if (preg_match("/\d/",$location)) {
331 return; // Ignore numbers
334 $location = strtolower($location);
336 if ($location == 'left')
337 $this->_text_h_align
= 1;
338 if ($location == 'centre')
339 $this->_text_h_align
= 2;
340 if ($location == 'center')
341 $this->_text_h_align
= 2;
342 if ($location == 'right')
343 $this->_text_h_align
= 3;
344 if ($location == 'fill')
345 $this->_text_h_align
= 4;
346 if ($location == 'justify')
347 $this->_text_h_align
= 5;
348 if ($location == 'merge')
349 $this->_text_h_align
= 6;
350 if ($location == 'equal_space') // For T.K.
351 $this->_text_h_align
= 7;
352 if ($location == 'top')
353 $this->text_v_align
= 0;
354 if ($location == 'vcentre')
355 $this->text_v_align
= 1;
356 if ($location == 'vcenter')
357 $this->text_v_align
= 1;
358 if ($location == 'bottom')
359 $this->text_v_align
= 2;
360 if ($location == 'vjustify')
361 $this->text_v_align
= 3;
362 if ($location == 'vequal_space') // For T.K.
363 $this->text_v_align
= 4;
367 * This is an alias for the unintuitive set_align('merge')
373 $this->set_align('merge');
377 * Bold has a range 0x64..0x3E8.
378 * 0x190 is normal. 0x2BC is bold.
381 * @param integer $weight Weight for the text, 0 maps to 0x190, 1 maps to 0x2BC.
382 It's Optional, default is 1 (bold).
384 function set_bold($weight = 1)
387 $weight = 0x2BC; // Bold text
390 $weight = 0x190; // Normal text
392 if($weight < 0x064) {
393 $weight = 0x190; // Lower bound
395 if($weight > 0x3E8) {
396 $weight = 0x190; // Upper bound
398 $this->bold
= $weight;
402 /************************************
403 * FUNCTIONS FOR SETTING CELLS BORDERS
407 * Sets the bottom border of the cell
410 * @param integer $style style of the cell border. 1 => thin, 2 => thick.
412 function set_bottom($style)
414 $this->bottom
= $style;
418 * Sets the top border of the cell
421 * @param integer $style style of the cell top border. 1 => thin, 2 => thick.
423 function set_top($style)
429 * Sets the left border of the cell
432 * @param integer $style style of the cell left border. 1 => thin, 2 => thick.
434 function set_left($style)
436 $this->left
= $style;
440 * Sets the right border of the cell
443 * @param integer $style style of the cell right border. 1 => thin, 2 => thick.
445 function set_right($style)
447 $this->right
= $style;
452 * Set cells borders to the same style
455 * @param integer $style style to apply for all cell borders. 1 => thin, 2 => thick.
457 function set_border($style)
459 $this->set_bottom($style);
460 $this->set_top($style);
461 $this->set_left($style);
462 $this->set_right($style);
466 /*******************************************
467 * FUNCTIONS FOR SETTING CELLS BORDERS COLORS
471 * Sets all the cell's borders to the same color
474 * @param mixed $color The color we are setting. Either a string (like 'blue'),
475 * or an integer (like 0x41).
477 function set_border_color($color)
479 $this->set_bottom_color($color);
480 $this->set_top_color($color);
481 $this->set_left_color($color);
482 $this->set_right_color($color);
486 * Sets the cell's bottom border color
489 * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
491 function set_bottom_color($color)
493 $value = $this->_get_color($color);
494 $this->bottom_color
= $value;
498 * Sets the cell's top border color
501 * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
503 function set_top_color($color)
505 $value = $this->_get_color($color);
506 $this->top_color
= $value;
510 * Sets the cell's left border color
513 * @param mixed $color either a string (like 'blue'), or an integer (like 0x41).
515 function set_left_color($color)
517 $value = $this->_get_color($color);
518 $this->left_color
= $value;
522 * Sets the cell's right border color
525 * @param mixed $color either a string (like 'blue'), or an integer (like 0x41).
527 function set_right_color($color)
529 $value = $this->_get_color($color);
530 $this->right_color
= $value;
535 * Sets the cell's foreground color
538 * @param mixed $color either a string (like 'blue'), or an integer (like 0x41).
540 function set_fg_color($color)
542 $value = $this->_get_color($color);
543 $this->fg_color
= $value;
547 * Sets the cell's background color
550 * @param mixed $color either a string (like 'blue'), or an integer (like 0x41).
552 function set_bg_color($color)
554 $value = $this->_get_color($color);
555 $this->bg_color
= $value;
559 * Sets the cell's color
562 * @param mixed $color either a string (like 'blue'), or an integer (like 0x41).
564 function set_color($color)
566 $value = $this->_get_color($color);
567 $this->color
= $value;
571 * Sets the pattern attribute of a cell
574 * @param integer $arg Optional. Defaults to 1.
576 function set_pattern($arg = 1)
578 $this->pattern
= $arg;
582 * Sets the underline of the text
585 * @param integer $underline The value for underline. Possible values are:
586 * 1 => underline, 2 => double underline.
588 function set_underline($underline)
590 $this->_underline
= $underline;
594 * Sets the font style as italic
598 function set_italic()
607 * @param integer $size The font size (in pixels I think).
609 function set_size($size)
615 * Sets the num format
618 * @param integer $num_format The num format.
620 function set_num_format($num_format)
622 $this->_num_format
= $num_format;
629 * @param integer $text_wrap Optional. 0 => no text wrapping, 1 => text wrapping.
632 function set_text_wrap($text_wrap = 1)
634 $this->_text_wrap
= $text_wrap;