From 38a15e5da5865d52f807d4f3dab0e6ebccb60725 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Sun, 15 Dec 2013 14:35:18 +0100 Subject: [PATCH] Word2007 : Support for tab stops --- .gitignore | 3 +- Classes/PHPWord/Style/Paragraph.php | 33 +++-- Classes/PHPWord/Style/Tab.php | 147 +++++++++++++++++++++++ Classes/PHPWord/Style/Tabs.php | 67 +++++++++++ Classes/PHPWord/Writer/Word2007/Base.php | 46 +++---- changelog.txt | 1 + composer.json | 2 +- samples/Sample_02_TabStops.php | 64 ++++++++++ samples/old/Image.php | 2 +- samples/old/{_earth.JPG => _earth.jpg} | Bin 10 files changed, 332 insertions(+), 33 deletions(-) create mode 100644 Classes/PHPWord/Style/Tab.php create mode 100644 Classes/PHPWord/Style/Tabs.php create mode 100644 samples/Sample_02_TabStops.php rename samples/old/{_earth.JPG => _earth.jpg} (100%) diff --git a/.gitignore b/.gitignore index 4abb8c94..edbe05c7 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ *.odt *.docx *.rtf -*.txt \ No newline at end of file +*.txt +*.xml diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index 9c841c7c..937d8650 100644 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -62,8 +62,14 @@ class PHPWord_Style_Paragraph { * @var int */ private $_spacing; - - + + /** + * Set of Custom Tab Stops + * + * @var array + */ + private $_tabs; + /** * Indent by how much * @@ -80,6 +86,7 @@ class PHPWord_Style_Paragraph { $this->_spaceBefore = null; $this->_spaceAfter = null; $this->_spacing = null; + $this->_tabs = null; $this->_indent = null; } @@ -90,13 +97,16 @@ class PHPWord_Style_Paragraph { * @param mixed $value */ public function setStyleValue($key, $value) { - if($key == '_spacing') { - $value += 240; // because line height of 1 matches 240 twips - } if($key == '_indent') { - $value = (int)$value * 720; // 720 twips per indent + $value = (int)$value * 720; // 720 twips per indent } - $this->$key = $value; + if($key == '_spacing') { + $value += 240; // because line height of 1 matches 240 twips + } + if($key === '_tabs') { + $value = new PHPWord_Style_Tabs($value); + } + $this->$key = $value; } /** @@ -202,5 +212,14 @@ class PHPWord_Style_Paragraph { $this->_indent = $pValue; return $this; } + + /** + * Get tabs + * + * @return PHPWord_Style_Tabs + */ + public function getTabs() { + return $this->_tabs; + } } ?> \ No newline at end of file diff --git a/Classes/PHPWord/Style/Tab.php b/Classes/PHPWord/Style/Tab.php new file mode 100644 index 00000000..cd0882c1 --- /dev/null +++ b/Classes/PHPWord/Style/Tab.php @@ -0,0 +1,147 @@ +_val = (self::isStopType($val)) ? $val : 'clear'; + + // Default to 0 if the position is non-numeric + $this->_position = (is_numeric($position)) ? intval($position) : 0; + + // Default to NULL if no tab leader + $this->_leader = (self::isLeaderType($leader)) ? $leader : NULL; + } + + /** + * Creates the XML DOM for the instance of PHPWord_Style_Tab. + * + * @param PHPWord_Shared_XMLWriter $objWriter + */ + public function toXml(PHPWord_Shared_XMLWriter &$objWriter = NULL) { + if(isset($objWriter)) { + $objWriter->startElement("w:tab"); + $objWriter->writeAttribute("w:val", $this->_val); + if(!is_null($this->_leader)) { + $objWriter->writeAttribute("w:leader", $this->_leader); + } + $objWriter->writeAttribute("w:pos", $this->_position); + $objWriter->endElement(); + } + } + + /** + * Test if attribute is a valid stop type. + * + * @param string $attribute + * @return bool True if it is; false otherwise. + */ + private static function isStopType($attribute) { + return in_array($attribute, self::$_possibleStopTypes); + } + + /** + * Test if attribute is a valid leader type. + * + * @param string $attribute + * @return bool True if it is; false otherwise. + */ + private static function isLeaderType($attribute) { + return in_array($attribute, self::$_possibleLeaders); + } +} +?> \ No newline at end of file diff --git a/Classes/PHPWord/Style/Tabs.php b/Classes/PHPWord/Style/Tabs.php new file mode 100644 index 00000000..096c0b6f --- /dev/null +++ b/Classes/PHPWord/Style/Tabs.php @@ -0,0 +1,67 @@ +_tabs = $tabs; + } + + /** + * + * @param PHPWord_Shared_XMLWriter $objWriter + */ + public function toXml(PHPWord_Shared_XMLWriter &$objWriter = NULL) { + if(isset($objWriter)) { + $objWriter->startElement("w:tabs"); + foreach ($this->_tabs as &$tab) { + $tab->toXml($objWriter); + } + $objWriter->endElement(); + } + } +} +?> \ No newline at end of file diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 3800148e..549f1253 100644 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -114,13 +114,12 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { $spaceAfter = $style->getSpaceAfter(); $spacing = $style->getSpacing(); $indent = $style->getIndent(); - + $tabs = $style->getTabs(); - if(!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent)) { - - if(!$withoutPPR) { - $objWriter->startElement('w:pPr'); - } + if(!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent) || !is_null($tabs)) { + if(!$withoutPPR) { + $objWriter->startElement('w:pPr'); + } if(!is_null($align)) { $objWriter->startElement('w:jc'); @@ -136,26 +135,27 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { } if(!is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($spacing)) { - $objWriter->startElement('w:spacing'); - - if(!is_null($spaceBefore)) { - $objWriter->writeAttribute('w:before', $spaceBefore); - } - if(!is_null($spaceAfter)) { - $objWriter->writeAttribute('w:after', $spaceAfter); - } - if(!is_null($spacing)) { - $objWriter->writeAttribute('w:line', $spacing); - $objWriter->writeAttribute('w:lineRule', 'auto'); - } - + if(!is_null($spaceBefore)) { + $objWriter->writeAttribute('w:before', $spaceBefore); + } + if(!is_null($spaceAfter)) { + $objWriter->writeAttribute('w:after', $spaceAfter); + } + if(!is_null($spacing)) { + $objWriter->writeAttribute('w:line', $spacing); + $objWriter->writeAttribute('w:lineRule', 'auto'); + } $objWriter->endElement(); } - - if(!$withoutPPR) { - $objWriter->endElement(); // w:pPr - } + + if(!is_null($tabs)) { + $tabs->toXml($objWriter); + } + + if(!$withoutPPR) { + $objWriter->endElement(); // w:pPr + } } } diff --git a/changelog.txt b/changelog.txt index fa976217..d614ffaf 100644 --- a/changelog.txt +++ b/changelog.txt @@ -27,6 +27,7 @@ Fixed in branch for release 0.7 : - Feature: (Progi1984) GH-1 - Implement RTF Writer - Feature: (Progi1984) GH-2 - Implement ODT Writer - Feature: (kaystrobach) - Word2007 : Add rowspan and colspan to cells +- Feature: (RLovelett) - Word2007 : Support for tab stops - General: (MarkBaker) - Add superscript/subscript styling in Excel2007 Writer - General: (deds) - add indentation support to paragraphs - General: (Progi1984) GH-27 - Support for Composer diff --git a/composer.json b/composer.json index 28060794..0d714310 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ ], "require": { - "php": ">=5.2.0", + "php": ">=5.3.0", "ext-xml": "*" }, "recommend": { diff --git a/samples/Sample_02_TabStops.php b/samples/Sample_02_TabStops.php new file mode 100644 index 00000000..3c4d3841 --- /dev/null +++ b/samples/Sample_02_TabStops.php @@ -0,0 +1,64 @@ +'); +} + +require_once '../Classes/PHPWord.php'; + +// New Word Document +echo date('H:i:s') , ' Create new PHPWord object' , EOL; +$PHPWord = new PHPWord(); + +// Ads styles +$PHPWord->addParagraphStyle('multipleTab', array( + 'tabs' => array( + new PHPWord_Style_Tab('left', 1550), + new PHPWord_Style_Tab('center', 3200), + new PHPWord_Style_Tab('right', 5300) + ) +)); +$PHPWord->addParagraphStyle('rightTab', array( + 'tabs' => array( + new PHPWord_Style_Tab('right', 9090) + ) +)); +$PHPWord->addParagraphStyle('centerTab', array( + 'tabs' => array( + new PHPWord_Style_Tab('center', 4680) + ) +)); + +// New portrait section +$section = $PHPWord->createSection(); + +// Add listitem elements +$section->addText("Multiple Tabs:\tOne\tTwo\tThree", NULL, 'multipleTab'); +$section->addText("Left Aligned\tRight Aligned", NULL, 'rightTab'); +$section->addText("\tCenter Aligned", NULL, 'centerTab'); + +// Save File +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 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; + +// Echo done +echo date('H:i:s') , ' Done writing file' , EOL; +?> \ No newline at end of file diff --git a/samples/old/Image.php b/samples/old/Image.php index 71d0a7f2..6316783f 100644 --- a/samples/old/Image.php +++ b/samples/old/Image.php @@ -11,7 +11,7 @@ $section = $PHPWord->createSection(); $section->addImage('_mars.jpg'); $section->addTextBreak(2); -$section->addImage('_earth.JPG', array('width'=>210, 'height'=>210, 'align'=>'center')); +$section->addImage('_earth.jpg', array('width'=>210, 'height'=>210, 'align'=>'center')); $section->addTextBreak(2); $section->addImage('_mars.jpg', array('width'=>100, 'height'=>100, 'align'=>'right')); diff --git a/samples/old/_earth.JPG b/samples/old/_earth.jpg similarity index 100% rename from samples/old/_earth.JPG rename to samples/old/_earth.jpg