PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Shared/Font.php

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