ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Format.php
Go to the documentation of this file.
1 <?php
2 /*
3 * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
4 *
5 * The majority of this is _NOT_ my code. I simply ported it from the
6 * PERL Spreadsheet::WriteExcel module.
7 *
8 * The author of the Spreadsheet::WriteExcel module is John McNamara
9 * <jmcnamara@cpan.org>
10 *
11 * I _DO_ maintain this code, and John McNamara has nothing to do with the
12 * porting of this code to PHP. Any questions directly related to this
13 * class library should be directed to me.
14 *
15 * License Information:
16 *
17 * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
18 * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
19 *
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
24 *
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 */
34 
35 require_once 'PEAR.php';
36 
46 {
52 
58 
64 
69  var $_size;
70 
75  var $_bold;
76 
81  var $_italic;
82 
87  var $_color;
88 
94 
100 
106 
112 
118 
124 
130 
136 
141  var $_hidden;
142 
147  var $_locked;
148 
154 
160 
166 
172 
178 
184 
190 
196 
201  var $_bottom;
202 
208 
213  var $_top;
214 
220 
225  var $_left;
226 
232 
237  var $_right;
238 
244 
252  function Spreadsheet_Excel_Writer_Format($BIFF_version, $index = 0, $properties = array())
253  {
254  $this->_xf_index = $index;
255  $this->_BIFF_version = $BIFF_version;
256  $this->font_index = 0;
257  $this->_font_name = 'Arial';
258  $this->_size = 10;
259  $this->_bold = 0x0190;
260  $this->_italic = 0;
261  $this->_color = 0x7FFF;
262  $this->_underline = 0;
263  $this->_font_strikeout = 0;
264  $this->_font_outline = 0;
265  $this->_font_shadow = 0;
266  $this->_font_script = 0;
267  $this->_font_family = 0;
268  $this->_font_charset = 0;
269 
270  $this->_num_format = 0;
271 
272  $this->_hidden = 0;
273  $this->_locked = 0;
274 
275  $this->_text_h_align = 0;
276  $this->_text_wrap = 0;
277  $this->_text_v_align = 2;
278  $this->_text_justlast = 0;
279  $this->_rotation = 0;
280 
281  $this->_fg_color = 0x40;
282  $this->_bg_color = 0x41;
283 
284  $this->_pattern = 0;
285 
286  $this->_bottom = 0;
287  $this->_top = 0;
288  $this->_left = 0;
289  $this->_right = 0;
290  $this->_diag = 0;
291 
292  $this->_bottom_color = 0x40;
293  $this->_top_color = 0x40;
294  $this->_left_color = 0x40;
295  $this->_right_color = 0x40;
296  $this->_diag_color = 0x40;
297 
298  // Set properties passed to Spreadsheet_Excel_Writer_Workbook::addFormat()
299  foreach ($properties as $property => $value)
300  {
301  if (method_exists($this, 'set'.ucwords($property))) {
302  $method_name = 'set'.ucwords($property);
303  $this->$method_name($value);
304  }
305  }
306  }
307 
308 
315  function getXf($style)
316  {
317  // Set the type of the XF record and some of the attributes.
318  if ($style == 'style') {
319  $style = 0xFFF5;
320  } else {
321  $style = $this->_locked;
322  $style |= $this->_hidden << 1;
323  }
324 
325  // Flags to indicate if attributes have been set.
326  $atr_num = ($this->_num_format != 0)?1:0;
327  $atr_fnt = ($this->font_index != 0)?1:0;
328  $atr_alc = ($this->_text_wrap)?1:0;
329  $atr_bdr = ($this->_bottom ||
330  $this->_top ||
331  $this->_left ||
332  $this->_right)?1:0;
333  $atr_pat = (($this->_fg_color != 0x40) ||
334  ($this->_bg_color != 0x41) ||
335  $this->_pattern)?1:0;
336  $atr_prot = $this->_locked | $this->_hidden;
337 
338  // Zero the default border colour if the border has not been set.
339  if ($this->_bottom == 0) {
340  $this->_bottom_color = 0;
341  }
342  if ($this->_top == 0) {
343  $this->_top_color = 0;
344  }
345  if ($this->_right == 0) {
346  $this->_right_color = 0;
347  }
348  if ($this->_left == 0) {
349  $this->_left_color = 0;
350  }
351  if ($this->_diag == 0) {
352  $this->_diag_color = 0;
353  }
354 
355  $record = 0x00E0; // Record identifier
356  if ($this->_BIFF_version == 0x0500) {
357  $length = 0x0010; // Number of bytes to follow
358  }
359  if ($this->_BIFF_version == 0x0600) {
360  $length = 0x0014;
361  }
362 
363  $ifnt = $this->font_index; // Index to FONT record
364  $ifmt = $this->_num_format; // Index to FORMAT record
365  if ($this->_BIFF_version == 0x0500) {
366  $align = $this->_text_h_align; // Alignment
367  $align |= $this->_text_wrap << 3;
368  $align |= $this->_text_v_align << 4;
369  $align |= $this->_text_justlast << 7;
370  $align |= $this->_rotation << 8;
371  $align |= $atr_num << 10;
372  $align |= $atr_fnt << 11;
373  $align |= $atr_alc << 12;
374  $align |= $atr_bdr << 13;
375  $align |= $atr_pat << 14;
376  $align |= $atr_prot << 15;
377 
378  $icv = $this->_fg_color; // fg and bg pattern colors
379  $icv |= $this->_bg_color << 7;
380 
381  $fill = $this->_pattern; // Fill and border line style
382  $fill |= $this->_bottom << 6;
383  $fill |= $this->_bottom_color << 9;
384 
385  $border1 = $this->_top; // Border line style and color
386  $border1 |= $this->_left << 3;
387  $border1 |= $this->_right << 6;
388  $border1 |= $this->_top_color << 9;
389 
390  $border2 = $this->_left_color; // Border color
391  $border2 |= $this->_right_color << 7;
392 
393  $header = pack("vv", $record, $length);
394  $data = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
395  $icv, $fill,
396  $border1, $border2);
397  } elseif ($this->_BIFF_version == 0x0600) {
398  $align = $this->_text_h_align; // Alignment
399  $align |= $this->_text_wrap << 3;
400  $align |= $this->_text_v_align << 4;
401  $align |= $this->_text_justlast << 7;
402 
403  $used_attrib = $atr_num << 2;
404  $used_attrib |= $atr_fnt << 3;
405  $used_attrib |= $atr_alc << 4;
406  $used_attrib |= $atr_bdr << 5;
407  $used_attrib |= $atr_pat << 6;
408  $used_attrib |= $atr_prot << 7;
409 
410  $icv = $this->_fg_color; // fg and bg pattern colors
411  $icv |= $this->_bg_color << 7;
412 
413  $border1 = $this->_left; // Border line style and color
414  $border1 |= $this->_right << 4;
415  $border1 |= $this->_top << 8;
416  $border1 |= $this->_bottom << 12;
417  $border1 |= $this->_left_color << 16;
418  $border1 |= $this->_right_color << 23;
419  $diag_tl_to_rb = 0; // FIXME: add method
420  $diag_tr_to_lb = 0; // FIXME: add method
421  $border1 |= $diag_tl_to_rb << 30;
422  $border1 |= $diag_tr_to_lb << 31;
423 
424  $border2 = $this->_top_color; // Border color
425  $border2 |= $this->_bottom_color << 7;
426  $border2 |= $this->_diag_color << 14;
427  $border2 |= $this->_diag << 21;
428  $border2 |= $this->_pattern << 26;
429 
430  $header = pack("vv", $record, $length);
431 
432  $rotation = $this->_rotation;
433  $biff8_options = 0x00;
434  $data = pack("vvvC", $ifnt, $ifmt, $style, $align);
435  $data .= pack("CCC", $rotation, $biff8_options, $used_attrib);
436  $data .= pack("VVv", $border1, $border2, $icv);
437  }
438 
439  return($header . $data);
440  }
441 
447  function getFont()
448  {
449  $dyHeight = $this->_size * 20; // Height of font (1/20 of a point)
450  $icv = $this->_color; // Index to color palette
451  $bls = $this->_bold; // Bold style
452  $sss = $this->_font_script; // Superscript/subscript
453  $uls = $this->_underline; // Underline
454  $bFamily = $this->_font_family; // Font family
455  $bCharSet = $this->_font_charset; // Character set
456  $encoding = 0; // TODO: Unicode support
457 
458  $cch = strlen($this->_font_name); // Length of font name
459  $record = 0x31; // Record identifier
460  if ($this->_BIFF_version == 0x0500) {
461  $length = 0x0F + $cch; // Record length
462  } elseif ($this->_BIFF_version == 0x0600) {
463  $length = 0x10 + $cch;
464  }
465  $reserved = 0x00; // Reserved
466  $grbit = 0x00; // Font attributes
467  if ($this->_italic) {
468  $grbit |= 0x02;
469  }
470  if ($this->_font_strikeout) {
471  $grbit |= 0x08;
472  }
473  if ($this->_font_outline) {
474  $grbit |= 0x10;
475  }
476  if ($this->_font_shadow) {
477  $grbit |= 0x20;
478  }
479 
480  $header = pack("vv", $record, $length);
481  if ($this->_BIFF_version == 0x0500) {
482  $data = pack("vvvvvCCCCC", $dyHeight, $grbit, $icv, $bls,
483  $sss, $uls, $bFamily,
484  $bCharSet, $reserved, $cch);
485  } elseif ($this->_BIFF_version == 0x0600) {
486  $data = pack("vvvvvCCCCCC", $dyHeight, $grbit, $icv, $bls,
487  $sss, $uls, $bFamily,
488  $bCharSet, $reserved, $cch, $encoding);
489  }
490  return($header . $data . $this->_font_name);
491  }
492 
503  function getFontKey()
504  {
505  $key = "$this->_font_name$this->_size";
506  $key .= "$this->_font_script$this->_underline";
507  $key .= "$this->_font_strikeout$this->_bold$this->_font_outline";
508  $key .= "$this->_font_family$this->_font_charset";
509  $key .= "$this->_font_shadow$this->_color$this->_italic";
510  $key = str_replace(' ', '_', $key);
511  return ($key);
512  }
513 
519  function getXfIndex()
520  {
521  return($this->_xf_index);
522  }
523 
533  function _getColor($name_color = '')
534  {
535  $colors = array(
536  'aqua' => 0x0F,
537  'cyan' => 0x0F,
538  'black' => 0x08,
539  'blue' => 0x0C,
540  'brown' => 0x10,
541  'magenta' => 0x0E,
542  'fuchsia' => 0x0E,
543  'gray' => 0x17,
544  'grey' => 0x17,
545  'green' => 0x11,
546  'lime' => 0x0B,
547  'navy' => 0x12,
548  'orange' => 0x35,
549  'purple' => 0x14,
550  'red' => 0x0A,
551  'silver' => 0x16,
552  'white' => 0x09,
553  'yellow' => 0x0D
554  );
555 
556  // Return the default color, 0x7FFF, if undef,
557  if ($name_color == '') {
558  return(0x7FFF);
559  }
560 
561  // or the color string converted to an integer,
562  if (isset($colors[$name_color])) {
563  return($colors[$name_color]);
564  }
565 
566  // or the default color if string is unrecognised,
567  if (preg_match("/\D/",$name_color)) {
568  return(0x7FFF);
569  }
570 
571  // or an index < 8 mapped into the correct range,
572  if ($name_color < 8) {
573  return($name_color + 8);
574  }
575 
576  // or the default color if arg is outside range,
577  if ($name_color > 63) {
578  return(0x7FFF);
579  }
580 
581  // or an integer in the valid range
582  return($name_color);
583  }
584 
591  function setAlign($location)
592  {
593  if (preg_match("/\d/",$location)) {
594  return; // Ignore numbers
595  }
596 
597  $location = strtolower($location);
598 
599  if ($location == 'left') {
600  $this->_text_h_align = 1;
601  }
602  if ($location == 'centre') {
603  $this->_text_h_align = 2;
604  }
605  if ($location == 'center') {
606  $this->_text_h_align = 2;
607  }
608  if ($location == 'right') {
609  $this->_text_h_align = 3;
610  }
611  if ($location == 'fill') {
612  $this->_text_h_align = 4;
613  }
614  if ($location == 'justify') {
615  $this->_text_h_align = 5;
616  }
617  if ($location == 'merge') {
618  $this->_text_h_align = 6;
619  }
620  if ($location == 'equal_space') { // For T.K.
621  $this->_text_h_align = 7;
622  }
623  if ($location == 'top') {
624  $this->_text_v_align = 0;
625  }
626  if ($location == 'vcentre') {
627  $this->_text_v_align = 1;
628  }
629  if ($location == 'vcenter') {
630  $this->_text_v_align = 1;
631  }
632  if ($location == 'bottom') {
633  $this->_text_v_align = 2;
634  }
635  if ($location == 'vjustify') {
636  $this->_text_v_align = 3;
637  }
638  if ($location == 'vequal_space') { // For T.K.
639  $this->_text_v_align = 4;
640  }
641  }
642 
650  {
651  if (preg_match("/\d/",$location)) {
652  return; // Ignore numbers
653  }
654 
655  $location = strtolower($location);
656 
657  if ($location == 'left') {
658  $this->_text_h_align = 1;
659  }
660  if ($location == 'centre') {
661  $this->_text_h_align = 2;
662  }
663  if ($location == 'center') {
664  $this->_text_h_align = 2;
665  }
666  if ($location == 'right') {
667  $this->_text_h_align = 3;
668  }
669  if ($location == 'fill') {
670  $this->_text_h_align = 4;
671  }
672  if ($location == 'justify') {
673  $this->_text_h_align = 5;
674  }
675  if ($location == 'merge') {
676  $this->_text_h_align = 6;
677  }
678  if ($location == 'equal_space') { // For T.K.
679  $this->_text_h_align = 7;
680  }
681  }
682 
690  {
691  if (preg_match("/\d/",$location)) {
692  return; // Ignore numbers
693  }
694 
695  $location = strtolower($location);
696 
697  if ($location == 'top') {
698  $this->_text_v_align = 0;
699  }
700  if ($location == 'vcentre') {
701  $this->_text_v_align = 1;
702  }
703  if ($location == 'vcenter') {
704  $this->_text_v_align = 1;
705  }
706  if ($location == 'bottom') {
707  $this->_text_v_align = 2;
708  }
709  if ($location == 'vjustify') {
710  $this->_text_v_align = 3;
711  }
712  if ($location == 'vequal_space') { // For T.K.
713  $this->_text_v_align = 4;
714  }
715  }
716 
722  function setMerge()
723  {
724  $this->setAlign('merge');
725  }
726 
737  function setBold($weight = 1)
738  {
739  if ($weight == 1) {
740  $weight = 0x2BC; // Bold text
741  }
742  if ($weight == 0) {
743  $weight = 0x190; // Normal text
744  }
745  if ($weight < 0x064) {
746  $weight = 0x190; // Lower bound
747  }
748  if ($weight > 0x3E8) {
749  $weight = 0x190; // Upper bound
750  }
751  $this->_bold = $weight;
752  }
753 
754 
755  /************************************
756  * FUNCTIONS FOR SETTING CELLS BORDERS
757  */
758 
765  function setBottom($style)
766  {
767  $this->_bottom = $style;
768  }
769 
776  function setTop($style)
777  {
778  $this->_top = $style;
779  }
780 
787  function setLeft($style)
788  {
789  $this->_left = $style;
790  }
791 
798  function setRight($style)
799  {
800  $this->_right = $style;
801  }
802 
803 
810  function setBorder($style)
811  {
812  $this->setBottom($style);
813  $this->setTop($style);
814  $this->setLeft($style);
815  $this->setRight($style);
816  }
817 
818 
819  /*******************************************
820  * FUNCTIONS FOR SETTING CELLS BORDERS COLORS
821  */
822 
830  function setBorderColor($color)
831  {
832  $this->setBottomColor($color);
833  $this->setTopColor($color);
834  $this->setLeftColor($color);
835  $this->setRightColor($color);
836  }
837 
844  function setBottomColor($color)
845  {
846  $value = $this->_getColor($color);
847  $this->_bottom_color = $value;
848  }
849 
856  function setTopColor($color)
857  {
858  $value = $this->_getColor($color);
859  $this->_top_color = $value;
860  }
861 
868  function setLeftColor($color)
869  {
870  $value = $this->_getColor($color);
871  $this->_left_color = $value;
872  }
873 
880  function setRightColor($color)
881  {
882  $value = $this->_getColor($color);
883  $this->_right_color = $value;
884  }
885 
886 
893  function setFgColor($color)
894  {
895  $value = $this->_getColor($color);
896  $this->_fg_color = $value;
897  if ($this->_pattern == 0) { // force color to be seen
898  $this->_pattern = 1;
899  }
900  }
901 
908  function setBgColor($color)
909  {
910  $value = $this->_getColor($color);
911  $this->_bg_color = $value;
912  if ($this->_pattern == 0) { // force color to be seen
913  $this->_pattern = 1;
914  }
915  }
916 
923  function setColor($color)
924  {
925  $value = $this->_getColor($color);
926  $this->_color = $value;
927  }
928 
936  function setPattern($arg = 1)
937  {
938  $this->_pattern = $arg;
939  }
940 
948  function setUnderline($underline)
949  {
950  $this->_underline = $underline;
951  }
952 
958  function setItalic()
959  {
960  $this->_italic = 1;
961  }
962 
969  function setSize($size)
970  {
971  $this->_size = $size;
972  }
973 
979  function setTextWrap()
980  {
981  $this->_text_wrap = 1;
982  }
983 
991  function setTextRotation($angle)
992  {
993  switch ($angle)
994  {
995  case 0:
996  $this->_rotation = 0;
997  break;
998  case 90:
999  if ($this->_BIFF_version == 0x0500) {
1000  $this->_rotation = 3;
1001  } elseif ($this->_BIFF_version == 0x0600) {
1002  $this->_rotation = 180;
1003  }
1004  break;
1005  case 270:
1006  if ($this->_BIFF_version == 0x0500) {
1007  $this->_rotation = 2;
1008  } elseif ($this->_BIFF_version == 0x0600) {
1009  $this->_rotation = 90;
1010  }
1011  break;
1012  case -1:
1013  if ($this->_BIFF_version == 0x0500) {
1014  $this->_rotation = 1;
1015  } elseif ($this->_BIFF_version == 0x0600) {
1016  $this->_rotation = 255;
1017  }
1018  break;
1019  default :
1020  return $this->raiseError("Invalid value for angle.".
1021  " Possible values are: 0, 90, 270 and -1 ".
1022  "for stacking top-to-bottom.");
1023  $this->_rotation = 0;
1024  break;
1025  }
1026  }
1027 
1035  function setNumFormat($num_format)
1036  {
1037  $this->_num_format = $num_format;
1038  }
1039 
1045  function setStrikeOut()
1046  {
1047  $this->_font_strikeout = 1;
1048  }
1049 
1055  function setOutLine()
1056  {
1057  $this->_font_outline = 1;
1058  }
1059 
1065  function setShadow()
1066  {
1067  $this->_font_shadow = 1;
1068  }
1069 
1077  function setScript($script)
1078  {
1079  $this->_font_script = $script;
1080  }
1081 
1087  function setLocked()
1088  {
1089  $this->_locked = 1;
1090  }
1091 
1097  function setUnLocked()
1098  {
1099  $this->_locked = 0;
1100  }
1101 
1109  function setFontFamily($font_family)
1110  {
1111  $this->_font_name = $font_family;
1112  }
1113 }
1114 ?>