Skip to content

Commit c775a2e

Browse files
committed
Rasamassen PR's 2821 and 2823
I made RTF usable some time ago. @rasamassen has recently done a lot of useful work to cover many missing areas. Since who knows when those changes will be merged, I intend to incorporate much of that work. I will do this with several pushes, each based on one or more of those changes. This one is based on PR PHPOffice#2821 (fixes issue PHPOffice#1106), and PR PHPOffice#2823.
1 parent fffe56d commit c775a2e

File tree

8 files changed

+458
-7
lines changed

8 files changed

+458
-7
lines changed

src/PhpWord/Escaper/Rtf.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ protected function escapeAsciiCharacter($code)
3131
if ($code == 9) {
3232
return '{\\tab}';
3333
}
34+
if ($code === 10) {
35+
return ''; // or maybe '\par'
36+
}
3437
if (0x20 > $code || $code >= 0x80) {
3538
return '{\\u' . $code . '}';
3639
}

src/PhpWord/Style/ListItem.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ public function getNumStyle()
115115
return $this->numStyle;
116116
}
117117

118+
/**
119+
* Get numbering style.
120+
*
121+
* @return ?Numbering
122+
*/
123+
public function getNumbering()
124+
{
125+
$numStyleObject = Style::getStyle($this->numStyle);
126+
if ($numStyleObject instanceof Numbering) {
127+
return $numStyleObject;
128+
}
129+
130+
return null;
131+
}
132+
118133
/**
119134
* Set numbering style name.
120135
*

src/PhpWord/Writer/RTF.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ public function getColorTable()
101101
return $this->getWriterPart('Header')->getColorTable();
102102
}
103103

104+
/**
105+
* Get list table.
106+
*
107+
* @return array
108+
*/
109+
public function getListTable()
110+
{
111+
return $this->getWriterPart('Header')->getListTable();
112+
}
113+
104114
/**
105115
* Get last paragraph style.
106116
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function write()
4646
return '';
4747
}
4848
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
49-
$withoutP = in_array($containerClass, ['TextRun', 'Footnote', 'Endnote']) ? true : false;
49+
$withoutP = in_array($containerClass, ['TextRun', 'Footnote', 'Endnote', 'ListItemRun', 'Field']) ? true : false;
5050
$content = '';
5151

5252
$elements = $container->getElements();

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,49 @@
2525
*/
2626
class ListItem extends Text
2727
{
28+
/**
29+
* Write list item element.
30+
*/
31+
public function write()
32+
{
33+
/** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
34+
$element = $this->element;
35+
if (!$element instanceof \PhpOffice\PhpWord\Element\ListItem) {
36+
return '';
37+
}
38+
39+
$this->getStyles();
40+
41+
$depth = (int) $element->getDepth();
42+
$style = $element->getStyle();
43+
$text = $element->getTextObject();
44+
45+
// Bullet List
46+
$content = '';
47+
$content .= $this->writeOpening();
48+
if ($style instanceof \PhpOffice\PhpWord\Style\ListItem) {
49+
$numStyle = $style->getNumbering();
50+
if ($numStyle->getType() == 'singleLevel') {
51+
$depth = 0;
52+
}
53+
$levels = $numStyle->getLevels();
54+
$content .= '\ilvl' . $element->getDepth();
55+
$content .= '\ls' . $style->getNumId();
56+
$content .= '\tx' . $levels[$depth]->getTabPos();
57+
$content .= '\fi' . $levels[$depth]->getHanging() * -1;
58+
$content .= '\li' . $levels[$depth]->getLeft();
59+
$content .= '\lin' . $levels[$depth]->getLeft();
60+
}
61+
$content .= $this->writeFontStyle(); // Doesn't work. Don't know why. Probalby something to do with \PphOffice\PhpWord\Element\ListItem storing styles in a textObject type \PphOffice\PhpWord\Element\Text rather than within the Element itself
62+
$content .= PHP_EOL;
63+
/* $content .= '{\listtext\f2 \\\'b7\tab }'; // Not sure if needed for listItemRun
64+
$content .= PHP_EOL; */
65+
$content .= '{';
66+
$content .= $this->writeText($element->getText());
67+
$content .= '}';
68+
$content .= PHP_EOL;
69+
$content .= $this->writeClosing();
70+
71+
return $content;
72+
}
2873
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWord\Writer\RTF\Element;
20+
21+
/**
22+
* ListItem element HTML writer.
23+
*
24+
* @since 0.10.0
25+
*/
26+
class ListItemRun extends TextRun
27+
{
28+
/**
29+
* Write list item.
30+
*
31+
* @return string
32+
*/
33+
public function write()
34+
{
35+
$element = $this->element;
36+
if (!$element instanceof \PhpOffice\PhpWord\Element\ListItemRun) {
37+
return '';
38+
}
39+
40+
$writer = new Container($this->parentWriter, $element);
41+
$this->getStyles();
42+
43+
$depth = (int) $element->getDepth();
44+
$style = $element->getStyle();
45+
46+
$content = '';
47+
$content .= $this->writeOpening();
48+
if ($style instanceof \PhpOffice\PhpWord\Style\ListItem) {
49+
$numStyle = $style->getNumbering();
50+
$levels = $numStyle->getLevels();
51+
$content .= '\ilvl' . $element->getDepth();
52+
$content .= '\ls' . $style->getNumId();
53+
$content .= '\tx' . $levels[$depth]->getTabPos();
54+
$hanging = $levels[$depth]->getLeft() + $levels[$depth]->getHanging();
55+
$left = 0 - $levels[$depth]->getHanging();
56+
$content .= '\fi' . $left;
57+
$content .= '\li' . $hanging;
58+
$content .= '\lin' . $hanging;
59+
}
60+
$content .= '{';
61+
$content .= $writer->write();
62+
$content .= '}';
63+
$content .= $this->writeClosing();
64+
65+
return $content;
66+
}
67+
}

0 commit comments

Comments
 (0)