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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.7, 2009-04-22
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Shared_Font
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Shared
  34.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. {
  37.     /**
  38.      * Calculate an (approximate) OpenXML column width, based on font size and text contained
  39.      *
  40.      * @param     int        $fontSize            Font size (in pixels or points)
  41.      * @param     bool    $fontSizeInPixels    Is the font size specified in pixels (true) or in points (false) ?
  42.      * @param     string    $columnText            Text to calculate width
  43.      * @param     int        $rotation            Rotation angle
  44.      * @return     int        Column width
  45.      */
  46.     public static function calculateColumnWidth($fontSize 9$fontSizeInPixels false$columnText ''$rotation 0{
  47.         if (!$fontSizeInPixels{
  48.             // Translate points size to pixel size
  49.             $fontSize PHPExcel_Shared_Font::fontSizeToPixels($fontSize);
  50.         }
  51.         
  52.         // If it is rich text, use rich text...
  53.         if ($columnText instanceof PHPExcel_RichText{
  54.             $columnText $columnText->getPlainText();
  55.         }
  56.         
  57.         // Only measure the part before the first newline character
  58.         if (strpos($columnText"\r"!== false{
  59.             $columnText substr($columnText0strpos($columnText"\r"));
  60.         }
  61.         if (strpos($columnText"\n"!== false{
  62.             $columnText substr($columnText0strpos($columnText"\n"));
  63.         }
  64.         
  65.         // Calculate column width
  66.         $columnWidth ((strlen($columnText$fontSize 5$fontSize 256 256;
  67.  
  68.         // Calculate approximate rotated column width
  69.         if ($rotation !== 0{
  70.             if ($rotation == -165{
  71.                 // stacked text
  72.                 $columnWidth 4// approximation
  73.             else {
  74.                 // rotated text
  75.                 $columnWidth $columnWidth cos(deg2rad($rotation))
  76.                                 + $fontSize abs(sin(deg2rad($rotation))) 5// approximation
  77.             }
  78.         }
  79.  
  80.         // Return
  81.         return round($columnWidth6);
  82.     }
  83.     
  84.     /**
  85.      * Calculate an (approximate) pixel size, based on a font points size
  86.      *
  87.      * @param     int        $fontSizeInPoints    Font size (in points)
  88.      * @return     int        Font size (in pixels)
  89.      */
  90.     public static function fontSizeToPixels($fontSizeInPoints 12{
  91.         return ((16 12$fontSizeInPoints);
  92.     }
  93.     
  94.     /**
  95.      * Calculate an (approximate) pixel size, based on inch size
  96.      *
  97.      * @param     int        $sizeInInch    Font size (in inch)
  98.      * @return     int        Size (in pixels)
  99.      */
  100.     public static function inchSizeToPixels($sizeInInch 1{
  101.         return ($sizeInInch 96);
  102.     }
  103.     
  104.     /**
  105.      * Calculate an (approximate) pixel size, based on centimeter size
  106.      *
  107.      * @param     int        $sizeInCm    Font size (in centimeters)
  108.      * @return     int        Size (in pixels)
  109.      */
  110.     public static function centimeterSizeToPixels($sizeInCm 1{
  111.         return ($sizeInCm 37.795275591);
  112.     }
  113. }

Documentation generated on Wed, 22 Apr 2009 08:58:29 +0200 by phpDocumentor 1.4.1