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

Documentation generated on Sun, 27 Feb 2011 16:31:43 -0800 by phpDocumentor 1.4.3