| 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 |
$styleIndex = $style->getId() + 1; // 1-based |
| 218 |
|
| 219 |
$content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce' . $styleIndex . '" style:parent-style-name="Default">'; |
| 220 |
|
| 221 |
$content .= $this->getTextPropertiesSectionContent($style); |
| 222 |
$content .= $this->getTableCellPropertiesSectionContent($style); |
| 223 |
|
| 224 |
$content .= '</style:style>'; |
| 225 |
|
| 226 |
return $content; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section |
| 231 |
* |
| 232 |
* @param \Box\Spout\Writer\Style\Style $style |
| 233 |
* @return string |
| 234 |
*/ |
| 235 |
private function getTextPropertiesSectionContent($style) |
| 236 |
{ |
| 237 |
$content = ''; |
| 238 |
|
| 239 |
if ($style->shouldApplyFont()) { |
| 240 |
$content .= $this->getFontSectionContent($style); |
| 241 |
} |
| 242 |
|
| 243 |
return $content; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section |
| 248 |
* |
| 249 |
* @param \Box\Spout\Writer\Style\Style $style |
| 250 |
* @return string |
| 251 |
*/ |
| 252 |
private function getFontSectionContent($style) |
| 253 |
{ |
| 254 |
$defaultStyle = $this->getDefaultStyle(); |
| 255 |
|
| 256 |
$content = '<style:text-properties'; |
| 257 |
|
| 258 |
$fontColor = $style->getFontColor(); |
| 259 |
if ($fontColor !== $defaultStyle->getFontColor()) { |
| 260 |
$content .= ' fo:color="#' . $fontColor . '"'; |
| 261 |
} |
| 262 |
|
| 263 |
$fontName = $style->getFontName(); |
| 264 |
if ($fontName !== $defaultStyle->getFontName()) { |
| 265 |
$content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"'; |
| 266 |
} |
| 267 |
|
| 268 |
$fontSize = $style->getFontSize(); |
| 269 |
if ($fontSize !== $defaultStyle->getFontSize()) { |
| 270 |
$content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"'; |
| 271 |
} |
| 272 |
|
| 273 |
if ($style->isFontBold()) { |
| 274 |
$content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"'; |
| 275 |
} |
| 276 |
if ($style->isFontItalic()) { |
| 277 |
$content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"'; |
| 278 |
} |
| 279 |
if ($style->isFontUnderline()) { |
| 280 |
$content .= ' style:text-underline-style="solid" style:text-underline-type="single"'; |
| 281 |
} |
| 282 |
if ($style->isFontStrikethrough()) { |
| 283 |
$content .= ' style:text-line-through-style="solid"'; |
| 284 |
} |
| 285 |
|
| 286 |
$content .= '/>'; |
| 287 |
|
| 288 |
return $content; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Returns the contents of the "<style:table-cell-properties>" section, inside "<style:style>" section |
| 293 |
* |
| 294 |
* @param \Box\Spout\Writer\Style\Style $style |
| 295 |
* @return string |
| 296 |
*/ |
| 297 |
private function getTableCellPropertiesSectionContent($style) |
| 298 |
{ |
| 299 |
$content = ''; |
| 300 |
|
| 301 |
if ($style->shouldWrapText()) { |
| 302 |
$content .= $this->getWrapTextXMLContent(); |
| 303 |
} |
| 304 |
|
| 305 |
if ($style->shouldApplyBorder()) { |
| 306 |
$content .= $this->getBorderXMLContent($style); |
| 307 |
} |
| 308 |
|
| 309 |
if ($style->shouldApplyBackgroundColor()) { |
| 310 |
$content .= $this->getBackgroundColorXMLContent($style); |
| 311 |
} |
| 312 |
|
| 313 |
return $content; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Returns the contents of the wrap text definition for the "<style:table-cell-properties>" section |
| 318 |
* |
| 319 |
* @return string |
| 320 |
*/ |
| 321 |
private function getWrapTextXMLContent() |
| 322 |
{ |
| 323 |
return '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>'; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Returns the contents of the borders definition for the "<style:table-cell-properties>" section |
| 328 |
* |
| 329 |
* @param \Box\Spout\Writer\Style\Style $style |
| 330 |
* @return string |
| 331 |
*/ |
| 332 |
private function getBorderXMLContent($style) |
| 333 |
{ |
| 334 |
$borderProperty = '<style:table-cell-properties %s />'; |
| 335 |
|
| 336 |
$borders = array_map(function (BorderPart $borderPart) { |
| 337 |
return BorderHelper::serializeBorderPart($borderPart); |
| 338 |
}, $style->getBorder()->getParts()); |
| 339 |
|
| 340 |
return sprintf($borderProperty, implode(' ', $borders)); |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Returns the contents of the background color definition for the "<style:table-cell-properties>" section |
| 345 |
* |
| 346 |
* @param \Box\Spout\Writer\Style\Style $style |
| 347 |
* @return string |
| 348 |
*/ |
| 349 |
private function getBackgroundColorXMLContent($style) |
| 350 |
{ |
| 351 |
return sprintf( |
| 352 |
'<style:table-cell-properties fo:background-color="#%s"/>', |
| 353 |
$style->getBackgroundColor() |
| 354 |
); |
| 355 |
} |
| 356 |
} |
| 357 |
|