PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.0.3
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.0.3
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
visualizer / vendor / phpoffice / phpexcel / Classes / PHPExcel / Shared / Font.php

Font.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.0.3, at vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/Font.php

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