1<?php
2
3/**
4 * PHPExcel_Writer_Excel5_Font
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 * @category   PHPExcel
23 * @package    PHPExcel_Writer_Excel5
24 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26 * @version    ##VERSION##, ##DATE##
27 */
28class PHPExcel_Writer_Excel5_Font
29{
30    /**
31     * Color index
32     *
33     * @var int
34     */
35    private $colorIndex;
36
37    /**
38     * Font
39     *
40     * @var PHPExcel_Style_Font
41     */
42    private $font;
43
44    /**
45     * Constructor
46     *
47     * @param PHPExcel_Style_Font $font
48     */
49    public function __construct(PHPExcel_Style_Font $font = null)
50    {
51        $this->colorIndex = 0x7FFF;
52        $this->font = $font;
53    }
54
55    /**
56     * Set the color index
57     *
58     * @param int $colorIndex
59     */
60    public function setColorIndex($colorIndex)
61    {
62        $this->colorIndex = $colorIndex;
63    }
64
65    /**
66     * Get font record data
67     *
68     * @return string
69     */
70    public function writeFont()
71    {
72        $font_outline = 0;
73        $font_shadow = 0;
74
75        $icv = $this->colorIndex; // Index to color palette
76        if ($this->font->getSuperScript()) {
77            $sss = 1;
78        } elseif ($this->font->getSubScript()) {
79            $sss = 2;
80        } else {
81            $sss = 0;
82        }
83        $bFamily = 0; // Font family
84        $bCharSet = PHPExcel_Shared_Font::getCharsetFromFontName($this->font->getName()); // Character set
85
86        $record = 0x31;        // Record identifier
87        $reserved = 0x00;    // Reserved
88        $grbit = 0x00;        // Font attributes
89        if ($this->font->getItalic()) {
90            $grbit |= 0x02;
91        }
92        if ($this->font->getStrikethrough()) {
93            $grbit |= 0x08;
94        }
95        if ($font_outline) {
96            $grbit |= 0x10;
97        }
98        if ($font_shadow) {
99            $grbit |= 0x20;
100        }
101
102        $data = pack(
103            "vvvvvCCCC",
104            // Fontsize (in twips)
105            $this->font->getSize() * 20,
106            $grbit,
107            // Colour
108            $icv,
109            // Font weight
110            self::mapBold($this->font->getBold()),
111            // Superscript/Subscript
112            $sss,
113            self::mapUnderline($this->font->getUnderline()),
114            $bFamily,
115            $bCharSet,
116            $reserved
117        );
118        $data .= PHPExcel_Shared_String::UTF8toBIFF8UnicodeShort($this->font->getName());
119
120        $length = strlen($data);
121        $header = pack("vv", $record, $length);
122
123        return($header . $data);
124    }
125
126    /**
127     * Map to BIFF5-BIFF8 codes for bold
128     *
129     * @param boolean $bold
130     * @return int
131     */
132    private static function mapBold($bold)
133    {
134        if ($bold) {
135            return 0x2BC;  //  700 = Bold font weight
136        }
137        return 0x190;      //  400 = Normal font weight
138    }
139
140    /**
141     * Map of BIFF2-BIFF8 codes for underline styles
142     * @static    array of int
143     *
144     */
145    private static $mapUnderline = array(
146        PHPExcel_Style_Font::UNDERLINE_NONE              => 0x00,
147        PHPExcel_Style_Font::UNDERLINE_SINGLE            => 0x01,
148        PHPExcel_Style_Font::UNDERLINE_DOUBLE            => 0x02,
149        PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING  => 0x21,
150        PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING  => 0x22,
151    );
152
153    /**
154     * Map underline
155     *
156     * @param string
157     * @return int
158     */
159    private static function mapUnderline($underline)
160    {
161        if (isset(self::$mapUnderline[$underline])) {
162            return self::$mapUnderline[$underline];
163        }
164        return 0x00;
165    }
166}
167