Skip to content

Commit 1ee43da

Browse files
committed
#196: RTF link styling
1 parent 7ae8c3c commit 1ee43da

File tree

11 files changed

+214
-156
lines changed

11 files changed

+214
-156
lines changed

src/PhpWord/Element/ListItem.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,26 @@
2626
class ListItem extends AbstractElement
2727
{
2828
/**
29-
* ListItem Style
29+
* Element style
3030
*
3131
* @var \PhpOffice\PhpWord\Style\ListItem
3232
*/
3333
private $style;
3434

3535
/**
36-
* Textrun
36+
* Text object
3737
*
38-
* @var Text
38+
* @var \PhpOffice\PhpWord\Element\Text
3939
*/
4040
private $textObject;
4141

4242
/**
43-
* ListItem Depth
43+
* Depth
4444
*
4545
* @var int
4646
*/
4747
private $depth;
4848

49-
5049
/**
5150
* Create a new ListItem
5251
*
@@ -70,26 +69,43 @@ public function __construct($text, $depth = 0, $fontStyle = null, $listStyle = n
7069
}
7170

7271
/**
73-
* Get ListItem style
72+
* Get style
73+
*
74+
* @return \PhpOffice\PhpWord\Style\ListItem
7475
*/
7576
public function getStyle()
7677
{
7778
return $this->style;
7879
}
7980

8081
/**
81-
* Get ListItem TextRun
82+
* Get Text object
83+
*
84+
* @return \PhpOffice\PhpWord\Element\Text
8285
*/
8386
public function getTextObject()
8487
{
8588
return $this->textObject;
8689
}
8790

8891
/**
89-
* Get ListItem depth
92+
* Get depth
93+
*
94+
* @return int
9095
*/
9196
public function getDepth()
9297
{
9398
return $this->depth;
9499
}
100+
101+
/**
102+
* Get text
103+
*
104+
* @return string
105+
* @since 0.11.0
106+
*/
107+
public function getText()
108+
{
109+
return $this->textObject->getText();
110+
}
95111
}

src/PhpWord/Writer/RTF.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private function populateFontTable()
226226
$elements = $section->getElements();
227227

