[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Shared/ -> Font.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1