EOD;
$content .= $this->getFontFaceSectionContent();
$content .= $this->getStylesSectionContent();
$content .= $this->getAutomaticStylesSectionContent($numWorksheets);
$content .= $this->getMasterStylesSectionContent($numWorksheets);
$content .= <<<'EOD'
EOD;
return $content;
}
/**
* Returns the content of the "" section, inside "styles.xml" file.
*
* @return string
*/
protected function getFontFaceSectionContent()
{
$content = '';
foreach ($this->styleRegistry->getUsedFonts() as $fontName) {
$content .= '';
}
$content .= '';
return $content;
}
/**
* Returns the content of the "" section, inside "styles.xml" file.
*
* @return string
*/
protected function getStylesSectionContent()
{
$defaultStyle = $this->getDefaultStyle();
return <<
EOD;
}
/**
* Returns the content of the "" section, inside "styles.xml" file.
*
* @param int $numWorksheets Number of worksheets created
* @return string
*/
protected function getAutomaticStylesSectionContent($numWorksheets)
{
$content = '';
for ($i = 1; $i <= $numWorksheets; $i++) {
$content .= <<
EOD;
}
$content .= '';
return $content;
}
/**
* Returns the content of the "" section, inside "styles.xml" file.
*
* @param int $numWorksheets Number of worksheets created
* @return string
*/
protected function getMasterStylesSectionContent($numWorksheets)
{
$content = '';
for ($i = 1; $i <= $numWorksheets; $i++) {
$content .= <<
EOD;
}
$content .= '';
return $content;
}
/**
* Returns the contents of the "" section, inside "content.xml" file.
*
* @return string
*/
public function getContentXmlFontFaceSectionContent()
{
$content = '';
foreach ($this->styleRegistry->getUsedFonts() as $fontName) {
$content .= '';
}
$content .= '';
return $content;
}
/**
* Returns the contents of the "" section, inside "content.xml" file.
*
* @param Worksheet[] $worksheets
* @return string
*/
public function getContentXmlAutomaticStylesSectionContent($worksheets)
{
$content = '';
foreach ($this->styleRegistry->getRegisteredStyles() as $style) {
$content .= $this->getStyleSectionContent($style);
}
$content .= <<<'EOD'
EOD;
foreach ($worksheets as $worksheet) {
$worksheetId = $worksheet->getId();
$isSheetVisible = $worksheet->getExternalSheet()->isVisible() ? 'true' : 'false';
$content .= <<
EOD;
}
$content .= '';
return $content;
}
/**
* Returns the contents of the "" section, inside "" section
*
* @param \Box\Spout\Common\Entity\Style\Style $style
* @return string
*/
protected function getStyleSectionContent($style)
{
$styleIndex = $style->getId() + 1; // 1-based
$content = '';
$content .= $this->getTextPropertiesSectionContent($style);
$content .= $this->getParagraphPropertiesSectionContent($style);
$content .= $this->getTableCellPropertiesSectionContent($style);
$content .= '';
return $content;
}
/**
* Returns the contents of the "" section, inside "" section
*
* @param \Box\Spout\Common\Entity\Style\Style $style
* @return string
*/
private function getTextPropertiesSectionContent($style)
{
if (!$style->shouldApplyFont()) {
return '';
}
return 'getFontSectionContent($style)
. '/>';
}
/**
* Returns the contents of the fonts definition section, inside "" section
*
* @param \Box\Spout\Common\Entity\Style\Style $style
*
* @return string
*/
private function getFontSectionContent($style)
{
$defaultStyle = $this->getDefaultStyle();
$content = '';
$fontColor = $style->getFontColor();
if ($fontColor !== $defaultStyle->getFontColor()) {
$content .= ' fo:color="#' . $fontColor . '"';
}
$fontName = $style->getFontName();
if ($fontName !== $defaultStyle->getFontName()) {
$content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"';
}
$fontSize = $style->getFontSize();
if ($fontSize !== $defaultStyle->getFontSize()) {
$content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"';
}
if ($style->isFontBold()) {
$content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"';
}
if ($style->isFontItalic()) {
$content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"';
}
if ($style->isFontUnderline()) {
$content .= ' style:text-underline-style="solid" style:text-underline-type="single"';
}
if ($style->isFontStrikethrough()) {
$content .= ' style:text-line-through-style="solid"';
}
return $content;
}
/**
* Returns the contents of the "" section, inside "" section
*
* @param \Box\Spout\Common\Entity\Style\Style $style
*
* @return string
*/
private function getParagraphPropertiesSectionContent($style)
{
if (!$style->shouldApplyCellAlignment() && !$style->shouldApplyCellVerticalAlignment()) {
return '';
}
return 'getCellAlignmentSectionContent($style)
. $this->getCellVerticalAlignmentSectionContent($style)
. '/>';
}
/**
* Returns the contents of the cell alignment definition for the "" section
*
* @param \Box\Spout\Common\Entity\Style\Style $style
*
* @return string
*/
private function getCellAlignmentSectionContent($style)
{
return \sprintf(
' fo:text-align="%s" ',
$this->transformCellAlignment($style->getCellAlignment())
);
}
/**
* Returns the contents of the cell vertical alignment definition for the "" section
*
* @param \Box\Spout\Common\Entity\Style\Style $style
*
* @return string
*/
private function getCellVerticalAlignmentSectionContent($style)
{
return \sprintf(
' fo:vertical-align="%s" ',
$this->transformCellVerticalAlignment($style->getCellVerticalAlignment())
);
}
/**
* Even though "left" and "right" alignments are part of the spec, and interpreted
* respectively as "start" and "end", using the recommended values increase compatibility
* with software that will read the created ODS file.
*
* @param string $cellAlignment
*
* @return string
*/
private function transformCellAlignment($cellAlignment)
{
switch ($cellAlignment) {
case CellAlignment::LEFT: return 'start';
case CellAlignment::RIGHT: return 'end';
default: return $cellAlignment;
}
}
/**
* Spec uses 'middle' rather than 'center'
* http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#__RefHeading__1420236_253892949
*
* @param string $cellAlignment
*
* @return string
*/
private function transformCellVerticalAlignment($cellVerticalAlignment)
{
return ($cellVerticalAlignment === CellVerticalAlignment::CENTER)
? 'middle'
: $cellVerticalAlignment;
}
/**
* Returns the contents of the "" section, inside "" section
*
* @param \Box\Spout\Common\Entity\Style\Style $style
* @return string
*/
private function getTableCellPropertiesSectionContent($style)
{
$content = 'hasSetWrapText()) {
$content .= $this->getWrapTextXMLContent($style->shouldWrapText());
}
if ($style->shouldApplyBorder()) {
$content .= $this->getBorderXMLContent($style);
}
if ($style->shouldApplyBackgroundColor()) {
$content .= $this->getBackgroundColorXMLContent($style);
}
$content .= '/>';
return $content;
}
/**
* Returns the contents of the wrap text definition for the "" section
*
* @param boolean $shouldWrapText
* @return string
*/
private function getWrapTextXMLContent($shouldWrapText)
{
return ' fo:wrap-option="' . ($shouldWrapText ? '' : 'no-') . 'wrap" style:vertical-align="automatic" ';
}
/**
* Returns the contents of the borders definition for the "" section
*
* @param \Box\Spout\Common\Entity\Style\Style $style
* @return string
*/
private function getBorderXMLContent($style)
{
$borders = \array_map(function (BorderPart $borderPart) {
return BorderHelper::serializeBorderPart($borderPart);
}, $style->getBorder()->getParts());
return \sprintf(' %s ', \implode(' ', $borders));
}
/**
* Returns the contents of the background color definition for the "" section
*
* @param \Box\Spout\Common\Entity\Style\Style $style
* @return string
*/
private function getBackgroundColorXMLContent($style)
{
return \sprintf(' fo:background-color="#%s" ', $style->getBackgroundColor());
}
}