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
|
|
|
private int $colorIndex; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Constructor. |
16
|
|
|
*/ |
17
|
97 |
|
public function __construct(/** |
18
|
|
|
* Font. |
19
|
|
|
*/ |
20
|
|
|
private \PhpOffice\PhpSpreadsheet\Style\Font $font |
21
|
|
|
) { |
22
|
97 |
|
$this->colorIndex = 0x7FFF; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set the color index. |
27
|
|
|
*/ |
28
|
97 |
|
public function setColorIndex(int $colorIndex): void |
29
|
|
|
{ |
30
|
97 |
|
$this->colorIndex = $colorIndex; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private static int $notImplemented = 0; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get font record data. |
37
|
|
|
*/ |
38
|
96 |
|
public function writeFont(): string |
39
|
|
|
{ |
40
|
96 |
|
$font_outline = self::$notImplemented; |
41
|
96 |
|
$font_shadow = self::$notImplemented; |
42
|
|
|
|
43
|
96 |
|
$icv = $this->colorIndex; // Index to color palette |
44
|
96 |
|
if ($this->font->getSuperscript()) { |
45
|
3 |
|
$sss = 1; |
46
|
96 |
|
} elseif ($this->font->getSubscript()) { |
47
|
3 |
|
$sss = 2; |
48
|
|
|
} else { |
49
|
96 |
|
$sss = 0; |
50
|
|
|
} |
51
|
96 |
|
$bFamily = 0; // Font family |
52
|
96 |
|
$bCharSet = \PhpOffice\PhpSpreadsheet\Shared\Font::getCharsetFromFontName((string) $this->font->getName()); // Character set |
53
|
|
|
|
54
|
96 |
|
$record = 0x31; // Record identifier |
55
|
96 |
|
$reserved = 0x00; // Reserved |
56
|
96 |
|
$grbit = 0x00; // Font attributes |
57
|
96 |
|
if ($this->font->getItalic()) { |
58
|
14 |
|
$grbit |= 0x02; |
59
|
|
|
} |
60
|
96 |
|
if ($this->font->getStrikethrough()) { |
61
|
2 |
|
$grbit |= 0x08; |
62
|
|
|
} |
63
|
96 |
|
if ($font_outline) { |
64
|
|
|
$grbit |= 0x10; |
65
|
|
|
} |
66
|
96 |
|
if ($font_shadow) { |
67
|
|
|
$grbit |= 0x20; |
68
|
|
|
} |
69
|
|
|
|
70
|
96 |
|
$data = pack( |
71
|
96 |
|
'vvvvvCCCC', |
72
|
|
|
// Fontsize (in twips) |
73
|
96 |
|
$this->font->getSize() * 20, |
74
|
96 |
|
$grbit, |
75
|
|
|
// Colour |
76
|
96 |
|
$icv, |
77
|
|
|
// Font weight |
78
|
96 |
|
self::mapBold($this->font->getBold()), |
79
|
|
|
// Superscript/Subscript |
80
|
96 |
|
$sss, |
81
|
96 |
|
self::mapUnderline((string) $this->font->getUnderline()), |
82
|
96 |
|
$bFamily, |
83
|
96 |
|
$bCharSet, |
84
|
96 |
|
$reserved |
85
|
96 |
|
); |
86
|
96 |
|
$data .= StringHelper::UTF8toBIFF8UnicodeShort((string) $this->font->getName()); |
87
|
|
|
|
88
|
96 |
|
$length = strlen($data); |
89
|
96 |
|
$header = pack('vv', $record, $length); |
90
|
|
|
|
91
|
96 |
|
return $header . $data; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Map to BIFF5-BIFF8 codes for bold. |
96
|
|
|
*/ |
97
|
96 |
|
private static function mapBold(?bool $bold): int |
98
|
|
|
{ |
99
|
96 |
|
if ($bold === true) { |
100
|
28 |
|
return 0x2BC; // 700 = Bold font weight |
101
|
|
|
} |
102
|
|
|
|
103
|
96 |
|
return 0x190; // 400 = Normal font weight |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Map of BIFF2-BIFF8 codes for underline styles. |
108
|
|
|
* |
109
|
|
|
* @var int[] |
110
|
|
|
*/ |
111
|
|
|
private static array $mapUnderline = [ |
112
|
|
|
\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_NONE => 0x00, |
113
|
|
|
\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE => 0x01, |
114
|
|
|
\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE => 0x02, |
115
|
|
|
\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLEACCOUNTING => 0x21, |
116
|
|
|
\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLEACCOUNTING => 0x22, |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Map underline. |
121
|
|
|
*/ |
122
|
96 |
|
private static function mapUnderline(string $underline): int |
123
|
|
|
{ |
124
|
96 |
|
return self::$mapUnderline[$underline] ?? 0x00; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|