source: pro-violet-viettel/www/deploy/20150304/application/libraries/PHPExcel/Shared/Font.php @ 803

Last change on this file since 803 was 780, checked in by dungnv, 10 years ago
File size: 22.5 KB
Line 
1<?php
2/**
3 * PHPExcel
4 *
5 * Copyright (c) 2006 - 2014 PHPExcel
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 *
21 * @category   PHPExcel
22 * @package    PHPExcel_Shared
23 * @copyright  Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
25 * @version    1.8.0, 2014-03-02
26 */
27
28
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 */
36class 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
203                self::$autoSizeMethod = $pValue;
204
205                return TRUE;
206        }
207
208        /**
209         * Get autoSize method
210         *
211         * @return string
212         */
213        public static function getAutoSizeMethod()
214        {
215                return self::$autoSizeMethod;
216        }
217
218        /**
219         * Set the path to the folder containing .ttf files. There should be a trailing slash.
220         * Typical locations on variout some platforms:
221         *      <ul>
222         *              <li>C:/Windows/Fonts/</li>
223         *              <li>/usr/share/fonts/truetype/</li>
224         *              <li>~/.fonts/</li>
225         *      </ul>
226         *
227         * @param string $pValue
228         */
229        public static function setTrueTypeFontPath($pValue = '')
230        {
231                self::$trueTypeFontPath = $pValue;
232        }
233
234        /**
235         * Get the path to the folder containing .ttf files.
236         *
237         * @return string
238         */
239        public static function getTrueTypeFontPath()
240        {
241                return self::$trueTypeFontPath;
242        }
243
244        /**
245         * Calculate an (approximate) OpenXML column width, based on font size and text contained
246         *
247         * @param       PHPExcel_Style_Font                     $font                   Font object
248         * @param       PHPExcel_RichText|string        $cellText               Text to calculate width
249         * @param       integer                                         $rotation               Rotation angle
250         * @param       PHPExcel_Style_Font|NULL        $defaultFont    Font object
251         * @return      integer         Column width
252         */
253        public static function calculateColumnWidth(PHPExcel_Style_Font $font, $cellText = '', $rotation = 0, PHPExcel_Style_Font $defaultFont = null) {
254
255                // If it is rich text, use plain text
256                if ($cellText instanceof PHPExcel_RichText) {
257                        $cellText = $cellText->getPlainText();
258                }
259
260                // Special case if there are one or more newline characters ("\n")
261                if (strpos($cellText, "\n") !== false) {
262                        $lineTexts = explode("\n", $cellText);
263                        $lineWitdhs = array();
264                        foreach ($lineTexts as $lineText) {
265                                $lineWidths[] = self::calculateColumnWidth($font, $lineText, $rotation = 0, $defaultFont);
266                        }
267                        return max($lineWidths); // width of longest line in cell
268                }
269
270                // Try to get the exact text width in pixels
271                try {
272                        // If autosize method is set to 'approx', use approximation
273                        if (self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX) {
274                                throw new PHPExcel_Exception('AutoSize method is set to approx');
275                        }
276
277                        // Width of text in pixels excl. padding
278                        $columnWidth = self::getTextWidthPixelsExact($cellText, $font, $rotation);
279
280                        // Excel adds some padding, use 1.07 of the width of an 'n' glyph
281                        $columnWidth += ceil(self::getTextWidthPixelsExact('0', $font, 0) * 1.07); // pixels incl. padding
282
283                } catch (PHPExcel_Exception $e) {
284                        // Width of text in pixels excl. padding, approximation
285                        $columnWidth = self::getTextWidthPixelsApprox($cellText, $font, $rotation);
286
287                        // Excel adds some padding, just use approx width of 'n' glyph
288                        $columnWidth += self::getTextWidthPixelsApprox('n', $font, 0);
289                }
290
291                // Convert from pixel width to column width
292                $columnWidth = PHPExcel_Shared_Drawing::pixelsToCellDimension($columnWidth, $defaultFont);
293
294                // Return
295                return round($columnWidth, 6);
296        }
297
298        /**
299         * Get GD text width in pixels for a string of text in a certain font at a certain rotation angle
300         *
301         * @param string $text
302         * @param PHPExcel_Style_Font
303         * @param int $rotation
304         * @return int
305         * @throws PHPExcel_Exception
306         */
307        public static function getTextWidthPixelsExact($text, PHPExcel_Style_Font $font, $rotation = 0) {
308                if (!function_exists('imagettfbbox')) {
309                        throw new PHPExcel_Exception('GD library needs to be enabled');
310                }
311
312                // font size should really be supplied in pixels in GD2,
313                // but since GD2 seems to assume 72dpi, pixels and points are the same
314                $fontFile = self::getTrueTypeFontFileFromFont($font);
315                $textBox = imagettfbbox($font->getSize(), $rotation, $fontFile, $text);
316
317                // Get corners positions
318                $lowerLeftCornerX  = $textBox[0];
319                $lowerLeftCornerY  = $textBox[1];
320                $lowerRightCornerX = $textBox[2];
321                $lowerRightCornerY = $textBox[3];
322                $upperRightCornerX = $textBox[4];
323                $upperRightCornerY = $textBox[5];
324                $upperLeftCornerX  = $textBox[6];
325                $upperLeftCornerY  = $textBox[7];
326
327                // Consider the rotation when calculating the width
328                $textWidth = max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX);
329
330                return $textWidth;
331        }
332
333        /**
334         * Get approximate width in pixels for a string of text in a certain font at a certain rotation angle
335         *
336         * @param string $columnText
337         * @param PHPExcel_Style_Font $font
338         * @param int $rotation
339         * @return int Text width in pixels (no padding added)
340         */
341        public static function getTextWidthPixelsApprox($columnText, PHPExcel_Style_Font $font = null, $rotation = 0)
342        {
343                $fontName = $font->getName();
344                $fontSize = $font->getSize();
345
346                // Calculate column width in pixels. We assume fixed glyph width. Result varies with font name and size.
347                switch ($fontName) {
348                        case 'Calibri':
349                                // value 8.26 was found via interpolation by inspecting real Excel files with Calibri 11 font.
350                                $columnWidth = (int) (8.26 * PHPExcel_Shared_String::CountCharacters($columnText));
351                                $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size
352                                break;
353
354                        case 'Arial':
355                                // value 7 was found via interpolation by inspecting real Excel files with Arial 10 font.
356                                $columnWidth = (int) (7 * PHPExcel_Shared_String::CountCharacters($columnText));
357                                $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size
358                                break;
359
360                        case 'Verdana':
361                                // value 8 was found via interpolation by inspecting real Excel files with Verdana 10 font.
362                                $columnWidth = (int) (8 * PHPExcel_Shared_String::CountCharacters($columnText));
363                                $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size
364                                break;
365
366                        default:
367                                // just assume Calibri
368                                $columnWidth = (int) (8.26 * PHPExcel_Shared_String::CountCharacters($columnText));
369                                $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size
370                                break;
371                }
372
373                // Calculate approximate rotated column width
374                if ($rotation !== 0) {
375                        if ($rotation == -165) {
376                                // stacked text
377                                $columnWidth = 4; // approximation
378                        } else {
379                                // rotated text
380                                $columnWidth = $columnWidth * cos(deg2rad($rotation))
381                                                                + $fontSize * abs(sin(deg2rad($rotation))) / 5; // approximation
382                        }
383                }
384
385                // pixel width is an integer
386                $columnWidth = (int) $columnWidth;
387                return $columnWidth;
388        }
389
390        /**
391         * Calculate an (approximate) pixel size, based on a font points size
392         *
393         * @param       int             $fontSizeInPoints       Font size (in points)
394         * @return      int             Font size (in pixels)
395         */
396        public static function fontSizeToPixels($fontSizeInPoints = 11) {
397                return (int) ((4 / 3) * $fontSizeInPoints);
398        }
399
400        /**
401         * Calculate an (approximate) pixel size, based on inch size
402         *
403         * @param       int             $sizeInInch     Font size (in inch)
404         * @return      int             Size (in pixels)
405         */
406        public static function inchSizeToPixels($sizeInInch = 1) {
407                return ($sizeInInch * 96);
408        }
409
410        /**
411         * Calculate an (approximate) pixel size, based on centimeter size
412         *
413         * @param       int             $sizeInCm       Font size (in centimeters)
414         * @return      int             Size (in pixels)
415         */
416        public static function centimeterSizeToPixels($sizeInCm = 1) {
417                return ($sizeInCm * 37.795275591);
418        }
419
420        /**
421         * Returns the font path given the font
422         *
423         * @param PHPExcel_Style_Font
424         * @return string Path to TrueType font file
425         */
426        public static function getTrueTypeFontFileFromFont($font) {
427                if (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath)) {
428                        throw new PHPExcel_Exception('Valid directory to TrueType Font files not specified');
429                }
430
431                $name           = $font->getName();
432                $bold           = $font->getBold();
433                $italic         = $font->getItalic();
434
435                // Check if we can map font to true type font file
436                switch ($name) {
437                        case 'Arial':
438                                $fontFile = (
439                                        $bold ? ($italic ? self::ARIAL_BOLD_ITALIC : self::ARIAL_BOLD)
440                                                  : ($italic ? self::ARIAL_ITALIC : self::ARIAL)
441                                );
442                                break;
443
444                        case 'Calibri':
445                                $fontFile = (
446                                        $bold ? ($italic ? self::CALIBRI_BOLD_ITALIC : self::CALIBRI_BOLD)
447                                                  : ($italic ? self::CALIBRI_ITALIC : self::CALIBRI)
448                                );
449                                break;
450
451                        case 'Courier New':
452                                $fontFile = (
453                                        $bold ? ($italic ? self::COURIER_NEW_BOLD_ITALIC : self::COURIER_NEW_BOLD)
454                                                  : ($italic ? self::COURIER_NEW_ITALIC : self::COURIER_NEW)
455                                );
456                                break;
457
458                        case 'Comic Sans MS':
459                                $fontFile = (
460                                        $bold ? self::COMIC_SANS_MS_BOLD : self::COMIC_SANS_MS
461                                );
462                                break;
463
464                        case 'Georgia':
465                                $fontFile = (
466                                        $bold ? ($italic ? self::GEORGIA_BOLD_ITALIC : self::GEORGIA_BOLD)
467                                                  : ($italic ? self::GEORGIA_ITALIC : self::GEORGIA)
468                                );
469                                break;
470
471                        case 'Impact':
472                                $fontFile = self::IMPACT;
473                                break;
474
475                        case 'Liberation Sans':
476                                $fontFile = (
477                                        $bold ? ($italic ? self::LIBERATION_SANS_BOLD_ITALIC : self::LIBERATION_SANS_BOLD)
478                                                  : ($italic ? self::LIBERATION_SANS_ITALIC : self::LIBERATION_SANS)
479                                );
480                                break;
481
482                        case 'Lucida Console':
483                                $fontFile = self::LUCIDA_CONSOLE;
484                                break;
485
486                        case 'Lucida Sans Unicode':
487                                $fontFile = self::LUCIDA_SANS_UNICODE;
488                                break;
489
490                        case 'Microsoft Sans Serif':
491                                $fontFile = self::MICROSOFT_SANS_SERIF;
492                                break;
493
494                        case 'Palatino Linotype':
495                                $fontFile = (
496                                        $bold ? ($italic ? self::PALATINO_LINOTYPE_BOLD_ITALIC : self::PALATINO_LINOTYPE_BOLD)
497                                                  : ($italic ? self::PALATINO_LINOTYPE_ITALIC : self::PALATINO_LINOTYPE)
498                                );
499                                break;
500
501                        case 'Symbol':
502                                $fontFile = self::SYMBOL;
503                                break;
504
505                        case 'Tahoma':
506                                $fontFile = (
507                                        $bold ? self::TAHOMA_BOLD : self::TAHOMA
508                                );
509                                break;
510
511                        case 'Times New Roman':
512                                $fontFile = (
513                                        $bold ? ($italic ? self::TIMES_NEW_ROMAN_BOLD_ITALIC : self::TIMES_NEW_ROMAN_BOLD)
514                                                  : ($italic ? self::TIMES_NEW_ROMAN_ITALIC : self::TIMES_NEW_ROMAN)
515                                );
516                                break;
517
518                        case 'Trebuchet MS':
519                                $fontFile = (
520                                        $bold ? ($italic ? self::TREBUCHET_MS_BOLD_ITALIC : self::TREBUCHET_MS_BOLD)
521                                                  : ($italic ? self::TREBUCHET_MS_ITALIC : self::TREBUCHET_MS)
522                                );
523                                break;
524
525                        case 'Verdana':
526                                $fontFile = (
527                                        $bold ? ($italic ? self::VERDANA_BOLD_ITALIC : self::VERDANA_BOLD)
528                                                  : ($italic ? self::VERDANA_ITALIC : self::VERDANA)
529                                );
530                                break;
531
532                        default:
533                                throw new PHPExcel_Exception('Unknown font name "'. $name .'". Cannot map to TrueType font file');
534                                break;
535                }
536
537                $fontFile = self::$trueTypeFontPath . $fontFile;
538
539                // Check if file actually exists
540                if (!file_exists($fontFile)) {
541                        throw New PHPExcel_Exception('TrueType Font file not found');
542                }
543
544                return $fontFile;
545        }
546
547        /**
548         * Returns the associated charset for the font name.
549         *
550         * @param string $name Font name
551         * @return int Character set code
552         */
553        public static function getCharsetFromFontName($name)
554        {
555                switch ($name) {
556                        // Add more cases. Check FONT records in real Excel files.
557                        case 'EucrosiaUPC':             return self::CHARSET_ANSI_THAI;
558                        case 'Wingdings':               return self::CHARSET_SYMBOL;
559                        case 'Wingdings 2':             return self::CHARSET_SYMBOL;
560                        case 'Wingdings 3':             return self::CHARSET_SYMBOL;
561                        default:                                return self::CHARSET_ANSI_LATIN;
562                }
563        }
564
565        /**
566         * Get the effective column width for columns without a column dimension or column with width -1
567         * For example, for Calibri 11 this is 9.140625 (64 px)
568         *
569         * @param PHPExcel_Style_Font $font The workbooks default font
570         * @param boolean $pPixels true = return column width in pixels, false = return in OOXML units
571         * @return mixed Column width
572         */
573        public static function getDefaultColumnWidthByFont(PHPExcel_Style_Font $font, $pPixels = false)
574        {
575                if (isset(self::$defaultColumnWidths[$font->getName()][$font->getSize()])) {
576                        // Exact width can be determined
577                        $columnWidth = $pPixels ?
578                                self::$defaultColumnWidths[$font->getName()][$font->getSize()]['px']
579                                        : self::$defaultColumnWidths[$font->getName()][$font->getSize()]['width'];
580
581                } else {
582                        // We don't have data for this particular font and size, use approximation by
583                        // extrapolating from Calibri 11
584                        $columnWidth = $pPixels ?
585                                self::$defaultColumnWidths['Calibri'][11]['px']
586                                        : self::$defaultColumnWidths['Calibri'][11]['width'];
587                        $columnWidth = $columnWidth * $font->getSize() / 11;
588
589                        // Round pixels to closest integer
590                        if ($pPixels) {
591                                $columnWidth = (int) round($columnWidth);
592                        }
593                }
594
595                return $columnWidth;
596        }
597
598        /**
599         * Get the effective row height for rows without a row dimension or rows with height -1
600         * For example, for Calibri 11 this is 15 points
601         *
602         * @param PHPExcel_Style_Font $font The workbooks default font
603         * @return float Row height in points
604         */
605        public static function getDefaultRowHeightByFont(PHPExcel_Style_Font $font)
606        {
607                switch ($font->getName()) {
608                        case 'Arial':
609                                switch ($font->getSize()) {
610                                        case 10:
611                                                // inspection of Arial 10 workbook says 12.75pt ~17px
612                                                $rowHeight = 12.75;
613                                                break;
614
615                                        case 9:
616                                                // inspection of Arial 9 workbook says 12.00pt ~16px
617                                                $rowHeight = 12;
618                                                break;
619
620                                        case 8:
621                                                // inspection of Arial 8 workbook says 11.25pt ~15px
622                                                $rowHeight = 11.25;
623                                                break;
624
625                                        case 7:
626                                                // inspection of Arial 7 workbook says 9.00pt ~12px
627                                                $rowHeight = 9;
628                                                break;
629
630                                        case 6:
631                                        case 5:
632                                                // inspection of Arial 5,6 workbook says 8.25pt ~11px
633                                                $rowHeight = 8.25;
634                                                break;
635
636                                        case 4:
637                                                // inspection of Arial 4 workbook says 6.75pt ~9px
638                                                $rowHeight = 6.75;
639                                                break;
640
641                                        case 3:
642                                                // inspection of Arial 3 workbook says 6.00pt ~8px
643                                                $rowHeight = 6;
644                                                break;
645
646                                        case 2:
647                                        case 1:
648                                                // inspection of Arial 1,2 workbook says 5.25pt ~7px
649                                                $rowHeight = 5.25;
650                                                break;
651
652                                        default:
653                                                // use Arial 10 workbook as an approximation, extrapolation
654                                                $rowHeight = 12.75 * $font->getSize() / 10;
655                                                break;
656                                }
657                                break;
658
659                        case 'Calibri':
660                                switch ($font->getSize()) {
661                                        case 11:
662                                                // inspection of Calibri 11 workbook says 15.00pt ~20px
663                                                $rowHeight = 15;
664                                                break;
665
666                                        case 10:
667                                                // inspection of Calibri 10 workbook says 12.75pt ~17px
668                                                $rowHeight = 12.75;
669                                                break;
670
671                                        case 9:
672                                                // inspection of Calibri 9 workbook says 12.00pt ~16px
673                                                $rowHeight = 12;
674                                                break;
675
676                                        case 8:
677                                                // inspection of Calibri 8 workbook says 11.25pt ~15px
678                                                $rowHeight = 11.25;
679                                                break;
680
681                                        case 7:
682                                                // inspection of Calibri 7 workbook says 9.00pt ~12px
683                                                $rowHeight = 9;
684                                                break;
685
686                                        case 6:
687                                        case 5:
688                                                // inspection of Calibri 5,6 workbook says 8.25pt ~11px
689                                                $rowHeight = 8.25;
690                                                break;
691
692                                        case 4:
693                                                // inspection of Calibri 4 workbook says 6.75pt ~9px
694                                                $rowHeight = 6.75;
695                                                break;
696
697                                        case 3:
698                                                // inspection of Calibri 3 workbook says 6.00pt ~8px
699                                                $rowHeight = 6.00;
700                                                break;
701
702                                        case 2:
703                                        case 1:
704                                                // inspection of Calibri 1,2 workbook says 5.25pt ~7px
705                                                $rowHeight = 5.25;
706                                                break;
707
708                                        default:
709                                                // use Calibri 11 workbook as an approximation, extrapolation
710                                                $rowHeight = 15 * $font->getSize() / 11;
711                                                break;
712                                }
713                                break;
714
715                        case 'Verdana':
716                                switch ($font->getSize()) {
717                                        case 10:
718                                                // inspection of Verdana 10 workbook says 12.75pt ~17px
719                                                $rowHeight = 12.75;
720                                                break;
721
722                                        case 9:
723                                                // inspection of Verdana 9 workbook says 11.25pt ~15px
724                                                $rowHeight = 11.25;
725                                                break;
726
727                                        case 8:
728                                                // inspection of Verdana 8 workbook says 10.50pt ~14px
729                                                $rowHeight = 10.50;
730                                                break;
731
732                                        case 7:
733                                                // inspection of Verdana 7 workbook says 9.00pt ~12px
734                                                $rowHeight = 9.00;
735                                                break;
736
737                                        case 6:
738                                        case 5:
739                                                // inspection of Verdana 5,6 workbook says 8.25pt ~11px
740                                                $rowHeight = 8.25;
741                                                break;
742
743                                        case 4:
744                                                // inspection of Verdana 4 workbook says 6.75pt ~9px
745                                                $rowHeight = 6.75;
746                                                break;
747
748                                        case 3:
749                                                // inspection of Verdana 3 workbook says 6.00pt ~8px
750                                                $rowHeight = 6;
751                                                break;
752
753                                        case 2:
754                                        case 1:
755                                                // inspection of Verdana 1,2 workbook says 5.25pt ~7px
756                                                $rowHeight = 5.25;
757                                                break;
758
759                                        default:
760                                                // use Verdana 10 workbook as an approximation, extrapolation
761                                                $rowHeight = 12.75 * $font->getSize() / 10;
762                                                break;
763                                }
764                                break;
765
766                        default:
767                                // just use Calibri as an approximation
768                                $rowHeight = 15 * $font->getSize() / 11;
769                                break;
770                }
771
772                return $rowHeight;
773        }
774
775}
Note: See TracBrowser for help on using the repository browser.