Skip to content

Commit 7863e39

Browse files
committed
Writer: Style writers
1 parent ec9d900 commit 7863e39

32 files changed

+994
-625
lines changed

src/PhpWord/TOC.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99

1010
namespace PhpOffice\PhpWord;
1111

12-
use PhpOffice\PhpWord\Style\Font;
13-
use PhpOffice\PhpWord\Style\TOC as TOCStyle;
14-
1512
/**
1613
* Table of contents
1714
*/

src/PhpWord/Writer/HTML.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
use PhpOffice\PhpWord\Style\Paragraph;
1818
use PhpOffice\PhpWord\Writer\HTML\Element\Element as ElementWriter;
1919
use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter;
20-
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
20+
use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter;
21+
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
22+
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
2123

2224
/**
2325
* HTML writer
@@ -212,26 +214,26 @@ private function writeStyles()
212214
),
213215
);
214216
foreach ($defaultStyles as $selector => $style) {
215-
$styleWriter = new StyleWriter();
216-
$css .= $selector . ' ';
217-
$css .= $styleWriter->assembleCss($style, true) . PHP_EOL;
217+
$styleWriter = new GenericStyleWriter($style);
218+
$css .= $selector . ' {' . $styleWriter->write() . '}' . PHP_EOL;
218219
}
219220

220221
// Custom styles
221222
$customStyles = Style::getStyles();
222223
if (is_array($customStyles)) {
223224
foreach ($customStyles as $name => $style) {
224-
$styleWriter = new StyleWriter($this, $style, true);
225225
if ($style instanceof Font) {
226+
$styleWriter = new FontStyleWriter($style);
226227
if ($style->getStyleType() == 'title') {
227228
$name = str_replace('Heading_', 'h', $name);
228229
} else {
229230
$name = '.' . $name;
230231
}
231-
$css .= "{$name} " . $styleWriter->write() . PHP_EOL;
232+
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
232233
} elseif ($style instanceof Paragraph) {
234+
$styleWriter = new ParagraphStyleWriter($style);
233235
$name = '.' . $name;
234-
$css .= "{$name} " . $styleWriter->write() . PHP_EOL;
236+
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
235237
}
236238
}
237239
}

src/PhpWord/Writer/HTML/Element/Image.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace PhpOffice\PhpWord\Writer\HTML\Element;
1111

1212
use PhpOffice\PhpWord\Element\Image as ImageElement;
13-
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
13+
use PhpOffice\PhpWord\Writer\HTML\Style\Image as ImageStyleWriter;
1414

1515
/**
1616
* Image element HTML writer
@@ -33,11 +33,9 @@ public function write()
3333
if (!$this->parentWriter->isPdf()) {
3434
$imageData = $this->getBase64ImageData($this->element);
3535
if (!is_null($imageData)) {
36-
$styleWriter = new StyleWriter();
37-
$style = $styleWriter->assembleCss(array(
38-
'width' => $this->element->getStyle()->getWidth() . 'px',
39-
'height' => $this->element->getStyle()->getHeight() . 'px',
40-
));
36+
$styleWriter = new ImageStyleWriter($this->element->getStyle());
37+
$style = $styleWriter->write();
38+
4139
$html = "<img border=\"0\" style=\"{$style}\" src=\"{$imageData}\"/>";
4240
if (!$this->withoutP) {
4341
$html = "<p>{$html}</p>" . PHP_EOL;

src/PhpWord/Writer/HTML/Element/ListItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ListItem extends Element
2424
public function write()
2525
{
2626
$text = htmlspecialchars($this->element->getTextObject()->getText());
27-
$html = '<p>' . $text . '</{$p}>' . PHP_EOL;
27+
$html = '<p>' . $text . '</p>' . PHP_EOL;
2828

2929
return $html;
3030
}

src/PhpWord/Writer/HTML/Element/Text.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
use PhpOffice\PhpWord\Style\Font;
1313
use PhpOffice\PhpWord\Style\Paragraph;
14-
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
14+
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
15+
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
1516

1617
/**
1718
* Text element HTML writer
@@ -32,14 +33,14 @@ public function write()
3233
$pStyle = $this->element->getParagraphStyle();
3334
$pStyleIsObject = ($pStyle instanceof Paragraph);
3435
if ($pStyleIsObject) {
35-
$styleWriter = new StyleWriter($this->parentWriter, $pStyle);
36+
$styleWriter = new ParagraphStyleWriter($pStyle);
3637
$pStyle = $styleWriter->write();
3738
}
3839
// Font style
3940
$fStyle = $this->element->getFontStyle();
4041
$fStyleIsObject = ($fStyle instanceof Font);
4142
if ($fStyleIsObject) {
42-
$styleWriter = new StyleWriter($this->parentWriter, $fStyle);
43+
$styleWriter = new FontStyleWriter($fStyle);
4344
$fStyle = $styleWriter->write();
4445
}
4546

src/PhpWord/Writer/HTML/Element/TextRun.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace PhpOffice\PhpWord\Writer\HTML\Element;
1111

1212
use PhpOffice\PhpWord\Style\Paragraph;
13-
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
13+
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
1414

1515
/**
1616
* TextRun element HTML writer
@@ -33,7 +33,7 @@ public function write()
3333
$pStyle = $this->element->getParagraphStyle();
3434
$pStyleIsObject = ($pStyle instanceof Paragraph);
3535
if ($pStyleIsObject) {
36-
$styleWriter = new StyleWriter($this->parentWriter, $pStyle);
36+
$styleWriter = new ParagraphStyleWriter($pStyle);
3737
$pStyle = $styleWriter->write();
3838
}
3939
$tag = $this->withoutP ? 'span' : 'p';
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\HTML\Style;
11+
12+
/**
13+
* Style writer
14+
*
15+
* @since 0.10.0
16+
*/
17+
abstract class AbstractStyle
18+
{
19+
/**
20+
* Style
21+
*
22+
* @var array|\PhpOffice\PhpWord\Style\AbstractStyle
23+
*/
24+
protected $style;
25+
26+
/**
27+
* Write style
28+
*/
29+
abstract public function write();
30+
31+
/**
32+
* Create new instance
33+
*
34+
* @param array|\PhpOffice\PhpWord\Style\AbstractStyle $style
35+
*/
36+
public function __construct($style = null)
37+
{
38+
$this->style = $style;
39+
}
40+
41+
/**
42+
* Takes array where of CSS properties / values and converts to CSS string
43+
*
44+
* @param array $css
45+
* @return string
46+
*/
47+
protected function assembleCss($css)
48+
{
49+
$pairs = array();
50+
$string = '';
51+
foreach ($css as $key => $value) {
52+
if ($value != '') {
53+
$pairs[] = $key . ': ' . $value;
54+
}
55+
}
56+
if (!empty($pairs)) {
57+
$string = implode('; ', $pairs) . ';';
58+
}
59+
60+
return $string;
61+
}
62+
}

