diff --git a/CHANGELOG.md b/CHANGELOG.md index 30551204..b0614611 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r - Config: Ability to use a config file to store various common settings - @ivanlanin GH-200 - ODT: Enable inline font style in TextRun - @ivanlanin - ODT: Enable underline, strike/doublestrike, smallcaps/allcaps, superscript/subscript font style - @ivanlanin +- ODT: Enable section and column - @ivanlanin ### Bugfixes diff --git a/samples/Sample_05_Multicolumn.php b/samples/Sample_05_Multicolumn.php index a3083824..f4737060 100644 --- a/samples/Sample_05_Multicolumn.php +++ b/samples/Sample_05_Multicolumn.php @@ -18,7 +18,7 @@ $section = $phpWord->addSection(array( 'colsNum' => 2, 'colsSpace' => 1440, 'breakType' => 'continuous')); -$section->addText('Three columns, one inch (1440 twips) spacing. ' . $filler); +$section->addText('Two columns, one inch (1440 twips) spacing. ' . $filler); // Normal $section = $phpWord->addSection(array('breakType' => 'continuous')); diff --git a/src/PhpWord/Writer/ODText/Part/Content.php b/src/PhpWord/Writer/ODText/Part/Content.php index f63deba6..fabc9432 100644 --- a/src/PhpWord/Writer/ODText/Part/Content.php +++ b/src/PhpWord/Writer/ODText/Part/Content.php @@ -27,6 +27,8 @@ use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Writer\ODText\Element\Container; +use PhpOffice\PhpWord\Writer\ODText\Style\Paragraph as ParagraphStyleWriter; +use PhpOffice\PhpWord\Writer\ODText\Style\Section as SectionStyleWriter; /** * ODText content part writer: content.xml @@ -58,9 +60,10 @@ class Content extends AbstractPart // Automatic styles $xmlWriter->startElement('office:automatic-styles'); - $this->writeTextAutoStyles($xmlWriter); - $this->writeImageAutoStyles($xmlWriter); - $this->writeTableAutoStyles($xmlWriter, $phpWord); + $this->writeSectionStyles($xmlWriter, $phpWord); + $this->writeTextStyles($xmlWriter); + $this->writeImageStyles($xmlWriter); + $this->writeTableStyles($xmlWriter, $phpWord); $xmlWriter->endElement(); // office:automatic-styles // Body @@ -81,10 +84,13 @@ class Content extends AbstractPart // Sections $sections = $phpWord->getSections(); foreach ($sections as $section) { - // $xmlWriter->startElement('text:section'); + $name = 'Section' . $section->getSectionId(); + $xmlWriter->startElement('text:section'); + $xmlWriter->writeAttribute('text:name', $name); + $xmlWriter->writeAttribute('text:style-name', $name); $containerWriter = new Container($xmlWriter, $section); $containerWriter->write(); - // $xmlWriter->endElement(); // text:section + $xmlWriter->endElement(); // text:section } $xmlWriter->endElement(); // office:text @@ -95,10 +101,27 @@ class Content extends AbstractPart return $xmlWriter->getData(); } + /** + * Write section automatic styles + * + * @since 0.11.0 + * @todo Put more section properties/styles + */ + private function writeSectionStyles(XMLWriter $xmlWriter, PhpWord $phpWord) + { + $sections = $phpWord->getSections(); + foreach ($sections as $section) { + $style = $section->getSettings(); + $style->setStyleName("Section{$section->getSectionId()}"); + $styleWriter = new SectionStyleWriter($xmlWriter, $style); + $styleWriter->write(); + } + } + /** * Write automatic styles */ - private function writeTextAutoStyles(XMLWriter $xmlWriter) + private function writeTextStyles(XMLWriter $xmlWriter) { $styles = Style::getStyles(); $paragraphStyleCount = 0; @@ -119,7 +142,7 @@ class Content extends AbstractPart $style = new Paragraph(); $style->setStyleName('P1'); $style->setAuto(); - $styleWriter = new \PhpOffice\PhpWord\Writer\ODText\Style\Paragraph($xmlWriter, $style); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); $styleWriter->write(); } } @@ -128,7 +151,7 @@ class Content extends AbstractPart /** * Write image automatic styles */ - private function writeImageAutoStyles(XMLWriter $xmlWriter) + private function writeImageStyles(XMLWriter $xmlWriter) { $images = Media::getElements('section'); foreach ($images as $image) { @@ -149,7 +172,7 @@ class Content extends AbstractPart /** * Write table automatic styles */ - private function writeTableAutoStyles(XMLWriter $xmlWriter, PhpWord $phpWord) + private function writeTableStyles(XMLWriter $xmlWriter, PhpWord $phpWord) { $sections = $phpWord->getSections(); foreach ($sections as $section) { diff --git a/src/PhpWord/Writer/ODText/Part/Styles.php b/src/PhpWord/Writer/ODText/Part/Styles.php index a226f7ff..de8ebd92 100644 --- a/src/PhpWord/Writer/ODText/Part/Styles.php +++ b/src/PhpWord/Writer/ODText/Part/Styles.php @@ -17,10 +17,12 @@ namespace PhpOffice\PhpWord\Writer\ODText\Part; +use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Shared\XMLWriter; +use PhpOffice\PhpWord\Style; /** - * ODText styloes part writer: styles.xml + * ODText styles part writer: styles.xml */ class Styles extends AbstractPart { @@ -43,14 +45,14 @@ class Styles extends AbstractPart // Office styles $xmlWriter->startElement('office:styles'); - $this->writePart($xmlWriter, 'Default'); - $this->writePart($xmlWriter, 'Named'); + $this->writeDefault($xmlWriter); + $this->writeNamed($xmlWriter); $xmlWriter->endElement(); // Automatic styles $xmlWriter->startElement('office:automatic-styles'); - $this->writePart($xmlWriter, 'PageLayout'); - $this->writePart($xmlWriter, 'Master'); + $this->writePageLayout($xmlWriter); + $this->writeMaster($xmlWriter); $xmlWriter->endElement(); $xmlWriter->endElement(); // office:document-styles @@ -59,15 +61,129 @@ class Styles extends AbstractPart } /** - * Write style part - * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param string $subStyle + * Write default styles */ - private function writePart(XMLWriter $xmlWriter, $subStyle) + private function writeDefault(XMLWriter $xmlWriter) { - $writerClass = "PhpOffice\\PhpWord\\Writer\\ODText\\Style\\{$subStyle}Style"; - $styleWriter = new $writerClass($xmlWriter); - $styleWriter->write(); + $xmlWriter->startElement('style:default-style'); + $xmlWriter->writeAttribute('style:family', 'paragraph'); + + // Paragraph + $xmlWriter->startElement('style:paragraph-properties'); + $xmlWriter->writeAttribute('fo:hyphenation-ladder-count', 'no-limit'); + $xmlWriter->writeAttribute('style:text-autospace', 'ideograph-alpha'); + $xmlWriter->writeAttribute('style:punctuation-wrap', 'hanging'); + $xmlWriter->writeAttribute('style:line-break', 'strict'); + $xmlWriter->writeAttribute('style:tab-stop-distance', '1.249cm'); + $xmlWriter->writeAttribute('style:writing-mode', 'page'); + $xmlWriter->endElement(); // style:paragraph-properties + + // Font + $xmlWriter->startElement('style:text-properties'); + $xmlWriter->writeAttribute('style:use-window-font-color', 'true'); + $xmlWriter->writeAttribute('style:font-name', Settings::getDefaultFontName()); + $xmlWriter->writeAttribute('fo:font-size', Settings::getDefaultFontSize() . 'pt'); + $xmlWriter->writeAttribute('fo:language', 'fr'); + $xmlWriter->writeAttribute('fo:country', 'FR'); + $xmlWriter->writeAttribute('style:letter-kerning', 'true'); + $xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2'); + $xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt'); + $xmlWriter->writeAttribute('style:language-asian', 'zh'); + $xmlWriter->writeAttribute('style:country-asian', 'CN'); + $xmlWriter->writeAttribute('style:font-name-complex', Settings::getDefaultFontName() . '2'); + $xmlWriter->writeAttribute('style:font-size-complex', Settings::getDefaultFontSize() . 'pt'); + $xmlWriter->writeAttribute('style:language-complex', 'hi'); + $xmlWriter->writeAttribute('style:country-complex', 'IN'); + $xmlWriter->writeAttribute('fo:hyphenate', 'false'); + $xmlWriter->writeAttribute('fo:hyphenation-remain-char-count', '2'); + $xmlWriter->writeAttribute('fo:hyphenation-push-char-count', '2'); + $xmlWriter->endElement(); // style:text-properties + + $xmlWriter->endElement(); // style:default-style + } + + /** + * Write named styles + */ + private function writeNamed(XMLWriter $xmlWriter) + { + $styles = Style::getStyles(); + if (count($styles) > 0) { + foreach ($styles as $style) { + if ($style->isAuto() === false) { + $styleClass = str_replace('\\Style\\', '\\Writer\\ODText\\Style\\', get_class($style)); + if (class_exists($styleClass)) { + /** @var $styleWriter \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle Type hint */ + $styleWriter = new $styleClass($xmlWriter, $style); + $styleWriter->write(); + } + } + } + } + } + /** + * Write page layout styles + */ + private function writePageLayout(XMLWriter $xmlWriter) + { + $xmlWriter->startElement('style:page-layout'); + $xmlWriter->writeAttribute('style:name', 'Mpm1'); + + $xmlWriter->startElement('style:page-layout-properties'); + $xmlWriter->writeAttribute('fo:page-width', "21.001cm"); + $xmlWriter->writeAttribute('fo:page-height', '29.7cm'); + $xmlWriter->writeAttribute('style:num-format', '1'); + $xmlWriter->writeAttribute('style:print-orientation', 'portrait'); + $xmlWriter->writeAttribute('fo:margin-top', '2.501cm'); + $xmlWriter->writeAttribute('fo:margin-bottom', '2cm'); + $xmlWriter->writeAttribute('fo:margin-left', '2.501cm'); + $xmlWriter->writeAttribute('fo:margin-right', '2.501cm'); + $xmlWriter->writeAttribute('style:writing-mode', 'lr-tb'); + $xmlWriter->writeAttribute('style:layout-grid-color', '#c0c0c0'); + $xmlWriter->writeAttribute('style:layout-grid-lines', '25199'); + $xmlWriter->writeAttribute('style:layout-grid-base-height', '0.423cm'); + $xmlWriter->writeAttribute('style:layout-grid-ruby-height', '0cm'); + $xmlWriter->writeAttribute('style:layout-grid-mode', 'none'); + $xmlWriter->writeAttribute('style:layout-grid-ruby-below', 'false'); + $xmlWriter->writeAttribute('style:layout-grid-print', 'false'); + $xmlWriter->writeAttribute('style:layout-grid-display', 'false'); + $xmlWriter->writeAttribute('style:layout-grid-base-width', '0.37cm'); + $xmlWriter->writeAttribute('style:layout-grid-snap-to', 'true'); + $xmlWriter->writeAttribute('style:footnote-max-height', '0cm'); + + $xmlWriter->startElement('style:footnote-sep'); + $xmlWriter->writeAttribute('style:width', '0.018cm'); + $xmlWriter->writeAttribute('style:line-style', 'solid'); + $xmlWriter->writeAttribute('style:adjustment', 'left'); + $xmlWriter->writeAttribute('style:rel-width', '25%'); + $xmlWriter->writeAttribute('style:color', '#000000'); + $xmlWriter->endElement(); //style:footnote-sep + + $xmlWriter->endElement(); // style:page-layout-properties + + + $xmlWriter->startElement('style:header-style'); + $xmlWriter->endElement(); // style:header-style + + $xmlWriter->startElement('style:footer-style'); + $xmlWriter->endElement(); // style:footer-style + + $xmlWriter->endElement(); // style:page-layout + } + /** + * Write master style + */ + private function writeMaster(XMLWriter $xmlWriter) + { + $xmlWriter = $this->getXmlWriter(); + + $xmlWriter->startElement('office:master-styles'); + + $xmlWriter->startElement('style:master-page'); + $xmlWriter->writeAttribute('style:name', 'Standard'); + $xmlWriter->writeAttribute('style:page-layout-name', 'Mpm1'); + $xmlWriter->endElement(); // style:master-page + + $xmlWriter->endElement(); // office:master-styles } } diff --git a/src/PhpWord/Writer/ODText/Style/DefaultStyle.php b/src/PhpWord/Writer/ODText/Style/DefaultStyle.php deleted file mode 100644 index d77157fa..00000000 --- a/src/PhpWord/Writer/ODText/Style/DefaultStyle.php +++ /dev/null @@ -1,72 +0,0 @@ -getXmlWriter(); - - $xmlWriter->startElement('style:default-style'); - $xmlWriter->writeAttribute('style:family', 'paragraph'); - - // Paragraph - $xmlWriter->startElement('style:paragraph-properties'); - $xmlWriter->writeAttribute('fo:hyphenation-ladder-count', 'no-limit'); - $xmlWriter->writeAttribute('style:text-autospace', 'ideograph-alpha'); - $xmlWriter->writeAttribute('style:punctuation-wrap', 'hanging'); - $xmlWriter->writeAttribute('style:line-break', 'strict'); - $xmlWriter->writeAttribute('style:tab-stop-distance', '1.249cm'); - $xmlWriter->writeAttribute('style:writing-mode', 'page'); - $xmlWriter->endElement(); // style:paragraph-properties - - // Font - $xmlWriter->startElement('style:text-properties'); - $xmlWriter->writeAttribute('style:use-window-font-color', 'true'); - $xmlWriter->writeAttribute('style:font-name', Settings::getDefaultFontName()); - $xmlWriter->writeAttribute('fo:font-size', Settings::getDefaultFontSize() . 'pt'); - $xmlWriter->writeAttribute('fo:language', 'fr'); - $xmlWriter->writeAttribute('fo:country', 'FR'); - $xmlWriter->writeAttribute('style:letter-kerning', 'true'); - $xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2'); - $xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt'); - $xmlWriter->writeAttribute('style:language-asian', 'zh'); - $xmlWriter->writeAttribute('style:country-asian', 'CN'); - $xmlWriter->writeAttribute('style:font-name-complex', Settings::getDefaultFontName() . '2'); - $xmlWriter->writeAttribute('style:font-size-complex', Settings::getDefaultFontSize() . 'pt'); - $xmlWriter->writeAttribute('style:language-complex', 'hi'); - $xmlWriter->writeAttribute('style:country-complex', 'IN'); - $xmlWriter->writeAttribute('fo:hyphenate', 'false'); - $xmlWriter->writeAttribute('fo:hyphenation-remain-char-count', '2'); - $xmlWriter->writeAttribute('fo:hyphenation-push-char-count', '2'); - $xmlWriter->endElement(); // style:text-properties - - $xmlWriter->endElement(); // style:default-style - } -} diff --git a/src/PhpWord/Writer/ODText/Style/MasterStyle.php b/src/PhpWord/Writer/ODText/Style/MasterStyle.php deleted file mode 100644 index 97d91a6a..00000000 --- a/src/PhpWord/Writer/ODText/Style/MasterStyle.php +++ /dev/null @@ -1,43 +0,0 @@ -getXmlWriter(); - - $xmlWriter->startElement('office:master-styles'); - - $xmlWriter->startElement('style:master-page'); - $xmlWriter->writeAttribute('style:name', 'Standard'); - $xmlWriter->writeAttribute('style:page-layout-name', 'Mpm1'); - $xmlWriter->endElement(); // style:master-page - - $xmlWriter->endElement(); // office:master-styles - } -} diff --git a/src/PhpWord/Writer/ODText/Style/PageLayoutStyle.php b/src/PhpWord/Writer/ODText/Style/PageLayoutStyle.php deleted file mode 100644 index cfcb6c3b..00000000 --- a/src/PhpWord/Writer/ODText/Style/PageLayoutStyle.php +++ /dev/null @@ -1,78 +0,0 @@ -getXmlWriter(); - - $xmlWriter->startElement('style:page-layout'); - $xmlWriter->writeAttribute('style:name', 'Mpm1'); - - $xmlWriter->startElement('style:page-layout-properties'); - $xmlWriter->writeAttribute('fo:page-width', "21.001cm"); - $xmlWriter->writeAttribute('fo:page-height', '29.7cm'); - $xmlWriter->writeAttribute('style:num-format', '1'); - $xmlWriter->writeAttribute('style:print-orientation', 'portrait'); - $xmlWriter->writeAttribute('fo:margin-top', '2.501cm'); - $xmlWriter->writeAttribute('fo:margin-bottom', '2cm'); - $xmlWriter->writeAttribute('fo:margin-left', '2.501cm'); - $xmlWriter->writeAttribute('fo:margin-right', '2.501cm'); - $xmlWriter->writeAttribute('style:writing-mode', 'lr-tb'); - $xmlWriter->writeAttribute('style:layout-grid-color', '#c0c0c0'); - $xmlWriter->writeAttribute('style:layout-grid-lines', '25199'); - $xmlWriter->writeAttribute('style:layout-grid-base-height', '0.423cm'); - $xmlWriter->writeAttribute('style:layout-grid-ruby-height', '0cm'); - $xmlWriter->writeAttribute('style:layout-grid-mode', 'none'); - $xmlWriter->writeAttribute('style:layout-grid-ruby-below', 'false'); - $xmlWriter->writeAttribute('style:layout-grid-print', 'false'); - $xmlWriter->writeAttribute('style:layout-grid-display', 'false'); - $xmlWriter->writeAttribute('style:layout-grid-base-width', '0.37cm'); - $xmlWriter->writeAttribute('style:layout-grid-snap-to', 'true'); - $xmlWriter->writeAttribute('style:footnote-max-height', '0cm'); - - $xmlWriter->startElement('style:footnote-sep'); - $xmlWriter->writeAttribute('style:width', '0.018cm'); - $xmlWriter->writeAttribute('style:line-style', 'solid'); - $xmlWriter->writeAttribute('style:adjustment', 'left'); - $xmlWriter->writeAttribute('style:rel-width', '25%'); - $xmlWriter->writeAttribute('style:color', '#000000'); - $xmlWriter->endElement(); //style:footnote-sep - - $xmlWriter->endElement(); // style:page-layout-properties - - - $xmlWriter->startElement('style:header-style'); - $xmlWriter->endElement(); // style:header-style - - $xmlWriter->startElement('style:footer-style'); - $xmlWriter->endElement(); // style:footer-style - - $xmlWriter->endElement(); // style:page-layout - } -} diff --git a/src/PhpWord/Writer/ODText/Style/NamedStyle.php b/src/PhpWord/Writer/ODText/Style/Section.php similarity index 52% rename from src/PhpWord/Writer/ODText/Style/NamedStyle.php rename to src/PhpWord/Writer/ODText/Style/Section.php index e2b63162..3d910157 100644 --- a/src/PhpWord/Writer/ODText/Style/NamedStyle.php +++ b/src/PhpWord/Writer/ODText/Style/Section.php @@ -17,34 +17,35 @@ namespace PhpOffice\PhpWord\Writer\ODText\Style; -use PhpOffice\PhpWord\Style; - /** - * Named style writer + * Section style writer * * @since 0.11.0 */ -class NamedStyle extends AbstractStyle +class Section extends AbstractStyle { /** * Write style */ public function write() { + /** @var \PhpOffice\PhpWord\Style\Section $style Type hint */ + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Section) { + return; + } $xmlWriter = $this->getXmlWriter(); - $styles = Style::getStyles(); - if (count($styles) > 0) { - foreach ($styles as $style) { - if ($style->isAuto() === false) { - $styleClass = str_replace('\\Style\\', '\\Writer\\ODText\\Style\\', get_class($style)); - if (class_exists($styleClass)) { - /** @var $styleWriter \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle Type hint */ - $styleWriter = new $styleClass($xmlWriter, $style); - $styleWriter->write(); - } - } - } - } + $xmlWriter->startElement('style:style'); + $xmlWriter->writeAttribute('style:name', $style->getStyleName()); + $xmlWriter->writeAttribute('style:family', "section"); + $xmlWriter->startElement('style:section-properties'); + + $xmlWriter->startElement('style:columns'); + $xmlWriter->writeAttribute('fo:column-count', $style->getColsNum()); + $xmlWriter->endElement(); // style:columns + + $xmlWriter->endElement(); // style:section-properties + $xmlWriter->endElement(); // style:style } } diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 961e9a55..06ee88d7 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -154,7 +154,7 @@ class Styles extends AbstractPart * Write default font and other default styles * * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter - * @param array $styles + * @param \PhpOffice\PhpWord\Style\AbstractStyle $styles */ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles) {