Skip to content

Commit ba408a8

Browse files
committed
RTF Writer Support ListItem Font Styles
ListItem stores its font style with its text in a TextObject. Use that when writing out its text. @rasamassen, you will want to add this change to one of your PRs, probably PR PHPOffice#2019.
1 parent acb2e37 commit ba408a8

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ PHPWord requires the following:
5454

5555
- PHP 7.1+
5656
- [XML Parser extension](http://www.php.net/manual/en/xml.installation.php)
57-
- [Laminas Escaper component](https://docs.laminas.dev/laminas-escaper/intro/)
5857
- [Zip extension](http://php.net/manual/en/book.zip.php) (optional, used to write OOXML and ODF)
5958
- [GD extension](http://php.net/manual/en/book.image.php) (optional, used to add images)
6059
- [XMLWriter extension](http://php.net/manual/en/book.xmlwriter.php) (optional, used to write OOXML and ODF)
@@ -129,7 +128,7 @@ $fontStyle = new \PhpOffice\PhpWord\Style\Font();
129128
$fontStyle->setBold(true);
130129
$fontStyle->setName('Tahoma');
131130
$fontStyle->setSize(13);
132-
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
131+
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodore Roosevelt)');
133132
$myTextElement->setFontStyle($fontStyle);
134133

135134
// Saving the document as OOXML file...

docs/usage/template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Customer: ${customer_name#3}
166166
Address: ${customer_address#3}
167167
```
168168

169-
It is also possible to pass an array with the values to replace the marcros with.
169+
It is also possible to pass an array with the values to replace the macros with.
170170
If an array with replacements is passed, the ``count`` argument is ignored, it is the size of the array that counts.
171171

172172
``` php

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
namespace PhpOffice\PhpWord\Writer\RTF\Element;
2020

2121
use PhpOffice\PhpWord\Element\ListItem as Li;
22+
use PhpOffice\PhpWord\Style;
2223

2324
/**
2425
* ListItem element RTF writer; extends from text.
@@ -51,7 +52,7 @@ private function writeElement(Li $element)
5152
// Bullet List
5253
$content = '';
5354
$content .= $this->writeOpening();
54-
if ($style instanceof \PhpOffice\PhpWord\Style\ListItem) {
55+
if ($style instanceof Style\ListItem) {
5556
$numStyle = $style->getNumbering();
5657
if ($numStyle->getType() == 'singleLevel') {
5758
$depth = 0;
@@ -64,13 +65,34 @@ private function writeElement(Li $element)
6465
$content .= '\li' . $levels[$depth]->getLeft();
6566
$content .= '\lin' . $levels[$depth]->getLeft();
6667
}
67-
$content .= $this->writeFontStyle(); // Doesn't work. Don't know why. Probably something to do with \PhpOffice\PhpWord\Element\ListItem storing styles in a textObject type \PhpOffice\PhpWord\Element\Text rather than within the Element itself
68+
$content .= $this->writeFontStyle(); // ListItem Text has its own font style applied later.
6869
$content .= PHP_EOL;
6970
/* $content .= '{\listtext\f2 \\\'b7\tab }'; // Not sure if needed for listItemRun
7071
$content .= PHP_EOL; */
7172
$content .= '{';
72-
$content .= $this->writeText($element->getText());
73+
74+
$textStart = $textStyle = $textEnd = '';
75+
$textFontStyle = null;
76+
$textObject = $element->getTextObject();
77+
if ($textObject !== null) {
78+
$textFontStyle = $textObject->getFontStyle();
79+
if (is_string($textFontStyle)) {
80+
$textFontStyle = Style::getStyle($textFontStyle);
81+
}
82+
}
83+
if ($textFontStyle instanceof Style\Font) {
84+
$this->fontStyle = $textFontStyle;
85+
$textStyle = $this->writeFontStyle();
86+
if ($textStyle !== '') {
87+
$textStart = '{';
88+
$textEnd = '}';
89+
}
90+
}
91+
92+
$content .= $textStart . $textStyle . $this->writeText($element->getText()) . $textEnd;
93+
7394
$content .= '}';
95+
7496
$content .= PHP_EOL;
7597
$content .= $this->writeClosing();
7698

src/PhpWord/Writer/RTF/Element/Ruby.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace PhpOffice\PhpWord\Writer\RTF\Element;
2020

21+
use PhpOffice\PhpWord\Element\Ruby as RubyElement;
22+
2123
/**
2224
* Ruby element RTF writer. Writes {baseText} ({rubyText}) in current paragraph style
2325
* because RTF does not natively support ruby text.
@@ -31,13 +33,16 @@ class Ruby extends AbstractElement
3133
*/
3234
public function write()
3335
{
34-
/** @var \PhpOffice\PhpWord\Element\Ruby $element */
3536
$element = $this->element;
36-
$elementClass = str_replace('\\Writer\\RTF', '', static::class);
37-
if (!$element instanceof $elementClass || !is_string($element->getBaseTextRun()->getText())) {
38-
return '';
39-
}
4037

38+
return ($element instanceof RubyElement) ? $this->writeElement($element) : '';
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
private function writeElement(RubyElement $element)
45+
{
4146
$this->getStyles();
4247

4348
$content = '';

0 commit comments

Comments
 (0)