Font.php 23 KB

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