Style.php
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods\Cell;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\Style as CellStyle;
class Style
{
public const CELL_STYLE_PREFIX = 'ce';
private $writer;
public function __construct(XMLWriter $writer)
{
$this->writer = $writer;
}
private function mapHorizontalAlignment(string $horizontalAlignment): string
{
switch ($horizontalAlignment) {
case Alignment::HORIZONTAL_CENTER:
case Alignment::HORIZONTAL_CENTER_CONTINUOUS:
case Alignment::HORIZONTAL_DISTRIBUTED:
return 'center';
case Alignment::HORIZONTAL_RIGHT:
return 'end';
case Alignment::HORIZONTAL_FILL:
case Alignment::HORIZONTAL_JUSTIFY:
return 'justify';
}
return 'start';
}
private function mapVerticalAlignment(string $verticalAlignment): string
{
switch ($verticalAlignment) {
case Alignment::VERTICAL_TOP:
return 'top';
case Alignment::VERTICAL_CENTER:
return 'middle';
case Alignment::VERTICAL_DISTRIBUTED:
case Alignment::VERTICAL_JUSTIFY:
return 'automatic';
}
return 'bottom';
}
private function writeFillStyle(Fill $fill): void
{
switch ($fill->getFillType()) {
case Fill::FILL_SOLID:
$this->writer->writeAttribute('fo:background-color', sprintf(
'#%s',
strtolower($fill->getStartColor()->getRGB())
));
break;
case Fill::FILL_GRADIENT_LINEAR:
case Fill::FILL_GRADIENT_PATH:
/// TODO :: To be implemented
break;
case Fill::FILL_NONE:
default:
}
}
private function writeCellProperties(CellStyle $style): void
{
// Align
$hAlign = $style->getAlignment()->getHorizontal();
$vAlign = $style->getAlignment()->getVertical();
$wrap = $style->getAlignment()->getWrapText();
$this->writer->startElement('style:table-cell-properties');
if (!empty($vAlign) || $wrap) {
if (!empty($vAlign)) {
$vAlign = $this->mapVerticalAlignment($vAlign);
$this->writer->writeAttribute('style:vertical-align', $vAlign);
}
if ($wrap) {
$this->writer->writeAttribute('fo:wrap-option', 'wrap');
}
}
$this->writer->writeAttribute('style:rotation-align', 'none');
// Fill
if ($fill = $style->getFill()) {
$this->writeFillStyle($fill);
}
$this->writer->endElement();
if (!empty($hAlign)) {
$hAlign = $this->mapHorizontalAlignment($hAlign);
$this->writer->startElement('style:paragraph-properties');
$this->writer->writeAttribute('fo:text-align', $hAlign);
$this->writer->endElement();
}
}
protected function mapUnderlineStyle(Font $font): string
{
switch ($font->getUnderline()) {
case Font::UNDERLINE_DOUBLE:
case Font::UNDERLINE_DOUBLEACCOUNTING:
return'double';
case Font::UNDERLINE_SINGLE:
case Font::UNDERLINE_SINGLEACCOUNTING:
return'single';
}
return 'none';
}
protected function writeTextProperties(CellStyle $style): void
{
// Font
$this->writer->startElement('style:text-properties');
$font = $style->getFont();
if ($font->getBold()) {
$this->writer->writeAttribute('fo:font-weight', 'bold');
$this->writer->writeAttribute('style:font-weight-complex', 'bold');
$this->writer->writeAttribute('style:font-weight-asian', 'bold');
}
if ($font->getItalic()) {
$this->writer->writeAttribute('fo:font-style', 'italic');
}
if ($color = $font->getColor()) {
$this->writer->writeAttribute('fo:color', sprintf('#%s', $color->getRGB()));
}
if ($family = $font->getName()) {
$this->writer->writeAttribute('fo:font-family', $family);
}
if ($size = $font->getSize()) {
$this->writer->writeAttribute('fo:font-size', sprintf('%.1Fpt', $size));
}
if ($font->getUnderline() && $font->getUnderline() !== Font::UNDERLINE_NONE) {
$this->writer->writeAttribute('style:text-underline-style', 'solid');
$this->writer->writeAttribute('style:text-underline-width', 'auto');
$this->writer->writeAttribute('style:text-underline-color', 'font-color');
$underline = $this->mapUnderlineStyle($font);
$this->writer->writeAttribute('style:text-underline-type', $underline);
}
$this->writer->endElement(); // Close style:text-properties
}
public function write(CellStyle $style): void
{
$this->writer->startElement('style:style');
$this->writer->writeAttribute('style:name', self::CELL_STYLE_PREFIX . $style->getIndex());
$this->writer->writeAttribute('style:family', 'table-cell');
$this->writer->writeAttribute('style:parent-style-name', 'Default');
// Alignment, fill colour, etc
$this->writeCellProperties($style);
// style:text-properties
$this->writeTextProperties($style);
// End
$this->writer->endElement(); // Close style:style
}
}