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