ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 $_BIFFVersion;
79 
85  private $_isStyleXf;
86 
91  private $_fontIndex;
92 
98 
104 
109  public $_fg_color;
110 
115  public $_bg_color;
116 
122 
127  public $_top_color;
128 
133  public $_left_color;
134 
140 
148  public function __construct(PHPExcel_Style $style = null)
149  {
150  $this->_isStyleXf = false;
151  $this->_BIFFVersion = 0x0600;
152  $this->_fontIndex = 0;
153 
154  $this->_numberFormatIndex = 0;
155 
156  $this->_text_justlast = 0;
157 
158  $this->_fg_color = 0x40;
159  $this->_bg_color = 0x41;
160 
161  $this->_diag = 0;
162 
163  $this->_bottom_color = 0x40;
164  $this->_top_color = 0x40;
165  $this->_left_color = 0x40;
166  $this->_right_color = 0x40;
167  $this->_diag_color = 0x40;
168  $this->_style = $style;
169 
170  }
171 
172 
179  function writeXf()
180  {
181  // Set the type of the XF record and some of the attributes.
182  if ($this->_isStyleXf) {
183  $style = 0xFFF5;
184  } else {
185  $style = $this->_mapLocked($this->_style->getProtection()->getLocked());
186  $style |= $this->_mapHidden($this->_style->getProtection()->getHidden()) << 1;
187  }
188 
189  // Flags to indicate if attributes have been set.
190  $atr_num = ($this->_numberFormatIndex != 0)?1:0;
191  $atr_fnt = ($this->_fontIndex != 0)?1:0;
192  $atr_alc = ((int) $this->_style->getAlignment()->getWrapText())?1:0;
193  $atr_bdr = ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) ||
194  $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
195  $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
196  $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0;
197  $atr_pat = (($this->_fg_color != 0x40) ||
198  ($this->_bg_color != 0x41) ||
199  $this->_mapFillType($this->_style->getFill()->getFillType()))?1:0;
200  $atr_prot = $this->_mapLocked($this->_style->getProtection()->getLocked())
201  | $this->_mapHidden($this->_style->getProtection()->getHidden());
202 
203  // Zero the default border colour if the border has not been set.
204  if ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) {
205  $this->_bottom_color = 0;
206  }
207  if ($this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) {
208  $this->_top_color = 0;
209  }
210  if ($this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) {
211  $this->_right_color = 0;
212  }
213  if ($this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) {
214  $this->_left_color = 0;
215  }
216  if ($this->_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) == 0) {
217  $this->_diag_color = 0;
218  }
219 
220  $record = 0x00E0; // Record identifier
221  if ($this->_BIFFVersion == 0x0500) {
222  $length = 0x0010; // Number of bytes to follow
223  }
224  if ($this->_BIFFVersion == 0x0600) {
225  $length = 0x0014;
226  }
227 
228  $ifnt = $this->_fontIndex; // Index to FONT record
229  $ifmt = $this->_numberFormatIndex; // Index to FORMAT record
230  if ($this->_BIFFVersion == 0x0500) {
231  $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
232  $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
233  $align |= $this->_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
234  $align |= $this->_text_justlast << 7;
235  $align |= 0 << 8; // rotation
236  $align |= $atr_num << 10;
237  $align |= $atr_fnt << 11;
238  $align |= $atr_alc << 12;
239  $align |= $atr_bdr << 13;
240  $align |= $atr_pat << 14;
241  $align |= $atr_prot << 15;
242 
243  $icv = $this->_fg_color; // fg and bg pattern colors
244  $icv |= $this->_bg_color << 7;
245 
246  $fill = $this->_mapFillType($this->_style->getFill()->getFillType()); // Fill and border line style
247  $fill |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 6;
248  $fill |= $this->_bottom_color << 9;
249 
250  $border1 = $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()); // Border line style and color
251  $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) << 3;
252  $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 6;
253  $border1 |= $this->_top_color << 9;
254 
255  $border2 = $this->_left_color; // Border color
256  $border2 |= $this->_right_color << 7;
257 
258  $header = pack("vv", $record, $length);
259  $data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
260  $icv, $fill,
261  $border1, $border2);
262  } elseif ($this->_BIFFVersion == 0x0600) {
263  $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
264  $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
265  $align |= $this->_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
266  $align |= $this->_text_justlast << 7;
267 
268  $used_attrib = $atr_num << 2;
269  $used_attrib |= $atr_fnt << 3;
270  $used_attrib |= $atr_alc << 4;
271  $used_attrib |= $atr_bdr << 5;
272  $used_attrib |= $atr_pat << 6;
273  $used_attrib |= $atr_prot << 7;
274 
275  $icv = $this->_fg_color; // fg and bg pattern colors
276  $icv |= $this->_bg_color << 7;
277 
278  $border1 = $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color
279  $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4;
280  $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8;
281  $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12;
282  $border1 |= $this->_left_color << 16;
283  $border1 |= $this->_right_color << 23;
284 
285  $diagonalDirection = $this->_style->getBorders()->getDiagonalDirection();
286  $diag_tl_to_rb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
287  || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_DOWN;
288  $diag_tr_to_lb = $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_BOTH
289  || $diagonalDirection == PHPExcel_Style_Borders::DIAGONAL_UP;
290  $border1 |= $diag_tl_to_rb << 30;
291  $border1 |= $diag_tr_to_lb << 31;
292 
293  $border2 = $this->_top_color; // Border color
294  $border2 |= $this->_bottom_color << 7;
295  $border2 |= $this->_diag_color << 14;
296  $border2 |= $this->_mapBorderStyle($this->_style->getBorders()->getDiagonal()->getBorderStyle()) << 21;
297  $border2 |= $this->_mapFillType($this->_style->getFill()->getFillType()) << 26;
298 
299  $header = pack("vv", $record, $length);
300 
301  //BIFF8 options: identation, shrinkToFit and text direction
302  $biff8_options = $this->_style->getAlignment()->getIndent();
303  $biff8_options |= (int) $this->_style->getAlignment()->getShrinkToFit() << 4;
304 
305  $data = pack("vvvC", $ifnt, $ifmt, $style, $align);
306  $data .= pack("CCC"
307  , $this->_mapTextRotation($this->_style->getAlignment()->getTextRotation())
308  , $biff8_options
309  , $used_attrib
310  );
311  $data .= pack("VVv", $border1, $border2, $icv);
312  }
313 
314  return($header . $data);
315  }
316 
322  public function setBIFFVersion($BIFFVersion)
323  {
324  $this->_BIFFVersion = $BIFFVersion;
325  }
326 
332  public function setIsStyleXf($value)
333  {
334  $this->_isStyleXf = $value;
335  }
336 
343  function setBottomColor($colorIndex)
344  {
345  $this->_bottom_color = $colorIndex;
346  }
347 
354  function setTopColor($colorIndex)
355  {
356  $this->_top_color = $colorIndex;
357  }
358 
365  function setLeftColor($colorIndex)
366  {
367  $this->_left_color = $colorIndex;
368  }
369 
376  function setRightColor($colorIndex)
377  {
378  $this->_right_color = $colorIndex;
379  }
380 
387  function setDiagColor($colorIndex)
388  {
389  $this->_diag_color = $colorIndex;
390  }
391 
392 
399  function setFgColor($colorIndex)
400  {
401  $this->_fg_color = $colorIndex;
402  }
403 
410  function setBgColor($colorIndex)
411  {
412  $this->_bg_color = $colorIndex;
413  }
414 
422  function setNumberFormatIndex($numberFormatIndex)
423  {
424  $this->_numberFormatIndex = $numberFormatIndex;
425  }
426 
432  public function setFontIndex($value)
433  {
434  $this->_fontIndex = $value;
435  }
436 
440  private function _mapBorderStyle($borderStyle) {
441  switch ($borderStyle) {
442  case PHPExcel_Style_Border::BORDER_NONE: return 0x00;
443  case PHPExcel_Style_Border::BORDER_THIN; return 0x01;
444  case PHPExcel_Style_Border::BORDER_MEDIUM; return 0x02;
445  case PHPExcel_Style_Border::BORDER_DASHED; return 0x03;
446  case PHPExcel_Style_Border::BORDER_DOTTED; return 0x04;
447  case PHPExcel_Style_Border::BORDER_THICK; return 0x05;
448  case PHPExcel_Style_Border::BORDER_DOUBLE; return 0x06;
449  case PHPExcel_Style_Border::BORDER_HAIR; return 0x07;
451  case PHPExcel_Style_Border::BORDER_DASHDOT; return 0x09;
456  default: return 0x00;
457  }
458  }
459 
463  private function _mapFillType($fillType) {
464  switch ($fillType) {
465  case PHPExcel_Style_Fill::FILL_NONE: return 0x00;
466  case PHPExcel_Style_Fill::FILL_SOLID: return 0x01;
484  case PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR: // does not exist in BIFF8
485  case PHPExcel_Style_Fill::FILL_GRADIENT_PATH: // does not exist in BIFF8
486  default: return 0x00;
487  }
488  }
489 
496  private function _mapHAlign($hAlign)
497  {
498  switch ($hAlign) {
505  default: return 0;
506  }
507  }
508 
515  private function _mapVAlign($vAlign) {
516  switch ($vAlign) {
521  default: return 2;
522  }
523  }
524 
531  private function _mapTextRotation($textRotation) {
532  if ($textRotation >= 0) {
533  return $textRotation;
534  }
535  if ($textRotation == -165) {
536  return 255;
537  }
538  if ($textRotation < 0) {
539  return 90 - $textRotation;
540  }
541  }
542 
549  private function _mapLocked($locked) {
550  switch ($locked) {
554  default: return 1;
555  }
556  }
557 
564  private function _mapHidden($hidden) {
565  switch ($hidden) {
569  default: return 0;
570  }
571  }
572 
573 }