No Description

Font.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 ##VERSION##, ##DATE##
  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. $lineWidths = 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. $approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX;
  240. if (!$approximate) {
  241. $columnWidthAdjust = ceil(self::getTextWidthPixelsExact('n', $font, 0) * 1.07);
  242. try {
  243. // Width of text in pixels excl. padding
  244. // and addition because Excel adds some padding, just use approx width of 'n' glyph
  245. $columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation) + $columnWidthAdjust;
  246. } catch (PHPExcel_Exception $e) {
  247. $approximate = true;
  248. }
  249. }
  250. if ($approximate) {
  251. $columnWidthAdjust = self::getTextWidthPixelsApprox('n', $font, 0);
  252. // Width of text in pixels excl. padding, approximation
  253. // and addition because Excel adds some padding, just use approx width of 'n' glyph
  254. $columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation) + $columnWidthAdjust;
  255. }
  256. // Convert from pixel width to column width
  257. $columnWidth = PHPExcel_Shared_Drawing::pixelsToCellDimension($columnWidth, $defaultFont);
  258. // Return
  259. return round($columnWidth, 6);
  260. }
  261. /**
  262. * Get GD text width in pixels for a string of text in a certain font at a certain rotation angle
  263. *
  264. * @param string $text
  265. * @param PHPExcel_Style_Font
  266. * @param int $rotation
  267. * @return int
  268. * @throws PHPExcel_Exception
  269. */
  270. public static function getTextWidthPixelsExact($text, PHPExcel_Style_Font $font, $rotation = 0) {
  271. if (!function_exists('imagettfbbox')) {
  272. throw new PHPExcel_Exception('GD library needs to be enabled');
  273. }
  274. // font size should really be supplied in pixels in GD2,
  275. // but since GD2 seems to assume 72dpi, pixels and points are the same
  276. $fontFile = self::getTrueTypeFontFileFromFont($font);
  277. $textBox = imagettfbbox($font->getSize(), $rotation, $fontFile, $text);
  278. // Get corners positions
  279. $lowerLeftCornerX = $textBox[0];
  280. // $lowerLeftCornerY = $textBox[1];
  281. $lowerRightCornerX = $textBox[2];
  282. // $lowerRightCornerY = $textBox[3];
  283. $upperRightCornerX = $textBox[4];
  284. // $upperRightCornerY = $textBox[5];
  285. $upperLeftCornerX = $textBox[6];
  286. // $upperLeftCornerY = $textBox[7];
  287. // Consider the rotation when calculating the width
  288. $textWidth = max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX);
  289. return $textWidth;
  290. }
  291. /**
  292. * Get approximate width in pixels for a string of text in a certain font at a certain rotation angle
  293. *
  294. * @param string $columnText
  295. * @param PHPExcel_Style_Font $font
  296. * @param int $rotation
  297. * @return int Text width in pixels (no padding added)
  298. */
  299. public static function getTextWidthPixelsApprox($columnText, PHPExcel_Style_Font $font = null, $rotation = 0)
  300. {
  301. $fontName = $font->getName();
  302. $fontSize = $font->getSize();
  303. // Calculate column width in pixels. We assume fixed glyph width. Result varies with font name and size.
  304. switch ($fontName) {
  305. case 'Calibri':
  306. // value 8.26 was found via interpolation by inspecting real Excel files with Calibri 11 font.
  307. $columnWidth = (int) (8.26 * PHPExcel_Shared_String::CountCharacters($columnText));
  308. $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size
  309. break;
  310. case 'Arial':
  311. // value 7 was found via interpolation by inspecting real Excel files with Arial 10 font.
  312. // $columnWidth = (int) (7 * PHPExcel_Shared_String::CountCharacters($columnText));
  313. // value 8 was set because of experience in different exports at Arial 10 font.
  314. $columnWidth = (int) (8 * PHPExcel_Shared_String::CountCharacters($columnText));
  315. $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size
  316. break;
  317. case 'Verdana':
  318. // value 8 was found via interpolation by inspecting real Excel files with Verdana 10 font.
  319. $columnWidth = (int) (8 * PHPExcel_Shared_String::CountCharacters($columnText));
  320. $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size
  321. break;
  322. default:
  323. // just assume Calibri
  324. $columnWidth = (int) (8.26 * PHPExcel_Shared_String::CountCharacters($columnText));
  325. $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size
  326. break;
  327. }
  328. // Calculate approximate rotated column width
  329. if ($rotation !== 0) {
  330. if ($rotation == -165) {
  331. // stacked text
  332. $columnWidth = 4; // approximation
  333. } else {
  334. // rotated text
  335. $columnWidth = $columnWidth * cos(deg2rad($rotation))
  336. + $fontSize * abs(sin(deg2rad($rotation))) / 5; // approximation
  337. }
  338. }
  339. // pixel width is an integer
  340. return (int) $columnWidth;
  341. }
  342. /**
  343. * Calculate an (approximate) pixel size, based on a font points size
  344. *
  345. * @param int $fontSizeInPoints Font size (in points)
  346. * @return int Font size (in pixels)
  347. */
  348. public static function fontSizeToPixels($fontSizeInPoints = 11) {
  349. return (int) ((4 / 3) * $fontSizeInPoints);
  350. }
  351. /**
  352. * Calculate an (approximate) pixel size, based on inch size
  353. *
  354. * @param int $sizeInInch Font size (in inch)
  355. * @return int Size (in pixels)
  356. */
  357. public static function inchSizeToPixels($sizeInInch = 1) {
  358. return ($sizeInInch * 96);
  359. }
  360. /**
  361. * Calculate an (approximate) pixel size, based on centimeter size
  362. *
  363. * @param int $sizeInCm Font size (in centimeters)
  364. * @return int Size (in pixels)
  365. */
  366. public static function centimeterSizeToPixels($sizeInCm = 1) {
  367. return ($sizeInCm * 37.795275591);
  368. }
  369. /**
  370. * Returns the font path given the font
  371. *
  372. * @param PHPExcel_Style_Font
  373. * @return string Path to TrueType font file
  374. */
  375. public static function getTrueTypeFontFileFromFont($font) {
  376. if (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath)) {
  377. throw new PHPExcel_Exception('Valid directory to TrueType Font files not specified');
  378. }
  379. $name = $font->getName();
  380. $bold = $font->getBold();
  381. $italic = $font->getItalic();
  382. // Check if we can map font to true type font file
  383. switch ($name) {
  384. case 'Arial':
  385. $fontFile = (
  386. $bold ? ($italic ? self::ARIAL_BOLD_ITALIC : self::ARIAL_BOLD)
  387. : ($italic ? self::ARIAL_ITALIC : self::ARIAL)
  388. );
  389. break;
  390. case 'Calibri':
  391. $fontFile = (
  392. $bold ? ($italic ? self::CALIBRI_BOLD_ITALIC : self::CALIBRI_BOLD)
  393. : ($italic ? self::CALIBRI_ITALIC : self::CALIBRI)
  394. );
  395. break;
  396. case 'Courier New':
  397. $fontFile = (
  398. $bold ? ($italic ? self::COURIER_NEW_BOLD_ITALIC : self::COURIER_NEW_BOLD)
  399. : ($italic ? self::COURIER_NEW_ITALIC : self::COURIER_NEW)
  400. );
  401. break;
  402. case 'Comic Sans MS':
  403. $fontFile = (
  404. $bold ? self::COMIC_SANS_MS_BOLD : self::COMIC_SANS_MS
  405. );
  406. break;
  407. case 'Georgia':
  408. $fontFile = (
  409. $bold ? ($italic ? self::GEORGIA_BOLD_ITALIC : self::GEORGIA_BOLD)
  410. : ($italic ? self::GEORGIA_ITALIC : self::GEORGIA)
  411. );
  412. break;
  413. case 'Impact':
  414. $fontFile = self::IMPACT;
  415. break;
  416. case 'Liberation Sans':
  417. $fontFile = (
  418. $bold ? ($italic ? self::LIBERATION_SANS_BOLD_ITALIC : self::LIBERATION_SANS_BOLD)
  419. : ($italic ? self::LIBERATION_SANS_ITALIC : self::LIBERATION_SANS)
  420. );
  421. break;
  422. case 'Lucida Console':
  423. $fontFile = self::LUCIDA_CONSOLE;
  424. break;
  425. case 'Lucida Sans Unicode':
  426. $fontFile = self::LUCIDA_SANS_UNICODE;
  427. break;
  428. case 'Microsoft Sans Serif':
  429. $fontFile = self::MICROSOFT_SANS_SERIF;
  430. break;
  431. case 'Palatino Linotype':
  432. $fontFile = (
  433. $bold ? ($italic ? self::PALATINO_LINOTYPE_BOLD_ITALIC : self::PALATINO_LINOTYPE_BOLD)
  434. : ($italic ? self::PALATINO_LINOTYPE_ITALIC : self::PALATINO_LINOTYPE)
  435. );
  436. break;
  437. case 'Symbol':
  438. $fontFile = self::SYMBOL;
  439. break;
  440. case 'Tahoma':
  441. $fontFile = (
  442. $bold ? self::TAHOMA_BOLD : self::TAHOMA
  443. );
  444. break;
  445. case 'Times New Roman':
  446. $fontFile = (
  447. $bold ? ($italic ? self::TIMES_NEW_ROMAN_BOLD_ITALIC : self::TIMES_NEW_ROMAN_BOLD)
  448. : ($italic ? self::TIMES_NEW_ROMAN_ITALIC : self::TIMES_NEW_ROMAN)
  449. );
  450. break;
  451. case 'Trebuchet MS':
  452. $fontFile = (
  453. $bold ? ($italic ? self::TREBUCHET_MS_BOLD_ITALIC : self::TREBUCHET_MS_BOLD)
  454. : ($italic ? self::TREBUCHET_MS_ITALIC : self::TREBUCHET_MS)
  455. );
  456. break;
  457. case 'Verdana':
  458. $fontFile = (
  459. $bold ? ($italic ? self::VERDANA_BOLD_ITALIC : self::VERDANA_BOLD)
  460. : ($italic ? self::VERDANA_ITALIC : self::VERDANA)
  461. );
  462. break;
  463. default:
  464. throw new PHPExcel_Exception('Unknown font name "'. $name .'". Cannot map to TrueType font file');
  465. break;
  466. }
  467. $fontFile = self::$trueTypeFontPath . $fontFile;
  468. // Check if file actually exists
  469. if (!file_exists($fontFile)) {
  470. throw New PHPExcel_Exception('TrueType Font file not found');
  471. }
  472. return $fontFile;
  473. }
  474. /**
  475. * Returns the associated charset for the font name.
  476. *
  477. * @param string $name Font name
  478. * @return int Character set code
  479. */
  480. public static function getCharsetFromFontName($name)
  481. {
  482. switch ($name) {
  483. // Add more cases. Check FONT records in real Excel files.
  484. case 'EucrosiaUPC': return self::CHARSET_ANSI_THAI;
  485. case 'Wingdings': return self::CHARSET_SYMBOL;
  486. case 'Wingdings 2': return self::CHARSET_SYMBOL;
  487. case 'Wingdings 3': return self::CHARSET_SYMBOL;
  488. default: return self::CHARSET_ANSI_LATIN;
  489. }
  490. }
  491. /**
  492. * Get the effective column width for columns without a column dimension or column with width -1
  493. * For example, for Calibri 11 this is 9.140625 (64 px)
  494. *
  495. * @param PHPExcel_Style_Font $font The workbooks default font
  496. * @param boolean $pPixels true = return column width in pixels, false = return in OOXML units
  497. * @return mixed Column width
  498. */
  499. public static function getDefaultColumnWidthByFont(PHPExcel_Style_Font $font, $pPixels = false)
  500. {
  501. if (isset(self::$defaultColumnWidths[$font->getName()][$font->getSize()])) {
  502. // Exact width can be determined
  503. $columnWidth = $pPixels ?
  504. self::$defaultColumnWidths[$font->getName()][$font->getSize()]['px']
  505. : self::$defaultColumnWidths[$font->getName()][$font->getSize()]['width'];
  506. } else {
  507. // We don't have data for this particular font and size, use approximation by
  508. // extrapolating from Calibri 11
  509. $columnWidth = $pPixels ?
  510. self::$defaultColumnWidths['Calibri'][11]['px']
  511. : self::$defaultColumnWidths['Calibri'][11]['width'];
  512. $columnWidth = $columnWidth * $font->getSize() / 11;
  513. // Round pixels to closest integer
  514. if ($pPixels) {
  515. $columnWidth = (int) round($columnWidth);
  516. }
  517. }
  518. return $columnWidth;
  519. }
  520. /**
  521. * Get the effective row height for rows without a row dimension or rows with height -1
  522. * For example, for Calibri 11 this is 15 points
  523. *
  524. * @param PHPExcel_Style_Font $font The workbooks default font
  525. * @return float Row height in points
  526. */
  527. public static function getDefaultRowHeightByFont(PHPExcel_Style_Font $font)
  528. {
  529. switch ($font->getName()) {
  530. case 'Arial':
  531. switch ($font->getSize()) {
  532. case 10:
  533. // inspection of Arial 10 workbook says 12.75pt ~17px
  534. $rowHeight = 12.75;
  535. break;
  536. case 9:
  537. // inspection of Arial 9 workbook says 12.00pt ~16px
  538. $rowHeight = 12;
  539. break;
  540. case 8:
  541. // inspection of Arial 8 workbook says 11.25pt ~15px
  542. $rowHeight = 11.25;
  543. break;
  544. case 7:
  545. // inspection of Arial 7 workbook says 9.00pt ~12px
  546. $rowHeight = 9;
  547. break;
  548. case 6:
  549. case 5:
  550. // inspection of Arial 5,6 workbook says 8.25pt ~11px
  551. $rowHeight = 8.25;
  552. break;
  553. case 4:
  554. // inspection of Arial 4 workbook says 6.75pt ~9px
  555. $rowHeight = 6.75;
  556. break;
  557. case 3:
  558. // inspection of Arial 3 workbook says 6.00pt ~8px
  559. $rowHeight = 6;
  560. break;
  561. case 2:
  562. case 1:
  563. // inspection of Arial 1,2 workbook says 5.25pt ~7px
  564. $rowHeight = 5.25;
  565. break;
  566. default:
  567. // use Arial 10 workbook as an approximation, extrapolation
  568. $rowHeight = 12.75 * $font->getSize() / 10;
  569. break;
  570. }
  571. break;
  572. case 'Calibri':
  573. switch ($font->getSize()) {
  574. case 11:
  575. // inspection of Calibri 11 workbook says 15.00pt ~20px
  576. $rowHeight = 15;
  577. break;
  578. case 10:
  579. // inspection of Calibri 10 workbook says 12.75pt ~17px
  580. $rowHeight = 12.75;
  581. break;
  582. case 9:
  583. // inspection of Calibri 9 workbook says 12.00pt ~16px
  584. $rowHeight = 12;
  585. break;
  586. case 8:
  587. // inspection of Calibri 8 workbook says 11.25pt ~15px
  588. $rowHeight = 11.25;
  589. break;
  590. case 7:
  591. // inspection of Calibri 7 workbook says 9.00pt ~12px
  592. $rowHeight = 9;
  593. break;
  594. case 6:
  595. case 5:
  596. // inspection of Calibri 5,6 workbook says 8.25pt ~11px
  597. $rowHeight = 8.25;
  598. break;
  599. case 4:
  600. // inspection of Calibri 4 workbook says 6.75pt ~9px
  601. $rowHeight = 6.75;
  602. break;
  603. case 3:
  604. // inspection of Calibri 3 workbook says 6.00pt ~8px
  605. $rowHeight = 6.00;
  606. break;
  607. case 2:
  608. case 1:
  609. // inspection of Calibri 1,2 workbook says 5.25pt ~7px
  610. $rowHeight = 5.25;
  611. break;
  612. default:
  613. // use Calibri 11 workbook as an approximation, extrapolation
  614. $rowHeight = 15 * $font->getSize() / 11;
  615. break;
  616. }
  617. break;
  618. case 'Verdana':
  619. switch ($font->getSize()) {
  620. case 10:
  621. // inspection of Verdana 10 workbook says 12.75pt ~17px
  622. $rowHeight = 12.75;
  623. break;
  624. case 9:
  625. // inspection of Verdana 9 workbook says 11.25pt ~15px
  626. $rowHeight = 11.25;
  627. break;
  628. case 8:
  629. // inspection of Verdana 8 workbook says 10.50pt ~14px
  630. $rowHeight = 10.50;
  631. break;
  632. case 7:
  633. // inspection of Verdana 7 workbook says 9.00pt ~12px
  634. $rowHeight = 9.00;
  635. break;
  636. case 6:
  637. case 5:
  638. // inspection of Verdana 5,6 workbook says 8.25pt ~11px
  639. $rowHeight = 8.25;
  640. break;
  641. case 4:
  642. // inspection of Verdana 4 workbook says 6.75pt ~9px
  643. $rowHeight = 6.75;
  644. break;
  645. case 3:
  646. // inspection of Verdana 3 workbook says 6.00pt ~8px
  647. $rowHeight = 6;
  648. break;
  649. case 2:
  650. case 1:
  651. // inspection of Verdana 1,2 workbook says 5.25pt ~7px
  652. $rowHeight = 5.25;
  653. break;
  654. default:
  655. // use Verdana 10 workbook as an approximation, extrapolation
  656. $rowHeight = 12.75 * $font->getSize() / 10;
  657. break;
  658. }
  659. break;
  660. default:
  661. // just use Calibri as an approximation
  662. $rowHeight = 15 * $font->getSize() / 11;
  663. break;
  664. }
  665. return $rowHeight;
  666. }
  667. }