[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/include/php_writeexcel/ -> class.writeexcel_format.inc.php (source)

   1  <?php
   2  
   3  /*
   4   * Copyleft 2002 Johann Hanne
   5   *
   6   * This is free software; you can redistribute it and/or
   7   * modify it under the terms of the GNU Lesser General Public
   8   * License as published by the Free Software Foundation; either
   9   * version 2.1 of the License, or (at your option) any later version.
  10   *
  11   * This software is distributed in the hope that it will be useful,
  12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14   * Lesser General Public License for more details.
  15   *
  16   * You should have received a copy of the GNU Lesser General Public
  17   * License along with this software; if not, write to the
  18   * Free Software Foundation, Inc., 59 Temple Place,
  19   * Suite 330, Boston, MA  02111-1307 USA
  20   */
  21  
  22  /*
  23   * This is the Spreadsheet::WriteExcel Perl package ported to PHP
  24   * Spreadsheet::WriteExcel was written by John McNamara, jmcnamara@cpan.org
  25   */
  26  
  27  class writeexcel_format {
  28  
  29      var $_xf_index;
  30      var $_font_index;
  31      var $_font;
  32      var $_size;
  33      var $_bold;
  34      var $_italic;
  35      var $_color;
  36      var $_underline;
  37      var $_font_strikeout;
  38      var $_font_outline;
  39      var $_font_shadow;
  40      var $_font_script;
  41      var $_font_family;
  42      var $_font_charset;
  43      var $_num_format;
  44      var $_hidden;
  45      var $_locked;
  46      var $_text_h_align;
  47      var $_text_wrap;
  48      var $_text_v_align;
  49      var $_text_justlast;
  50      var $_rotation;
  51      var $_fg_color;
  52      var $_bg_color;
  53      var $_pattern;
  54      var $_bottom;
  55      var $_top;
  56      var $_left;
  57      var $_right;
  58      var $_bottom_color;
  59      var $_top_color;
  60      var $_left_color;
  61      var $_right_color;
  62  
  63      /*
  64       * Constructor
  65       */
  66      function writeexcel_format() {
  67          $_=func_get_args();
  68  
  69          $this->_xf_index       = (sizeof($_)>0) ? array_shift($_) : 0;
  70  
  71          $this->_font_index     = 0;
  72          $this->_font           = 'Arial';
  73          $this->_size           = 10;
  74          $this->_bold           = 0x0190;
  75          $this->_italic         = 0;
  76          $this->_color          = 0x7FFF;
  77          $this->_underline      = 0;
  78          $this->_font_strikeout = 0;
  79          $this->_font_outline   = 0;
  80          $this->_font_shadow    = 0;
  81          $this->_font_script    = 0;
  82          $this->_font_family    = 0;
  83          $this->_font_charset   = 0;
  84  
  85          $this->_num_format     = 0;
  86  
  87          $this->_hidden         = 0;
  88          $this->_locked         = 1;
  89  
  90          $this->_text_h_align   = 0;
  91          $this->_text_wrap      = 0;
  92          $this->_text_v_align   = 2;
  93          $this->_text_justlast  = 0;
  94          $this->_rotation       = 0;
  95  
  96          $this->_fg_color       = 0x40;
  97          $this->_bg_color       = 0x41;
  98  
  99          $this->_pattern        = 0;
 100  
 101          $this->_bottom         = 0;
 102          $this->_top            = 0;
 103          $this->_left           = 0;
 104          $this->_right          = 0;
 105  
 106          $this->_bottom_color   = 0x40;
 107          $this->_top_color      = 0x40;
 108          $this->_left_color     = 0x40;
 109          $this->_right_color    = 0x40;
 110  
 111          // Set properties passed to writeexcel_workbook::addformat()
 112          if (sizeof($_)>0) {
 113              call_user_method_array('set_properties', $this, $_);
 114          }
 115      }
 116  
 117      /*
 118       * Copy the attributes of another writeexcel_format object.
 119       */
 120      function copy($other) {
 121          $xf = $this->_xf_index;   // Backup XF index
 122      //Changes done for supporting PHP5 compatibility
 123          $this[] = clone($other);           // Copy properties
 124          $this->_xf_index = $xf;   // Restore XF index
 125      }
 126  
 127      /*
 128       * Generate an Excel BIFF XF record.
 129       */
 130      function get_xf() {
 131  
 132          $_=func_get_args();
 133  
 134          // $record    Record identifier
 135          // $length    Number of bytes to follow
 136  
 137          // $ifnt      Index to FONT record
 138          // $ifmt      Index to FORMAT record
 139          // $style     Style and other options
 140          // $align     Alignment
 141          // $icv       fg and bg pattern colors
 142          // $fill      Fill and border line style
 143          // $border1   Border line style and color
 144          // $border2   Border color
 145  
 146          // Set the type of the XF record and some of the attributes.
 147          if ($_[0] == "style") {
 148              $style = 0xFFF5;
 149          } else {
 150              $style   = $this->_locked;
 151              $style  |= $this->_hidden << 1;
 152          }
 153  
 154          // Flags to indicate if attributes have been set.
 155          $atr_num     = ($this->_num_format != 0) ? 1 : 0;
 156          $atr_fnt     = ($this->_font_index != 0) ? 1 : 0;
 157          $atr_alc     =  $this->_text_wrap ? 1 : 0;
 158          $atr_bdr     = ($this->_bottom   ||
 159                          $this->_top      ||
 160                          $this->_left     ||
 161                          $this->_right) ? 1 : 0;
 162          $atr_pat     = ($this->_fg_color != 0x41 ||
 163                          $this->_bg_color != 0x41 ||
 164                          $this->_pattern  != 0x00) ? 1 : 0;
 165          $atr_prot    = 0;
 166  
 167          // Reset the default colors for the non-font properties
 168          if ($this->_fg_color     == 0x7FFF) $this->_fg_color     = 0x40;
 169          if ($this->_bg_color     == 0x7FFF) $this->_bg_color     = 0x41;
 170          if ($this->_bottom_color == 0x7FFF) $this->_bottom_color = 0x41;
 171          if ($this->_top_color    == 0x7FFF) $this->_top_color    = 0x41;
 172          if ($this->_left_color   == 0x7FFF) $this->_left_color   = 0x41;
 173          if ($this->_right_color  == 0x7FFF) $this->_right_color  = 0x41;
 174  
 175          // Zero the default border colour if the border has not been set.
 176          if ($this->_bottom == 0) {
 177              $this->_bottom_color = 0;
 178          }
 179          if ($this->_top    == 0) {
 180              $this->_top_color    = 0;
 181          }
 182          if ($this->_right  == 0) {
 183              $this->_right_color  = 0;
 184          }
 185          if ($this->_left   == 0) {
 186              $this->_left_color   = 0;
 187          }
 188  
 189          // The following 2 logical statements take care of special cases in 
 190          // relation to cell colors and patterns:
 191          // 1. For a solid fill (_pattern == 1) Excel reverses the role of
 192          //    foreground and background colors
 193          // 2. If the user specifies a foreground or background color
 194          //    without a pattern they probably wanted a solid fill, so we
 195          //    fill in the defaults.
 196          if ($this->_pattern <= 0x01 && 
 197              $this->_bg_color != 0x41 && 
 198              $this->_fg_color == 0x40 )
 199          {
 200              $this->_fg_color = $this->_bg_color;
 201              $this->_bg_color = 0x40;
 202              $this->_pattern  = 1;
 203          }
 204  
 205          if ($this->_pattern <= 0x01 &&
 206              $this->_bg_color == 0x41 &&
 207              $this->_fg_color != 0x40 )
 208          {
 209              $this->_bg_color = 0x40;
 210              $this->_pattern  = 1;
 211          }
 212  
 213          $record         = 0x00E0;
 214          $length         = 0x0010;
 215  
 216          $ifnt           = $this->_font_index;
 217          $ifmt           = $this->_num_format;
 218  
 219          $align          = $this->_text_h_align;
 220          $align         |= $this->_text_wrap     << 3;
 221          $align         |= $this->_text_v_align  << 4;
 222          $align         |= $this->_text_justlast << 7;
 223          $align         |= $this->_rotation      << 8;
 224          $align         |= $atr_num              << 10;
 225          $align         |= $atr_fnt              << 11;
 226          $align         |= $atr_alc              << 12;
 227          $align         |= $atr_bdr              << 13;
 228          $align         |= $atr_pat              << 14;
 229          $align         |= $atr_prot             << 15;
 230  
 231          $icv            = $this->_fg_color;
 232          $icv           |= $this->_bg_color      << 7;
 233  
 234          $fill           = $this->_pattern;
 235          $fill          |= $this->_bottom        << 6;
 236          $fill          |= $this->_bottom_color  << 9;
 237  
 238          $border1        = $this->_top;
 239          $border1       |= $this->_left          << 3;
 240          $border1       |= $this->_right         << 6;
 241          $border1       |= $this->_top_color     << 9;
 242  
 243          $border2        = $this->_left_color;
 244          $border2       |= $this->_right_color   << 7;
 245  
 246          $header      = pack("vv",       $record, $length);
 247          $data        = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
 248                                          $icv, $fill,
 249                                          $border1, $border2);
 250  
 251          return($header . $data);
 252      }
 253  
 254      /*
 255       * Generate an Excel BIFF FONT record.
 256       */
 257      function get_font() {
 258  
 259          // $record     Record identifier
 260          // $length     Record length
 261  
 262          // $dyHeight   Height of font (1/20 of a point)
 263          // $grbit      Font attributes
 264          // $icv        Index to color palette
 265          // $bls        Bold style
 266          // $sss        Superscript/subscript
 267          // $uls        Underline
 268          // $bFamily    Font family
 269          // $bCharSet   Character set
 270          // $reserved   Reserved
 271          // $cch        Length of font name
 272          // $rgch       Font name
 273  
 274          $dyHeight   = $this->_size * 20;
 275          $icv        = $this->_color;
 276          $bls        = $this->_bold;
 277          $sss        = $this->_font_script;
 278          $uls        = $this->_underline;
 279          $bFamily    = $this->_font_family;
 280          $bCharSet   = $this->_font_charset;
 281          $rgch       = $this->_font;
 282  
 283          $cch        = strlen($rgch);
 284          $record     = 0x31;
 285          $length     = 0x0F + $cch;
 286          $reserved   = 0x00;
 287  
 288          $grbit      = 0x00;
 289  
 290          if ($this->_italic) {
 291              $grbit     |= 0x02;
 292          }
 293  
 294          if ($this->_font_strikeout) {
 295              $grbit     |= 0x08;
 296          }
 297  
 298          if ($this->_font_outline) {
 299              $grbit     |= 0x10;
 300          }
 301  
 302          if ($this->_font_shadow) {
 303              $grbit     |= 0x20;
 304          }
 305  
 306          $header  = pack("vv",         $record, $length);
 307          $data    = pack("vvvvvCCCCC", $dyHeight, $grbit, $icv, $bls,
 308                                        $sss, $uls, $bFamily,
 309                                        $bCharSet, $reserved, $cch);
 310  
 311          return($header . $data . $this->_font);
 312      }
 313  
 314      /*
 315       * Returns a unique hash key for a font.
 316       * Used by writeexcel_workbook::_store_all_fonts()
 317       */
 318      function get_font_key() {
 319  
 320          # The following elements are arranged to increase the probability of
 321          # generating a unique key. Elements that hold a large range of numbers
 322          # eg. _color are placed between two binary elements such as _italic
 323          #
 324          $key  = $this->_font.$this->_size.
 325                  $this->_font_script.$this->_underline.
 326                  $this->_font_strikeout.$this->_bold.$this->_font_outline.
 327                  $this->_font_family.$this->_font_charset.
 328                  $this->_font_shadow.$this->_color.$this->_italic;
 329  
 330          $key = preg_replace('/ /', '_', $key); # Convert the key to a single word
 331  
 332          return $key;
 333      }
 334  
 335      /*
 336       * Returns the used by Worksheet->_XF()
 337       */
 338      function get_xf_index() {
 339          return $this->_xf_index;
 340      }
 341  
 342      /*
 343       * Used in conjunction with the set_xxx_color methods to convert a color
 344       * string into a number. Color range is 0..63 but we will restrict it
 345       * to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
 346       */
 347      function _get_color($color=false) {
 348  
 349          $colors = array(
 350                          'aqua'    => 0x0F,
 351                          'cyan'    => 0x0F,
 352                          'black'   => 0x08,
 353                          'blue'    => 0x0C,
 354                          'brown'   => 0x10,
 355                          'magenta' => 0x0E,
 356                          'fuchsia' => 0x0E,
 357                          'gray'    => 0x17,
 358                          'grey'    => 0x17,
 359                          'green'   => 0x11,
 360                          'lime'    => 0x0B,
 361                          'navy'    => 0x12,
 362                          'orange'  => 0x35,
 363                          'purple'  => 0x14,
 364                          'red'     => 0x0A,
 365                          'silver'  => 0x16,
 366                          'white'   => 0x09,
 367                          'yellow'  => 0x0D
 368                         );
 369  
 370          // Return the default color, 0x7FFF, if undef,
 371          if ($color===false) {
 372              return 0x7FFF;
 373          }
 374  
 375          // or the color string converted to an integer,
 376          if (isset($colors[strtolower($color)])) {
 377              return $colors[strtolower($color)];
 378          }
 379  
 380          // or the default color if string is unrecognised,
 381          if (preg_match('/\D/', $color)) {
 382              return 0x7FFF;
 383          }
 384  
 385          // or an index < 8 mapped into the correct range,
 386          if ($color<8) {
 387              return $color + 8;
 388          }
 389  
 390          // or the default color if arg is outside range,
 391          if ($color>63) {
 392              return 0x7FFF;
 393          }
 394  
 395          // or an integer in the valid range
 396          return $color;
 397      }
 398  
 399      /*
 400       * Set cell alignment.
 401       */
 402      function set_align($location) {
 403  
 404          // Ignore numbers
 405          if (preg_match('/\d/', $location)) {
 406              return;
 407          }
 408  
 409          $location = strtolower($location);
 410  
 411          switch ($location) {
 412  
 413          case 'left':
 414              $this->set_text_h_align(1);
 415              break;
 416  
 417          case 'centre':
 418          case 'center':
 419              $this->set_text_h_align(2);
 420              break;
 421  
 422          case 'right':
 423              $this->set_text_h_align(3);
 424              break;
 425  
 426          case 'fill':
 427              $this->set_text_h_align(4);
 428              break;
 429  
 430          case 'justify':
 431              $this->set_text_h_align(5);
 432              break;
 433  
 434          case 'merge':
 435              $this->set_text_h_align(6);
 436              break;
 437  
 438          case 'equal_space':
 439              $this->set_text_h_align(7);
 440              break;
 441  
 442          case 'top':
 443              $this->set_text_v_align(0);
 444              break;
 445  
 446          case 'vcentre':
 447          case 'vcenter':
 448              $this->set_text_v_align(1);
 449              break;
 450              break;
 451  
 452          case 'bottom':
 453              $this->set_text_v_align(2);
 454              break;
 455  
 456          case 'vjustify':
 457              $this->set_text_v_align(3);
 458              break;
 459  
 460          case 'vequal_space':
 461              $this->set_text_v_align(4);
 462              break;
 463          }
 464      }
 465  
 466      /*
 467       * Set vertical cell alignment. This is required by the set_properties()
 468       * method to differentiate between the vertical and horizontal properties.
 469       */
 470      function set_valign($location) {
 471          $this->set_align($location);
 472      }
 473  
 474      /*
 475       * This is an alias for the unintuitive set_align('merge')
 476       */
 477      function set_merge() {
 478          $this->set_text_h_align(6);
 479      }
 480  
 481      /*
 482       * Bold has a range 0x64..0x3E8.
 483       * 0x190 is normal. 0x2BC is bold.
 484       */
 485      function set_bold($weight=1) {
 486  
 487          if ($weight == 1) {
 488              // Bold text
 489              $weight = 0x2BC;
 490          }
 491  
 492          if ($weight == 0) {
 493              // Normal text
 494              $weight = 0x190;
 495          }
 496  
 497          if ($weight < 0x064) {
 498              // Lower bound
 499              $weight = 0x190;
 500          }
 501  
 502          if ($weight > 0x3E8) {
 503              // Upper bound
 504              $weight = 0x190;
 505          }
 506  
 507          $this->_bold = $weight;
 508      }
 509  
 510      /*
 511       * Set all cell borders (bottom, top, left, right) to the same style
 512       */
 513      function set_border($style) {
 514          $this->set_bottom($style);
 515          $this->set_top($style);
 516          $this->set_left($style);
 517          $this->set_right($style);
 518      }
 519  
 520      /*
 521       * Set all cell borders (bottom, top, left, right) to the same color
 522       */
 523      function set_border_color($color) {
 524          $this->set_bottom_color($color);
 525          $this->set_top_color($color);
 526          $this->set_left_color($color);
 527          $this->set_right_color($color);
 528      }
 529  
 530      /*
 531       * Convert hashes of properties to method calls.
 532       */
 533      function set_properties() {
 534  
 535          $_=func_get_args();
 536  
 537          $properties=array();
 538          foreach($_ as $props) {
 539              if (is_array($props)) {
 540                  $properties=array_merge($properties, $props);
 541              } else {
 542                  $properties[]=$props;
 543              }
 544          }
 545  
 546          foreach ($properties as $key=>$value) {
 547  
 548              // Strip leading "-" from Tk style properties eg. -color => 'red'.
 549              $key = preg_replace('/^-/', '', $key);
 550  
 551              /* Make sure method names are alphanumeric characters only, in
 552                 case tainted data is passed to the eval(). */
 553              if (preg_match('/\W/', $key)) {
 554                  trigger_error("Unknown property: $key.",
 555                                E_USER_ERROR);
 556              }
 557  
 558              /* Evaling all $values as a strings gets around the problem of
 559                 some numerical format strings being evaluated as numbers, for
 560                 example "00000" for a zip code. */
 561              if (is_int($key)) {
 562                  eval("\$this->set_$value();");
 563              } else {
 564                  eval("\$this->set_$key('$value');");
 565              }
 566  
 567          }
 568      }
 569  
 570      function set_font($font) {
 571          $this->_font=$font;
 572      }
 573  
 574      function set_size($size) {
 575          $this->_size=$size;
 576      }
 577  
 578      function set_italic($italic=1) {
 579          $this->_italic=$italic;
 580      }
 581  
 582      function set_color($color) {
 583          $this->_color=$this->_get_color($color);
 584      }
 585  
 586      function set_underline($underline=1) {
 587          $this->_underline=$underline;
 588      }
 589  
 590      function set_font_strikeout($font_strikeout=1) {
 591          $this->_font_strikeout=$font_strikeout;
 592      }
 593  
 594      function set_font_outline($font_outline=1) {
 595          $this->_font_outline=$font_outline;
 596      }
 597  
 598      function set_font_shadow($font_shadow=1) {
 599          $this->_font_shadow=$font_shadow;
 600      }
 601  
 602      function set_font_script($font_script=1) {
 603          $this->_font_script=$font_script;
 604      }
 605  
 606      /* Undocumented */
 607      function set_font_family($font_family=1) {
 608          $this->_font_family=$font_family;
 609      }
 610  
 611      /* Undocumented */
 612      function set_font_charset($font_charset=1) {
 613          $this->_font_charset=$font_charset;
 614      }
 615  
 616      function set_num_format($num_format=1) {
 617          $this->_num_format=$num_format;
 618      }
 619  
 620      function set_hidden($hidden=1) {
 621          $this->_hidden=$hidden;
 622      }
 623  
 624      function set_locked($locked=1) {
 625          $this->_locked=$locked;
 626      }
 627  
 628      function set_text_h_align($align) {
 629          $this->_text_h_align=$align;
 630      }
 631  
 632      function set_text_wrap($wrap=1) {
 633          $this->_text_wrap=$wrap;
 634      }
 635  
 636      function set_text_v_align($align) {
 637          $this->_text_v_align=$align;
 638      }
 639  
 640      function set_text_justlast($text_justlast=1) {
 641          $this->_text_justlast=$text_justlast;
 642      }
 643  
 644      function set_rotation($rotation=1) {
 645          $this->_rotation=$rotation;
 646      }
 647  
 648      function set_fg_color($color) {
 649          $this->_fg_color=$this->_get_color($color);
 650      }
 651  
 652      function set_bg_color($color) {
 653          $this->_bg_color=$this->_get_color($color);
 654      }
 655  
 656      function set_pattern($pattern=1) {
 657          $this->_pattern=$pattern;
 658      }
 659  
 660      function set_bottom($bottom=1) {
 661          $this->_bottom=$bottom;
 662      }
 663  
 664      function set_top($top=1) {
 665          $this->_top=$top;
 666      }
 667  
 668      function set_left($left=1) {
 669          $this->_left=$left;
 670      }
 671  
 672      function set_right($right=1) {
 673           $this->_right=$right;
 674      }
 675  
 676      function set_bottom_color($color) {
 677          $this->_bottom_color=$this->_get_color($color);
 678      }
 679  
 680      function set_top_color($color) {
 681          $this->_top_color=$this->_get_color($color);
 682      }
 683  
 684      function set_left_color($color) {
 685          $this->_left_color=$this->_get_color($color);
 686      }
 687  
 688      function set_right_color($color) {
 689          $this->_right_color=$this->_get_color($color);
 690      }
 691  
 692  }
 693  
 694  ?>


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7