You are here

public static function PHPExcel_Shared_Font::getTextWidthPixelsApprox 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::getTextWidthPixelsApprox()

* Get approximate width in pixels for a string of text in a certain font at a certain rotation angle * *

Parameters

string $columnText: * @param PHPExcel_Style_Font $font * @param int $rotation * @return int Text width in pixels (no padding added)

1 call to PHPExcel_Shared_Font::getTextWidthPixelsApprox()
PHPExcel_Shared_Font::calculateColumnWidth in vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/Font.php
* Calculate an (approximate) OpenXML column width, based on font size and text contained * *

File

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

Class

PHPExcel_Shared_Font
PHPExcel_Shared_Font

Code

public static function getTextWidthPixelsApprox($columnText, PHPExcel_Style_Font $font = null, $rotation = 0) {
  $fontName = $font
    ->getName();
  $fontSize = $font
    ->getSize();

  // Calculate column width in pixels. We assume fixed glyph width. Result varies with font name and size.
  switch ($fontName) {
    case 'Calibri':

      // value 8.26 was found via interpolation by inspecting real Excel files with Calibri 11 font.
      $columnWidth = (int) (8.26 * PHPExcel_Shared_String::CountCharacters($columnText));
      $columnWidth = $columnWidth * $fontSize / 11;

      // extrapolate from font size
      break;
    case 'Arial':

      // value 7 was found via interpolation by inspecting real Excel files with Arial 10 font.
      //				$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
      break;
    case 'Verdana':

      // value 8 was found via interpolation by inspecting real Excel files with Verdana 10 font.
      $columnWidth = (int) (8 * PHPExcel_Shared_String::CountCharacters($columnText));
      $columnWidth = $columnWidth * $fontSize / 10;

      // extrapolate from font size
      break;
    default:

      // just assume Calibri
      $columnWidth = (int) (8.26 * PHPExcel_Shared_String::CountCharacters($columnText));
      $columnWidth = $columnWidth * $fontSize / 11;

      // extrapolate from font size
      break;
  }

  // Calculate approximate rotated column width
  if ($rotation !== 0) {
    if ($rotation == -165) {

      // stacked text
      $columnWidth = 4;

      // approximation
    }
    else {

      // rotated text
      $columnWidth = $columnWidth * cos(deg2rad($rotation)) + $fontSize * abs(sin(deg2rad($rotation))) / 5;

      // approximation
    }
  }

  // pixel width is an integer
  return (int) $columnWidth;
}