src/PhpWord/Writer/HTML/Style/Font.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
namespace PhpOffice\PhpWord\Writer\HTML\Style;
1111

1212
use PhpOffice\PhpWord\PhpWord;
13+
use PhpOffice\PhpWord\Style\Font as FontStyle;
1314

1415
/**
1516
* Font style HTML writer
1617
*
1718
* @since 0.10.0
1819
*/
19-
class Font extends Style
20+
class Font extends AbstractStyle
2021
{
2122
/**
2223
* Write style
@@ -48,13 +49,13 @@ public function write()
4849
$css['vertical-align'] = 'sub';
4950
}
5051
$css['text-decoration'] = '';
51-
if ($this->style->getUnderline() != \PhpOffice\PhpWord\Style\Font::UNDERLINE_NONE) {
52+
if ($this->style->getUnderline() != FontStyle::UNDERLINE_NONE) {
5253
$css['text-decoration'] .= 'underline ';
5354
}
5455
if ($this->style->getStrikethrough()) {
5556
$css['text-decoration'] .= 'line-through ';
5657
}
5758

58-
return $this->assembleCss($css, $this->curlyBracket);
59+
return $this->assembleCss($css);
5960
}
6061
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\HTML\Style;
11+
12+
/**
13+
* Generic style writer
14+
*
15+
* @since 0.10.0
16+
*/
17+
class Generic extends AbstractStyle
18+
{
19+
/**
20+
* Write style
21+
*
22+
* @return string
23+
*/
24+
public function write()
25+
{
26+
$css = array();
27+
28+
if (is_array($this->style) && !empty($this->style)) {
29+
$css = $this->style;
30+
}
31+
32+
return $this->assembleCss($css);
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\HTML\Style;
11+
12+
/**
13+
* Paragraph style HTML writer
14+
*
15+
* @since 0.10.0
16+
*/
17+
class Image extends AbstractStyle
18+
{
19+
/**
20+
* Write style
21+
*
22+
* @return string
23+
*/
24+
public function write()
25+
{
26+
$css = array();
27+
if ($this->style->getWidth()) {
28+
$css['width'] = $this->style->getWidth() . 'px';
29+
}
30+
if ($this->style->getWidth()) {
31+
$css['height'] = $this->style->getHeight() . 'px';
32+
}
33+
34+
return $this->assembleCss($css);
35+
}
36+
}

0 commit comments

Comments
 (0)