123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- <?php
- class PHPExcel_Writer_Excel5_Xf
- {
-
- private $isStyleXf;
-
- private $fontIndex;
-
- private $numberFormatIndex;
-
- private $textJustLast;
-
- private $foregroundColor;
-
- private $backgroundColor;
-
- private $bottomBorderColor;
-
- private $topBorderColor;
-
- private $leftBorderColor;
-
- private $rightBorderColor;
-
- public function __construct(PHPExcel_Style $style = null)
- {
- $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;
- }
-
- public function writeXf()
- {
-
- if ($this->isStyleXf) {
- $style = 0xFFF5;
- } else {
- $style = self::mapLocked($this->_style->getProtection()->getLocked());
- $style |= self::mapHidden($this->_style->getProtection()->getHidden()) << 1;
- }
-
- $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());
-
- 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;
- $length = 0x0014;
- $ifnt = $this->fontIndex;
- $ifmt = $this->numberFormatIndex;
- $align = $this->mapHAlign($this->_style->getAlignment()->getHorizontal());
- $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;
- $icv |= $this->backgroundColor << 7;
- $border1 = self::mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle());
- $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 == PHPExcel_Style_Borders::DIAGONAL_BOTH
- || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_DOWN;
- $diag_tr_to_lb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
- || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_UP;
- $border1 |= $diag_tl_to_rb << 30;
- $border1 |= $diag_tr_to_lb << 31;
- $border2 = $this->topBorderColor;
- $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 = $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);
- }
-
- public function setIsStyleXf($value)
- {
- $this->isStyleXf = $value;
- }
-
- public function setBottomColor($colorIndex)
- {
- $this->bottomBorderColor = $colorIndex;
- }
-
- public function setTopColor($colorIndex)
- {
- $this->topBorderColor = $colorIndex;
- }
-
- public function setLeftColor($colorIndex)
- {
- $this->leftBorderColor = $colorIndex;
- }
-
- public function setRightColor($colorIndex)
- {
- $this->rightBorderColor = $colorIndex;
- }
-
- public function setDiagColor($colorIndex)
- {
- $this->_diag_color = $colorIndex;
- }
-
- public function setFgColor($colorIndex)
- {
- $this->foregroundColor = $colorIndex;
- }
-
- public function setBgColor($colorIndex)
- {
- $this->backgroundColor = $colorIndex;
- }
-
- public function setNumberFormatIndex($numberFormatIndex)
- {
- $this->numberFormatIndex = $numberFormatIndex;
- }
-
- public function setFontIndex($value)
- {
- $this->fontIndex = $value;
- }
-
- private static $mapBorderStyles = array(
- PHPExcel_Style_Border::BORDER_NONE => 0x00,
- PHPExcel_Style_Border::BORDER_THIN => 0x01,
- PHPExcel_Style_Border::BORDER_MEDIUM => 0x02,
- PHPExcel_Style_Border::BORDER_DASHED => 0x03,
- PHPExcel_Style_Border::BORDER_DOTTED => 0x04,
- PHPExcel_Style_Border::BORDER_THICK => 0x05,
- PHPExcel_Style_Border::BORDER_DOUBLE => 0x06,
- PHPExcel_Style_Border::BORDER_HAIR => 0x07,
- PHPExcel_Style_Border::BORDER_MEDIUMDASHED => 0x08,
- PHPExcel_Style_Border::BORDER_DASHDOT => 0x09,
- PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT => 0x0A,
- PHPExcel_Style_Border::BORDER_DASHDOTDOT => 0x0B,
- PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT => 0x0C,
- PHPExcel_Style_Border::BORDER_SLANTDASHDOT => 0x0D,
- );
-
- private static function mapBorderStyle($borderStyle)
- {
- if (isset(self::$mapBorderStyles[$borderStyle])) {
- return self::$mapBorderStyles[$borderStyle];
- }
- return 0x00;
- }
-
- private static $mapFillTypes = array(
- PHPExcel_Style_Fill::FILL_NONE => 0x00,
- PHPExcel_Style_Fill::FILL_SOLID => 0x01,
- PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY => 0x02,
- PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY => 0x03,
- PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY => 0x04,
- PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL => 0x05,
- PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL => 0x06,
- PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN => 0x07,
- PHPExcel_Style_Fill::FILL_PATTERN_DARKUP => 0x08,
- PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID => 0x09,
- PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS => 0x0A,
- PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL => 0x0B,
- PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL => 0x0C,
- PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN => 0x0D,
- PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP => 0x0E,
- PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID => 0x0F,
- PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS => 0x10,
- PHPExcel_Style_Fill::FILL_PATTERN_GRAY125 => 0x11,
- PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625 => 0x12,
- PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR => 0x00,
- PHPExcel_Style_Fill::FILL_GRADIENT_PATH => 0x00,
- );
-
- private static function mapFillType($fillType)
- {
- if (isset(self::$mapFillTypes[$fillType])) {
- return self::$mapFillTypes[$fillType];
- }
- return 0x00;
- }
-
- private static $mapHAlignments = array(
- PHPExcel_Style_Alignment::HORIZONTAL_GENERAL => 0,
- PHPExcel_Style_Alignment::HORIZONTAL_LEFT => 1,
- PHPExcel_Style_Alignment::HORIZONTAL_CENTER => 2,
- PHPExcel_Style_Alignment::HORIZONTAL_RIGHT => 3,
- PHPExcel_Style_Alignment::HORIZONTAL_FILL => 4,
- PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY => 5,
- PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS => 6,
- );
-
- private function mapHAlign($hAlign)
- {
- if (isset(self::$mapHAlignments[$hAlign])) {
- return self::$mapHAlignments[$hAlign];
- }
- return 0;
- }
-
- private static $mapVAlignments = array(
- PHPExcel_Style_Alignment::VERTICAL_TOP => 0,
- PHPExcel_Style_Alignment::VERTICAL_CENTER => 1,
- PHPExcel_Style_Alignment::VERTICAL_BOTTOM => 2,
- PHPExcel_Style_Alignment::VERTICAL_JUSTIFY => 3,
- );
-
- private static function mapVAlign($vAlign)
- {
- if (isset(self::$mapVAlignments[$vAlign])) {
- return self::$mapVAlignments[$vAlign];
- }
- return 2;
- }
-
- private static function mapTextRotation($textRotation)
- {
- if ($textRotation >= 0) {
- return $textRotation;
- } elseif ($textRotation == -165) {
- return 255;
- } elseif ($textRotation < 0) {
- return 90 - $textRotation;
- }
- }
-
- private static function mapLocked($locked)
- {
- switch ($locked) {
- case PHPExcel_Style_Protection::PROTECTION_INHERIT:
- return 1;
- case PHPExcel_Style_Protection::PROTECTION_PROTECTED:
- return 1;
- case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED:
- return 0;
- default:
- return 1;
- }
- }
-
- private static function mapHidden($hidden)
- {
- switch ($hidden) {
- case PHPExcel_Style_Protection::PROTECTION_INHERIT:
- return 0;
- case PHPExcel_Style_Protection::PROTECTION_PROTECTED:
- return 1;
- case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED:
- return 0;
- default:
- return 0;
- }
- }
- }
|