Skip to content

Commit 045cc87

Browse files
committed
Merge branch 'rtf' into develop
2 parents 23047c0 + e46fecf commit 045cc87

File tree

7 files changed

+466
-196
lines changed

7 files changed

+466
-196
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
2828
- ODT Reader: Ability to read standard and custom document properties - @ivanlanin
2929
- Word2007 Writer: Enable the missing custom document properties writer - @ivanlanin
3030
- Image: Enable "image float left" - @ivanlanin GH-244
31+
- RTF Writer: Ability to write document properties - @ivanlanin
3132

3233
### Bugfixes
3334

docs/intro.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Writers
6161
+---------------------------+----------------------+--------+-------+-------+--------+-------+
6262
| Features | | DOCX | ODT | RTF | HTML | PDF |
6363
+===========================+======================+========+=======+=======+========+=======+
64-
| **Document Properties** | Standard ||| | | |
64+
| **Document Properties** | Standard ||| | | |
6565
+---------------------------+----------------------+--------+-------+-------+--------+-------+
6666
| | Custom ||| | | |
6767
+---------------------------+----------------------+--------+-------+-------+--------+-------+

docs/src/documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Below are the supported features for each file formats.
7878

7979
| Features | | DOCX | ODT | RTF | HTML | PDF |
8080
|-------------------------|--------------------|------|-----|-----|------|-----|
81-
| **Document Properties** | Standard ||| | | |
81+
| **Document Properties** | Standard ||| | | |
8282
| | Custom ||| | | |
8383
| **Element Type** | Text ||||||
8484
| | Text Run ||||||

src/PhpWord/Writer/RTF.php

Lines changed: 26 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919

2020
use PhpOffice\PhpWord\Exception\Exception;
2121
use PhpOffice\PhpWord\PhpWord;
22-
use PhpOffice\PhpWord\Settings;
23-
use PhpOffice\PhpWord\Shared\Drawing;
24-
use PhpOffice\PhpWord\Style;
25-
use PhpOffice\PhpWord\Style\Font;
26-
use PhpOffice\PhpWord\Writer\RTF\Element\Container;
2722

2823
/**
2924
* RTF writer
@@ -32,20 +27,6 @@
3227
*/
3328
class RTF extends AbstractWriter implements WriterInterface
3429
{
35-
/**
36-
* Color register
37-
*
38-
* @var array
39-
*/
40-
private $colorTable;
41-
42-
/**
43-
* Font register
44-
*
45-
* @var array
46-
*/
47-
private $fontTable;
48-
4930
/**
5031
* Last paragraph style
5132
*
@@ -60,6 +41,18 @@ class RTF extends AbstractWriter implements WriterInterface
6041
public function __construct(PhpWord $phpWord = null)
6142
{
6243
$this->setPhpWord($phpWord);
44+
45+
$this->parts = array('Header', 'Document');
46+
foreach ($this->parts as $partName) {
47+
$partClass = get_class($this) . '\\Part\\' . $partName;
48+
if (class_exists($partClass)) {
49+
/** @var \PhpOffice\PhpWord\Writer\RTF\Part\AbstractPart $part Type hint */
50+
$part = new $partClass();
51+
$part->setParentWriter($this);
52+
$this->writerParts[strtolower($partName)] = $part;
53+
}
54+
}
55+
6356
}
6457

