1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Writer\Ods\Cell;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Helper\Dimension;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Style\Border;
|
|
|
9 |
use PhpOffice\PhpSpreadsheet\Style\Borders;
|
|
|
10 |
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
|
11 |
use PhpOffice\PhpSpreadsheet\Style\Font;
|
|
|
12 |
use PhpOffice\PhpSpreadsheet\Style\Style as CellStyle;
|
|
|
13 |
use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension;
|
|
|
14 |
use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension;
|
|
|
15 |
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
16 |
|
|
|
17 |
class Style
|
|
|
18 |
{
|
|
|
19 |
public const CELL_STYLE_PREFIX = 'ce';
|
|
|
20 |
public const COLUMN_STYLE_PREFIX = 'co';
|
|
|
21 |
public const ROW_STYLE_PREFIX = 'ro';
|
|
|
22 |
public const TABLE_STYLE_PREFIX = 'ta';
|
|
|
23 |
public const INDENT_TO_INCHES = 0.1043; // undocumented, used trial and error
|
|
|
24 |
|
|
|
25 |
private XMLWriter $writer;
|
|
|
26 |
|
|
|
27 |
public function __construct(XMLWriter $writer)
|
|
|
28 |
{
|
|
|
29 |
$this->writer = $writer;
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
private function mapHorizontalAlignment(?string $horizontalAlignment): string
|
|
|
33 |
{
|
|
|
34 |
return match ($horizontalAlignment) {
|
|
|
35 |
Alignment::HORIZONTAL_CENTER, Alignment::HORIZONTAL_CENTER_CONTINUOUS, Alignment::HORIZONTAL_DISTRIBUTED => 'center',
|
|
|
36 |
Alignment::HORIZONTAL_RIGHT => 'end',
|
|
|
37 |
Alignment::HORIZONTAL_FILL, Alignment::HORIZONTAL_JUSTIFY => 'justify',
|
|
|
38 |
Alignment::HORIZONTAL_GENERAL, '', null => '',
|
|
|
39 |
default => 'start',
|
|
|
40 |
};
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
private function mapVerticalAlignment(string $verticalAlignment): string
|
|
|
44 |
{
|
|
|
45 |
return match ($verticalAlignment) {
|
|
|
46 |
Alignment::VERTICAL_TOP => 'top',
|
|
|
47 |
Alignment::VERTICAL_CENTER => 'middle',
|
|
|
48 |
Alignment::VERTICAL_DISTRIBUTED, Alignment::VERTICAL_JUSTIFY => 'automatic',
|
|
|
49 |
default => 'bottom',
|
|
|
50 |
};
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
private function writeFillStyle(Fill $fill): void
|
|
|
54 |
{
|
|
|
55 |
switch ($fill->getFillType()) {
|
|
|
56 |
case Fill::FILL_SOLID:
|
|
|
57 |
$this->writer->writeAttribute('fo:background-color', sprintf(
|
|
|
58 |
'#%s',
|
|
|
59 |
strtolower($fill->getStartColor()->getRGB())
|
|
|
60 |
));
|
|
|
61 |
|
|
|
62 |
break;
|
|
|
63 |
case Fill::FILL_GRADIENT_LINEAR:
|
|
|
64 |
case Fill::FILL_GRADIENT_PATH:
|
|
|
65 |
/// TODO :: To be implemented
|
|
|
66 |
break;
|
|
|
67 |
case Fill::FILL_NONE:
|
|
|
68 |
default:
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private function writeBordersStyle(Borders $borders): void
|
|
|
73 |
{
|
|
|
74 |
$this->writeBorderStyle('bottom', $borders->getBottom());
|
|
|
75 |
$this->writeBorderStyle('left', $borders->getLeft());
|
|
|
76 |
$this->writeBorderStyle('right', $borders->getRight());
|
|
|
77 |
$this->writeBorderStyle('top', $borders->getTop());
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private function writeBorderStyle(string $direction, Border $border): void
|
|
|
81 |
{
|
|
|
82 |
if ($border->getBorderStyle() === Border::BORDER_NONE) {
|
|
|
83 |
return;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$this->writer->writeAttribute('fo:border-' . $direction, sprintf(
|
|
|
87 |
'%s %s #%s',
|
|
|
88 |
$this->mapBorderWidth($border),
|
|
|
89 |
$this->mapBorderStyle($border),
|
|
|
90 |
$border->getColor()->getRGB(),
|
|
|
91 |
));
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
private function mapBorderWidth(Border $border): string
|
|
|
95 |
{
|
|
|
96 |
switch ($border->getBorderStyle()) {
|
|
|
97 |
case Border::BORDER_THIN:
|
|
|
98 |
case Border::BORDER_DASHED:
|
|
|
99 |
case Border::BORDER_DASHDOT:
|
|
|
100 |
case Border::BORDER_DASHDOTDOT:
|
|
|
101 |
case Border::BORDER_DOTTED:
|
|
|
102 |
case Border::BORDER_HAIR:
|
|
|
103 |
return '0.75pt';
|
|
|
104 |
case Border::BORDER_MEDIUM:
|
|
|
105 |
case Border::BORDER_MEDIUMDASHED:
|
|
|
106 |
case Border::BORDER_MEDIUMDASHDOT:
|
|
|
107 |
case Border::BORDER_MEDIUMDASHDOTDOT:
|
|
|
108 |
case Border::BORDER_SLANTDASHDOT:
|
|
|
109 |
return '1.75pt';
|
|
|
110 |
case Border::BORDER_DOUBLE:
|
|
|
111 |
case Border::BORDER_THICK:
|
|
|
112 |
return '2.5pt';
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
return '1pt';
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
private function mapBorderStyle(Border $border): string
|
|
|
119 |
{
|
|
|
120 |
switch ($border->getBorderStyle()) {
|
|
|
121 |
case Border::BORDER_DOTTED:
|
|
|
122 |
case Border::BORDER_MEDIUMDASHDOTDOT:
|
|
|
123 |
return Border::BORDER_DOTTED;
|
|
|
124 |
|
|
|
125 |
case Border::BORDER_DASHED:
|
|
|
126 |
case Border::BORDER_DASHDOT:
|
|
|
127 |
case Border::BORDER_DASHDOTDOT:
|
|
|
128 |
case Border::BORDER_MEDIUMDASHDOT:
|
|
|
129 |
case Border::BORDER_MEDIUMDASHED:
|
|
|
130 |
case Border::BORDER_SLANTDASHDOT:
|
|
|
131 |
return Border::BORDER_DASHED;
|
|
|
132 |
|
|
|
133 |
case Border::BORDER_DOUBLE:
|
|
|
134 |
return Border::BORDER_DOUBLE;
|
|
|
135 |
|
|
|
136 |
case Border::BORDER_HAIR:
|
|
|
137 |
case Border::BORDER_MEDIUM:
|
|
|
138 |
case Border::BORDER_THICK:
|
|
|
139 |
case Border::BORDER_THIN:
|
|
|
140 |
return 'solid';
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
return 'solid';
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
private function writeCellProperties(CellStyle $style): void
|
|
|
147 |
{
|
|
|
148 |
// Align
|
|
|
149 |
$hAlign = $style->getAlignment()->getHorizontal();
|
|
|
150 |
$hAlign = $this->mapHorizontalAlignment($hAlign);
|
|
|
151 |
$vAlign = $style->getAlignment()->getVertical();
|
|
|
152 |
$wrap = $style->getAlignment()->getWrapText();
|
|
|
153 |
$indent = $style->getAlignment()->getIndent();
|
|
|
154 |
|
|
|
155 |
$this->writer->startElement('style:table-cell-properties');
|
|
|
156 |
if (!empty($vAlign) || $wrap) {
|
|
|
157 |
if (!empty($vAlign)) {
|
|
|
158 |
$vAlign = $this->mapVerticalAlignment($vAlign);
|
|
|
159 |
$this->writer->writeAttribute('style:vertical-align', $vAlign);
|
|
|
160 |
}
|
|
|
161 |
if ($wrap) {
|
|
|
162 |
$this->writer->writeAttribute('fo:wrap-option', 'wrap');
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
$this->writer->writeAttribute('style:rotation-align', 'none');
|
|
|
166 |
|
|
|
167 |
// Fill
|
|
|
168 |
$this->writeFillStyle($style->getFill());
|
|
|
169 |
|
|
|
170 |
// Border
|
|
|
171 |
$this->writeBordersStyle($style->getBorders());
|
|
|
172 |
|
|
|
173 |
$this->writer->endElement();
|
|
|
174 |
|
|
|
175 |
if ($hAlign !== '' || !empty($indent)) {
|
|
|
176 |
$this->writer
|
|
|
177 |
->startElement('style:paragraph-properties');
|
|
|
178 |
if ($hAlign !== '') {
|
|
|
179 |
$this->writer->writeAttribute('fo:text-align', $hAlign);
|
|
|
180 |
}
|
|
|
181 |
if (!empty($indent)) {
|
|
|
182 |
$indentString = sprintf('%.4f', $indent * self::INDENT_TO_INCHES) . 'in';
|
|
|
183 |
$this->writer->writeAttribute('fo:margin-left', $indentString);
|
|
|
184 |
}
|
|
|
185 |
$this->writer->endElement();
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
protected function mapUnderlineStyle(Font $font): string
|
|
|
190 |
{
|
|
|
191 |
return match ($font->getUnderline()) {
|
|
|
192 |
Font::UNDERLINE_DOUBLE, Font::UNDERLINE_DOUBLEACCOUNTING => 'double',
|
|
|
193 |
Font::UNDERLINE_SINGLE, Font::UNDERLINE_SINGLEACCOUNTING => 'single',
|
|
|
194 |
default => 'none',
|
|
|
195 |
};
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
protected function writeTextProperties(CellStyle $style): void
|
|
|
199 |
{
|
|
|
200 |
// Font
|
|
|
201 |
$this->writer->startElement('style:text-properties');
|
|
|
202 |
|
|
|
203 |
$font = $style->getFont();
|
|
|
204 |
|
|
|
205 |
if ($font->getBold()) {
|
|
|
206 |
$this->writer->writeAttribute('fo:font-weight', 'bold');
|
|
|
207 |
$this->writer->writeAttribute('style:font-weight-complex', 'bold');
|
|
|
208 |
$this->writer->writeAttribute('style:font-weight-asian', 'bold');
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
if ($font->getItalic()) {
|
|
|
212 |
$this->writer->writeAttribute('fo:font-style', 'italic');
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
$this->writer->writeAttribute('fo:color', sprintf('#%s', $font->getColor()->getRGB()));
|
|
|
216 |
|
|
|
217 |
if ($family = $font->getName()) {
|
|
|
218 |
$this->writer->writeAttribute('fo:font-family', $family);
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
if ($size = $font->getSize()) {
|
|
|
222 |
$this->writer->writeAttribute('fo:font-size', sprintf('%.1Fpt', $size));
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
if ($font->getUnderline() && $font->getUnderline() !== Font::UNDERLINE_NONE) {
|
|
|
226 |
$this->writer->writeAttribute('style:text-underline-style', 'solid');
|
|
|
227 |
$this->writer->writeAttribute('style:text-underline-width', 'auto');
|
|
|
228 |
$this->writer->writeAttribute('style:text-underline-color', 'font-color');
|
|
|
229 |
|
|
|
230 |
$underline = $this->mapUnderlineStyle($font);
|
|
|
231 |
$this->writer->writeAttribute('style:text-underline-type', $underline);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
$this->writer->endElement(); // Close style:text-properties
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
protected function writeColumnProperties(ColumnDimension $columnDimension): void
|
|
|
238 |
{
|
|
|
239 |
$this->writer->startElement('style:table-column-properties');
|
|
|
240 |
$this->writer->writeAttribute(
|
|
|
241 |
'style:column-width',
|
|
|
242 |
round($columnDimension->getWidth(Dimension::UOM_CENTIMETERS), 3) . 'cm'
|
|
|
243 |
);
|
|
|
244 |
$this->writer->writeAttribute('fo:break-before', 'auto');
|
|
|
245 |
|
|
|
246 |
// End
|
|
|
247 |
$this->writer->endElement(); // Close style:table-column-properties
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
public function writeColumnStyles(ColumnDimension $columnDimension, int $sheetId): void
|
|
|
251 |
{
|
|
|
252 |
$this->writer->startElement('style:style');
|
|
|
253 |
$this->writer->writeAttribute('style:family', 'table-column');
|
|
|
254 |
$this->writer->writeAttribute(
|
|
|
255 |
'style:name',
|
|
|
256 |
sprintf('%s_%d_%d', self::COLUMN_STYLE_PREFIX, $sheetId, $columnDimension->getColumnNumeric())
|
|
|
257 |
);
|
|
|
258 |
|
|
|
259 |
$this->writeColumnProperties($columnDimension);
|
|
|
260 |
|
|
|
261 |
// End
|
|
|
262 |
$this->writer->endElement(); // Close style:style
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
protected function writeRowProperties(RowDimension $rowDimension): void
|
|
|
266 |
{
|
|
|
267 |
$this->writer->startElement('style:table-row-properties');
|
|
|
268 |
$this->writer->writeAttribute(
|
|
|
269 |
'style:row-height',
|
|
|
270 |
round($rowDimension->getRowHeight(Dimension::UOM_CENTIMETERS), 3) . 'cm'
|
|
|
271 |
);
|
|
|
272 |
$this->writer->writeAttribute('style:use-optimal-row-height', 'false');
|
|
|
273 |
$this->writer->writeAttribute('fo:break-before', 'auto');
|
|
|
274 |
|
|
|
275 |
// End
|
|
|
276 |
$this->writer->endElement(); // Close style:table-row-properties
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
public function writeRowStyles(RowDimension $rowDimension, int $sheetId): void
|
|
|
280 |
{
|
|
|
281 |
$this->writer->startElement('style:style');
|
|
|
282 |
$this->writer->writeAttribute('style:family', 'table-row');
|
|
|
283 |
$this->writer->writeAttribute(
|
|
|
284 |
'style:name',
|
|
|
285 |
sprintf('%s_%d_%d', self::ROW_STYLE_PREFIX, $sheetId, $rowDimension->getRowIndex())
|
|
|
286 |
);
|
|
|
287 |
|
|
|
288 |
$this->writeRowProperties($rowDimension);
|
|
|
289 |
|
|
|
290 |
// End
|
|
|
291 |
$this->writer->endElement(); // Close style:style
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
public function writeTableStyle(Worksheet $worksheet, int $sheetId): void
|
|
|
295 |
{
|
|
|
296 |
$this->writer->startElement('style:style');
|
|
|
297 |
$this->writer->writeAttribute('style:family', 'table');
|
|
|
298 |
$this->writer->writeAttribute(
|
|
|
299 |
'style:name',
|
|
|
300 |
sprintf('%s%d', self::TABLE_STYLE_PREFIX, $sheetId)
|
|
|
301 |
);
|
|
|
302 |
$this->writer->writeAttribute('style:master-page-name', 'Default');
|
|
|
303 |
|
|
|
304 |
$this->writer->startElement('style:table-properties');
|
|
|
305 |
|
|
|
306 |
$this->writer->writeAttribute(
|
|
|
307 |
'table:display',
|
|
|
308 |
$worksheet->getSheetState() === Worksheet::SHEETSTATE_VISIBLE ? 'true' : 'false'
|
|
|
309 |
);
|
|
|
310 |
|
|
|
311 |
$this->writer->endElement(); // Close style:table-properties
|
|
|
312 |
$this->writer->endElement(); // Close style:style
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
public function write(CellStyle $style): void
|
|
|
316 |
{
|
|
|
317 |
$this->writer->startElement('style:style');
|
|
|
318 |
$this->writer->writeAttribute('style:name', self::CELL_STYLE_PREFIX . $style->getIndex());
|
|
|
319 |
$this->writer->writeAttribute('style:family', 'table-cell');
|
|
|
320 |
$this->writer->writeAttribute('style:parent-style-name', 'Default');
|
|
|
321 |
|
|
|
322 |
// Alignment, fill colour, etc
|
|
|
323 |
$this->writeCellProperties($style);
|
|
|
324 |
|
|
|
325 |
// style:text-properties
|
|
|
326 |
$this->writeTextProperties($style);
|
|
|
327 |
|
|
|
328 |
// End
|
|
|
329 |
$this->writer->endElement(); // Close style:style
|
|
|
330 |
}
|
|
|
331 |
}
|