Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Shared;
   4  
   5  use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
   6  use PhpOffice\PhpSpreadsheet\RichText\RichText;
   7  
   8  class Font
   9  {
  10      // Methods for resolving autosize value
  11      const AUTOSIZE_METHOD_APPROX = 'approx';
  12      const AUTOSIZE_METHOD_EXACT = 'exact';
  13  
  14      private static $autoSizeMethods = [
  15          self::AUTOSIZE_METHOD_APPROX,
  16          self::AUTOSIZE_METHOD_EXACT,
  17      ];
  18  
  19      /** Character set codes used by BIFF5-8 in Font records */
  20      const CHARSET_ANSI_LATIN = 0x00;
  21      const CHARSET_SYSTEM_DEFAULT = 0x01;
  22      const CHARSET_SYMBOL = 0x02;
  23      const CHARSET_APPLE_ROMAN = 0x4D;
  24      const CHARSET_ANSI_JAPANESE_SHIFTJIS = 0x80;
  25      const CHARSET_ANSI_KOREAN_HANGUL = 0x81;
  26      const CHARSET_ANSI_KOREAN_JOHAB = 0x82;
  27      const CHARSET_ANSI_CHINESE_SIMIPLIFIED = 0x86; //    gb2312
  28      const CHARSET_ANSI_CHINESE_TRADITIONAL = 0x88; //    big5
  29      const CHARSET_ANSI_GREEK = 0xA1;
  30      const CHARSET_ANSI_TURKISH = 0xA2;
  31      const CHARSET_ANSI_VIETNAMESE = 0xA3;
  32      const CHARSET_ANSI_HEBREW = 0xB1;
  33      const CHARSET_ANSI_ARABIC = 0xB2;
  34      const CHARSET_ANSI_BALTIC = 0xBA;
  35      const CHARSET_ANSI_CYRILLIC = 0xCC;
  36      const CHARSET_ANSI_THAI = 0xDD;
  37      const CHARSET_ANSI_LATIN_II = 0xEE;
  38      const CHARSET_OEM_LATIN_I = 0xFF;
  39  
  40      //  XXX: Constants created!
  41      /** Font filenames */
  42      const ARIAL = 'arial.ttf';
  43      const ARIAL_BOLD = 'arialbd.ttf';
  44      const ARIAL_ITALIC = 'ariali.ttf';
  45      const ARIAL_BOLD_ITALIC = 'arialbi.ttf';
  46  
  47      const CALIBRI = 'CALIBRI.TTF';
  48      const CALIBRI_BOLD = 'CALIBRIB.TTF';
  49      const CALIBRI_ITALIC = 'CALIBRII.TTF';
  50      const CALIBRI_BOLD_ITALIC = 'CALIBRIZ.TTF';
  51  
  52      const COMIC_SANS_MS = 'comic.ttf';
  53      const COMIC_SANS_MS_BOLD = 'comicbd.ttf';
  54  
  55      const COURIER_NEW = 'cour.ttf';
  56      const COURIER_NEW_BOLD = 'courbd.ttf';
  57      const COURIER_NEW_ITALIC = 'couri.ttf';
  58      const COURIER_NEW_BOLD_ITALIC = 'courbi.ttf';
  59  
  60      const GEORGIA = 'georgia.ttf';
  61      const GEORGIA_BOLD = 'georgiab.ttf';
  62      const GEORGIA_ITALIC = 'georgiai.ttf';
  63      const GEORGIA_BOLD_ITALIC = 'georgiaz.ttf';
  64  
  65      const IMPACT = 'impact.ttf';
  66  
  67      const LIBERATION_SANS = 'LiberationSans-Regular.ttf';
  68      const LIBERATION_SANS_BOLD = 'LiberationSans-Bold.ttf';
  69      const LIBERATION_SANS_ITALIC = 'LiberationSans-Italic.ttf';
  70      const LIBERATION_SANS_BOLD_ITALIC = 'LiberationSans-BoldItalic.ttf';
  71  
  72      const LUCIDA_CONSOLE = 'lucon.ttf';
  73      const LUCIDA_SANS_UNICODE = 'l_10646.ttf';
  74  
  75      const MICROSOFT_SANS_SERIF = 'micross.ttf';
  76  
  77      const PALATINO_LINOTYPE = 'pala.ttf';
  78      const PALATINO_LINOTYPE_BOLD = 'palab.ttf';
  79      const PALATINO_LINOTYPE_ITALIC = 'palai.ttf';
  80      const PALATINO_LINOTYPE_BOLD_ITALIC = 'palabi.ttf';
  81  
  82      const SYMBOL = 'symbol.ttf';
  83  
  84      const TAHOMA = 'tahoma.ttf';
  85      const TAHOMA_BOLD = 'tahomabd.ttf';
  86  
  87      const TIMES_NEW_ROMAN = 'times.ttf';
  88      const TIMES_NEW_ROMAN_BOLD = 'timesbd.ttf';
  89      const TIMES_NEW_ROMAN_ITALIC = 'timesi.ttf';
  90      const TIMES_NEW_ROMAN_BOLD_ITALIC = 'timesbi.ttf';
  91  
  92      const TREBUCHET_MS = 'trebuc.ttf';
  93      const TREBUCHET_MS_BOLD = 'trebucbd.ttf';
  94      const TREBUCHET_MS_ITALIC = 'trebucit.ttf';
  95      const TREBUCHET_MS_BOLD_ITALIC = 'trebucbi.ttf';
  96  
  97      const VERDANA = 'verdana.ttf';
  98      const VERDANA_BOLD = 'verdanab.ttf';
  99      const VERDANA_ITALIC = 'verdanai.ttf';
 100      const VERDANA_BOLD_ITALIC = 'verdanaz.ttf';
 101  
 102      /**
 103       * AutoSize method.
 104       *
 105       * @var string
 106       */
 107      private static $autoSizeMethod = self::AUTOSIZE_METHOD_APPROX;
 108  
 109      /**
 110       * Path to folder containing TrueType font .ttf files.
 111       *
 112       * @var string
 113       */
 114      private static $trueTypeFontPath = null;
 115  
 116      /**
 117       * How wide is a default column for a given default font and size?
 118       * Empirical data found by inspecting real Excel files and reading off the pixel width
 119       * in Microsoft Office Excel 2007.
 120       *
 121       * @var array
 122       */
 123      public static $defaultColumnWidths = [
 124          'Arial' => [
 125              1 => ['px' => 24, 'width' => 12.00000000],
 126              2 => ['px' => 24, 'width' => 12.00000000],
 127              3 => ['px' => 32, 'width' => 10.66406250],
 128              4 => ['px' => 32, 'width' => 10.66406250],
 129              5 => ['px' => 40, 'width' => 10.00000000],
 130              6 => ['px' => 48, 'width' => 9.59765625],
 131              7 => ['px' => 48, 'width' => 9.59765625],
 132              8 => ['px' => 56, 'width' => 9.33203125],
 133              9 => ['px' => 64, 'width' => 9.14062500],
 134              10 => ['px' => 64, 'width' => 9.14062500],
 135          ],
 136          'Calibri' => [
 137              1 => ['px' => 24, 'width' => 12.00000000],
 138              2 => ['px' => 24, 'width' => 12.00000000],
 139              3 => ['px' => 32, 'width' => 10.66406250],
 140              4 => ['px' => 32, 'width' => 10.66406250],
 141              5 => ['px' => 40, 'width' => 10.00000000],
 142              6 => ['px' => 48, 'width' => 9.59765625],
 143              7 => ['px' => 48, 'width' => 9.59765625],
 144              8 => ['px' => 56, 'width' => 9.33203125],
 145              9 => ['px' => 56, 'width' => 9.33203125],
 146              10 => ['px' => 64, 'width' => 9.14062500],
 147              11 => ['px' => 64, 'width' => 9.14062500],
 148          ],
 149          'Verdana' => [
 150              1 => ['px' => 24, 'width' => 12.00000000],
 151              2 => ['px' => 24, 'width' => 12.00000000],
 152              3 => ['px' => 32, 'width' => 10.66406250],
 153              4 => ['px' => 32, 'width' => 10.66406250],
 154              5 => ['px' => 40, 'width' => 10.00000000],
 155              6 => ['px' => 48, 'width' => 9.59765625],
 156              7 => ['px' => 48, 'width' => 9.59765625],
 157              8 => ['px' => 64, 'width' => 9.14062500],
 158              9 => ['px' => 72, 'width' => 9.00000000],
 159              10 => ['px' => 72, 'width' => 9.00000000],
 160          ],
 161      ];
 162  
 163      /**
 164       * Set autoSize method.
 165       *
 166       * @param string $pValue see self::AUTOSIZE_METHOD_*
 167       *
 168       * @return bool Success or failure
 169       */
 170      public static function setAutoSizeMethod($pValue)
 171      {
 172          if (!in_array($pValue, self::$autoSizeMethods)) {
 173              return false;
 174          }
 175          self::$autoSizeMethod = $pValue;
 176  
 177          return true;
 178      }
 179  
 180      /**
 181       * Get autoSize method.
 182       *
 183       * @return string
 184       */
 185      public static function getAutoSizeMethod()
 186      {
 187          return self::$autoSizeMethod;
 188      }
 189  
 190      /**
 191       * Set the path to the folder containing .ttf files. There should be a trailing slash.
 192       * Typical locations on variout some platforms:
 193       *    <ul>
 194       *        <li>C:/Windows/Fonts/</li>
 195       *        <li>/usr/share/fonts/truetype/</li>
 196       *        <li>~/.fonts/</li>
 197       * </ul>.
 198       *
 199       * @param string $pValue
 200       */
 201      public static function setTrueTypeFontPath($pValue): void
 202      {
 203          self::$trueTypeFontPath = $pValue;
 204      }
 205  
 206      /**
 207       * Get the path to the folder containing .ttf files.
 208       *
 209       * @return string
 210       */
 211      public static function getTrueTypeFontPath()
 212      {
 213          return self::$trueTypeFontPath;
 214      }
 215  
 216      /**
 217       * Calculate an (approximate) OpenXML column width, based on font size and text contained.
 218       *
 219       * @param \PhpOffice\PhpSpreadsheet\Style\Font $font Font object
 220       * @param RichText|string $cellText Text to calculate width
 221       * @param int $rotation Rotation angle
 222       * @param null|\PhpOffice\PhpSpreadsheet\Style\Font $defaultFont Font object
 223       *
 224       * @return int Column width
 225       */
 226      public static function calculateColumnWidth(\PhpOffice\PhpSpreadsheet\Style\Font $font, $cellText = '', $rotation = 0, ?\PhpOffice\PhpSpreadsheet\Style\Font $defaultFont = null)
 227      {
 228          // If it is rich text, use plain text
 229          if ($cellText instanceof RichText) {
 230              $cellText = $cellText->getPlainText();
 231          }
 232  
 233          // Special case if there are one or more newline characters ("\n")
 234          if (strpos($cellText, "\n") !== false) {
 235              $lineTexts = explode("\n", $cellText);
 236              $lineWidths = [];
 237              foreach ($lineTexts as $lineText) {
 238                  $lineWidths[] = self::calculateColumnWidth($font, $lineText, $rotation = 0, $defaultFont);
 239              }
 240  
 241              return max($lineWidths); // width of longest line in cell
 242          }
 243  
 244          // Try to get the exact text width in pixels
 245          $approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX;
 246          if (!$approximate) {
 247              $columnWidthAdjust = ceil(self::getTextWidthPixelsExact('n', $font, 0) * 1.07);
 248  
 249              try {
 250                  // Width of text in pixels excl. padding
 251                  // and addition because Excel adds some padding, just use approx width of 'n' glyph
 252                  $columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation) + $columnWidthAdjust;
 253              } catch (PhpSpreadsheetException $e) {
 254                  $approximate = true;
 255              }
 256          }
 257  
 258          if ($approximate) {
 259              $columnWidthAdjust = self::getTextWidthPixelsApprox('n', $font, 0);
 260              // Width of text in pixels excl. padding, approximation
 261              // and addition because Excel adds some padding, just use approx width of 'n' glyph
 262              $columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation) + $columnWidthAdjust;
 263          }
 264  
 265          // Convert from pixel width to column width
 266          $columnWidth = Drawing::pixelsToCellDimension($columnWidth, $defaultFont);
 267  
 268          // Return
 269          return round($columnWidth, 6);
 270      }
 271  
 272      /**
 273       * Get GD text width in pixels for a string of text in a certain font at a certain rotation angle.
 274       *
 275       * @param string $text
 276       * @param \PhpOffice\PhpSpreadsheet\Style\Font
 277       * @param int $rotation
 278       *
 279       * @return int
 280       */
 281      public static function getTextWidthPixelsExact($text, \PhpOffice\PhpSpreadsheet\Style\Font $font, $rotation = 0)
 282      {
 283          if (!function_exists('imagettfbbox')) {
 284              throw new PhpSpreadsheetException('GD library needs to be enabled');
 285          }
 286  
 287          // font size should really be supplied in pixels in GD2,
 288          // but since GD2 seems to assume 72dpi, pixels and points are the same
 289          $fontFile = self::getTrueTypeFontFileFromFont($font);
 290          $textBox = imagettfbbox($font->getSize(), $rotation, $fontFile, $text);
 291  
 292          // Get corners positions
 293          $lowerLeftCornerX = $textBox[0];
 294          $lowerRightCornerX = $textBox[2];
 295          $upperRightCornerX = $textBox[4];
 296          $upperLeftCornerX = $textBox[6];
 297  
 298          // Consider the rotation when calculating the width
 299          return max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX);
 300      }
 301  
 302      /**
 303       * Get approximate width in pixels for a string of text in a certain font at a certain rotation angle.
 304       *
 305       * @param string $columnText
 306       * @param int $rotation
 307       *
 308       * @return int Text width in pixels (no padding added)
 309       */
 310      public static function getTextWidthPixelsApprox($columnText, \PhpOffice\PhpSpreadsheet\Style\Font $font, $rotation = 0)
 311      {
 312          $fontName = $font->getName();
 313          $fontSize = $font->getSize();
 314  
 315          // Calculate column width in pixels. We assume fixed glyph width. Result varies with font name and size.
 316          switch ($fontName) {
 317              case 'Calibri':
 318                  // value 8.26 was found via interpolation by inspecting real Excel files with Calibri 11 font.
 319                  $columnWidth = (int) (8.26 * StringHelper::countCharacters($columnText));
 320                  $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size
 321  
 322                  break;
 323              case 'Arial':
 324                  // value 8 was set because of experience in different exports at Arial 10 font.
 325                  $columnWidth = (int) (8 * StringHelper::countCharacters($columnText));
 326                  $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size
 327  
 328                  break;
 329              case 'Verdana':
 330                  // value 8 was found via interpolation by inspecting real Excel files with Verdana 10 font.
 331                  $columnWidth = (int) (8 * StringHelper::countCharacters($columnText));
 332                  $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size
 333  
 334                  break;
 335              default:
 336                  // just assume Calibri
 337                  $columnWidth = (int) (8.26 * StringHelper::countCharacters($columnText));
 338                  $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size
 339  
 340                  break;
 341          }
 342  
 343          // Calculate approximate rotated column width
 344          if ($rotation !== 0) {
 345              if ($rotation == -165) {
 346                  // stacked text
 347                  $columnWidth = 4; // approximation
 348              } else {
 349                  // rotated text
 350                  $columnWidth = $columnWidth * cos(deg2rad($rotation))
 351                                  + $fontSize * abs(sin(deg2rad($rotation))) / 5; // approximation
 352              }
 353          }
 354  
 355          // pixel width is an integer
 356          return (int) $columnWidth;
 357      }
 358  
 359      /**
 360       * Calculate an (approximate) pixel size, based on a font points size.
 361       *
 362       * @param int $fontSizeInPoints Font size (in points)
 363       *
 364       * @return int Font size (in pixels)
 365       */
 366      public static function fontSizeToPixels($fontSizeInPoints)
 367      {
 368          return (int) ((4 / 3) * $fontSizeInPoints);
 369      }
 370  
 371      /**
 372       * Calculate an (approximate) pixel size, based on inch size.
 373       *
 374       * @param int $sizeInInch Font size (in inch)
 375       *
 376       * @return int Size (in pixels)
 377       */
 378      public static function inchSizeToPixels($sizeInInch)
 379      {
 380          return $sizeInInch * 96;
 381      }
 382  
 383      /**
 384       * Calculate an (approximate) pixel size, based on centimeter size.
 385       *
 386       * @param int $sizeInCm Font size (in centimeters)
 387       *
 388       * @return float Size (in pixels)
 389       */
 390      public static function centimeterSizeToPixels($sizeInCm)
 391      {
 392          return $sizeInCm * 37.795275591;
 393      }
 394  
 395      /**
 396       * Returns the font path given the font.
 397       *
 398       * @param \PhpOffice\PhpSpreadsheet\Style\Font $font
 399       *
 400       * @return string Path to TrueType font file
 401       */
 402      public static function getTrueTypeFontFileFromFont($font)
 403      {
 404          if (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath)) {
 405              throw new PhpSpreadsheetException('Valid directory to TrueType Font files not specified');
 406          }
 407  
 408          $name = $font->getName();
 409          $bold = $font->getBold();
 410          $italic = $font->getItalic();
 411  
 412          // Check if we can map font to true type font file
 413          switch ($name) {
 414              case 'Arial':
 415                  $fontFile = (
 416                      $bold ? ($italic ? self::ARIAL_BOLD_ITALIC : self::ARIAL_BOLD)
 417                            : ($italic ? self::ARIAL_ITALIC : self::ARIAL)
 418                  );
 419  
 420                  break;
 421              case 'Calibri':
 422                  $fontFile = (
 423                      $bold ? ($italic ? self::CALIBRI_BOLD_ITALIC : self::CALIBRI_BOLD)
 424                            : ($italic ? self::CALIBRI_ITALIC : self::CALIBRI)
 425                  );
 426  
 427                  break;
 428              case 'Courier New':
 429                  $fontFile = (
 430                      $bold ? ($italic ? self::COURIER_NEW_BOLD_ITALIC : self::COURIER_NEW_BOLD)
 431                            : ($italic ? self::COURIER_NEW_ITALIC : self::COURIER_NEW)
 432                  );
 433  
 434                  break;
 435              case 'Comic Sans MS':
 436                  $fontFile = (
 437                      $bold ? self::COMIC_SANS_MS_BOLD : self::COMIC_SANS_MS
 438                  );
 439  
 440                  break;
 441              case 'Georgia':
 442                  $fontFile = (
 443                      $bold ? ($italic ? self::GEORGIA_BOLD_ITALIC : self::GEORGIA_BOLD)
 444                            : ($italic ? self::GEORGIA_ITALIC : self::GEORGIA)
 445                  );
 446  
 447                  break;
 448              case 'Impact':
 449                  $fontFile = self::IMPACT;
 450  
 451                  break;
 452              case 'Liberation Sans':
 453                  $fontFile = (
 454                      $bold ? ($italic ? self::LIBERATION_SANS_BOLD_ITALIC : self::LIBERATION_SANS_BOLD)
 455                            : ($italic ? self::LIBERATION_SANS_ITALIC : self::LIBERATION_SANS)
 456                  );
 457  
 458                  break;
 459              case 'Lucida Console':
 460                  $fontFile = self::LUCIDA_CONSOLE;
 461  
 462                  break;
 463              case 'Lucida Sans Unicode':
 464                  $fontFile = self::LUCIDA_SANS_UNICODE;
 465  
 466                  break;
 467              case 'Microsoft Sans Serif':
 468                  $fontFile = self::MICROSOFT_SANS_SERIF;
 469  
 470                  break;
 471              case 'Palatino Linotype':
 472                  $fontFile = (
 473                      $bold ? ($italic ? self::PALATINO_LINOTYPE_BOLD_ITALIC : self::PALATINO_LINOTYPE_BOLD)
 474                            : ($italic ? self::PALATINO_LINOTYPE_ITALIC : self::PALATINO_LINOTYPE)
 475                  );
 476  
 477                  break;
 478              case 'Symbol':
 479                  $fontFile = self::SYMBOL;
 480  
 481                  break;
 482              case 'Tahoma':
 483                  $fontFile = (
 484                      $bold ? self::TAHOMA_BOLD : self::TAHOMA
 485                  );
 486  
 487                  break;
 488              case 'Times New Roman':
 489                  $fontFile = (
 490                      $bold ? ($italic ? self::TIMES_NEW_ROMAN_BOLD_ITALIC : self::TIMES_NEW_ROMAN_BOLD)
 491                            : ($italic ? self::TIMES_NEW_ROMAN_ITALIC : self::TIMES_NEW_ROMAN)
 492                  );
 493  
 494                  break;
 495              case 'Trebuchet MS':
 496                  $fontFile = (
 497                      $bold ? ($italic ? self::TREBUCHET_MS_BOLD_ITALIC : self::TREBUCHET_MS_BOLD)
 498                            : ($italic ? self::TREBUCHET_MS_ITALIC : self::TREBUCHET_MS)
 499                  );
 500  
 501                  break;
 502              case 'Verdana':
 503                  $fontFile = (
 504                      $bold ? ($italic ? self::VERDANA_BOLD_ITALIC : self::VERDANA_BOLD)
 505                            : ($italic ? self::VERDANA_ITALIC : self::VERDANA)
 506                  );
 507  
 508                  break;
 509              default:
 510                  throw new PhpSpreadsheetException('Unknown font name "' . $name . '". Cannot map to TrueType font file');
 511  
 512                  break;
 513          }
 514  
 515          $fontFile = self::$trueTypeFontPath . $fontFile;
 516  
 517          // Check if file actually exists
 518          if (!file_exists($fontFile)) {
 519              throw new PhpSpreadsheetException('TrueType Font file not found');
 520          }
 521  
 522          return $fontFile;
 523      }
 524  
 525      /**
 526       * Returns the associated charset for the font name.
 527       *
 528       * @param string $name Font name
 529       *
 530       * @return int Character set code
 531       */
 532      public static function getCharsetFromFontName($name)
 533      {
 534          switch ($name) {
 535              // Add more cases. Check FONT records in real Excel files.
 536              case 'EucrosiaUPC':
 537                  return self::CHARSET_ANSI_THAI;
 538              case 'Wingdings':
 539                  return self::CHARSET_SYMBOL;
 540              case 'Wingdings 2':
 541                  return self::CHARSET_SYMBOL;
 542              case 'Wingdings 3':
 543                  return self::CHARSET_SYMBOL;
 544              default:
 545                  return self::CHARSET_ANSI_LATIN;
 546          }
 547      }
 548  
 549      /**
 550       * Get the effective column width for columns without a column dimension or column with width -1
 551       * For example, for Calibri 11 this is 9.140625 (64 px).
 552       *
 553       * @param \PhpOffice\PhpSpreadsheet\Style\Font $font The workbooks default font
 554       * @param bool $pPixels true = return column width in pixels, false = return in OOXML units
 555       *
 556       * @return mixed Column width
 557       */
 558      public static function getDefaultColumnWidthByFont(\PhpOffice\PhpSpreadsheet\Style\Font $font, $pPixels = false)
 559      {
 560          if (isset(self::$defaultColumnWidths[$font->getName()][$font->getSize()])) {
 561              // Exact width can be determined
 562              $columnWidth = $pPixels ?
 563                  self::$defaultColumnWidths[$font->getName()][$font->getSize()]['px']
 564                      : self::$defaultColumnWidths[$font->getName()][$font->getSize()]['width'];
 565          } else {
 566              // We don't have data for this particular font and size, use approximation by
 567              // extrapolating from Calibri 11
 568              $columnWidth = $pPixels ?
 569                  self::$defaultColumnWidths['Calibri'][11]['px']
 570                      : self::$defaultColumnWidths['Calibri'][11]['width'];
 571              $columnWidth = $columnWidth * $font->getSize() / 11;
 572  
 573              // Round pixels to closest integer
 574              if ($pPixels) {
 575                  $columnWidth = (int) round($columnWidth);
 576              }
 577          }
 578  
 579          return $columnWidth;
 580      }
 581  
 582      /**
 583       * Get the effective row height for rows without a row dimension or rows with height -1
 584       * For example, for Calibri 11 this is 15 points.
 585       *
 586       * @param \PhpOffice\PhpSpreadsheet\Style\Font $font The workbooks default font
 587       *
 588       * @return float Row height in points
 589       */
 590      public static function getDefaultRowHeightByFont(\PhpOffice\PhpSpreadsheet\Style\Font $font)
 591      {
 592          switch ($font->getName()) {
 593              case 'Arial':
 594                  switch ($font->getSize()) {
 595                      case 10:
 596                          // inspection of Arial 10 workbook says 12.75pt ~17px
 597                          $rowHeight = 12.75;
 598  
 599                          break;
 600                      case 9:
 601                          // inspection of Arial 9 workbook says 12.00pt ~16px
 602                          $rowHeight = 12;
 603  
 604                          break;
 605                      case 8:
 606                          // inspection of Arial 8 workbook says 11.25pt ~15px
 607                          $rowHeight = 11.25;
 608  
 609                          break;
 610                      case 7:
 611                          // inspection of Arial 7 workbook says 9.00pt ~12px
 612                          $rowHeight = 9;
 613  
 614                          break;
 615                      case 6:
 616                      case 5:
 617                          // inspection of Arial 5,6 workbook says 8.25pt ~11px
 618                          $rowHeight = 8.25;
 619  
 620                          break;
 621                      case 4:
 622                          // inspection of Arial 4 workbook says 6.75pt ~9px
 623                          $rowHeight = 6.75;
 624  
 625                          break;
 626                      case 3:
 627                          // inspection of Arial 3 workbook says 6.00pt ~8px
 628                          $rowHeight = 6;
 629  
 630                          break;
 631                      case 2:
 632                      case 1:
 633                          // inspection of Arial 1,2 workbook says 5.25pt ~7px
 634                          $rowHeight = 5.25;
 635  
 636                          break;
 637                      default:
 638                          // use Arial 10 workbook as an approximation, extrapolation
 639                          $rowHeight = 12.75 * $font->getSize() / 10;
 640  
 641                          break;
 642                  }
 643  
 644                  break;
 645              case 'Calibri':
 646                  switch ($font->getSize()) {
 647                      case 11:
 648                          // inspection of Calibri 11 workbook says 15.00pt ~20px
 649                          $rowHeight = 15;
 650  
 651                          break;
 652                      case 10:
 653                          // inspection of Calibri 10 workbook says 12.75pt ~17px
 654                          $rowHeight = 12.75;
 655  
 656                          break;
 657                      case 9:
 658                          // inspection of Calibri 9 workbook says 12.00pt ~16px
 659                          $rowHeight = 12;
 660  
 661                          break;
 662                      case 8:
 663                          // inspection of Calibri 8 workbook says 11.25pt ~15px
 664                          $rowHeight = 11.25;
 665  
 666                          break;
 667                      case 7:
 668                          // inspection of Calibri 7 workbook says 9.00pt ~12px
 669                          $rowHeight = 9;
 670  
 671                          break;
 672                      case 6:
 673                      case 5:
 674                          // inspection of Calibri 5,6 workbook says 8.25pt ~11px
 675                          $rowHeight = 8.25;
 676  
 677                          break;
 678                      case 4:
 679                          // inspection of Calibri 4 workbook says 6.75pt ~9px
 680                          $rowHeight = 6.75;
 681  
 682                          break;
 683                      case 3:
 684                          // inspection of Calibri 3 workbook says 6.00pt ~8px
 685                          $rowHeight = 6.00;
 686  
 687                          break;
 688                      case 2:
 689                      case 1:
 690                          // inspection of Calibri 1,2 workbook says 5.25pt ~7px
 691                          $rowHeight = 5.25;
 692  
 693                          break;
 694                      default:
 695                          // use Calibri 11 workbook as an approximation, extrapolation
 696                          $rowHeight = 15 * $font->getSize() / 11;
 697  
 698                          break;
 699                  }
 700  
 701                  break;
 702              case 'Verdana':
 703                  switch ($font->getSize()) {
 704                      case 10:
 705                          // inspection of Verdana 10 workbook says 12.75pt ~17px
 706                          $rowHeight = 12.75;
 707  
 708                          break;
 709                      case 9:
 710                          // inspection of Verdana 9 workbook says 11.25pt ~15px
 711                          $rowHeight = 11.25;
 712  
 713                          break;
 714                      case 8:
 715                          // inspection of Verdana 8 workbook says 10.50pt ~14px
 716                          $rowHeight = 10.50;
 717  
 718                          break;
 719                      case 7:
 720                          // inspection of Verdana 7 workbook says 9.00pt ~12px
 721                          $rowHeight = 9.00;
 722  
 723                          break;
 724                      case 6:
 725                      case 5:
 726                          // inspection of Verdana 5,6 workbook says 8.25pt ~11px
 727                          $rowHeight = 8.25;
 728  
 729                          break;
 730                      case 4:
 731                          // inspection of Verdana 4 workbook says 6.75pt ~9px
 732                          $rowHeight = 6.75;
 733  
 734                          break;
 735                      case 3:
 736                          // inspection of Verdana 3 workbook says 6.00pt ~8px
 737                          $rowHeight = 6;
 738  
 739                          break;
 740                      case 2:
 741                      case 1:
 742                          // inspection of Verdana 1,2 workbook says 5.25pt ~7px
 743                          $rowHeight = 5.25;
 744  
 745                          break;
 746                      default:
 747                          // use Verdana 10 workbook as an approximation, extrapolation
 748                          $rowHeight = 12.75 * $font->getSize() / 10;
 749  
 750                          break;
 751                  }
 752  
 753                  break;
 754              default:
 755                  // just use Calibri as an approximation
 756                  $rowHeight = 15 * $font->getSize() / 11;
 757  
 758                  break;
 759          }
 760  
 761          return $rowHeight;
 762      }
 763  }