Font.php 26 KB

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