PHPExcel_Shared
[ class tree: PHPExcel_Shared ] [ index: PHPExcel_Shared ] [ all elements ]

Source for file Font.php

Documentation is available at Font.php

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

Documentation generated on Mon, 11 Jan 2010 08:09:29 +0100 by phpDocumentor 1.4.1