1 | <?php |
||
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) |
|
55 | |||
56 | /** |
||
57 | * Set the color index. |
||
58 | * |
||
59 | * @param int $colorIndex |
||
60 | */ |
||
61 | 39 | public function setColorIndex($colorIndex) |
|
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) |
|
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) |
|
172 | } |
||
173 |