From 8363ee27d1461177dd625974f43feb1c160eb6ba Mon Sep 17 00:00:00 2001 From: Julien Carignan Date: Fri, 28 Mar 2014 13:26:36 -0400 Subject: [PATCH] table row height rule moved to rowStyle (ivanlanin) new usage: $table->addRow($height, array("heightRule"=>"exact")); Signed-off-by: Julien Carignan --- Classes/PHPWord/Section/Table.php | 4 +-- Classes/PHPWord/Section/Table/Row.php | 20 +-------------- Classes/PHPWord/Style/Row.php | 32 ++++++++++++++++++++++++ Classes/PHPWord/Writer/Word2007/Base.php | 8 +++--- samples/Sample_21_TableRowRules.php | 11 +++++--- 5 files changed, 46 insertions(+), 29 deletions(-) diff --git a/Classes/PHPWord/Section/Table.php b/Classes/PHPWord/Section/Table.php index 7efdf0b8..80126cba 100755 --- a/Classes/PHPWord/Section/Table.php +++ b/Classes/PHPWord/Section/Table.php @@ -101,9 +101,9 @@ class PHPWord_Section_Table * @param int $height * @param mixed $style */ - public function addRow($height = null, $style = null, $hRules = null) + public function addRow($height = null, $style = null) { - $row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style, $hRules); + $row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style); $this->_rows[] = $row; return $row; } diff --git a/Classes/PHPWord/Section/Table/Row.php b/Classes/PHPWord/Section/Table/Row.php index 5b239c89..7ca99823 100644 --- a/Classes/PHPWord/Section/Table/Row.php +++ b/Classes/PHPWord/Section/Table/Row.php @@ -38,13 +38,6 @@ class PHPWord_Section_Table_Row */ private $_height = null; - /** - * Row heightRules - * - * @var array - */ - private $_heightRules = array(); - /** * Row style * @@ -82,12 +75,11 @@ class PHPWord_Section_Table_Row * @param int $height * @param mixed $style */ - public function __construct($insideOf, $pCount, $height = null, $style = null, $hRules = null) + public function __construct($insideOf, $pCount, $height = null, $style = null) { $this->_insideOf = $insideOf; $this->_pCount = $pCount; $this->_height = $height; - $this->_heightRules = $hRules; $this->_style = new PHPWord_Style_Row(); if (!is_null($style)) { @@ -146,14 +138,4 @@ class PHPWord_Section_Table_Row { return $this->_height; } - - /** - * Get all row height rules - * - * @return array - */ - public function getHeightRules() - { - return $this->_heightRules; - } } diff --git a/Classes/PHPWord/Style/Row.php b/Classes/PHPWord/Style/Row.php index da039d84..aae7e851 100644 --- a/Classes/PHPWord/Style/Row.php +++ b/Classes/PHPWord/Style/Row.php @@ -45,6 +45,13 @@ class PHPWord_Style_Row */ private $_cantSplit = false; + /** + * Table row height rule (auto, exact, atLeast) + * + * @var String + */ + private $_heightRule = null; + /** * Create a new row style */ @@ -112,4 +119,29 @@ class PHPWord_Style_Row { return $this->_cantSplit; } + + /** + * Set heightRule + * + * @param String $pValue + * @return PHPWord_Style_Row + */ + public function setHeightRule($pValue = false) + { + if (!is_string($pValue)) { + $pValue = null; + } + $this->_heightRule = $pValue; + return $this; + } + + /** + * Get heightRule + * + * @return heightRule + */ + public function getHeightRule() + { + return $this->_heightRule; + } } diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 57bb7266..0a45fb20 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -569,11 +569,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart for ($i = 0; $i < $_cRows; $i++) { $row = $_rows[$i]; - $height = $row->getHeight(); - $heightRules = $row->getHeightRules(); + $height = $row->getHeight(); $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); $cantSplit = $rowStyle->getCantSplit(); + $heightRule = $rowStyle->getHeightRule(); $objWriter->startElement('w:tr'); @@ -581,9 +581,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->startElement('w:trPr'); if (!is_null($height)) { $objWriter->startElement('w:trHeight'); - if(!is_null($heightRules)) { + if(!is_null($heightRule)) { $objWriter->startAttribute('w:hRule'); - $objWriter->text($heightRules); + $objWriter->text($heightRule); $objWriter->endAttribute(); } $objWriter->writeAttribute('w:val', $height); diff --git a/samples/Sample_21_TableRowRules.php b/samples/Sample_21_TableRowRules.php index 83d03e0c..18c67be3 100644 --- a/samples/Sample_21_TableRowRules.php +++ b/samples/Sample_21_TableRowRules.php @@ -9,7 +9,9 @@ echo date('H:i:s') , ' Create new PHPWord object' , EOL; $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); -$section->addText("By default, a table row adds a textbreak after its content (notice the red border), even if the row height is <= height of the content"); +$section->addText("By default, when you insert an image, it adds a textbreak after its content."); +$section->addText("If we want a simple border around an image, we wrap the image inside a table->row->cell"); +$section->addText("On the image with the red border, even if we set the row height to the height of the image, the textbreak is still there:"); $table1 = $section->addTable(array("cellMargin"=> 0, "cellMarginRight"=> 0, "cellMarginBottom"=> 0, "cellMarginLeft"=> 0)); $table1->addRow(3750); @@ -17,16 +19,17 @@ $cell1 = $table1->addCell(null, array("valign" => "top", "borderSize" => 30, "bo $cell1->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center")); $section->addTextBreak(); -$section->addText("But if we set the row rule \"exact\", we get rid of the textbreak!"); +$section->addText("But if we set the rowStyle hRule \"exact\", the real row height is used, removing the textbreak:"); $table2 = $section->addTable(array("cellMargin"=> 0, "cellMarginRight"=> 0, "cellMarginBottom"=> 0, "cellMarginLeft"=> 0)); -$table2->addRow(3750, null, "exact"); +$table2->addRow(3750, array("heightRule"=>"exact")); $cell2 = $table2->addCell(null, array("valign" => "top", "borderSize" => 30, "borderColor" => "00ff00")); $cell2->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center")); $section->addTextBreak(); $section->addText("In this example, image is 250px height. Rows are calculated in twips, and 1px = 15twips."); -$section->addText("So: $"."table2->addRow(3750, null, 'exact');"); +$section->addText("So: $"."table2->addRow(3750, array('heightRule'=>'exact'));"); +$section->addText("heightRule defaults to 'atLeast' when the row has an height set, and default to 'auto' otherwise"); // Save file $name = basename(__FILE__, '.php');