ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Xf.php
Go to the documentation of this file.
1 <?php
28 // Original file header of PEAR::Spreadsheet_Excel_Writer_Format (used as the base for this class):
29 // -----------------------------------------------------------------------------------------
30 // /*
31 // * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
32 // *
33 // * The majority of this is _NOT_ my code. I simply ported it from the
34 // * PERL Spreadsheet::WriteExcel module.
35 // *
36 // * The author of the Spreadsheet::WriteExcel module is John McNamara
37 // * <jmcnamara@cpan.org>
38 // *
39 // * I _DO_ maintain this code, and John McNamara has nothing to do with the
40 // * porting of this code to PHP. Any questions directly related to this
41 // * class library should be directed to me.
42 // *
43 // * License Information:
44 // *
45 // * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
46 // * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
47 // *
48 // * This library is free software; you can redistribute it and/or
49 // * modify it under the terms of the GNU Lesser General Public
50 // * License as published by the Free Software Foundation; either
51 // * version 2.1 of the License, or (at your option) any later version.
52 // *
53 // * This library is distributed in the hope that it will be useful,
54 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
56 // * Lesser General Public License for more details.
57 // *
58 // * You should have received a copy of the GNU Lesser General Public
59 // * License along with this library; if not, write to the Free Software
60 // * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
61 // */
62 
63 
72 {
78  private $_isStyleXf;
79 
84  private $_fontIndex;
85 
91 
97 
102  public $_fg_color;
103 
108  public $_bg_color;
109 
115 
120  public $_top_color;
121 
126  public $_left_color;
127 
133 
140  public function __construct(PHPExcel_Style $style = null)
141  {
142  $this->_isStyleXf = false;
143  $this->_fontIndex = 0;
144 
145  $this->_numberFormatIndex = 0;
146 
147  $this->_text_justlast = 0;
148 
149  $this->_fg_color = 0x40;
150  $this->_bg_color = 0x41;
151 
152  $this->_diag = 0;
153 
154  $this->_bottom_color = 0x40;
155  $this->_top_color = 0x40;
156  $this->_left_color = 0x40;
157  $this->_right_color = 0x40;
158  $this->_diag_color = 0x40;
159  $this->_style = $style;
160 
161  }
162 
163 
169  function writeXf()
170  {
171  // Set the type of the XF record and some of the attributes.
172  if ($this->_isStyleXf) {
173  $style = 0xFFF5;
174  } else {
175  $style = self::_mapLocked($this->_style->getProtection()->getLocked());
176  $style |= self::_mapHidden($this->_style->getProtection()->getHidden()) << 1;
177  }
178 
179  // Flags to indicate if attributes have been set.
180  $atr_num = ($this->_numberFormatIndex != 0)?1:0;
181  $atr_fnt = ($this->_fontIndex != 0)?1:0;
182  $atr_alc = ((int) $this->_style->getAlignment()->getWrapText()) ? 1 : 0;
183  $atr_bdr = (self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) ||
184  self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
185  self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
186  self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0;
187  $atr_pat = (($this->_fg_color != 0x40) ||
188  ($this->_bg_color != 0x41) ||
189  self::_mapFillType($this->_style->getFill()->getFillType()))?1:0;
190  $atr_prot = self::_mapLocked($this->_style->getProtection()->getLocked())
191  | self::_mapHidden($this->_style->getProtection()->getHidden());
192 
193  // Zero the default border colour if the border has not been set.
194  if (self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) {
195  $this->_bottom_color = 0;
196  }
197  if (self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) {
198  $this->_top_color = 0;
199  }
200  if (self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) {
201  $this->_right_color = 0;
202  }
203  if (self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) {
204  $this->_left_color = 0;
205  }
206  if (self::_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) == 0) {
207  $this->_diag_color = 0;
208  }
209 
210  $record = 0x00E0; // Record identifier
211  $length = 0x0014; // Number of bytes to follow
212 
213  $ifnt = $this->_fontIndex; // Index to FONT record
214  $ifmt = $this->_numberFormatIndex; // Index to FORMAT record
215 
216  $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
217  $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
218  $align |= self::_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
219  $align |= $this->_text_justlast << 7;
220 
221  $used_attrib = $atr_num << 2;
222  $used_attrib |= $atr_fnt << 3;
223  $used_attrib |= $atr_alc << 4;
224  $used_attrib |= $atr_bdr << 5;
225  $used_attrib |= $atr_pat << 6;
226  $used_attrib |= $atr_prot << 7;
227 
228  $icv = $this->_fg_color; // fg and bg pattern colors
229  $icv |= $this->_bg_color << 7;
230 
231  $border1 = self::_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color
232  $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4;
233  $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8;
234  $border1 |= self::_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12;
235  $border1 |= $this->_left_color << 16;
236  $border1 |= $this->_right_color << 23;
237 
238  $diagonalDirection = $this->_style->getBorders()->getDiagonalDirection();
239  $diag_tl_to_rb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
240  || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_DOWN;
241  $diag_tr_to_lb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
242  || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_UP;
243  $border1 |= $diag_tl_to_rb << 30;
244  $border1 |= $diag_tr_to_lb << 31;
245 
246  $border2 = $this->_top_color; // Border color
247  $border2 |= $this->_bottom_color << 7;
248  $border2 |= $this->_diag_color << 14;
249  $border2 |= self::_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21;
250  $border2 |= self::_mapFillType($this->_style->getFill()->getFillType()) << 26;
251 
252  $header = pack("vv", $record, $length);
253 
254  //BIFF8 options: identation, shrinkToFit and text direction
255  $biff8_options = $this->_style->getAlignment()->getIndent();
256  $biff8_options |= (int) $this->_style->getAlignment()->getShrinkToFit() << 4;
257 
258  $data = pack("vvvC", $ifnt, $ifmt, $style, $align);
259  $data .= pack("CCC"
260  , self::_mapTextRotation($this->_style->getAlignment()->getTextRotation())
261  , $biff8_options
262  , $used_attrib
263  );
264  $data .= pack("VVv", $border1, $border2, $icv);
265 
266  return($header . $data);
267  }
268 
274  public function setIsStyleXf($value)
275  {
276  $this->_isStyleXf = $value;
277  }
278 
285  function setBottomColor($colorIndex)
286  {
287  $this->_bottom_color = $colorIndex;
288  }
289 
296  function setTopColor($colorIndex)
297  {
298  $this->_top_color = $colorIndex;
299  }
300 
307  function setLeftColor($colorIndex)
308  {
309  $this->_left_color = $colorIndex;
310  }
311 
318  function setRightColor($colorIndex)
319  {
320  $this->_right_color = $colorIndex;
321  }
322 
329  function setDiagColor($colorIndex)
330  {
331  $this->_diag_color = $colorIndex;
332  }
333 
334 
341  function setFgColor($colorIndex)
342  {
343  $this->_fg_color = $colorIndex;
344  }
345 
352  function setBgColor($colorIndex)
353  {
354  $this->_bg_color = $colorIndex;
355  }
356 
364  function setNumberFormatIndex($numberFormatIndex)
365  {
366  $this->_numberFormatIndex = $numberFormatIndex;
367  }
368 
374  public function setFontIndex($value)
375  {
376  $this->_fontIndex = $value;
377  }
378 
398  );
399 
406  private static function _mapBorderStyle($borderStyle) {
407  if (isset(self::$_mapBorderStyle[$borderStyle]))
408  return self::$_mapBorderStyle[$borderStyle];
409  return 0x00;
410  }
411 
436  PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR => 0x00, // does not exist in BIFF8
437  PHPExcel_Style_Fill::FILL_GRADIENT_PATH => 0x00, // does not exist in BIFF8
438  );
445  private static function _mapFillType($fillType) {
446  if (isset(self::$_mapFillType[$fillType]))
447  return self::$_mapFillType[$fillType];
448  return 0x00;
449  }
450 
463  );
470  private function _mapHAlign($hAlign)
471  {
472  if (isset(self::$_mapHAlign[$hAlign]))
473  return self::$_mapHAlign[$hAlign];
474  return 0;
475  }
476 
486  );
493  private static function _mapVAlign($vAlign) {
494  if (isset(self::$_mapVAlign[$vAlign]))
495  return self::$_mapVAlign[$vAlign];
496  return 2;
497  }
498 
505  private static function _mapTextRotation($textRotation) {
506  if ($textRotation >= 0) {
507  return $textRotation;
508  }
509  if ($textRotation == -165) {
510  return 255;
511  }
512  if ($textRotation < 0) {
513  return 90 - $textRotation;
514  }
515  }
516 
523  private static function _mapLocked($locked) {
524  switch ($locked) {
528  default: return 1;
529  }
530  }
531 
538  private static function _mapHidden($hidden) {
539  switch ($hidden) {
543  default: return 0;
544  }
545  }
546 
547 }
static _mapLocked($locked)
Map locked.
Definition: Xf.php:523
const FILL_NONE
Definition: Fill.php:39
const FILL_PATTERN_LIGHTHORIZONTAL
Definition: Fill.php:55
$style
Definition: example_012.php:70
const FILL_GRADIENT_PATH
Definition: Fill.php:42
const FILL_PATTERN_LIGHTGRAY
Definition: Fill.php:53
setFgColor($colorIndex)
Sets the cell&#39;s foreground color.
Definition: Xf.php:341
const FILL_PATTERN_DARKGRID
Definition: Fill.php:45
setRightColor($colorIndex)
Sets the cell&#39;s right border color.
Definition: Xf.php:318
const FILL_PATTERN_DARKHORIZONTAL
Definition: Fill.php:46
const BORDER_MEDIUMDASHDOT
Definition: Border.php:47
writeXf()
Generate an Excel BIFF XF record (style or cell).
Definition: Xf.php:169
const BORDER_DASHDOTDOT
Definition: Border.php:41
setBottomColor($colorIndex)
Sets the cell&#39;s bottom border color.
Definition: Xf.php:285
static _mapFillType($fillType)
Map fill type.
Definition: Xf.php:445
setIsStyleXf($value)
Is this a style XF ?
Definition: Xf.php:274
const FILL_PATTERN_LIGHTTRELLIS
Definition: Fill.php:56
static $_mapBorderStyle
Map of BIFF2-BIFF8 codes for border styles.
Definition: Xf.php:384
static $_mapVAlign
Map of BIFF2-BIFF8 codes for vertical alignment.
Definition: Xf.php:482
setDiagColor($colorIndex)
Sets the cell&#39;s diagonal border color.
Definition: Xf.php:329
static $_mapFillType
Map of BIFF2-BIFF8 codes for fill types.
Definition: Xf.php:417
const HORIZONTAL_CENTER_CONTINUOUS
Definition: Alignment.php:43
const FILL_PATTERN_LIGHTDOWN
Definition: Fill.php:52
$header
static $_mapHAlign
Map of BIFF2-BIFF8 codes for horizontal alignment.
Definition: Xf.php:456
static _mapTextRotation($textRotation)
Map to BIFF8 codes for text rotation angle.
Definition: Xf.php:505
const FILL_PATTERN_MEDIUMGRAY
Definition: Fill.php:59
const PROTECTION_INHERIT
Protection styles.
Definition: Protection.php:39
static _mapHidden($hidden)
Map hidden.
Definition: Xf.php:538
const FILL_PATTERN_DARKDOWN
Definition: Fill.php:43
const FILL_PATTERN_GRAY125
Definition: Fill.php:51
const FILL_PATTERN_DARKTRELLIS
Definition: Fill.php:47
const FILL_PATTERN_LIGHTVERTICAL
Definition: Fill.php:58
const BORDER_MEDIUMDASHDOTDOT
Definition: Border.php:48
Create styles array
The data for the language used.
const FILL_PATTERN_GRAY0625
Definition: Fill.php:50
setTopColor($colorIndex)
Sets the cell&#39;s top border color.
Definition: Xf.php:296
const FILL_PATTERN_LIGHTGRID
Definition: Fill.php:54
__construct(PHPExcel_Style $style=null)
Constructor.
Definition: Xf.php:140
const BORDER_SLANTDASHDOT
Definition: Border.php:50
const FILL_PATTERN_DARKVERTICAL
Definition: Fill.php:49
static _mapBorderStyle($borderStyle)
Map border style.
Definition: Xf.php:406
const FILL_SOLID
Definition: Fill.php:40
const FILL_PATTERN_DARKGRAY
Definition: Fill.php:44
setLeftColor($colorIndex)
Sets the cell&#39;s left border color.
Definition: Xf.php:307
const FILL_PATTERN_LIGHTUP
Definition: Fill.php:57
setFontIndex($value)
Set the font index.
Definition: Xf.php:374
const FILL_PATTERN_DARKUP
Definition: Fill.php:48
const BORDER_MEDIUMDASHED
Definition: Border.php:49
setNumberFormatIndex($numberFormatIndex)
Sets the index to the number format record It can be date, time, currency, etc... ...
Definition: Xf.php:364
static _mapVAlign($vAlign)
Map to BIFF2-BIFF8 codes for vertical alignment.
Definition: Xf.php:493
setBgColor($colorIndex)
Sets the cell&#39;s background color.
Definition: Xf.php:352
_mapHAlign($hAlign)
Map to BIFF2-BIFF8 codes for horizontal alignment.
Definition: Xf.php:470
const FILL_GRADIENT_LINEAR
Definition: Fill.php:41