ILIAS  Release_4_0_x_branch Revision 61816
 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 
65 if (!defined('PHPEXCEL_ROOT')) {
69  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
70 }
71 
73 require_once PHPEXCEL_ROOT . 'PHPExcel/Style/Alignment.php';
74 
76 require_once PHPEXCEL_ROOT . 'PHPExcel/Style/Border.php';
77 
79 require_once PHPEXCEL_ROOT . 'PHPExcel/Style/Fill.php';
80 
82 require_once PHPEXCEL_ROOT . 'PHPExcel/Style/Protection.php';
83 
84 
93 {
99  private $_BIFFVersion;
100 
106  private $_isStyleXf;
107 
112  private $_fontIndex;
113 
119 
125 
131 
137 
143 
149 
155 
161 
169  public function __construct(PHPExcel_Style $style = null)
170  {
171  $this->_isStyleXf = false;
172  $this->_BIFFVersion = 0x0600;
173  $this->_fontIndex = 0;
174 
175  $this->_numberFormatIndex = 0;
176 
177  $this->_text_justlast = 0;
178 
179  $this->_fg_color = 0x40;
180  $this->_bg_color = 0x41;
181 
182  $this->_diag = 0;
183 
184  $this->_bottom_color = 0x40;
185  $this->_top_color = 0x40;
186  $this->_left_color = 0x40;
187  $this->_right_color = 0x40;
188  $this->_diag_color = 0x40;
189  $this->_style = $style;
190 
191  }
192 
193 
200  function writeXf()
201  {
202  // Set the type of the XF record and some of the attributes.
203  if ($this->_isStyleXf) {
204  $style = 0xFFF5;
205  } else {
206  $style = $this->_mapLocked($this->_style->getProtection()->getLocked());
207  $style |= $this->_mapHidden($this->_style->getProtection()->getHidden()) << 1;
208  }
209 
210  // Flags to indicate if attributes have been set.
211  $atr_num = ($this->_numberFormatIndex != 0)?1:0;
212  $atr_fnt = ($this->_fontIndex != 0)?1:0;
213  $atr_alc = ((int) $this->_style->getAlignment()->getWrapText())?1:0;
214  $atr_bdr = ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) ||
215  $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
216  $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
217  $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0;
218  $atr_pat = (($this->_fg_color != 0x40) ||
219  ($this->_bg_color != 0x41) ||
220  $this->_mapFillType($this->_style->getFill()->getFillType()))?1:0;
221  $atr_prot = $this->_mapLocked($this->_style->getProtection()->getLocked())
222  | $this->_mapHidden($this->_style->getProtection()->getHidden());
223 
224  // Zero the default border colour if the border has not been set.
225  if ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0) {
226  $this->_bottom_color = 0;
227  }
228  if ($this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) == 0) {
229  $this->_top_color = 0;
230  }
231  if ($this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0) {
232  $this->_right_color = 0;
233  }
234  if ($this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0) {
235  $this->_left_color = 0;
236  }
237  if ($this->_diag == 0) {
238  $this->_diag_color = 0;
239  }
240 
241  $record = 0x00E0; // Record identifier
242  if ($this->_BIFFVersion == 0x0500) {
243  $length = 0x0010; // Number of bytes to follow
244  }
245  if ($this->_BIFFVersion == 0x0600) {
246  $length = 0x0014;
247  }
248 
249  $ifnt = $this->_fontIndex; // Index to FONT record
250  $ifmt = $this->_numberFormatIndex; // Index to FORMAT record
251  if ($this->_BIFFVersion == 0x0500) {
252  $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
253  $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
254  $align |= $this->_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
255  $align |= $this->_text_justlast << 7;
256  $align |= 0 << 8; // rotation
257  $align |= $atr_num << 10;
258  $align |= $atr_fnt << 11;
259  $align |= $atr_alc << 12;
260  $align |= $atr_bdr << 13;
261  $align |= $atr_pat << 14;
262  $align |= $atr_prot << 15;
263 
264  $icv = $this->_fg_color; // fg and bg pattern colors
265  $icv |= $this->_bg_color << 7;
266 
267  $fill = $this->_mapFillType($this->_style->getFill()->getFillType()); // Fill and border line style
268  $fill |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 6;
269  $fill |= $this->_bottom_color << 9;
270 
271  $border1 = $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()); // Border line style and color
272  $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) << 3;
273  $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 6;
274  $border1 |= $this->_top_color << 9;
275 
276  $border2 = $this->_left_color; // Border color
277  $border2 |= $this->_right_color << 7;
278 
279  $header = pack("vv", $record, $length);
280  $data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
281  $icv, $fill,
282  $border1, $border2);
283  } elseif ($this->_BIFFVersion == 0x0600) {
284  $align = $this->_mapHAlign($this->_style->getAlignment()->getHorizontal()); // Alignment
285  $align |= (int) $this->_style->getAlignment()->getWrapText() << 3;
286  $align |= $this->_mapVAlign($this->_style->getAlignment()->getVertical()) << 4;
287  $align |= $this->_text_justlast << 7;
288 
289  $used_attrib = $atr_num << 2;
290  $used_attrib |= $atr_fnt << 3;
291  $used_attrib |= $atr_alc << 4;
292  $used_attrib |= $atr_bdr << 5;
293  $used_attrib |= $atr_pat << 6;
294  $used_attrib |= $atr_prot << 7;
295 
296  $icv = $this->_fg_color; // fg and bg pattern colors
297  $icv |= $this->_bg_color << 7;
298 
299  $border1 = $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()); // Border line style and color
300  $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) << 4;
301  $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) << 8;
302  $border1 |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) << 12;
303  $border1 |= $this->_left_color << 16;
304  $border1 |= $this->_right_color << 23;
305  $diag_tl_to_rb = 0;
306  $diag_tr_to_lb = 0;
307  $border1 |= $diag_tl_to_rb << 30;
308  $border1 |= $diag_tr_to_lb << 31;
309 
310  $border2 = $this->_top_color; // Border color
311  $border2 |= $this->_bottom_color << 7;
312  $border2 |= $this->_diag_color << 14;
313  $border2 |= $this->_diag << 21;
314  $border2 |= $this->_mapFillType($this->_style->getFill()->getFillType()) << 26;
315 
316  $header = pack("vv", $record, $length);
317 
318  //BIFF8 options: identation, shrinkToFit and text direction
319  $biff8_options = $this->_style->getAlignment()->getIndent();
320  $biff8_options |= (int) $this->_style->getAlignment()->getShrinkToFit() << 4;
321 
322  $data = pack("vvvC", $ifnt, $ifmt, $style, $align);
323  $data .= pack("CCC"
324  , $this->_mapTextRotation($this->_style->getAlignment()->getTextRotation())
325  , $biff8_options
326  , $used_attrib
327  );
328  $data .= pack("VVv", $border1, $border2, $icv);
329  }
330 
331  return($header . $data);
332  }
333 
339  public function setBIFFVersion($BIFFVersion)
340  {
341  $this->_BIFFVersion = $BIFFVersion;
342  }
343 
349  public function setIsStyleXf($value)
350  {
351  $this->_isStyleXf = $value;
352  }
353 
360  function setBottomColor($colorIndex)
361  {
362  $this->_bottom_color = $colorIndex;
363  }
364 
371  function setTopColor($colorIndex)
372  {
373  $this->_top_color = $colorIndex;
374  }
375 
382  function setLeftColor($colorIndex)
383  {
384  $this->_left_color = $colorIndex;
385  }
386 
393  function setRightColor($colorIndex)
394  {
395  $this->_right_color = $colorIndex;
396  }
397 
398 
405  function setFgColor($colorIndex)
406  {
407  $this->_fg_color = $colorIndex;
408  }
409 
416  function setBgColor($colorIndex)
417  {
418  $this->_bg_color = $colorIndex;
419  }
420 
428  function setNumberFormatIndex($numberFormatIndex)
429  {
430  $this->_numberFormatIndex = $numberFormatIndex;
431  }
432 
438  public function setFontIndex($value)
439  {
440  $this->_fontIndex = $value;
441  }
442 
446  private function _mapBorderStyle($borderStyle) {
447  switch ($borderStyle) {
448  case PHPExcel_Style_Border::BORDER_NONE: return 0x00;
449  case PHPExcel_Style_Border::BORDER_THIN; return 0x01;
450  case PHPExcel_Style_Border::BORDER_MEDIUM; return 0x02;
451  case PHPExcel_Style_Border::BORDER_DASHED; return 0x03;
452  case PHPExcel_Style_Border::BORDER_DOTTED; return 0x04;
453  case PHPExcel_Style_Border::BORDER_THICK; return 0x05;
454  case PHPExcel_Style_Border::BORDER_DOUBLE; return 0x06;
455  case PHPExcel_Style_Border::BORDER_HAIR; return 0x07;
457  case PHPExcel_Style_Border::BORDER_DASHDOT; return 0x09;
462  default: return 0x00;
463  }
464  }
465 
469  private function _mapFillType($fillType) {
470  switch ($fillType) {
471  case PHPExcel_Style_Fill::FILL_NONE: return 0x00;
472  case PHPExcel_Style_Fill::FILL_SOLID: return 0x01;
490  case PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR: // does not exist in BIFF8
491  case PHPExcel_Style_Fill::FILL_GRADIENT_PATH: // does not exist in BIFF8
492  default: return 0x00;
493  }
494  }
495 
502  private function _mapHAlign($hAlign)
503  {
504  switch ($hAlign) {
511  default: return 0;
512  }
513  }
514 
521  private function _mapVAlign($vAlign) {
522  switch ($vAlign) {
527  default: return 2;
528  }
529  }
530 
537  private function _mapTextRotation($textRotation) {
538  if ($textRotation >= 0) {
539  return $textRotation;
540  }
541  if ($textRotation == -165) {
542  return 255;
543  }
544  if ($textRotation < 0) {
545  return 90 - $textRotation;
546  }
547  }
548 
555  private function _mapLocked($locked) {
556  switch ($locked) {
560  default: return 1;
561  }
562  }
563 
570  private function _mapHidden($hidden) {
571  switch ($hidden) {
575  default: return 0;
576  }
577  }
578 
579 }