ODT: Enable section and column

This commit is contained in:
Ivan Lanin 2014-05-18 16:29:56 +07:00
parent b91e3209fa
commit 410ce4bffe
9 changed files with 182 additions and 234 deletions

View File

@ -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

View File

@ -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'));

View File

@ -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) {

View File

@ -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);
$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
}
}

View File

@ -1,72 +0,0 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\ODText\Style;
use PhpOffice\PhpWord\Settings;
/**
* Default-style writer; cannot use `default` only because it's a reserved word
*
* @since 0.11.0
*/
class DefaultStyle extends AbstractStyle
{
/**
* Write style
*/
public function write()
{
$xmlWriter = $this->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
}
}

View File

@ -1,43 +0,0 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\ODText\Style;
/**
* Master style writer
*
* @since 0.11.0
*/
class MasterStyle extends AbstractStyle
{
/**
* Write style
*/
public function write()
{
$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
}
}

View File

@ -1,78 +0,0 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\ODText\Style;
/**
* Page layout style writer
*
* @since 0.11.0
*/
class PageLayoutStyle extends AbstractStyle
{
/**
* Write style
*/
public function write()
{
$xmlWriter = $this->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
}
}

View File

@ -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
}
}

View File

@ -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)
{