fixing SST table (long strings). Thanks to Bernd Jaenichen
git-svn-id: https://svn.php.net/repository/pear/packages/Spreadsheet_Excel_Writer/trunk@153858 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
3ec4dce086
commit
14b702dfe3
|
|
@ -165,6 +165,12 @@ class Spreadsheet_Excel_Writer_Workbook extends Spreadsheet_Excel_Writer_BIFFwri
|
||||||
*/
|
*/
|
||||||
var $_tmp_dir;
|
var $_tmp_dir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* number of bytes for sizeinfo of strings
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
var $_string_sizeinfo_size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class constructor
|
* Class constructor
|
||||||
*
|
*
|
||||||
|
|
@ -193,6 +199,7 @@ class Spreadsheet_Excel_Writer_Workbook extends Spreadsheet_Excel_Writer_BIFFwri
|
||||||
$this->_palette = array();
|
$this->_palette = array();
|
||||||
$this->_codepage = 0x04E4; // FIXME: should change for BIFF8
|
$this->_codepage = 0x04E4; // FIXME: should change for BIFF8
|
||||||
$this->_country_code = -1;
|
$this->_country_code = -1;
|
||||||
|
$this->_string_sizeinfo = 3;
|
||||||
|
|
||||||
// Add the default format for hyperlinks
|
// Add the default format for hyperlinks
|
||||||
$this->_url_format =& $this->addFormat(array('color' => 'blue', 'underline' => 1));
|
$this->_url_format =& $this->addFormat(array('color' => 'blue', 'underline' => 1));
|
||||||
|
|
@ -265,7 +272,7 @@ class Spreadsheet_Excel_Writer_Workbook extends Spreadsheet_Excel_Writer_BIFFwri
|
||||||
$version = 0x0600;
|
$version = 0x0600;
|
||||||
$this->_BIFF_version = $version;
|
$this->_BIFF_version = $version;
|
||||||
// change BIFFwriter limit for CONTINUE records
|
// change BIFFwriter limit for CONTINUE records
|
||||||
$this->_limit = 8224;
|
$this->_limit = 8228;
|
||||||
$this->_tmp_format->_BIFF_version = $version;
|
$this->_tmp_format->_BIFF_version = $version;
|
||||||
$this->_url_format->_BIFF_version = $version;
|
$this->_url_format->_BIFF_version = $version;
|
||||||
$this->_parser->_BIFF_version = $version;
|
$this->_parser->_BIFF_version = $version;
|
||||||
|
|
@ -1297,122 +1304,47 @@ class Spreadsheet_Excel_Writer_Workbook extends Spreadsheet_Excel_Writer_BIFFwri
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate
|
* new method for calculating blocksizes of SST-record.
|
||||||
* Handling of the SST continue blocks is complicated by the need to include an
|
* Handling of the SST continue blocks is complicated by the need to include an
|
||||||
* additional continuation byte depending on whether the string is split between
|
* additional continuation byte depending on whether the string is split between
|
||||||
* blocks or whether it starts at the beginning of the block. (There are also
|
* blocks or whether it starts at the beginning of the block. (There are also
|
||||||
* additional complications that will arise later when/if Rich Strings are
|
* additional complications that will arise later when/if Rich Strings are
|
||||||
* supported).
|
* supported).
|
||||||
*
|
*
|
||||||
|
* @author <jaenichen@globalpark.de>
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
function _calculateSharedStringsSizes()
|
function _calculateSharedStringsSizes()
|
||||||
{
|
{
|
||||||
/* Iterate through the strings to calculate the CONTINUE block sizes.
|
$total_offset = 0;
|
||||||
The SST blocks requires a specialised CONTINUE block, so we have to
|
$continue_limit = 8228;
|
||||||
ensure that the maximum data block size is less than the limit used by
|
$header_size = 4;
|
||||||
_add_continue() in BIFFwriter.pm. For simplicity we use the same size
|
|
||||||
for the SST and CONTINUE records:
|
|
||||||
8228 : Maximum Excel97 block size
|
|
||||||
-4 : Length of block header
|
|
||||||
-8 : Length of additional SST header information
|
|
||||||
-8 : Arbitrary number to keep within _add_continue() limit
|
|
||||||
= 8208
|
|
||||||
*/
|
|
||||||
$total_offset = 12;
|
|
||||||
$continue_limit = 8208;
|
|
||||||
$block_length = 0;
|
|
||||||
$written = 0;
|
|
||||||
$this->_block_sizes = array();
|
$this->_block_sizes = array();
|
||||||
$continue = 0;
|
|
||||||
|
|
||||||
foreach (array_keys($this->_str_table) as $string) {
|
// set the SST block header information
|
||||||
$string_length = strlen($string);
|
$buffer = pack("vv", 0x00fc, 0);
|
||||||
|
$buffer .= pack("VV", 0, 0);
|
||||||
|
|
||||||
// Block length is the total length of the strings that will be
|
foreach (array_keys($this->_str_table) as $string)
|
||||||
// written out in a single SST or CONTINUE block.
|
{
|
||||||
$block_length += $string_length;
|
// block is full
|
||||||
|
if ((strlen($buffer) + strlen($string)) > $continue_limit) {
|
||||||
// We can write the string if it doesn't cross a CONTINUE boundary
|
$this->_calculateSSTContinueBlock($string, &$buffer, &$total_offset, $header_size, $continue_limit);
|
||||||
if ($block_length < $continue_limit) {
|
|
||||||
$written += $string_length;
|
|
||||||
$total_offset += $string_length;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// Deal with the cases where the next string to be written will exceed
|
{
|
||||||
// the CONTINUE boundary. If the string is very long it may need to be
|
// add string to block
|
||||||
// written in more than one CONTINUE record.
|
$buffer .= $string;
|
||||||
while ($block_length >= $continue_limit) {
|
|
||||||
|
|
||||||
// We need to avoid the case where a string is continued in the first
|
|
||||||
// n bytes that contain the string header information.
|
|
||||||
$header_length = 3; // Min string + header size -1
|
|
||||||
$space_remaining = $continue_limit - $written - $continue;
|
|
||||||
|
|
||||||
|
|
||||||
/* TODO: Unicode data should only be split on char (2 byte)
|
|
||||||
boundaries. Therefore, in some cases we need to reduce the
|
|
||||||
amount of available
|
|
||||||
*/
|
|
||||||
|
|
||||||
if ($space_remaining > $header_length) {
|
|
||||||
// Write as much as possible of the string in the current block
|
|
||||||
$written += $space_remaining;
|
|
||||||
|
|
||||||
// Reduce the current block length by the amount written
|
|
||||||
$block_length -= $continue_limit + $continue;
|
|
||||||
|
|
||||||
// Store the max size for this block
|
|
||||||
$this->_block_sizes[] = $continue_limit;
|
|
||||||
|
|
||||||
// If the current string was split then the next CONTINUE block
|
|
||||||
// should have the string continue flag (grbit) set unless the
|
|
||||||
// split string fits exactly into the remaining space.
|
|
||||||
if ($block_length > 0) {
|
|
||||||
$continue = 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$continue = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Store the max size for this block
|
|
||||||
$this->_block_sizes[] = $written + $continue;
|
|
||||||
|
|
||||||
// Not enough space to start the string in the current block
|
|
||||||
$block_length -= $continue_limit - $space_remaining - $continue;
|
|
||||||
$continue = 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the string (or substr) is small enough we can write it in the
|
|
||||||
// new CONTINUE block. Else, go through the loop again to write it in
|
|
||||||
// one or more CONTINUE blocks
|
|
||||||
if ($block_length < $continue_limit) {
|
|
||||||
$written = $block_length;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$written = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// save last block
|
||||||
|
if (strlen($buffer) > 0)
|
||||||
|
{
|
||||||
|
$this->_block_sizes[] = (strlen($buffer) - $header_size);
|
||||||
|
$total_offset += strlen($buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the max size for the last block unless it is empty
|
|
||||||
if ($written + $continue) {
|
|
||||||
$this->_block_sizes[] = $written + $continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Calculate the total length of the SST and associated CONTINUEs (if any).
|
|
||||||
The SST record will have a length even if it contains no strings.
|
|
||||||
This length is required to set the offsets in the BOUNDSHEET records since
|
|
||||||
they must be written before the SST records
|
|
||||||
*/
|
|
||||||
if (!empty($this->_block_sizes)) {
|
|
||||||
$total_offset += (count($this->_block_sizes) - 1) * 4; // add CONTINUE headers
|
|
||||||
}
|
|
||||||
return $total_offset;
|
return $total_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1425,116 +1357,122 @@ class Spreadsheet_Excel_Writer_Workbook extends Spreadsheet_Excel_Writer_BIFFwri
|
||||||
* access to SST. However, despite the documentation it doesn't seem to be
|
* access to SST. However, despite the documentation it doesn't seem to be
|
||||||
* required so we will ignore it.
|
* required so we will ignore it.
|
||||||
*
|
*
|
||||||
|
* @author <jaenichen@globalpark.de>
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
/* FIXME: update _calcSheetOffsets() when updating this method */
|
|
||||||
function _storeSharedStringsTable()
|
function _storeSharedStringsTable()
|
||||||
{
|
{
|
||||||
|
$continue_limit = 8228;
|
||||||
|
|
||||||
$record = 0x00fc; // Record identifier
|
$record = 0x00fc; // Record identifier
|
||||||
$length = 8 + array_sum($this->_block_sizes); // Number of bytes to follow
|
$length = array_shift($this->_block_sizes);
|
||||||
|
|
||||||
// Write the SST block header information
|
// Write the SST block header information
|
||||||
$header = pack("vv", $record, $length);
|
$buffer = pack("vv", $record, $length);
|
||||||
$data = pack("VV", $this->_str_total, $this->_str_unique);
|
$buffer .= pack("VV", $this->_str_total, $this->_str_total);
|
||||||
$this->_append($header.$data);
|
|
||||||
|
|
||||||
|
foreach (array_keys($this->_str_table) as $string)
|
||||||
// Iterate through the strings to calculate the CONTINUE block sizes
|
{
|
||||||
$continue_limit = 8208;
|
// block is full
|
||||||
$block_length = 0;
|
if ((strlen($buffer) + strlen($string)) > $continue_limit) {
|
||||||
$written = 0;
|
$this->_storeSSTContinueBlock($string, &$buffer, $continue_limit);
|
||||||
$continue = 0;
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
/* TODO: not good for performance */
|
// add string to block
|
||||||
foreach (array_keys($this->_str_table) as $string) {
|
$buffer .= $string;
|
||||||
|
}
|
||||||
$string_length = strlen($string);
|
|
||||||
$encoding = 0; // assume there are no Unicode strings
|
|
||||||
$split_string = 0;
|
|
||||||
|
|
||||||
// Block length is the total length of the strings that will be
|
|
||||||
// written out in a single SST or CONTINUE block.
|
|
||||||
//
|
|
||||||
$block_length += $string_length;
|
|
||||||
|
|
||||||
|
|
||||||
// We can write the string if it doesn't cross a CONTINUE boundary
|
|
||||||
if ($block_length < $continue_limit) {
|
|
||||||
$this->_append($string);
|
|
||||||
$written += $string_length;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deal with the cases where the next string to be written will exceed
|
// save last block
|
||||||
// the CONTINUE boundary. If the string is very long it may need to be
|
if (strlen($buffer) > 0)
|
||||||
// written in more than one CONTINUE record.
|
{
|
||||||
//
|
$this->_append($buffer);
|
||||||
while ($block_length >= $continue_limit) {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// We need to avoid the case where a string is continued in the first
|
/**
|
||||||
// n bytes that contain the string header information.
|
* method for calculation of SST-record Continue-Blocks.
|
||||||
//
|
*
|
||||||
$header_length = 3; // Min string + header size -1
|
* @author <jaenichen@globalpark.de>
|
||||||
$space_remaining = $continue_limit - $written - $continue;
|
* @access private
|
||||||
|
*/
|
||||||
|
function _calculateSSTContinueBlock($string, &$buffer, &$total_offset, $header_size, $continue_limit)
|
||||||
|
{
|
||||||
|
// calculate remaining space in block
|
||||||
|
$space = $continue_limit - strlen($buffer);
|
||||||
|
|
||||||
|
// if there's space left in block to store at least the sizeinfo of actual string
|
||||||
|
if ($space > $this->_string_sizeinfo)
|
||||||
|
{
|
||||||
|
// split string
|
||||||
|
$tmp = substr($string, 0, $space);
|
||||||
|
$buffer .= $tmp;
|
||||||
|
// save rest of string
|
||||||
|
$string = substr($string, $space);
|
||||||
|
}
|
||||||
|
|
||||||
// Unicode data should only be split on char (2 byte) boundaries.
|
// save blocksize decremented by headersize
|
||||||
// Therefore, in some cases we need to reduce the amount of available
|
$this->_block_sizes[] = (strlen($buffer) - $header_size);
|
||||||
|
// save full blocksize
|
||||||
|
$total_offset += strlen($buffer);
|
||||||
|
|
||||||
if ($space_remaining > $header_length) {
|
// set CONTINUE header
|
||||||
// Write as much as possible of the string in the current block
|
$buffer = pack("vv", 0x003c, 0);
|
||||||
$tmp = substr($string, 0, $space_remaining);
|
// set optional flagbyte of CONTINUE record for splittet strings
|
||||||
$this->_append($tmp);
|
if ($space > $this->_string_sizeinfo) {
|
||||||
|
$buffer .= pack('C', 0);
|
||||||
|
}
|
||||||
|
|
||||||
// The remainder will be written in the next block(s)
|
// block is full again
|
||||||
$string = substr($string, $space_remaining);
|
if ((strlen($buffer) + strlen($string)) > $continue_limit) {
|
||||||
|
$this->_calculateSSTContinueBlock($string, &$buffer, &$total_offset, $header_size, $continue_limit);
|
||||||
// Reduce the current block length by the amount written
|
|
||||||
$block_length -= $continue_limit - $continue;
|
|
||||||
|
|
||||||
// If the current string was split then the next CONTINUE block
|
|
||||||
// should have the string continue flag (grbit) set unless the
|
|
||||||
// split string fits exactly into the remaining space.
|
|
||||||
//
|
|
||||||
if ($block_length > 0) {
|
|
||||||
$continue = 1;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$continue = 0;
|
$buffer .= $string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// Not enough space to start the string in the current block
|
|
||||||
$block_length -= $continue_limit - $space_remaining - $continue;
|
|
||||||
$continue = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write the CONTINUE block header
|
/**
|
||||||
if (!empty($this->_block_sizes)) {
|
* method for storing of SST-record Continue-Blocks.
|
||||||
|
*
|
||||||
|
* @author <jaenichen@globalpark.de>
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
|
||||||
|
function _storeSSTContinueBlock($string, &$buffer, $continue_limit)
|
||||||
|
{
|
||||||
|
// calculate remaining space in block
|
||||||
|
$space = $continue_limit - strlen($buffer);
|
||||||
|
|
||||||
|
// if there's space left in block to store at least the sizeinfo of actual string
|
||||||
|
if ($space > $this->_string_sizeinfo)
|
||||||
|
{
|
||||||
|
// split string
|
||||||
|
$tmp = substr($string, 0, $space);
|
||||||
|
$buffer .= $tmp;
|
||||||
|
// save rest of string including optional flagbyte of CONTINUE record
|
||||||
|
$string = substr($string, $space);
|
||||||
|
}
|
||||||
|
|
||||||
|
// save block
|
||||||
|
$this->_append($buffer);
|
||||||
|
|
||||||
|
// save CONTINUE header
|
||||||
$record = 0x003C;
|
$record = 0x003C;
|
||||||
$length = array_pop($this->_block_sizes);
|
$length = array_shift($this->_block_sizes);
|
||||||
|
$buffer = pack('vv', $record, $length);
|
||||||
$header = pack('vv', $record, $length);
|
// set optional flagbyte of CONTINUE record for splittet strings
|
||||||
if ($continue) {
|
if ($space > $this->_string_sizeinfo) {
|
||||||
$header .= pack('C', $encoding);
|
$buffer .= pack('C', 0);
|
||||||
}
|
|
||||||
$this->_append($header);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the string (or substr) is small enough we can write it in the
|
// block is full again
|
||||||
// new CONTINUE block. Else, go through the loop again to write it in
|
if ((strlen($buffer) + strlen($string)) > $continue_limit) {
|
||||||
// one or more CONTINUE blocks
|
$this->_storeSSTContinueBlock($string, &$buffer, $continue_limit);
|
||||||
//
|
|
||||||
if ($block_length < $continue_limit) {
|
|
||||||
$this->_append($string);
|
|
||||||
|
|
||||||
$written = $block_length;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$written = 0;
|
$buffer .= $string;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue