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,15 +249,16 @@ 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) {
} elseif($element instanceof PHPWord_Section_TextRun) {
$this->_writeTextRun($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_TextBreak) {
$this->_writeTextBreak($objWriter);
/*
} 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) {
$this->_writeTextBreak($objWriter);
} /* elseif($element instanceof PHPWord_Section_PageBreak) {
} elseif($element instanceof PHPWord_Section_PageBreak) {
$this->_writePageBreak($objWriter);
} elseif($element instanceof PHPWord_Section_Table) {
$this->_writeTable($objWriter, $element);
@ -270,8 +271,8 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
$this->_writeObject($objWriter, $element);
} elseif($element instanceof PHPWord_TOC) {
$this->_writeTOC($objWriter);
}*/
else {
*/
} else {
print_r($element);
echo '<br />';
}
@ -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');

View File

@ -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,16 +310,16 @@ 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_TextBreak) {
$sRTFBody .= $this->_getDataContent_writeTextBreak();
} 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_TextBreak) {
$sRTFBody .= $this->_getDataContent_writeTextBreak();
} /* elseif($element instanceof PHPWord_Section_PageBreak) {
} elseif($element instanceof PHPWord_Section_PageBreak) {
$this->_writePageBreak($objWriter);
} elseif($element instanceof PHPWord_Section_Table) {
$this->_writeTable($objWriter, $element);
@ -330,8 +332,8 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$this->_writeObject($objWriter, $element);
} elseif($element instanceof PHPWord_TOC) {
$this->_writeTOC($objWriter);
}*/
else {
*/
} else {
print_r($element);
echo '<br />';
}
@ -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
}
}
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;
}

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.
- 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 :

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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