1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\ODS\Helper; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Writer\Common\Helper\AbstractStyleHelper; |
6
|
|
|
use Box\Spout\Writer\Style\BorderPart; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class StyleHelper |
10
|
|
|
* This class provides helper functions to manage styles |
11
|
|
|
* |
12
|
|
|
* @package Box\Spout\Writer\ODS\Helper |
13
|
|
|
*/ |
14
|
|
|
class StyleHelper extends AbstractStyleHelper |
15
|
|
|
{ |
16
|
|
|
/** @var string[] [FONT_NAME] => [] Map whose keys contain all the fonts used */ |
17
|
|
|
protected $usedFontsSet = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Registers the given style as a used style. |
21
|
|
|
* Duplicate styles won't be registered more than once. |
22
|
|
|
* |
23
|
|
|
* @param \Box\Spout\Writer\Style\Style $style The style to be registered |
24
|
|
|
* @return \Box\Spout\Writer\Style\Style The registered style, updated with an internal ID. |
25
|
|
|
*/ |
26
|
|
|
public function registerStyle($style) |
27
|
|
|
{ |
28
|
|
|
$this->usedFontsSet[$style->getFontName()] = true; |
29
|
|
|
return parent::registerStyle($style); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string[] List of used fonts name |
34
|
|
|
*/ |
35
|
|
|
protected function getUsedFonts() |
36
|
|
|
{ |
37
|
|
|
return array_keys($this->usedFontsSet); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the content of the "styles.xml" file, given a list of styles. |
42
|
|
|
* |
43
|
|
|
* @param int $numWorksheets Number of worksheets created |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getStylesXMLFileContent($numWorksheets) |
47
|
|
|
{ |
48
|
|
|
$content = <<<EOD |
49
|
|
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
50
|
|
|
<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"> |
51
|
|
|
EOD; |
52
|
|
|
|
53
|
|
|
$content .= $this->getFontFaceSectionContent(); |
54
|
|
|
$content .= $this->getStylesSectionContent(); |
55
|
|
|
$content .= $this->getAutomaticStylesSectionContent($numWorksheets); |
56
|
|
|
$content .= $this->getMasterStylesSectionContent($numWorksheets); |
57
|
|
|
|
58
|
|
|
$content .= <<<EOD |
59
|
|
|
</office:document-styles> |
60
|
|
|
EOD; |
61
|
|
|
|
62
|
|
|
return $content; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the content of the "<office:font-face-decls>" section, inside "styles.xml" file. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
protected function getFontFaceSectionContent() |
71
|
|
|
{ |
72
|
|
|
$content = '<office:font-face-decls>'; |
73
|
|
|
foreach ($this->getUsedFonts() as $fontName) { |
74
|
|
|
$content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>'; |
75
|
|
|
} |
76
|
|
|
$content .= '</office:font-face-decls>'; |
77
|
|
|
|
78
|
|
|
return $content; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the content of the "<office:styles>" section, inside "styles.xml" file. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getStylesSectionContent() |
87
|
|
|
{ |
88
|
|
|
$defaultStyle = $this->getDefaultStyle(); |
89
|
|
|
|
90
|
|
|
return <<<EOD |
91
|
|
|
<office:styles> |
92
|
|
|
<number:number-style style:name="N0"> |
93
|
|
|
<number:number number:min-integer-digits="1"/> |
94
|
|
|
</number:number-style> |
95
|
|
|
<style:style style:data-style-name="N0" style:family="table-cell" style:name="Default"> |
96
|
|
|
<style:table-cell-properties fo:background-color="transparent" style:vertical-align="automatic"/> |
97
|
|
|
<style:text-properties fo:color="#{$defaultStyle->getFontColor()}" |
98
|
|
|
fo:font-size="{$defaultStyle->getFontSize()}pt" style:font-size-asian="{$defaultStyle->getFontSize()}pt" style:font-size-complex="{$defaultStyle->getFontSize()}pt" |
99
|
|
|
style:font-name="{$defaultStyle->getFontName()}" style:font-name-asian="{$defaultStyle->getFontName()}" style:font-name-complex="{$defaultStyle->getFontName()}"/> |
100
|
|
|
</style:style> |
101
|
|
|
</office:styles> |
102
|
|
|
EOD; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns the content of the "<office:automatic-styles>" section, inside "styles.xml" file. |
107
|
|
|
* |
108
|
|
|
* @param int $numWorksheets Number of worksheets created |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
protected function getAutomaticStylesSectionContent($numWorksheets) |
112
|
|
|
{ |
113
|
|
|
$content = '<office:automatic-styles>'; |
114
|
|
|
|
115
|
|
|
for ($i = 1; $i <= $numWorksheets; $i++) { |
116
|
|
|
$content .= <<<EOD |
117
|
|
|
<style:page-layout style:name="pm$i"> |
118
|
|
|
<style:page-layout-properties style:first-page-number="continue" style:print="objects charts drawings" style:table-centering="none"/> |
119
|
|
|
<style:header-style/> |
120
|
|
|
<style:footer-style/> |
121
|
|
|
</style:page-layout> |
122
|
|
|
EOD; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$content .= '</office:automatic-styles>'; |
126
|
|
|
|
127
|
|
|
return $content; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns the content of the "<office:master-styles>" section, inside "styles.xml" file. |
132
|
|
|
* |
133
|
|
|
* @param int $numWorksheets Number of worksheets created |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
protected function getMasterStylesSectionContent($numWorksheets) |
137
|
|
|
{ |
138
|
|
|
$content = '<office:master-styles>'; |
139
|
|
|
|
140
|
|
|
for ($i = 1; $i <= $numWorksheets; $i++) { |
141
|
|
|
$content .= <<<EOD |
142
|
|
|
<style:master-page style:name="mp$i" style:page-layout-name="pm$i"> |
143
|
|
|
<style:header/> |
144
|
|
|
<style:header-left style:display="false"/> |
145
|
|
|
<style:footer/> |
146
|
|
|
<style:footer-left style:display="false"/> |
147
|
|
|
</style:master-page> |
148
|
|
|
EOD; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$content .= '</office:master-styles>'; |
152
|
|
|
|
153
|
|
|
return $content; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns the contents of the "<office:font-face-decls>" section, inside "content.xml" file. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function getContentXmlFontFaceSectionContent() |
163
|
|
|
{ |
164
|
|
|
$content = '<office:font-face-decls>'; |
165
|
|
|
foreach ($this->getUsedFonts() as $fontName) { |
166
|
|
|
$content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>'; |
167
|
|
|
} |
168
|
|
|
$content .= '</office:font-face-decls>'; |
169
|
|
|
|
170
|
|
|
return $content; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns the contents of the "<office:automatic-styles>" section, inside "content.xml" file. |
175
|
|
|
* |
176
|
|
|
* @param int $numWorksheets Number of worksheets created |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getContentXmlAutomaticStylesSectionContent($numWorksheets) |
180
|
|
|
{ |
181
|
|
|
$content = '<office:automatic-styles>'; |
182
|
|
|
|
183
|
|
|
foreach ($this->getRegisteredStyles() as $style) { |
184
|
|
|
$content .= $this->getStyleSectionContent($style); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$content .= <<<EOD |
188
|
|
|
<style:style style:family="table-column" style:name="co1"> |
189
|
|
|
<style:table-column-properties fo:break-before="auto"/> |
190
|
|
|
</style:style> |
191
|
|
|
<style:style style:family="table-row" style:name="ro1"> |
192
|
|
|
<style:table-row-properties fo:break-before="auto" style:row-height="15pt" style:use-optimal-row-height="true"/> |
193
|
|
|
</style:style> |
194
|
|
|
EOD; |
195
|
|
|
|
196
|
|
|
for ($i = 1; $i <= $numWorksheets; $i++) { |
197
|
|
|
$content .= <<<EOD |
198
|
|
|
<style:style style:family="table" style:master-page-name="mp$i" style:name="ta$i"> |
199
|
|
|
<style:table-properties style:writing-mode="lr-tb" table:display="true"/> |
200
|
|
|
</style:style> |
201
|
|
|
EOD; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$content .= '</office:automatic-styles>'; |
205
|
|
|
|
206
|
|
|
return $content; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Returns the contents of the "<style:style>" section, inside "<office:automatic-styles>" section |
211
|
|
|
* |
212
|
|
|
* @param \Box\Spout\Writer\Style\Style $style |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
protected function getStyleSectionContent($style) |
216
|
|
|
{ |
217
|
|
|
$defaultStyle = $this->getDefaultStyle(); |
218
|
|
|
$styleIndex = $style->getId() + 1; // 1-based |
219
|
|
|
|
220
|
|
|
$content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce' . $styleIndex . '" style:parent-style-name="Default">'; |
221
|
|
|
|
222
|
|
|
if ($style->shouldApplyFont()) { |
223
|
|
|
$content .= '<style:text-properties'; |
224
|
|
|
|
225
|
|
|
$fontColor = $style->getFontColor(); |
226
|
|
|
if ($fontColor !== $defaultStyle->getFontColor()) { |
227
|
|
|
$content .= ' fo:color="#' . $fontColor . '"'; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$fontName = $style->getFontName(); |
231
|
|
|
if ($fontName !== $defaultStyle->getFontName()) { |
232
|
|
|
$content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"'; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$fontSize = $style->getFontSize(); |
236
|
|
|
if ($fontSize !== $defaultStyle->getFontSize()) { |
237
|
|
|
$content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"'; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if ($style->isFontBold()) { |
241
|
|
|
$content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"'; |
242
|
|
|
} |
243
|
|
|
if ($style->isFontItalic()) { |
244
|
|
|
$content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"'; |
245
|
|
|
} |
246
|
|
|
if ($style->isFontUnderline()) { |
247
|
|
|
$content .= ' style:text-underline-style="solid" style:text-underline-type="single"'; |
248
|
|
|
} |
249
|
|
|
if ($style->isFontStrikethrough()) { |
250
|
|
|
$content .= ' style:text-line-through-style="solid"'; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$content .= '/>'; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if ($style->shouldWrapText()) { |
257
|
|
|
$content .= '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>'; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
if ($style->shouldApplyBorder()) { |
261
|
|
|
$borderProperty = '<style:table-cell-properties %s />'; |
262
|
|
|
$borders = array_map(function (BorderPart $borderPart) { |
263
|
|
|
return BorderHelper::serializeBorderPart($borderPart); |
264
|
|
|
}, $style->getBorder()->getParts()); |
265
|
|
|
$content .= sprintf($borderProperty, implode(' ', $borders)); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$content .= '</style:style>'; |
269
|
|
|
|
270
|
|
|
return $content; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|