PHPExcel_Writer_Excel5
[ class tree: PHPExcel_Writer_Excel5 ] [ index: PHPExcel_Writer_Excel5 ] [ all elements ]

Source for file Xf.php

Documentation is available at Xf.php

  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. *    PHPExcel_Writer_Excel5_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. /** PHPExcel_Style_Alignment */
  36. require_once 'PHPExcel/Style/Alignment.php';
  37.  
  38. /** PHPExcel_Style_Border */
  39. require_once 'PHPExcel/Style/Border.php';
  40.  
  41. /** PHPExcel_Style_Fill */
  42. require_once 'PHPExcel/Style/Fill.php';
  43.  
  44. /** PHPExcel_Style_Protection */
  45. require_once 'PHPExcel/Style/Protection.php';
  46.  
  47. /**
  48. * Class for generating Excel XF records (formats)
  49. *
  50. @author   Xavier Noguer <xnoguer@rezebra.com>
  51. @category PHPExcel
  52. @package  PHPExcel_Writer_Excel5
  53. */
  54.  
  55. {
  56.     /**
  57.      * BIFF version
  58.      *
  59.      * @var int 
  60.      */
  61.     private $_BIFFVersion;
  62.  
  63.     /**
  64.      * Style XF or a cell XF ?
  65.      *
  66.      * @var boolean 
  67.      */
  68.     private $_isStyleXf;
  69.  
  70.     /**
  71.     * Index to the FONT record. Index 4 does not exist
  72.     * @var integer 
  73.     */
  74.     private $_fontIndex;
  75.  
  76.     /**
  77.     * An index (2 bytes) to a FORMAT record (number format).
  78.     * @var integer 
  79.     */
  80.     var $_numberFormatIndex;
  81.  
  82.     /**
  83.     * 1 bit, apparently not used.
  84.     * @var integer 
  85.     */
  86.     var $_text_justlast;
  87.  
  88.     /**
  89.     * The cell's foreground color.
  90.     * @var integer 
  91.     */
  92.     var $_fg_color;
  93.  
  94.     /**
  95.     * The cell's background color.
  96.     * @var integer 
  97.     */
  98.     var $_bg_color;
  99.  
  100.     /**
  101.     * Color of the bottom border of the cell.
  102.     * @var integer 
  103.     */
  104.     var $_bottom_color;
  105.  
  106.     /**
  107.     * Color of the top border of the cell.
  108.     * @var integer 
  109.     */
  110.     var $_top_color;
  111.  
  112.     /**
  113.     * Color of the left border of the cell.
  114.     * @var integer 
  115.     */
  116.     var $_left_color;
  117.  
  118.     /**
  119.     * Color of the right border of the cell.
  120.     * @var integer 
  121.     */
  122.     var $_right_color;
  123.  
  124.     /**
  125.     * Constructor
  126.     *
  127.     * @access private
  128.     * @param integer $index the XF index for the format.
  129.     * @param PHPExcel_Style 
  130.     */
  131.     public function __construct($style null)
  132.     {
  133.         $this->_isStyleXf =     false;
  134.         $this->_BIFFVersion   = 0x0600;
  135.         $this->_fontIndex      = 0;
  136.  
  137.         $this->_numberFormatIndex     = 0;
  138.  
  139.         $this->_text_justlast  = 0;
  140.  
  141.         $this->_fg_color       = 0x40;
  142.         $this->_bg_color       = 0x41;
  143.  
  144.         $this->_diag           0;
  145.  
  146.         $this->_bottom_color   = 0x40;
  147.         $this->_top_color      = 0x40;
  148.         $this->_left_color     = 0x40;
  149.         $this->_right_color    = 0x40;
  150.         $this->_diag_color     0x40;
  151.         $this->_style $style;
  152.  
  153.     }
  154.  
  155.  
  156.     /**
  157.     * Generate an Excel BIFF XF record (style or cell).
  158.     *
  159.     * @param string $style The type of the XF record ('style' or 'cell').
  160.     * @return string The XF record
  161.     */
  162.     function writeXf()
  163.     {
  164.         // Set the type of the XF record and some of the attributes.
  165.         if ($this->_isStyleXf{
  166.             $style 0xFFF5;
  167.         else {
  168.             $style   $this->_mapLocked($this->_style->getProtection()->getLocked());
  169.             $style  |= $this->_mapHidden($this->_style->getProtection()->getHidden()) << 1;
  170.         }
  171.  
  172.         // Flags to indicate if attributes have been set.
  173.         $atr_num     ($this->_numberFormatIndex != 0)?1:0;
  174.         $atr_fnt     ($this->_fontIndex != 0)?1:0;
  175.         $atr_alc     ((int) $this->_style->getAlignment()->getWrapText())?1:0;
  176.         $atr_bdr     ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle())   ||
  177.                         $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle())      ||
  178.                         $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle())     ||
  179.                         $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()))?1:0;
  180.         $atr_pat     (($this->_fg_color != 0x40||
  181.                         ($this->_bg_color != 0x41||
  182.                         $this->_mapFillType($this->_style->getFill()->getFillType()))?1:0;
  183.         $atr_prot    $this->_mapLocked($this->_style->getProtection()->getLocked())
  184.                         | $this->_mapHidden($this->_style->getProtection()->getHidden());
  185.  
  186.         // Zero the default border colour if the border has not been set.
  187.         if ($this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle()) == 0{
  188.             $this->_bottom_color = 0;
  189.         }
  190.         if ($this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle())  == 0{
  191.             $this->_top_color = 0;
  192.         }
  193.         if ($this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle()) == 0{
  194.             $this->_right_color = 0;
  195.         }
  196.         if ($this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) == 0{
  197.             $this->_left_color = 0;
  198.         }
  199.         if ($this->_diag == 0{
  200.             $this->_diag_color 0;
  201.         }
  202.  
  203.         $record         0x00E0;              // Record identifier
  204.         if ($this->_BIFFVersion == 0x0500{
  205.             $length         0x0010;              // Number of bytes to follow
  206.         }
  207.         if ($this->_BIFFVersion == 0x0600{
  208.             $length         0x0014;
  209.         }
  210.  
  211.         $ifnt           $this->_fontIndex;   // Index to FONT record
  212.         $ifmt           $this->_numberFormatIndex;  // Index to FORMAT record
  213.         if ($this->_BIFFVersion == 0x0500{
  214.             $align          $this->_mapHAlign($this->_style->getAlignment()->getHorizontal());       // Alignment
  215.             $align         |= (int) $this->_style->getAlignment()->getWrapText()     << 3;
  216.             $align         |= $this->_mapVAlign($this->_style->getAlignment()->getVertical())  << 4;
  217.             $align         |= $this->_text_justlast << 7;
  218.             $align         |= 0                       << 8// rotation
  219.             $align         |= $atr_num                << 10;
  220.             $align         |= $atr_fnt                << 11;
  221.             $align         |= $atr_alc                << 12;
  222.             $align         |= $atr_bdr                << 13;
  223.             $align         |= $atr_pat                << 14;
  224.             $align         |= $atr_prot               << 15;
  225.  
  226.             $icv            $this->_fg_color;       // fg and bg pattern colors
  227.             $icv           |= $this->_bg_color      << 7;
  228.  
  229.             $fill           $this->_mapFillType($this->_style->getFill()->getFillType());        // Fill and border line style
  230.             $fill          |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle())        << 6;
  231.             $fill          |= $this->_bottom_color  << 9;
  232.  
  233.             $border1        $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle());            // Border line style and color
  234.             $border1       |= $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle())          << 3;
  235.             $border1       |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle())         << 6;
  236.             $border1       |= $this->_top_color     << 9;
  237.  
  238.             $border2        $this->_left_color;     // Border color
  239.             $border2       |= $this->_right_color   << 7;
  240.  
  241.             $header      pack("vv",       $record$length);
  242.             $data        pack("vvvvvvvv"$ifnt$ifmt$style$align,
  243.                                             $icv$fill,
  244.                                             $border1$border2);
  245.         elseif ($this->_BIFFVersion == 0x0600{
  246.             $align          $this->_mapHAlign($this->_style->getAlignment()->getHorizontal());       // Alignment
  247.             $align         |= (int) $this->_style->getAlignment()->getWrapText()     << 3;
  248.             $align         |= $this->_mapVAlign($this->_style->getAlignment()->getVertical())  << 4;
  249.             $align         |= $this->_text_justlast << 7;
  250.  
  251.             $used_attrib    $atr_num              << 2;
  252.             $used_attrib   |= $atr_fnt              << 3;
  253.             $used_attrib   |= $atr_alc              << 4;
  254.             $used_attrib   |= $atr_bdr              << 5;
  255.             $used_attrib   |= $atr_pat              << 6;
  256.             $used_attrib   |= $atr_prot             << 7;
  257.  
  258.             $icv            $this->_fg_color;      // fg and bg pattern colors
  259.             $icv           |= $this->_bg_color      << 7;
  260.  
  261.             $border1        $this->_mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle());          // Border line style and color
  262.             $border1       |= $this->_mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle())         << 4;
  263.             $border1       |= $this->_mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle())           << 8;
  264.             $border1       |= $this->_mapBorderStyle($this->_style->getBorders()->getBottom()->getBorderStyle())        << 12;
  265.             $border1       |= $this->_left_color    << 16;
  266.             $border1       |= $this->_right_color   << 23;
  267.             $diag_tl_to_rb 0// FIXME: add method
  268.             $diag_tr_to_lb 0// FIXME: add method
  269.             $border1       |= $diag_tl_to_rb        << 30;
  270.             $border1       |= $diag_tr_to_lb        << 31;
  271.  
  272.             $border2        $this->_top_color;    // Border color
  273.             $border2       |= $this->_bottom_color   << 7;
  274.             $border2       |= $this->_diag_color     << 14;
  275.             $border2       |= $this->_diag           << 21;
  276.             $border2       |= $this->_mapFillType($this->_style->getFill()->getFillType())        << 26;
  277.  
  278.             $header      pack("vv",       $record$length);
  279.  
  280.             //BIFF8 options: identation, shrinkToFit and  text direction
  281.             $biff8_options  $this->_style->getAlignment()->getIndent();
  282.             $biff8_options |= (int) $this->_style->getAlignment()->getShrinkToFit(<< 4;
  283.  
  284.             $data  pack("vvvC"$ifnt$ifmt$style$align);
  285.             $data .= pack("CCC"
  286.                 $this->_mapTextRotation($this->_style->getAlignment()->getTextRotation())
  287.                 $biff8_options
  288.                 $used_attrib
  289.                 );
  290.             $data .= pack("VVv"$border1$border2$icv);
  291.         }
  292.  
  293.         return($header $data);
  294.     }
  295.  
  296.     /**
  297.      * Set BIFF version
  298.      *
  299.      * @param int $BIFFVersion 
  300.      */
  301.     public function setBIFFVersion($BIFFVersion)
  302.     {
  303.         $this->_BIFFVersion = $BIFFVersion;
  304.     }
  305.  
  306.     /**
  307.      * Is this a style XF ?
  308.      *
  309.      * @param boolean $value 
  310.      */
  311.     public function setIsStyleXf($value)
  312.     {
  313.         $this->_isStyleXf = $value;
  314.     }
  315.  
  316.     /**
  317.     * Sets the cell's bottom border color
  318.     *
  319.     * @access public
  320.     * @param int $colorIndex Color index
  321.     */
  322.     function setBottomColor($colorIndex)
  323.     {
  324.         $this->_bottom_color = $colorIndex;
  325.     }
  326.  
  327.     /**
  328.     * Sets the cell's top border color
  329.     *
  330.     * @access public
  331.     * @param int $colorIndex Color index
  332.     */
  333.     function setTopColor($colorIndex)
  334.     {
  335.         $this->_top_color = $colorIndex;
  336.     }
  337.  
  338.     /**
  339.     * Sets the cell's left border color
  340.     *
  341.     * @access public
  342.     * @param int $colorIndex Color index
  343.     */
  344.     function setLeftColor($colorIndex)
  345.     {
  346.         $this->_left_color = $colorIndex;
  347.     }
  348.  
  349.     /**
  350.     * Sets the cell's right border color
  351.     *
  352.     * @access public
  353.     * @param int $colorIndex Color index
  354.     */
  355.     function setRightColor($colorIndex)
  356.     {
  357.         $this->_right_color = $colorIndex;
  358.     }
  359.  
  360.  
  361.     /**
  362.     * Sets the cell's foreground color
  363.     *
  364.     * @access public
  365.     * @param int $colorIndex Color index
  366.     */
  367.     function setFgColor($colorIndex)
  368.     {
  369.         $this->_fg_color = $colorIndex;
  370.     }
  371.  
  372.     /**
  373.     * Sets the cell's background color
  374.     *
  375.     * @access public
  376.     * @param int $colorIndex Color index
  377.     */
  378.     function setBgColor($colorIndex)
  379.     {
  380.         $this->_bg_color = $colorIndex;
  381.     }
  382.  
  383.     /**
  384.     * Sets the index to the number format record
  385.     * It can be date, time, currency, etc...
  386.     *
  387.     * @access public
  388.     * @param integer $numberFormatIndex Index to format record
  389.     */
  390.     function setNumberFormatIndex($numberFormatIndex)
  391.     {
  392.         $this->_numberFormatIndex = $numberFormatIndex;
  393.     }
  394.  
  395.     /**
  396.      * Set the font index.
  397.      *
  398.      * @param int $value Font index, note that value 4 does not exist
  399.      */
  400.     public function setFontIndex($value)
  401.     {
  402.         $this->_fontIndex = $value;
  403.     }
  404.  
  405.     /**
  406.      * Map border style
  407.      */
  408.     private function _mapBorderStyle($borderStyle{
  409.         switch ($borderStyle{
  410.             case PHPExcel_Style_Border::BORDER_NONE:                return 0x00;
  411.             case PHPExcel_Style_Border::BORDER_THIN;                return 0x01;
  412.             case PHPExcel_Style_Border::BORDER_MEDIUM;                return 0x02;
  413.             case PHPExcel_Style_Border::BORDER_DASHED;                return 0x03;
  414.             case PHPExcel_Style_Border::BORDER_DOTTED;                return 0x04;
  415.             case PHPExcel_Style_Border::BORDER_THICK;                return 0x05;
  416.             case PHPExcel_Style_Border::BORDER_DOUBLE;                return 0x06;
  417.             case PHPExcel_Style_Border::BORDER_HAIR;                return 0x07;
  418.             case PHPExcel_Style_Border::BORDER_MEDIUMDASHED;        return 0x08;
  419.             case PHPExcel_Style_Border::BORDER_DASHDOT;                return 0x09;
  420.             case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT;        return 0x0A;
  421.             case PHPExcel_Style_Border::BORDER_DASHDOTDOT;            return 0x0B;
  422.             case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;    return 0x0C;
  423.             case PHPExcel_Style_Border::BORDER_SLANTDASHDOT;        return 0x0D;
  424.             default:                                                return 0x00;
  425.         }
  426.     }
  427.  
  428.     /**
  429.      * Map fill type
  430.      */
  431.     private function _mapFillType($fillType{
  432.         switch ($fillType{
  433.             case PHPExcel_Style_Fill::FILL_NONE:                    return 0x00;
  434.             case PHPExcel_Style_Fill::FILL_SOLID:                    return 0x01;
  435.             case PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY:        return 0x02;
  436.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY:        return 0x03;
  437.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY:        return 0x04;
  438.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL:    return 0x05;
  439.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL:    return 0x06;
  440.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN:        return 0x07;
  441.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKUP:            return 0x08;
  442.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID:        return 0x09;
  443.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS:        return 0x0A;
  444.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL:    return 0x0B;
  445.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL:    return 0x0C;
  446.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN:        return 0x0D;
  447.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP:            return 0x0E;
  448.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID:        return 0x0F;
  449.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS:    return 0x10;
  450.             case PHPExcel_Style_Fill::FILL_PATTERN_GRAY125:            return 0x11;
  451.             case PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625:        return 0x12;
  452.             case PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR:        // does not exist in BIFF8
  453.             case PHPExcel_Style_Fill::FILL_GRADIENT_PATH:        // does not exist in BIFF8
  454.             default:                                                return 0x00;
  455.         }
  456.     }
  457.  
  458.     /**
  459.      * Map to BIFF2-BIFF8 codes for horizontal alignment
  460.      *
  461.      * @param string $hAlign 
  462.      * @return int 
  463.      */
  464.     private function _mapHAlign($hAlign)
  465.     {
  466.         switch ($hAlign{
  467.             case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL:    return 0;
  468.             case PHPExcel_Style_Alignment::HORIZONTAL_LEFT:        return 1;
  469.             case PHPExcel_Style_Alignment::HORIZONTAL_CENTER:    return 2;
  470.             case PHPExcel_Style_Alignment::HORIZONTAL_RIGHT:    return 3;
  471.             case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY:    return 5;
  472.             default:                                            return 0;
  473.         }
  474.     }
  475.  
  476.     /**
  477.      * Map to BIFF2-BIFF8 codes for vertical alignment
  478.      *
  479.      * @param string $vAlign 
  480.      * @return int 
  481.      */
  482.     private function _mapVAlign($vAlign{
  483.         switch ($vAlign{
  484.             case PHPExcel_Style_Alignment::VERTICAL_TOP:        return 0;
  485.             case PHPExcel_Style_Alignment::VERTICAL_CENTER:        return 1;
  486.             case PHPExcel_Style_Alignment::VERTICAL_BOTTOM:        return 2;
  487.             case PHPExcel_Style_Alignment::VERTICAL_JUSTIFY:    return 3;
  488.             default:                                            return 2;
  489.         }
  490.     }
  491.  
  492.     /**
  493.      * Map to BIFF8 codes for text rotation angle
  494.      *
  495.      * @param int $textRotation 
  496.      * @return int 
  497.      */
  498.     private function _mapTextRotation($textRotation{
  499.         if ($textRotation >= 0{
  500.             return $textRotation;
  501.         }
  502.         if ($textRotation == -165{
  503.             return 255;
  504.         }
  505.         if ($textRotation 0{
  506.             return 90 $textRotation;
  507.         }
  508.     }
  509.  
  510.     /**
  511.      * Map locked
  512.      *
  513.      * @param string 
  514.      * @return int 
  515.      */
  516.     private function _mapLocked($locked{
  517.         switch ($locked{
  518.             case PHPExcel_Style_Protection::PROTECTION_INHERIT:        return 1;
  519.             case PHPExcel_Style_Protection::PROTECTION_PROTECTED:    return 1;
  520.             case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED:    return 0;
  521.             default:                                                return 1;
  522.         }
  523.     }
  524.  
  525.     /**
  526.      * Map hidden
  527.      *
  528.      * @param string 
  529.      * @return int 
  530.      */
  531.     private function _mapHidden($hidden{
  532.         switch ($hidden{
  533.             case PHPExcel_Style_Protection::PROTECTION_INHERIT:        return 0;
  534.             case PHPExcel_Style_Protection::PROTECTION_PROTECTED:    return 1;
  535.             case PHPExcel_Style_Protection::PROTECTION_UNPROTECTED:    return 0;
  536.             default:                                                return 0;
  537.         }
  538.     }
  539.  
  540. }

Documentation generated on Wed, 22 Apr 2009 09:07:08 +0200 by phpDocumentor 1.4.1