updated-packages

This commit is contained in:
RafficMohammed
2023-01-08 00:13:22 +05:30
parent 3ff7df7487
commit da241bacb6
12659 changed files with 563377 additions and 510538 deletions

View File

@@ -0,0 +1,224 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Xls;
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
// Original file header of PEAR::Spreadsheet_Excel_Writer_BIFFwriter (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
// */
class BIFFwriter
{
/**
* The byte order of this architecture. 0 => little endian, 1 => big endian.
*
* @var int
*/
private static $byteOrder;
/**
* The string containing the data of the BIFF stream.
*
* @var string
*/
public $_data;
/**
* The size of the data in bytes. Should be the same as strlen($this->_data).
*
* @var int
*/
public $_datasize;
/**
* The maximum length for a BIFF record (excluding record header and length field). See addContinue().
*
* @var int
*
* @see addContinue()
*/
private $limit = 8224;
/**
* Constructor.
*/
public function __construct()
{
$this->_data = '';
$this->_datasize = 0;
}
/**
* Determine the byte order and store it as class data to avoid
* recalculating it for each call to new().
*
* @return int
*/
public static function getByteOrder()
{
if (!isset(self::$byteOrder)) {
// Check if "pack" gives the required IEEE 64bit float
$teststr = pack('d', 1.2345);
$number = pack('C8', 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
if ($number == $teststr) {
$byte_order = 0; // Little Endian
} elseif ($number == strrev($teststr)) {
$byte_order = 1; // Big Endian
} else {
// Give up. I'll fix this in a later version.
throw new WriterException('Required floating point format not supported on this platform.');
}
self::$byteOrder = $byte_order;
}
return self::$byteOrder;
}
/**
* General storage function.
*
* @param string $data binary data to append
*/
protected function append($data): void
{
if (strlen($data) - 4 > $this->limit) {
$data = $this->addContinue($data);
}
$this->_data .= $data;
$this->_datasize += strlen($data);
}
/**
* General storage function like append, but returns string instead of modifying $this->_data.
*
* @param string $data binary data to write
*
* @return string
*/
public function writeData($data)
{
if (strlen($data) - 4 > $this->limit) {
$data = $this->addContinue($data);
}
$this->_datasize += strlen($data);
return $data;
}
/**
* Writes Excel BOF record to indicate the beginning of a stream or
* sub-stream in the BIFF file.
*
* @param int $type type of BIFF file to write: 0x0005 Workbook,
* 0x0010 Worksheet
*/
protected function storeBof($type): void
{
$record = 0x0809; // Record identifier (BIFF5-BIFF8)
$length = 0x0010;
// by inspection of real files, MS Office Excel 2007 writes the following
$unknown = pack('VV', 0x000100D1, 0x00000406);
$build = 0x0DBB; // Excel 97
$year = 0x07CC; // Excel 97
$version = 0x0600; // BIFF8
$header = pack('vv', $record, $length);
$data = pack('vvvv', $version, $type, $build, $year);
$this->append($header . $data . $unknown);
}
/**
* Writes Excel EOF record to indicate the end of a BIFF stream.
*/
protected function storeEof(): void
{
$record = 0x000A; // Record identifier
$length = 0x0000; // Number of bytes to follow
$header = pack('vv', $record, $length);
$this->append($header);
}
/**
* Writes Excel EOF record to indicate the end of a BIFF stream.
*/
public function writeEof()
{
$record = 0x000A; // Record identifier
$length = 0x0000; // Number of bytes to follow
$header = pack('vv', $record, $length);
return $this->writeData($header);
}
/**
* Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In
* Excel 97 the limit is 8228 bytes. Records that are longer than these limits
* must be split up into CONTINUE blocks.
*
* This function takes a long BIFF record and inserts CONTINUE records as
* necessary.
*
* @param string $data The original binary data to be written
*
* @return string A very convenient string of continue blocks
*/
private function addContinue($data)
{
$limit = $this->limit;
$record = 0x003C; // Record identifier
// The first 2080/8224 bytes remain intact. However, we have to change
// the length field of the record.
$tmp = substr($data, 0, 2) . pack('v', $limit) . substr($data, 4, $limit);
$header = pack('vv', $record, $limit); // Headers for continue records
// Retrieve chunks of 2080/8224 bytes +4 for the header.
$data_length = strlen($data);
for ($i = $limit + 4; $i < ($data_length - $limit); $i += $limit) {
$tmp .= $header;
$tmp .= substr($data, $i, $limit);
}
// Retrieve the last chunk of data
$header = pack('vv', $record, strlen($data) - $i);
$tmp .= $header;
$tmp .= substr($data, $i);
return $tmp;
}
}

View File

@@ -0,0 +1,510 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Xls;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Shared\Escher\DgContainer;
use PhpOffice\PhpSpreadsheet\Shared\Escher\DgContainer\SpgrContainer;
use PhpOffice\PhpSpreadsheet\Shared\Escher\DgContainer\SpgrContainer\SpContainer;
use PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer;
use PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer;
use PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer\BSE;
use PhpOffice\PhpSpreadsheet\Shared\Escher\DggContainer\BstoreContainer\BSE\Blip;
class Escher
{
/**
* The object we are writing.
*/
private $object;
/**
* The written binary data.
*/
private $data;
/**
* Shape offsets. Positions in binary stream where a new shape record begins.
*
* @var array
*/
private $spOffsets;
/**
* Shape types.
*
* @var array
*/
private $spTypes;
/**
* Constructor.
*
* @param mixed $object
*/
public function __construct($object)
{
$this->object = $object;
}
/**
* Process the object to be written.
*
* @return string
*/
public function close()
{
// initialize
$this->data = '';
switch (get_class($this->object)) {
case \PhpOffice\PhpSpreadsheet\Shared\Escher::class:
if ($dggContainer = $this->object->getDggContainer()) {
$writer = new self($dggContainer);
$this->data = $writer->close();
} elseif ($dgContainer = $this->object->getDgContainer()) {
$writer = new self($dgContainer);
$this->data = $writer->close();
$this->spOffsets = $writer->getSpOffsets();
$this->spTypes = $writer->getSpTypes();
}
break;
case DggContainer::class:
// this is a container record
// initialize
$innerData = '';
// write the dgg
$recVer = 0x0;
$recInstance = 0x0000;
$recType = 0xF006;
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
// dgg data
$dggData =
pack(
'VVVV',
$this->object->getSpIdMax(), // maximum shape identifier increased by one
$this->object->getCDgSaved() + 1, // number of file identifier clusters increased by one
$this->object->getCSpSaved(),
$this->object->getCDgSaved() // count total number of drawings saved
);
// add file identifier clusters (one per drawing)
$IDCLs = $this->object->getIDCLs();
foreach ($IDCLs as $dgId => $maxReducedSpId) {
$dggData .= pack('VV', $dgId, $maxReducedSpId + 1);
}
$header = pack('vvV', $recVerInstance, $recType, strlen($dggData));
$innerData .= $header . $dggData;
// write the bstoreContainer
if ($bstoreContainer = $this->object->getBstoreContainer()) {
$writer = new self($bstoreContainer);
$innerData .= $writer->close();
}
// write the record
$recVer = 0xF;
$recInstance = 0x0000;
$recType = 0xF000;
$length = strlen($innerData);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$this->data = $header . $innerData;
break;
case BstoreContainer::class:
// this is a container record
// initialize
$innerData = '';
// treat the inner data
if ($BSECollection = $this->object->getBSECollection()) {
foreach ($BSECollection as $BSE) {
$writer = new self($BSE);
$innerData .= $writer->close();
}
}
// write the record
$recVer = 0xF;
$recInstance = count($this->object->getBSECollection());
$recType = 0xF001;
$length = strlen($innerData);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$this->data = $header . $innerData;
break;
case BSE::class:
// this is a semi-container record
// initialize
$innerData = '';
// here we treat the inner data
if ($blip = $this->object->getBlip()) {
$writer = new self($blip);
$innerData .= $writer->close();
}
// initialize
$data = '';
$btWin32 = $this->object->getBlipType();
$btMacOS = $this->object->getBlipType();
$data .= pack('CC', $btWin32, $btMacOS);
$rgbUid = pack('VVVV', 0, 0, 0, 0); // todo
$data .= $rgbUid;
$tag = 0;
$size = strlen($innerData);
$cRef = 1;
$foDelay = 0; //todo
$unused1 = 0x0;
$cbName = 0x0;
$unused2 = 0x0;
$unused3 = 0x0;
$data .= pack('vVVVCCCC', $tag, $size, $cRef, $foDelay, $unused1, $cbName, $unused2, $unused3);
$data .= $innerData;
// write the record
$recVer = 0x2;
$recInstance = $this->object->getBlipType();
$recType = 0xF007;
$length = strlen($data);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$this->data = $header;
$this->data .= $data;
break;
case Blip::class:
// this is an atom record
// write the record
switch ($this->object->getParent()->getBlipType()) {
case BSE::BLIPTYPE_JPEG:
// initialize
$innerData = '';
$rgbUid1 = pack('VVVV', 0, 0, 0, 0); // todo
$innerData .= $rgbUid1;
$tag = 0xFF; // todo
$innerData .= pack('C', $tag);
$innerData .= $this->object->getData();
$recVer = 0x0;
$recInstance = 0x46A;
$recType = 0xF01D;
$length = strlen($innerData);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$this->data = $header;
$this->data .= $innerData;
break;
case BSE::BLIPTYPE_PNG:
// initialize
$innerData = '';
$rgbUid1 = pack('VVVV', 0, 0, 0, 0); // todo
$innerData .= $rgbUid1;
$tag = 0xFF; // todo
$innerData .= pack('C', $tag);
$innerData .= $this->object->getData();
$recVer = 0x0;
$recInstance = 0x6E0;
$recType = 0xF01E;
$length = strlen($innerData);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$this->data = $header;
$this->data .= $innerData;
break;
}
break;
case DgContainer::class:
// this is a container record
// initialize
$innerData = '';
// write the dg
$recVer = 0x0;
$recInstance = $this->object->getDgId();
$recType = 0xF008;
$length = 8;
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
// number of shapes in this drawing (including group shape)
$countShapes = count($this->object->getSpgrContainer()->getChildren());
$innerData .= $header . pack('VV', $countShapes, $this->object->getLastSpId());
// write the spgrContainer
if ($spgrContainer = $this->object->getSpgrContainer()) {
$writer = new self($spgrContainer);
$innerData .= $writer->close();
// get the shape offsets relative to the spgrContainer record
$spOffsets = $writer->getSpOffsets();
$spTypes = $writer->getSpTypes();
// save the shape offsets relative to dgContainer
foreach ($spOffsets as &$spOffset) {
$spOffset += 24; // add length of dgContainer header data (8 bytes) plus dg data (16 bytes)
}
$this->spOffsets = $spOffsets;
$this->spTypes = $spTypes;
}
// write the record
$recVer = 0xF;
$recInstance = 0x0000;
$recType = 0xF002;
$length = strlen($innerData);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$this->data = $header . $innerData;
break;
case SpgrContainer::class:
// this is a container record
// initialize
$innerData = '';
// initialize spape offsets
$totalSize = 8;
$spOffsets = [];
$spTypes = [];
// treat the inner data
foreach ($this->object->getChildren() as $spContainer) {
$writer = new self($spContainer);
$spData = $writer->close();
$innerData .= $spData;
// save the shape offsets (where new shape records begin)
$totalSize += strlen($spData);
$spOffsets[] = $totalSize;
$spTypes = array_merge($spTypes, $writer->getSpTypes());
}
// write the record
$recVer = 0xF;
$recInstance = 0x0000;
$recType = 0xF003;
$length = strlen($innerData);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$this->data = $header . $innerData;
$this->spOffsets = $spOffsets;
$this->spTypes = $spTypes;
break;
case SpContainer::class:
// initialize
$data = '';
// build the data
// write group shape record, if necessary?
if ($this->object->getSpgr()) {
$recVer = 0x1;
$recInstance = 0x0000;
$recType = 0xF009;
$length = 0x00000010;
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$data .= $header . pack('VVVV', 0, 0, 0, 0);
}
$this->spTypes[] = ($this->object->getSpType());
// write the shape record
$recVer = 0x2;
$recInstance = $this->object->getSpType(); // shape type
$recType = 0xF00A;
$length = 0x00000008;
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$data .= $header . pack('VV', $this->object->getSpId(), $this->object->getSpgr() ? 0x0005 : 0x0A00);
// the options
if ($this->object->getOPTCollection()) {
$optData = '';
$recVer = 0x3;
$recInstance = count($this->object->getOPTCollection());
$recType = 0xF00B;
foreach ($this->object->getOPTCollection() as $property => $value) {
$optData .= pack('vV', $property, $value);
}
$length = strlen($optData);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$data .= $header . $optData;
}
// the client anchor
if ($this->object->getStartCoordinates()) {
$clientAnchorData = '';
$recVer = 0x0;
$recInstance = 0x0;
$recType = 0xF010;
// start coordinates
[$column, $row] = Coordinate::coordinateFromString($this->object->getStartCoordinates());
$c1 = Coordinate::columnIndexFromString($column) - 1;
$r1 = $row - 1;
// start offsetX
$startOffsetX = $this->object->getStartOffsetX();
// start offsetY
$startOffsetY = $this->object->getStartOffsetY();
// end coordinates
[$column, $row] = Coordinate::coordinateFromString($this->object->getEndCoordinates());
$c2 = Coordinate::columnIndexFromString($column) - 1;
$r2 = $row - 1;
// end offsetX
$endOffsetX = $this->object->getEndOffsetX();
// end offsetY
$endOffsetY = $this->object->getEndOffsetY();
$clientAnchorData = pack('vvvvvvvvv', $this->object->getSpFlag(), $c1, $startOffsetX, $r1, $startOffsetY, $c2, $endOffsetX, $r2, $endOffsetY);
$length = strlen($clientAnchorData);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$data .= $header . $clientAnchorData;
}
// the client data, just empty for now
if (!$this->object->getSpgr()) {
$clientDataData = '';
$recVer = 0x0;
$recInstance = 0x0;
$recType = 0xF011;
$length = strlen($clientDataData);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$data .= $header . $clientDataData;
}
// write the record
$recVer = 0xF;
$recInstance = 0x0000;
$recType = 0xF004;
$length = strlen($data);
$recVerInstance = $recVer;
$recVerInstance |= $recInstance << 4;
$header = pack('vvV', $recVerInstance, $recType, $length);
$this->data = $header . $data;
break;
}
return $this->data;
}
/**
* Gets the shape offsets.
*
* @return array
*/
public function getSpOffsets()
{
return $this->spOffsets;
}
/**
* Gets the shape types.
*
* @return array
*/
public function getSpTypes()
{
return $this->spTypes;
}
}

View File

@@ -0,0 +1,147 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Xls;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
class Font
{
/**
* Color index.
*
* @var int
*/
private $colorIndex;
/**
* Font.
*
* @var \PhpOffice\PhpSpreadsheet\Style\Font
*/
private $font;
/**
* Constructor.
*/
public function __construct(\PhpOffice\PhpSpreadsheet\Style\Font $font)
{
$this->colorIndex = 0x7FFF;
$this->font = $font;
}
/**
* Set the color index.
*
* @param int $colorIndex
*/
public function setColorIndex($colorIndex): void
{
$this->colorIndex = $colorIndex;
}
/**
* Get font record data.
*
* @return string
*/
public function writeFont()
{
$font_outline = 0;
$font_shadow = 0;
$icv = $this->colorIndex; // Index to color palette
if ($this->font->getSuperscript()) {
$sss = 1;
} elseif ($this->font->getSubscript()) {
$sss = 2;
} else {
$sss = 0;
}
$bFamily = 0; // Font family
$bCharSet = \PhpOffice\PhpSpreadsheet\Shared\Font::getCharsetFromFontName($this->font->getName()); // Character set
$record = 0x31; // Record identifier
$reserved = 0x00; // Reserved
$grbit = 0x00; // Font attributes
if ($this->font->getItalic()) {
$grbit |= 0x02;
}
if ($this->font->getStrikethrough()) {
$grbit |= 0x08;
}
if ($font_outline) {
$grbit |= 0x10;
}
if ($font_shadow) {
$grbit |= 0x20;
}
$data = pack(
'vvvvvCCCC',
// Fontsize (in twips)
$this->font->getSize() * 20,
$grbit,
// Colour
$icv,
// Font weight
self::mapBold($this->font->getBold()),
// Superscript/Subscript
$sss,
self::mapUnderline($this->font->getUnderline()),
$bFamily,
$bCharSet,
$reserved
);
$data .= StringHelper::UTF8toBIFF8UnicodeShort($this->font->getName());
$length = strlen($data);
$header = pack('vv', $record, $length);
return $header . $data;
}
/**
* Map to BIFF5-BIFF8 codes for bold.
*
* @param bool $bold
*
* @return int
*/
private static function mapBold($bold)
{
if ($bold) {
return 0x2BC; // 700 = Bold font weight
}
return 0x190; // 400 = Normal font weight
}
/**
* Map of BIFF2-BIFF8 codes for underline styles.
*
* @var array of int
*/
private static $mapUnderline = [
\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_NONE => 0x00,
\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE => 0x01,
\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE => 0x02,
\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLEACCOUNTING => 0x21,
\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLEACCOUNTING => 0x22,
];
/**
* Map underline.
*
* @param string $underline
*
* @return int
*/
private static function mapUnderline($underline)
{
if (isset(self::$mapUnderline[$underline])) {
return self::$mapUnderline[$underline];
}
return 0x00;
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,548 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Xls;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Borders;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Protection;
use PhpOffice\PhpSpreadsheet\Style\Style;
// 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
// */
class Xf
{
/**
* Style XF or a cell XF ?
*
* @var bool
*/
private $isStyleXf;
/**
* Index to the FONT record. Index 4 does not exist.
*
* @var int
*/
private $fontIndex;
/**
* An index (2 bytes) to a FORMAT record (number format).
*
* @var int
*/
private $numberFormatIndex;
/**
* 1 bit, apparently not used.
*
* @var int
*/
private $textJustLast;
/**
* The cell's foreground color.
*
* @var int
*/
private $foregroundColor;
/**
* The cell's background color.
*
* @var int
*/
private $backgroundColor;
/**
* Color of the bottom border of the cell.
*
* @var int
*/
private $bottomBorderColor;
/**
* Color of the top border of the cell.
*
* @var int
*/
private $topBorderColor;
/**
* Color of the left border of the cell.
*
* @var int
*/
private $leftBorderColor;
/**
* Color of the right border of the cell.
*
* @var int
*/
private $rightBorderColor;
/**
* Constructor.
*
* @param Style $style The XF format
*/
public function __construct(Style $style)
{
$this->isStyleXf = false;
$this->fontIndex = 0;
$this->numberFormatIndex = 0;
$this->textJustLast = 0;
$this->foregroundColor = 0x40;
$this->backgroundColor = 0x41;
$this->_diag = 0;
$this->bottomBorderColor = 0x40;
$this->topBorderColor = 0x40;
$this->leftBorderColor = 0x40;
$this->rightBorderColor = 0x40;
$this->_diag_color = 0x40;
$this->_style = $style;
}
/**
* Generate an Excel BIFF XF record (style or cell).
*
* @return string The XF record
*/
public function writeXf()
{
// Set the type of the XF record and some of the attributes.
if ($this->isStyleXf) {
$style = 0xFFF5;
} else {
$style = self::mapLocked($this->_style->getProtection()->getLocked());
$style |= self::mapHidden($this->_style->getProtection()->getHidden()) << 1;
}
// Flags to indicate if attributes have been set.
$atr_num = ($this->numberFormatIndex != 0) ? 1 : 0;
$atr_fnt = ($this->fontIndex != 0) ? 1 : 0;
$atr_alc = ((int) $this->_style->getAlignment()->getWrapText()) ? 1 : 0;
$atr_bdr = (self::mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) ||
self::mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
self::mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
self::mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle())) ? 1 : 0;
$atr_pat = (($this->foregroundColor != 0x40) ||
($this->backgroundColor != 0x41) ||
self::mapFillType($this->_style->getFill()->getFillType())) ? 1 : 0;
$atr_prot = self::mapLocked($this->_style->getProtection()->getLocked())
| self::mapHidden($this->_style->getProtection()->getHidden());
// Zero the default border colour if the border has not been set.
if (self::mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) {
$this->bottomBorderColor = 0;
}
if (self::mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) {
$this->topBorderColor = 0;
}
if (self::mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) {
$this->rightBorderColor = 0;
}
if (self::mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) {
$this->leftBorderColor = 0;
}
if (self::mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) == 0) {
$this->_diag_color = 0;
}
$record = 0x00E0; // Record identifier
$length = 0x0014; // Number of bytes to follow
$ifnt = $this->fontIndex; // Index to FONT record
$ifmt = $this->numberFormatIndex; // Index to FORMAT record
$align = $this->mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
$align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
$align |= self::mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
$align |= $this->textJustLast << 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->foregroundColor; // fg and bg pattern colors
$icv |= $this->backgroundColor << 7;
$border1 = self::mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color
$border1 |= self::mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4;
$border1 |= self::mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8;
$border1 |= self::mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12;
$border1 |= $this->leftBorderColor << 16;
$border1 |= $this->rightBorderColor << 23;
$diagonalDirection = $this->_style->getBorders()->getDiagonalDirection();
$diag_tl_to_rb = $diagonalDirection == Borders::DIAGONAL_BOTH
|| $diagonalDirection == Borders::DIAGONAL_DOWN;
$diag_tr_to_lb = $diagonalDirection == Borders::DIAGONAL_BOTH
|| $diagonalDirection == Borders::DIAGONAL_UP;
$border1 |= $diag_tl_to_rb << 30;
$border1 |= $diag_tr_to_lb << 31;
$border2 = $this->topBorderColor; // Border color
$border2 |= $this->bottomBorderColor << 7;
$border2 |= $this->_diag_color << 14;
$border2 |= self::mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21;
$border2 |= self::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);
$data .= pack('CCC', self::mapTextRotation($this->_style->getAlignment()->getTextRotation()), $biff8_options, $used_attrib);
$data .= pack('VVv', $border1, $border2, $icv);
return $header . $data;
}
/**
* Is this a style XF ?
*
* @param bool $value
*/
public function setIsStyleXf($value): void
{
$this->isStyleXf = $value;
}
/**
* Sets the cell's bottom border color.
*
* @param int $colorIndex Color index
*/
public function setBottomColor($colorIndex): void
{
$this->bottomBorderColor = $colorIndex;
}
/**
* Sets the cell's top border color.
*
* @param int $colorIndex Color index
*/
public function setTopColor($colorIndex): void
{
$this->topBorderColor = $colorIndex;
}
/**
* Sets the cell's left border color.
*
* @param int $colorIndex Color index
*/
public function setLeftColor($colorIndex): void
{
$this->leftBorderColor = $colorIndex;
}
/**
* Sets the cell's right border color.
*
* @param int $colorIndex Color index
*/
public function setRightColor($colorIndex): void
{
$this->rightBorderColor = $colorIndex;
}
/**
* Sets the cell's diagonal border color.
*
* @param int $colorIndex Color index
*/
public function setDiagColor($colorIndex): void
{
$this->_diag_color = $colorIndex;
}
/**
* Sets the cell's foreground color.
*
* @param int $colorIndex Color index
*/
public function setFgColor($colorIndex): void
{
$this->foregroundColor = $colorIndex;
}
/**
* Sets the cell's background color.
*
* @param int $colorIndex Color index
*/
public function setBgColor($colorIndex): void
{
$this->backgroundColor = $colorIndex;
}
/**
* Sets the index to the number format record
* It can be date, time, currency, etc...
*
* @param int $numberFormatIndex Index to format record
*/
public function setNumberFormatIndex($numberFormatIndex): void
{
$this->numberFormatIndex = $numberFormatIndex;
}
/**
* Set the font index.
*
* @param int $value Font index, note that value 4 does not exist
*/
public function setFontIndex($value): void
{
$this->fontIndex = $value;
}
/**
* Map of BIFF2-BIFF8 codes for border styles.
*
* @var array of int
*/
private static $mapBorderStyles = [
Border::BORDER_NONE => 0x00,
Border::BORDER_THIN => 0x01,
Border::BORDER_MEDIUM => 0x02,
Border::BORDER_DASHED => 0x03,
Border::BORDER_DOTTED => 0x04,
Border::BORDER_THICK => 0x05,
Border::BORDER_DOUBLE => 0x06,
Border::BORDER_HAIR => 0x07,
Border::BORDER_MEDIUMDASHED => 0x08,
Border::BORDER_DASHDOT => 0x09,
Border::BORDER_MEDIUMDASHDOT => 0x0A,
Border::BORDER_DASHDOTDOT => 0x0B,
Border::BORDER_MEDIUMDASHDOTDOT => 0x0C,
Border::BORDER_SLANTDASHDOT => 0x0D,
];
/**
* Map border style.
*
* @param string $borderStyle
*
* @return int
*/
private static function mapBorderStyle($borderStyle)
{
if (isset(self::$mapBorderStyles[$borderStyle])) {
return self::$mapBorderStyles[$borderStyle];
}
return 0x00;
}
/**
* Map of BIFF2-BIFF8 codes for fill types.
*
* @var array of int
*/
private static $mapFillTypes = [
Fill::FILL_NONE => 0x00,
Fill::FILL_SOLID => 0x01,
Fill::FILL_PATTERN_MEDIUMGRAY => 0x02,
Fill::FILL_PATTERN_DARKGRAY => 0x03,
Fill::FILL_PATTERN_LIGHTGRAY => 0x04,
Fill::FILL_PATTERN_DARKHORIZONTAL => 0x05,
Fill::FILL_PATTERN_DARKVERTICAL => 0x06,
Fill::FILL_PATTERN_DARKDOWN => 0x07,
Fill::FILL_PATTERN_DARKUP => 0x08,
Fill::FILL_PATTERN_DARKGRID => 0x09,
Fill::FILL_PATTERN_DARKTRELLIS => 0x0A,
Fill::FILL_PATTERN_LIGHTHORIZONTAL => 0x0B,
Fill::FILL_PATTERN_LIGHTVERTICAL => 0x0C,
Fill::FILL_PATTERN_LIGHTDOWN => 0x0D,
Fill::FILL_PATTERN_LIGHTUP => 0x0E,
Fill::FILL_PATTERN_LIGHTGRID => 0x0F,
Fill::FILL_PATTERN_LIGHTTRELLIS => 0x10,
Fill::FILL_PATTERN_GRAY125 => 0x11,
Fill::FILL_PATTERN_GRAY0625 => 0x12,
Fill::FILL_GRADIENT_LINEAR => 0x00, // does not exist in BIFF8
Fill::FILL_GRADIENT_PATH => 0x00, // does not exist in BIFF8
];
/**
* Map fill type.
*
* @param string $fillType
*
* @return int
*/
private static function mapFillType($fillType)
{
if (isset(self::$mapFillTypes[$fillType])) {
return self::$mapFillTypes[$fillType];
}
return 0x00;
}
/**
* Map of BIFF2-BIFF8 codes for horizontal alignment.
*
* @var array of int
*/
private static $mapHAlignments = [
Alignment::HORIZONTAL_GENERAL => 0,
Alignment::HORIZONTAL_LEFT => 1,
Alignment::HORIZONTAL_CENTER => 2,
Alignment::HORIZONTAL_RIGHT => 3,
Alignment::HORIZONTAL_FILL => 4,
Alignment::HORIZONTAL_JUSTIFY => 5,
Alignment::HORIZONTAL_CENTER_CONTINUOUS => 6,
];
/**
* Map to BIFF2-BIFF8 codes for horizontal alignment.
*
* @param string $hAlign
*
* @return int
*/
private function mapHAlign($hAlign)
{
if (isset(self::$mapHAlignments[$hAlign])) {
return self::$mapHAlignments[$hAlign];
}
return 0;
}
/**
* Map of BIFF2-BIFF8 codes for vertical alignment.
*
* @var array of int
*/
private static $mapVAlignments = [
Alignment::VERTICAL_TOP => 0,
Alignment::VERTICAL_CENTER => 1,
Alignment::VERTICAL_BOTTOM => 2,
Alignment::VERTICAL_JUSTIFY => 3,
];
/**
* Map to BIFF2-BIFF8 codes for vertical alignment.
*
* @param string $vAlign
*
* @return int
*/
private static function mapVAlign($vAlign)
{
if (isset(self::$mapVAlignments[$vAlign])) {
return self::$mapVAlignments[$vAlign];
}
return 2;
}
/**
* Map to BIFF8 codes for text rotation angle.
*
* @param int $textRotation
*
* @return int
*/
private static function mapTextRotation($textRotation)
{
if ($textRotation >= 0) {
return $textRotation;
} elseif ($textRotation == -165) {
return 255;
} elseif ($textRotation < 0) {
return 90 - $textRotation;
}
}
/**
* Map locked.
*
* @param string $locked
*
* @return int
*/
private static function mapLocked($locked)
{
switch ($locked) {
case Protection::PROTECTION_INHERIT:
return 1;
case Protection::PROTECTION_PROTECTED:
return 1;
case Protection::PROTECTION_UNPROTECTED:
return 0;
default:
return 1;
}
}
/**
* Map hidden.
*
* @param string $hidden
*
* @return int
*/
private static function mapHidden($hidden)
{
switch ($hidden) {
case Protection::PROTECTION_INHERIT:
return 0;
case Protection::PROTECTION_PROTECTED:
return 1;
case Protection::PROTECTION_UNPROTECTED:
return 0;
default:
return 0;
}
}
}