Completed
Push — develop ( 672893...dd9590 )
by Adrien
28:52 queued 21:58
created

Font::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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