228228
foreach ($elements as $element) {
229-
if ($element instanceof Text) {
229+
if (method_exists($element, 'getFontStyle')) {
230230
$fontStyle = $element->getFontStyle();
231231

232232
if ($fontStyle instanceof Font) {
@@ -284,7 +284,7 @@ private function populateColorTable()
284284
$elements = $section->getElements();
285285

286286
foreach ($elements as $element) {
287-
if ($element instanceof Text) {
287+
if (method_exists($element, 'getFontStyle')) {
288288
$fontStyle = $element->getFontStyle();
289289

290290
if ($fontStyle instanceof Font) {

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,143 @@
1717

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

20+
use PhpOffice\PhpWord\Shared\String;
21+
use PhpOffice\PhpWord\Style;
22+
use PhpOffice\PhpWord\Style\Font as FontStyle;
23+
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
24+
use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter;
25+
use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter;
26+
2027
/**
2128
* Abstract RTF element writer
2229
*
2330
* @since 0.11.0
2431
*/
2532
class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractElement
2633
{
34+
/**
35+
* Font style
36+
*
37+
* @var \PhpWord\PhpOffice\Style\Font
38+
*/
39+
private $fontStyle;
40+
41+
/**
42+
* Paragraph style
43+
*
44+
* @var \PhpWord\PhpOffice\Style\Paragraph
45+
*/
46+
private $paragraphStyle;
47+
48+
/**
49+
* Get font and paragraph styles
50+
*/
51+
protected function getStyles()
52+
{
53+
/** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */
54+
$parentWriter = $this->parentWriter;
55+
56+
// Font style
57+
if (method_exists($this->element, 'getFontStyle')) {
58+
$this->fontStyle = $this->element->getFontStyle();
59+
if (is_string($this->fontStyle)) {
60+
$this->fontStyle = Style::getStyle($this->fontStyle);
61+
}
62+
}
63+
64+
// Paragraph style
65+
if (method_exists($this->element, 'getParagraphStyle')) {
66+
$this->paragraphStyle = $this->element->getParagraphStyle();
67+
if (is_string($this->paragraphStyle)) {
68+
$this->paragraphStyle = Style::getStyle($this->paragraphStyle);
69+
}
70+
71+
if ($this->paragraphStyle !== null && !$this->withoutP) {
72+
if ($parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) {
73+
$parentWriter->setLastParagraphStyle($this->element->getParagraphStyle());
74+
} else {
75+
$parentWriter->setLastParagraphStyle();
76+
$this->paragraphStyle = null;
77+
}
78+
} else {
79+
$parentWriter->setLastParagraphStyle();
80+
$this->paragraphStyle = null;
81+
}
82+
}
83+
}
84+
85+
/**
86+
* Write opening
87+
*
88+
* @return string
89+
*/
90+
protected function writeOpening()
91+
{
92+
if ($this->withoutP || !$this->paragraphStyle instanceof ParagraphStyle) {
93+
return;
94+
}
95+
96+
$styleWriter = new ParagraphStyleWriter($this->paragraphStyle);
97+
return $styleWriter->write();
98+
}
99+
100+
/**
101+
* Write text
102+
*
103+
* @param string $text
104+
* @return string
105+
*/
106+
protected function writeText($text)
107+
{
108+
return String::toUnicode($text);
109+
}
110+
111+
/**
112+
* Write closing
113+
*
114+
* @return string
115+
*/
116+
protected function writeClosing()
117+
{
118+
if ($this->withoutP) {
119+
return;
120+
}
121+
122+
return '\par' . PHP_EOL;
123+
}
124+
125+
/**
126+
* Write font style
127+
*
128+
* @return string
129+
*/
130+
protected function writeFontStyle()
131+
{
132+
if (!$this->fontStyle instanceof FontStyle) {
133+
return '';
134+
}
135+
136+
/** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */
137+
$parentWriter = $this->parentWriter;
138+
139+
// Create style writer and set color/name index
140+
$styleWriter = new FontStyleWriter($this->fontStyle);
141+
if ($this->fontStyle->getColor() != null) {
142+
$colorIndex = array_search($this->fontStyle->getColor(), $parentWriter->getColorTable());
143+
if ($colorIndex !== false) {
144+
$styleWriter->setColorIndex($colorIndex + 1);
145+
}
146+
}
147+
if ($this->fontStyle->getName() != null) {
148+
$fontIndex = array_search($this->fontStyle->getName(), $parentWriter->getFontTable());
149+
if ($fontIndex !== false) {
150+
$styleWriter->setNameIndex($fontIndex + 1);
151+
}
152+
}
153+
154+
// Write style
155+
$content = $styleWriter->write();
156+
157+
return $content;
158+
}
27159
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

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

20-
use PhpOffice\PhpWord\Shared\String;
21-
2220
/**
2321
* Link element RTF writer
2422
*
@@ -33,21 +31,19 @@ class Link extends AbstractElement
3331
*/
3432
public function write()
3533
{
36-
$element = $this->element;
37-
if (!$element instanceof \PhpOffice\PhpWord\Element\Link) {
34+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) {
3835
return;
3936
}
4037

38+
$this->getStyles();
39+
4140
$content = '';
42-
if (!$this->withoutP) {
43-
$content .= '{';
44-
}
45-
$content .= '{\field {\*\fldinst {HYPERLINK "' . $element->getTarget() . '"}}{\\fldrslt {';
46-
$content .= String::toUnicode($element->getText());
41+
$content .= $this->writeOpening();
42+
$content .= '{\field {\*\fldinst {HYPERLINK "' . $this->element->getTarget() . '"}}{\\fldrslt {';
43+
$content .= $this->writeFontStyle();
44+
$content .= $this->writeText($this->element->getText());
4745
$content .= '}}}';
48-
if (!$this->withoutP) {
49-
$content .= '}';
50-
}
46+
$content .= $this->writeClosing();
5147

5248
return $content;
5349
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\RTF\Element;
19+
20+
use PhpOffice\PhpWord\Shared\String;
21+
22+
/**
23+
* ListItem element RTF writer; extends from text
24+
*
25+
* @since 0.11.0
26+
*/
27+
class ListItem extends Text
28+
{
29+
}

0 commit comments

Comments
 (0)