Source for file Font.php
Documentation is available at Font.php
* Copyright (c) 2006 - 2011 PHPExcel
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Shared
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.6, 2011-02-27
* @package PHPExcel_Shared
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
/* Methods for resolving autosize value */
const AUTOSIZE_METHOD_APPROX = 'approx';
const AUTOSIZE_METHOD_EXACT = 'exact';
/** Character set codes used by BIFF5-8 in Font records */
const CHARSET_ANSI_LATIN = 0x00;
const CHARSET_SYSTEM_DEFAULT = 0x01;
const CHARSET_SYMBOL = 0x02;
const CHARSET_APPLE_ROMAN = 0x4D;
const CHARSET_ANSI_JAPANESE_SHIFTJIS = 0x80;
const CHARSET_ANSI_KOREAN_HANGUL = 0x81;
const CHARSET_ANSI_KOREAN_JOHAB = 0x82;
const CHARSET_ANSI_CHINESE_SIMIPLIFIED = 0x86;
const CHARSET_ANSI_CHINESE_TRADITIONAL = 0x88;
const CHARSET_ANSI_GREEK = 0xA1;
const CHARSET_ANSI_TURKISH = 0xA2;
const CHARSET_ANSI_VIETNAMESE = 0xA3;
const CHARSET_ANSI_HEBREW = 0xB1;
const CHARSET_ANSI_ARABIC = 0xB2;
const CHARSET_ANSI_BALTIC = 0xBA;
const CHARSET_ANSI_CYRILLIC = 0xCC;
const CHARSET_ANSI_THAI = 0xDE;
const CHARSET_ANSI_LATIN_II = 0xEE;
const CHARSET_OEM_LATIN_I = 0xFF;
// XXX: Constants created!
const ARIAL = 'arial.ttf';
const ARIAL_BOLD = 'arialbd.ttf';
const ARIAL_ITALIC = 'ariali.ttf';
const ARIAL_BOLD_ITALIC = 'arialbi.ttf';
const CALIBRI = 'CALIBRI.TTF';
const CALIBRI_BOLD = 'CALIBRIB.TTF';
const CALIBRI_ITALIC = 'CALIBRII.TTF';
const CALIBRI_BOLD_ITALIC = 'CALIBRIZ.TTF';
const COMIC_SANS_MS = 'comic.ttf';
const COMIC_SANS_MS_BOLD = 'comicbd.ttf';
const COURIER_NEW = 'cour.ttf';
const COURIER_NEW_BOLD = 'courbd.ttf';
const COURIER_NEW_ITALIC = 'couri.ttf';
const COURIER_NEW_BOLD_ITALIC = 'courbi.ttf';
const GEORGIA = 'georgia.ttf';
const GEORGIA_BOLD = 'georgiab.ttf';
const GEORGIA_ITALIC = 'georgiai.ttf';
const GEORGIA_BOLD_ITALIC = 'georgiaz.ttf';
const IMPACT = 'impact.ttf';
const LIBERATION_SANS = 'LiberationSans-Regular.ttf';
const LIBERATION_SANS_BOLD = 'LiberationSans-Bold.ttf';
const LIBERATION_SANS_ITALIC = 'LiberationSans-Italic.ttf';
const LIBERATION_SANS_BOLD_ITALIC = 'LiberationSans-BoldItalic.ttf';
const LUCIDA_CONSOLE = 'lucon.ttf';
const LUCIDA_SANS_UNICODE = 'l_10646.ttf';
const MICROSOFT_SANS_SERIF = 'micross.ttf';
const PALATINO_LINOTYPE = 'pala.ttf';
const PALATINO_LINOTYPE_BOLD = 'palab.ttf';
const PALATINO_LINOTYPE_ITALIC = 'palai.ttf';
const PALATINO_LINOTYPE_BOLD_ITALIC = 'palabi.ttf';
const SYMBOL = 'symbol.ttf';
const TAHOMA = 'tahoma.ttf';
const TAHOMA_BOLD = 'tahomabd.ttf';
const TIMES_NEW_ROMAN = 'times.ttf';
const TIMES_NEW_ROMAN_BOLD = 'timesbd.ttf';
const TIMES_NEW_ROMAN_ITALIC = 'timesi.ttf';
const TIMES_NEW_ROMAN_BOLD_ITALIC = 'timesbi.ttf';
const TREBUCHET_MS = 'trebuc.ttf';
const TREBUCHET_MS_BOLD = 'trebucbd.ttf';
const TREBUCHET_MS_ITALIC = 'trebucit.ttf';
const TREBUCHET_MS_BOLD_ITALIC = 'trebucbi.ttf';
const VERDANA = 'verdana.ttf';
const VERDANA_BOLD = 'verdanab.ttf';
const VERDANA_ITALIC = 'verdanai.ttf';
const VERDANA_BOLD_ITALIC = 'verdanaz.ttf';
private static $autoSizeMethod = self::AUTOSIZE_METHOD_APPROX;
* Path to folder containing TrueType font .ttf files
private static $trueTypeFontPath = null;
* How wide is a default column for a given default font and size?
* Empirical data found by inspecting real Excel files and reading off the pixel width
* in Microsoft Office Excel 2007.
public static $defaultColumnWidths = array(
1 => array('px' => 24, 'width' => 12.00000000),
2 => array('px' => 24, 'width' => 12.00000000),
3 => array('px' => 32, 'width' => 10.66406250),
4 => array('px' => 32, 'width' => 10.66406250),
5 => array('px' => 40, 'width' => 10.00000000),
6 => array('px' => 48, 'width' => 9.59765625),
7 => array('px' => 48, 'width' => 9.59765625),
8 => array('px' => 56, 'width' => 9.33203125),
9 => array('px' => 64, 'width' => 9.14062500),
10 => array('px' => 64, 'width' => 9.14062500),
1 => array('px' => 24, 'width' => 12.00000000),
2 => array('px' => 24, 'width' => 12.00000000),
3 => array('px' => 32, 'width' => 10.66406250),
4 => array('px' => 32, 'width' => 10.66406250),
5 => array('px' => 40, 'width' => 10.00000000),
6 => array('px' => 48, 'width' => 9.59765625),
7 => array('px' => 48, 'width' => 9.59765625),
8 => array('px' => 56, 'width' => 9.33203125),
9 => array('px' => 56, 'width' => 9.33203125),
10 => array('px' => 64, 'width' => 9.14062500),
11 => array('px' => 64, 'width' => 9.14062500),
1 => array('px' => 24, 'width' => 12.00000000),
2 => array('px' => 24, 'width' => 12.00000000),
3 => array('px' => 32, 'width' => 10.66406250),
4 => array('px' => 32, 'width' => 10.66406250),
5 => array('px' => 40, 'width' => 10.00000000),
6 => array('px' => 48, 'width' => 9.59765625),
7 => array('px' => 48, 'width' => 9.59765625),
8 => array('px' => 64, 'width' => 9.14062500),
9 => array('px' => 72, 'width' => 9.00000000),
10 => array('px' => 72, 'width' => 9.00000000),
self::$autoSizeMethod = $pValue;
return self::$autoSizeMethod;
* Set the path to the folder containing .ttf files. There should be a trailing slash.
* Typical locations on variout some platforms:
* <li>C:/Windows/Fonts/</li>
* <li>/usr/share/fonts/truetype/</li>
self::$trueTypeFontPath = $pValue;
* Get the path to the folder containing .ttf files.
return self::$trueTypeFontPath;
* Calculate an (approximate) OpenXML column width, based on font size and text contained
* @param int $fontSize Font size (in pixels or points)
* @param bool $fontSizeInPixels Is the font size specified in pixels (true) or in points (false) ?
* @param string $cellText Text to calculate width
* @param int $rotation Rotation angle
* @return int Column width
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);
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
// If autosize method is set to 'approx', use approximation
if (self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX) {
throw new Exception('AutoSize method is set to approx');
// Width of text in pixels excl. padding
$columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation);
// Excel adds some padding, use 1.07 of the width of an 'n' glyph
$columnWidth += ceil(self::getTextWidthPixelsExact('0', $font, 0) * 1.07); // pixels incl. padding
// Width of text in pixels excl. padding, approximation
$columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation);
// Excel adds some padding, just use approx width of 'n' glyph
$columnWidth += self::getTextWidthPixelsApprox('n', $font, 0);
// Convert from pixel width to column width
return round($columnWidth, 6);
* Get GD text width in pixels for a string of text in a certain font at a certain rotation angle
* @param PHPExcel_Style_Font
throw new Exception('GD library needs to be enabled');
// font size should really be supplied in pixels in GD2,
// but since GD2 seems to assume 72dpi, pixels and points are the same
$fontFile = self::getTrueTypeFontFileFromFont($font);
$textBox = imagettfbbox($font->getSize(), $rotation, $fontFile, $text);
$lowerLeftCornerX = $textBox[0];
$lowerLeftCornerY = $textBox[1];
$lowerRightCornerX = $textBox[2];
$lowerRightCornerY = $textBox[3];
$upperRightCornerX = $textBox[4];
$upperRightCornerY = $textBox[5];
$upperLeftCornerX = $textBox[6];
$upperLeftCornerY = $textBox[7];
// Consider the rotation when calculating the width
$textWidth = max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX);
* Get approximate width in pixels for a string of text in a certain font at a certain rotation angle
* @param string $columnText
* @param PHPExcel_Style_Font $font
* @return int Text width in pixels (no padding added)
$fontName = $font->getName();
$fontSize = $font->getSize();
// Calculate column width in pixels. We assume fixed glyph width. Result varies with font name and size.
// value 8.26 was found via interpolation by inspecting real Excel files with Calibri 11 font.
$columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size
// value 7 was found via interpolation by inspecting real Excel files with Arial 10 font.
$columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size
// value 8 was found via interpolation by inspecting real Excel files with Verdana 10 font.
$columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size
$columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size
// Calculate approximate rotated column width
$columnWidth = 4; // approximation
$columnWidth = $columnWidth * cos(deg2rad($rotation))
// pixel width is an integer
$columnWidth = (int) $columnWidth;
* Calculate an (approximate) pixel size, based on a font points size
* @param int $fontSizeInPoints Font size (in points)
* @return int Font size (in pixels)
return (int) ((4 / 3) * $fontSizeInPoints);
* Calculate an (approximate) pixel size, based on inch size
* @param int $sizeInInch Font size (in inch)
* @return int Size (in pixels)
return ($sizeInInch * 96);
* Calculate an (approximate) pixel size, based on centimeter size
* @param int $sizeInCm Font size (in centimeters)
* @return int Size (in pixels)
return ($sizeInCm * 37.795275591);
* Returns the font path given the font
* @param PHPExcel_Style_Font
* @return string Path to TrueType font file
if (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath)) {
throw new Exception('Valid directory to TrueType Font files not specified');
$name = $font->getName();
$bold = $font->getBold();
$italic = $font->getItalic();
// Check if we can map font to true type font file
$bold ? ($italic ? self::ARIAL_BOLD_ITALIC : self::ARIAL_BOLD)
: ($italic ? self::ARIAL_ITALIC : self::ARIAL)
$bold ? ($italic ? self::CALIBRI_BOLD_ITALIC : self::CALIBRI_BOLD)
: ($italic ? self::CALIBRI_ITALIC : self::CALIBRI)
$bold ? ($italic ? self::COURIER_NEW_BOLD_ITALIC : self::COURIER_NEW_BOLD)
: ($italic ? self::COURIER_NEW_ITALIC : self::COURIER_NEW)
$bold ? self::COMIC_SANS_MS_BOLD : self::COMIC_SANS_MS
$bold ? ($italic ? self::GEORGIA_BOLD_ITALIC : self::GEORGIA_BOLD)
: ($italic ? self::GEORGIA_ITALIC : self::GEORGIA)
$fontFile = self::IMPACT;
$bold ? ($italic ? self::LIBERATION_SANS_BOLD_ITALIC : self::LIBERATION_SANS_BOLD)
: ($italic ? self::LIBERATION_SANS_ITALIC : self::LIBERATION_SANS)
$fontFile = self::LUCIDA_CONSOLE;
case 'Lucida Sans Unicode':
$fontFile = self::LUCIDA_SANS_UNICODE;
case 'Microsoft Sans Serif':
$fontFile = self::MICROSOFT_SANS_SERIF;
case 'Palatino Linotype':
$bold ? ($italic ? self::PALATINO_LINOTYPE_BOLD_ITALIC : self::PALATINO_LINOTYPE_BOLD)
: ($italic ? self::PALATINO_LINOTYPE_ITALIC : self::PALATINO_LINOTYPE)
$fontFile = self::SYMBOL;
$bold ? self::TAHOMA_BOLD : self::TAHOMA
$bold ? ($italic ? self::TIMES_NEW_ROMAN_BOLD_ITALIC : self::TIMES_NEW_ROMAN_BOLD)
: ($italic ? self::TIMES_NEW_ROMAN_ITALIC : self::TIMES_NEW_ROMAN)
$bold ? ($italic ? self::TREBUCHET_MS_BOLD_ITALIC : self::TREBUCHET_MS_BOLD)
: ($italic ? self::TREBUCHET_MS_ITALIC : self::TREBUCHET_MS)
$bold ? ($italic ? self::VERDANA_BOLD_ITALIC : self::VERDANA_BOLD)
: ($italic ? self::VERDANA_ITALIC : self::VERDANA)
throw new Exception('Unknown font name "'. $name . '". Cannot map to TrueType font file');
$fontFile = self::$trueTypeFontPath . $fontFile;
// Check if file actually exists
if (!file_exists($fontFile)) {
throw New Exception('TrueType Font file not found');
* Returns the associated charset for the font name.
* @param string $name Font name
* @return int Character set code
// Add more cases. Check FONT records in real Excel files.
case 'EucrosiaUPC': return self::CHARSET_ANSI_THAI;
case 'Wingdings': return self::CHARSET_SYMBOL;
case 'Wingdings 2': return self::CHARSET_SYMBOL;
case 'Wingdings 3': return self::CHARSET_SYMBOL;
default: return self::CHARSET_ANSI_LATIN;
* Get the effective column width for columns without a column dimension or column with width -1
* For example, for Calibri 11 this is 9.140625 (64 px)
* @param PHPExcel_Style_Font $font The workbooks default font
* @param boolean $pPixels true = return column width in pixels, false = return in OOXML units
* @return mixed Column width
if (isset (self::$defaultColumnWidths[$font->getName()][$font->getSize()])) {
// Exact width can be determined
$columnWidth = $pPixels ?
self::$defaultColumnWidths[$font->getName()][$font->getSize()]['px']
: self::$defaultColumnWidths[$font->getName()][$font->getSize()]['width'];
// We don't have data for this particular font and size, use approximation by
// extrapolating from Calibri 11
$columnWidth = $pPixels ?
self::$defaultColumnWidths['Calibri'][11]['px']
: self::$defaultColumnWidths['Calibri'][11]['width'];
$columnWidth = $columnWidth * $font->getSize() / 11;
// Round pixels to closest integer
$columnWidth = (int) round($columnWidth);
* Get the effective row height for rows without a row dimension or rows with height -1
* For example, for Calibri 11 this is 15 points
* @param PHPExcel_Style_Font $font The workbooks default font
* @return float Row height in points
switch ($font->getName()) {
switch ($font->getSize()) {
// inspection of Arial 10 workbook says 12.75pt ~17px
// inspection of Arial 9 workbook says 12.00pt ~16px
// inspection of Arial 8 workbook says 11.25pt ~15px
// inspection of Arial 7 workbook says 9.00pt ~12px
// inspection of Arial 5,6 workbook says 8.25pt ~11px
// inspection of Arial 4 workbook says 6.75pt ~9px
// inspection of Arial 3 workbook says 6.00pt ~8px
// inspection of Arial 1,2 workbook says 5.25pt ~7px
// use Arial 10 workbook as an approximation, extrapolation
$rowHeight = 12.75 * $font->getSize() / 10;
switch ($font->getSize()) {
// inspection of Calibri 11 workbook says 15.00pt ~20px
// inspection of Calibri 10 workbook says 12.75pt ~17px
// inspection of Calibri 9 workbook says 12.00pt ~16px
// inspection of Calibri 8 workbook says 11.25pt ~15px
// inspection of Calibri 7 workbook says 9.00pt ~12px
// inspection of Calibri 5,6 workbook says 8.25pt ~11px
// inspection of Calibri 4 workbook says 6.75pt ~9px
// inspection of Calibri 3 workbook says 6.00pt ~8px
// inspection of Calibri 1,2 workbook says 5.25pt ~7px
// use Calibri 11 workbook as an approximation, extrapolation
$rowHeight = 15 * $font->getSize() / 11;
switch ($font->getSize()) {
// inspection of Verdana 10 workbook says 12.75pt ~17px
// inspection of Verdana 9 workbook says 11.25pt ~15px
// inspection of Verdana 8 workbook says 10.50pt ~14px
// inspection of Verdana 7 workbook says 9.00pt ~12px
// inspection of Verdana 5,6 workbook says 8.25pt ~11px
// inspection of Verdana 4 workbook says 6.75pt ~9px
// inspection of Verdana 3 workbook says 6.00pt ~8px
// inspection of Verdana 1,2 workbook says 5.25pt ~7px
// use Verdana 10 workbook as an approximation, extrapolation
$rowHeight = 12.75 * $font->getSize() / 10;
// just use Calibri as an approximation
$rowHeight = 15 * $font->getSize() / 11;
|