Source for file Xf.php
Documentation is available at Xf.php
* Copyright (c) 2006 - 2011 PHPExcel
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Writer_Excel5
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.6, 2011-02-27
// Original file header of PEAR::Spreadsheet_Excel_Writer_Format (used as the base for this class):
// -----------------------------------------------------------------------------------------
// * 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
// * <jmcnamara@cpan.org>
// * 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.
// * License Information:
// * 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
* PHPExcel_Writer_Excel5_Xf
* @package PHPExcel_Writer_Excel5
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* Style XF or a cell XF ?
* Index to the FONT record. Index 4 does not exist
* An index (2 bytes) to a FORMAT record (number format).
* 1 bit, apparently not used.
* The cell's foreground color.
* The cell's background color.
* Color of the bottom border of the cell.
* Color of the top border of the cell.
* Color of the left border of the cell.
* Color of the right border of the cell.
* @param integer $index the XF index for the format.
public function __construct(PHPExcel_Style $style = null)
$this->_isStyleXf = false;
$this->_BIFFVersion = 0x0600;
$this->_diag_color = 0x40;
* 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->_mapLocked($this->_style->getProtection()->getLocked());
$style |= $this->_mapHidden($this->_style->getProtection()->getHidden()) << 1;
// Flags to indicate if attributes have been set.
$atr_fnt = ($this->_fontIndex != 0)? 1: 0;
$atr_alc = ((int) $this->_style->getAlignment()->getWrapText())? 1: 0;
$atr_bdr = ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) ||
$this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
$this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
$this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))? 1: 0;
$this->_mapFillType($this->_style->getFill()->getFillType()))? 1: 0;
$atr_prot = $this->_mapLocked($this->_style->getProtection()->getLocked())
| $this->_mapHidden($this->_style->getProtection()->getHidden());
// Zero the default border colour if the border has not been set.
if ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) {
if ($this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) {
if ($this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) {
if ($this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) {
if ($this->_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) == 0) {
$record = 0x00E0; // Record identifier
if ($this->_BIFFVersion == 0x0500) {
$length = 0x0010; // Number of bytes to follow
if ($this->_BIFFVersion == 0x0600) {
$ifnt = $this->_fontIndex; // Index to FONT record
if ($this->_BIFFVersion == 0x0500) {
$align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
$align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
$align |= $this->_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
$align |= 0 << 8; // rotation
$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
$fill = $this->_mapFillType($this->_style->getFill()->getFillType()); // Fill and border line style
$fill |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 6;
$border1 = $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()); // Border line style and color
$border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) << 3;
$border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 6;
$header = pack("vv", $record, $length);
$data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
} elseif ($this->_BIFFVersion == 0x0600) {
$align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
$align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
$align |= $this->_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
$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
$border1 = $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color
$border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4;
$border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8;
$border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12;
$diagonalDirection = $this->_style->getBorders()->getDiagonalDirection();
$border1 |= $diag_tl_to_rb << 30;
$border1 |= $diag_tr_to_lb << 31;
$border2 |= $this->_diag_color << 14;
$border2 |= $this->_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21;
$border2 |= $this->_mapFillType($this->_style->getFill()->getFillType()) << 26;
$header = pack("vv", $record, $length);
//BIFF8 options: identation, shrinkToFit and text direction
$biff8_options = $this->_style->getAlignment()->getIndent();
$biff8_options |= (int) $this->_style->getAlignment()->getShrinkToFit() << 4;
$data = pack("vvvC", $ifnt, $ifmt, $style, $align);
, $this->_mapTextRotation($this->_style->getAlignment()->getTextRotation())
$data .= pack("VVv", $border1, $border2, $icv);
* @param int $BIFFVersion
$this->_BIFFVersion = $BIFFVersion;
$this->_isStyleXf = $value;
* Sets the cell's bottom border color
* @param int $colorIndex Color index
* Sets the cell's top border color
* @param int $colorIndex Color index
* Sets the cell's left border color
* @param int $colorIndex Color index
* Sets the cell's right border color
* @param int $colorIndex Color index
* Sets the cell's diagonal border color
* @param int $colorIndex Color index
$this->_diag_color = $colorIndex;
* Sets the cell's foreground color
* @param int $colorIndex Color index
* Sets the cell's background color
* @param int $colorIndex Color index
* Sets the index to the number format record
* It can be date, time, currency, etc...
* @param integer $numberFormatIndex Index to format record
* @param int $value Font index, note that value 4 does not exist
$this->_fontIndex = $value;
private function _mapBorderStyle($borderStyle) {
private function _mapFillType($fillType) {
* Map to BIFF2-BIFF8 codes for horizontal alignment
private function _mapHAlign($hAlign)
* Map to BIFF2-BIFF8 codes for vertical alignment
private function _mapVAlign($vAlign) {
* Map to BIFF8 codes for text rotation angle
* @param int $textRotation
private function _mapTextRotation($textRotation) {
if ($textRotation >= 0) {
if ($textRotation == - 165) {
return 90 - $textRotation;
private function _mapLocked($locked) {
private function _mapHidden($hidden) {
|