Skip to content

Commit

Permalink
Don't cache column width adjustment, because it should be based on th…
Browse files Browse the repository at this point in the history
…e font size being used for a cell, and so should be dynamic
  • Loading branch information
MarkBaker committed Jul 3, 2014
1 parent ddec5e9 commit 59ef7c3
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions Classes/PHPExcel/Shared/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ public static function setAutoSizeMethod($pValue = self::AUTOSIZE_METHOD_APPROX)
if (!in_array($pValue,self::$_autoSizeMethods)) {
return FALSE;
}

self::$autoSizeMethod = $pValue;

return TRUE;
Expand Down Expand Up @@ -241,8 +240,6 @@ public static function getTrueTypeFontPath()
return self::$trueTypeFontPath;
}

private static $columnWidthAdjust;

/**
* Calculate an (approximate) OpenXML column width, based on font size and text contained
*
Expand Down Expand Up @@ -271,24 +268,21 @@ public static function calculateColumnWidth(PHPExcel_Style_Font $font, $cellText
// 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);
}
$columnWidthAdjust = ceil(self::getTextWidthPixelsExact('n', $font, 0) * 1.07);
try {
// Width of text in pixels excl. padding
$columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation) + self::$columnWidthAdjust;
// and addition because Excel adds some padding, just use approx width of 'n' glyph
$columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation) + $columnWidthAdjust;
} catch (PHPExcel_Exception $e) {
$approximate == true;
}
}

if ($approximate) {
if (is_null(self::$columnWidthAdjust)) {
self::$columnWidthAdjust = self::getTextWidthPixelsApprox('n', $font, 0);
}
$columnWidthAdjust = self::getTextWidthPixelsApprox('n', $font, 0);
// Width of text in pixels excl. padding, approximation
// and addition because Excel adds some padding, just use approx width of 'n' glyph
$columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation) + self::$columnWidthAdjust;
$columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation) + $columnWidthAdjust;
}

// Convert from pixel width to column width
Expand Down

0 comments on commit 59ef7c3

Please sign in to comment.