Font.php 26 KB

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