Merge pull request #101 from ivanlanin/develop

Basic support for TextRun on ODT and RTF
This commit is contained in:
Progi1984 2014-03-10 13:28:31 +01:00
commit fb1d7d4db6
7 changed files with 139 additions and 72 deletions

View File

@ -249,29 +249,30 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
foreach ($_elements as $element) { foreach ($_elements as $element) {
if ($element instanceof PHPWord_Section_Text) { if ($element instanceof PHPWord_Section_Text) {
$this->_writeText($objWriter, $element); $this->_writeText($objWriter, $element);
} /* elseif($element instanceof PHPWord_Section_TextRun) { } elseif($element instanceof PHPWord_Section_TextRun) {
$this->_writeTextRun($objWriter, $element); $this->_writeTextRun($objWriter, $element);
} elseif($element instanceof PHPWord_Section_Link) { } elseif ($element instanceof PHPWord_Section_TextBreak) {
$this->_writeLink($objWriter, $element);
} elseif($element instanceof PHPWord_Section_Title) {
$this->_writeTitle($objWriter, $element);
}*/ elseif ($element instanceof PHPWord_Section_TextBreak) {
$this->_writeTextBreak($objWriter); $this->_writeTextBreak($objWriter);
} /* elseif($element instanceof PHPWord_Section_PageBreak) { /*
$this->_writePageBreak($objWriter); } elseif($element instanceof PHPWord_Section_Link) {
} elseif($element instanceof PHPWord_Section_Table) { $this->_writeLink($objWriter, $element);
$this->_writeTable($objWriter, $element); } elseif($element instanceof PHPWord_Section_Title) {
} elseif($element instanceof PHPWord_Section_ListItem) { $this->_writeTitle($objWriter, $element);
$this->_writeListItem($objWriter, $element); } elseif($element instanceof PHPWord_Section_PageBreak) {
} elseif($element instanceof PHPWord_Section_Image || $this->_writePageBreak($objWriter);
$element instanceof PHPWord_Section_MemoryImage) { } elseif($element instanceof PHPWord_Section_Table) {
$this->_writeImage($objWriter, $element); $this->_writeTable($objWriter, $element);
} elseif($element instanceof PHPWord_Section_Object) { } elseif($element instanceof PHPWord_Section_ListItem) {
$this->_writeObject($objWriter, $element); $this->_writeListItem($objWriter, $element);
} elseif($element instanceof PHPWord_TOC) { } elseif($element instanceof PHPWord_Section_Image ||
$this->_writeTOC($objWriter); $element instanceof PHPWord_Section_MemoryImage) {
}*/ $this->_writeImage($objWriter, $element);
else { } elseif($element instanceof PHPWord_Section_Object) {
$this->_writeObject($objWriter, $element);
} elseif($element instanceof PHPWord_TOC) {
$this->_writeTOC($objWriter);
*/
} else {
print_r($element); print_r($element);
echo '<br />'; echo '<br />';
} }
@ -292,19 +293,32 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
return $objWriter->getData(); 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(); $styleFont = $text->getFontStyle();
$styleParagraph = $text->getParagraphStyle(); $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) { if ($SfIsObject) {
// Don't never be the case, because I browse all sections for cleaning all styles not declared // 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'); die('PHPWord : $SfIsObject wouldn\'t be an object');
} else { } else {
// text:p if (!$withoutP) {
$objWriter->startElement('text:p'); $objWriter->startElement('text:p'); // text:p
}
if (empty($styleFont)) { if (empty($styleFont)) {
if (empty($styleParagraph)) { if (empty($styleParagraph)) {
$objWriter->writeAttribute('text:style-name', 'P1'); $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->writeRaw($text->getText());
$objWriter->endElement(); $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) protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
{ {
$objWriter->startElement('text:p'); $objWriter->startElement('text:p');

View File

@ -148,6 +148,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$sRTFContent .= '\deff0'; $sRTFContent .= '\deff0';
// Set the default tab size (720 twips) // Set the default tab size (720 twips)
$sRTFContent .= '\deftab720'; $sRTFContent .= '\deftab720';
$sRTFContent .= PHP_EOL;
// Set the font tbl group // Set the font tbl group
$sRTFContent .= '{\fonttbl'; $sRTFContent .= '{\fonttbl';
foreach ($this->_fontTable as $idx => $font) { foreach ($this->_fontTable as $idx => $font) {
@ -162,7 +163,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
} }
$sRTFContent .= ';}' . PHP_EOL; $sRTFContent .= ';}' . PHP_EOL;
// Set the generator // Set the generator
$sRTFContent .= '{\*\generator PHPWord;}'; $sRTFContent .= '{\*\generator PHPWord;}' . PHP_EOL;
// Set the view mode of the document // Set the view mode of the document
$sRTFContent .= '\viewkind4'; $sRTFContent .= '\viewkind4';
// Set the numberof bytes that follows a unicode character // Set the numberof bytes that follows a unicode character
@ -177,6 +178,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$sRTFContent .= '\kerning1'; $sRTFContent .= '\kerning1';
// Set the font size in half-points // Set the font size in half-points
$sRTFContent .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2); $sRTFContent .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2);
$sRTFContent .= PHP_EOL;
// Body // Body
$sRTFContent .= $this->_getDataContent(); $sRTFContent .= $this->_getDataContent();
@ -308,30 +310,30 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
foreach ($_elements as $element) { foreach ($_elements as $element) {
if ($element instanceof PHPWord_Section_Text) { if ($element instanceof PHPWord_Section_Text) {
$sRTFBody .= $this->_getDataContent_writeText($element); $sRTFBody .= $this->_getDataContent_writeText($element);
} /* elseif($element instanceof PHPWord_Section_TextRun) { } elseif ($element instanceof PHPWord_Section_TextBreak) {
$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) {
$sRTFBody .= $this->_getDataContent_writeTextBreak(); $sRTFBody .= $this->_getDataContent_writeTextBreak();
} /* elseif($element instanceof PHPWord_Section_PageBreak) { } elseif ($element instanceof PHPWord_Section_TextRun) {
$this->_writePageBreak($objWriter); $sRTFBody .= $this->_getDataContent_writeTextRun($element);
} elseif($element instanceof PHPWord_Section_Table) { /*
$this->_writeTable($objWriter, $element); } elseif($element instanceof PHPWord_Section_Link) {
} elseif($element instanceof PHPWord_Section_ListItem) { $this->_writeLink($objWriter, $element);
$this->_writeListItem($objWriter, $element); } elseif($element instanceof PHPWord_Section_Title) {
} elseif($element instanceof PHPWord_Section_Image || $this->_writeTitle($objWriter, $element);
$element instanceof PHPWord_Section_MemoryImage) { } elseif($element instanceof PHPWord_Section_PageBreak) {
$this->_writeImage($objWriter, $element); $this->_writePageBreak($objWriter);
} elseif($element instanceof PHPWord_Section_Object) { } elseif($element instanceof PHPWord_Section_Table) {
$this->_writeObject($objWriter, $element); $this->_writeTable($objWriter, $element);
} elseif($element instanceof PHPWord_TOC) { } elseif($element instanceof PHPWord_Section_ListItem) {
$this->_writeTOC($objWriter); $this->_writeListItem($objWriter, $element);
}*/ } elseif($element instanceof PHPWord_Section_Image ||
else { $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); print_r($element);
echo '<br />'; echo '<br />';
} }
@ -341,7 +343,10 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
return $sRTFBody; return $sRTFBody;
} }
private function _getDataContent_writeText(PHPWord_Section_Text $text) /**
* Get text
*/
private function _getDataContent_writeText(PHPWord_Section_Text $text, $withoutP = false)
{ {
$sRTFText = ''; $sRTFText = '';
@ -357,7 +362,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$styleParagraph = PHPWord_Style::getStyle($styleParagraph); $styleParagraph = PHPWord_Style::getStyle($styleParagraph);
} }
if ($styleParagraph) { if ($styleParagraph && !$withoutP) {
if ($this->_lastParagraphStyle != $text->getParagraphStyle()) { if ($this->_lastParagraphStyle != $text->getParagraphStyle()) {
$sRTFText .= '\pard\nowidctlpar'; $sRTFText .= '\pard\nowidctlpar';
if ($styleParagraph->getSpaceAfter() != null) { 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; return $sRTFText;
} }

View File

@ -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. - 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 - Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion
- Feature: (ivanlanin) - Paragraph: setTabs() function - Feature: (ivanlanin) - Paragraph: setTabs() function
- Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF
- QA: (Progi1984) - UnitTests - QA: (Progi1984) - UnitTests
Changes in branch for release 0.7.0 : Changes in branch for release 0.7.0 :

View File

@ -39,13 +39,13 @@ echo date('H:i:s') , ' Write to Word2007 format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); $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 = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
$objWriter->save(str_replace('.php', '.odt', __FILE__)); $objWriter->save(str_replace('.php', '.odt', __FILE__));
echo date('H:i:s') , ' Write to RTF format' , EOL; echo date('H:i:s') , ' Write to RTF format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save(str_replace('.php', '.rtf', __FILE__));*/ $objWriter->save(str_replace('.php', '.rtf', __FILE__));
// Echo memory peak usage // Echo memory peak usage

View File

@ -47,17 +47,13 @@ echo date('H:i:s') , ' Write to Word2007 format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); $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; echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
$objWriter->save(str_replace('.php', '.odt', __FILE__)); $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; echo date('H:i:s') , ' Write to RTF format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save(str_replace('.php', '.rtf', __FILE__)); $objWriter->save(str_replace('.php', '.rtf', __FILE__));
*/
// Echo memory peak usage // Echo memory peak usage
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL; echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;

View File

@ -50,13 +50,13 @@ echo date('H:i:s') , " Write to Word2007 format" , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); $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 = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
// $objWriter->save(str_replace('.php', '.odt', __FILE__)); $objWriter->save(str_replace('.php', '.odt', __FILE__));
// echo date('H:i:s') , " Write to RTF format" , EOL; echo date('H:i:s') , " Write to RTF format" , EOL;
// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
// $objWriter->save(str_replace('.php', '.rtf', __FILE__)); $objWriter->save(str_replace('.php', '.rtf', __FILE__));
// Echo memory peak usage // Echo memory peak usage

View File

@ -60,13 +60,13 @@ echo date('H:i:s') , " Write to Word2007 format" , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); $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 = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
// $objWriter->save(str_replace('.php', '.odt', __FILE__)); $objWriter->save(str_replace('.php', '.odt', __FILE__));
// echo date('H:i:s') , " Write to RTF format" , EOL; echo date('H:i:s') , " Write to RTF format" , EOL;
// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
// $objWriter->save(str_replace('.php', '.rtf', __FILE__)); $objWriter->save(str_replace('.php', '.rtf', __FILE__));
// Echo memory peak usage // Echo memory peak usage