126 1 => [
'px' => 24,
'width' => 12.00000000],
127 2 => [
'px' => 24,
'width' => 12.00000000],
128 3 => [
'px' => 32,
'width' => 10.66406250],
129 4 => [
'px' => 32,
'width' => 10.66406250],
130 5 => [
'px' => 40,
'width' => 10.00000000],
131 6 => [
'px' => 48,
'width' => 9.59765625],
132 7 => [
'px' => 48,
'width' => 9.59765625],
133 8 => [
'px' => 56,
'width' => 9.33203125],
134 9 => [
'px' => 64,
'width' => 9.14062500],
135 10 => [
'px' => 64,
'width' => 9.14062500],
138 1 => [
'px' => 24,
'width' => 12.00000000],
139 2 => [
'px' => 24,
'width' => 12.00000000],
140 3 => [
'px' => 32,
'width' => 10.66406250],
141 4 => [
'px' => 32,
'width' => 10.66406250],
142 5 => [
'px' => 40,
'width' => 10.00000000],
143 6 => [
'px' => 48,
'width' => 9.59765625],
144 7 => [
'px' => 48,
'width' => 9.59765625],
145 8 => [
'px' => 56,
'width' => 9.33203125],
146 9 => [
'px' => 56,
'width' => 9.33203125],
147 10 => [
'px' => 64,
'width' => 9.14062500],
148 11 => [
'px' => 64,
'width' => 9.14062500],
151 1 => [
'px' => 24,
'width' => 12.00000000],
152 2 => [
'px' => 24,
'width' => 12.00000000],
153 3 => [
'px' => 32,
'width' => 10.66406250],
154 4 => [
'px' => 32,
'width' => 10.66406250],
155 5 => [
'px' => 40,
'width' => 10.00000000],
156 6 => [
'px' => 48,
'width' => 9.59765625],
157 7 => [
'px' => 48,
'width' => 9.59765625],
158 8 => [
'px' => 64,
'width' => 9.14062500],
159 9 => [
'px' => 72,
'width' => 9.00000000],
160 10 => [
'px' => 72,
'width' => 9.00000000],
173 if (!in_array($pValue, self::$autoSizeMethods)) {
176 self::$autoSizeMethod = $pValue;
204 self::$trueTypeFontPath = $pValue;
230 if ($cellText instanceof
RichText) {
231 $cellText = $cellText->getPlainText();
235 if (strpos($cellText ??
'',
"\n") !==
false) {
236 $lineTexts = explode(
"\n", $cellText);
238 foreach ($lineTexts as $lineText) {
242 return max($lineWidths);
249 $columnWidthAdjust = ceil(self::getTextWidthPixelsExact(
'n', $font, 0) * 1.07);
255 }
catch (PhpSpreadsheetException $e) {
271 return (
int) round($columnWidth, 6);
279 if (!function_exists(
'imagettfbbox')) {
280 throw new PhpSpreadsheetException(
'GD library needs to be enabled');
286 $textBox = imagettfbbox($font->getSize(), $rotation, $fontFile,
$text);
289 $lowerLeftCornerX = $textBox[0];
290 $lowerRightCornerX = $textBox[2];
291 $upperRightCornerX = $textBox[4];
292 $upperLeftCornerX = $textBox[6];
295 return max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX);
308 $fontName = $font->getName();
309 $fontSize = $font->getSize();
316 $columnWidth = $columnWidth * $fontSize / 11;
322 $columnWidth = $columnWidth * $fontSize / 10;
328 $columnWidth = $columnWidth * $fontSize / 10;
334 $columnWidth = $columnWidth * $fontSize / 11;
340 if ($rotation !== 0) {
346 $columnWidth = $columnWidth * cos(deg2rad($rotation))
347 + $fontSize * abs(sin(deg2rad($rotation))) / 5;
352 return (
int) $columnWidth;
364 return (
int) ((4 / 3) * $fontSizeInPoints);
376 return $sizeInInch * 96;
388 return $sizeInCm * 37.795275591;
400 if (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath)) {
401 throw new PhpSpreadsheetException(
'Valid directory to TrueType Font files not specified');
404 $name = $font->getName();
405 $bold = $font->getBold();
406 $italic = $font->getItalic();
413 : ($italic ? self::ARIAL_ITALIC : self::ARIAL)
420 : ($italic ? self::CALIBRI_ITALIC : self::CALIBRI)
427 : ($italic ? self::COURIER_NEW_ITALIC : self::COURIER_NEW)
431 case 'Comic Sans MS':
440 : ($italic ? self::GEORGIA_ITALIC : self::GEORGIA)
448 case 'Liberation Sans':
451 : ($italic ? self::LIBERATION_SANS_ITALIC : self::LIBERATION_SANS)
455 case 'Lucida Console':
459 case 'Lucida Sans Unicode':
463 case 'Microsoft Sans Serif':
467 case 'Palatino Linotype':
470 : ($italic ? self::PALATINO_LINOTYPE_ITALIC : self::PALATINO_LINOTYPE)
484 case 'Times New Roman':
487 : ($italic ? self::TIMES_NEW_ROMAN_ITALIC : self::TIMES_NEW_ROMAN)
494 : ($italic ? self::TREBUCHET_MS_ITALIC : self::TREBUCHET_MS)
501 : ($italic ? self::VERDANA_ITALIC : self::VERDANA)
506 throw new PhpSpreadsheetException(
'Unknown font name "' .
$name .
'". Cannot map to TrueType font file');
511 $fontFile = self::$trueTypeFontPath . $fontFile;
514 if (!file_exists($fontFile)) {
515 throw new PhpSpreadsheetException(
'TrueType Font file not found');
556 if (isset(self::$defaultColumnWidths[$font->getName()][$font->getSize()])) {
558 $columnWidth = $pPixels ?
559 self::$defaultColumnWidths[$font->getName()][$font->getSize()][
'px']
560 : self::$defaultColumnWidths[$font->getName()][$font->getSize()][
'width'];
564 $columnWidth = $pPixels ?
565 self::$defaultColumnWidths[
'Calibri'][11][
'px']
566 : self::$defaultColumnWidths[
'Calibri'][11][
'width'];
567 $columnWidth = $columnWidth * $font->getSize() / 11;
571 $columnWidth = (int) round($columnWidth);
588 switch ($font->getName()) {
590 switch ($font->getSize()) {
635 $rowHeight = 12.75 * $font->getSize() / 10;
642 switch ($font->getSize()) {
692 $rowHeight = 15 * $font->getSize() / 11;
699 switch ($font->getSize()) {
744 $rowHeight = 12.75 * $font->getSize() / 10;
752 $rowHeight = 15 * $font->getSize() / 11;
An exception for terminatinating execution or to throw for unit testing.
static pixelsToCellDimension($pValue, \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont)
Convert pixels to column width.
const COURIER_NEW_BOLD_ITALIC
const CHARSET_ANSI_KOREAN_HANGUL
const CHARSET_OEM_LATIN_I
const CHARSET_APPLE_ROMAN
const CHARSET_ANSI_VIETNAMESE
const CHARSET_ANSI_CHINESE_SIMIPLIFIED
const CHARSET_ANSI_LATIN_II
static getDefaultColumnWidthByFont(\PhpOffice\PhpSpreadsheet\Style\Font $font, $pPixels=false)
Get the effective column width for columns without a column dimension or column with width -1 For exa...
static getAutoSizeMethod()
Get autoSize method.
const CHARSET_ANSI_TURKISH
const GEORGIA_BOLD_ITALIC
const CHARSET_SYSTEM_DEFAULT
const VERDANA_BOLD_ITALIC
static inchSizeToPixels($sizeInInch)
Calculate an (approximate) pixel size, based on inch size.
const PALATINO_LINOTYPE_ITALIC
static calculateColumnWidth(\PhpOffice\PhpSpreadsheet\Style\Font $font, $cellText='', $rotation=0, ?\PhpOffice\PhpSpreadsheet\Style\Font $defaultFont=null)
Calculate an (approximate) OpenXML column width, based on font size and text contained.
static getTrueTypeFontPath()
Get the path to the folder containing .ttf files.
const TIMES_NEW_ROMAN_BOLD
static getTextWidthPixelsApprox($columnText, \PhpOffice\PhpSpreadsheet\Style\Font $font, $rotation=0)
Get approximate width in pixels for a string of text in a certain font at a certain rotation angle.
const LIBERATION_SANS_BOLD
static getTrueTypeFontFileFromFont($font)
Returns the font path given the font.
const CHARSET_ANSI_BALTIC
const TIMES_NEW_ROMAN_BOLD_ITALIC
const CHARSET_ANSI_JAPANESE_SHIFTJIS
static centimeterSizeToPixels($sizeInCm)
Calculate an (approximate) pixel size, based on centimeter size.
const CHARSET_ANSI_HEBREW
const PALATINO_LINOTYPE_BOLD
const CALIBRI_BOLD_ITALIC
static getTextWidthPixelsExact(string $text, \PhpOffice\PhpSpreadsheet\Style\Font $font, int $rotation=0)
Get GD text width in pixels for a string of text in a certain font at a certain rotation angle.
static getDefaultRowHeightByFont(\PhpOffice\PhpSpreadsheet\Style\Font $font)
Get the effective row height for rows without a row dimension or rows with height -1 For example,...
static fontSizeToPixels($fontSizeInPoints)
Calculate an (approximate) pixel size, based on a font points size.
const TIMES_NEW_ROMAN_ITALIC
const CHARSET_ANSI_CHINESE_TRADITIONAL
static setAutoSizeMethod($pValue)
Set autoSize method.
const LIBERATION_SANS_BOLD_ITALIC
const CHARSET_ANSI_ARABIC
const CHARSET_ANSI_CYRILLIC
const TREBUCHET_MS_ITALIC
const PALATINO_LINOTYPE_BOLD_ITALIC
const LIBERATION_SANS_ITALIC
static setTrueTypeFontPath($pValue)
Set the path to the folder containing .ttf files.
const LUCIDA_SANS_UNICODE
const CHARSET_ANSI_LATIN
Character set codes used by BIFF5-8 in Font records.
const MICROSOFT_SANS_SERIF
const AUTOSIZE_METHOD_EXACT
static getCharsetFromFontName($name)
Returns the associated charset for the font name.
const CHARSET_ANSI_KOREAN_JOHAB
const ARIAL
Font filenames.
const TREBUCHET_MS_BOLD_ITALIC
static $defaultColumnWidths
const AUTOSIZE_METHOD_APPROX
static countCharacters($value, $enc='UTF-8')
Get character count.
const TEXTROTATION_STACK_PHPSPREADSHEET