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