diff --git a/src/PhpWord/Style.php b/src/PhpWord/Style.php index e0b5d439..012104c7 100755 --- a/src/PhpWord/Style.php +++ b/src/PhpWord/Style.php @@ -167,6 +167,7 @@ class Style $styleObject->setStyleValue($key, $value); } } + $styleObject->setStyleName($styleName); $styleObject->setIndex(self::countStyles() + 1); // One based index self::$styles[$styleName] = $styleObject; } diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php index ea77dc70..0967b27c 100644 --- a/src/PhpWord/Style/AbstractStyle.php +++ b/src/PhpWord/Style/AbstractStyle.php @@ -18,6 +18,13 @@ use PhpOffice\PhpWord\Shared\String; */ abstract class AbstractStyle { + /** + * Style name + * + * @var string + */ + protected $styleName; + /** * Index number in Style collection for named style * @@ -27,6 +34,29 @@ abstract class AbstractStyle */ protected $index; + /** + * Get style name + * + * @return string + */ + public function getStyleName() + { + return $this->styleName; + } + + /** + * Set style name + * + * @param string $value + * @return self + */ + public function setStyleName($value) + { + $this->styleName = $value; + + return $this; + } + /** * Get index number * @@ -41,6 +71,7 @@ abstract class AbstractStyle * Set index number * * @param integer|null $value + * @return self */ public function setIndex($value = null) { diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index 572ed9f6..69754e81 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -110,47 +110,23 @@ class Content extends Base if (preg_match('#^T[0-9]+$#', $styleName) != 0 || preg_match('#^P[0-9]+$#', $styleName) != 0 ) { - // Font - if ($style instanceof Font) { - $xmlWriter->startElement('style:style'); - $xmlWriter->writeAttribute('style:name', $styleName); - $xmlWriter->writeAttribute('style:family', 'text'); - // style:text-properties - $xmlWriter->startElement('style:text-properties'); - $xmlWriter->writeAttribute('fo:color', '#' . $style->getColor()); - $xmlWriter->writeAttribute('style:font-name', $style->getName()); - $xmlWriter->writeAttribute('style:font-name-complex', $style->getName()); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $styleClass = str_replace('Style', 'Writer\\ODText\\Style', get_class($style)); + if (class_exists($styleClass)) { + $styleWriter = new $styleClass($xmlWriter, $style); + $styleWriter->setIsAuto(true); + $styleWriter->write(); } if ($style instanceof Paragraph) { $pStyleCount++; - // style:style - $xmlWriter->startElement('style:style'); - $xmlWriter->writeAttribute('style:name', $styleName); - $xmlWriter->writeAttribute('style:family', 'paragraph'); - $xmlWriter->writeAttribute('style:parent-style-name', 'Standard'); - $xmlWriter->writeAttribute('style:master-page-name', 'Standard'); - // style:paragraph-properties - $xmlWriter->startElement('style:paragraph-properties'); - $xmlWriter->writeAttribute('style:page-number', 'auto'); - $xmlWriter->endElement(); - $xmlWriter->endElement(); } } } if ($pStyleCount == 0) { - // style:style - $xmlWriter->startElement('style:style'); - $xmlWriter->writeAttribute('style:name', 'P1'); - $xmlWriter->writeAttribute('style:family', 'paragraph'); - $xmlWriter->writeAttribute('style:parent-style-name', 'Standard'); - $xmlWriter->writeAttribute('style:master-page-name', 'Standard'); - // style:paragraph-properties - $xmlWriter->startElement('style:paragraph-properties'); - $xmlWriter->writeAttribute('style:page-number', 'auto'); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $style = new Paragraph(); + $style->setStyleName('P1'); + $styleWriter = new \PhpOffice\PhpWord\Writer\ODText\Style\Paragraph($xmlWriter, $style); + $styleWriter->setIsAuto(true); + $styleWriter->write(); } } diff --git a/src/PhpWord/Writer/ODText/Style/AbstractStyle.php b/src/PhpWord/Writer/ODText/Style/AbstractStyle.php new file mode 100644 index 00000000..30416b5d --- /dev/null +++ b/src/PhpWord/Writer/ODText/Style/AbstractStyle.php @@ -0,0 +1,19 @@ +xmlWriter->startElement('style:style'); + $this->xmlWriter->writeAttribute('style:name', $this->style->getStyleName()); + $this->xmlWriter->writeAttribute('style:family', 'text'); + $this->xmlWriter->startElement('style:text-properties'); + if ($this->style->getName()) { + $this->xmlWriter->writeAttribute('style:font-name', $this->style->getName()); + $this->xmlWriter->writeAttribute('style:font-name-complex', $this->style->getName()); + } + if ($this->style->getSize()) { + $this->xmlWriter->writeAttribute('fo:font-size', ($this->style->getSize()) . 'pt'); + $this->xmlWriter->writeAttribute('style:font-size-asian', ($this->style->getSize()) . 'pt'); + $this->xmlWriter->writeAttribute('style:font-size-complex', ($this->style->getSize()) . 'pt'); + } + if ($this->style->getColor()) { + $this->xmlWriter->writeAttribute('fo:color', '#' . $this->style->getColor()); + } + if ($this->style->getItalic()) { + $this->xmlWriter->writeAttribute('fo:font-style', 'italic'); + $this->xmlWriter->writeAttribute('style:font-style-asian', 'italic'); + $this->xmlWriter->writeAttribute('style:font-style-complex', 'italic'); + } + if ($this->style->getBold()) { + $this->xmlWriter->writeAttribute('fo:font-weight', 'bold'); + $this->xmlWriter->writeAttribute('style:font-weight-asian', 'bold'); + } + $this->xmlWriter->endElement(); // style:text-properties + $this->xmlWriter->endElement(); // style:style + } + + /** + * Set is automatic style + * + * @param bool $value + */ + public function setIsAuto($value) + { + $this->isAuto = $value; + } +} diff --git a/src/PhpWord/Writer/ODText/Style/Paragraph.php b/src/PhpWord/Writer/ODText/Style/Paragraph.php new file mode 100644 index 00000000..9203d5a2 --- /dev/null +++ b/src/PhpWord/Writer/ODText/Style/Paragraph.php @@ -0,0 +1,64 @@ +style->getSpaceBefore()) ? '0' : round(17.6 / $this->style->getSpaceBefore(), 2); + $marginBottom = is_null($this->style->getSpaceAfter()) ? '0' : round(17.6 / $this->style->getSpaceAfter(), 2); + + $this->xmlWriter->startElement('style:style'); + $this->xmlWriter->writeAttribute('style:name', $this->style->getStyleName()); + $this->xmlWriter->writeAttribute('style:family', 'paragraph'); + if ($this->isAuto) { + $this->xmlWriter->writeAttribute('style:parent-style-name', 'Standard'); + $this->xmlWriter->writeAttribute('style:master-page-name', 'Standard'); + } + + $this->xmlWriter->startElement('style:paragraph-properties'); + if ($this->isAuto) { + $this->xmlWriter->writeAttribute('style:page-number', 'auto'); + } else { + $this->xmlWriter->writeAttribute('fo:margin-top', $marginTop . 'cm'); + $this->xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm'); + $this->xmlWriter->writeAttribute('fo:text-align', $this->style->getAlign()); + } + $this->xmlWriter->endElement(); //style:paragraph-properties + + $this->xmlWriter->endElement(); //style:style + } + + /** + * Set is automatic style + * + * @param bool $value + */ + public function setIsAuto($value) + { + $this->isAuto = $value; + } +} diff --git a/src/PhpWord/Writer/ODText/Styles.php b/src/PhpWord/Writer/ODText/Styles.php index 8ca18760..f21502fc 100644 --- a/src/PhpWord/Writer/ODText/Styles.php +++ b/src/PhpWord/Writer/ODText/Styles.php @@ -9,12 +9,9 @@ namespace PhpOffice\PhpWord\Writer\ODText; -use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Style\Font; -use PhpOffice\PhpWord\Style\Paragraph; -use PhpOffice\PhpWord\Style\Table; use PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Exception\Exception; /** * ODText styloes part writer @@ -93,46 +90,10 @@ class Styles extends Base if (preg_match('#^T[0-9]+$#', $styleName) == 0 && preg_match('#^P[0-9]+$#', $styleName) == 0 ) { - // Font - if ($style instanceof Font) { - // style:style - $xmlWriter->startElement('style:style'); - $xmlWriter->writeAttribute('style:name', $styleName); - $xmlWriter->writeAttribute('style:family', 'text'); - - // style:text-properties - $xmlWriter->startElement('style:text-properties'); - $xmlWriter->writeAttribute('fo:font-size', ($style->getSize()) . 'pt'); - $xmlWriter->writeAttribute('style:font-size-asian', ($style->getSize()) . 'pt'); - $xmlWriter->writeAttribute('style:font-size-complex', ($style->getSize()) . 'pt'); - if ($style->getItalic()) { - $xmlWriter->writeAttribute('fo:font-style', 'italic'); - $xmlWriter->writeAttribute('style:font-style-asian', 'italic'); - $xmlWriter->writeAttribute('style:font-style-complex', 'italic'); - } - if ($style->getBold()) { - $xmlWriter->writeAttribute('fo:font-weight', 'bold'); - $xmlWriter->writeAttribute('style:font-weight-asian', 'bold'); - } - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } elseif ($style instanceof Paragraph) { - // Paragraph - // style:style - $xmlWriter->startElement('style:style'); - $xmlWriter->writeAttribute('style:name', $styleName); - $xmlWriter->writeAttribute('style:family', 'paragraph'); - - //style:paragraph-properties - $xmlWriter->startElement('style:paragraph-properties'); - $xmlWriter->writeAttribute('fo:margin-top', ((is_null($style->getSpaceBefore())) ? '0' : round(17.6 / $style->getSpaceBefore(), 2)) . 'cm'); - $xmlWriter->writeAttribute('fo:margin-bottom', ((is_null($style->getSpaceAfter())) ? '0' : round(17.6 / $style->getSpaceAfter(), 2)) . 'cm'); - $xmlWriter->writeAttribute('fo:text-align', $style->getAlign()); - $xmlWriter->endElement(); - - $xmlWriter->endElement(); - } elseif ($style instanceof Table) { - // Table + $styleClass = str_replace('Style', 'Writer\\ODText\\Style', get_class($style)); + if (class_exists($styleClass)) { + $styleWriter = new $styleClass($xmlWriter, $style); + $styleWriter->write(); } } }