Skip to content

Commit 2cbdb0b

Browse files
committed
ODText Writer: Style writers
1 parent d25dc96 commit 2cbdb0b

File tree

7 files changed

+199
-78
lines changed

7 files changed

+199
-78
lines changed

src/PhpWord/Style.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ private static function setStyleValues($styleName, $styleObject, $styleValues =
167167
$styleObject->setStyleValue($key, $value);
168168
}
169169
}
170+
$styleObject->setStyleName($styleName);
170171
$styleObject->setIndex(self::countStyles() + 1); // One based index
171172
self::$styles[$styleName] = $styleObject;
172173
}

src/PhpWord/Style/AbstractStyle.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
*/
1919
abstract class AbstractStyle
2020
{
21+
/**
22+
* Style name
23+
*
24+
* @var string
25+
*/
26+
protected $styleName;
27+
2128
/**
2229
* Index number in Style collection for named style
2330
*
@@ -27,6 +34,29 @@ abstract class AbstractStyle
2734
*/
2835
protected $index;
2936

37+
/**
38+
* Get style name
39+
*
40+
* @return string
41+
*/
42+
public function getStyleName()
43+
{
44+
return $this->styleName;
45+
}
46+
47+
/**
48+
* Set style name
49+
*
50+
* @param string $value
51+
* @return self
52+
*/
53+
public function setStyleName($value)
54+
{
55+
$this->styleName = $value;
56+
57+
return $this;
58+
}
59+
3060
/**
3161
* Get index number
3262
*
@@ -41,6 +71,7 @@ public function getIndex()
4171
* Set index number
4272
*
4373
* @param integer|null $value
74+
* @return self
4475
*/
4576
public function setIndex($value = null)
4677
{

src/PhpWord/Writer/ODText/Content.php

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -110,47 +110,23 @@ private function writeAutomaticStyles(XMLWriter $xmlWriter, PhpWord $phpWord)
110110
if (preg_match('#^T[0-9]+$#', $styleName) != 0
111111
|| preg_match('#^P[0-9]+$#', $styleName) != 0
112112
) {
113-
// Font
114-
if ($style instanceof Font) {
115-
$xmlWriter->startElement('style:style');
116-
$xmlWriter->writeAttribute('style:name', $styleName);
117-
$xmlWriter->writeAttribute('style:family', 'text');
118-
// style:text-properties
119-
$xmlWriter->startElement('style:text-properties');
120-
$xmlWriter->writeAttribute('fo:color', '#' . $style->getColor());
121-
$xmlWriter->writeAttribute('style:font-name', $style->getName());
122-
$xmlWriter->writeAttribute('style:font-name-complex', $style->getName());
123-
$xmlWriter->endElement();
124-
$xmlWriter->endElement();
113+
$styleClass = str_replace('Style', 'Writer\\ODText\\Style', get_class($style));
114+
if (class_exists($styleClass)) {
115+
$styleWriter = new $styleClass($xmlWriter, $style);
116+
$styleWriter->setIsAuto(true);
117+
$styleWriter->write();
125118
}
126119
if ($style instanceof Paragraph) {
127120
$pStyleCount++;
128-
// style:style
129-
$xmlWriter->startElement('style:style');
130-
$xmlWriter->writeAttribute('style:name', $styleName);
131-
$xmlWriter->writeAttribute('style:family', 'paragraph');
132-
$xmlWriter->writeAttribute('style:parent-style-name', 'Standard');
133-
$xmlWriter->writeAttribute('style:master-page-name', 'Standard');
134-
// style:paragraph-properties
135-
$xmlWriter->startElement('style:paragraph-properties');
136-
$xmlWriter->writeAttribute('style:page-number', 'auto');
137-
$xmlWriter->endElement();
138-
$xmlWriter->endElement();
139121
}
140122
}
141123
}
142124
if ($pStyleCount == 0) {
143-
// style:style
144-
$xmlWriter->startElement('style:style');
145-
$xmlWriter->writeAttribute('style:name', 'P1');
146-
$xmlWriter->writeAttribute('style:family', 'paragraph');
147-
$xmlWriter->writeAttribute('style:parent-style-name', 'Standard');
148-
$xmlWriter->writeAttribute('style:master-page-name', 'Standard');
149-
// style:paragraph-properties
150-
$xmlWriter->startElement('style:paragraph-properties');
151-
$xmlWriter->writeAttribute('style:page-number', 'auto');
152-
$xmlWriter->endElement();
153-
$xmlWriter->endElement();
125+
$style = new Paragraph();
126+
$style->setStyleName('P1');
127+
$styleWriter = new \PhpOffice\PhpWord\Writer\ODText\Style\Paragraph($xmlWriter, $style);
128+
$styleWriter->setIsAuto(true);
129+
$styleWriter->write();
154130
}
155131
}
156132

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Writer\ODText\Style;
11+
12+
/**
13+
* Style writer
14+
*
15+
* @since 0.10.0
16+
*/
17+
abstract class AbstractStyle extends \PhpOffice\PhpWord\Writer\Word2007\Style\AbstractStyle
18+
{
19+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Writer\ODText\Style;
11+
12+
use PhpOffice\PhpWord\PhpWord;
13+
14+
/**
15+
* Font style writer
16+
*
17+
* @since 0.10.0
18+
*/
19+
class Font extends AbstractStyle
20+
{
21+
/**
22+
* Is automatic style
23+
*/
24+
private $isAuto = false;
25+
26+
/**
27+
* Write style
28+
*/
29+
public function write()
30+
{
31+
$this->xmlWriter->startElement('style:style');
32+
$this->xmlWriter->writeAttribute('style:name', $this->style->getStyleName());
33+
$this->xmlWriter->writeAttribute('style:family', 'text');
34+
$this->xmlWriter->startElement('style:text-properties');
35+
if ($this->style->getName()) {
36+
$this->xmlWriter->writeAttribute('style:font-name', $this->style->getName());
37+
$this->xmlWriter->writeAttribute('style:font-name-complex', $this->style->getName());
38+
}
39+
if ($this->style->getSize()) {
40+
$this->xmlWriter->writeAttribute('fo:font-size', ($this->style->getSize()) . 'pt');
41+
$this->xmlWriter->writeAttribute('style:font-size-asian', ($this->style->getSize()) . 'pt');
42+
$this->xmlWriter->writeAttribute('style:font-size-complex', ($this->style->getSize()) . 'pt');
43+
}
44+
if ($this->style->getColor()) {
45+
$this->xmlWriter->writeAttribute('fo:color', '#' . $this->style->getColor());
46+
}
47+
if ($this->style->getItalic()) {
48+
$this->xmlWriter->writeAttribute('fo:font-style', 'italic');
49+
$this->xmlWriter->writeAttribute('style:font-style-asian', 'italic');
50+
$this->xmlWriter->writeAttribute('style:font-style-complex', 'italic');
51+
}
52+
if ($this->style->getBold()) {
53+
$this->xmlWriter->writeAttribute('fo:font-weight', 'bold');
54+
$this->xmlWriter->writeAttribute('style:font-weight-asian', 'bold');
55+
}
56+
$this->xmlWriter->endElement(); // style:text-properties
57+
$this->xmlWriter->endElement(); // style:style
58+
}
59+
60+
/**
61+
* Set is automatic style
62+
*
63+
* @param bool $value
64+
*/
65+
public function setIsAuto($value)
66+
{
67+
$this->isAuto = $value;
68+
}
69+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Writer\ODText\Style;
11+
12+
use PhpOffice\PhpWord\PhpWord;
13+
14+
/**
15+
* Font style writer
16+
*
17+
* @since 0.10.0
18+
*/
19+
class Paragraph extends AbstractStyle
20+
{
21+
/**
22+
* Is automatic style
23+
*/
24+
private $isAuto = false;
25+
26+
/**
27+
* Write style
28+
*/
29+
public function write()
30+
{
31+
$marginTop = is_null($this->style->getSpaceBefore()) ? '0' : round(17.6 / $this->style->getSpaceBefore(), 2);
32+
$marginBottom = is_null($this->style->getSpaceAfter()) ? '0' : round(17.6 / $this->style->getSpaceAfter(), 2);
33+
34+
$this->xmlWriter->startElement('style:style');
35+
$this->xmlWriter->writeAttribute('style:name', $this->style->getStyleName());
36+
$this->xmlWriter->writeAttribute('style:family', 'paragraph');
37+
if ($this->isAuto) {
38+
$this->xmlWriter->writeAttribute('style:parent-style-name', 'Standard');
39+
$this->xmlWriter->writeAttribute('style:master-page-name', 'Standard');
40+
}
41+
42+
$this->xmlWriter->startElement('style:paragraph-properties');
43+
if ($this->isAuto) {
44+
$this->xmlWriter->writeAttribute('style:page-number', 'auto');
45+
} else {
46+
$this->xmlWriter->writeAttribute('fo:margin-top', $marginTop . 'cm');
47+
$this->xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm');
48+
$this->xmlWriter->writeAttribute('fo:text-align', $this->style->getAlign());
49+
}
50+
$this->xmlWriter->endElement(); //style:paragraph-properties
51+
52+
$this->xmlWriter->endElement(); //style:style
53+
}
54+
55+
/**
56+
* Set is automatic style
57+
*
58+
* @param bool $value
59+
*/
60+
public function setIsAuto($value)
61+
{
62+
$this->isAuto = $value;
63+
}
64+
}

src/PhpWord/Writer/ODText/Styles.php

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99

1010
namespace PhpOffice\PhpWord\Writer\ODText;
1111

12-
use PhpOffice\PhpWord\Exception\Exception;
1312
use PhpOffice\PhpWord\PhpWord;
14-
use PhpOffice\PhpWord\Style\Font;
15-
use PhpOffice\PhpWord\Style\Paragraph;
16-
use PhpOffice\PhpWord\Style\Table;
1713
use PhpOffice\PhpWord\Style;
14+
use PhpOffice\PhpWord\Exception\Exception;
1815

1916
/**
2017
* ODText styloes part writer
@@ -93,46 +90,10 @@ public function writeStyles(PhpWord $phpWord = null)
9390
if (preg_match('#^T[0-9]+$#', $styleName) == 0
9491
&& preg_match('#^P[0-9]+$#', $styleName) == 0
9592
) {
96-
// Font
97-
if ($style instanceof Font) {
98-
// style:style
99-
$xmlWriter->startElement('style:style');
100-
$xmlWriter->writeAttribute('style:name', $styleName);
101-
$xmlWriter->writeAttribute('style:family', 'text');
102-
103-
// style:text-properties
104-
$xmlWriter->startElement('style:text-properties');
105-
$xmlWriter->writeAttribute('fo:font-size', ($style->getSize()) . 'pt');
106-
$xmlWriter->writeAttribute('style:font-size-asian', ($style->getSize()) . 'pt');
107-
$xmlWriter->writeAttribute('style:font-size-complex', ($style->getSize()) . 'pt');
108-
if ($style->getItalic()) {
109-
$xmlWriter->writeAttribute('fo:font-style', 'italic');
110-
$xmlWriter->writeAttribute('style:font-style-asian', 'italic');
111-
$xmlWriter->writeAttribute('style:font-style-complex', 'italic');
112-
}
113-
if ($style->getBold()) {
114-
$xmlWriter->writeAttribute('fo:font-weight', 'bold');
115-
$xmlWriter->writeAttribute('style:font-weight-asian', 'bold');
116-
}
117-
$xmlWriter->endElement();
118-
$xmlWriter->endElement();
119-
} elseif ($style instanceof Paragraph) {
120-
// Paragraph
121-
// style:style
122-
$xmlWriter->startElement('style:style');
123-
$xmlWriter->writeAttribute('style:name', $styleName);
124-
$xmlWriter->writeAttribute('style:family', 'paragraph');
125-
126-
//style:paragraph-properties
127-
$xmlWriter->startElement('style:paragraph-properties');
128-
$xmlWriter->writeAttribute('fo:margin-top', ((is_null($style->getSpaceBefore())) ? '0' : round(17.6 / $style->getSpaceBefore(), 2)) . 'cm');
129-
$xmlWriter->writeAttribute('fo:margin-bottom', ((is_null($style->getSpaceAfter())) ? '0' : round(17.6 / $style->getSpaceAfter(), 2)) . 'cm');
130-
$xmlWriter->writeAttribute('fo:text-align', $style->getAlign());
131-
$xmlWriter->endElement();
132-
133-
$xmlWriter->endElement();
134-
} elseif ($style instanceof Table) {
135-
// Table
93+
$styleClass = str_replace('Style', 'Writer\\ODText\\Style', get_class($style));
94+
if (class_exists($styleClass)) {
95+
$styleWriter = new $styleClass($xmlWriter, $style);
96+
$styleWriter->write();
13697
}
13798
}
13899
}

0 commit comments

Comments
 (0)