Skip to content

Commit 7677e15

Browse files
committed
Basic support for TextRun on ODT and RTF
1 parent 45a9283 commit 7677e15

File tree

7 files changed

+139
-72
lines changed

7 files changed

+139
-72
lines changed

Classes/PHPWord/Writer/ODText/Content.php

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -249,29 +249,30 @@ public function writeContent(PHPWord $pPHPWord = null)
249249
foreach ($_elements as $element) {
250250
if ($element instanceof PHPWord_Section_Text) {
251251
$this->_writeText($objWriter, $element);
252-
} /* elseif($element instanceof PHPWord_Section_TextRun) {
253-
$this->_writeTextRun($objWriter, $element);
254-
} elseif($element instanceof PHPWord_Section_Link) {
255-
$this->_writeLink($objWriter, $element);
256-
} elseif($element instanceof PHPWord_Section_Title) {
257-
$this->_writeTitle($objWriter, $element);
258-
}*/ elseif ($element instanceof PHPWord_Section_TextBreak) {
252+
} elseif($element instanceof PHPWord_Section_TextRun) {
253+
$this->_writeTextRun($objWriter, $element);
254+
} elseif ($element instanceof PHPWord_Section_TextBreak) {
259255
$this->_writeTextBreak($objWriter);
260-
} /* elseif($element instanceof PHPWord_Section_PageBreak) {
261-
$this->_writePageBreak($objWriter);
262-
} elseif($element instanceof PHPWord_Section_Table) {
263-
$this->_writeTable($objWriter, $element);
264-
} elseif($element instanceof PHPWord_Section_ListItem) {
265-
$this->_writeListItem($objWriter, $element);
266-
} elseif($element instanceof PHPWord_Section_Image ||
267-
$element instanceof PHPWord_Section_MemoryImage) {
268-
$this->_writeImage($objWriter, $element);
269-
} elseif($element instanceof PHPWord_Section_Object) {
270-
$this->_writeObject($objWriter, $element);
271-
} elseif($element instanceof PHPWord_TOC) {
272-
$this->_writeTOC($objWriter);
273-
}*/
274-
else {
256+
/*
257+
} elseif($element instanceof PHPWord_Section_Link) {
258+
$this->_writeLink($objWriter, $element);
259+
} elseif($element instanceof PHPWord_Section_Title) {
260+
$this->_writeTitle($objWriter, $element);
261+
} elseif($element instanceof PHPWord_Section_PageBreak) {
262+
$this->_writePageBreak($objWriter);
263+
} elseif($element instanceof PHPWord_Section_Table) {
264+
$this->_writeTable($objWriter, $element);
265+
} elseif($element instanceof PHPWord_Section_ListItem) {
266+
$this->_writeListItem($objWriter, $element);
267+
} elseif($element instanceof PHPWord_Section_Image ||
268+
$element instanceof PHPWord_Section_MemoryImage) {
269+
$this->_writeImage($objWriter, $element);
270+
} elseif($element instanceof PHPWord_Section_Object) {
271+
$this->_writeObject($objWriter, $element);
272+
} elseif($element instanceof PHPWord_TOC) {
273+
$this->_writeTOC($objWriter);
274+
*/
275+
} else {
275276
print_r($element);
276277
echo '<br />';
277278
}
@@ -292,19 +293,32 @@ public function writeContent(PHPWord $pPHPWord = null)
292293
return $objWriter->getData();
293294
}
294295

