diff --git a/Classes/PHPWord/Writer/ODText/Content.php b/Classes/PHPWord/Writer/ODText/Content.php index fad07d5a..f79eb4b4 100755 --- a/Classes/PHPWord/Writer/ODText/Content.php +++ b/Classes/PHPWord/Writer/ODText/Content.php @@ -249,29 +249,30 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart foreach ($_elements as $element) { if ($element instanceof PHPWord_Section_Text) { $this->_writeText($objWriter, $element); - } /* elseif($element instanceof PHPWord_Section_TextRun) { - $this->_writeTextRun($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Link) { - $this->_writeLink($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Title) { - $this->_writeTitle($objWriter, $element); - }*/ elseif ($element instanceof PHPWord_Section_TextBreak) { + } elseif($element instanceof PHPWord_Section_TextRun) { + $this->_writeTextRun($objWriter, $element); + } elseif ($element instanceof PHPWord_Section_TextBreak) { $this->_writeTextBreak($objWriter); - } /* elseif($element instanceof PHPWord_Section_PageBreak) { - $this->_writePageBreak($objWriter); - } elseif($element instanceof PHPWord_Section_Table) { - $this->_writeTable($objWriter, $element); - } elseif($element instanceof PHPWord_Section_ListItem) { - $this->_writeListItem($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Image || - $element instanceof PHPWord_Section_MemoryImage) { - $this->_writeImage($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Object) { - $this->_writeObject($objWriter, $element); - } elseif($element instanceof PHPWord_TOC) { - $this->_writeTOC($objWriter); - }*/ - else { + /* + } elseif($element instanceof PHPWord_Section_Link) { + $this->_writeLink($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Title) { + $this->_writeTitle($objWriter, $element); + } elseif($element instanceof PHPWord_Section_PageBreak) { + $this->_writePageBreak($objWriter); + } elseif($element instanceof PHPWord_Section_Table) { + $this->_writeTable($objWriter, $element); + } elseif($element instanceof PHPWord_Section_ListItem) { + $this->_writeListItem($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Image || + $element instanceof PHPWord_Section_MemoryImage) { + $this->_writeImage($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Object) { + $this->_writeObject($objWriter, $element); + } elseif($element instanceof PHPWord_TOC) { + $this->_writeTOC($objWriter); + */ + } else { print_r($element); echo '
'; } @@ -292,19 +293,32 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart return $objWriter->getData(); } - protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false) + /** + * Write text + * + * @param PHPWord_Shared_XMLWriter $objWriter + * @param PHPWord_Section_Text $text + * @param bool $withoutP + */ + protected function _writeText( + PHPWord_Shared_XMLWriter $objWriter = null, + PHPWord_Section_Text $text, + $withoutP = false) { $styleFont = $text->getFontStyle(); $styleParagraph = $text->getParagraphStyle(); - $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; + // @todo Commented for TextRun. Should really checkout this value + // $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; + $SfIsObject = false; if ($SfIsObject) { // Don't never be the case, because I browse all sections for cleaning all styles not declared die('PHPWord : $SfIsObject wouldn\'t be an object'); } else { - // text:p - $objWriter->startElement('text:p'); + if (!$withoutP) { + $objWriter->startElement('text:p'); // text:p + } if (empty($styleFont)) { if (empty($styleParagraph)) { $objWriter->writeAttribute('text:style-name', 'P1'); @@ -324,10 +338,38 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart $objWriter->writeRaw($text->getText()); $objWriter->endElement(); } - $objWriter->endElement(); + if (!$withoutP) { + $objWriter->endElement(); // text:p + } } } + /** + * Write TextRun section + * + * @param PHPWord_Shared_XMLWriter $objWriter + * @param PHPWord_Section_TextRun $textrun + * @todo Enable all other section types + */ + protected function _writeTextRun( + PHPWord_Shared_XMLWriter $objWriter = null, + PHPWord_Section_TextRun $textrun) + { + $elements = $textrun->getElements(); + $objWriter->startElement('text:p'); + if (count($elements) > 0) { + foreach ($elements as $element) { + if ($element instanceof PHPWord_Section_Text) { + $this->_writeText($objWriter, $element, true); + } + } + } + $objWriter->endElement(); + } + + /** + * Write TextBreak + */ protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null) { $objWriter->startElement('text:p'); diff --git a/Classes/PHPWord/Writer/RTF.php b/Classes/PHPWord/Writer/RTF.php index 14f56318..e22fe99c 100755 --- a/Classes/PHPWord/Writer/RTF.php +++ b/Classes/PHPWord/Writer/RTF.php @@ -148,6 +148,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter $sRTFContent .= '\deff0'; // Set the default tab size (720 twips) $sRTFContent .= '\deftab720'; + $sRTFContent .= PHP_EOL; // Set the font tbl group $sRTFContent .= '{\fonttbl'; foreach ($this->_fontTable as $idx => $font) { @@ -162,7 +163,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter } $sRTFContent .= ';}' . PHP_EOL; // Set the generator - $sRTFContent .= '{\*\generator PHPWord;}'; + $sRTFContent .= '{\*\generator PHPWord;}' . PHP_EOL; // Set the view mode of the document $sRTFContent .= '\viewkind4'; // Set the numberof bytes that follows a unicode character @@ -177,6 +178,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter $sRTFContent .= '\kerning1'; // Set the font size in half-points $sRTFContent .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2); + $sRTFContent .= PHP_EOL; // Body $sRTFContent .= $this->_getDataContent(); @@ -308,30 +310,30 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter foreach ($_elements as $element) { if ($element instanceof PHPWord_Section_Text) { $sRTFBody .= $this->_getDataContent_writeText($element); - } /* elseif($element instanceof PHPWord_Section_TextRun) { - $this->_writeTextRun($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Link) { - $this->_writeLink($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Title) { - $this->_writeTitle($objWriter, $element); - }*/ - elseif ($element instanceof PHPWord_Section_TextBreak) { + } elseif ($element instanceof PHPWord_Section_TextBreak) { $sRTFBody .= $this->_getDataContent_writeTextBreak(); - } /* elseif($element instanceof PHPWord_Section_PageBreak) { - $this->_writePageBreak($objWriter); - } elseif($element instanceof PHPWord_Section_Table) { - $this->_writeTable($objWriter, $element); - } elseif($element instanceof PHPWord_Section_ListItem) { - $this->_writeListItem($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Image || - $element instanceof PHPWord_Section_MemoryImage) { - $this->_writeImage($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Object) { - $this->_writeObject($objWriter, $element); - } elseif($element instanceof PHPWord_TOC) { - $this->_writeTOC($objWriter); - }*/ - else { + } elseif ($element instanceof PHPWord_Section_TextRun) { + $sRTFBody .= $this->_getDataContent_writeTextRun($element); + /* + } elseif($element instanceof PHPWord_Section_Link) { + $this->_writeLink($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Title) { + $this->_writeTitle($objWriter, $element); + } elseif($element instanceof PHPWord_Section_PageBreak) { + $this->_writePageBreak($objWriter); + } elseif($element instanceof PHPWord_Section_Table) { + $this->_writeTable($objWriter, $element); + } elseif($element instanceof PHPWord_Section_ListItem) { + $this->_writeListItem($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Image || + $element instanceof PHPWord_Section_MemoryImage) { + $this->_writeImage($objWriter, $element); + } elseif($element instanceof PHPWord_Section_Object) { + $this->_writeObject($objWriter, $element); + } elseif($element instanceof PHPWord_TOC) { + $this->_writeTOC($objWriter); + */ + } else { print_r($element); echo '
'; } @@ -341,7 +343,10 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter return $sRTFBody; } - private function _getDataContent_writeText(PHPWord_Section_Text $text) + /** + * Get text + */ + private function _getDataContent_writeText(PHPWord_Section_Text $text, $withoutP = false) { $sRTFText = ''; @@ -357,7 +362,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter $styleParagraph = PHPWord_Style::getStyle($styleParagraph); } - if ($styleParagraph) { + if ($styleParagraph && !$withoutP) { if ($this->_lastParagraphStyle != $text->getParagraphStyle()) { $sRTFText .= '\pard\nowidctlpar'; if ($styleParagraph->getSpaceAfter() != null) { @@ -423,7 +428,30 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter } } - $sRTFText .= '\par' . PHP_EOL; + if (!$withoutP) { + $sRTFText .= '\par' . PHP_EOL; + } + return $sRTFText; + } + + /** + * Get text run content + */ + private function _getDataContent_writeTextRun(PHPWord_Section_TextRun $textrun) + { + $sRTFText = ''; + $elements = $textrun->getElements(); + if (count($elements) > 0) { + $sRTFText .= '\pard\nowidctlpar' . PHP_EOL; + foreach ($elements as $element) { + if ($element instanceof PHPWord_Section_Text) { + $sRTFText .= '{'; + $sRTFText .= $this->_getDataContent_writeText($element, true); + $sRTFText .= '}' . PHP_EOL; + } + } + $sRTFText .= '\par' . PHP_EOL; + } return $sRTFText; } diff --git a/changelog.txt b/changelog.txt index 9b81b967..2230f687 100755 --- a/changelog.txt +++ b/changelog.txt @@ -49,6 +49,7 @@ Changes in branch for release 0.7.1 : - General: (ivanlanin) GH-93 - Font: Use points instead of halfpoints internally. Conversion to halfpoints done during XML Writing. - Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion - Feature: (ivanlanin) - Paragraph: setTabs() function +- Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF - QA: (Progi1984) - UnitTests Changes in branch for release 0.7.0 : diff --git a/samples/Sample_03_Sections.php b/samples/Sample_03_Sections.php index d2231dfa..afca97da 100755 --- a/samples/Sample_03_Sections.php +++ b/samples/Sample_03_Sections.php @@ -39,13 +39,13 @@ echo date('H:i:s') , ' Write to Word2007 format' , EOL; $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save(str_replace('.php', '.docx', __FILE__)); -/*echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; +echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); $objWriter->save(str_replace('.php', '.odt', __FILE__)); echo date('H:i:s') , ' Write to RTF format' , EOL; $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); -$objWriter->save(str_replace('.php', '.rtf', __FILE__));*/ +$objWriter->save(str_replace('.php', '.rtf', __FILE__)); // Echo memory peak usage diff --git a/samples/Sample_04_Textrun.php b/samples/Sample_04_Textrun.php index 164afc3c..ac0906c9 100644 --- a/samples/Sample_04_Textrun.php +++ b/samples/Sample_04_Textrun.php @@ -47,17 +47,13 @@ echo date('H:i:s') , ' Write to Word2007 format' , EOL; $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save(str_replace('.php', '.docx', __FILE__)); -/* Text Run is not currently supported for ODText echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); $objWriter->save(str_replace('.php', '.odt', __FILE__)); -*/ -/* Text Run is not currently supported for RTF echo date('H:i:s') , ' Write to RTF format' , EOL; $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); $objWriter->save(str_replace('.php', '.rtf', __FILE__)); -*/ // Echo memory peak usage echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL; diff --git a/samples/Sample_05_Multicolumn.php b/samples/Sample_05_Multicolumn.php index a7889a85..7f0a75d0 100644 --- a/samples/Sample_05_Multicolumn.php +++ b/samples/Sample_05_Multicolumn.php @@ -50,13 +50,13 @@ echo date('H:i:s') , " Write to Word2007 format" , EOL; $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save(str_replace('.php', '.docx', __FILE__)); -// echo date('H:i:s') , " Write to OpenDocumentText format" , EOL; -// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); -// $objWriter->save(str_replace('.php', '.odt', __FILE__)); +echo date('H:i:s') , " Write to OpenDocumentText format" , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); +$objWriter->save(str_replace('.php', '.odt', __FILE__)); -// echo date('H:i:s') , " Write to RTF format" , EOL; -// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); -// $objWriter->save(str_replace('.php', '.rtf', __FILE__)); +echo date('H:i:s') , " Write to RTF format" , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); +$objWriter->save(str_replace('.php', '.rtf', __FILE__)); // Echo memory peak usage diff --git a/samples/Sample_08_ParagraphPagination.php b/samples/Sample_08_ParagraphPagination.php index 97b43032..7bbead7a 100644 --- a/samples/Sample_08_ParagraphPagination.php +++ b/samples/Sample_08_ParagraphPagination.php @@ -60,13 +60,13 @@ echo date('H:i:s') , " Write to Word2007 format" , EOL; $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save(str_replace('.php', '.docx', __FILE__)); -// echo date('H:i:s') , " Write to OpenDocumentText format" , EOL; -// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); -// $objWriter->save(str_replace('.php', '.odt', __FILE__)); +echo date('H:i:s') , " Write to OpenDocumentText format" , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); +$objWriter->save(str_replace('.php', '.odt', __FILE__)); -// echo date('H:i:s') , " Write to RTF format" , EOL; -// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); -// $objWriter->save(str_replace('.php', '.rtf', __FILE__)); +echo date('H:i:s') , " Write to RTF format" , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); +$objWriter->save(str_replace('.php', '.rtf', __FILE__)); // Echo memory peak usage