1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\ODS\Manager\Style; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Entity\Style\BorderPart; |
6
|
|
|
use Box\Spout\Writer\Common\Entity\Worksheet; |
7
|
|
|
use Box\Spout\Writer\ODS\Helper\BorderHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class StyleManager |
11
|
|
|
* Manages styles to be applied to a cell |
12
|
|
|
*/ |
13
|
|
|
class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager |
14
|
|
|
{ |
15
|
|
|
/** @var StyleRegistry */ |
16
|
|
|
protected $styleRegistry; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Returns the content of the "styles.xml" file, given a list of styles. |
20
|
|
|
* |
21
|
|
|
* @param int $numWorksheets Number of worksheets created |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
34 |
|
public function getStylesXMLFileContent($numWorksheets) |
25
|
|
|
{ |
26
|
|
|
$content = <<<'EOD' |
27
|
34 |
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
28
|
|
|
<office:document-styles office:version="1.2" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:msoxl="http://schemas.microsoft.com/office/excel/formula" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:xlink="http://www.w3.org/1999/xlink"> |
29
|
|
|
EOD; |
30
|
|
|
|
31
|
34 |
|
$content .= $this->getFontFaceSectionContent(); |
32
|
34 |
|
$content .= $this->getStylesSectionContent(); |
33
|
34 |
|
$content .= $this->getAutomaticStylesSectionContent($numWorksheets); |
34
|
34 |
|
$content .= $this->getMasterStylesSectionContent($numWorksheets); |
35
|
|
|
|
36
|
|
|
$content .= <<<'EOD' |
37
|
34 |
|
</office:document-styles> |
38
|
|
|
EOD; |
39
|
|
|
|
40
|
34 |
|
return $content; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns the content of the "<office:font-face-decls>" section, inside "styles.xml" file. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
34 |
|
protected function getFontFaceSectionContent() |
49
|
|
|
{ |
50
|
34 |
|
$content = '<office:font-face-decls>'; |
51
|
34 |
|
foreach ($this->styleRegistry->getUsedFonts() as $fontName) { |
52
|
34 |
|
$content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>'; |
53
|
|
|
} |
54
|
34 |
|
$content .= '</office:font-face-decls>'; |
55
|
|
|
|
56
|
34 |
|
return $content; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the content of the "<office:styles>" section, inside "styles.xml" file. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
34 |
|
protected function getStylesSectionContent() |
65
|
|
|
{ |
66
|
34 |
|
$defaultStyle = $this->getDefaultStyle(); |
67
|
|
|
|
68
|
|
|
return <<<EOD |
69
|
|
|
<office:styles> |
70
|
|
|
<number:number-style style:name="N0"> |
71
|
|
|
<number:number number:min-integer-digits="1"/> |
72
|
|
|
</number:number-style> |
73
|
|
|
<style:style style:data-style-name="N0" style:family="table-cell" style:name="Default"> |
74
|
|
|
<style:table-cell-properties fo:background-color="transparent" style:vertical-align="automatic"/> |
75
|
34 |
|
<style:text-properties fo:color="#{$defaultStyle->getFontColor()}" |
76
|
34 |
|
fo:font-size="{$defaultStyle->getFontSize()}pt" style:font-size-asian="{$defaultStyle->getFontSize()}pt" style:font-size-complex="{$defaultStyle->getFontSize()}pt" |
77
|
34 |
|
style:font-name="{$defaultStyle->getFontName()}" style:font-name-asian="{$defaultStyle->getFontName()}" style:font-name-complex="{$defaultStyle->getFontName()}"/> |
78
|
|
|
</style:style> |
79
|
|
|
</office:styles> |
80
|
|
|
EOD; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns the content of the "<office:automatic-styles>" section, inside "styles.xml" file. |
85
|
|
|
* |
86
|
|
|
* @param int $numWorksheets Number of worksheets created |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
34 |
|
protected function getAutomaticStylesSectionContent($numWorksheets) |
90
|
|
|
{ |
91
|
34 |
|
$content = '<office:automatic-styles>'; |
92
|
|
|
|
93
|
34 |
|
for ($i = 1; $i <= $numWorksheets; $i++) { |
94
|
|
|
$content .= <<<EOD |
95
|
34 |
|
<style:page-layout style:name="pm$i"> |
96
|
|
|
<style:page-layout-properties style:first-page-number="continue" style:print="objects charts drawings" style:table-centering="none"/> |
97
|
|
|
<style:header-style/> |
98
|
|
|
<style:footer-style/> |
99
|
|
|
</style:page-layout> |
100
|
|
|
EOD; |
101
|
|
|
} |
102
|
|
|
|
103
|
34 |
|
$content .= '</office:automatic-styles>'; |
104
|
|
|
|
105
|
34 |
|
return $content; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the content of the "<office:master-styles>" section, inside "styles.xml" file. |
110
|
|
|
* |
111
|
|
|
* @param int $numWorksheets Number of worksheets created |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
34 |
|
protected function getMasterStylesSectionContent($numWorksheets) |
115
|
|
|
{ |
116
|
34 |
|
$content = '<office:master-styles>'; |
117
|
|
|
|
118
|
34 |
|
for ($i = 1; $i <= $numWorksheets; $i++) { |
119
|
|
|
$content .= <<<EOD |
120
|
34 |
|
<style:master-page style:name="mp$i" style:page-layout-name="pm$i"> |
121
|
|
|
<style:header/> |
122
|
|
|
<style:header-left style:display="false"/> |
123
|
|
|
<style:footer/> |
124
|
|
|
<style:footer-left style:display="false"/> |
125
|
|
|
</style:master-page> |
126
|
|
|
EOD; |
127
|
|
|
} |
128
|
|
|
|
129
|
34 |
|
$content .= '</office:master-styles>'; |
130
|
|
|
|
131
|
34 |
|
return $content; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the contents of the "<office:font-face-decls>" section, inside "content.xml" file. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
34 |
|
public function getContentXmlFontFaceSectionContent() |
140
|
|
|
{ |
141
|
34 |
|
$content = '<office:font-face-decls>'; |
142
|
34 |
|
foreach ($this->styleRegistry->getUsedFonts() as $fontName) { |
143
|
34 |
|
$content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>'; |
144
|
|
|
} |
145
|
34 |
|
$content .= '</office:font-face-decls>'; |
146
|
|
|
|
147
|
34 |
|
return $content; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns the contents of the "<office:automatic-styles>" section, inside "content.xml" file. |
152
|
|
|
* |
153
|
|
|
* @param Worksheet[] $worksheets |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
34 |
|
public function getContentXmlAutomaticStylesSectionContent($worksheets) |
157
|
|
|
{ |
158
|
34 |
|
$content = '<office:automatic-styles>'; |
159
|
|
|
|
160
|
34 |
|
foreach ($this->styleRegistry->getRegisteredStyles() as $style) { |
161
|
34 |
|
$content .= $this->getStyleSectionContent($style); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$content .= <<<'EOD' |
165
|
34 |
|
<style:style style:family="table-column" style:name="co1"> |
166
|
|
|
<style:table-column-properties fo:break-before="auto"/> |
167
|
|
|
</style:style> |
168
|
|
|
<style:style style:family="table-row" style:name="ro1"> |
169
|
|
|
<style:table-row-properties fo:break-before="auto" style:row-height="15pt" style:use-optimal-row-height="true"/> |
170
|
|
|
</style:style> |
171
|
|
|
EOD; |
172
|
|
|
|
173
|
34 |
|
foreach ($worksheets as $worksheet) { |
174
|
34 |
|
$worksheetId = $worksheet->getId(); |
175
|
34 |
|
$isSheetVisible = $worksheet->getExternalSheet()->isVisible() ? 'true' : 'false'; |
176
|
|
|
|
177
|
|
|
$content .= <<<EOD |
178
|
34 |
|
<style:style style:family="table" style:master-page-name="mp$worksheetId" style:name="ta$worksheetId"> |
179
|
34 |
|
<style:table-properties style:writing-mode="lr-tb" table:display="$isSheetVisible"/> |
180
|
|
|
</style:style> |
181
|
|
|
EOD; |
182
|
|
|
} |
183
|
|
|
|
184
|
34 |
|
$content .= '</office:automatic-styles>'; |
185
|
|
|
|
186
|
34 |
|
return $content; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the contents of the "<style:style>" section, inside "<office:automatic-styles>" section |
191
|
|
|
* |
192
|
|
|
* @param \Box\Spout\Common\Entity\Style\Style $style |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
34 |
|
protected function getStyleSectionContent($style) |
196
|
|
|
{ |
197
|
34 |
|
$styleIndex = $style->getId() + 1; // 1-based |
198
|
|
|
|
199
|
34 |
|
$content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce' . $styleIndex . '" style:parent-style-name="Default">'; |
200
|
|
|
|
201
|
34 |
|
$content .= $this->getTextPropertiesSectionContent($style); |
202
|
34 |
|
$content .= $this->getTableCellPropertiesSectionContent($style); |
203
|
|
|
|
204
|
34 |
|
$content .= '</style:style>'; |
205
|
|
|
|
206
|
34 |
|
return $content; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section |
211
|
|
|
* |
212
|
|
|
* @param \Box\Spout\Common\Entity\Style\Style $style |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
34 |
|
private function getTextPropertiesSectionContent($style) |
216
|
|
|
{ |
217
|
34 |
|
$content = ''; |
218
|
|
|
|
219
|
34 |
|
if ($style->shouldApplyFont()) { |
220
|
5 |
|
$content .= $this->getFontSectionContent($style); |
221
|
|
|
} |
222
|
|
|
|
223
|
34 |
|
return $content; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section |
228
|
|
|
* |
229
|
|
|
* @param \Box\Spout\Common\Entity\Style\Style $style |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
5 |
|
private function getFontSectionContent($style) |
233
|
|
|
{ |
234
|
5 |
|
$defaultStyle = $this->getDefaultStyle(); |
235
|
|
|
|
236
|
5 |
|
$content = '<style:text-properties'; |
237
|
|
|
|
238
|
5 |
|
$fontColor = $style->getFontColor(); |
239
|
5 |
|
if ($fontColor !== $defaultStyle->getFontColor()) { |
240
|
1 |
|
$content .= ' fo:color="#' . $fontColor . '"'; |
241
|
|
|
} |
242
|
|
|
|
243
|
5 |
|
$fontName = $style->getFontName(); |
244
|
5 |
|
if ($fontName !== $defaultStyle->getFontName()) { |
245
|
1 |
|
$content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"'; |
246
|
|
|
} |
247
|
|
|
|
248
|
5 |
|
$fontSize = $style->getFontSize(); |
249
|
5 |
|
if ($fontSize !== $defaultStyle->getFontSize()) { |
250
|
2 |
|
$content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"'; |
251
|
|
|
} |
252
|
|
|
|
253
|
5 |
|
if ($style->isFontBold()) { |
254
|
4 |
|
$content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"'; |
255
|
|
|
} |
256
|
5 |
|
if ($style->isFontItalic()) { |
257
|
1 |
|
$content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"'; |
258
|
|
|
} |
259
|
5 |
|
if ($style->isFontUnderline()) { |
260
|
2 |
|
$content .= ' style:text-underline-style="solid" style:text-underline-type="single"'; |
261
|
|
|
} |
262
|
5 |
|
if ($style->isFontStrikethrough()) { |
263
|
1 |
|
$content .= ' style:text-line-through-style="solid"'; |
264
|
|
|
} |
265
|
|
|
|
266
|
5 |
|
$content .= '/>'; |
267
|
|
|
|
268
|
5 |
|
return $content; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Returns the contents of the "<style:table-cell-properties>" section, inside "<style:style>" section |
273
|
|
|
* |
274
|
|
|
* @param \Box\Spout\Common\Entity\Style\Style $style |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
34 |
|
private function getTableCellPropertiesSectionContent($style) |
278
|
|
|
{ |
279
|
34 |
|
$content = ''; |
280
|
|
|
|
281
|
34 |
|
if ($style->shouldWrapText()) { |
282
|
3 |
|
$content .= $this->getWrapTextXMLContent(); |
283
|
|
|
} |
284
|
|
|
|
285
|
34 |
|
if ($style->shouldApplyBorder()) { |
286
|
1 |
|
$content .= $this->getBorderXMLContent($style); |
287
|
|
|
} |
288
|
|
|
|
289
|
34 |
|
if ($style->shouldApplyBackgroundColor()) { |
290
|
2 |
|
$content .= $this->getBackgroundColorXMLContent($style); |
291
|
|
|
} |
292
|
|
|
|
293
|
34 |
|
return $content; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Returns the contents of the wrap text definition for the "<style:table-cell-properties>" section |
298
|
|
|
* |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
3 |
|
private function getWrapTextXMLContent() |
302
|
|
|
{ |
303
|
3 |
|
return '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>'; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Returns the contents of the borders definition for the "<style:table-cell-properties>" section |
308
|
|
|
* |
309
|
|
|
* @param \Box\Spout\Common\Entity\Style\Style $style |
310
|
|
|
* @return string |
311
|
|
|
*/ |
312
|
1 |
|
private function getBorderXMLContent($style) |
313
|
|
|
{ |
314
|
1 |
|
$borderProperty = '<style:table-cell-properties %s />'; |
315
|
|
|
|
316
|
|
|
$borders = array_map(function (BorderPart $borderPart) { |
317
|
1 |
|
return BorderHelper::serializeBorderPart($borderPart); |
318
|
1 |
|
}, $style->getBorder()->getParts()); |
319
|
|
|
|
320
|
1 |
|
return sprintf($borderProperty, implode(' ', $borders)); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Returns the contents of the background color definition for the "<style:table-cell-properties>" section |
325
|
|
|
* |
326
|
|
|
* @param \Box\Spout\Common\Entity\Style\Style $style |
327
|
|
|
* @return string |
328
|
|
|
*/ |
329
|
2 |
|
private function getBackgroundColorXMLContent($style) |
330
|
|
|
{ |
331
|
2 |
|
return sprintf( |
332
|
2 |
|
'<style:table-cell-properties fo:background-color="#%s"/>', |
333
|
2 |
|
$style->getBackgroundColor() |
334
|
|
|
); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|