Font.php 29 KB

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