home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Worksheet.php < prev    next >
Encoding:
PHP Script  |  2003-12-16  |  108.5 KB  |  3,490 lines

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