5
* Copyright (c) 2006 - 2009 PHPExcel
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.
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.
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
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
30
* PHPExcel_Shared_Font
33
* @package PHPExcel_Shared
34
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
36
class PHPExcel_Shared_Font
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;
60
* Calculate an (approximate) OpenXML column width, based on font size and text contained
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
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);
74
// If it is rich text, use rich text...
75
if ($columnText instanceof PHPExcel_RichText) {
76
$columnText = $columnText->getPlainText();
79
// Only measure the part before the first newline character
80
if (strpos($columnText, "\r") !== false) {
81
$columnText = substr($columnText, 0, strpos($columnText, "\r"));
83
if (strpos($columnText, "\n") !== false) {
84
$columnText = substr($columnText, 0, strpos($columnText, "\n"));
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
92
// Calculate approximate rotated column width
93
if ($rotation !== 0) {
94
if ($rotation == -165) {
96
$columnWidth = 4; // approximation
99
$columnWidth = $columnWidth * cos(deg2rad($rotation))
100
+ $fontSize * abs(sin(deg2rad($rotation))) / 5; // approximation
105
return round($columnWidth, 6);
109
* Calculate an (approximate) pixel size, based on a font points size
111
* @param int $fontSizeInPoints Font size (in points)
112
* @return int Font size (in pixels)
114
public static function fontSizeToPixels($fontSizeInPoints = 12) {
115
return ((16 / 12) * $fontSizeInPoints);
119
* Calculate an (approximate) pixel size, based on inch size
121
* @param int $sizeInInch Font size (in inch)
122
* @return int Size (in pixels)
124
public static function inchSizeToPixels($sizeInInch = 1) {
125
return ($sizeInInch * 96);
129
* Calculate an (approximate) pixel size, based on centimeter size
131
* @param int $sizeInCm Font size (in centimeters)
132
* @return int Size (in pixels)
134
public static function centimeterSizeToPixels($sizeInCm = 1) {
135
return ($sizeInCm * 37.795275591);
139
* Returns the associated charset for the font name.
141
* @param string $name Font name
142
* @return int Character set code
144
public static function getCharsetFromFontName($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;