You are here

public static function PHPExcel_Shared_Font::calculateColumnWidth in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/Font.php \PHPExcel_Shared_Font::calculateColumnWidth()

* Calculate an (approximate) OpenXML column width, based on font size and text contained * *

Parameters

PHPExcel_Style_Font $font Font object: * @param PHPExcel_RichText|string $cellText Text to calculate width * @param integer $rotation Rotation angle * @param PHPExcel_Style_Font|NULL $defaultFont Font object * @return integer Column width

1 call to PHPExcel_Shared_Font::calculateColumnWidth()
PHPExcel_Worksheet::calculateColumnWidths in vendor/phpoffice/phpexcel/Classes/PHPExcel/Worksheet.php
Calculate widths for auto-size columns

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/Font.php, line 252

Class

PHPExcel_Shared_Font
PHPExcel_Shared_Font

Code

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 ($cellText instanceof PHPExcel_RichText) {
    $cellText = $cellText
      ->getPlainText();
  }

  // Special case if there are one or more newline characters ("\n")
  if (strpos($cellText, "\n") !== false) {
    $lineTexts = explode("\n", $cellText);
    $lineWidths = array();
    foreach ($lineTexts as $lineText) {
      $lineWidths[] = self::calculateColumnWidth($font, $lineText, $rotation = 0, $defaultFont);
    }
    return max($lineWidths);

    // width of longest line in cell
  }

  // Try to get the exact text width in pixels
  $approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX;
  if (!$approximate) {
    $columnWidthAdjust = ceil(self::getTextWidthPixelsExact('n', $font, 0) * 1.07);
    try {

      // Width of text in pixels excl. padding
      // 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) {
    $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) + $columnWidthAdjust;
  }

  // Convert from pixel width to column width
  $columnWidth = PHPExcel_Shared_Drawing::pixelsToCellDimension($columnWidth, $defaultFont);

  // Return
  return round($columnWidth, 6);
}