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\Fill; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Style as CellStyle; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
14
|
|
|
|
15
|
|
|
class Style |
16
|
|
|
{ |
17
|
|
|
public const CELL_STYLE_PREFIX = 'ce'; |
18
|
|
|
public const COLUMN_STYLE_PREFIX = 'co'; |
19
|
|
|
public const ROW_STYLE_PREFIX = 'ro'; |
20
|
|
|
public const TABLE_STYLE_PREFIX = 'ta'; |
21
|
|
|
|
22
|
|
|
private XMLWriter $writer; |
23
|
|
|
|
24
|
26 |
|
public function __construct(XMLWriter $writer) |
25
|
|
|
{ |
26
|
26 |
|
$this->writer = $writer; |
27
|
|
|
} |
28
|
|
|
|
29
|
26 |
|
private function mapHorizontalAlignment(string $horizontalAlignment): string |
30
|
|
|
{ |
31
|
26 |
|
return match ($horizontalAlignment) { |
32
|
26 |
|
Alignment::HORIZONTAL_CENTER, Alignment::HORIZONTAL_CENTER_CONTINUOUS, Alignment::HORIZONTAL_DISTRIBUTED => 'center', |
33
|
26 |
|
Alignment::HORIZONTAL_RIGHT => 'end', |
34
|
26 |
|
Alignment::HORIZONTAL_FILL, Alignment::HORIZONTAL_JUSTIFY => 'justify', |
35
|
26 |
|
default => 'start', |
36
|
26 |
|
}; |
37
|
|
|
} |
38
|
|
|
|
39
|
26 |
|
private function mapVerticalAlignment(string $verticalAlignment): string |
40
|
|
|
{ |
41
|
26 |
|
return match ($verticalAlignment) { |
42
|
26 |
|
Alignment::VERTICAL_TOP => 'top', |
43
|
26 |
|
Alignment::VERTICAL_CENTER => 'middle', |
44
|
26 |
|
Alignment::VERTICAL_DISTRIBUTED, Alignment::VERTICAL_JUSTIFY => 'automatic', |
45
|
26 |
|
default => 'bottom', |
46
|
26 |
|
}; |
47
|
|
|
} |
48
|
|
|
|
49
|
26 |
|
private function writeFillStyle(Fill $fill): void |
50
|
|
|
{ |
51
|
26 |
|
switch ($fill->getFillType()) { |
52
|
|
|
case Fill::FILL_SOLID: |
53
|
1 |
|
$this->writer->writeAttribute('fo:background-color', sprintf( |
54
|
1 |
|
'#%s', |
55
|
1 |
|
strtolower($fill->getStartColor()->getRGB()) |
56
|
1 |
|
)); |
57
|
|
|
|
58
|
1 |
|
break; |
59
|
|
|
case Fill::FILL_GRADIENT_LINEAR: |
60
|
|
|
case Fill::FILL_GRADIENT_PATH: |
61
|
|
|
/// TODO :: To be implemented |
62
|
|
|
break; |
63
|
|
|
case Fill::FILL_NONE: |
64
|
|
|
default: |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
26 |
|
private function writeCellProperties(CellStyle $style): void |
69
|
|
|
{ |
70
|
|
|
// Align |
71
|
26 |
|
$hAlign = $style->getAlignment()->getHorizontal(); |
72
|
26 |
|
$vAlign = $style->getAlignment()->getVertical(); |
73
|
26 |
|
$wrap = $style->getAlignment()->getWrapText(); |
74
|
|
|
|
75
|
26 |
|
$this->writer->startElement('style:table-cell-properties'); |
76
|
26 |
|
if (!empty($vAlign) || $wrap) { |
77
|
26 |
|
if (!empty($vAlign)) { |
78
|
26 |
|
$vAlign = $this->mapVerticalAlignment($vAlign); |
79
|
26 |
|
$this->writer->writeAttribute('style:vertical-align', $vAlign); |
80
|
|
|
} |
81
|
26 |
|
if ($wrap) { |
82
|
1 |
|
$this->writer->writeAttribute('fo:wrap-option', 'wrap'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
26 |
|
$this->writer->writeAttribute('style:rotation-align', 'none'); |
86
|
|
|
|
87
|
|
|
// Fill |
88
|
26 |
|
$this->writeFillStyle($style->getFill()); |
89
|
|
|
|
90
|
26 |
|
$this->writer->endElement(); |
91
|
|
|
|
92
|
26 |
|
if (!empty($hAlign)) { |
93
|
26 |
|
$hAlign = $this->mapHorizontalAlignment($hAlign); |
94
|
26 |
|
$this->writer->startElement('style:paragraph-properties'); |
95
|
26 |
|
$this->writer->writeAttribute('fo:text-align', $hAlign); |
96
|
26 |
|
$this->writer->endElement(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
protected function mapUnderlineStyle(Font $font): string |
101
|
|
|
{ |
102
|
1 |
|
return match ($font->getUnderline()) { |
103
|
1 |
|
Font::UNDERLINE_DOUBLE, Font::UNDERLINE_DOUBLEACCOUNTING => 'double', |
104
|
1 |
|
Font::UNDERLINE_SINGLE, Font::UNDERLINE_SINGLEACCOUNTING => 'single', |
105
|
1 |
|
default => 'none', |
106
|
1 |
|
}; |
107
|
|
|
} |
108
|
|
|
|
109
|
26 |
|
protected function writeTextProperties(CellStyle $style): void |
110
|
|
|
{ |
111
|
|
|
// Font |
112
|
26 |
|
$this->writer->startElement('style:text-properties'); |
113
|
|
|
|
114
|
26 |
|
$font = $style->getFont(); |
115
|
|
|
|
116
|
26 |
|
if ($font->getBold()) { |
117
|
1 |
|
$this->writer->writeAttribute('fo:font-weight', 'bold'); |
118
|
1 |
|
$this->writer->writeAttribute('style:font-weight-complex', 'bold'); |
119
|
1 |
|
$this->writer->writeAttribute('style:font-weight-asian', 'bold'); |
120
|
|
|
} |
121
|
|
|
|
122
|
26 |
|
if ($font->getItalic()) { |
123
|
1 |
|
$this->writer->writeAttribute('fo:font-style', 'italic'); |
124
|
|
|
} |
125
|
|
|
|
126
|
26 |
|
$this->writer->writeAttribute('fo:color', sprintf('#%s', $font->getColor()->getRGB())); |
127
|
|
|
|
128
|
26 |
|
if ($family = $font->getName()) { |
129
|
26 |
|
$this->writer->writeAttribute('fo:font-family', $family); |
130
|
|
|
} |
131
|
|
|
|
132
|
26 |
|
if ($size = $font->getSize()) { |
133
|
26 |
|
$this->writer->writeAttribute('fo:font-size', sprintf('%.1Fpt', $size)); |
134
|
|
|
} |
135
|
|
|
|
136
|
26 |
|
if ($font->getUnderline() && $font->getUnderline() !== Font::UNDERLINE_NONE) { |
137
|
1 |
|
$this->writer->writeAttribute('style:text-underline-style', 'solid'); |
138
|
1 |
|
$this->writer->writeAttribute('style:text-underline-width', 'auto'); |
139
|
1 |
|
$this->writer->writeAttribute('style:text-underline-color', 'font-color'); |
140
|
|
|
|
141
|
1 |
|
$underline = $this->mapUnderlineStyle($font); |
142
|
1 |
|
$this->writer->writeAttribute('style:text-underline-type', $underline); |
143
|
|
|
} |
144
|
|
|
|
145
|
26 |
|
$this->writer->endElement(); // Close style:text-properties |
146
|
|
|
} |
147
|
|
|
|
148
|
5 |
|
protected function writeColumnProperties(ColumnDimension $columnDimension): void |
149
|
|
|
{ |
150
|
5 |
|
$this->writer->startElement('style:table-column-properties'); |
151
|
5 |
|
$this->writer->writeAttribute( |
152
|
5 |
|
'style:column-width', |
153
|
5 |
|
round($columnDimension->getWidth(Dimension::UOM_CENTIMETERS), 3) . 'cm' |
154
|
5 |
|
); |
155
|
5 |
|
$this->writer->writeAttribute('fo:break-before', 'auto'); |
156
|
|
|
|
157
|
|
|
// End |
158
|
5 |
|
$this->writer->endElement(); // Close style:table-column-properties |
159
|
|
|
} |
160
|
|
|
|
161
|
5 |
|
public function writeColumnStyles(ColumnDimension $columnDimension, int $sheetId): void |
162
|
|
|
{ |
163
|
5 |
|
$this->writer->startElement('style:style'); |
164
|
5 |
|
$this->writer->writeAttribute('style:family', 'table-column'); |
165
|
5 |
|
$this->writer->writeAttribute( |
166
|
5 |
|
'style:name', |
167
|
5 |
|
sprintf('%s_%d_%d', self::COLUMN_STYLE_PREFIX, $sheetId, $columnDimension->getColumnNumeric()) |
168
|
5 |
|
); |
169
|
|
|
|
170
|
5 |
|
$this->writeColumnProperties($columnDimension); |
171
|
|
|
|
172
|
|
|
// End |
173
|
5 |
|
$this->writer->endElement(); // Close style:style |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
protected function writeRowProperties(RowDimension $rowDimension): void |
177
|
|
|
{ |
178
|
|
|
$this->writer->startElement('style:table-row-properties'); |
179
|
|
|
$this->writer->writeAttribute( |
180
|
|
|
'style:row-height', |
181
|
|
|
round($rowDimension->getRowHeight(Dimension::UOM_CENTIMETERS), 3) . 'cm' |
182
|
|
|
); |
183
|
|
|
$this->writer->writeAttribute('style:use-optimal-row-height', 'false'); |
184
|
|
|
$this->writer->writeAttribute('fo:break-before', 'auto'); |
185
|
|
|
|
186
|
|
|
// End |
187
|
|
|
$this->writer->endElement(); // Close style:table-row-properties |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function writeRowStyles(RowDimension $rowDimension, int $sheetId): void |
191
|
|
|
{ |
192
|
|
|
$this->writer->startElement('style:style'); |
193
|
|
|
$this->writer->writeAttribute('style:family', 'table-row'); |
194
|
|
|
$this->writer->writeAttribute( |
195
|
|
|
'style:name', |
196
|
|
|
sprintf('%s_%d_%d', self::ROW_STYLE_PREFIX, $sheetId, $rowDimension->getRowIndex()) |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
$this->writeRowProperties($rowDimension); |
200
|
|
|
|
201
|
|
|
// End |
202
|
|
|
$this->writer->endElement(); // Close style:style |
203
|
|
|
} |
204
|
|
|
|
205
|
26 |
|
public function writeTableStyle(Worksheet $worksheet, int $sheetId): void |
206
|
|
|
{ |
207
|
26 |
|
$this->writer->startElement('style:style'); |
208
|
26 |
|
$this->writer->writeAttribute('style:family', 'table'); |
209
|
26 |
|
$this->writer->writeAttribute( |
210
|
26 |
|
'style:name', |
211
|
26 |
|
sprintf('%s%d', self::TABLE_STYLE_PREFIX, $sheetId) |
212
|
26 |
|
); |
213
|
|
|
|
214
|
26 |
|
$this->writer->startElement('style:table-properties'); |
215
|
|
|
|
216
|
26 |
|
$this->writer->writeAttribute( |
217
|
26 |
|
'table:display', |
218
|
26 |
|
$worksheet->getSheetState() === Worksheet::SHEETSTATE_VISIBLE ? 'true' : 'false' |
219
|
26 |
|
); |
220
|
|
|
|
221
|
26 |
|
$this->writer->endElement(); // Close style:table-properties |
222
|
26 |
|
$this->writer->endElement(); // Close style:style |
223
|
|
|
} |
224
|
|
|
|
225
|
26 |
|
public function write(CellStyle $style): void |
226
|
|
|
{ |
227
|
26 |
|
$this->writer->startElement('style:style'); |
228
|
26 |
|
$this->writer->writeAttribute('style:name', self::CELL_STYLE_PREFIX . $style->getIndex()); |
229
|
26 |
|
$this->writer->writeAttribute('style:family', 'table-cell'); |
230
|
26 |
|
$this->writer->writeAttribute('style:parent-style-name', 'Default'); |
231
|
|
|
|
232
|
|
|
// Alignment, fill colour, etc |
233
|
26 |
|
$this->writeCellProperties($style); |
234
|
|
|
|
235
|
|
|
// style:text-properties |
236
|
26 |
|
$this->writeTextProperties($style); |
237
|
|
|
|
238
|
|
|
// End |
239
|
26 |
|
$this->writer->endElement(); // Close style:style |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|