Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Word2007 Reader : Support for table cell borders and margins #2454

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Improved TextDirection for styling a cell by @terryzwt in #2429
- Word2007 Reader : Added option to disable loading images by @aelliott1485 in #2450
- HTML Writer : Added border-spacing to default styles for table by @kernusr in #2451
- Word2007 Reader : Support for table cell borders and margins by @kernusr in #2454

### Bug fixes

Expand Down
25 changes: 20 additions & 5 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,8 @@ protected function readTable(XMLReader $xmlReader, DOMElement $domNode, $parent,
} elseif ('w:tc' == $rowNode->nodeName) { // Cell
$cellWidth = $xmlReader->getAttribute('w:w', $rowNode, 'w:tcPr/w:tcW');
$cellStyle = null;
$cellStyleNode = $xmlReader->getElement('w:tcPr', $rowNode);
if (null !== $cellStyleNode) {
$cellStyle = $this->readCellStyle($xmlReader, $cellStyleNode);
if ($xmlReader->elementExists('w:tcPr', $rowNode)) {
$cellStyle = $this->readCellStyle($xmlReader, $rowNode);
}

$cell = $row->addCell($cellWidth, $cellStyle);
Expand Down Expand Up @@ -573,7 +572,7 @@ private function readTableIndent(XMLReader $xmlReader, DOMElement $domNode)
/**
* Read w:tcPr.
*
* @return array
* @return null|array
*/
private function readCellStyle(XMLReader $xmlReader, DOMElement $domNode)
{
Expand All @@ -585,8 +584,24 @@ private function readCellStyle(XMLReader $xmlReader, DOMElement $domNode)
'bgColor' => [self::READ_VALUE, 'w:shd', 'w:fill'],
'noWrap' => [self::READ_VALUE, 'w:noWrap', null, null, true],
];
$style = null;

return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
if ($xmlReader->elementExists('w:tcPr', $domNode)) {
$styleNode = $xmlReader->getElement('w:tcPr', $domNode);

$borders = ['top', 'left', 'bottom', 'right'];
foreach ($borders as $side) {
$ucfSide = ucfirst($side);

$styleDefs['border' . $ucfSide . 'Size'] = [self::READ_VALUE, 'w:tcBorders/w:' . $side, 'w:sz'];
$styleDefs['border' . $ucfSide . 'Color'] = [self::READ_VALUE, 'w:tcBorders/w:' . $side, 'w:color'];
$styleDefs['border' . $ucfSide . 'Style'] = [self::READ_VALUE, 'w:tcBorders/w:' . $side, 'w:val'];
}

$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
}

return $style;
}

/**
Expand Down
126 changes: 126 additions & 0 deletions src/PhpWord/Style/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
class Border extends AbstractStyle
{
const DEFAULT_MARGIN = 1440; // In twips.

/**
* Border Top Size.
*
Expand Down Expand Up @@ -106,6 +108,34 @@ class Border extends AbstractStyle
*/
protected $borderBottomStyle;

/**
* Top margin spacing.
*
* @var float|int
*/
protected $marginTop = self::DEFAULT_MARGIN;

/**
* Left margin spacing.
*
* @var float|int
*/
protected $marginLeft = self::DEFAULT_MARGIN;

/**
* Right margin spacing.
*
* @var float|int
*/
protected $marginRight = self::DEFAULT_MARGIN;

/**
* Bottom margin spacing.
*
* @var float|int
*/
protected $marginBottom = self::DEFAULT_MARGIN;

/**
* Get border size.
*
Expand Down Expand Up @@ -501,4 +531,100 @@ public function hasBorder()

return $borders !== array_filter($borders, 'is_null');
}

/**
* Get Margin Top.
*
* @return float|int
*/
public function getMarginTop()
{
return $this->marginTop;
}

/**
* Set Margin Top.
*
* @param float|int $value
*
* @return self
*/
public function setMarginTop($value = null)
{
$this->marginTop = $this->setNumericVal($value, self::DEFAULT_MARGIN);

return $this;
}

/**
* Get Margin Left.
*
* @return float|int
*/
public function getMarginLeft()
{
return $this->marginLeft;
}

/**
* Set Margin Left.
*
* @param float|int $value
*
* @return self
*/
public function setMarginLeft($value = null)
{
$this->marginLeft = $this->setNumericVal($value, self::DEFAULT_MARGIN);

return $this;
}

/**
* Get Margin Right.
*
* @return float|int
*/
public function getMarginRight()
{
return $this->marginRight;
}

/**
* Set Margin Right.
*
* @param float|int $value
*
* @return self
*/
public function setMarginRight($value = null)
{
$this->marginRight = $this->setNumericVal($value, self::DEFAULT_MARGIN);

return $this;
}

/**
* Get Margin Bottom.
*
* @return float|int
*/
public function getMarginBottom()
{
return $this->marginBottom;
}

/**
* Set Margin Bottom.
*
* @param float|int $value
*
* @return self
*/
public function setMarginBottom($value = null)
{
$this->marginBottom = $this->setNumericVal($value, self::DEFAULT_MARGIN);

return $this;
}
}
125 changes: 0 additions & 125 deletions src/PhpWord/Style/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class Section extends Border
*/
const DEFAULT_WIDTH = 11905.511811024; // In twips.
const DEFAULT_HEIGHT = 16837.79527559; // In twips.
const DEFAULT_MARGIN = 1440; // In twips.
const DEFAULT_GUTTER = 0; // In twips.
const DEFAULT_HEADER_HEIGHT = 720; // In twips.
const DEFAULT_FOOTER_HEIGHT = 720; // In twips.
Expand Down Expand Up @@ -77,34 +76,6 @@ class Section extends Border
*/
private $pageSizeH = self::DEFAULT_HEIGHT;

