Completed
Push — develop ( c96e2d...d2f55f )
by Adrien
48:43 queued 44:11
created

Font::setColorIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer\Xls;
4
5
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
6
7
class Font
8
{
9
    /**
10
     * Color index.
11
     *
12
     * @var int
13
     */
14
    private $colorIndex;
15
16
    /**
17
     * Font.
18
     *
19
     * @var \PhpOffice\PhpSpreadsheet\Style\Font
20
     */
21
    private $font;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param \PhpOffice\PhpSpreadsheet\Style\Font $font
27
     */
28 42
    public function __construct(\PhpOffice\PhpSpreadsheet\Style\Font $font)
29
    {
30 42
        $this->colorIndex = 0x7FFF;
31 42
        $this->font = $font;
32 42
    }
33
34
    /**
35
     * Set the color index.
36
     *
37
     * @param int $colorIndex
38
     */
39 42
    public function setColorIndex($colorIndex)
40
    {
41 42
        $this->colorIndex = $colorIndex;
42 42
    }
43
44
    /**
45
     * Get font record data.
46
     *
47
     * @return string
48
     */
49 42
    public function writeFont()
50
    {
51 42
        $font_outline = 0;
52 42
        $font_shadow = 0;
53
54 42
        $icv = $this->colorIndex; // Index to color palette
55 42
        if ($this->font->getSuperscript()) {
56 2
            $sss = 1;
57 42
        } elseif ($this->font->getSubscript()) {
58 2
            $sss = 2;
59
        } else {
60 42
            $sss = 0;
61
        }
62 42
        $bFamily = 0; // Font family
63 42
        $bCharSet = \PhpOffice\PhpSpreadsheet\Shared\Font::getCharsetFromFontName($this->font->getName()); // Character set
64
65 42
        $record = 0x31; // Record identifier
66 42
        $reserved = 0x00; // Reserved
67 42
        $grbit = 0x00; // Font attributes
68 42
        if ($this->font->getItalic()) {
69 11
            $grbit |= 0x02;
70
        }
71 42
        if ($this->font->getStrikethrough()) {
72 2
            $grbit |= 0x08;
73
        }
74 42
        if ($font_outline) {
75
            $grbit |= 0x10;
76
        }
77 42
        if ($font_shadow) {
78
            $grbit |= 0x20;
79
        }
80
81 42
        $data = pack(
82 42
            'vvvvvCCCC',
83
            // Fontsize (in twips)
84 42
            $this->font->getSize() * 20,
85 42
            $grbit,
86
            // Colour
87 42
            $icv,
88
            // Font weight
89 42
            self::mapBold($this->font->getBold()),
90
            // Superscript/Subscript
91 42
            $sss,
92 42
            self::mapUnderline($this->font->getUnderline()),
93 42
            $bFamily,
94 42
            $bCharSet,
95 42
            $reserved
96
        );
97 42
        $data .= StringHelper::UTF8toBIFF8UnicodeShort($this->font->getName());
98
99 42
        $length = strlen($data);
100 42
        $header = pack('vv', $record, $length);
101
102 42
        return $header . $data;
103
    }
104
105
    /**
106
     * Map to BIFF5-BIFF8 codes for bold.
107
     *
108
     * @param bool $bold
109
     *
110
     * @return int
111
     */
112 42
    private static function mapBold($bold)
113
    {
114 42
        if ($bold) {
115 17
            return 0x2BC; //  700 = Bold font weight
116
        }
117
118 42
        return 0x190; //  400 = Normal font weight
119
    }
120
121
    /**
122
     * Map of BIFF2-BIFF8 codes for underline styles.
123
     *
124
     * @var array of int
125
     */
126
    private static $mapUnderline = [
127
        \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_NONE => 0x00,
128
        \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE => 0x01,
129
        \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE => 0x02,
130
        \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLEACCOUNTING => 0x21,
131
        \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLEACCOUNTING => 0x22,
132
    ];
133
134
    /**
135
     * Map underline.
136
     *
137
     * @param string $underline
138
     *
139
     * @return int
140
     */
141 42
    private static function mapUnderline($underline)
142
    {
143 42
        if (isset(self::$mapUnderline[$underline])) {
144 42
            return self::$mapUnderline[$underline];
145
        }
146
147
        return 0x00;
148
    }
149
}
150