ODText Writer: Style writers

This commit is contained in:
Ivan Lanin 2014-04-26 19:06:44 +07:00
parent d25dc965c9
commit 2cbdb0b383
7 changed files with 199 additions and 78 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,19 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\ODText\Style;
/**
* Style writer
*
* @since 0.10.0
*/
abstract class AbstractStyle extends \PhpOffice\PhpWord\Writer\Word2007\Style\AbstractStyle
{
}

View File

@ -0,0 +1,69 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\ODText\Style;
use PhpOffice\PhpWord\PhpWord;
/**
* Font style writer
*
* @since 0.10.0
*/
class Font extends AbstractStyle
{
/**
* Is automatic style
*/
private $isAuto = false;
/**
* Write style
*/
public function write()
{
$this->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;
}
}

View File

@ -0,0 +1,64 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\ODText\Style;
use PhpOffice\PhpWord\PhpWord;
/**
* Font style writer
*
* @since 0.10.0
*/
class Paragraph extends AbstractStyle
{
/**
* Is automatic style
*/
private $isAuto = false;
/**
* Write style
*/
public function write()
{
$marginTop = is_null($this->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;
}
}

View File

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