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.7.0, 2009-08-10
  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.     /** Character set codes used by BIFF5-8 in Font records */
  38.     const CHARSET_ANSI_LATIN                0x00;
  39.     const CHARSET_SYSTEM_DEFAULT            0x01;
  40.     const CHARSET_SYMBOL                    0x02;
  41.     const CHARSET_APPLE_ROMAN                0x4D;
  42.     const CHARSET_ANSI_JAPANESE_SHIFTJIS    0x80;
  43.     const CHARSET_ANSI_KOREAN_HANGUL        0x81;
  44.     const CHARSET_ANSI_KOREAN_JOHAB            0x82;
  45.     const CHARSET_ANSI_CHINESE_SIMIPLIFIED    0x86;
  46.     const CHARSET_ANSI_CHINESE_TRADITIONAL    0x88;
  47.     const CHARSET_ANSI_GREEK                0xA1;
  48.     const CHARSET_ANSI_TURKISH                0xA2;
  49.     const CHARSET_ANSI_VIETNAMESE            0xA3;
  50.     const CHARSET_ANSI_HEBREW                0xB1;
  51.     const CHARSET_ANSI_ARABIC                0xB2;
  52.     const CHARSET_ANSI_BALTIC                0xBA;
  53.     const CHARSET_ANSI_CYRILLIC                0xCC;
  54.     const CHARSET_ANSI_THAI                    0xDE;
  55.     const CHARSET_ANSI_LATIN_II                0xEE;
  56.     const CHARSET_OEM_LATIN_I                0xFF;
  57.     
  58.     /**
  59.      * Calculate an (approximate) OpenXML column width, based on font size and text contained
  60.      *
  61.      * @param     int        $fontSize            Font size (in pixels or points)
  62.      * @param     bool    $fontSizeInPixels    Is the font size specified in pixels (true) or in points (false) ?
  63.      * @param     string    $columnText            Text to calculate width
  64.      * @param     int        $rotation            Rotation angle
  65.      * @return     int        Column width
  66.      */
  67.     public static function calculateColumnWidth($fontSize 9$fontSizeInPixels false$columnText ''$rotation 0{
  68.         if (!$fontSizeInPixels{
  69.             // Translate points size to pixel size
  70.             $fontSize PHPExcel_Shared_Font::fontSizeToPixels($fontSize);
  71.         }
  72.         
  73.         // If it is rich text, use rich text...
  74.         if ($columnText instanceof PHPExcel_RichText{
  75.             $columnText $columnText->getPlainText();
  76.         }
  77.         
  78.         // Only measure the part before the first newline character
  79.         if (strpos($columnText"\r"!== false{
  80.             $columnText substr($columnText0strpos($columnText"\r"));
  81.         }
  82.         if (strpos($columnText"\n"!== false{
  83.             $columnText substr($columnText0strpos($columnText"\n"));
  84.         }
  85.         
  86.         // Calculate column width
  87.         // values 1.025 and 0.584 found via interpolation by inspecting real Excel files with
  88.         // Calibri font. May need further adjustment
  89.         $columnWidth 1.025 strlen($columnText0.584// Excel adds some padding
  90.  
  91.         // Calculate approximate rotated column width
  92.         if ($rotation !== 0{
  93.             if ($rotation == -165{
  94.                 // stacked text
  95.                 $columnWidth 4// approximation
  96.             else {
  97.                 // rotated text
  98.                 $columnWidth $columnWidth cos(deg2rad($rotation))
  99.                                 + $fontSize abs(sin(deg2rad($rotation))) 5// approximation
  100.             }
  101.         }
  102.  
  103.         // Return
  104.         return round($columnWidth6);
  105.     }
  106.     
  107.     /**
  108.      * Calculate an (approximate) pixel size, based on a font points size
  109.      *
  110.      * @param     int        $fontSizeInPoints    Font size (in points)
  111.      * @return     int        Font size (in pixels)
  112.      */
  113.     public static function fontSizeToPixels($fontSizeInPoints 12{
  114.         return ((16 12$fontSizeInPoints);
  115.     }
  116.     
  117.     /**
  118.      * Calculate an (approximate) pixel size, based on inch size
  119.      *
  120.      * @param     int        $sizeInInch    Font size (in inch)
  121.      * @return     int        Size (in pixels)
  122.      */
  123.     public static function inchSizeToPixels($sizeInInch 1{
  124.         return ($sizeInInch 96);
  125.     }
  126.     
  127.     /**
  128.      * Calculate an (approximate) pixel size, based on centimeter size
  129.      *
  130.      * @param     int        $sizeInCm    Font size (in centimeters)
  131.      * @return     int        Size (in pixels)
  132.      */
  133.     public static function centimeterSizeToPixels($sizeInCm 1{
  134.         return ($sizeInCm 37.795275591);
  135.     }
  136.  
  137.     /**
  138.      * Returns the associated charset for the font name.
  139.      *
  140.      * @param string $name Font name
  141.      * @return int Character set code
  142.      */
  143.     public static function getCharsetFromFontName($name)
  144.     {
  145.         switch ($name{
  146.             // Add more cases. Check FONT records in real Excel files.
  147.             case 'Wingdings':        return self::CHARSET_SYMBOL;
  148.             case 'Wingdings 2':        return self::CHARSET_SYMBOL;
  149.             case 'Wingdings 3':        return self::CHARSET_SYMBOL;
  150.             default:                return self::CHARSET_ANSI_LATIN;
  151.         }
  152.     }
  153.  
  154. }

Documentation generated on Mon, 10 Aug 2009 08:04:23 +0200 by phpDocumentor 1.4.1