16 self::AUTOSIZE_METHOD_APPROX,
17 self::AUTOSIZE_METHOD_EXACT,
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;
188 return self::$autoSizeMethod;
204 self::$trueTypeFontPath = $pValue;
214 return self::$trueTypeFontPath;
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) {
239 $lineWidths[] = self::calculateColumnWidth($font, $lineText, $rotation = 0, $defaultFont);
242 return max($lineWidths);
246 $approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX;
249 $columnWidthAdjust = ceil(self::getTextWidthPixelsExact(
'n', $font, 0) * 1.07);
254 $columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation) + $columnWidthAdjust;
261 $columnWidthAdjust = self::getTextWidthPixelsApprox(
'n', $font, 0);
264 $columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation) + $columnWidthAdjust;
271 return (
int) round($columnWidth, 6);
279 if (!function_exists(
'imagettfbbox')) {
285 $fontFile = self::getTrueTypeFontFileFromFont($font);
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)) {
404 $name = $font->getName();
405 $bold = $font->getBold();
406 $italic = $font->getItalic();
412 $bold ? ($italic ? self::ARIAL_BOLD_ITALIC : self::ARIAL_BOLD)
413 : ($italic ? self::ARIAL_ITALIC : self::ARIAL)
419 $bold ? ($italic ? self::CALIBRI_BOLD_ITALIC : self::CALIBRI_BOLD)
420 : ($italic ? self::CALIBRI_ITALIC : self::CALIBRI)
426 $bold ? ($italic ? self::COURIER_NEW_BOLD_ITALIC : self::COURIER_NEW_BOLD)
427 : ($italic ? self::COURIER_NEW_ITALIC : self::COURIER_NEW)
431 case 'Comic Sans MS':
433 $bold ? self::COMIC_SANS_MS_BOLD : self::COMIC_SANS_MS
439 $bold ? ($italic ? self::GEORGIA_BOLD_ITALIC : self::GEORGIA_BOLD)
440 : ($italic ? self::GEORGIA_ITALIC : self::GEORGIA)
445 $fontFile = self::IMPACT;
448 case 'Liberation Sans':
450 $bold ? ($italic ? self::LIBERATION_SANS_BOLD_ITALIC : self::LIBERATION_SANS_BOLD)
451 : ($italic ? self::LIBERATION_SANS_ITALIC : self::LIBERATION_SANS)
455 case 'Lucida Console':
456 $fontFile = self::LUCIDA_CONSOLE;
459 case 'Lucida Sans Unicode':
460 $fontFile = self::LUCIDA_SANS_UNICODE;
463 case 'Microsoft Sans Serif':
464 $fontFile = self::MICROSOFT_SANS_SERIF;
467 case 'Palatino Linotype':
469 $bold ? ($italic ? self::PALATINO_LINOTYPE_BOLD_ITALIC : self::PALATINO_LINOTYPE_BOLD)
470 : ($italic ? self::PALATINO_LINOTYPE_ITALIC : self::PALATINO_LINOTYPE)
475 $fontFile = self::SYMBOL;
480 $bold ? self::TAHOMA_BOLD : self::TAHOMA
484 case 'Times New Roman':
486 $bold ? ($italic ? self::TIMES_NEW_ROMAN_BOLD_ITALIC : self::TIMES_NEW_ROMAN_BOLD)
487 : ($italic ? self::TIMES_NEW_ROMAN_ITALIC : self::TIMES_NEW_ROMAN)
493 $bold ? ($italic ? self::TREBUCHET_MS_BOLD_ITALIC : self::TREBUCHET_MS_BOLD)
494 : ($italic ? self::TREBUCHET_MS_ITALIC : self::TREBUCHET_MS)
500 $bold ? ($italic ? self::VERDANA_BOLD_ITALIC : self::VERDANA_BOLD)
501 : ($italic ? self::VERDANA_ITALIC : self::VERDANA)
511 $fontFile = self::$trueTypeFontPath . $fontFile;
514 if (!file_exists($fontFile)) {
533 return self::CHARSET_ANSI_THAI;
535 return self::CHARSET_SYMBOL;
537 return self::CHARSET_SYMBOL;
539 return self::CHARSET_SYMBOL;
541 return self::CHARSET_ANSI_LATIN;
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;
const CHARSET_ANSI_ARABIC
const CHARSET_OEM_LATIN_I
const CHARSET_ANSI_CHINESE_SIMIPLIFIED
static static setAutoSizeMethod($pValue)
Set autoSize method.
const PALATINO_LINOTYPE_BOLD_ITALIC
const CALIBRI_BOLD_ITALIC
const CHARSET_APPLE_ROMAN
const CHARSET_ANSI_BALTIC
const ARIAL
Font filenames.
const CHARSET_ANSI_CHINESE_TRADITIONAL
const TIMES_NEW_ROMAN_ITALIC
const CHARSET_ANSI_LATIN
Character set codes used by BIFF5-8 in Font records.
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...
const LUCIDA_SANS_UNICODE
static countCharacters($value, $enc='UTF-8')
Get character count.
static getCharsetFromFontName($name)
Returns the associated charset for the font name.
const LIBERATION_SANS_BOLD
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...
const PALATINO_LINOTYPE_BOLD
const CHARSET_ANSI_JAPANESE_SHIFTJIS
const CHARSET_ANSI_TURKISH
const CHARSET_SYSTEM_DEFAULT
const LIBERATION_SANS_ITALIC
static centimeterSizeToPixels($sizeInCm)
Calculate an (approximate) pixel size, based on centimeter size.
const PALATINO_LINOTYPE_ITALIC
const TEXTROTATION_STACK_PHPSPREADSHEET
static inchSizeToPixels($sizeInInch)
Calculate an (approximate) pixel size, based on inch size.
static setTrueTypeFontPath($pValue)
Set the path to the folder containing .ttf files.
const GEORGIA_BOLD_ITALIC
const CHARSET_ANSI_KOREAN_JOHAB
const MICROSOFT_SANS_SERIF
const CHARSET_ANSI_VIETNAMESE
const TIMES_NEW_ROMAN_BOLD
static getTrueTypeFontPath()
Get the path to the folder containing .ttf files.
static getAutoSizeMethod()
Get autoSize method.
static fontSizeToPixels($fontSizeInPoints)
Calculate an (approximate) pixel size, based on a font points size.
const AUTOSIZE_METHOD_APPROX
static pixelsToCellDimension($pValue, \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont)
Convert pixels to column width.
static $defaultColumnWidths
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 CHARSET_ANSI_CYRILLIC
const LIBERATION_SANS_BOLD_ITALIC
const AUTOSIZE_METHOD_EXACT
const CHARSET_ANSI_LATIN_II
static getTrueTypeFontFileFromFont($font)
Returns the font path given the font.
const VERDANA_BOLD_ITALIC
const TREBUCHET_MS_ITALIC
const CHARSET_ANSI_HEBREW
const TREBUCHET_MS_BOLD_ITALIC
const COURIER_NEW_BOLD_ITALIC
const CHARSET_ANSI_KOREAN_HANGUL
const TIMES_NEW_ROMAN_BOLD_ITALIC
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 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.