• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/Spreadsheet/Excel/Writer/Format.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003 *  Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
00004 *
00005 *  The majority of this is _NOT_ my code.  I simply ported it from the
00006 *  PERL Spreadsheet::WriteExcel module.
00007 *
00008 *  The author of the Spreadsheet::WriteExcel module is John McNamara 
00009 *  <jmcnamara@cpan.org>
00010 *
00011 *  I _DO_ maintain this code, and John McNamara has nothing to do with the
00012 *  porting of this code to PHP.  Any questions directly related to this
00013 *  class library should be directed to me.
00014 *
00015 *  License Information:
00016 *
00017 *    Spreadsheet_Excel_Writer:  A library for generating Excel Spreadsheets
00018 *    Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
00019 *
00020 *    This library is free software; you can redistribute it and/or
00021 *    modify it under the terms of the GNU Lesser General Public
00022 *    License as published by the Free Software Foundation; either
00023 *    version 2.1 of the License, or (at your option) any later version.
00024 *
00025 *    This library is distributed in the hope that it will be useful,
00026 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00028 *    Lesser General Public License for more details.
00029 *
00030 *    You should have received a copy of the GNU Lesser General Public
00031 *    License along with this library; if not, write to the Free Software
00032 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00033 */
00034 
00035 require_once('PEAR.php');
00036 
00045 class Spreadsheet_Excel_Writer_Format extends PEAR
00046 {
00051     var $_xf_index;
00052 
00057     var $font_index;
00058 
00063     var $_font_name;
00064 
00069     var $_size;
00070 
00075     var $_bold;
00076 
00081     var $_italic;
00082 
00087     var $_color;
00088 
00093     var $_underline;
00094 
00099     var $_font_strikeout;
00100 
00105     var $_font_outline;
00106 
00111     var $_font_shadow;
00112 
00117     var $_font_script;
00118 
00123     var $_font_family;
00124 
00129     var $_font_charset;
00130 
00135     var $_num_format;
00136 
00141     var $_hidden;
00142 
00147     var $_locked;
00148 
00153     var $_text_h_align;
00154 
00159     var $_text_wrap;
00160 
00165     var $_text_v_align;
00166 
00171     var $_text_justlast;
00172 
00177     var $_rotation;
00178 
00183     var $_fg_color;
00184 
00189     var $_bg_color;
00190 
00195     var $_pattern;
00196 
00201     var $_bottom;
00202 
00207     var $_bottom_color;
00208 
00213     var $_top;
00214 
00219     var $_top_color;
00220 
00225     var $_left;
00226 
00231     var $_left_color;
00232 
00237     var $_right;
00238 
00243     var $_right_color;
00244 
00252     function Spreadsheet_Excel_Writer_Format($BIFF_version, $index = 0, $properties =  array())
00253     {
00254         $this->_xf_index       = $index;
00255         $this->_BIFF_version   = $BIFF_version;
00256         $this->font_index      = 0;
00257         $this->_font_name      = 'Arial';
00258         $this->_size           = 10;
00259         $this->_bold           = 0x0190;
00260         $this->_italic         = 0;
00261         $this->_color          = 0x7FFF;
00262         $this->_underline      = 0;
00263         $this->_font_strikeout = 0;
00264         $this->_font_outline   = 0;
00265         $this->_font_shadow    = 0;
00266         $this->_font_script    = 0;
00267         $this->_font_family    = 0;
00268         $this->_font_charset   = 0;
00269                                
00270         $this->_num_format     = 0;
00271                                
00272         $this->_hidden         = 0;
00273         $this->_locked         = 0;
00274 
00275         $this->_text_h_align   = 0;
00276         $this->_text_wrap      = 0;
00277         $this->_text_v_align   = 2;
00278         $this->_text_justlast  = 0;
00279         $this->_rotation       = 0;
00280 
00281         $this->_fg_color       = 0x40;
00282         $this->_bg_color       = 0x41;
00283 
00284         $this->_pattern        = 0;
00285                                
00286         $this->_bottom         = 0;
00287         $this->_top            = 0;
00288         $this->_left           = 0;
00289         $this->_right          = 0;
00290         $this->_diag           = 0;
00291                                
00292         $this->_bottom_color   = 0x40;
00293         $this->_top_color      = 0x40;
00294         $this->_left_color     = 0x40;
00295         $this->_right_color    = 0x40;
00296         $this->_diag_color     = 0x40;
00297     
00298         // Set properties passed to Spreadsheet_Excel_Writer_Workbook::addFormat()
00299         foreach($properties as $property => $value)
00300         {
00301             if(method_exists($this,'set'.ucwords($property)))
00302             {
00303                 $method_name = 'set'.ucwords($property);
00304                 $this->$method_name($value);
00305             }
00306         }
00307     }
00308 
00309 
00316     function getXf($style)
00317     {
00318         // Set the type of the XF record and some of the attributes.
00319         if ($style == "style") {
00320             $style = 0xFFF5;
00321         }
00322         else {
00323             $style   = $this->_locked;
00324             $style  |= $this->_hidden << 1;
00325         }
00326     
00327         // Flags to indicate if attributes have been set.
00328         $atr_num     = ($this->_num_format != 0)?1:0;
00329         $atr_fnt     = ($this->font_index != 0)?1:0;
00330         $atr_alc     = ($this->_text_wrap)?1:0;
00331         $atr_bdr     = ($this->_bottom   ||
00332                         $this->_top      ||
00333                         $this->_left     ||
00334                         $this->_right)?1:0;
00335         $atr_pat     = (($this->_fg_color != 0x40) ||
00336                         ($this->_bg_color != 0x41) ||
00337                         $this->_pattern)?1:0;
00338         $atr_prot    = $this->_locked | $this->_hidden;
00339     
00340         // Zero the default border colour if the border has not been set.
00341         if ($this->_bottom == 0) {
00342             $this->_bottom_color = 0;
00343         }
00344         if ($this->_top  == 0) {
00345             $this->_top_color = 0;
00346         }
00347         if ($this->_right == 0) {
00348             $this->_right_color = 0;
00349         }
00350         if ($this->_left == 0) {
00351             $this->_left_color = 0;
00352         }
00353         if ($this->_diag == 0) {
00354             $this->_diag_color = 0;
00355         }
00356     
00357         $record         = 0x00E0;              // Record identifier
00358         if ($this->_BIFF_version == 0x0500) {
00359             $length         = 0x0010;              // Number of bytes to follow
00360         }
00361         if ($this->_BIFF_version == 0x0600) {
00362             $length         = 0x0014;
00363         }
00364                                                
00365         $ifnt           = $this->font_index;   // Index to FONT record
00366         $ifmt           = $this->_num_format;  // Index to FORMAT record
00367         if ($this->_BIFF_version == 0x0500)
00368         {
00369             $align          = $this->_text_h_align;       // Alignment
00370             $align         |= $this->_text_wrap     << 3;
00371             $align         |= $this->_text_v_align  << 4;
00372             $align         |= $this->_text_justlast << 7;
00373             $align         |= $this->_rotation      << 8;
00374             $align         |= $atr_num                << 10;
00375             $align         |= $atr_fnt                << 11;
00376             $align         |= $atr_alc                << 12;
00377             $align         |= $atr_bdr                << 13;
00378             $align         |= $atr_pat                << 14;
00379             $align         |= $atr_prot               << 15;
00380  
00381             $icv            = $this->_fg_color;       // fg and bg pattern colors
00382             $icv           |= $this->_bg_color      << 7;
00383      
00384             $fill           = $this->_pattern;        // Fill and border line style
00385             $fill          |= $this->_bottom        << 6;
00386             $fill          |= $this->_bottom_color  << 9;
00387      
00388             $border1        = $this->_top;            // Border line style and color
00389             $border1       |= $this->_left          << 3;
00390             $border1       |= $this->_right         << 6;
00391             $border1       |= $this->_top_color     << 9;
00392      
00393             $border2        = $this->_left_color;     // Border color
00394             $border2       |= $this->_right_color   << 7;
00395          
00396             $header      = pack("vv",       $record, $length);
00397             $data        = pack("vvvvvvvv", $ifnt, $ifmt, $style, $align,
00398                                             $icv, $fill,
00399                                             $border1, $border2);
00400         }
00401         elseif ($this->_BIFF_version == 0x0600)
00402         {
00403             $align          = $this->_text_h_align;       // Alignment
00404             $align         |= $this->_text_wrap     << 3;
00405             $align         |= $this->_text_v_align  << 4;
00406             $align         |= $this->_text_justlast << 7;
00407 
00408             $used_attrib    = $atr_num              << 2;
00409             $used_attrib   |= $atr_fnt              << 3;
00410             $used_attrib   |= $atr_alc              << 4;
00411             $used_attrib   |= $atr_bdr              << 5;
00412             $used_attrib   |= $atr_pat              << 6;
00413             $used_attrib   |= $atr_prot             << 7;
00414 
00415             $icv            = $this->_fg_color;      // fg and bg pattern colors
00416             $icv           |= $this->_bg_color      << 7;
00417    
00418             $border1        = $this->_left;          // Border line style and color
00419             $border1       |= $this->_right         << 4;
00420             $border1       |= $this->_top           << 8;
00421             $border1       |= $this->_bottom        << 12;
00422             $border1       |= $this->_left_color    << 16;
00423             $border1       |= $this->_right_color   << 23;
00424             $diag_tl_to_rb = 0; // FIXME: add method
00425             $diag_tr_to_lb = 0; // FIXME: add method
00426             $border1       |= $diag_tl_to_rb        << 30;
00427             $border1       |= $diag_tr_to_lb        << 31;
00428      
00429             $border2        = $this->_top_color;    // Border color
00430             $border2       |= $this->_bottom_color   << 7;
00431             $border2       |= $this->_diag_color     << 14;
00432             $border2       |= $this->_diag           << 21;
00433             $border2       |= $this->_pattern        << 26;
00434 
00435             $header      = pack("vv",       $record, $length);
00436 
00437             $rotation      = 0x00;
00438             $biff8_options = 0x00;
00439             $data  = pack("vvvC", $ifnt, $ifmt, $style, $align);
00440             $data .= pack("CCC", $rotation, $biff8_options, $used_attrib);
00441             $data .= pack("VVv", $border1, $border2, $icv);
00442         }
00443 
00444         return($header.$data);
00445     }
00446     
00452     function getFont()
00453     {
00454         $dyHeight   = $this->_size * 20;    // Height of font (1/20 of a point)
00455         $icv        = $this->_color;        // Index to color palette
00456         $bls        = $this->_bold;         // Bold style
00457         $sss        = $this->_font_script;  // Superscript/subscript
00458         $uls        = $this->_underline;    // Underline
00459         $bFamily    = $this->_font_family;  // Font family
00460         $bCharSet   = $this->_font_charset; // Character set
00461         $encoding   = 0;                    // TODO: Unicode support
00462     
00463         $cch        = strlen($this->_font_name); // Length of font name
00464         $record     = 0x31;                      // Record identifier
00465         if ($this->_BIFF_version == 0x0500) {
00466             $length     = 0x0F + $cch;            // Record length
00467         }
00468         elseif ($this->_BIFF_version == 0x0600) {
00469             $length     = 0x10 + $cch;
00470         }
00471         $reserved   = 0x00;                // Reserved
00472         $grbit      = 0x00;                // Font attributes
00473         if ($this->_italic) {
00474             $grbit     |= 0x02;
00475         }
00476         if ($this->_font_strikeout) {
00477             $grbit     |= 0x08;
00478         }
00479         if ($this->_font_outline) {
00480             $grbit     |= 0x10;
00481         }
00482         if ($this->_font_shadow) {
00483             $grbit     |= 0x20;
00484         }
00485     
00486         $header  = pack("vv",         $record, $length);
00487         if ($this->_BIFF_version == 0x0500) {
00488             $data    = pack("vvvvvCCCCC", $dyHeight, $grbit, $icv, $bls,
00489                                           $sss, $uls, $bFamily,
00490                                           $bCharSet, $reserved, $cch);
00491         }
00492         elseif ($this->_BIFF_version == 0x0600) {
00493             $data    = pack("vvvvvCCCCCC", $dyHeight, $grbit, $icv, $bls,
00494                                            $sss, $uls, $bFamily,
00495                                            $bCharSet, $reserved, $cch, $encoding);
00496         }
00497         return($header . $data. $this->_font_name);
00498     }
00499     
00510     function getFontKey()
00511     {
00512         $key  = "$this->_font_name$this->_size";
00513         $key .= "$this->_font_script$this->_underline";
00514         $key .= "$this->_font_strikeout$this->_bold$this->_font_outline";
00515         $key .= "$this->_font_family$this->_font_charset";
00516         $key .= "$this->_font_shadow$this->_color$this->_italic";
00517         $key  = str_replace(" ","_",$key);
00518         return ($key);
00519     }
00520     
00526     function getXfIndex()
00527     {
00528         return($this->_xf_index);
00529     }
00530     
00540     function _getColor($name_color = '')
00541     {
00542         $colors = array(
00543                         'aqua'    => 0x0F,
00544                         'cyan'    => 0x0F,
00545                         'black'   => 0x08,
00546                         'blue'    => 0x0C,
00547                         'brown'   => 0x10,
00548                         'magenta' => 0x0E,
00549                         'fuchsia' => 0x0E,
00550                         'gray'    => 0x17,
00551                         'grey'    => 0x17,
00552                         'green'   => 0x11,
00553                         'lime'    => 0x0B,
00554                         'navy'    => 0x12,
00555                         'orange'  => 0x35,
00556                         'purple'  => 0x14,
00557                         'red'     => 0x0A,
00558                         'silver'  => 0x16,
00559                         'white'   => 0x09,
00560                         'yellow'  => 0x0D
00561                        );
00562     
00563         // Return the default color, 0x7FFF, if undef,
00564         if($name_color == '') {
00565             return(0x7FFF);
00566         }
00567     
00568         // or the color string converted to an integer,
00569         if(isset($colors[$name_color])) {
00570             return($colors[$name_color]);
00571         }
00572     
00573         // or the default color if string is unrecognised,
00574         if(preg_match("/\D/",$name_color)) {
00575             return(0x7FFF);
00576         }
00577     
00578         // or an index < 8 mapped into the correct range,
00579         if($name_color < 8) {
00580             return($name_color + 8);
00581         }
00582     
00583         // or the default color if arg is outside range,
00584         if($name_color > 63) {
00585             return(0x7FFF);
00586         }
00587     
00588         // or an integer in the valid range
00589         return($name_color);
00590     }
00591     
00598     function setAlign($location)
00599     {
00600         if (preg_match("/\d/",$location)) {
00601             return;                      // Ignore numbers
00602         }
00603     
00604         $location = strtolower($location);
00605     
00606         if ($location == 'left') {
00607             $this->_text_h_align = 1;
00608         }
00609         if ($location == 'centre') {
00610             $this->_text_h_align = 2;
00611         }
00612         if ($location == 'center') {
00613             $this->_text_h_align = 2;
00614         }
00615         if ($location == 'right') {
00616             $this->_text_h_align = 3;
00617         }
00618         if ($location == 'fill') {
00619             $this->_text_h_align = 4;
00620         }
00621         if ($location == 'justify') {
00622             $this->_text_h_align = 5;
00623         }
00624         if ($location == 'merge') {
00625             $this->_text_h_align = 6;
00626         }
00627         if ($location == 'equal_space') { // For T.K.
00628             $this->_text_h_align = 7;
00629         }
00630         if ($location == 'top') {
00631             $this->_text_v_align = 0;
00632         }
00633         if ($location == 'vcentre') {
00634             $this->_text_v_align = 1;
00635         }
00636         if ($location == 'vcenter') {
00637             $this->_text_v_align = 1;
00638         }
00639         if ($location == 'bottom') {
00640             $this->_text_v_align = 2;
00641         }
00642         if ($location == 'vjustify') {
00643             $this->_text_v_align = 3;
00644         }
00645         if ($location == 'vequal_space') { // For T.K.
00646             $this->_text_v_align = 4;
00647         }
00648     }
00649     
00655     function setMerge()
00656     {
00657         $this->setAlign('merge');
00658     }
00659     
00670     function setBold($weight = 1)
00671     {
00672         if($weight == 1) {
00673             $weight = 0x2BC;  // Bold text
00674         }
00675         if($weight == 0) {
00676             $weight = 0x190;  // Normal text
00677         }
00678         if($weight <  0x064) {
00679             $weight = 0x190;  // Lower bound
00680         }
00681         if($weight >  0x3E8) {
00682             $weight = 0x190;  // Upper bound
00683         }
00684         $this->_bold = $weight;
00685     }
00686     
00687     
00688     /************************************
00689     * FUNCTIONS FOR SETTING CELLS BORDERS
00690     */
00691     
00698     function setBottom($style)
00699     {
00700         $this->_bottom = $style;
00701     }
00702     
00709     function setTop($style)
00710     {
00711         $this->_top = $style;
00712     }
00713     
00720     function setLeft($style)
00721     {
00722         $this->_left = $style;
00723     }
00724     
00731     function setRight($style)
00732     {
00733         $this->_right = $style;
00734     }
00735     
00736     
00743     function setBorder($style)
00744     {
00745         $this->setBottom($style);
00746         $this->setTop($style);
00747         $this->setLeft($style);
00748         $this->setRight($style);
00749     }
00750     
00751     
00752     /*******************************************
00753     * FUNCTIONS FOR SETTING CELLS BORDERS COLORS
00754     */
00755     
00763     function setBorderColor($color)
00764     {
00765         $this->setBottomColor($color);
00766         $this->setTopColor($color);
00767         $this->setLeftColor($color);
00768         $this->setRightColor($color);
00769     }
00770     
00777     function setBottomColor($color)
00778     {
00779         $value = $this->_getColor($color);
00780         $this->_bottom_color = $value;
00781     }
00782     
00789     function setTopColor($color)
00790     {
00791         $value = $this->_getColor($color);
00792         $this->_top_color = $value;
00793     }
00794     
00801     function setLeftColor($color)
00802     {
00803         $value = $this->_getColor($color);
00804         $this->_left_color = $value;
00805     }
00806     
00813     function setRightColor($color)
00814     {
00815         $value = $this->_getColor($color);
00816         $this->_right_color = $value;
00817     }
00818     
00819     
00826     function setFgColor($color)
00827     {
00828         $value = $this->_getColor($color);
00829         $this->_fg_color = $value;
00830         if ($this->_pattern == 0) { // force color to be seen
00831             $this->_pattern = 1;
00832         }
00833     }
00834       
00841     function setBgColor($color)
00842     {
00843         $value = $this->_getColor($color);
00844         $this->_bg_color = $value;
00845         if ($this->_pattern == 0) { // force color to be seen
00846             $this->_pattern = 1;
00847         }
00848     }
00849     
00856     function setColor($color)
00857     {
00858         $value = $this->_getColor($color);
00859         $this->_color = $value;
00860     }
00861     
00869     function setPattern($arg = 1)
00870     {
00871         $this->_pattern = $arg;
00872     }
00873     
00881     function setUnderline($underline)
00882     {
00883         $this->_underline = $underline;
00884     }
00885  
00891     function setItalic()
00892     {
00893         $this->_italic = 1;
00894     }
00895 
00902     function setSize($size)
00903     {
00904         $this->_size = $size;
00905     }
00906     
00912     function setTextWrap()
00913     {
00914         $this->_text_wrap = 1;
00915     }
00916 
00924     function setTextRotation($angle)
00925     {
00926         switch ($angle)
00927         {
00928             case 0:
00929                 $this->_rotation = 0;
00930                 break;
00931             case 90:
00932                 $this->_rotation = 3;
00933                 break;
00934             case 270:
00935                 $this->_rotation = 2;
00936                 break;
00937             case -1:
00938                 $this->_rotation = 1;
00939                 break;
00940             default :
00941                 return $this->raiseError("Invalid value for angle.".
00942                                   " Possible values are: 0, 90, 270 and -1 ".
00943                                   "for stacking top-to-bottom.");
00944                 $this->_rotation = 0;
00945                 break;
00946         }
00947     }
00948 
00956     function setNumFormat($num_format)
00957     {
00958         $this->_num_format = $num_format;
00959     }
00960 
00966     function setStrikeOut()
00967     {
00968         $this->_font_strikeout = 1;
00969     }
00970 
00976     function setOutLine()
00977     {
00978         $this->_font_outline = 1;
00979     }
00980 
00986     function setShadow()
00987     {
00988         $this->_font_shadow = 1;
00989     }
00990 
00998     function setScript($script)
00999     {
01000         $this->_font_script = $script;
01001     }
01002 
01008      function setLocked()
01009      {
01010          $this->_locked = 1;
01011      }
01012 
01018     function setUnLocked()
01019     {
01020         $this->_locked = 0;
01021     }
01022 
01030     function setFontFamily($font_family)
01031     {
01032         $this->_font_name = $font_family;
01033     }
01034 }
01035 ?>

Generated on Fri Dec 13 2013 13:52:08 for ILIAS Release_3_7_x_branch .rev 46817 by  doxygen 1.7.1