Improve speed of calculation for autosize columns to reduce save time

This commit is contained in:
MarkBaker 2014-07-03 00:43:29 +01:00
parent 78a065754d
commit ddec5e9706

View File

@ -241,6 +241,8 @@ class PHPExcel_Shared_Font
return self::$trueTypeFontPath; return self::$trueTypeFontPath;
} }
private static $columnWidthAdjust;
/** /**
* Calculate an (approximate) OpenXML column width, based on font size and text contained * Calculate an (approximate) OpenXML column width, based on font size and text contained
* *
@ -251,7 +253,6 @@ class PHPExcel_Shared_Font
* @return integer Column width * @return integer Column width
*/ */
public static function calculateColumnWidth(PHPExcel_Style_Font $font, $cellText = '', $rotation = 0, PHPExcel_Style_Font $defaultFont = null) { public static function calculateColumnWidth(PHPExcel_Style_Font $font, $cellText = '', $rotation = 0, PHPExcel_Style_Font $defaultFont = null) {
// If it is rich text, use plain text // If it is rich text, use plain text
if ($cellText instanceof PHPExcel_RichText) { if ($cellText instanceof PHPExcel_RichText) {
$cellText = $cellText->getPlainText(); $cellText = $cellText->getPlainText();
@ -268,24 +269,26 @@ class PHPExcel_Shared_Font
} }
// Try to get the exact text width in pixels // Try to get the exact text width in pixels
$approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX;
if (!$approximate) {
if (is_null(self::$columnWidthAdjust)) {
self::$columnWidthAdjust = ceil(self::getTextWidthPixelsExact('0', $font, 0) * 1.07);
}
try { try {
// If autosize method is set to 'approx', use approximation // Width of text in pixels excl. padding
if (self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX) { $columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation) + self::$columnWidthAdjust;
throw new PHPExcel_Exception('AutoSize method is set to approx'); } catch (PHPExcel_Exception $e) {
$approximate == true;
}
} }
// Width of text in pixels excl. padding if ($approximate) {
$columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation); if (is_null(self::$columnWidthAdjust)) {
self::$columnWidthAdjust = self::getTextWidthPixelsApprox('n', $font, 0);
// Excel adds some padding, use 1.07 of the width of an 'n' glyph }
$columnWidth += ceil(self::getTextWidthPixelsExact('0', $font, 0) * 1.07); // pixels incl. padding
} catch (PHPExcel_Exception $e) {
// Width of text in pixels excl. padding, approximation // Width of text in pixels excl. padding, approximation
$columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation); // and addition because Excel adds some padding, just use approx width of 'n' glyph
$columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation) + self::$columnWidthAdjust;
// Excel adds some padding, just use approx width of 'n' glyph
$columnWidth += self::getTextWidthPixelsApprox('n', $font, 0);
} }
// Convert from pixel width to column width // Convert from pixel width to column width
@ -316,13 +319,13 @@ class PHPExcel_Shared_Font
// Get corners positions // Get corners positions
$lowerLeftCornerX = $textBox[0]; $lowerLeftCornerX = $textBox[0];
$lowerLeftCornerY = $textBox[1]; // $lowerLeftCornerY = $textBox[1];
$lowerRightCornerX = $textBox[2]; $lowerRightCornerX = $textBox[2];
$lowerRightCornerY = $textBox[3]; // $lowerRightCornerY = $textBox[3];
$upperRightCornerX = $textBox[4]; $upperRightCornerX = $textBox[4];
$upperRightCornerY = $textBox[5]; // $upperRightCornerY = $textBox[5];
$upperLeftCornerX = $textBox[6]; $upperLeftCornerX = $textBox[6];
$upperLeftCornerY = $textBox[7]; // $upperLeftCornerY = $textBox[7];
// Consider the rotation when calculating the width // Consider the rotation when calculating the width
$textWidth = max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX); $textWidth = max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX);
@ -353,7 +356,9 @@ class PHPExcel_Shared_Font
case 'Arial': case 'Arial':
// value 7 was found via interpolation by inspecting real Excel files with Arial 10 font. // value 7 was found via interpolation by inspecting real Excel files with Arial 10 font.
$columnWidth = (int) (7 * PHPExcel_Shared_String::CountCharacters($columnText)); // $columnWidth = (int) (7 * PHPExcel_Shared_String::CountCharacters($columnText));
// value 8 was set because of experience in different exports at Arial 10 font.
$columnWidth = (int) (8 * PHPExcel_Shared_String::CountCharacters($columnText));
$columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size
break; break;
@ -383,8 +388,7 @@ class PHPExcel_Shared_Font
} }
// pixel width is an integer // pixel width is an integer
$columnWidth = (int) $columnWidth; return (int) $columnWidth;
return $columnWidth;
} }
/** /**