Source for file Format.php
Documentation is available at Format.php
* Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
* The majority of this is _NOT_ my code. I simply ported it from the
* PERL Spreadsheet::WriteExcel module.
* The author of the Spreadsheet::WriteExcel module is John McNamara
* I _DO_ maintain this code, and John McNamara has nothing to do with the
* porting of this code to PHP. Any questions directly related to this
* class library should be directed to me.
* Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
* Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
require_once('PEAR.php');
* Class for generating Excel XF records (formats)
* @author Xavier Noguer <xnoguer@rezebra.com>
* @package Spreadsheet_Excel_Writer
* The index given by the workbook when creating a new format.
* Index to the FONT record.
* Height of font (1/20 of a point)
* Bit specifiying if the font is italic.
* Index to the cell's color
* The text underline property
* Bit specifiying if the font has strikeout.
* Bit specifiying if the font has outline.
* Bit specifiying if the font has shadow.
* 2 bytes specifiying the script type for the font.
* Byte specifiying the font family.
* Byte specifiying the font charset.
* An index (2 bytes) to a FORMAT record (number format).
* Bit specifying if formulas are hidden.
* Bit specifying if the cell is locked.
* The three bits specifying the text horizontal alignment.
* Bit specifying if the text is wrapped at the right border.
* The three bits specifying the text vertical alignment.
* 1 bit, apparently not used.
* The two bits specifying the text rotation.
* The cell's foreground color.
* The cell's background color.
* The cell's background fill pattern.
* Style of the bottom border of the cell
* Color of the bottom border of the cell.
* Style of the top border of the cell
* Color of the top border of the cell.
* Style of the left border of the cell
* Color of the left border of the cell.
* Style of the right border of the cell
* Color of the right border of the cell.
* @param integer $index the XF index for the format.
* @param array $properties array with properties to be set on initialization.
function Spreadsheet_Excel_Writer_Format ($BIFF_version, $index = 0 , $properties = array ())
$this->_xf_index = $index;
$this->_BIFF_version = $BIFF_version;
$this->_font_name = 'Arial';
$this->_font_strikeout = 0;
$this->_font_outline = 0;
$this->_font_charset = 0;
$this->_text_h_align = 0;
$this->_text_v_align = 2;
$this->_text_justlast = 0;
$this->_bottom_color = 0x40;
$this->_top_color = 0x40;
$this->_left_color = 0x40;
$this->_right_color = 0x40;
$this->_diag_color = 0x40;
// Set properties passed to Spreadsheet_Excel_Writer_Workbook::addFormat()
foreach($properties as $property => $value)
$method_name = 'set'. ucwords($property);
$this->$method_name($value);
* Generate an Excel BIFF XF record (style or cell).
* @param string $style The type of the XF record ('style' or 'cell').
* @return string The XF record
// Set the type of the XF record and some of the attributes.
$style |= $this->_hidden << 1;
// Flags to indicate if attributes have been set.
$atr_num = ($this->_num_format != 0 )?1:0;
$atr_alc = ($this->_text_wrap)?1:0;
$atr_bdr = ($this->_bottom ||
$atr_pat = (($this->_fg_color != 0x40 ) ||
($this->_bg_color != 0x41 ) ||
$atr_prot = $this->_locked | $this->_hidden;
// Zero the default border colour if the border has not been set.
if ($this->_bottom == 0 ) {
$this->_bottom_color = 0;
if ($this->_right == 0 ) {
$record = 0x00E0; // Record identifier
if ($this->_BIFF_version == 0x0500 ) {
$length = 0x0010; // Number of bytes to follow
if ($this->_BIFF_version == 0x0600 ) {
$ifnt = $this->font_index; // Index to FONT record
$ifmt = $this->_num_format; // Index to FORMAT record
if ($this->_BIFF_version == 0x0500 )
$align = $this->_text_h_align; // Alignment
$align |= $this->_text_wrap << 3;
$align |= $this->_text_v_align << 4;
$align |= $this->_text_justlast << 7;
$align |= $this->_rotation << 8;
$align |= $atr_num << 10;
$align |= $atr_fnt << 11;
$align |= $atr_alc << 12;
$align |= $atr_bdr << 13;
$align |= $atr_pat << 14;
$align |= $atr_prot << 15;
$icv = $this->_fg_color; // fg and bg pattern colors
$icv |= $this->_bg_color << 7;
$fill = $this->_pattern; // Fill and border line style
$fill |= $this->_bottom << 6;
$fill |= $this->_bottom_color << 9;
$border1 = $this->_top; // Border line style and color
$border1 |= $this->_left << 3;
$border1 |= $this->_right << 6;
$border1 |= $this->_top_color << 9;
$border2 = $this->_left_color; // Border color
$border2 |= $this->_right_color << 7;
$header = pack("vv", $record, $length);
$data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
elseif ($this->_BIFF_version == 0x0600 )
$align = $this->_text_h_align; // Alignment
$align |= $this->_text_wrap << 3;
$align |= $this->_text_v_align << 4;
$align |= $this->_text_justlast << 7;
$used_attrib = $atr_num << 2;
$used_attrib |= $atr_fnt << 3;
$used_attrib |= $atr_alc << 4;
$used_attrib |= $atr_bdr << 5;
$used_attrib |= $atr_pat << 6;
$used_attrib |= $atr_prot << 7;
$icv = $this->_fg_color; // fg and bg pattern colors
$icv |= $this->_bg_color << 7;
$border1 = $this->_left; // Border line style and color
$border1 |= $this->_right << 4;
$border1 |= $this->_top << 8;
$border1 |= $this->_bottom << 12;
$border1 |= $this->_left_color << 16;
$border1 |= $this->_right_color << 23;
$diag_tl_to_rb = 0; // FIXME: add method
$diag_tr_to_lb = 0; // FIXME: add method
$border1 |= $diag_tl_to_rb << 30;
$border1 |= $diag_tr_to_lb << 31;
$border2 = $this->_top_color; // Border color
$border2 |= $this->_bottom_color << 7;
$border2 |= $this->_diag_color << 14;
$border2 |= $this->_diag << 21;
$border2 |= $this->_pattern << 26;
$header = pack("vv", $record, $length);
$data = pack("vvvC", $ifnt, $ifmt, $style, $align);
$data .= pack("CCC", $rotation, $biff8_options, $used_attrib);
$data .= pack("VVv", $border1, $border2, $icv);
* Generate an Excel BIFF FONT record.
* @return string The FONT record
$dyHeight = $this->_size * 20; // Height of font (1/20 of a point)
$icv = $this->_color; // Index to color palette
$bls = $this->_bold; // Bold style
$sss = $this->_font_script; // Superscript/subscript
$uls = $this->_underline; // Underline
$bFamily = $this->_font_family; // Font family
$bCharSet = $this->_font_charset; // Character set
$encoding = 0; // TODO: Unicode support
$cch = strlen($this->_font_name); // Length of font name
$record = 0x31; // Record identifier
if ($this->_BIFF_version == 0x0500 ) {
$length = 0x0F + $cch; // Record length
elseif ($this->_BIFF_version == 0x0600 ) {
$reserved = 0x00; // Reserved
$grbit = 0x00; // Font attributes
if ($this->_font_strikeout) {
if ($this->_font_outline) {
if ($this->_font_shadow) {
$header = pack("vv", $record, $length);
if ($this->_BIFF_version == 0x0500 ) {
$data = pack("vvvvvCCCCC", $dyHeight, $grbit, $icv, $bls,
$bCharSet, $reserved, $cch);
elseif ($this->_BIFF_version == 0x0600 ) {
$data = pack("vvvvvCCCCCC", $dyHeight, $grbit, $icv, $bls,
$bCharSet, $reserved, $cch, $encoding);
return($header . $data. $this->_font_name);
* Returns a unique hash key for a font.
* Used by Spreadsheet_Excel_Writer_Workbook::_storeAllFonts()
* The elements that form the key are arranged to increase the probability of
* generating a unique key. Elements that hold a large range of numbers
* (eg. _color) are placed between two binary elements such as _italic
* @return string A key for this font
$key = " $this->_font_name$this->_size";
$key .= " $this->_font_script$this->_underline";
$key .= " $this->_font_strikeout$this->_bold$this->_font_outline";
$key .= " $this->_font_family$this->_font_charset";
$key .= " $this->_font_shadow$this->_color$this->_italic";
* Returns the index used by Spreadsheet_Excel_Writer_Worksheet::_XF()
* @return integer The index for the XF record
return($this->_xf_index);
* Used in conjunction with the set_xxx_color methods to convert a color
* string into a number. Color range is 0..63 but we will restrict it
* to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
* @param string $name_color name of the color (i.e.: 'blue', 'red', etc..). Optional.
* @return integer The color index
function _getColor ($name_color = '')
// Return the default color, 0x7FFF, if undef,
// or the color string converted to an integer,
if(isset ($colors[$name_color])) {
return($colors[$name_color]);
// or the default color if string is unrecognised,
// or an index < 8 mapped into the correct range,
// or the default color if arg is outside range,
// or an integer in the valid range
* @param string $location alignment for the cell ('left', 'right', etc...).
return; // Ignore numbers
if ($location == 'left') {
$this->_text_h_align = 1;
if ($location == 'centre') {
$this->_text_h_align = 2;
if ($location == 'center') {
$this->_text_h_align = 2;
if ($location == 'right') {
$this->_text_h_align = 3;
if ($location == 'fill') {
$this->_text_h_align = 4;
if ($location == 'justify') {
$this->_text_h_align = 5;
if ($location == 'merge') {
$this->_text_h_align = 6;
if ($location == 'equal_space') { // For T.K.
$this->_text_h_align = 7;
if ($location == 'top') {
$this->_text_v_align = 0;
if ($location == 'vcentre') {
$this->_text_v_align = 1;
if ($location == 'vcenter') {
$this->_text_v_align = 1;
if ($location == 'bottom') {
$this->_text_v_align = 2;
if ($location == 'vjustify') {
$this->_text_v_align = 3;
if ($location == 'vequal_space') { // For T.K.
$this->_text_v_align = 4;
* This is an alias for the unintuitive setAlign('merge')
* Sets the boldness of the text.
* Bold has a range 100..1000.
* 0 (400) is normal. 1 (700) is bold.
* @param integer $weight Weight for the text, 0 maps to 400 (normal text),
1 maps to 700 (bold text). Valid range is: 100-1000.
It's Optional, default is 1 (bold).
$weight = 0x2BC; // Bold text
$weight = 0x190; // Normal text
$weight = 0x190; // Lower bound
$weight = 0x190; // Upper bound
/************************************
* FUNCTIONS FOR SETTING CELLS BORDERS
* Sets the width for the bottom border of the cell
* @param integer $style style of the cell border. 1 => thin, 2 => thick.
* Sets the width for the top border of the cell
* @param integer $style style of the cell top border. 1 => thin, 2 => thick.
* Sets the width for the left border of the cell
* @param integer $style style of the cell left border. 1 => thin, 2 => thick.
* Sets the width for the right border of the cell
* @param integer $style style of the cell right border. 1 => thin, 2 => thick.
* Set cells borders to the same style
* @param integer $style style to apply for all cell borders. 1 => thin, 2 => thick.
/*******************************************
* FUNCTIONS FOR SETTING CELLS BORDERS COLORS
* Sets all the cell's borders to the same color
* @param mixed $color The color we are setting. Either a string (like 'blue'),
* or an integer (range is [8...63]).
* Sets the cell's bottom border color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
$value = $this->_getColor ($color);
$this->_bottom_color = $value;
* Sets the cell's top border color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
$value = $this->_getColor ($color);
$this->_top_color = $value;
* Sets the cell's left border color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
$value = $this->_getColor ($color);
$this->_left_color = $value;
* Sets the cell's right border color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
$value = $this->_getColor ($color);
$this->_right_color = $value;
* Sets the cell's foreground color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
$value = $this->_getColor ($color);
$this->_fg_color = $value;
if ($this->_pattern == 0 ) { // force color to be seen
* Sets the cell's background color
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
$value = $this->_getColor ($color);
$this->_bg_color = $value;
if ($this->_pattern == 0 ) { // force color to be seen
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]).
$value = $this->_getColor ($color);
* Sets the fill pattern attribute of a cell
* @param integer $arg Optional. Defaults to 1. Meaningful values are: 0-18,
* 0 meaning no background.
* Sets the underline of the text
* @param integer $underline The value for underline. Possible values are:
* 1 => underline, 2 => double underline.
$this->_underline = $underline;
* Sets the font style as italic
* @param integer $size The font size (in pixels I think).
* Sets the orientation of the text
* @param integer $angle The rotation angle for the text (clockwise). Possible
values are: 0, 90, 270 and -1 for stacking top-to-bottom.
return $this->raiseError ("Invalid value for angle.".
" Possible values are: 0, 90, 270 and -1 ".
"for stacking top-to-bottom.");
* Sets the numeric format.
* It can be date, time, currency, etc...
* @param integer $num_format The numeric format.
$this->_num_format = $num_format;
* Sets font as strikeout.
$this->_font_strikeout = 1;
* Sets outlining for a font.
$this->_font_outline = 1;
* Sets the script type of the text
* @param integer $script The value for script type. Possible values are:
* 1 => superscript, 2 => subscript.
$this->_font_script = $script;
* Unlocks a cell. Useful for unprotecting particular cells of a protected sheet.
* Sets the font family name.
* @param string $fontfamily The font family name. Possible values are:
* 'Times New Roman', 'Arial', 'Courier'.
$this->_font_name = $font_family;
Documentation generated on Mon, 11 Mar 2019 13:51:57 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|