~ubuntu-branches/ubuntu/lucid/phpmyadmin/lucid

« back to all changes in this revision

Viewing changes to libraries/PHPExcel/PHPExcel/Shared/Font.php

  • Committer: Bazaar Package Importer
  • Author(s): Michal Čihař
  • Date: 2010-03-08 15:25:00 UTC
  • mfrom: (1.2.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100308152500-6e8hmuqc5co39de5
Tags: 4:3.3.0-1
* New upstream version.
* Rediff debian/patches.
* Fix permissions on mediawiki export extension.

Show diffs side-by-side

added added

removed removed

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