6558
/**
@@ -70,10 +63,17 @@ public function __construct(PhpWord $phpWord = null)
7063
*/
7164
public function save($filename = null)
7265
{
66+
$content = '';
7367
$filename = $this->getTempFile($filename);
7468
$hFile = fopen($filename, 'w');
7569
if ($hFile !== false) {
76-
fwrite($hFile, $this->writeDocument());
70+
$content .= '{';
71+
$content .= '\rtf1' . PHP_EOL;
72+
$content .= $this->getWriterPart('Header')->write();
73+
$content .= $this->getWriterPart('Document')->write();
74+
$content .= '}';
75+
76+
fwrite($hFile, $content);
7777
fclose($hFile);
7878
} else {
7979
throw new Exception("Can't open file");
@@ -82,19 +82,19 @@ public function save($filename = null)
8282
}
8383

8484
/**
85-
* Get color table
85+
* Get font table
8686
*/
87-
public function getColorTable()
87+
public function getFontTable()
8888
{
89-
return $this->colorTable;
89+
return $this->getWriterPart('Header')->getFontTable();
9090
}
9191

9292
/**
93-
* Get font table
93+
* Get color table
9494
*/
95-
public function getFontTable()
95+
public function getColorTable()
9696
{
97-
return $this->fontTable;
97+
return $this->getWriterPart('Header')->getColorTable();
9898
}
9999

100100
/**
@@ -114,172 +114,4 @@ public function setLastParagraphStyle($value = '')
114114
{
115115
$this->lastParagraphStyle = $value;
116116
}
117-
118-
/**
119-
* Get all data
120-
*
121-
* @return string
122-
*/
123-
private function writeDocument()
124-
{
125-
$this->fontTable = $this->populateFontTable();
126-
$this->colorTable = $this->populateColorTable();
127-
128-
// Set the default character set
129-
$content = '{\rtf1';
130-
$content .= '\ansi\ansicpg1252'; // Set the default font (the first one)
131-
$content .= '\deff0'; // Set the default tab size (720 twips)
132-
$content .= '\deftab720';
133-
$content .= PHP_EOL;
134-
135-
// Set the font tbl group
136-
$content .= '{\fonttbl';
137-
foreach ($this->fontTable as $idx => $font) {
138-
$content .= '{\f' . $idx . '\fnil\fcharset0 ' . $font . ';}';
139-
}
140-
$content .= '}' . PHP_EOL;
141-
142-
// Set the color tbl group
143-
$content .= '{\colortbl ';
144-
foreach ($this->colorTable as $color) {
145-
$arrColor = Drawing::htmlToRGB($color);
146-
$content .= ';\red' . $arrColor[0] . '\green' . $arrColor[1] . '\blue' . $arrColor[2] . '';
147-
}
148-
$content .= ';}' . PHP_EOL;
149-
150-
$content .= '{\*\generator PhpWord;}' . PHP_EOL; // Set the generator
151-
$content .= '\viewkind4'; // Set the view mode of the document
152-
$content .= '\uc1'; // Set the numberof bytes that follows a unicode character
153-
$content .= '\pard'; // Resets to default paragraph properties.
154-
$content .= '\nowidctlpar'; // No widow/orphan control
155-
$content .= '\lang1036'; // Applies a language to a text run (1036 : French (France))
156-
$content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs
157-
$content .= '\fs' . (Settings::getDefaultFontSize() * 2); // Set the font size in half-points
158-
$content .= PHP_EOL;
159-
160-
// Body
161-
$content .= $this->writeContent();
162-
163-
$content .= '}';
164-
165-
return $content;
166-
}
167-
168-
/**
169-
* Get content data
170-
*
171-
* @return string
172-
*/
173-
private function writeContent()
174-
{
175-
$content = '';
176-
177-
$sections = $this->getPhpWord()->getSections();
178-
foreach ($sections as $section) {
179-
$writer = new Container($this, $section);
180-
$content .= $writer->write();
181-
}
182-
183-
return $content;
184-
}
185-
186-
/**
187-
* Get all fonts
188-
*
189-
* @return array
190-
*/
191-
private function populateFontTable()
192-
{
193-
$phpWord = $this->getPhpWord();
194-
$fontTable = array();
195-
$fontTable[] = Settings::getDefaultFontName();
196-
197-
// Browse styles
198-
$styles = Style::getStyles();
199-
if (count($styles) > 0) {
200-
foreach ($styles as $style) {
201-
// Font
202-
if ($style instanceof Font) {
203-
if (in_array($style->getName(), $fontTable) == false) {
204-
$fontTable[] = $style->getName();
205-
}
206-
}
207-
}
208-
}
209-
210-
// Search all fonts used
211-
$sections = $phpWord->getSections();
212-
$countSections = count($sections);
213-
if ($countSections > 0) {
214-
foreach ($sections as $section) {
215-
$elements = $section->getElements();
216-
foreach ($elements as $element) {
217-
if (method_exists($element, 'getFontStyle')) {
218-
$fontStyle = $element->getFontStyle();
219-
if ($fontStyle instanceof Font) {
220-
if (in_array($fontStyle->getName(), $fontTable) == false) {
221-
$fontTable[] = $fontStyle->getName();
222-
}
223-
}
224-
}
225-
}
226-
}
227-
}
228-
229-
return $fontTable;
230-
}
231-
232-
/**
233-
* Get all colors
234-
*
235-
* @return array
236-
*/
237-
private function populateColorTable()
238-
{
239-
$phpWord = $this->getPhpWord();
240-
$defaultFontColor = Settings::DEFAULT_FONT_COLOR;
241-
$colorTable = array();
242-
243-
// Browse styles
244-
$styles = Style::getStyles();
245-
if (count($styles) > 0) {
246-
foreach ($styles as $style) {
247-
// Font
248-
if ($style instanceof Font) {
249-
$color = $style->getColor();
250-
$fgcolor = $style->getFgColor();
251-
if (!in_array($color, $colorTable) && $color != $defaultFontColor && !empty($color)) {
252-
$colorTable[] = $color;
253-
}
254-
if (!in_array($fgcolor, $colorTable) && $fgcolor != $defaultFontColor && !empty($fgcolor)) {
255-
$colorTable[] = $fgcolor;
256-
}
257-
}
258-
}
259-
}
260-
261-
// Search all fonts used
262-
$sections = $phpWord->getSections();
263-
$countSections = count($sections);
264-
if ($countSections > 0) {
265-
foreach ($sections as $section) {
266-
$elements = $section->getElements();
267-
foreach ($elements as $element) {
268-
if (method_exists($element, 'getFontStyle')) {
269-
$fontStyle = $element->getFontStyle();
270-
if ($fontStyle instanceof Font) {
271-
if (in_array($fontStyle->getColor(), $colorTable) == false) {
272-
$colorTable[] = $fontStyle->getColor();
273-
}
274-
if (in_array($fontStyle->getFgColor(), $colorTable) == false) {
275-
$colorTable[] = $fontStyle->getFgColor();
276-
}
277-
}
278-
}
279-
}
280-
}
281-
}
282-
283-
return $colorTable;
284-
}
285117
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Part;
19+
20+
use PhpOffice\PhpWord\Exception\Exception;
21+
use PhpOffice\PhpWord\Writer\AbstractWriter;
22+
23+
/**
24+
* Abstract RTF part writer
25+
*
26+
* @since 0.11.0
27+
*/
28+
abstract class AbstractPart
29+
{
30+
/**
31+
* Parent writer
32+
*
33+
* @var \PhpOffice\PhpWord\Writer\AbstractWriter
34+
*/
35+
private $parentWriter;
36+
37+
/**
38+
* Write part
39+
*
40+
* @return string
41+
*/
42+
abstract public function write();
43+
44+
/**
45+
* Set parent writer
46+
*
47+
* @param \PhpOffice\PhpWord\Writer\AbstractWriter $writer
48+
*/
49+
public function setParentWriter(AbstractWriter $writer = null)
50+
{
51+
$this->parentWriter = $writer;
52+
}
53+
54+
/**
55+
* Get parent writer
56+
*
57+
* @return \PhpOffice\PhpWord\Writer\AbstractWriter
58+
* @throws \PhpOffice\PhpWord\Exception\Exception
59+
*/
60+
public function getParentWriter()
61+
{
62+
if ($this->parentWriter !== null) {
63+
return $this->parentWriter;
64+
} else {
65+
throw new Exception('No parent WriterInterface assigned.');
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)