295-
protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false)
296+
/**
297+
* Write text
298+
*
299+
* @param PHPWord_Shared_XMLWriter $objWriter
300+
* @param PHPWord_Section_Text $text
301+
* @param bool $withoutP
302+
*/
303+
protected function _writeText(
304+
PHPWord_Shared_XMLWriter $objWriter = null,
305+
PHPWord_Section_Text $text,
306+
$withoutP = false)
296307
{
297308
$styleFont = $text->getFontStyle();
298309
$styleParagraph = $text->getParagraphStyle();
299310

300-
$SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
311+
// @todo Commented for TextRun. Should really checkout this value
312+
// $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
313+
$SfIsObject = false;
301314

302315
if ($SfIsObject) {
303316
// Don't never be the case, because I browse all sections for cleaning all styles not declared
304317
die('PHPWord : $SfIsObject wouldn\'t be an object');
305318
} else {
306-
// text:p
307-
$objWriter->startElement('text:p');
319+
if (!$withoutP) {
320+
$objWriter->startElement('text:p'); // text:p
321+
}
308322
if (empty($styleFont)) {
309323
if (empty($styleParagraph)) {
310324
$objWriter->writeAttribute('text:style-name', 'P1');
@@ -324,10 +338,38 @@ protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWor
324338
$objWriter->writeRaw($text->getText());
325339
$objWriter->endElement();
326340
}
327-
$objWriter->endElement();
341+
if (!$withoutP) {
342+
$objWriter->endElement(); // text:p
343+
}
344+
}
345+
}
346+
347+
/**
348+
* Write TextRun section
349+
*
350+
* @param PHPWord_Shared_XMLWriter $objWriter
351+
* @param PHPWord_Section_TextRun $textrun
352+
* @todo Enable all other section types
353+
*/
354+
protected function _writeTextRun(
355+
PHPWord_Shared_XMLWriter $objWriter = null,
356+
PHPWord_Section_TextRun $textrun)
357+
{
358+
$elements = $textrun->getElements();
359+
$objWriter->startElement('text:p');
360+
if (count($elements) > 0) {
361+
foreach ($elements as $element) {
362+
if ($element instanceof PHPWord_Section_Text) {
363+
$this->_writeText($objWriter, $element, true);
364+
}
365+
}
328366
}
367+
$objWriter->endElement();
329368
}
330369

370+
/**
371+
* Write TextBreak
372+
*/
331373
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
332374
{
333375
$objWriter->startElement('text:p');

Classes/PHPWord/Writer/RTF.php

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ private function _getData()
148148
$sRTFContent .= '\deff0';
149149
// Set the default tab size (720 twips)
150150
$sRTFContent .= '\deftab720';
151+
$sRTFContent .= PHP_EOL;
151152
// Set the font tbl group
152153
$sRTFContent .= '{\fonttbl';
153154
foreach ($this->_fontTable as $idx => $font) {
@@ -162,7 +163,7 @@ private function _getData()
162163
}
163164
$sRTFContent .= ';}' . PHP_EOL;
164165
// Set the generator
165-
$sRTFContent .= '{\*\generator PHPWord;}';
166+
$sRTFContent .= '{\*\generator PHPWord;}' . PHP_EOL;
166167
// Set the view mode of the document
167168
$sRTFContent .= '\viewkind4';
168169
// Set the numberof bytes that follows a unicode character
@@ -177,6 +178,7 @@ private function _getData()
177178
$sRTFContent .= '\kerning1';
178179
// Set the font size in half-points
179180
$sRTFContent .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2);
181+
$sRTFContent .= PHP_EOL;
180182
// Body
181183
$sRTFContent .= $this->_getDataContent();
182184

@@ -308,30 +310,30 @@ private function _getDataContent()
308310
foreach ($_elements as $element) {
309311
if ($element instanceof PHPWord_Section_Text) {
310312
$sRTFBody .= $this->_getDataContent_writeText($element);
311-
} /* elseif($element instanceof PHPWord_Section_TextRun) {
312-
$this->_writeTextRun($objWriter, $element);
313-
} elseif($element instanceof PHPWord_Section_Link) {
314-
$this->_writeLink($objWriter, $element);
315-
} elseif($element instanceof PHPWord_Section_Title) {
316-
$this->_writeTitle($objWriter, $element);
317-
}*/
318-
elseif ($element instanceof PHPWord_Section_TextBreak) {
313+
} elseif ($element instanceof PHPWord_Section_TextBreak) {
319314
$sRTFBody .= $this->_getDataContent_writeTextBreak();
320-
} /* elseif($element instanceof PHPWord_Section_PageBreak) {
321-
$this->_writePageBreak($objWriter);
322-
} elseif($element instanceof PHPWord_Section_Table) {
323-
$this->_writeTable($objWriter, $element);
324-
} elseif($element instanceof PHPWord_Section_ListItem) {
325-
$this->_writeListItem($objWriter, $element);
326-
} elseif($element instanceof PHPWord_Section_Image ||
327-
$element instanceof PHPWord_Section_MemoryImage) {
328-
$this->_writeImage($objWriter, $element);
329-
} elseif($element instanceof PHPWord_Section_Object) {
330-
$this->_writeObject($objWriter, $element);
331-
} elseif($element instanceof PHPWord_TOC) {
332-
$this->_writeTOC($objWriter);
333-
}*/
334-
else {
315+
} elseif ($element instanceof PHPWord_Section_TextRun) {
316+
$sRTFBody .= $this->_getDataContent_writeTextRun($element);
317+
/*
318+
} elseif($element instanceof PHPWord_Section_Link) {
319+
$this->_writeLink($objWriter, $element);
320+
} elseif($element instanceof PHPWord_Section_Title) {
321+
$this->_writeTitle($objWriter, $element);
322+
} elseif($element instanceof PHPWord_Section_PageBreak) {
323+
$this->_writePageBreak($objWriter);
324+
} elseif($element instanceof PHPWord_Section_Table) {
325+
$this->_writeTable($objWriter, $element);
326+
} elseif($element instanceof PHPWord_Section_ListItem) {
327+
$this->_writeListItem($objWriter, $element);
328+
} elseif($element instanceof PHPWord_Section_Image ||
329+
$element instanceof PHPWord_Section_MemoryImage) {
330+
$this->_writeImage($objWriter, $element);
331+
} elseif($element instanceof PHPWord_Section_Object) {
332+
$this->_writeObject($objWriter, $element);
333+
} elseif($element instanceof PHPWord_TOC) {
334+
$this->_writeTOC($objWriter);
335+
*/
336+
} else {
335337
print_r($element);
336338
echo '<br />';
337339
}
@@ -341,7 +343,10 @@ private function _getDataContent()
341343
return $sRTFBody;
342344
}
343345

344-
private function _getDataContent_writeText(PHPWord_Section_Text $text)
346+
/**
347+
* Get text
348+
*/
349+
private function _getDataContent_writeText(PHPWord_Section_Text $text, $withoutP = false)
345350
{
346351
$sRTFText = '';
347352

@@ -357,7 +362,7 @@ private function _getDataContent_writeText(PHPWord_Section_Text $text)
357362
$styleParagraph = PHPWord_Style::getStyle($styleParagraph);
358363
}
359364

360-
if ($styleParagraph) {
365+
if ($styleParagraph && !$withoutP) {
361366
if ($this->_lastParagraphStyle != $text->getParagraphStyle()) {
362367
$sRTFText .= '\pard\nowidctlpar';
363368
if ($styleParagraph->getSpaceAfter() != null) {
@@ -423,7 +428,30 @@ private function _getDataContent_writeText(PHPWord_Section_Text $text)
423428
}
424429
}
425430

426-
$sRTFText .= '\par' . PHP_EOL;
431+
if (!$withoutP) {
432+
$sRTFText .= '\par' . PHP_EOL;
433+
}
434+
return $sRTFText;
435+
}
436+
437+
/**
438+
* Get text run content
439+
*/
440+
private function _getDataContent_writeTextRun(PHPWord_Section_TextRun $textrun)
441+
{
442+
$sRTFText = '';
443+
$elements = $textrun->getElements();
444+
if (count($elements) > 0) {
445+
$sRTFText .= '\pard\nowidctlpar' . PHP_EOL;
446+
foreach ($elements as $element) {
447+
if ($element instanceof PHPWord_Section_Text) {
448+
$sRTFText .= '{';
449+
$sRTFText .= $this->_getDataContent_writeText($element, true);
450+
$sRTFText .= '}' . PHP_EOL;
451+
}
452+
}
453+
$sRTFText .= '\par' . PHP_EOL;
454+
}
427455
return $sRTFText;
428456
}
429457

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Changes in branch for release 0.7.1 :
4949
- General: (ivanlanin) GH-93 - Font: Use points instead of halfpoints internally. Conversion to halfpoints done during XML Writing.
5050
- Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion
5151
- Feature: (ivanlanin) - Paragraph: setTabs() function
52+
- Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF
5253
- QA: (Progi1984) - UnitTests
5354

5455
Changes in branch for release 0.7.0 :

samples/Sample_03_Sections.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
4040
$objWriter->save(str_replace('.php', '.docx', __FILE__));
4141

42-
/*echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL;
42+
echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL;
4343
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
4444
$objWriter->save(str_replace('.php', '.odt', __FILE__));
4545

4646
echo date('H:i:s') , ' Write to RTF format' , EOL;
4747
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
48-
$objWriter->save(str_replace('.php', '.rtf', __FILE__));*/
48+
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
4949

5050

5151
// Echo memory peak usage

samples/Sample_04_Textrun.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,13 @@
4747
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
4848
$objWriter->save(str_replace('.php', '.docx', __FILE__));
4949

50-
/* Text Run is not currently supported for ODText
5150
echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL;
5251
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
5352
$objWriter->save(str_replace('.php', '.odt', __FILE__));
54-
*/
5553

56-
/* Text Run is not currently supported for RTF
5754
echo date('H:i:s') , ' Write to RTF format' , EOL;
5855
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
5956
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
60-
*/
6157

6258
// Echo memory peak usage
6359
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;

samples/Sample_05_Multicolumn.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
5151
$objWriter->save(str_replace('.php', '.docx', __FILE__));
5252

53-
// echo date('H:i:s') , " Write to OpenDocumentText format" , EOL;
54-
// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
55-
// $objWriter->save(str_replace('.php', '.odt', __FILE__));
53+
echo date('H:i:s') , " Write to OpenDocumentText format" , EOL;
54+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
55+
$objWriter->save(str_replace('.php', '.odt', __FILE__));
5656

57-
// echo date('H:i:s') , " Write to RTF format" , EOL;
58-
// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
59-
// $objWriter->save(str_replace('.php', '.rtf', __FILE__));
57+
echo date('H:i:s') , " Write to RTF format" , EOL;
58+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
59+
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
6060

6161

6262
// Echo memory peak usage

samples/Sample_08_ParagraphPagination.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@
6060
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
6161
$objWriter->save(str_replace('.php', '.docx', __FILE__));
6262

63-
// echo date('H:i:s') , " Write to OpenDocumentText format" , EOL;
64-
// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
65-
// $objWriter->save(str_replace('.php', '.odt', __FILE__));
63+
echo date('H:i:s') , " Write to OpenDocumentText format" , EOL;
64+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
65+
$objWriter->save(str_replace('.php', '.odt', __FILE__));
6666

67-
// echo date('H:i:s') , " Write to RTF format" , EOL;
68-
// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
69-
// $objWriter->save(str_replace('.php', '.rtf', __FILE__));
67+
echo date('H:i:s') , " Write to RTF format" , EOL;
68+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
69+
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
7070

7171

7272
// Echo memory peak usage

0 commit comments

Comments
 (0)