/**
* Top margin spacing.
*
* @var float|int
*/
private $marginTop = self::DEFAULT_MARGIN;

/**
* Left margin spacing.
*
* @var float|int
*/
private $marginLeft = self::DEFAULT_MARGIN;

/**
* Right margin spacing.
*
* @var float|int
*/
private $marginRight = self::DEFAULT_MARGIN;

/**
* Bottom margin spacing.
*
* @var float|int
*/
private $marginBottom = self::DEFAULT_MARGIN;

/**
* Page gutter spacing.
*
Expand Down Expand Up @@ -344,102 +315,6 @@ public function setPageSizeH($value = null)
return $this;
}

/**
* Get Margin Top.
*
* @return float|int
*/
public function getMarginTop()
{
return $this->marginTop;
}

/**
* Set Margin Top.
*
* @param float|int $value
*
* @return self
*/
public function setMarginTop($value = null)
{
$this->marginTop = $this->setNumericVal($value, self::DEFAULT_MARGIN);

return $this;
}

/**
* Get Margin Left.
*
* @return float|int
*/
public function getMarginLeft()
{
return $this->marginLeft;
}

/**
* Set Margin Left.
*
* @param float|int $value
*
* @return self
*/
public function setMarginLeft($value = null)
{
$this->marginLeft = $this->setNumericVal($value, self::DEFAULT_MARGIN);

return $this;
}

/**
* Get Margin Right.
*
* @return float|int
*/
public function getMarginRight()
{
return $this->marginRight;
}

/**
* Set Margin Right.
*
* @param float|int $value
*
* @return self
*/
public function setMarginRight($value = null)
{
$this->marginRight = $this->setNumericVal($value, self::DEFAULT_MARGIN);

return $this;
}

/**
* Get Margin Bottom.
*
* @return float|int
*/
public function getMarginBottom()
{
return $this->marginBottom;
}

/**
* Set Margin Bottom.
*
* @param float|int $value
*
* @return self
*/
public function setMarginBottom($value = null)
{
$this->marginBottom = $this->setNumericVal($value, self::DEFAULT_MARGIN);

return $this;
}

/**
* Get gutter.
*
Expand Down
Loading