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

Source for file Worksheet.php

Documentation is available at Worksheet.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. *    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('Spreadsheet/Excel/Writer/Parser.php');
  36. require_once('Spreadsheet/Excel/Writer/BIFFwriter.php');
  37.  
  38. /**
  39. * Class for generating Excel Spreadsheets
  40. *
  41. @author   Xavier Noguer <xnoguer@rezebra.com>
  42. @category FileFormats
  43. @package  Spreadsheet_Excel_Writer
  44. */
  45.  
  46. {
  47.     /**
  48.     * Name of the Worksheet
  49.     * @var string 
  50.     */
  51.     var $name;
  52.  
  53.     /**
  54.     * Index for the Worksheet
  55.     * @var integer 
  56.     */
  57.     var $index;
  58.  
  59.     /**
  60.     * Reference to the (default) Format object for URLs
  61.     * @var object Format 
  62.     */
  63.     var $_url_format;
  64.  
  65.     /**
  66.     * Reference to the parser used for parsing formulas
  67.     * @var object Format 
  68.     */
  69.     var $_parser;
  70.  
  71.     /**
  72.     * Filehandle to the temporary file for storing data
  73.     * @var resource 
  74.     */
  75.     var $_filehandle;
  76.  
  77.     /**
  78.     * Boolean indicating if we are using a temporary file for storing data
  79.     * @var bool 
  80.     */
  81.     var $_using_tmpfile;
  82.  
  83.     /**
  84.     * Maximum number of rows for an Excel spreadsheet (BIFF5)
  85.     * @var integer 
  86.     */
  87.     var $_xls_rowmax;
  88.  
  89.     /**
  90.     * Maximum number of columns for an Excel spreadsheet (BIFF5)
  91.     * @var integer 
  92.     */
  93.     var $_xls_colmax;
  94.  
  95.     /**
  96.     * Maximum number of characters for a string (LABEL record in BIFF5)
  97.     * @var integer 
  98.     */
  99.     var $_xls_strmax;
  100.  
  101.     /**
  102.     * First row for the DIMENSIONS record
  103.     * @var integer 
  104.     * @see _storeDimensions()
  105.     */
  106.     var $_dim_rowmin;
  107.  
  108.     /**
  109.     * Last row for the DIMENSIONS record
  110.     * @var integer 
  111.     * @see _storeDimensions()
  112.     */
  113.     var $_dim_rowmax;
  114.  
  115.     /**
  116.     * First column for the DIMENSIONS record
  117.     * @var integer 
  118.     * @see _storeDimensions()
  119.     */
  120.     var $_dim_colmin;
  121.  
  122.     /**
  123.     * Last column for the DIMENSIONS record
  124.     * @var integer 
  125.     * @see _storeDimensions()
  126.     */
  127.     var $_dim_colmax;
  128.  
  129.     /**
  130.     * Array containing format information for columns
  131.     * @var array 
  132.     */
  133.     var $_colinfo;
  134.  
  135.     /**
  136.     * Array containing the selected area for the worksheet
  137.     * @var array 
  138.     */
  139.     var $_selection;
  140.  
  141.     /**
  142.     * Array containing the panes for the worksheet
  143.     * @var array 
  144.     */
  145.     var $_panes;
  146.  
  147.     /**
  148.     * The active pane for the worksheet
  149.     * @var integer 
  150.     */
  151.     var $_active_pane;
  152.  
  153.     /**
  154.     * Bit specifying if panes are frozen
  155.     * @var integer 
  156.     */
  157.     var $_frozen;
  158.  
  159.     /**
  160.     * Bit specifying if the worksheet is selected
  161.     * @var integer 
  162.     */
  163.     var $selected;
  164.  
  165.     /**
  166.     * The paper size (for printing) (DOCUMENT!!!)
  167.     * @var integer 
  168.     */
  169.     var $_paper_size;
  170.  
  171.     /**
  172.     * Bit specifying paper orientation (for printing). 0 => landscape, 1 => portrait
  173.     * @var integer 
  174.     */
  175.     var $_orientation;
  176.  
  177.     /**
  178.     * The page header caption
  179.     * @var string 
  180.     */
  181.     var $_header;
  182.  
  183.     /**
  184.     * The page footer caption
  185.     * @var string 
  186.     */
  187.     var $_footer;
  188.  
  189.     /**
  190.     * The horizontal centering value for the page
  191.     * @var integer 
  192.     */
  193.     var $_hcenter;
  194.  
  195.     /**
  196.     * The vertical centering value for the page
  197.     * @var integer 
  198.     */
  199.     var $_vcenter;
  200.  
  201.     /**
  202.     * The margin for the header
  203.     * @var float 
  204.     */
  205.     var $_margin_head;
  206.  
  207.     /**
  208.     * The margin for the footer
  209.     * @var float 
  210.     */
  211.     var $_margin_foot;
  212.  
  213.     /**
  214.     * The left margin for the worksheet in inches
  215.     * @var float 
  216.     */
  217.     var $_margin_left;
  218.  
  219.     /**
  220.     * The right margin for the worksheet in inches
  221.     * @var float 
  222.     */
  223.     var $_margin_right;
  224.  
  225.     /**
  226.     * The top margin for the worksheet in inches
  227.     * @var float 
  228.     */
  229.     var $_margin_top;
  230.  
  231.     /**
  232.     * The bottom margin for the worksheet in inches
  233.     * @var float 
  234.     */
  235.     var $_margin_bottom;
  236.  
  237.     /**
  238.     * First row to reapeat on each printed page
  239.     * @var integer 
  240.     */
  241.     var $title_rowmin;
  242.  
  243.     /**
  244.     * Last row to reapeat on each printed page
  245.     * @var integer 
  246.     */
  247.     var $title_rowmax;
  248.  
  249.     /**
  250.     * First column to reapeat on each printed page
  251.     * @var integer 
  252.     */
  253.     var $title_colmin;
  254.  
  255.     /**
  256.     * First row of the area to print
  257.     * @var integer 
  258.     */
  259.     var $print_rowmin;
  260.  
  261.     /**
  262.     * Last row to of the area to print
  263.     * @var integer 
  264.     */
  265.     var $print_rowmax;
  266.  
  267.     /**
  268.     * First column of the area to print
  269.     * @var integer 
  270.     */
  271.     var $print_colmin;
  272.  
  273.     /**
  274.     * Last column of the area to print
  275.     * @var integer 
  276.     */
  277.     var $print_colmax;
  278.  
  279.     /**
  280.     * Whether to use outline.
  281.     * @var integer 
  282.     */
  283.     var $_outline_on;
  284.   
  285.     /**
  286.     * Auto outline styles.
  287.     * @var bool 
  288.     */
  289.     var $_outline_style;
  290.  
  291.     /**
  292.     * Whether to have outline summary below.
  293.     * @var bool 
  294.     */
  295.     var $_outline_below;
  296.  
  297.     /**
  298.     * Whether to have outline summary at the right.
  299.     * @var bool 
  300.     */
  301.     var $_outline_right;
  302.  
  303.     /**
  304.     * Outline row level.
  305.     * @var integer 
  306.     */
  307.     var $_outline_row_level;
  308.  
  309.     /**
  310.     * Whether to fit to page when printing or not.
  311.     * @var bool 
  312.     */
  313.     var $_fit_page;
  314.  
  315.     /** 
  316.     * Number of pages to fit wide
  317.     * @var integer 
  318.     */
  319.     var $_fit_width;
  320.  
  321.     /** 
  322.     * Number of pages to fit high
  323.     * @var integer 
  324.     */
  325.     var $_fit_height;
  326.  
  327.     /**
  328.     * Reference to the total number of strings in the workbook
  329.     * @var integer 
  330.     */
  331.     var $_str_total;
  332.  
  333.     /**
  334.     * Reference to the number of unique strings in the workbook
  335.     * @var integer 
  336.     */
  337.     var $_str_unique;
  338.  
  339.     /**
  340.     * Reference to the array containing all the unique strings in the workbook
  341.     * @var array 
  342.     */
  343.     var $_str_table;
  344.  
  345.     /**
  346.     * Merged cell ranges
  347.     * @var array 
  348.     */
  349.     var $_merged_ranges;
  350.  
  351.     /**
  352.     * Constructor
  353.     *
  354.     * @param string  $name         The name of the new worksheet
  355.     * @param integer $index        The index of the new worksheet
  356.     * @param mixed   &$activesheet The current activesheet of the workbook we belong to
  357.     * @param mixed   &$firstsheet  The first worksheet in the workbook we belong to
  358.     * @param mixed   &$url_format  The default format for hyperlinks
  359.     * @param mixed   &$parser      The formula parser created for the Workbook
  360.     * @access private
  361.     */
  362.     function Spreadsheet_Excel_Writer_Worksheet($BIFF_version$name,
  363.                                                 $index&$activesheet,
  364.                                                 &$firstsheet&$str_total,
  365.                                                 &$str_unique&$str_table,
  366.                                                 &$url_format&$parser)
  367.     {
  368.         // It needs to call its parent's constructor explicitly
  369.         $this->Spreadsheet_Excel_Writer_BIFFwriter();
  370.         $this->_BIFF_version   $BIFF_version;
  371.         $rowmax                = 65536; // 16384 in Excel 5
  372.         $colmax                = 256;
  373.     
  374.         $this->name            = $name;
  375.         $this->index           = $index;
  376.         $this->activesheet     &$activesheet;
  377.         $this->firstsheet      &$firstsheet;
  378.         $this->_str_total      &$str_total;
  379.         $this->_str_unique     &$str_unique;
  380.         $this->_str_table      &$str_table;
  381.         $this->_url_format     &$url_format;
  382.         $this->_parser         &$parser;
  383.     
  384.         //$this->ext_sheets      = array();
  385.         $this->_filehandle     "";
  386.         $this->_using_tmpfile  = true;
  387.         //$this->fileclosed      = 0;
  388.         //$this->offset          = 0;
  389.         $this->_xls_rowmax     $rowmax;
  390.         $this->_xls_colmax     $colmax;
  391.         $this->_xls_strmax     = 255;
  392.         $this->_dim_rowmin     $rowmax + 1;
  393.         $this->_dim_rowmax     = 0;
  394.         $this->_dim_colmin     $colmax + 1;
  395.         $this->_dim_colmax     = 0;
  396.         $this->_colinfo        = array();
  397.         $this->_selection      = array(0,0,0,0);
  398.         $this->_panes          = array();
  399.         $this->_active_pane    = 3;
  400.         $this->_frozen         = 0;
  401.         $this->selected        = 0;
  402.     
  403.         $this->_paper_size      = 0x0;
  404.         $this->_orientation     = 0x1;
  405.         $this->_header          '';
  406.         $this->_footer          '';
  407.         $this->_hcenter         = 0;
  408.         $this->_vcenter         = 0;
  409.         $this->_margin_head     = 0.50;
  410.         $this->_margin_foot     = 0.50;
  411.         $this->_margin_left     = 0.75;
  412.         $this->_margin_right    = 0.75;
  413.         $this->_margin_top      = 1.00;
  414.         $this->_margin_bottom   = 1.00;
  415.     
  416.         $this->title_rowmin     = NULL;
  417.         $this->title_rowmax     = NULL;
  418.         $this->title_colmin     = NULL;
  419.         $this->title_colmax     = NULL;
  420.         $this->print_rowmin     = NULL;
  421.         $this->print_rowmax     = NULL;
  422.         $this->print_colmin     = NULL;
  423.         $this->print_colmax     = NULL;
  424.     
  425.         $this->_print_gridlines  = 1;
  426.         $this->_screen_gridlines = 1;
  427.         $this->_print_headers    = 0;
  428.     
  429.         $this->_fit_page        = 0;
  430.         $this->_fit_width       = 0;
  431.         $this->_fit_height      = 0;
  432.     
  433.         $this->_hbreaks         = array();
  434.         $this->_vbreaks         = array();
  435.     
  436.         $this->_protect         = 0;
  437.         $this->_password        = NULL;
  438.     
  439.         $this->col_sizes        = array();
  440.         $this->_row_sizes        = array();
  441.     
  442.         $this->_zoom            = 100;
  443.         $this->_print_scale     = 100;
  444.  
  445.         $this->_outline_row_level = 0;
  446.         $this->_outline_style     = 0;
  447.         $this->_outline_below     = 1;
  448.         $this->_outline_right     = 1;
  449.         $this->_outline_on        = 1;
  450.  
  451.         $this->_merged_ranges     = array();
  452.  
  453.         $this->_dv                = array();
  454.  
  455.         $this->_initialize();
  456.     }
  457.     
  458.     /**
  459.     * Open a tmp file to store the majority of the Worksheet data. If this fails,
  460.     * for example due to write permissions, store the data in memory. This can be
  461.     * slow for large files.
  462.     *
  463.     * @access private
  464.     */
  465.     function _initialize()
  466.     {
  467.         // Open tmp file for storing Worksheet data
  468.         $fh tmpfile();
  469.         if $fh{
  470.             // Store filehandle
  471.             $this->_filehandle $fh;
  472.         }
  473.         else {
  474.             // If tmpfile() fails store data in memory
  475.             $this->_using_tmpfile = false;
  476.         }
  477.     }
  478.     
  479.     /**
  480.     * Add data to the beginning of the workbook (note the reverse order)
  481.     * and to the end of the workbook.
  482.     *
  483.     * @access public
  484.     * @see Spreadsheet_Excel_Writer_Workbook::storeWorkbook()
  485.     * @param array $sheetnames The array of sheetnames from the Workbook this
  486.     *                           worksheet belongs to
  487.     */
  488.     function close($sheetnames)
  489.     {
  490.         $num_sheets count($sheetnames);
  491.     
  492.         /***********************************************
  493.         * Prepend in reverse order!!
  494.         */
  495.     
  496.         // Prepend the sheet dimensions
  497.         $this->_storeDimensions();
  498.     
  499.         // Prepend the sheet password
  500.         $this->_storePassword();
  501.     
  502.         // Prepend the sheet protection
  503.         $this->_storeProtect();
  504.     
  505.         // Prepend the page setup
  506.         $this->_storeSetup();
  507.     
  508.         /* FIXME: margins are actually appended */
  509.         // Prepend the bottom margin
  510.         $this->_storeMarginBottom();
  511.     
  512.         // Prepend the top margin
  513.         $this->_storeMarginTop();
  514.     
  515.         // Prepend the right margin
  516.         $this->_storeMarginRight();
  517.     
  518.         // Prepend the left margin
  519.         $this->_storeMarginLeft();
  520.     
  521.         // Prepend the page vertical centering
  522.         $this->_storeVcenter();
  523.     
  524.         // Prepend the page horizontal centering
  525.         $this->_storeHcenter();
  526.     
  527.         // Prepend the page footer
  528.         $this->_storeFooter();
  529.     
  530.         // Prepend the page header
  531.         $this->_storeHeader();
  532.     
  533.         // Prepend the vertical page breaks
  534.         $this->_storeVbreak();
  535.     
  536.         // Prepend the horizontal page breaks
  537.         $this->_storeHbreak();
  538.     
  539.         // Prepend WSBOOL
  540.         $this->_storeWsbool();
  541.    
  542.         // Prepend GRIDSET
  543.         $this->_storeGridset();
  544.  
  545.          //  Prepend GUTS
  546.         if ($this->_BIFF_version == 0x0500{
  547.             $this->_storeGuts();
  548.         }
  549.  
  550.         // Prepend PRINTGRIDLINES
  551.         $this->_storePrintGridlines();
  552.     
  553.         // Prepend PRINTHEADERS
  554.         $this->_storePrintHeaders();
  555.     
  556.         // Prepend EXTERNSHEET references
  557.         if ($this->_BIFF_version == 0x0500{
  558.             for ($i $num_sheets$i > 0; $i--{
  559.                 $sheetname $sheetnames[$i-1];
  560.                 $this->_storeExternsheet($sheetname);
  561.             }
  562.         }
  563.     
  564.         // Prepend the EXTERNCOUNT of external references.
  565.         if ($this->_BIFF_version == 0x0500{
  566.             $this->_storeExterncount($num_sheets);
  567.         }
  568.     
  569.         // Prepend the COLINFO records if they exist
  570.         if (!empty($this->_colinfo))
  571.         {
  572.             for ($i=0; $i count($this->_colinfo)$i++{
  573.                 $this->_storeColinfo($this->_colinfo[$i]);
  574.             }
  575.             $this->_storeDefcol();
  576.         }
  577.     
  578.         // Prepend the BOF record
  579.         $this->_storeBof(0x0010);
  580.     
  581.         /*
  582.         * End of prepend. Read upwards from here.
  583.         ***********************************************/
  584.     
  585.         // Append
  586.         $this->_storeWindow2();
  587.         $this->_storeZoom();
  588.         if (!empty($this->_panes)) {
  589.             $this->_storePanes($this->_panes);
  590.         }
  591.         $this->_storeSelection($this->_selection);
  592.         $this->_storeMergedCells();
  593.         /* TODO: add data validity */
  594.         /*if ($this->_BIFF_version == 0x0600) {
  595.             $this->_storeDataValidity();
  596.         }*/
  597.         $this->_storeEof();
  598.     }
  599.     
  600.     /**
  601.     * Retrieve the worksheet name.
  602.     * This is usefull when creating worksheets without a name.
  603.     *
  604.     * @access public
  605.     * @return string The worksheet's name
  606.     */
  607.     function getName()
  608.     {
  609.         return $this->name;
  610.     }
  611.     
  612.     /**
  613.     * Retrieves data from memory in one chunk, or from disk in $buffer
  614.     * sized chunks.
  615.     *
  616.     * @return string The data
  617.     */
  618.     function getData()
  619.     {
  620.         $buffer = 4096;
  621.     
  622.         // Return data stored in memory
  623.         if (isset($this->_data))
  624.         {
  625.             $tmp   $this->_data;
  626.             unset($this->_data);
  627.             $fh    $this->_filehandle;
  628.             if ($this->_using_tmpfile{
  629.                 fseek($fh0);
  630.             }
  631.             return $tmp;
  632.         }
  633.         // Return data stored on disk
  634.         if ($this->_using_tmpfile)
  635.         {
  636.             if ($tmp fread($this->_filehandle$buffer)) {
  637.                 return $tmp;
  638.             }
  639.         }
  640.     
  641.         // No data to return
  642.         return '';
  643.     }
  644.  
  645.     /**
  646.     * Sets a merged cell range
  647.     *
  648.     * @access public
  649.     * @param integer $first_row First row of the area to merge
  650.     * @param integer $first_col First column of the area to merge
  651.     * @param integer $last_row  Last row of the area to merge
  652.     * @param integer $last_col  Last column of the area to merge
  653.     */
  654.     function setMerge($first_row$first_col$last_row$last_col)
  655.     {
  656.         if (($last_row $first_rowor ($last_col $first_col)) {
  657.             return;
  658.         }
  659.         // don't check rowmin, rowmax, etc... because we don't know when this
  660.         // is going to be called
  661.         $this->_merged_ranges[= array($first_row$first_col$last_row$last_col);
  662.     }
  663.  
  664.     /**
  665.     * Set this worksheet as a selected worksheet,
  666.     * i.e. the worksheet has its tab highlighted.
  667.     *
  668.     * @access public
  669.     */
  670.     function select()
  671.     {
  672.         $this->selected = 1;
  673.     }
  674.     
  675.     /**
  676.     * Set this worksheet as the active worksheet,
  677.     * i.e. the worksheet that is displayed when the workbook is opened.
  678.     * Also set it as selected.
  679.     *
  680.     * @access public
  681.     */
  682.     function activate()
  683.     {
  684.         $this->selected = 1;
  685.         $this->activesheet $this->index;
  686.     }
  687.     
  688.     /**
  689.     * Set this worksheet as the first visible sheet.
  690.     * This is necessary when there are a large number of worksheets and the
  691.     * activated worksheet is not visible on the screen.
  692.     *
  693.     * @access public
  694.     */
  695.     function setFirstSheet()
  696.     {
  697.         $this->firstsheet $this->index;
  698.     }
  699.  
  700.     /**
  701.     * Set the worksheet protection flag
  702.     * to prevent accidental modification and to
  703.     * hide formulas if the locked and hidden format properties have been set.
  704.     *
  705.     * @access public
  706.     * @param string $password The password to use for protecting the sheet.
  707.     */
  708.     function protect($password)
  709.     {
  710.         $this->_protect   = 1;
  711.         $this->_password  $this->_encodePassword($password);
  712.     }
  713.  
  714.     /**
  715.     * Set the width of a single column or a range of columns.
  716.     *
  717.     * @access public
  718.     * @param integer $firstcol first column on the range
  719.     * @param integer $lastcol  last column on the range
  720.     * @param integer $width    width to set
  721.     * @param mixed   $format   The optional XF format to apply to the columns
  722.     * @param integer $hidden   The optional hidden atribute
  723.     * @param integer $level    The optional outline level
  724.     */
  725.     function setColumn($firstcol$lastcol$width$format = 0$hidden = 0$level = 0)
  726.     {
  727.         $this->_colinfo[= array($firstcol$lastcol$width&$format$hidden$level);
  728.     
  729.         // Set width to zero if column is hidden
  730.         $width ($hidden? 0 : $width;
  731.     
  732.         for ($col $firstcol$col <= $lastcol$col++{
  733.             $this->col_sizes[$col$width;
  734.         }
  735.     }
  736.     
  737.     /**
  738.     * Set which cell or cells are selected in a worksheet
  739.     *
  740.     * @access public
  741.     * @param integer $first_row    first row in the selected quadrant
  742.     * @param integer $first_column first column in the selected quadrant
  743.     * @param integer $last_row     last row in the selected quadrant
  744.     * @param integer $last_column  last column in the selected quadrant
  745.     */
  746.     function setSelection($first_row,$first_column,$last_row,$last_column)
  747.     {
  748.         $this->_selection = array($first_row,$first_column,$last_row,$last_column);
  749.     }
  750.     
  751.     /**
  752.     * Set panes and mark them as frozen.
  753.     *
  754.     * @access public
  755.     * @param array $panes This is the only parameter received and is composed of the following:
  756.     *                      0 => Vertical split position,
  757.     *                      1 => Horizontal split position
  758.     *                      2 => Top row visible
  759.     *                      3 => Leftmost column visible
  760.     *                      4 => Active pane
  761.     */
  762.     function freezePanes($panes)
  763.     {
  764.         $this->_frozen = 1;
  765.         $this->_panes  $panes;
  766.     }
  767.     
  768.     /**
  769.     * Set panes and mark them as unfrozen.
  770.     *
  771.     * @access public
  772.     * @param array $panes This is the only parameter received and is composed of the following:
  773.     *                      0 => Vertical split position,
  774.     *                      1 => Horizontal split position
  775.     *                      2 => Top row visible
  776.     *                      3 => Leftmost column visible
  777.     *                      4 => Active pane
  778.     */
  779.     function thawPanes($panes)
  780.     {
  781.         $this->_frozen = 0;
  782.         $this->_panes  $panes;
  783.     }
  784.     
  785.     /**
  786.     * Set the page orientation as portrait.
  787.     *
  788.     * @access public
  789.     */
  790.     function setPortrait()
  791.     {
  792.         $this->_orientation = 1;
  793.     }
  794.     
  795.     /**
  796.     * Set the page orientation as landscape.
  797.     *
  798.     * @access public
  799.     */
  800.     function setLandscape()
  801.     {
  802.         $this->_orientation = 0;
  803.     }
  804.     
  805.     /**
  806.     * Set the paper type. Ex. 1 = US Letter, 9 = A4
  807.     *
  808.     * @access public
  809.     * @param integer $size The type of paper size to use
  810.     */
  811.     function setPaper($size = 0)
  812.     {
  813.         $this->_paper_size $size;
  814.     }
  815.     
  816.     
  817.     /**
  818.     * Set the page header caption and optional margin.
  819.     *
  820.     * @access public
  821.     * @param string $string The header text
  822.     * @param float  $margin optional head margin in inches.
  823.     */
  824.     function setHeader($string,$margin = 0.50)
  825.     {
  826.         if (strlen($string>= 255{
  827.             //carp 'Header string must be less than 255 characters';
  828.             return;
  829.         }
  830.         $this->_header      $string;
  831.         $this->_margin_head $margin;
  832.     }
  833.     
  834.     /**
  835.     * Set the page footer caption and optional margin.
  836.     *
  837.     * @access public
  838.     * @param string $string The footer text
  839.     * @param float  $margin optional foot margin in inches.
  840.     */
  841.     function setFooter($string,$margin = 0.50)
  842.     {
  843.         if (strlen($string>= 255{
  844.             //carp 'Footer string must be less than 255 characters';
  845.             return;
  846.         }
  847.         $this->_footer      $string;
  848.         $this->_margin_foot $margin;
  849.     }
  850.     
  851.     /**
  852.     * Center the page horinzontally.
  853.     *
  854.     * @access public
  855.     * @param integer $center the optional value for centering. Defaults to 1 (center).
  856.     */
  857.     function centerHorizontally($center = 1)
  858.     {
  859.         $this->_hcenter $center;
  860.     }
  861.     
  862.     /**
  863.     * Center the page vertically.
  864.     *
  865.     * @access public
  866.     * @param integer $center the optional value for centering. Defaults to 1 (center).
  867.     */
  868.     function centerVertically($center = 1)
  869.     {
  870.         $this->_vcenter $center;
  871.     }
  872.     
  873.     /**
  874.     * Set all the page margins to the same value in inches.
  875.     *
  876.     * @access public
  877.     * @param float $margin The margin to set in inches
  878.     */
  879.     function setMargins($margin)
  880.     {
  881.         $this->setMarginLeft($margin);
  882.         $this->setMarginRight($margin);
  883.         $this->setMarginTop($margin);
  884.         $this->setMarginBottom($margin);
  885.     }
  886.     
  887.     /**
  888.     * Set the left and right margins to the same value in inches.
  889.     *
  890.     * @access public
  891.     * @param float $margin The margin to set in inches
  892.     */
  893.     function setMargins_LR($margin)
  894.     {
  895.         $this->setMarginLeft($margin);
  896.         $this->setMarginRight($margin);
  897.     }
  898.     
  899.     /**
  900.     * Set the top and bottom margins to the same value in inches.
  901.     *
  902.     * @access public
  903.     * @param float $margin The margin to set in inches
  904.     */
  905.     function setMargins_TB($margin)
  906.     {
  907.         $this->setMarginTop($margin);
  908.         $this->setMarginBottom($margin);
  909.     }
  910.     
  911.     /**
  912.     * Set the left margin in inches.
  913.     *
  914.     * @access public
  915.     * @param float $margin The margin to set in inches
  916.     */
  917.     function setMarginLeft($margin = 0.75)
  918.     {
  919.         $this->_margin_left $margin;
  920.     }
  921.     
  922.     /**
  923.     * Set the right margin in inches.
  924.     *
  925.     * @access public
  926.     * @param float $margin The margin to set in inches
  927.     */
  928.     function setMarginRight($margin = 0.75)
  929.     {
  930.         $this->_margin_right $margin;
  931.     }
  932.     
  933.     /**
  934.     * Set the top margin in inches.
  935.     *
  936.     * @access public
  937.     * @param float $margin The margin to set in inches
  938.     */
  939.     function setMarginTop($margin = 1.00)
  940.     {
  941.         $this->_margin_top $margin;
  942.     }
  943.     
  944.     /**
  945.     * Set the bottom margin in inches.
  946.     *
  947.     * @access public
  948.     * @param float $margin The margin to set in inches
  949.     */
  950.     function setMarginBottom($margin = 1.00)
  951.     {
  952.         $this->_margin_bottom $margin;
  953.     }
  954.     
  955.     /**
  956.     * Set the rows to repeat at the top of each printed page.
  957.     *
  958.     * @access public
  959.     * @param integer $first_row First row to repeat
  960.     * @param integer $last_row  Last row to repeat. Optional.
  961.     */
  962.     function repeatRows($first_row$last_row = NULL)
  963.     {
  964.         $this->title_rowmin  = $first_row;
  965.         if (isset($last_row)) //Second row is optional
  966.             $this->title_rowmax  = $last_row;
  967.         }
  968.         else {
  969.             $this->title_rowmax  = $first_row;
  970.         }
  971.     }
  972.     
  973.     /**
  974.     * Set the columns to repeat at the left hand side of each printed page.
  975.     *
  976.     * @access public
  977.     * @param integer $first_col First column to repeat
  978.     * @param integer $last_col  Last column to repeat. Optional.
  979.     */
  980.     function repeatColumns($first_col$last_col = NULL)
  981.     {
  982.         $this->title_colmin  = $first_col;
  983.         if (isset($last_col)) // Second col is optional
  984.             $this->title_colmax  $last_col;
  985.         }
  986.         else {
  987.             $this->title_colmax  $first_col;
  988.         }
  989.     }
  990.     
  991.     /**
  992.     * Set the area of each worksheet that will be printed.
  993.     *
  994.     * @access public
  995.     * @param integer $first_row First row of the area to print
  996.     * @param integer $first_col First column of the area to print
  997.     * @param integer $last_row  Last row of the area to print
  998.     * @param integer $last_col  Last column of the area to print
  999.     */
  1000.     function printArea($first_row$first_col$last_row$last_col)
  1001.     {
  1002.         $this->print_rowmin  = $first_row;
  1003.         $this->print_colmin  = $first_col;
  1004.         $this->print_rowmax  = $last_row;
  1005.         $this->print_colmax  = $last_col;
  1006.     }
  1007.     
  1008.     
  1009.     /**
  1010.     * Set the option to hide gridlines on the printed page.
  1011.     *
  1012.     * @access public
  1013.     */
  1014.     function hideGridlines()
  1015.     {
  1016.         $this->_print_gridlines = 0;
  1017.     }
  1018.     
  1019.     /**
  1020.     * Set the option to hide gridlines on the worksheet (as seen on the screen).
  1021.     *
  1022.     * @access public
  1023.     */
  1024.     function hideScreenGridlines()
  1025.     {
  1026.         $this->_screen_gridlines = 0;
  1027.     }
  1028.  
  1029.     /**
  1030.     * Set the option to print the row and column headers on the printed page.
  1031.     *
  1032.     * @access public
  1033.     * @param integer $print Whether to print the headers or not. Defaults to 1 (print).
  1034.     */
  1035.     function printRowColHeaders($print = 1)
  1036.     {
  1037.         $this->_print_headers $print;
  1038.     }
  1039.     
  1040.     /**
  1041.     * Set the vertical and horizontal number of pages that will define the maximum area printed.
  1042.     * It doesn't seem to work with OpenOffice.
  1043.     *
  1044.     * @access public
  1045.     * @param  integer $width  Maximun width of printed area in pages
  1046.     * @param  integer $height Maximun heigth of printed area in pages
  1047.     * @see setPrintScale()
  1048.     */
  1049.     function fitToPages($width$height)
  1050.     {
  1051.         $this->_fit_page      = 1;
  1052.         $this->_fit_width     $width;
  1053.         $this->_fit_height    $height;
  1054.     }
  1055.     
  1056.     /**
  1057.     * Store the horizontal page breaks on a worksheet (for printing).
  1058.     * The breaks represent the row after which the break is inserted.
  1059.     *
  1060.     * @access public
  1061.     * @param array $breaks Array containing the horizontal page breaks
  1062.     */
  1063.     function setHPagebreaks($breaks)
  1064.     {
  1065.         foreach($breaks as $break{
  1066.             array_push($this->_hbreaks,$break);
  1067.         }
  1068.     }
  1069.     
  1070.     /**
  1071.     * Store the vertical page breaks on a worksheet (for printing).
  1072.     * The breaks represent the column after which the break is inserted.
  1073.     *
  1074.     * @access public
  1075.     * @param array $breaks Array containing the vertical page breaks
  1076.     */
  1077.     function setVPagebreaks($breaks)
  1078.     {
  1079.         foreach($breaks as $break{
  1080.             array_push($this->_vbreaks,$break);
  1081.         }
  1082.     }
  1083.     
  1084.     
  1085.     /**
  1086.     * Set the worksheet zoom factor.
  1087.     *
  1088.     * @access public
  1089.     * @param integer $scale The zoom factor
  1090.     */
  1091.     function setZoom($scale = 100)
  1092.     {
  1093.         // Confine the scale to Excel's range
  1094.         if ($scale < 10 or $scale > 400
  1095.         {
  1096.             $this->raiseError("Zoom factor $scale outside range: 10 <= zoom <= 400");
  1097.             $scale = 100;
  1098.         }
  1099.     
  1100.         $this->_zoom floor($scale);
  1101.     }
  1102.     
  1103.     /**
  1104.     * Set the scale factor for the printed page.
  1105.     * It turns off the "fit to page" option
  1106.     *
  1107.     * @access public
  1108.     * @param integer $scale The optional scale factor. Defaults to 100
  1109.     */
  1110.     function setPrintScale($scale = 100)
  1111.     {
  1112.         // Confine the scale to Excel's range
  1113.         if ($scale < 10 or $scale > 400)
  1114.         {
  1115.             $this->raiseError("Print scale $scale outside range: 10 <= zoom <= 400");
  1116.             $scale = 100;
  1117.         }
  1118.     
  1119.         // Turn off "fit to page" option
  1120.         $this->_fit_page    = 0;
  1121.     
  1122.         $this->_print_scale floor($scale);
  1123.     }
  1124.     
  1125.     /**
  1126.     * Map to the appropriate write method acording to the token recieved.
  1127.     *
  1128.     * @access public
  1129.     * @param integer $row    The row of the cell we are writing to
  1130.     * @param integer $col    The column of the cell we are writing to
  1131.     * @param mixed   $token  What we are writing
  1132.     * @param mixed   $format The optional format to apply to the cell
  1133.     */
  1134.     function write($row$col$token$format = 0)
  1135.     {
  1136.         // Check for a cell reference in A1 notation and substitute row and column
  1137.         /*if ($_[0] =~ /^\D/) {
  1138.             @_ = $this->_substituteCellref(@_);
  1139.     }*/
  1140.     
  1141.     
  1142.         // Match number
  1143.         if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/",$token)) {
  1144.             return $this->writeNumber($row,$col,$token,$format);
  1145.         }
  1146.         // Match http or ftp URL
  1147.         elseif (preg_match("/^[fh]tt?p:\/\//",$token)) {
  1148.             return $this->writeUrl($row$col$token''$format);
  1149.         }
  1150.         // Match mailto:
  1151.         elseif (preg_match("/^mailto:/",$token)) {
  1152.             return $this->writeUrl($row$col$token''$format);
  1153.         }
  1154.         // Match internal or external sheet link
  1155.         elseif (preg_match("/^(?:in|ex)ternal:/",$token)) {
  1156.             return $this->writeUrl($row$col$token''$format);
  1157.         }
  1158.         // Match formula
  1159.         elseif (preg_match("/^=/",$token)) {
  1160.             return $this->writeFormula($row$col$token$format);
  1161.         }
  1162.         // Match formula
  1163.         elseif (preg_match("/^@/",$token)) {
  1164.             return $this->writeFormula($row$col$token$format);
  1165.         }
  1166.         // Match blank
  1167.         elseif ($token == ''{
  1168.             return $this->writeBlank($row,$col,$format);
  1169.         }
  1170.         // Default: match string
  1171.         else {
  1172.             return $this->writeString($row,$col,$token,$format);
  1173.         }
  1174.     }
  1175.     
  1176.     /**
  1177.     * Write an array of values as a row
  1178.     *
  1179.     * @access public
  1180.     * @param integer $row    The row we are writing to
  1181.     * @param integer $col    The first col (leftmost col) we are writing to
  1182.     * @param array   $val    The array of values to write
  1183.     * @param mixed   $format The optional format to apply to the cell
  1184.     * @return mixed PEAR_Error on failure
  1185.     */
  1186.  
  1187.     function writeRow($row$col$val$format=0)
  1188.     {   
  1189.         $retval '';
  1190.         if (is_array($val)) {
  1191.             foreach($val as $v{
  1192.                 if (is_array($v)) {
  1193.                     $this->writeCol($row$col$v$format);
  1194.                 else {
  1195.                     $this->write($row$col$v$format);
  1196.                 }
  1197.                 $col++;
  1198.             }
  1199.         else {
  1200.             $retval = new PEAR_Error('$val needs to be an array');
  1201.         }
  1202.         return($retval);
  1203.     }
  1204.       
  1205.     /**
  1206.     * Write an array of values as a column
  1207.     *
  1208.     * @access public
  1209.     * @param integer $row    The first row (uppermost row) we are writing to
  1210.     * @param integer $col    The col we are writing to
  1211.     * @param array   $val    The array of values to write
  1212.     * @param mixed   $format The optional format to apply to the cell
  1213.     * @return mixed PEAR_Error on failure
  1214.     */
  1215.     
  1216.     function writeCol($row$col$val$format=0)
  1217.     {
  1218.         $retval '';
  1219.         if (is_array($val)) {
  1220.             foreach($val as $v
  1221.                 $this->write($row$col$v$format);
  1222.                 $row++;
  1223.             }
  1224.         else {
  1225.             $retval = new PEAR_Error('$val needs to be an array');
  1226.         }
  1227.         return($retval);
  1228.     }
  1229.     
  1230.     /**
  1231.     * Returns an index to the XF record in the workbook
  1232.     *
  1233.     * @access private
  1234.     * @param mixed &$format The optional XF format
  1235.     * @return integer The XF record index
  1236.     */
  1237.     function _XF(&$format)
  1238.     {
  1239.         if ($format != 0{
  1240.             return($format->getXfIndex());
  1241.         }
  1242.         else {
  1243.             return(0x0F);
  1244.         }
  1245.     }
  1246.     
  1247.     
  1248.     /******************************************************************************
  1249.     *******************************************************************************
  1250.     *
  1251.     * Internal methods
  1252.     */
  1253.     
  1254.     
  1255.     /**
  1256.     * Store Worksheet data in memory using the parent's class append() or to a
  1257.     * temporary file, the default.
  1258.     *
  1259.     * @access private
  1260.     * @param string $data The binary data to append
  1261.     */
  1262.     function _append($data)
  1263.     {
  1264.         if ($this->_using_tmpfile)
  1265.         {
  1266.             // Add CONTINUE records if necessary
  1267.             if (strlen($data$this->_limit{
  1268.                 $data $this->_addContinue($data);
  1269.             }
  1270.             fwrite($this->_filehandle,$data);
  1271.             $this->_datasize += strlen($data);
  1272.         }
  1273.         else {
  1274.             parent::_append($data);
  1275.         }
  1276.     }
  1277.     
  1278.     /**
  1279.     * Substitute an Excel cell reference in A1 notation for  zero based row and
  1280.     * column values in an argument list.
  1281.     *
  1282.     * Ex: ("A4", "Hello") is converted to (3, 0, "Hello").
  1283.     *
  1284.     * @access private
  1285.     * @param string $cell The cell reference. Or range of cells.
  1286.     * @return array 
  1287.     */
  1288.     function _substituteCellref($cell)
  1289.     {
  1290.         $cell strtoupper($cell);
  1291.     
  1292.         // Convert a column range: 'A:A' or 'B:G'
  1293.         if (preg_match("/([A-I]?[A-Z]):([A-I]?[A-Z])/",$cell,$match)) {
  1294.             list($no_use$col1=  $this->_cellToRowcol($match[1.'1')// Add a dummy row
  1295.             list($no_use$col2=  $this->_cellToRowcol($match[2.'1')// Add a dummy row
  1296.             return(array($col1$col2));
  1297.         }
  1298.     
  1299.         // Convert a cell range: 'A1:B7'
  1300.         if (preg_match("/\$?([A-I]?[A-Z]\$?\d+):\$?([A-I]?[A-Z]\$?\d+)/",$cell,$match)) {
  1301.             list($row1$col1=  $this->_cellToRowcol($match[1]);
  1302.             list($row2$col2=  $this->_cellToRowcol($match[2]);
  1303.             return(array($row1$col1$row2$col2));
  1304.         }
  1305.     
  1306.         // Convert a cell reference: 'A1' or 'AD2000'
  1307.         if (preg_match("/\$?([A-I]?[A-Z]\$?\d+)/",$cell)) {
  1308.             list($row1$col1=  $this->_cellToRowcol($match[1]);
  1309.             return(array($row1$col1));
  1310.         }
  1311.     
  1312.         // TODO use real error codes
  1313.         $this->raiseError("Unknown cell reference $cell"0PEAR_ERROR_DIE);
  1314.     }
  1315.     
  1316.     /**
  1317.     * Convert an Excel cell reference in A1 notation to a zero based row and column
  1318.     * reference; converts C1 to (0, 2).
  1319.     *
  1320.     * @access private
  1321.     * @param string $cell The cell reference.
  1322.     * @return array containing (row, column)
  1323.     */
  1324.     function _cellToRowcol($cell)
  1325.     {
  1326.         preg_match("/\$?([A-I]?[A-Z])\$?(\d+)/",$cell,$match);
  1327.         $col     $match[1];
  1328.         $row     $match[2];
  1329.     
  1330.         // Convert base26 column string to number
  1331.         $chars split(''$col);
  1332.         $expn  = 0;
  1333.         $col   = 0;
  1334.     
  1335.         while ($chars{
  1336.             $char array_pop($chars);        // LS char first
  1337.             $col += (ord($char-ord('A'+1pow(26,$expn);
  1338.             $expn++;
  1339.         }
  1340.     
  1341.         // Convert 1-index to zero-index
  1342.         $row--;
  1343.         $col--;
  1344.     
  1345.         return(array($row$col));
  1346.     }
  1347.  
  1348.     /**
  1349.     * Based on the algorithm provided by Daniel Rentz of OpenOffice.
  1350.     *
  1351.     * @access private
  1352.     * @param string $plaintext The password to be encoded in plaintext.
  1353.     * @return string The encoded password
  1354.     */
  1355.     function _encodePassword($plaintext)
  1356.     {
  1357.         $password = 0x0000;
  1358.         $i        = 1;       // char position
  1359.  
  1360.         // split the plain text password in its component characters
  1361.         $chars preg_split('//'$plaintext-1PREG_SPLIT_NO_EMPTY);
  1362.         foreach($chars as $char)
  1363.         {
  1364.             $value        ord($char<< $i;   // shifted ASCII value 
  1365.             $rotated_bits $value >> 15;       // rotated bits beyond bit 15
  1366.             $value       &= 0x7fff;             // first 15 bits
  1367.             $password    ^= ($value $rotated_bits);
  1368.             $i++;
  1369.         }
  1370.     
  1371.         $password ^= strlen($plaintext);
  1372.         $password ^= 0xCE4B;
  1373.  
  1374.         return($password);
  1375.     }
  1376.  
  1377.     /**
  1378.     * This method sets the properties for outlining and grouping. The defaults
  1379.     * correspond to Excel's defaults.
  1380.     *
  1381.     * @param bool $visible 
  1382.     * @param bool $symbols_below 
  1383.     * @param bool $symbols_right 
  1384.     * @param bool $auto_style 
  1385.     */
  1386.     function setOutline($visible = true$symbols_below = true$symbols_right = true$auto_style = false)
  1387.     {
  1388.         $this->_outline_on    $visible;
  1389.         $this->_outline_below $symbols_below;
  1390.         $this->_outline_right $symbols_right;
  1391.         $this->_outline_style $auto_style;
  1392.  
  1393.         // Ensure this is a boolean vale for Window2
  1394.         if ($this->_outline_on{
  1395.             $this->_outline_on = 1;
  1396.         }
  1397.      }
  1398.  
  1399.     /******************************************************************************
  1400.     *******************************************************************************
  1401.     *
  1402.     * BIFF RECORDS
  1403.     */
  1404.     
  1405.     
  1406.     /**
  1407.     * Write a double to the specified row and column (zero indexed).
  1408.     * An integer can be written as a double. Excel will display an
  1409.     * integer. $format is optional.
  1410.     *
  1411.     * Returns  0 : normal termination
  1412.     *         -2 : row or column out of range
  1413.     *
  1414.     * @access public
  1415.     * @param integer $row    Zero indexed row
  1416.     * @param integer $col    Zero indexed column
  1417.     * @param float   $num    The number to write
  1418.     * @param mixed   $format The optional XF format
  1419.     * @return integer 
  1420.     */
  1421.     function writeNumber($row$col$num$format = 0)
  1422.     {
  1423.         $record    = 0x0203;                 // Record identifier
  1424.         $length    = 0x000E;                 // Number of bytes to follow
  1425.     
  1426.         $xf        $this->_XF($format);    // The cell format
  1427.     
  1428.         // Check that row and col are valid and store max and min values
  1429.         if ($row >= $this->_xls_rowmax)
  1430.         {
  1431.             return(-2);
  1432.         }
  1433.         if ($col >= $this->_xls_colmax)
  1434.         {
  1435.             return(-2);
  1436.         }
  1437.         if ($row <  $this->_dim_rowmin
  1438.         {
  1439.             $this->_dim_rowmin $row;
  1440.         }
  1441.         if ($row >  $this->_dim_rowmax
  1442.         {
  1443.             $this->_dim_rowmax $row;
  1444.         }
  1445.         if ($col <  $this->_dim_colmin
  1446.         {
  1447.             $this->_dim_colmin $col;
  1448.         }
  1449.         if ($col >  $this->_dim_colmax
  1450.         {
  1451.             $this->_dim_colmax $col;
  1452.         }
  1453.     
  1454.         $header    pack("vv",  $record$length);
  1455.         $data      pack("vvv"$row$col$xf);
  1456.         $xl_double pack("d",   $num);
  1457.         if ($this->_byte_order// if it's Big Endian
  1458.         {
  1459.             $xl_double strrev($xl_double);
  1460.         }
  1461.     
  1462.         $this->_append($header.$data.$xl_double);
  1463.         return(0);
  1464.     }
  1465.     
  1466.     /**
  1467.     * Write a string to the specified row and column (zero indexed).
  1468.     * NOTE: there is an Excel 5 defined limit of 255 characters.
  1469.     * $format is optional.
  1470.     * Returns  0 : normal termination
  1471.     *         -2 : row or column out of range
  1472.     *         -3 : long string truncated to 255 chars
  1473.     *
  1474.     * @access public
  1475.     * @param integer $row    Zero indexed row
  1476.     * @param integer $col    Zero indexed column
  1477.     * @param string  $str    The string to write
  1478.     * @param mixed   $format The XF format for the cell
  1479.     * @return integer 
  1480.     */
  1481.     function writeString($row$col$str$format = 0)
  1482.     {
  1483.         if ($this->_BIFF_version == 0x0600{
  1484.             return $this->writeStringBIFF8($row$col$str$format);
  1485.         }
  1486.         $strlen    strlen($str);
  1487.         $record    = 0x0204;                   // Record identifier
  1488.         $length    = 0x0008 + $strlen;         // Bytes to follow
  1489.         $xf        $this->_XF($format);      // The cell format
  1490.         
  1491.         $str_error = 0;
  1492.     
  1493.         // Check that row and col are valid and store max and min values
  1494.         if ($row >= $this->_xls_rowmax
  1495.         {
  1496.             return(-2);
  1497.         }
  1498.         if ($col >= $this->_xls_colmax
  1499.         {
  1500.             return(-2);
  1501.         }
  1502.         if ($row <  $this->_dim_rowmin
  1503.         {
  1504.             $this->_dim_rowmin $row;
  1505.         }
  1506.         if ($row >  $this->_dim_rowmax
  1507.         {
  1508.             $this->_dim_rowmax $row;
  1509.         }
  1510.         if ($col <  $this->_dim_colmin
  1511.         {
  1512.             $this->_dim_colmin $col;
  1513.         }
  1514.         if ($col >  $this->_dim_colmax
  1515.         {
  1516.             $this->_dim_colmax $col;
  1517.         }
  1518.     
  1519.         if ($strlen $this->_xls_strmax)  // LABEL must be < 255 chars
  1520.         {
  1521.             $str       substr($str0$this->_xls_strmax);
  1522.             $length    = 0x0008 + $this->_xls_strmax;
  1523.             $strlen    $this->_xls_strmax;
  1524.             $str_error = -3;
  1525.         }
  1526.     
  1527.         $header    pack("vv",   $record$length);
  1528.         $data      pack("vvvv"$row$col$xf$strlen);
  1529.         $this->_append($header.$data.$str);
  1530.         return($str_error);
  1531.     }
  1532.  
  1533.     function writeStringBIFF8($row$col$str$format = 0)
  1534.     {
  1535.         $strlen    strlen($str);
  1536.         $record    = 0x00FD;                   // Record identifier
  1537.         $length    = 0x000A;                   // Bytes to follow
  1538.         $xf        $this->_XF($format);      // The cell format
  1539.         $encoding  = 0x0;
  1540.         
  1541.         $str_error = 0;
  1542.     
  1543.         // Check that row and col are valid and store max and min values
  1544.         if ($this->_checkRowCol($row$col== false{
  1545.             return -2;
  1546.         }
  1547.  
  1548.         $str pack('vC'$strlen$encoding).$str;
  1549.  
  1550.         /* check if string is already present */
  1551.         if (!isset($this->_str_table[$str])) {
  1552.             $this->_str_table[$str$this->_str_unique++;
  1553.         }
  1554.         $this->_str_total++;
  1555.     
  1556.         $header    pack('vv',   $record$length);
  1557.         $data      pack('vvvV'$row$col$xf$this->_str_table[$str]);
  1558.         $this->_append($header.$data);
  1559.         return $str_error;
  1560.     }
  1561.  
  1562.     /**
  1563.     * Check row and col before writing to a cell, and update the sheet's
  1564.     * dimensions accordingly
  1565.     *
  1566.     * @access private
  1567.     * @param integer $row    Zero indexed row
  1568.     * @param integer $col    Zero indexed column
  1569.     * @return boolean true for success, false if row and/or col are grester
  1570.     *                  then maximums allowed.
  1571.     */
  1572.     function _checkRowCol($row$col)
  1573.     {
  1574.         if ($row >= $this->_xls_rowmax{
  1575.             return false;
  1576.         }
  1577.         if ($col >= $this->_xls_colmax{
  1578.             return false;
  1579.         }
  1580.         if ($row <  $this->_dim_rowmin{
  1581.             $this->_dim_rowmin $row;
  1582.         }
  1583.         if ($row >  $this->_dim_rowmax{
  1584.             $this->_dim_rowmax $row;
  1585.         }
  1586.         if ($col <  $this->_dim_colmin{
  1587.             $this->_dim_colmin $col;
  1588.         }
  1589.         if ($col >  $this->_dim_colmax{
  1590.             $this->_dim_colmax $col;
  1591.         }
  1592.         return true;
  1593.     }
  1594.  
  1595.     /**
  1596.     * Writes a note associated with the cell given by the row and column.
  1597.     * NOTE records don't have a length limit.
  1598.     *
  1599.     * @access public
  1600.     * @param integer $row    Zero indexed row
  1601.     * @param integer $col    Zero indexed column
  1602.     * @param string  $note   The note to write
  1603.     */
  1604.     function writeNote($row$col$note)
  1605.     {
  1606.         $note_length    strlen($note);
  1607.         $record         = 0x001C;                // Record identifier
  1608.         $max_length     = 2048;                  // Maximun length for a NOTE record
  1609.         //$length      = 0x0006 + $note_length;    // Bytes to follow
  1610.  
  1611.         // Check that row and col are valid and store max and min values
  1612.         if ($row >= $this->_xls_rowmax
  1613.         {
  1614.             return(-2);
  1615.         }
  1616.         if ($col >= $this->_xls_colmax
  1617.         {
  1618.             return(-2);
  1619.         }
  1620.         if ($row <  $this->_dim_rowmin
  1621.         {
  1622.             $this->_dim_rowmin $row;
  1623.         }
  1624.         if ($row >  $this->_dim_rowmax
  1625.         {
  1626.             $this->_dim_rowmax $row;
  1627.         }
  1628.         if ($col <  $this->_dim_colmin
  1629.         {
  1630.             $this->_dim_colmin $col;
  1631.         }
  1632.         if ($col >  $this->_dim_colmax
  1633.         {
  1634.             $this->_dim_colmax $col;
  1635.         }
  1636.  
  1637.         // Length for this record is no more than 2048 + 6
  1638.         $length    = 0x0006 + min($note_length2048);
  1639.         $header    pack("vv",   $record$length);
  1640.         $data      pack("vvv"$row$col$note_length);
  1641.         $this->_append($header.$data.substr($note02048));
  1642.  
  1643.         for($i $max_length$i $note_length$i += $max_length)
  1644.         {
  1645.             $chunk  substr($note$i$max_length);
  1646.             $length = 0x0006 + strlen($chunk);
  1647.             $header pack("vv",   $record$length);
  1648.             $data   pack("vvv"-10strlen($chunk));
  1649.             $this->_append($header.$data.$chunk);
  1650.         }
  1651.         return(0);
  1652.     }
  1653.  
  1654.     /**
  1655.     * Write a blank cell to the specified row and column (zero indexed).
  1656.     * A blank cell is used to specify formatting without adding a string
  1657.     * or a number.
  1658.     *
  1659.     * A blank cell without a format serves no purpose. Therefore, we don't write
  1660.     * a BLANK record unless a format is specified.
  1661.     *
  1662.     * Returns  0 : normal termination (including no format)
  1663.     *         -1 : insufficient number of arguments
  1664.     *         -2 : row or column out of range
  1665.     *
  1666.     * @access public
  1667.     * @param integer $row    Zero indexed row
  1668.     * @param integer $col    Zero indexed column
  1669.     * @param mixed   $format The XF format
  1670.     */
  1671.     function writeBlank($row$col$format)
  1672.     {
  1673.         // Don't write a blank cell unless it has a format
  1674.         if ($format == 0)
  1675.         {
  1676.             return(0);
  1677.         }
  1678.     
  1679.         $record    = 0x0201;                 // Record identifier
  1680.         $length    = 0x0006;                 // Number of bytes to follow
  1681.         $xf        $this->_XF($format);    // The cell format
  1682.     
  1683.         // Check that row and col are valid and store max and min values
  1684.         if ($row >= $this->_xls_rowmax
  1685.         {
  1686.             return(-2);
  1687.         }
  1688.         if ($col >= $this->_xls_colmax
  1689.         {
  1690.             return(-2);
  1691.         }
  1692.         if ($row <  $this->_dim_rowmin
  1693.         {
  1694.             $this->_dim_rowmin $row;
  1695.         }
  1696.         if ($row >  $this->_dim_rowmax
  1697.         {
  1698.             $this->_dim_rowmax $row;
  1699.         }
  1700.         if ($col <  $this->_dim_colmin
  1701.         {
  1702.             $this->_dim_colmin $col;
  1703.         }
  1704.         if ($col >  $this->_dim_colmax
  1705.         {
  1706.             $this->_dim_colmax $col;
  1707.         }
  1708.     
  1709.         $header    pack("vv",  $record$length);
  1710.         $data      pack("vvv"$row$col$xf);
  1711.         $this->_append($header.$data);
  1712.         return 0;
  1713.     }
  1714.  
  1715.     /**
  1716.     * Write a formula to the specified row and column (zero indexed).
  1717.     * The textual representation of the formula is passed to the parser in
  1718.     * Parser.php which returns a packed binary string.
  1719.     *
  1720.     * Returns  0 : normal termination
  1721.     *         -1 : formula errors (bad formula)
  1722.     *         -2 : row or column out of range
  1723.     *
  1724.     * @access public
  1725.     * @param integer $row     Zero indexed row
  1726.     * @param integer $col     Zero indexed column
  1727.     * @param string  $formula The formula text string
  1728.     * @param mixed   $format  The optional XF format
  1729.     * @return integer 
  1730.     */
  1731.     function writeFormula($row$col$formula$format = 0)
  1732.     {
  1733.         $record    = 0x0006;     // Record identifier
  1734.     
  1735.         // Excel normally stores the last calculated value of the formula in $num.
  1736.         // Clearly we are not in a position to calculate this a priori. Instead
  1737.         // we set $num to zero and set the option flags in $grbit to ensure
  1738.         // automatic calculation of the formula when the file is opened.
  1739.         //
  1740.         $xf        $this->_XF($format)// The cell format
  1741.         $num       = 0x00;                // Current value of formula
  1742.         $grbit     = 0x03;                // Option flags
  1743.         $unknown   = 0x0000;              // Must be zero
  1744.     
  1745.     
  1746.         // Check that row and col are valid and store max and min values
  1747.         if ($this->_checkRowCol($row$col== false{
  1748.             return -2;
  1749.         }
  1750.     
  1751.         // Strip the '=' or '@' sign at the beginning of the formula string
  1752.         if (preg_match("/^=/",$formula)) {
  1753.             $formula preg_replace("/(^=)/","",$formula);
  1754.         }
  1755.         elseif (preg_match("/^@/",$formula)) {
  1756.             $formula preg_replace("/(^@)/","",$formula);
  1757.         }
  1758.         else
  1759.         {
  1760.             // Error handling
  1761.             $this->writeString($row$col'Unrecognised character for formula');
  1762.             return -1;
  1763.         }
  1764.     
  1765.         // Parse the formula using the parser in Parser.php
  1766.         $error $this->_parser->parse($formula);
  1767.         if ($this->isError($error))
  1768.         {
  1769.             $this->writeString($row$col$error->getMessage())
  1770.             return -1;
  1771.         }
  1772.     
  1773.         $formula $this->_parser->toReversePolish();
  1774.         if ($this->isError($formula))
  1775.         {
  1776.             $this->writeString($row$col$formula->getMessage());
  1777.             return -1;
  1778.         }
  1779.     
  1780.         $formlen    strlen($formula);    // Length of the binary string
  1781.         $length     = 0x16 + $formlen;     // Length of the record data
  1782.     
  1783.         $header    pack("vv",      $record$length);
  1784.         $data      pack("vvvdvVv"$row$col$xf$num,
  1785.                                      $grbit$unknown$formlen);
  1786.     
  1787.         $this->_append($header.$data.$formula);
  1788.         return 0;
  1789.     }
  1790.     
  1791.     /**
  1792.     * Write a hyperlink.
  1793.     * This is comprised of two elements: the visible label and
  1794.     * the invisible link. The visible label is the same as the link unless an
  1795.     * alternative string is specified. The label is written using the
  1796.     * writeString() method. Therefore the 255 characters string limit applies.
  1797.     * $string and $format are optional.
  1798.     *
  1799.     * The hyperlink can be to a http, ftp, mail, internal sheet (not yet), or external
  1800.     * directory url.
  1801.     *
  1802.     * Returns  0 : normal termination
  1803.     *         -2 : row or column out of range
  1804.     *         -3 : long string truncated to 255 chars
  1805.     *
  1806.     * @access public
  1807.     * @param integer $row    Row
  1808.     * @param integer $col    Column
  1809.     * @param string  $url    URL string
  1810.     * @param string  $string Alternative label
  1811.     * @param mixed   $format The cell format
  1812.     * @return integer 
  1813.     */
  1814.     function writeUrl($row$col$url$string ''$format = 0)
  1815.     {
  1816.         // Add start row and col to arg list
  1817.         return($this->_writeUrlRange($row$col$row$col$url$string$format));
  1818.     }
  1819.     
  1820.     /**
  1821.     * This is the more general form of writeUrl(). It allows a hyperlink to be
  1822.     * written to a range of cells. This function also decides the type of hyperlink
  1823.     * to be written. These are either, Web (http, ftp, mailto), Internal
  1824.     * (Sheet1!A1) or external ('c:\temp\foo.xls#Sheet1!A1').
  1825.     *
  1826.     * @access private
  1827.     * @see writeUrl()
  1828.     * @param integer $row1   Start row
  1829.     * @param integer $col1   Start column
  1830.     * @param integer $row2   End row
  1831.     * @param integer $col2   End column
  1832.     * @param string  $url    URL string
  1833.     * @param string  $string Alternative label
  1834.     * @param mixed   $format The cell format
  1835.     * @return integer 
  1836.     */
  1837.     
  1838.     function _writeUrlRange($row1$col1$row2$col2$url$string ''$format = 0)
  1839.     {
  1840.     
  1841.         // Check for internal/external sheet links or default to web link
  1842.         if (preg_match('[^internal:]'$url)) {
  1843.             return($this->_writeUrlInternal($row1$col1$row2$col2$url$string$format));
  1844.         }
  1845.         if (preg_match('[^external:]'$url)) {
  1846.             return($this->_writeUrlExternal($row1$col1$row2$col2$url$string$format));
  1847.         }
  1848.         return($this->_writeUrlWeb($row1$col1$row2$col2$url$string$format));
  1849.     }
  1850.     
  1851.     
  1852.     /**
  1853.     * Used to write http, ftp and mailto hyperlinks.
  1854.     * The link type ($options) is 0x03 is the same as absolute dir ref without
  1855.     * sheet. However it is differentiated by the $unknown2 data stream.
  1856.     *
  1857.     * @access private
  1858.     * @see writeUrl()
  1859.     * @param integer $row1   Start row
  1860.     * @param integer $col1   Start column
  1861.     * @param integer $row2   End row
  1862.     * @param integer $col2   End column
  1863.     * @param string  $url    URL string
  1864.     * @param string  $str    Alternative label
  1865.     * @param mixed   $format The cell format
  1866.     * @return integer 
  1867.     */
  1868.     function _writeUrlWeb($row1$col1$row2$col2$url$str$format = 0)
  1869.     {
  1870.         $record      = 0x01B8;                       // Record identifier
  1871.         $length      = 0x00000;                      // Bytes to follow
  1872.     
  1873.         if ($format == 0{
  1874.             $format $this->_url_format;
  1875.         }
  1876.     
  1877.         // Write the visible label using the writeString() method.
  1878.         if ($str == ''{
  1879.             $str $url;
  1880.         }
  1881.         $str_error $this->writeString($row1$col1$str$format);
  1882.         if (($str_error == -2or ($str_error == -3)) {
  1883.             return $str_error;
  1884.         }
  1885.     
  1886.         // Pack the undocumented parts of the hyperlink stream
  1887.         $unknown1    pack("H*""D0C9EA79F9BACE118C8200AA004BA90B02000000");
  1888.         $unknown2    pack("H*""E0C9EA79F9BACE118C8200AA004BA90B");
  1889.     
  1890.         // Pack the option flags
  1891.         $options     pack("V"0x03);
  1892.     
  1893.         // Convert URL to a null terminated wchar string
  1894.         $url         join("\0"preg_split("''"$url-1PREG_SPLIT_NO_EMPTY));
  1895.         $url         $url "\0\0\0";
  1896.     
  1897.         // Pack the length of the URL
  1898.         $url_len     pack("V"strlen($url));
  1899.     
  1900.         // Calculate the data length
  1901.         $length      = 0x34 + strlen($url);
  1902.     
  1903.         // Pack the header data
  1904.         $header      pack("vv",   $record$length);
  1905.         $data        pack("vvvv"$row1$row2$col1$col2);
  1906.     
  1907.         // Write the packed data
  1908.         $this->_append$header$data.
  1909.                         $unknown1$options.
  1910.                         $unknown2$url_len$url);
  1911.         return($str_error);
  1912.     }
  1913.     
  1914.     /**
  1915.     * Used to write internal reference hyperlinks such as "Sheet1!A1".
  1916.     *
  1917.     * @access private
  1918.     * @see writeUrl()
  1919.     * @param integer $row1   Start row
  1920.     * @param integer $col1   Start column
  1921.     * @param integer $row2   End row
  1922.     * @param integer $col2   End column
  1923.     * @param string  $url    URL string
  1924.     * @param string  $str    Alternative label
  1925.     * @param mixed   $format The cell format
  1926.     * @return integer 
  1927.     */
  1928.     function _writeUrlInternal($row1$col1$row2$col2$url$str$format = 0)
  1929.     {
  1930.         $record      = 0x01B8;                       // Record identifier
  1931.         $length      = 0x00000;                      // Bytes to follow
  1932.     
  1933.         if ($format == 0{
  1934.             $format $this->_url_format;
  1935.         }
  1936.     
  1937.         // Strip URL type
  1938.         $url preg_replace('s[^internal:]'''$url);
  1939.     
  1940.         // Write the visible label
  1941.         if ($str == ''{
  1942.             $str $url;
  1943.         }
  1944.         $str_error $this->writeString($row1$col1$str$format);
  1945.         if (($str_error == -2or ($str_error == -3)) {
  1946.             return $str_error;
  1947.         }
  1948.     
  1949.         // Pack the undocumented parts of the hyperlink stream
  1950.         $unknown1    pack("H*""D0C9EA79F9BACE118C8200AA004BA90B02000000");
  1951.     
  1952.         // Pack the option flags
  1953.         $options     pack("V"0x08);
  1954.     
  1955.         // Convert the URL type and to a null terminated wchar string
  1956.         $url         join("\0"preg_split("''"$url-1PREG_SPLIT_NO_EMPTY));
  1957.         $url         $url "\0\0\0";
  1958.     
  1959.         // Pack the length of the URL as chars (not wchars)
  1960.         $url_len     pack("V"floor(strlen($url)/2));
  1961.     
  1962.         // Calculate the data length
  1963.         $length      = 0x24 + strlen($url);
  1964.     
  1965.         // Pack the header data
  1966.         $header      pack("vv",   $record$length);
  1967.         $data        pack("vvvv"$row1$row2$col1$col2);
  1968.     
  1969.         // Write the packed data
  1970.         $this->_append($header$data.
  1971.                        $unknown1$options.
  1972.                        $url_len$url);
  1973.         return($str_error);
  1974.     }
  1975.     
  1976.     /**
  1977.     * Write links to external directory names such as 'c:\foo.xls',
  1978.     * c:\foo.xls#Sheet1!A1', '../../foo.xls'. and '../../foo.xls#Sheet1!A1'.
  1979.     *
  1980.     * Note: Excel writes some relative links with the $dir_long string. We ignore
  1981.     * these cases for the sake of simpler code.
  1982.     *
  1983.     * @access private
  1984.     * @see writeUrl()
  1985.     * @param integer $row1   Start row
  1986.     * @param integer $col1   Start column
  1987.     * @param integer $row2   End row
  1988.     * @param integer $col2   End column
  1989.     * @param string  $url    URL string
  1990.     * @param string  $str    Alternative label
  1991.     * @param mixed   $format The cell format
  1992.     * @return integer 
  1993.     */
  1994.     function _writeUrlExternal($row1$col1$row2$col2$url$str$format = 0)
  1995.     {
  1996.         // Network drives are different. We will handle them separately
  1997.         // MS/Novell network drives and shares start with \\
  1998.         if (preg_match('[^external:\\\\]'$url)) {
  1999.             return//($this->_writeUrlExternal_net($row1, $col1, $row2, $col2, $url, $str, $format));
  2000.         }
  2001.     
  2002.         $record      = 0x01B8;                       // Record identifier
  2003.         $length      = 0x00000;                      // Bytes to follow
  2004.     
  2005.         if ($format == 0{
  2006.             $format $this->_url_format;
  2007.         }
  2008.     
  2009.         // Strip URL type and change Unix dir separator to Dos style (if needed)
  2010.         //
  2011.         $url preg_replace('[^external:]'''$url);
  2012.         $url preg_replace('[/]'"\\"$url);
  2013.     
  2014.         // Write the visible label
  2015.         if ($str == ''{
  2016.             $str preg_replace('[\#]'' - '$url);
  2017.         }
  2018.         $str_error $this->writeString($row1$col1$str$format);
  2019.         if (($str_error == -2or ($str_error == -3)) {
  2020.             return $str_error;
  2021.         }
  2022.     
  2023.         // Determine if the link is relative or absolute:
  2024.         //   relative if link contains no dir separator, "somefile.xls"
  2025.         //   relative if link starts with up-dir, "..\..\somefile.xls"
  2026.         //   otherwise, absolute
  2027.         
  2028.         $absolute    = 0x02; // Bit mask
  2029.         if (!preg_match('[\\]'$url)) {
  2030.             $absolute    = 0x00;
  2031.         }
  2032.         if (preg_match('[^\.\.\\]'$url)) {
  2033.             $absolute    = 0x00;
  2034.         }
  2035.     
  2036.         // Determine if the link contains a sheet reference and change some of the
  2037.         // parameters accordingly.
  2038.         // Split the dir name and sheet name (if it exists)
  2039.         list($dir_long $sheetsplit('/\#/'$url);
  2040.         $link_type               = 0x01 | $absolute;
  2041.     
  2042.         if (isset($sheet)) {
  2043.             $link_type |= 0x08;
  2044.             $sheet_len  pack("V"strlen($sheet+ 0x01);
  2045.             $sheet      join("\0"split(''$sheet));
  2046.             $sheet     .= "\0\0\0";
  2047.         }
  2048.         else {
  2049.             $sheet_len   '';
  2050.             $sheet       '';
  2051.         }
  2052.     
  2053.         // Pack the link type
  2054.         $link_type   pack("V"$link_type);
  2055.     
  2056.         // Calculate the up-level dir count e.g.. (..\..\..\ == 3)
  2057.         $up_count    preg_match_all("/\.\.\\/"$dir_long$useless);
  2058.         $up_count    pack("v"$up_count);
  2059.     
  2060.         // Store the short dos dir name (null terminated)
  2061.         $dir_short   preg_replace('/\.\.\\/'''$dir_long"\0";
  2062.     
  2063.         // Store the long dir name as a wchar string (non-null terminated)
  2064.         $dir_long       join("\0"split(''$dir_long));
  2065.         $dir_long       $dir_long "\0";
  2066.     
  2067.         // Pack the lengths of the dir strings
  2068.         $dir_short_len pack("V"strlen($dir_short)      );
  2069.         $dir_long_len  pack("V"strlen($dir_long)       );
  2070.         $stream_len    pack("V"strlen($dir_long+ 0x06);
  2071.     
  2072.         // Pack the undocumented parts of the hyperlink stream
  2073.         $unknown1 pack("H*",'D0C9EA79F9BACE118C8200AA004BA90B02000000'       );
  2074.         $unknown2 pack("H*",'0303000000000000C000000000000046'               );
  2075.         $unknown3 pack("H*",'FFFFADDE000000000000000000000000000000000000000');
  2076.         $unknown4 pack("v",  0x03                                            );
  2077.     
  2078.         // Pack the main data stream
  2079.         $data        pack("vvvv"$row1$row2$col1$col2.
  2080.                           $unknown1     .
  2081.                           $link_type    .
  2082.                           $unknown2     .
  2083.                           $up_count     .
  2084.                           $dir_short_len.
  2085.                           $dir_short    .
  2086.                           $unknown3     .
  2087.                           $stream_len   .
  2088.                           $dir_long_len .
  2089.                           $unknown4     .
  2090.                           $dir_long     .
  2091.                           $sheet_len    .
  2092.                           $sheet        ;
  2093.     
  2094.         // Pack the header data
  2095.         $length   strlen($data);
  2096.         $header   pack("vv"$record$length);
  2097.     
  2098.         // Write the packed data
  2099.         $this->_append($header$data);
  2100.         return($str_error);
  2101.     }
  2102.     
  2103.     
  2104.     /**
  2105.     * This method is used to set the height and format for a row.
  2106.     *
  2107.     * @access public
  2108.     * @param integer $row    The row to set
  2109.     * @param integer $height Height we are giving to the row.
  2110.     *                         Use NULL to set XF without setting height
  2111.     * @param mixed   $format XF format we are giving to the row
  2112.     * @param bool    $hidden The optional hidden attribute
  2113.     * @param integer $level  The optional outline level for row, in range [0,7]
  2114.     */
  2115.     function setRow($row$height$format = 0$hidden = false$level = 0)
  2116.     {
  2117.         $record      = 0x0208;               // Record identifier
  2118.         $length      = 0x0010;               // Number of bytes to follow
  2119.     
  2120.         $colMic      = 0x0000;               // First defined column
  2121.         $colMac      = 0x0000;               // Last defined column
  2122.         $irwMac      = 0x0000;               // Used by Excel to optimise loading
  2123.         $reserved    = 0x0000;               // Reserved
  2124.         $grbit       = 0x0000;               // Option flags
  2125.         $ixfe        $this->_XF($format);  // XF index
  2126.  
  2127.         // set _row_sizes so _sizeRow() can use it
  2128.         $this->_row_sizes[$row$height;
  2129.  
  2130.         // Use setRow($row, NULL, $XF) to set XF format without setting height
  2131.         if ($height != NULL{
  2132.             $miyRw $height * 20;  // row height
  2133.         }
  2134.         else {
  2135.             $miyRw = 0xff;          // default row height is 256
  2136.         }
  2137.  
  2138.         $level max(0min($level7));  // level should be between 0 and 7
  2139.         $this->_outline_row_level max($level$this->_outline_row_level);
  2140.  
  2141.  
  2142.         // Set the options flags. fUnsynced is used to show that the font and row
  2143.         // heights are not compatible. This is usually the case for WriteExcel.
  2144.         // The collapsed flag 0x10 doesn't seem to be used to indicate that a row
  2145.         // is collapsed. Instead it is used to indicate that the previous row is
  2146.         // collapsed. The zero height flag, 0x20, is used to collapse a row.
  2147.  
  2148.         $grbit |= $level;
  2149.         if ($hidden{
  2150.             $grbit |= 0x0020;
  2151.         }
  2152.         $grbit |= 0x0040; // fUnsynced
  2153.         if ($format{
  2154.             $grbit |= 0x0080;
  2155.         }
  2156.         $grbit |= 0x0100;
  2157.  
  2158.         $header   pack("vv",       $record$length);
  2159.         $data     pack("vvvvvvvv"$row$colMic$colMac$miyRw,
  2160.                                      $irwMac,$reserved$grbit$ixfe);
  2161.         $this->_append($header.$data);
  2162.     }
  2163.     
  2164.     /**
  2165.     * Writes Excel DIMENSIONS to define the area in which there is data.
  2166.     *
  2167.     * @access private
  2168.     */
  2169.     function _storeDimensions()
  2170.     {
  2171.         $record    = 0x0200;                 // Record identifier
  2172.         $row_min   $this->_dim_rowmin;     // First row
  2173.         $row_max   $this->_dim_rowmax + 1; // Last row plus 1
  2174.         $col_min   $this->_dim_colmin;     // First column
  2175.         $col_max   $this->_dim_colmax + 1; // Last column plus 1
  2176.         $reserved  = 0x0000;                 // Reserved by Excel
  2177.     
  2178.         if ($this->_BIFF_version == 0x0500{
  2179.             $length    = 0x000A;               // Number of bytes to follow
  2180.             $data      pack("vvvvv"$row_min$row_max,
  2181.                                        $col_min$col_max$reserved);
  2182.         }
  2183.         elseif ($this->_BIFF_version == 0x0600{
  2184.             $length    = 0x000E;
  2185.             $data      pack("VVvvv"$row_min$row_max,
  2186.                                        $col_min$col_max$reserved);
  2187.         }
  2188.         $header pack("vv"$record$length);
  2189.         $this->_prepend($header.$data);
  2190.     }
  2191.     
  2192.     /**
  2193.     * Write BIFF record Window2.
  2194.     *
  2195.     * @access private
  2196.     */
  2197.     function _storeWindow2()
  2198.     {
  2199.         $record         = 0x023E;     // Record identifier
  2200.         if ($this->_BIFF_version == 0x0500{
  2201.             $length         = 0x000A;     // Number of bytes to follow
  2202.         }
  2203.         elseif ($this->_BIFF_version == 0x0600{
  2204.             $length         = 0x0012;
  2205.         }
  2206.  
  2207.         $grbit          = 0x00B6;     // Option flags
  2208.         $rwTop          = 0x0000;     // Top row visible in window
  2209.         $colLeft        = 0x0000;     // Leftmost column visible in window
  2210.         
  2211.     
  2212.         // The options flags that comprise $grbit
  2213.         $fDspFmla       = 0;                     // 0 - bit
  2214.         $fDspGrid       $this->_screen_gridlines// 1
  2215.         $fDspRwCol      = 1;                     // 2
  2216.         $fFrozen        $this->_frozen;        // 3
  2217.         $fDspZeros      = 1;                     // 4
  2218.         $fDefaultHdr    = 1;                     // 5
  2219.         $fArabic        = 0;                     // 6
  2220.         $fDspGuts       $this->_outline_on;    // 7
  2221.         $fFrozenNoSplit = 0;                     // 0 - bit
  2222.         $fSelected      $this->selected;       // 1
  2223.         $fPaged         = 1;                     // 2
  2224.     
  2225.         $grbit             $fDspFmla;
  2226.         $grbit            |= $fDspGrid       << 1;
  2227.         $grbit            |= $fDspRwCol      << 2;
  2228.         $grbit            |= $fFrozen        << 3;
  2229.         $grbit            |= $fDspZeros      << 4;
  2230.         $grbit            |= $fDefaultHdr    << 5;
  2231.         $grbit            |= $fArabic        << 6;
  2232.         $grbit            |= $fDspGuts       << 7;
  2233.         $grbit            |= $fFrozenNoSplit << 8;
  2234.         $grbit            |= $fSelected      << 9;
  2235.         $grbit            |= $fPaged         << 10;
  2236.     
  2237.         $header  pack("vv",   $record$length);
  2238.         $data    pack("vvv"$grbit$rwTop$colLeft);
  2239.         // FIXME !!!
  2240.         if ($this->_BIFF_version == 0x0500{
  2241.             $rgbHdr         = 0x00000000; // Row/column heading and gridline color
  2242.             $data .= pack("V"$rgbHdr);
  2243.         }
  2244.         elseif ($this->_BIFF_version == 0x0600{
  2245.             $rgbHdr       = 0x0040; // Row/column heading and gridline color index
  2246.             $zoom_factor_page_break = 0x0000;
  2247.             $zoom_factor_normal     = 0x0000;
  2248.             $data .= pack("vvvvV"$rgbHdr0x0000$zoom_factor_page_break$zoom_factor_normal0x00000000);
  2249.         }
  2250.         $this->_append($header.$data);
  2251.     }
  2252.     
  2253.     /**
  2254.     * Write BIFF record DEFCOLWIDTH if COLINFO records are in use.
  2255.     *
  2256.     * @access private
  2257.     */
  2258.     function _storeDefcol()
  2259.     {
  2260.         $record   = 0x0055;      // Record identifier
  2261.         $length   = 0x0002;      // Number of bytes to follow
  2262.         $colwidth = 0x0008;      // Default column width
  2263.     
  2264.         $header   pack("vv"$record$length);
  2265.         $data     pack("v",  $colwidth);
  2266.         $this->_prepend($header.$data);
  2267.     }
  2268.     
  2269.     /**
  2270.     * Write BIFF record COLINFO to define column widths
  2271.     *
  2272.     * Note: The SDK says the record length is 0x0B but Excel writes a 0x0C
  2273.     * length record.
  2274.     *
  2275.     * @access private
  2276.     * @param array $col_array This is the only parameter received and is composed of the following:
  2277.     *                 0 => First formatted column,
  2278.     *                 1 => Last formatted column,
  2279.     *                 2 => Col width (8.43 is Excel default),
  2280.     *                 3 => The optional XF format of the column,
  2281.     *                 4 => Option flags.
  2282.     *                 5 => Optional outline level
  2283.     */
  2284.     function _storeColinfo($col_array)
  2285.     {
  2286.         if (isset($col_array[0])) {
  2287.             $colFirst $col_array[0];
  2288.         }
  2289.         if (isset($col_array[1])) {
  2290.             $colLast $col_array[1];
  2291.         }
  2292.         if (isset($col_array[2])) {
  2293.             $coldx $col_array[2];
  2294.         }
  2295.         else {
  2296.             $coldx = 8.43;
  2297.         }
  2298.         if (isset($col_array[3])) {
  2299.             $format $col_array[3];
  2300.         }
  2301.         else {
  2302.             $format = 0;
  2303.         }
  2304.         if (isset($col_array[4])) {
  2305.             $grbit $col_array[4];
  2306.         }
  2307.         else {
  2308.             $grbit = 0;
  2309.         }
  2310.         if (isset($col_array[5])) {
  2311.             $level $col_array[5];
  2312.         }
  2313.         else {
  2314.             $level = 0;
  2315.         }
  2316.         $record   = 0x007D;          // Record identifier
  2317.         $length   = 0x000B;          // Number of bytes to follow
  2318.     
  2319.         $coldx   += 0.72;            // Fudge. Excel subtracts 0.72 !?
  2320.         $coldx   *= 256;             // Convert to units of 1/256 of a char
  2321.     
  2322.         $ixfe     $this->_XF($format);
  2323.         $reserved = 0x00;            // Reserved
  2324.  
  2325.         $level max(0min($level7));
  2326.         $grbit |= $level << 8;
  2327.  
  2328.         $header   pack("vv",     $record$length);
  2329.         $data     pack("vvvvvC"$colFirst$colLast$coldx,
  2330.                                    $ixfe$grbit$reserved);
  2331.         $this->_prepend($header.$data);
  2332.     }
  2333.     
  2334.     /**
  2335.     * Write BIFF record SELECTION.
  2336.     *
  2337.     * @access private
  2338.     * @param array $array array containing ($rwFirst,$colFirst,$rwLast,$colLast)
  2339.     * @see setSelection()
  2340.     */
  2341.     function _storeSelection($array)
  2342.     {
  2343.         list($rwFirst,$colFirst,$rwLast,$colLast$array;
  2344.         $record   = 0x001D;                  // Record identifier
  2345.         $length   = 0x000F;                  // Number of bytes to follow
  2346.     
  2347.         $pnn      $this->_active_pane;     // Pane position
  2348.         $rwAct    $rwFirst;                // Active row
  2349.         $colAct   $colFirst;               // Active column
  2350.         $irefAct  = 0;                       // Active cell ref
  2351.         $cref     = 1;                       // Number of refs
  2352.     
  2353.         if (!isset($rwLast)) {
  2354.             $rwLast   $rwFirst;       // Last  row in reference
  2355.         }
  2356.         if (!isset($colLast)) {
  2357.             $colLast  $colFirst;      // Last  col in reference
  2358.         }
  2359.     
  2360.         // Swap last row/col for first row/col as necessary
  2361.         if ($rwFirst $rwLast)
  2362.         {
  2363.             list($rwFirst$rwLast= array($rwLast$rwFirst);
  2364.         }
  2365.     
  2366.         if ($colFirst $colLast)
  2367.         {
  2368.             list($colFirst$colLast= array($colLast$colFirst);
  2369.         }
  2370.     
  2371.         $header   pack("vv",         $record$length);
  2372.         $data     pack("CvvvvvvCC",  $pnn$rwAct$colAct,
  2373.                                        $irefAct$cref,
  2374.                                        $rwFirst$rwLast,
  2375.                                        $colFirst$colLast);
  2376.         $this->_append($header.$data);
  2377.     }
  2378.  
  2379.     /**
  2380.     * Store the MERGEDCELLS record for all ranges of merged cells
  2381.     *
  2382.     * @access private
  2383.     */
  2384.     function _storeMergedCells()
  2385.     {
  2386.         // if there are no merged cell ranges set, return
  2387.         if (count($this->_merged_ranges== 0{
  2388.             return;
  2389.         }
  2390.         $record   = 0x00E5;
  2391.         $length   = 2 + count($this->_merged_ranges* 8; 
  2392.  
  2393.         $header   pack('vv'$record$length);
  2394.         $data     pack('v',  count($this->_merged_ranges));
  2395.         foreach ($this->_merged_ranges as $range{
  2396.             $data .= pack('vvvv'$range[0]$range[2]$range[1]$range[3]);
  2397.         }
  2398.         $this->_append($header.$data);
  2399.     }
  2400.     
  2401.     /**
  2402.     * Write BIFF record EXTERNCOUNT to indicate the number of external sheet
  2403.     * references in a worksheet.
  2404.     *
  2405.     * Excel only stores references to external sheets that are used in formulas.
  2406.     * For simplicity we store references to all the sheets in the workbook
  2407.     * regardless of whether they are used or not. This reduces the overall
  2408.     * complexity and eliminates the need for a two way dialogue between the formula
  2409.     * parser the worksheet objects.
  2410.     *
  2411.     * @access private
  2412.     * @param integer $count The number of external sheet references in this worksheet
  2413.     */
  2414.     function _storeExterncount($count)
  2415.     {
  2416.         $record   = 0x0016;          // Record identifier
  2417.         $length   = 0x0002;          // Number of bytes to follow
  2418.     
  2419.         $header   pack("vv"$record$length);
  2420.         $data     pack("v",  $count);
  2421.         $this->_prepend($header.$data);
  2422.     }
  2423.     
  2424.     /**
  2425.     * Writes the Excel BIFF EXTERNSHEET record. These references are used by
  2426.     * formulas. A formula references a sheet name via an index. Since we store a
  2427.     * reference to all of the external worksheets the EXTERNSHEET index is the same
  2428.     * as the worksheet index.
  2429.     *
  2430.     * @access private
  2431.     * @param string $sheetname The name of a external worksheet
  2432.     */
  2433.     function _storeExternsheet($sheetname)
  2434.     {
  2435.         $record    = 0x0017;         // Record identifier
  2436.     
  2437.         // References to the current sheet are encoded differently to references to
  2438.         // external sheets.
  2439.         //
  2440.         if ($this->name == $sheetname{
  2441.             $sheetname '';
  2442.             $length    = 0x02;  // The following 2 bytes
  2443.             $cch       = 1;     // The following byte
  2444.             $rgch      = 0x02;  // Self reference
  2445.         }
  2446.         else {
  2447.             $length    = 0x02 + strlen($sheetname);
  2448.             $cch       strlen($sheetname);
  2449.             $rgch      = 0x03;  // Reference to a sheet in the current workbook
  2450.         }
  2451.     
  2452.         $header     pack("vv",  $record$length);
  2453.         $data       pack("CC"$cch$rgch);
  2454.         $this->_prepend($header.$data.$sheetname);
  2455.     }
  2456.     
  2457.     /**
  2458.     * Writes the Excel BIFF PANE record.
  2459.     * The panes can either be frozen or thawed (unfrozen).
  2460.     * Frozen panes are specified in terms of an integer number of rows and columns.
  2461.     * Thawed panes are specified in terms of Excel's units for rows and columns.
  2462.     *
  2463.     * @access private
  2464.     * @param array $panes This is the only parameter received and is composed of the following:
  2465.     *                      0 => Vertical split position,
  2466.     *                      1 => Horizontal split position
  2467.     *                      2 => Top row visible
  2468.     *                      3 => Leftmost column visible
  2469.     *                      4 => Active pane
  2470.     */
  2471.     function _storePanes($panes)
  2472.     {
  2473.         $y       $panes[0];
  2474.         $x       $panes[1];
  2475.         $rwTop   $panes[2];
  2476.         $colLeft $panes[3];
  2477.         if (count($panes> 4// if Active pane was received
  2478.             $pnnAct $panes[4];
  2479.         }
  2480.         else {
  2481.             $pnnAct = NULL;
  2482.         }
  2483.         $record  = 0x0041;       // Record identifier
  2484.         $length  = 0x000A;       // Number of bytes to follow
  2485.     
  2486.         // Code specific to frozen or thawed panes.
  2487.         if ($this->_frozen)
  2488.         {
  2489.             // Set default values for $rwTop and $colLeft
  2490.             if (!isset($rwTop)) {
  2491.                 $rwTop   $y;
  2492.             }
  2493.             if (!isset($colLeft)) {
  2494.                 $colLeft $x;
  2495.             }
  2496.         }
  2497.         else
  2498.         {
  2499.             // Set default values for $rwTop and $colLeft
  2500.             if (!isset($rwTop)) {
  2501.                 $rwTop   = 0;
  2502.             }
  2503.             if (!isset($colLeft)) {
  2504.                 $colLeft = 0;
  2505.             }
  2506.     
  2507.             // Convert Excel's row and column units to the internal units.
  2508.             // The default row height is 12.75
  2509.             // The default column width is 8.43
  2510.             // The following slope and intersection values were interpolated.
  2511.             //
  2512.             $y = 20*$y      + 255;
  2513.             $x = 113.879*$x + 390;
  2514.         }
  2515.     
  2516.     
  2517.         // Determine which pane should be active. There is also the undocumented
  2518.         // option to override this should it be necessary: may be removed later.
  2519.         //
  2520.         if (!isset($pnnAct))
  2521.         {
  2522.             if ($x != 0 and $y != 0)
  2523.                 $pnnAct = 0; // Bottom right
  2524.             if ($x != 0 and $y == 0)
  2525.                 $pnnAct = 1; // Top right
  2526.             if ($x == 0 and $y != 0)
  2527.                 $pnnAct = 2; // Bottom left
  2528.             if ($x == 0 and $y == 0)
  2529.                 $pnnAct = 3; // Top left
  2530.         }
  2531.     
  2532.         $this->_active_pane $pnnAct// Used in _storeSelection
  2533.     
  2534.         $header     pack("vv",    $record$length);
  2535.         $data       pack("vvvvv"$x$y$rwTop$colLeft$pnnAct);
  2536.         $this->_append($header.$data);
  2537.     }
  2538.     
  2539.     /**
  2540.     * Store the page setup SETUP BIFF record.
  2541.     *
  2542.     * @access private
  2543.     */
  2544.     function _storeSetup()
  2545.     {
  2546.         $record       = 0x00A1;                  // Record identifier
  2547.         $length       = 0x0022;                  // Number of bytes to follow
  2548.     
  2549.         $iPaperSize   $this->_paper_size;    // Paper size
  2550.         $iScale       $this->_print_scale;   // Print scaling factor
  2551.         $iPageStart   = 0x01;                 // Starting page number
  2552.         $iFitWidth    $this->_fit_width;    // Fit to number of pages wide
  2553.         $iFitHeight   $this->_fit_height;   // Fit to number of pages high
  2554.         $grbit        = 0x00;                 // Option flags
  2555.         $iRes         = 0x0258;               // Print resolution
  2556.         $iVRes        = 0x0258;               // Vertical print resolution
  2557.         $numHdr       $this->_margin_head;  // Header Margin
  2558.         $numFtr       $this->_margin_foot;   // Footer Margin
  2559.         $iCopies      = 0x01;                 // Number of copies
  2560.     
  2561.         $fLeftToRight = 0x0;                     // Print over then down
  2562.         $fLandscape   $this->_orientation;     // Page orientation
  2563.         $fNoPls       = 0x0;                     // Setup not read from printer
  2564.         $fNoColor     = 0x0;                     // Print black and white
  2565.         $fDraft       = 0x0;                     // Print draft quality
  2566.         $fNotes       = 0x0;                     // Print notes
  2567.         $fNoOrient    = 0x0;                     // Orientation not set
  2568.         $fUsePage     = 0x0;                     // Use custom starting page
  2569.     
  2570.         $grbit           $fLeftToRight;
  2571.         $grbit          |= $fLandscape    << 1;
  2572.         $grbit          |= $fNoPls        << 2;
  2573.         $grbit          |= $fNoColor      << 3;
  2574.         $grbit          |= $fDraft        << 4;
  2575.         $grbit          |= $fNotes        << 5;
  2576.         $grbit          |= $fNoOrient     << 6;
  2577.         $grbit          |= $fUsePage      << 7;
  2578.     
  2579.         $numHdr pack("d"$numHdr);
  2580.         $numFtr pack("d"$numFtr);
  2581.         if ($this->_byte_order// if it's Big Endian
  2582.         {
  2583.             $numHdr strrev($numHdr);
  2584.             $numFtr strrev($numFtr);
  2585.         }
  2586.     
  2587.         $header pack("vv"$record$length);
  2588.         $data1  pack("vvvvvvvv"$iPaperSize,
  2589.                                    $iScale,
  2590.                                    $iPageStart,
  2591.                                    $iFitWidth,
  2592.                                    $iFitHeight,
  2593.                                    $grbit,
  2594.                                    $iRes,
  2595.                                    $iVRes);
  2596.         $data2  $numHdr.$numFtr;
  2597.         $data3  pack("v"$iCopies);
  2598.         $this->_prepend($header.$data1.$data2.$data3);
  2599.     }
  2600.     
  2601.     /**
  2602.     * Store the header caption BIFF record.
  2603.     *
  2604.     * @access private
  2605.     */
  2606.     function _storeHeader()
  2607.     {
  2608.         $record  = 0x0014;               // Record identifier
  2609.     
  2610.         $str      $this->_header;       // header string
  2611.         $cch      strlen($str);         // Length of header string
  2612.         if ($this->_BIFF_version == 0x0600{
  2613.             $encoding = 0x0;                  // TODO: Unicode support
  2614.             $length   = 3 + $cch;             // Bytes to follow
  2615.         }
  2616.         else {
  2617.             $length  = 1 + $cch;             // Bytes to follow
  2618.         }
  2619.         $header   pack("vv"$record$length);
  2620.         if ($this->_BIFF_version == 0x0600{
  2621.             $data     pack("vC",  $cch$encoding);
  2622.         }
  2623.         else {
  2624.             $data      pack("C",  $cch);
  2625.         }
  2626.     
  2627.         $this->_append($header.$data.$str);
  2628.     }
  2629.     
  2630.     /**
  2631.     * Store the footer caption BIFF record.
  2632.     *
  2633.     * @access private
  2634.     */
  2635.     function _storeFooter()
  2636.     {
  2637.         $record  = 0x0015;               // Record identifier
  2638.     
  2639.         $str      $this->_footer;       // Footer string
  2640.         $cch      strlen($str);         // Length of footer string
  2641.         if ($this->_BIFF_version == 0x0600{
  2642.             $encoding = 0x0;                  // TODO: Unicode support
  2643.             $length   = 3 + $cch;             // Bytes to follow
  2644.         }
  2645.         else {
  2646.             $length  = 1 + $cch;
  2647.         }
  2648.         $header    pack("vv"$record$length);
  2649.         if ($this->_BIFF_version == 0x0600{
  2650.             $data      pack("vC",  $cch$encoding);
  2651.         }
  2652.         else {
  2653.             $data      pack("C",  $cch);
  2654.         }
  2655.     
  2656.         $this->_append($header.$data.$str);
  2657.     }
  2658.     
  2659.     /**
  2660.     * Store the horizontal centering HCENTER BIFF record.
  2661.     *
  2662.     * @access private
  2663.     */
  2664.     function _storeHcenter()
  2665.     {
  2666.         $record   = 0x0083;              // Record identifier
  2667.         $length   = 0x0002;              // Bytes to follow
  2668.     
  2669.         $fHCenter $this->_hcenter;     // Horizontal centering
  2670.     
  2671.         $header    pack("vv"$record$length);
  2672.         $data      pack("v",  $fHCenter);
  2673.     
  2674.         $this->_append($header.$data);
  2675.     }
  2676.     
  2677.     /**
  2678.     * Store the vertical centering VCENTER BIFF record.
  2679.     *
  2680.     * @access private
  2681.     */
  2682.     function _storeVcenter()
  2683.     {
  2684.         $record   = 0x0084;              // Record identifier
  2685.         $length   = 0x0002;              // Bytes to follow
  2686.     
  2687.         $fVCenter $this->_vcenter;     // Horizontal centering
  2688.     
  2689.         $header    pack("vv"$record$length);
  2690.         $data      pack("v",  $fVCenter);
  2691.         $this->_append($header.$data);
  2692.     }
  2693.     
  2694.     /**
  2695.     * Store the LEFTMARGIN BIFF record.
  2696.     *
  2697.     * @access private
  2698.     */
  2699.     function _storeMarginLeft()
  2700.     {
  2701.         $record  = 0x0026;                   // Record identifier
  2702.         $length  = 0x0008;                   // Bytes to follow
  2703.     
  2704.         $margin  $this->_margin_left;       // Margin in inches
  2705.     
  2706.         $header    pack("vv",  $record$length);
  2707.         $data      pack("d",   $margin);
  2708.         if ($this->_byte_order// if it's Big Endian
  2709.         
  2710.             $data strrev($data);
  2711.         }
  2712.     
  2713.         $this->_append($header.$data);
  2714.     }
  2715.     
  2716.     /**
  2717.     * Store the RIGHTMARGIN BIFF record.
  2718.     *
  2719.     * @access private
  2720.     */
  2721.     function _storeMarginRight()
  2722.     {
  2723.         $record  = 0x0027;                   // Record identifier
  2724.         $length  = 0x0008;                   // Bytes to follow
  2725.     
  2726.         $margin  $this->_margin_right;      // Margin in inches
  2727.     
  2728.         $header    pack("vv",  $record$length);
  2729.         $data      pack("d",   $margin);
  2730.         if ($this->_byte_order// if it's Big Endian
  2731.         
  2732.             $data strrev($data);
  2733.         }
  2734.     
  2735.         $this->_append($header.$data);
  2736.     }
  2737.     
  2738.     /**
  2739.     * Store the TOPMARGIN BIFF record.
  2740.     *
  2741.     * @access private
  2742.     */
  2743.     function _storeMarginTop()
  2744.     {
  2745.         $record  = 0x0028;                   // Record identifier
  2746.         $length  = 0x0008;                   // Bytes to follow
  2747.     
  2748.         $margin  $this->_margin_top;        // Margin in inches
  2749.     
  2750.         $header    pack("vv",  $record$length);
  2751.         $data      pack("d",   $margin);
  2752.         if ($this->_byte_order// if it's Big Endian
  2753.         
  2754.             $data strrev($data);
  2755.         }
  2756.     
  2757.         $this->_append($header.$data);
  2758.     }
  2759.     
  2760.     /**
  2761.     * Store the BOTTOMMARGIN BIFF record.
  2762.     *
  2763.     * @access private
  2764.     */
  2765.     function _storeMarginBottom()
  2766.     {
  2767.         $record  = 0x0029;                   // Record identifier
  2768.         $length  = 0x0008;                   // Bytes to follow
  2769.     
  2770.         $margin  $this->_margin_bottom;     // Margin in inches
  2771.     
  2772.         $header    pack("vv",  $record$length);
  2773.         $data      pack("d",   $margin);
  2774.         if ($this->_byte_order// if it's Big Endian
  2775.         
  2776.             $data strrev($data);
  2777.         }
  2778.     
  2779.         $this->_append($header.$data);
  2780.     }
  2781.  
  2782.     /**
  2783.     * Merges the area given by its arguments.
  2784.     * This is an Excel97/2000 method. It is required to perform more complicated
  2785.     * merging than the normal setAlign('merge').
  2786.     *
  2787.     * @access public
  2788.     * @param integer $first_row First row of the area to merge
  2789.     * @param integer $first_col First column of the area to merge
  2790.     * @param integer $last_row  Last row of the area to merge
  2791.     * @param integer $last_col  Last column of the area to merge
  2792.     */
  2793.     function mergeCells($first_row$first_col$last_row$last_col)
  2794.     {
  2795.         $record  = 0x00E5;                   // Record identifier
  2796.         $length  = 0x000A;                   // Bytes to follow
  2797.         $cref     = 1;                       // Number of refs
  2798.  
  2799.         // Swap last row/col for first row/col as necessary
  2800.         if ($first_row $last_row{
  2801.             list($first_row$last_row= array($last_row$first_row);
  2802.         }
  2803.     
  2804.         if ($first_col $last_col{
  2805.             list($first_col$last_col= array($last_col$first_col);
  2806.         }
  2807.     
  2808.         $header   pack("vv",    $record$length);
  2809.         $data     pack("vvvvv"$cref$first_row$last_row,
  2810.                                   $first_col$last_col);
  2811.     
  2812.         $this->_append($header.$data);
  2813.     }
  2814.  
  2815.     /**
  2816.     * Write the PRINTHEADERS BIFF record.
  2817.     *
  2818.     * @access private
  2819.     */
  2820.     function _storePrintHeaders()
  2821.     {
  2822.         $record      = 0x002a;                   // Record identifier
  2823.         $length      = 0x0002;                   // Bytes to follow
  2824.     
  2825.         $fPrintRwCol $this->_print_headers;     // Boolean flag
  2826.     
  2827.         $header      pack("vv"$record$length);
  2828.         $data        pack("v"$fPrintRwCol);
  2829.         $this->_prepend($header.$data);
  2830.     }
  2831.     
  2832.     /**
  2833.     * Write the PRINTGRIDLINES BIFF record. Must be used in conjunction with the
  2834.     * GRIDSET record.
  2835.     *
  2836.     * @access private
  2837.     */
  2838.     function _storePrintGridlines()
  2839.     {
  2840.         $record      = 0x002b;                    // Record identifier
  2841.         $length      = 0x0002;                    // Bytes to follow
  2842.     
  2843.         $fPrintGrid  $this->_print_gridlines;    // Boolean flag
  2844.     
  2845.         $header      pack("vv"$record$length);
  2846.         $data        pack("v"$fPrintGrid);
  2847.         $this->_prepend($header.$data);
  2848.     }
  2849.     
  2850.     /**
  2851.     * Write the GRIDSET BIFF record. Must be used in conjunction with the
  2852.     * PRINTGRIDLINES record.
  2853.     *
  2854.     * @access private
  2855.     */
  2856.     function _storeGridset()
  2857.     {
  2858.         $record      = 0x0082;                        // Record identifier
  2859.         $length      = 0x0002;                        // Bytes to follow
  2860.     
  2861.         $fGridSet    !($this->_print_gridlines);     // Boolean flag
  2862.     
  2863.         $header      pack("vv",  $record$length);
  2864.         $data        pack("v",   $fGridSet);
  2865.         $this->_prepend($header.$data);
  2866.     }
  2867.  
  2868.     /**
  2869.     * Write the GUTS BIFF record. This is used to configure the gutter margins
  2870.     * where Excel outline symbols are displayed. The visibility of the gutters is
  2871.     * controlled by a flag in WSBOOL.
  2872.     *
  2873.     * @see _storeWsbool()
  2874.     * @access private
  2875.     */
  2876.     function _storeGuts()
  2877.     {
  2878.         $record      = 0x0080;   // Record identifier
  2879.         $length      = 0x0008;   // Bytes to follow
  2880.    
  2881.         $dxRwGut     = 0x0000;   // Size of row gutter
  2882.         $dxColGut    = 0x0000;   // Size of col gutter
  2883.    
  2884.         $row_level   $this->_outline_row_level;
  2885.         $col_level   = 0;
  2886.    
  2887.         // Calculate the maximum column outline level. The equivalent calculation
  2888.         // for the row outline level is carried out in setRow().
  2889.         for ($i=0; $i count($this->_colinfo)$i++)
  2890.         {
  2891.            // Skip cols without outline level info.
  2892.            if (count($col_level>= 6{
  2893.               $col_level max($this->_colinfo[$i][5]$col_level);
  2894.            }
  2895.         }
  2896.    
  2897.         // Set the limits for the outline levels (0 <= x <= 7).
  2898.         $col_level max(0min($col_level7));
  2899.    
  2900.         // The displayed level is one greater than the max outline levels
  2901.         if ($row_level{
  2902.             $row_level++;
  2903.         }
  2904.         if ($col_level{
  2905.             $col_level++;
  2906.         }
  2907.    
  2908.         $header      pack("vv",   $record$length);
  2909.         $data        pack("vvvv"$dxRwGut$dxColGut$row_level$col_level);
  2910.    
  2911.         $this->_prepend($header.$data);
  2912.     }
  2913.  
  2914.  
  2915.     /**
  2916.     * Write the WSBOOL BIFF record, mainly for fit-to-page. Used in conjunction
  2917.     * with the SETUP record.
  2918.     *
  2919.     * @access private
  2920.     */
  2921.     function _storeWsbool()
  2922.     {
  2923.         $record      = 0x0081;   // Record identifier
  2924.         $length      = 0x0002;   // Bytes to follow
  2925.         $grbit       = 0x0000;
  2926.     
  2927.         // The only option that is of interest is the flag for fit to page. So we
  2928.         // set all the options in one go.
  2929.         //
  2930.         /*if ($this->_fit_page) {
  2931.             $grbit = 0x05c1;
  2932.         }
  2933.         else {
  2934.             $grbit = 0x04c1;
  2935.         }*/
  2936.         // Set the option flags
  2937.         $grbit |= 0x0001;                           // Auto page breaks visible
  2938.         if ($this->_outline_style{
  2939.             $grbit |= 0x0020; // Auto outline styles
  2940.         }
  2941.         if ($this->_outline_below{
  2942.             $grbit |= 0x0040; // Outline summary below
  2943.         }
  2944.         if ($this->_outline_right{
  2945.             $grbit |= 0x0080; // Outline summary right
  2946.         }
  2947.         if ($this->_fit_page{
  2948.             $grbit |= 0x0100; // Page setup fit to page
  2949.         }
  2950.         if ($this->_outline_on{
  2951.             $grbit |= 0x0400; // Outline symbols displayed
  2952.         }
  2953.  
  2954.         $header      pack("vv"$record$length);
  2955.         $data        pack("v",  $grbit);
  2956.         $this->_prepend($header.$data);
  2957.     }
  2958.  
  2959.     /**
  2960.     * Write the HORIZONTALPAGEBREAKS BIFF record.
  2961.     *
  2962.     * @access private
  2963.     */
  2964.     function _storeHbreak()
  2965.     {
  2966.         // Return if the user hasn't specified pagebreaks
  2967.         if (empty($this->_hbreaks)) {
  2968.             return;
  2969.         }
  2970.     
  2971.         // Sort and filter array of page breaks
  2972.         $breaks $this->_hbreaks;
  2973.         sort($breaksSORT_NUMERIC);
  2974.         if ($breaks[0== 0// don't use first break if it's 0
  2975.             array_shift($breaks);
  2976.         }
  2977.     
  2978.         $record  = 0x001b;               // Record identifier
  2979.         $cbrk    count($breaks);       // Number of page breaks
  2980.         if ($this->_BIFF_version == 0x0600{
  2981.             $length  = 2 + 6*$cbrk;      // Bytes to follow
  2982.         }
  2983.         else {
  2984.             $length  = 2 + 2*$cbrk;      // Bytes to follow
  2985.         }
  2986.  
  2987.         $header  pack("vv"$record$length);
  2988.         $data    pack("v",  $cbrk);
  2989.     
  2990.         // Append each page break
  2991.         foreach($breaks as $break{
  2992.             if ($this->_BIFF_version == 0x0600{
  2993.                 $data .= pack("vvv"$break0x00000x00ff);
  2994.             }
  2995.             else {
  2996.                 $data .= pack("v"$break);
  2997.             }
  2998.         }
  2999.     
  3000.         $this->_prepend($header.$data);
  3001.     }
  3002.     
  3003.     
  3004.     /**
  3005.     * Write the VERTICALPAGEBREAKS BIFF record.
  3006.     *
  3007.     * @access private
  3008.     */
  3009.     function _storeVbreak()
  3010.     {
  3011.         // Return if the user hasn't specified pagebreaks
  3012.         if (empty($this->_vbreaks)) {
  3013.             return;
  3014.         }
  3015.     
  3016.         // 1000 vertical pagebreaks appears to be an internal Excel 5 limit.
  3017.         // It is slightly higher in Excel 97/200, approx. 1026
  3018.         $breaks array_slice($this->_vbreaks,0,1000);
  3019.     
  3020.         // Sort and filter array of page breaks
  3021.         sort($breaksSORT_NUMERIC);
  3022.         if ($breaks[0== 0// don't use first break if it's 0
  3023.             array_shift($breaks);
  3024.         }
  3025.     
  3026.         $record  = 0x001a;               // Record identifier
  3027.         $cbrk    count($breaks);       // Number of page breaks
  3028.         if ($this->_BIFF_version == 0x0600)
  3029.             $length  = 2 + 6*$cbrk;      // Bytes to follow
  3030.         else {
  3031.             $length  = 2 + 2*$cbrk;      // Bytes to follow
  3032.         }
  3033.     
  3034.         $header  pack("vv",  $record$length);
  3035.         $data    pack("v",   $cbrk);
  3036.     
  3037.         // Append each page break
  3038.         foreach ($breaks as $break{
  3039.             if ($this->_BIFF_version == 0x0600{
  3040.                 $data .= pack("vvv"$break0x00000xffff);
  3041.             }
  3042.             else {
  3043.                 $data .= pack("v"$break);
  3044.             }
  3045.         }
  3046.     
  3047.         $this->_prepend($header.$data);
  3048.     }
  3049.     
  3050.     /**
  3051.     * Set the Biff PROTECT record to indicate that the worksheet is protected.
  3052.     *
  3053.     * @access private
  3054.     */
  3055.     function _storeProtect()
  3056.     {
  3057.         // Exit unless sheet protection has been specified
  3058.         if ($this->_protect == 0{
  3059.             return;
  3060.         }
  3061.     
  3062.         $record      = 0x0012;             // Record identifier
  3063.         $length      = 0x0002;             // Bytes to follow
  3064.     
  3065.         $fLock       $this->_protect;    // Worksheet is protected
  3066.     
  3067.         $header      pack("vv"$record$length);
  3068.         $data        pack("v",  $fLock);
  3069.     
  3070.         $this->_prepend($header.$data);
  3071.     }
  3072.     
  3073.     /**
  3074.     * Write the worksheet PASSWORD record.
  3075.     *
  3076.     * @access private
  3077.     */
  3078.     function _storePassword()
  3079.     {
  3080.         // Exit unless sheet protection and password have been specified
  3081.         if (($this->_protect == 0or (!isset($this->_password))) {
  3082.             return;
  3083.         }
  3084.     
  3085.         $record      = 0x0013;               // Record identifier
  3086.         $length      = 0x0002;               // Bytes to follow
  3087.     
  3088.         $wPassword   $this->_password;     // Encoded password
  3089.     
  3090.         $header      pack("vv"$record$length);
  3091.         $data        pack("v",  $wPassword);
  3092.     
  3093.         $this->_prepend($header.$data);
  3094.     }
  3095.     
  3096.  
  3097.     /**
  3098.     * Insert a 24bit bitmap image in a worksheet.
  3099.     *
  3100.     * @access public
  3101.     * @param integer $row     The row we are going to insert the bitmap into
  3102.     * @param integer $col     The column we are going to insert the bitmap into
  3103.     * @param string  $bitmap  The bitmap filename
  3104.     * @param integer $x       The horizontal position (offset) of the image inside the cell.
  3105.     * @param integer $y       The vertical position (offset) of the image inside the cell.
  3106.     * @param integer $scale_x The horizontal scale
  3107.     * @param integer $scale_y The vertical scale
  3108.     */
  3109.     function insertBitmap($row$col$bitmap$x = 0$y = 0$scale_x = 1$scale_y = 1)
  3110.     {
  3111.         $bitmap_array $this->_processBitmap($bitmap);
  3112.         if ($this->isError($bitmap_array))
  3113.         {
  3114.             $this->writeString($row$col$bitmap_array->getMessage());
  3115.             return;
  3116.         }
  3117.         list($width$height$size$data$bitmap_array//$this->_processBitmap($bitmap);
  3118.     
  3119.         // Scale the frame of the image.
  3120.         $width  *= $scale_x;
  3121.         $height *= $scale_y;
  3122.     
  3123.         // Calculate the vertices of the image and write the OBJ record
  3124.         $this->_positionImage($col$row$x$y$width$height);
  3125.     
  3126.         // Write the IMDATA record to store the bitmap data
  3127.         $record      = 0x007f;
  3128.         $length      = 8 + $size;
  3129.         $cf          = 0x09;
  3130.         $env         = 0x01;
  3131.         $lcb         $size;
  3132.     
  3133.         $header      pack("vvvvV"$record$length$cf$env$lcb);
  3134.         $this->_append($header.$data);
  3135.     }
  3136.     
  3137.     /**
  3138.     * Calculate the vertices that define the position of the image as required by
  3139.     * the OBJ record.
  3140.     *
  3141.     *         +------------+------------+
  3142.     *         |     A      |      B     |
  3143.     *   +-----+------------+------------+
  3144.     *   |     |(x1,y1)     |            |
  3145.     *   |  1  |(A1)._______|______      |
  3146.     *   |     |    |              |     |
  3147.     *   |     |    |              |     |
  3148.     *   +-----+----|    BITMAP    |-----+
  3149.     *   |     |    |              |     |
  3150.     *   |  2  |    |______________.     |
  3151.     *   |     |            |        (B2)|
  3152.     *   |     |            |     (x2,y2)|
  3153.     *   +---- +------------+------------+
  3154.     *
  3155.     * Example of a bitmap that covers some of the area from cell A1 to cell B2.
  3156.     *
  3157.     * Based on the width and height of the bitmap we need to calculate 8 vars:
  3158.     *     $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
  3159.     * The width and height of the cells are also variable and have to be taken into
  3160.     * account.
  3161.     * The values of $col_start and $row_start are passed in from the calling
  3162.     * function. The values of $col_end and $row_end are calculated by subtracting
  3163.     * the width and height of the bitmap from the width and height of the
  3164.     * underlying cells.
  3165.     * The vertices are expressed as a percentage of the underlying cell width as
  3166.     * follows (rhs values are in pixels):
  3167.     *
  3168.     *       x1 = X / W *1024
  3169.     *       y1 = Y / H *256
  3170.     *       x2 = (X-1) / W *1024
  3171.     *       y2 = (Y-1) / H *256
  3172.     *
  3173.     *       Where:  X is distance from the left side of the underlying cell
  3174.     *               Y is distance from the top of the underlying cell
  3175.     *               W is the width of the cell
  3176.     *               H is the height of the cell
  3177.     *
  3178.     * @access private
  3179.     * @note  the SDK incorrectly states that the height should be expressed as a
  3180.     *         percentage of 1024.
  3181.     * @param integer $col_start Col containing upper left corner of object
  3182.     * @param integer $row_start Row containing top left corner of object
  3183.     * @param integer $x1        Distance to left side of object
  3184.     * @param integer $y1        Distance to top of object
  3185.     * @param integer $width     Width of image frame
  3186.     * @param integer $height    Height of image frame
  3187.     */
  3188.     function _positionImage($col_start$row_start$x1$y1$width$height)
  3189.     {
  3190.         // Initialise end cell to the same as the start cell
  3191.         $col_end    $col_start;  // Col containing lower right corner of object
  3192.         $row_end    $row_start;  // Row containing bottom right corner of object
  3193.     
  3194.         // Zero the specified offset if greater than the cell dimensions
  3195.         if ($x1 >= $this->_sizeCol($col_start))
  3196.         {
  3197.             $x1 = 0;
  3198.         }
  3199.         if ($y1 >= $this->_sizeRow($row_start))
  3200.         {
  3201.             $y1 = 0;
  3202.         }
  3203.     
  3204.         $width      $width  $x1 -1;
  3205.         $height     $height $y1 -1;
  3206.     
  3207.         // Subtract the underlying cell widths to find the end cell of the image
  3208.         while ($width >= $this->_sizeCol($col_end)) {
  3209.             $width -= $this->_sizeCol($col_end);
  3210.             $col_end++;
  3211.         }
  3212.     
  3213.         // Subtract the underlying cell heights to find the end cell of the image
  3214.         while ($height >= $this->_sizeRow($row_end)) {
  3215.             $height -= $this->_sizeRow($row_end);
  3216.             $row_end++;
  3217.         }
  3218.     
  3219.         // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
  3220.         // with zero eight or width.
  3221.         //
  3222.         if ($this->_sizeCol($col_start== 0)
  3223.             return;
  3224.         if ($this->_sizeCol($col_end)   == 0)
  3225.             return;
  3226.         if ($this->_sizeRow($row_start== 0)
  3227.             return;
  3228.         if ($this->_sizeRow($row_end)   == 0)
  3229.             return;
  3230.     
  3231.         // Convert the pixel values to the percentage value expected by Excel
  3232.         $x1 $x1     $this->_sizeCol($col_start)   * 1024;
  3233.         $y1 $y1     $this->_sizeRow($row_start)   *  256;
  3234.         $x2 $width  $this->_sizeCol($col_end)     * 1024; // Distance to right side of object
  3235.         $y2 $height $this->_sizeRow($row_end)     *  256; // Distance to bottom of object
  3236.     
  3237.         $this->_storeObjPicture$col_start$x1,
  3238.                                   $row_start$y1,
  3239.                                   $col_end$x2,
  3240.                                   $row_end$y2
  3241.                                 );
  3242.     }
  3243.     
  3244.     /**
  3245.     * Convert the width of a cell from user's units to pixels. By interpolation
  3246.     * the relationship is: y = 7x +5. If the width hasn't been set by the user we
  3247.     * use the default value. If the col is hidden we use a value of zero.
  3248.     *
  3249.     * @access private
  3250.     * @param integer $col The column
  3251.     * @return integer The width in pixels
  3252.     */
  3253.     function _sizeCol($col)
  3254.     {
  3255.         // Look up the cell value to see if it has been changed
  3256.         if (isset($this->col_sizes[$col])) {
  3257.             if ($this->col_sizes[$col== 0{
  3258.                 return(0);
  3259.             }
  3260.             else {
  3261.                 return(floor(7 * $this->col_sizes[$col+ 5));
  3262.             }
  3263.         }
  3264.         else {
  3265.             return(64);
  3266.         }
  3267.     }
  3268.     
  3269.     /**
  3270.     * Convert the height of a cell from user's units to pixels. By interpolation
  3271.     * the relationship is: y = 4/3x. If the height hasn't been set by the user we
  3272.     * use the default value. If the row is hidden we use a value of zero. (Not
  3273.     * possible to hide row yet).
  3274.     *
  3275.     * @access private
  3276.     * @param integer $row The row
  3277.     * @return integer The width in pixels
  3278.     */
  3279.     function _sizeRow($row)
  3280.     {
  3281.         // Look up the cell value to see if it has been changed
  3282.         if (isset($this->_row_sizes[$row])) {
  3283.             if ($this->_row_sizes[$row== 0{
  3284.                 return(0);
  3285.             }
  3286.             else {
  3287.                 return(floor(4/3 * $this->_row_sizes[$row]));
  3288.             }
  3289.         }
  3290.         else {
  3291.             return(17);
  3292.         }
  3293.     }
  3294.     
  3295.     /**
  3296.     * Store the OBJ record that precedes an IMDATA record. This could be generalise
  3297.     * to support other Excel objects.
  3298.     *
  3299.     * @access private
  3300.     * @param integer $colL Column containing upper left corner of object
  3301.     * @param integer $dxL  Distance from left side of cell
  3302.     * @param integer $rwT  Row containing top left corner of object
  3303.     * @param integer $dyT  Distance from top of cell
  3304.     * @param integer $colR Column containing lower right corner of object
  3305.     * @param integer $dxR  Distance from right of cell
  3306.     * @param integer $rwB  Row containing bottom right corner of object
  3307.     * @param integer $dyB  Distance from bottom of cell
  3308.     */
  3309.     function _storeObjPicture($colL,$dxL,$rwT,$dyT,$colR,$dxR,$rwB,$dyB)
  3310.     {
  3311.         $record      = 0x005d;   // Record identifier
  3312.         $length      = 0x003c;   // Bytes to follow
  3313.     
  3314.         $cObj        = 0x0001;   // Count of objects in file (set to 1)
  3315.         $OT          = 0x0008;   // Object type. 8 = Picture
  3316.         $id          = 0x0001;   // Object ID
  3317.         $grbit       = 0x0614;   // Option flags
  3318.     
  3319.         $cbMacro     = 0x0000;   // Length of FMLA structure
  3320.         $Reserved1   = 0x0000;   // Reserved
  3321.         $Reserved2   = 0x0000;   // Reserved
  3322.     
  3323.         $icvBack     = 0x09;     // Background colour
  3324.         $icvFore     = 0x09;     // Foreground colour
  3325.         $fls         = 0x00;     // Fill pattern
  3326.         $fAuto       = 0x00;     // Automatic fill
  3327.         $icv         = 0x08;     // Line colour
  3328.         $lns         = 0xff;     // Line style
  3329.         $lnw         = 0x01;     // Line weight
  3330.         $fAutoB      = 0x00;     // Automatic border
  3331.         $frs         = 0x0000;   // Frame style
  3332.         $cf          = 0x0009;   // Image format, 9 = bitmap
  3333.         $Reserved3   = 0x0000;   // Reserved
  3334.         $cbPictFmla  = 0x0000;   // Length of FMLA structure
  3335.         $Reserved4   = 0x0000;   // Reserved
  3336.         $grbit2      = 0x0001;   // Option flags
  3337.         $Reserved5   = 0x0000;   // Reserved
  3338.     
  3339.     
  3340.         $header      pack("vv"$record$length);
  3341.         $data        pack("V"$cObj);
  3342.         $data       .= pack("v"$OT);
  3343.         $data       .= pack("v"$id);
  3344.         $data       .= pack("v"$grbit);
  3345.         $data       .= pack("v"$colL);
  3346.         $data       .= pack("v"$dxL);
  3347.         $data       .= pack("v"$rwT);
  3348.         $data       .= pack("v"$dyT);
  3349.         $data       .= pack("v"$colR);
  3350.         $data       .= pack("v"$dxR);
  3351.         $data       .= pack("v"$rwB);
  3352.         $data       .= pack("v"$dyB);
  3353.         $data       .= pack("v"$cbMacro);
  3354.         $data       .= pack("V"$Reserved1);
  3355.         $data       .= pack("v"$Reserved2);
  3356.         $data       .= pack("C"$icvBack);
  3357.         $data       .= pack("C"$icvFore);
  3358.         $data       .= pack("C"$fls);
  3359.         $data       .= pack("C"$fAuto);
  3360.         $data       .= pack("C"$icv);
  3361.         $data       .= pack("C"$lns);
  3362.         $data       .= pack("C"$lnw);
  3363.         $data       .= pack("C"$fAutoB);
  3364.         $data       .= pack("v"$frs);
  3365.         $data       .= pack("V"$cf);
  3366.         $data       .= pack("v"$Reserved3);
  3367.         $data       .= pack("v"$cbPictFmla);
  3368.         $data       .= pack("v"$Reserved4);
  3369.         $data       .= pack("v"$grbit2);
  3370.         $data       .= pack("V"$Reserved5);
  3371.     
  3372.         $this->_append($header.$data);
  3373.     }
  3374.     
  3375.     /**
  3376.     * Convert a 24 bit bitmap into the modified internal format used by Windows.
  3377.     * This is described in BITMAPCOREHEADER and BITMAPCOREINFO structures in the
  3378.     * MSDN library.
  3379.     *
  3380.     * @access private
  3381.     * @param string $bitmap The bitmap to process
  3382.     * @return array Array with data and properties of the bitmap
  3383.     */
  3384.     function _processBitmap($bitmap)
  3385.     {
  3386.         // Open file.
  3387.         $bmp_fd @fopen($bitmap,"rb");
  3388.         if (!$bmp_fd{
  3389.             $this->raiseError("Couldn't import $bitmap");
  3390.         }
  3391.             
  3392.         // Slurp the file into a string.
  3393.         $data fread($bmp_fdfilesize($bitmap));
  3394.     
  3395.         // Check that the file is big enough to be a bitmap.
  3396.         if (strlen($data<= 0x36{
  3397.             $this->raiseError("$bitmap doesn't contain enough data.\n");
  3398.         }
  3399.     
  3400.         // The first 2 bytes are used to identify the bitmap.
  3401.         $identity unpack("A2ident"$data);
  3402.         if ($identity['ident'!= "BM"{
  3403.             $this->raiseError("$bitmap doesn't appear to be a valid bitmap image.\n");
  3404.         }
  3405.     
  3406.         // Remove bitmap data: ID.
  3407.         $data substr($data2);
  3408.     
  3409.         // Read and remove the bitmap size. This is more reliable than reading
  3410.         // the data size at offset 0x22.
  3411.         //
  3412.         $size_array   unpack("Vsa"substr($data04));
  3413.         $size   $size_array['sa'];
  3414.         $data   substr($data4);
  3415.         $size  -= 0x36; // Subtract size of bitmap header.
  3416.         $size  += 0x0C; // Add size of BIFF header.
  3417.     
  3418.         // Remove bitmap data: reserved, offset, header length.
  3419.         $data substr($data12);
  3420.     
  3421.         // Read and remove the bitmap width and height. Verify the sizes.
  3422.         $width_and_height unpack("V2"substr($data08));
  3423.         $width  $width_and_height[1];
  3424.         $height $width_and_height[2];
  3425.         $data   substr($data8);
  3426.         if ($width > 0xFFFF
  3427.             $this->raiseError("$bitmap: largest image width supported is 65k.\n");
  3428.         }
  3429.         if ($height > 0xFFFF
  3430.             $this->raiseError("$bitmap: largest image height supported is 65k.\n");
  3431.         }
  3432.     
  3433.         // Read and remove the bitmap planes and bpp data. Verify them.
  3434.         $planes_and_bitcount unpack("v2"substr($data04));
  3435.         $data substr($data4);
  3436.         if ($planes_and_bitcount[2!= 24// Bitcount
  3437.             $this->raiseError("$bitmap isn't a 24bit true color bitmap.\n");
  3438.         }
  3439.         if ($planes_and_bitcount[1!= 1{
  3440.             $this->raiseError("$bitmap: only 1 plane supported in bitmap image.\n");
  3441.         }
  3442.     
  3443.         // Read and remove the bitmap compression. Verify compression.
  3444.         $compression unpack("Vcomp"substr($data04));
  3445.         $data substr($data4);
  3446.       
  3447.         //$compression = 0;
  3448.         if ($compression['comp'!= 0{
  3449.             $this->raiseError("$bitmap: compression not supported in bitmap image.\n");
  3450.         }
  3451.     
  3452.         // Remove bitmap data: data size, hres, vres, colours, imp. colours.
  3453.         $data substr($data20);
  3454.     
  3455.         // Add the BITMAPCOREHEADER data
  3456.         $header  pack("Vvvvv"0x000c$width$height0x010x18);
  3457.         $data    $header $data;
  3458.     
  3459.         return (array($width$height$size$data));
  3460.     }
  3461.     
  3462.     /**
  3463.     * Store the window zoom factor. This should be a reduced fraction but for
  3464.     * simplicity we will store all fractions with a numerator of 100.
  3465.     *
  3466.     * @access private
  3467.     */
  3468.     function _storeZoom()
  3469.     {
  3470.         // If scale is 100 we don't need to write a record
  3471.         if ($this->_zoom == 100{
  3472.             return;
  3473.         }
  3474.     
  3475.         $record      = 0x00A0;               // Record identifier
  3476.         $length      = 0x0004;               // Bytes to follow
  3477.     
  3478.         $header      pack("vv"$record$length);
  3479.         $data        pack("vv"$this->_zoom100);
  3480.         $this->_append($header.$data);
  3481.     }
  3482.  
  3483.     /**
  3484.     * FIXME: add comments
  3485.     */
  3486.     function setValidation($row1$col1$row2$col2&$validator)
  3487.     {
  3488.         $this->_dv[$validator->_getData(.
  3489.                        pack("vvvvv"1$row1$row2$col1$col2);
  3490.     }
  3491.    
  3492.     /**
  3493.     * Store the DVAL and DV records.
  3494.     *
  3495.     * @access private
  3496.     */
  3497.     function _storeDataValidity()
  3498.     {
  3499.         $record      = 0x01b2;      // Record identifier
  3500.         $length      = 0x0012;      // Bytes to follow
  3501.    
  3502.         $grbit       = 0x0002;      // Prompt box at cell, no cached validity data at DV records
  3503.         $horPos      = 0x00000000;  // Horizontal position of prompt box, if fixed position
  3504.         $verPos      = 0x00000000;  // Vertical position of prompt box, if fixed position
  3505.         $objId       = 0xffffffff;  // Object identifier of drop down arrow object, or -1 if not visible
  3506.       
  3507.         $header      pack('vv'$record$length);
  3508.         $data        pack('vVVVV'$grbit$horPos$verPos$objId,
  3509.                                      count($this->_dv));
  3510.         $this->_append($header.$data);
  3511.       
  3512.         $record = 0x01be;              // Record identifier
  3513.         foreach($this->_dv as $dv)
  3514.         {
  3515.             $length strlen($dv);      // Bytes to follow
  3516.             $header pack("vv"$record$length);
  3517.             $this->_append($header.$dv);
  3518.         }
  3519.     }
  3520. }
  3521. ?>

Documentation generated on Mon, 11 Mar 2019 13:52:00 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.