For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

741 lines
29 KiB

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