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.
<?php

namespace PhpOffice\PhpSpreadsheet\Shared;

use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
> use PhpOffice\PhpSpreadsheet\Style\Alignment; > use PhpOffice\PhpSpreadsheet\Style\Font as FontStyle;
class Font { // Methods for resolving autosize value const AUTOSIZE_METHOD_APPROX = 'approx'; const AUTOSIZE_METHOD_EXACT = 'exact';
< private static $autoSizeMethods = [
> private const AUTOSIZE_METHODS = [
self::AUTOSIZE_METHOD_APPROX, self::AUTOSIZE_METHOD_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; // gb2312 const CHARSET_ANSI_CHINESE_TRADITIONAL = 0x88; // big5 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 = 0xDD; const CHARSET_ANSI_LATIN_II = 0xEE; const CHARSET_OEM_LATIN_I = 0xFF; // XXX: Constants created! /** Font filenames */ 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 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';
> const FONT_FILE_NAMES = [ /** > 'Arial' => [ * AutoSize method. > 'x' => self::ARIAL, * > 'xb' => self::ARIAL_BOLD, * @var string > 'xi' => self::ARIAL_ITALIC, */ > 'xbi' => self::ARIAL_BOLD_ITALIC, private static $autoSizeMethod = self::AUTOSIZE_METHOD_APPROX; > ], > 'Calibri' => [ /** > 'x' => self::CALIBRI, * Path to folder containing TrueType font .ttf files. > 'xb' => self::CALIBRI_BOLD, * > 'xi' => self::CALIBRI_ITALIC, * @var string > 'xbi' => self::CALIBRI_BOLD_ITALIC, */ > ], private static $trueTypeFontPath = null; > 'Comic Sans MS' => [ > 'x' => self::COMIC_SANS_MS, /** > 'xb' => self::COMIC_SANS_MS_BOLD, * How wide is a default column for a given default font and size? > 'xi' => self::COMIC_SANS_MS, * Empirical data found by inspecting real Excel files and reading off the pixel width > 'xbi' => self::COMIC_SANS_MS_BOLD, * in Microsoft Office Excel 2007. > ], * > 'Courier New' => [ * @var array > 'x' => self::COURIER_NEW, */ > 'xb' => self::COURIER_NEW_BOLD, public static $defaultColumnWidths = [ > 'xi' => self::COURIER_NEW_ITALIC, 'Arial' => [ > 'xbi' => self::COURIER_NEW_BOLD_ITALIC, 1 => ['px' => 24, 'width' => 12.00000000], > ], 2 => ['px' => 24, 'width' => 12.00000000], > 'Georgia' => [ 3 => ['px' => 32, 'width' => 10.66406250], > 'x' => self::GEORGIA, 4 => ['px' => 32, 'width' => 10.66406250], > 'xb' => self::GEORGIA_BOLD, 5 => ['px' => 40, 'width' => 10.00000000], > 'xi' => self::GEORGIA_ITALIC, 6 => ['px' => 48, 'width' => 9.59765625], > 'xbi' => self::GEORGIA_BOLD_ITALIC, 7 => ['px' => 48, 'width' => 9.59765625], > ], 8 => ['px' => 56, 'width' => 9.33203125], > 'Impact' => [ 9 => ['px' => 64, 'width' => 9.14062500], > 'x' => self::IMPACT, 10 => ['px' => 64, 'width' => 9.14062500], > 'xb' => self::IMPACT, ], > 'xi' => self::IMPACT, 'Calibri' => [ > 'xbi' => self::IMPACT, 1 => ['px' => 24, 'width' => 12.00000000], > ], 2 => ['px' => 24, 'width' => 12.00000000], > 'Liberation Sans' => [ 3 => ['px' => 32, 'width' => 10.66406250], > 'x' => self::LIBERATION_SANS, 4 => ['px' => 32, 'width' => 10.66406250], > 'xb' => self::LIBERATION_SANS_BOLD, 5 => ['px' => 40, 'width' => 10.00000000], > 'xi' => self::LIBERATION_SANS_ITALIC, 6 => ['px' => 48, 'width' => 9.59765625], > 'xbi' => self::LIBERATION_SANS_BOLD_ITALIC, 7 => ['px' => 48, 'width' => 9.59765625], > ], 8 => ['px' => 56, 'width' => 9.33203125], > 'Lucida Console' => [ 9 => ['px' => 56, 'width' => 9.33203125], > 'x' => self::LUCIDA_CONSOLE, 10 => ['px' => 64, 'width' => 9.14062500], > 'xb' => self::LUCIDA_CONSOLE, 11 => ['px' => 64, 'width' => 9.14062500], > 'xi' => self::LUCIDA_CONSOLE, ], > 'xbi' => self::LUCIDA_CONSOLE, 'Verdana' => [ > ], 1 => ['px' => 24, 'width' => 12.00000000], > 'Lucida Sans Unicode' => [ 2 => ['px' => 24, 'width' => 12.00000000], > 'x' => self::LUCIDA_SANS_UNICODE, 3 => ['px' => 32, 'width' => 10.66406250], > 'xb' => self::LUCIDA_SANS_UNICODE, 4 => ['px' => 32, 'width' => 10.66406250], > 'xi' => self::LUCIDA_SANS_UNICODE, 5 => ['px' => 40, 'width' => 10.00000000], > 'xbi' => self::LUCIDA_SANS_UNICODE, 6 => ['px' => 48, 'width' => 9.59765625], > ], 7 => ['px' => 48, 'width' => 9.59765625], > 'Microsoft Sans Serif' => [ 8 => ['px' => 64, 'width' => 9.14062500], > 'x' => self::MICROSOFT_SANS_SERIF, 9 => ['px' => 72, 'width' => 9.00000000], > 'xb' => self::MICROSOFT_SANS_SERIF, 10 => ['px' => 72, 'width' => 9.00000000], > 'xi' => self::MICROSOFT_SANS_SERIF, ], > 'xbi' => self::MICROSOFT_SANS_SERIF, ]; > ], > 'Palatino Linotype' => [ /** > 'x' => self::PALATINO_LINOTYPE, * Set autoSize method. > 'xb' => self::PALATINO_LINOTYPE_BOLD, * > 'xi' => self::PALATINO_LINOTYPE_ITALIC, * @param string $pValue see self::AUTOSIZE_METHOD_* > 'xbi' => self::PALATINO_LINOTYPE_BOLD_ITALIC, * > ], * @return bool Success or failure > 'Symbol' => [ */ > 'x' => self::SYMBOL, public static function setAutoSizeMethod($pValue) > 'xb' => self::SYMBOL, { > 'xi' => self::SYMBOL, if (!in_array($pValue, self::$autoSizeMethods)) { > 'xbi' => self::SYMBOL, return false; > ], } > 'Tahoma' => [ self::$autoSizeMethod = $pValue; > 'x' => self::TAHOMA, > 'xb' => self::TAHOMA_BOLD, return true; > 'xi' => self::TAHOMA, } > 'xbi' => self::TAHOMA_BOLD, > ], /** > 'Times New Roman' => [ * Get autoSize method. > 'x' => self::TIMES_NEW_ROMAN, * > 'xb' => self::TIMES_NEW_ROMAN_BOLD, * @return string > 'xi' => self::TIMES_NEW_ROMAN_ITALIC, */ > 'xbi' => self::TIMES_NEW_ROMAN_BOLD_ITALIC, public static function getAutoSizeMethod() > ], { > 'Trebuchet MS' => [ return self::$autoSizeMethod; > 'x' => self::TREBUCHET_MS, } > 'xb' => self::TREBUCHET_MS_BOLD, > 'xi' => self::TREBUCHET_MS_ITALIC, /** > 'xbi' => self::TREBUCHET_MS_BOLD_ITALIC, * Set the path to the folder containing .ttf files. There should be a trailing slash. > ], * Typical locations on variout some platforms: > 'Verdana' => [ * <ul> > 'x' => self::VERDANA, * <li>C:/Windows/Fonts/</li> > 'xb' => self::VERDANA_BOLD, * <li>/usr/share/fonts/truetype/</li> > 'xi' => self::VERDANA_ITALIC, * <li>~/.fonts/</li> > 'xbi' => self::VERDANA_BOLD_ITALIC, * </ul>. > ], * > ]; * @param string $pValue > */ > /** public static function setTrueTypeFontPath($pValue): void > * Array that can be used to supplement FONT_FILE_NAMES for calculating exact width. { > * self::$trueTypeFontPath = $pValue; > * @var array } > */ > private static $extraFontArray = []; /** > * Get the path to the folder containing .ttf files. > public static function setExtraFontArray(array $extraFontArray): void * > { * @return string > self::$extraFontArray = $extraFontArray; */ > } public static function getTrueTypeFontPath() > { > public static function getExtraFontArray(): array return self::$trueTypeFontPath; > { } > return self::$extraFontArray; > } /** >
< private static $trueTypeFontPath = null;
> private static $trueTypeFontPath = '';
< * < * @var array
> * Added height in points.
< public static $defaultColumnWidths = [
> public const DEFAULT_COLUMN_WIDTHS = [
< 1 => ['px' => 24, 'width' => 12.00000000], < 2 => ['px' => 24, 'width' => 12.00000000], < 3 => ['px' => 32, 'width' => 10.66406250], < 4 => ['px' => 32, 'width' => 10.66406250], < 5 => ['px' => 40, 'width' => 10.00000000], < 6 => ['px' => 48, 'width' => 9.59765625], < 7 => ['px' => 48, 'width' => 9.59765625], < 8 => ['px' => 56, 'width' => 9.33203125], < 9 => ['px' => 64, 'width' => 9.14062500], < 10 => ['px' => 64, 'width' => 9.14062500],
> 1 => ['px' => 24, 'width' => 12.00000000, 'height' => 5.25], > 2 => ['px' => 24, 'width' => 12.00000000, 'height' => 5.25], > 3 => ['px' => 32, 'width' => 10.66406250, 'height' => 6.0], > > 4 => ['px' => 32, 'width' => 10.66406250, 'height' => 6.75], > 5 => ['px' => 40, 'width' => 10.00000000, 'height' => 8.25], > 6 => ['px' => 48, 'width' => 9.59765625, 'height' => 8.25], > 7 => ['px' => 48, 'width' => 9.59765625, 'height' => 9.0], > 8 => ['px' => 56, 'width' => 9.33203125, 'height' => 11.25], > 9 => ['px' => 64, 'width' => 9.14062500, 'height' => 12.0], > 10 => ['px' => 64, 'width' => 9.14062500, 'height' => 12.75],
< 1 => ['px' => 24, 'width' => 12.00000000], < 2 => ['px' => 24, 'width' => 12.00000000], < 3 => ['px' => 32, 'width' => 10.66406250], < 4 => ['px' => 32, 'width' => 10.66406250], < 5 => ['px' => 40, 'width' => 10.00000000], < 6 => ['px' => 48, 'width' => 9.59765625], < 7 => ['px' => 48, 'width' => 9.59765625], < 8 => ['px' => 56, 'width' => 9.33203125], < 9 => ['px' => 56, 'width' => 9.33203125], < 10 => ['px' => 64, 'width' => 9.14062500], < 11 => ['px' => 64, 'width' => 9.14062500],
> 1 => ['px' => 24, 'width' => 12.00000000, 'height' => 5.25], > 2 => ['px' => 24, 'width' => 12.00000000, 'height' => 5.25], > 3 => ['px' => 32, 'width' => 10.66406250, 'height' => 6.00], > 4 => ['px' => 32, 'width' => 10.66406250, 'height' => 6.75], > 5 => ['px' => 40, 'width' => 10.00000000, 'height' => 8.25], > 6 => ['px' => 48, 'width' => 9.59765625, 'height' => 8.25], > 7 => ['px' => 48, 'width' => 9.59765625, 'height' => 9.0], > 8 => ['px' => 56, 'width' => 9.33203125, 'height' => 11.25], > 9 => ['px' => 56, 'width' => 9.33203125, 'height' => 12.0], > 10 => ['px' => 64, 'width' => 9.14062500, 'height' => 12.75], > 11 => ['px' => 64, 'width' => 9.14062500, 'height' => 15.0],
< 1 => ['px' => 24, 'width' => 12.00000000], < 2 => ['px' => 24, 'width' => 12.00000000], < 3 => ['px' => 32, 'width' => 10.66406250], < 4 => ['px' => 32, 'width' => 10.66406250], < 5 => ['px' => 40, 'width' => 10.00000000], < 6 => ['px' => 48, 'width' => 9.59765625], < 7 => ['px' => 48, 'width' => 9.59765625], < 8 => ['px' => 64, 'width' => 9.14062500], < 9 => ['px' => 72, 'width' => 9.00000000], < 10 => ['px' => 72, 'width' => 9.00000000],
> 1 => ['px' => 24, 'width' => 12.00000000, 'height' => 5.25], > 2 => ['px' => 24, 'width' => 12.00000000, 'height' => 5.25], > 3 => ['px' => 32, 'width' => 10.66406250, 'height' => 6.0], > 4 => ['px' => 32, 'width' => 10.66406250, 'height' => 6.75], > 5 => ['px' => 40, 'width' => 10.00000000, 'height' => 8.25], > 6 => ['px' => 48, 'width' => 9.59765625, 'height' => 8.25], > 7 => ['px' => 48, 'width' => 9.59765625, 'height' => 9.0], > 8 => ['px' => 64, 'width' => 9.14062500, 'height' => 10.5], > 9 => ['px' => 72, 'width' => 9.00000000, 'height' => 11.25], > 10 => ['px' => 72, 'width' => 9.00000000, 'height' => 12.75],
$columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation) + $columnWidthAdjust;
> * List of column widths. Replaced by constant; } catch (PhpSpreadsheetException $e) { > * previously it was public and updateable, allowing $approximate = true; > * user to make inappropriate alterations. } > * } > * @deprecated 1.25.0 Use DEFAULT_COLUMN_WIDTHS constant instead. > * if ($approximate) { > * @var array $columnWidthAdjust = self::getTextWidthPixelsApprox('n', $font, 0); > */ // Width of text in pixels excl. padding, approximation > public static $defaultColumnWidths = self::DEFAULT_COLUMN_WIDTHS; // and addition because Excel adds some padding, just use approx width of 'n' glyph > $columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation) + $columnWidthAdjust; > /**
< * @param string $pValue see self::AUTOSIZE_METHOD_*
> * @param string $method see self::AUTOSIZE_METHOD_*
< public static function setAutoSizeMethod($pValue)
> public static function setAutoSizeMethod($method)
< if (!in_array($pValue, self::$autoSizeMethods)) {
> if (!in_array($method, self::AUTOSIZE_METHODS)) {
< self::$autoSizeMethod = $pValue;
> self::$autoSizeMethod = $method;
< * @param string $pValue
> * @param string $folderPath
< public static function setTrueTypeFontPath($pValue): void
> public static function setTrueTypeFontPath($folderPath): void
< self::$trueTypeFontPath = $pValue;
> self::$trueTypeFontPath = $folderPath;
< * @param \PhpOffice\PhpSpreadsheet\Style\Font $font Font object < * @param RichText|string $cellText Text to calculate width
> * @param FontStyle $font Font object > * @param null|RichText|string $cellText Text to calculate width
< * @param null|\PhpOffice\PhpSpreadsheet\Style\Font $defaultFont Font object < * < * @return int Column width
> * @param null|FontStyle $defaultFont Font object > * @param bool $filterAdjustment Add space for Autofilter or Table dropdown
< public static function calculateColumnWidth(\PhpOffice\PhpSpreadsheet\Style\Font $font, $cellText = '', $rotation = 0, ?\PhpOffice\PhpSpreadsheet\Style\Font $defaultFont = null) < {
> public static function calculateColumnWidth( > FontStyle $font, > $cellText = '', > $rotation = 0, > ?FontStyle $defaultFont = null, > bool $filterAdjustment = false, > int $indentAdjustment = 0 > ): float {
* @param int $rotation
> $cellText = (string) $cellText;
< $lineWidths[] = self::calculateColumnWidth($font, $lineText, $rotation = 0, $defaultFont);
> $lineWidths[] = self::calculateColumnWidth($font, $lineText, $rotation = 0, $defaultFont, $filterAdjustment);
< $approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX;
> $approximate = self::$autoSizeMethod === self::AUTOSIZE_METHOD_APPROX; > $columnWidth = 0;
< $columnWidthAdjust = ceil(self::getTextWidthPixelsExact('n', $font, 0) * 1.07);
> $columnWidthAdjust = ceil( > self::getTextWidthPixelsExact( > str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))), > $font, > 0 > ) * 1.07 > );
< $columnWidthAdjust = self::getTextWidthPixelsApprox('n', $font, 0);
> $columnWidthAdjust = self::getTextWidthPixelsApprox( > str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))), > $font, > 0 > );
< $columnWidth = Drawing::pixelsToCellDimension($columnWidth, $defaultFont);
> $columnWidth = Drawing::pixelsToCellDimension((int) $columnWidth, $defaultFont ?? new FontStyle());
< return round($columnWidth, 6);
> return round($columnWidth, 4);
< * < * @param string $text < * @param \PhpOffice\PhpSpreadsheet\Style\Font < * @param int $rotation < * < * @return int
< public static function getTextWidthPixelsExact($text, \PhpOffice\PhpSpreadsheet\Style\Font $font, $rotation = 0)
> public static function getTextWidthPixelsExact(string $text, FontStyle $font, int $rotation = 0): float
< if (!function_exists('imagettfbbox')) { < throw new PhpSpreadsheetException('GD library needs to be enabled'); < } <
< $textBox = imagettfbbox($font->getSize(), $rotation, $fontFile, $text);
> $textBox = imagettfbbox($font->getSize() ?? 10.0, $rotation, $fontFile, $text); > if ($textBox === false) { > // @codeCoverageIgnoreStart > throw new PhpSpreadsheetException('imagettfbbox failed'); > // @codeCoverageIgnoreEnd > }
$upperLeftCornerX = $textBox[6]; // Consider the rotation when calculating the width
< return max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX);
> return round(max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX), 4);
} /** * Get approximate width in pixels for a string of text in a certain font at a certain rotation angle. * * @param string $columnText * @param int $rotation * * @return int Text width in pixels (no padding added) */
< public static function getTextWidthPixelsApprox($columnText, \PhpOffice\PhpSpreadsheet\Style\Font $font, $rotation = 0)
> public static function getTextWidthPixelsApprox($columnText, FontStyle $font, $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 * StringHelper::countCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size break; case 'Arial': // value 8 was set because of experience in different exports at Arial 10 font. $columnWidth = (int) (8 * StringHelper::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 * StringHelper::countCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size break; default: // just assume Calibri $columnWidth = (int) (8.26 * StringHelper::countCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size break; } // Calculate approximate rotated column width if ($rotation !== 0) {
< if ($rotation == -165) {
> if ($rotation == Alignment::TEXTROTATION_STACK_PHPSPREADSHEET) {
// 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; } /** * Calculate an (approximate) pixel size, based on a font points size. * * @param int $fontSizeInPoints Font size (in points) * * @return int Font size (in pixels) */ public static function fontSizeToPixels($fontSizeInPoints) { 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) */ public static function inchSizeToPixels($sizeInInch) { return $sizeInInch * 96; } /** * Calculate an (approximate) pixel size, based on centimeter size. * * @param int $sizeInCm Font size (in centimeters) * * @return float Size (in pixels) */ public static function centimeterSizeToPixels($sizeInCm) { return $sizeInCm * 37.795275591; } /** * Returns the font path given the font. *
< * @param \PhpOffice\PhpSpreadsheet\Style\Font $font < *
* @return string Path to TrueType font file */
< public static function getTrueTypeFontFileFromFont($font)
> public static function getTrueTypeFontFileFromFont(FontStyle $font, bool $checkPath = true)
{
< if (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath)) {
> if ($checkPath && (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath))) {
throw new PhpSpreadsheetException('Valid directory to TrueType Font files not specified'); } $name = $font->getName();
> $fontArray = array_merge(self::FONT_FILE_NAMES, self::$extraFontArray); $bold = $font->getBold(); > if (!isset($fontArray[$name])) { $italic = $font->getItalic(); > throw new PhpSpreadsheetException('Unknown font name "' . $name . '". Cannot map to TrueType font file'); > }
< < // Check if we can map font to true type font file < switch ($name) { < case 'Arial': < $fontFile = ( < $bold ? ($italic ? self::ARIAL_BOLD_ITALIC : self::ARIAL_BOLD) < : ($italic ? self::ARIAL_ITALIC : self::ARIAL) < ); < < break; < case 'Calibri': < $fontFile = ( < $bold ? ($italic ? self::CALIBRI_BOLD_ITALIC : self::CALIBRI_BOLD) < : ($italic ? self::CALIBRI_ITALIC : self::CALIBRI) < ); < < break; < case 'Courier New': < $fontFile = ( < $bold ? ($italic ? self::COURIER_NEW_BOLD_ITALIC : self::COURIER_NEW_BOLD) < : ($italic ? self::COURIER_NEW_ITALIC : self::COURIER_NEW) < ); < < break; < case 'Comic Sans MS': < $fontFile = ( < $bold ? self::COMIC_SANS_MS_BOLD : self::COMIC_SANS_MS < ); < < break; < case 'Georgia': < $fontFile = ( < $bold ? ($italic ? self::GEORGIA_BOLD_ITALIC : self::GEORGIA_BOLD) < : ($italic ? self::GEORGIA_ITALIC : self::GEORGIA) < ); < < break; < case 'Impact': < $fontFile = self::IMPACT; < < break; < case 'Liberation Sans': < $fontFile = ( < $bold ? ($italic ? self::LIBERATION_SANS_BOLD_ITALIC : self::LIBERATION_SANS_BOLD) < : ($italic ? self::LIBERATION_SANS_ITALIC : self::LIBERATION_SANS) < ); < < break; < case 'Lucida Console': < $fontFile = self::LUCIDA_CONSOLE; < < break; < case 'Lucida Sans Unicode': < $fontFile = self::LUCIDA_SANS_UNICODE; < < break; < case 'Microsoft Sans Serif': < $fontFile = self::MICROSOFT_SANS_SERIF; < < break; < case 'Palatino Linotype': < $fontFile = ( < $bold ? ($italic ? self::PALATINO_LINOTYPE_BOLD_ITALIC : self::PALATINO_LINOTYPE_BOLD) < : ($italic ? self::PALATINO_LINOTYPE_ITALIC : self::PALATINO_LINOTYPE) < ); < < break; < case 'Symbol': < $fontFile = self::SYMBOL; < < break; < case 'Tahoma': < $fontFile = ( < $bold ? self::TAHOMA_BOLD : self::TAHOMA < ); < < break; < case 'Times New Roman': < $fontFile = ( < $bold ? ($italic ? self::TIMES_NEW_ROMAN_BOLD_ITALIC : self::TIMES_NEW_ROMAN_BOLD) < : ($italic ? self::TIMES_NEW_ROMAN_ITALIC : self::TIMES_NEW_ROMAN) < ); < < break; < case 'Trebuchet MS': < $fontFile = ( < $bold ? ($italic ? self::TREBUCHET_MS_BOLD_ITALIC : self::TREBUCHET_MS_BOLD) < : ($italic ? self::TREBUCHET_MS_ITALIC : self::TREBUCHET_MS) < ); < < break; < case 'Verdana': < $fontFile = ( < $bold ? ($italic ? self::VERDANA_BOLD_ITALIC : self::VERDANA_BOLD) < : ($italic ? self::VERDANA_ITALIC : self::VERDANA) < ); < < break; < default: < throw new PhpSpreadsheetException('Unknown font name "' . $name . '". Cannot map to TrueType font file'); < < break;
> $index = 'x'; > if ($bold) { > $index .= 'b';
> if ($italic) { $fontFile = self::$trueTypeFontPath . $fontFile; > $index .= 'i'; > } // Check if file actually exists > $fontFile = $fontArray[$name][$index];
< $fontFile = self::$trueTypeFontPath . $fontFile;
> $separator = ''; > if (mb_strlen(self::$trueTypeFontPath) > 1 && mb_substr(self::$trueTypeFontPath, -1) !== '/' && mb_substr(self::$trueTypeFontPath, -1) !== '\\') { > $separator = DIRECTORY_SEPARATOR; > } > $fontFile = self::$trueTypeFontPath . $separator . $fontFile;
throw new PhpSpreadsheetException('TrueType Font file not found');
> if ($checkPath && !file_exists($fontFile)) { } > $alternateName = $name; > if ($index !== 'x' && $fontArray[$name][$index] !== $fontArray[$name]['x']) { return $fontFile; > // Bold but no italic: } > // Comic Sans > // Tahoma /** > // Neither bold nor italic: * Returns the associated charset for the font name. > // Impact * > // Lucida Console * @param string $name Font name > // Lucida Sans Unicode * > // Microsoft Sans Serif * @return int Character set code > // Symbol */ > if ($index === 'xb') { public static function getCharsetFromFontName($name) > $alternateName .= ' Bold'; { > } elseif ($index === 'xi') { switch ($name) { > $alternateName .= ' Italic'; // Add more cases. Check FONT records in real Excel files. > } elseif ($fontArray[$name]['xb'] === $fontArray[$name]['xbi']) { case 'EucrosiaUPC': > $alternateName .= ' Bold'; return self::CHARSET_ANSI_THAI; > } else { case 'Wingdings': > $alternateName .= ' Bold Italic'; return self::CHARSET_SYMBOL; > } case 'Wingdings 2': > } return self::CHARSET_SYMBOL; > $fontFile = self::$trueTypeFontPath . $separator . $alternateName . '.ttf';
case 'Wingdings 3':
> }
return self::CHARSET_SYMBOL;
> public const CHARSET_FROM_FONT_NAME = [ default: > 'EucrosiaUPC' => self::CHARSET_ANSI_THAI, return self::CHARSET_ANSI_LATIN; > 'Wingdings' => self::CHARSET_SYMBOL, } > 'Wingdings 2' => self::CHARSET_SYMBOL, } > 'Wingdings 3' => self::CHARSET_SYMBOL, > ]; /** >
< * @param string $name Font name
> * @param string $fontName Font name
< public static function getCharsetFromFontName($name)
> public static function getCharsetFromFontName($fontName)
< switch ($name) { < // 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; < }
> return self::CHARSET_FROM_FONT_NAME[$fontName] ?? self::CHARSET_ANSI_LATIN;
< * @param \PhpOffice\PhpSpreadsheet\Style\Font $font The workbooks default font < * @param bool $pPixels true = return column width in pixels, false = return in OOXML units
> * @param FontStyle $font The workbooks default font > * @param bool $returnAsPixels true = return column width in pixels, false = return in OOXML units
< public static function getDefaultColumnWidthByFont(\PhpOffice\PhpSpreadsheet\Style\Font $font, $pPixels = false)
> public static function getDefaultColumnWidthByFont(FontStyle $font, $returnAsPixels = false)
< if (isset(self::$defaultColumnWidths[$font->getName()][$font->getSize()])) {
> if (isset(self::DEFAULT_COLUMN_WIDTHS[$font->getName()][$font->getSize()])) {
< $columnWidth = $pPixels ? < self::$defaultColumnWidths[$font->getName()][$font->getSize()]['px'] < : self::$defaultColumnWidths[$font->getName()][$font->getSize()]['width'];
> $columnWidth = $returnAsPixels ? > self::DEFAULT_COLUMN_WIDTHS[$font->getName()][$font->getSize()]['px'] > : self::DEFAULT_COLUMN_WIDTHS[$font->getName()][$font->getSize()]['width'];
< $columnWidth = $pPixels ? < self::$defaultColumnWidths['Calibri'][11]['px'] < : self::$defaultColumnWidths['Calibri'][11]['width'];
> $columnWidth = $returnAsPixels ? > self::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['px'] > : self::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['width'];
< if ($pPixels) {
> if ($returnAsPixels) {
} } return $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 \PhpOffice\PhpSpreadsheet\Style\Font $font The workbooks default font
> * @param FontStyle $font The workbooks default font
* * @return float Row height in points */
< public static function getDefaultRowHeightByFont(\PhpOffice\PhpSpreadsheet\Style\Font $font)
> public static function getDefaultRowHeightByFont(FontStyle $font)
{
< switch ($font->getName()) { < case 'Arial': < switch ($font->getSize()) { < case 10: < // inspection of Arial 10 workbook says 12.75pt ~17px < $rowHeight = 12.75; < < break; < case 9: < // inspection of Arial 9 workbook says 12.00pt ~16px < $rowHeight = 12; < < break; < case 8: < // inspection of Arial 8 workbook says 11.25pt ~15px < $rowHeight = 11.25; < < break; < case 7: < // inspection of Arial 7 workbook says 9.00pt ~12px < $rowHeight = 9; < < break; < case 6: < case 5: < // inspection of Arial 5,6 workbook says 8.25pt ~11px < $rowHeight = 8.25; < < break; < case 4: < // inspection of Arial 4 workbook says 6.75pt ~9px < $rowHeight = 6.75; < < break; < case 3: < // inspection of Arial 3 workbook says 6.00pt ~8px < $rowHeight = 6; < < break; < case 2: < case 1: < // inspection of Arial 1,2 workbook says 5.25pt ~7px < $rowHeight = 5.25; < < break; < default: < // use Arial 10 workbook as an approximation, extrapolation < $rowHeight = 12.75 * $font->getSize() / 10; < < break; < } < < break; < case 'Calibri': < switch ($font->getSize()) { < case 11: < // inspection of Calibri 11 workbook says 15.00pt ~20px < $rowHeight = 15; < < break; < case 10: < // inspection of Calibri 10 workbook says 12.75pt ~17px < $rowHeight = 12.75; < < break; < case 9: < // inspection of Calibri 9 workbook says 12.00pt ~16px < $rowHeight = 12; < < break; < case 8: < // inspection of Calibri 8 workbook says 11.25pt ~15px < $rowHeight = 11.25; < < break; < case 7: < // inspection of Calibri 7 workbook says 9.00pt ~12px < $rowHeight = 9; < < break; < case 6: < case 5: < // inspection of Calibri 5,6 workbook says 8.25pt ~11px < $rowHeight = 8.25; < < break; < case 4: < // inspection of Calibri 4 workbook says 6.75pt ~9px < $rowHeight = 6.75; < < break; < case 3: < // inspection of Calibri 3 workbook says 6.00pt ~8px < $rowHeight = 6.00; < < break; < case 2: < case 1: < // inspection of Calibri 1,2 workbook says 5.25pt ~7px < $rowHeight = 5.25; < < break; < default: < // use Calibri 11 workbook as an approximation, extrapolation < $rowHeight = 15 * $font->getSize() / 11; < < break; < } < < break; < case 'Verdana': < switch ($font->getSize()) { < case 10: < // inspection of Verdana 10 workbook says 12.75pt ~17px < $rowHeight = 12.75; < < break; < case 9: < // inspection of Verdana 9 workbook says 11.25pt ~15px < $rowHeight = 11.25; < < break; < case 8: < // inspection of Verdana 8 workbook says 10.50pt ~14px < $rowHeight = 10.50; < < break; < case 7: < // inspection of Verdana 7 workbook says 9.00pt ~12px < $rowHeight = 9.00; < < break; < case 6: < case 5: < // inspection of Verdana 5,6 workbook says 8.25pt ~11px < $rowHeight = 8.25; < < break; < case 4: < // inspection of Verdana 4 workbook says 6.75pt ~9px < $rowHeight = 6.75; < < break; < case 3: < // inspection of Verdana 3 workbook says 6.00pt ~8px < $rowHeight = 6; < < break; < case 2: < case 1: < // inspection of Verdana 1,2 workbook says 5.25pt ~7px < $rowHeight = 5.25; < < break; < default: < // use Verdana 10 workbook as an approximation, extrapolation < $rowHeight = 12.75 * $font->getSize() / 10; < < break; < } < < break; < default: < // just use Calibri as an approximation < $rowHeight = 15 * $font->getSize() / 11; < < break;
> $name = $font->getName(); > $size = $font->getSize(); > if (isset(self::DEFAULT_COLUMN_WIDTHS[$name][$size])) { > $rowHeight = self::DEFAULT_COLUMN_WIDTHS[$name][$size]['height']; > } elseif ($name === 'Arial' || $name === 'Verdana') { > $rowHeight = self::DEFAULT_COLUMN_WIDTHS[$name][10]['height'] * $size / 10.0; > } else { > $rowHeight = self::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['height'] * $size / 11.0;
} return $rowHeight; } }