Skip to content

Commit

Permalink
Reader Word2007 : Respect paragraph indent units (PHPOffice#2726)
Browse files Browse the repository at this point in the history
Co-authored-by: Maksim Tiugaev <[email protected]>
  • Loading branch information
Progi1984 and tugmaks authored Jan 22, 2025
1 parent 337dc27 commit 136f549
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 75 deletions.
6 changes: 6 additions & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
- Writer ODText: Support for images inside a textRun by [@Progi1984](https://github.com/Progi1984) fixing [#2240](https://github.com/PHPOffice/PHPWord/issues/2240) in [#2668](https://github.com/PHPOffice/PHPWord/pull/2668)
- Allow vAlign and vMerge on Style\Cell to be set to null by [@SpraxDev](https://github.com/SpraxDev) fixing [#2673](https://github.com/PHPOffice/PHPWord/issues/2673) in [#2676](https://github.com/PHPOffice/PHPWord/pull/2676)
- Reader HTML: Support for differents size units for table by [@Progi1984](https://github.com/Progi1984) fixing [#2384](https://github.com/PHPOffice/PHPWord/issues/2384), [#2701](https://github.com/PHPOffice/PHPWord/issues/2701) in [#2725](https://github.com/PHPOffice/PHPWord/pull/2725)
- Reader Word2007 : Respect paragraph indent units by [@tugmaks](https://github.com/tugmaks) & [@Progi1984](https://github.com/Progi1984) fixing [#507](https://github.com/PHPOffice/PHPWord/issues/507) in [#2726](https://github.com/PHPOffice/PHPWord/pull/2726)

### Miscellaneous

- Bump dompdf/dompdf from 2.0.4 to 3.0.0 by [@dependabot](https://github.com/dependabot) fixing [#2621](https://github.com/PHPOffice/PHPWord/issues/2621) in [#2666](https://github.com/PHPOffice/PHPWord/pull/2666)
- Add test case to make sure vMerge defaults to 'continue' by [@SpraxDev](https://github.com/SpraxDev) in [#2677](https://github.com/PHPOffice/PHPWord/pull/2677)

### Deprecations
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::getIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::getIndentLeft()`
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::setHanging()` : Use `PhpOffice\PhpWord\Style\Paragraph::setIndentHanging()`
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::setIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::setIndentLeft()`

### BC Breaks
6 changes: 4 additions & 2 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,10 @@ protected function readParagraphStyle(XMLReader $xmlReader, DOMElement $domNode)
'alignment' => [self::READ_VALUE, 'w:jc'],
'basedOn' => [self::READ_VALUE, 'w:basedOn'],
'next' => [self::READ_VALUE, 'w:next'],
'indent' => [self::READ_VALUE, 'w:ind', 'w:left'],
'hanging' => [self::READ_VALUE, 'w:ind', 'w:hanging'],
'indentLeft' => [self::READ_VALUE, 'w:ind', 'w:left'],
'indentRight' => [self::READ_VALUE, 'w:ind', 'w:right'],
'indentHanging' => [self::READ_VALUE, 'w:ind', 'w:hanging'],
'indentFirstLine' => [self::READ_VALUE, 'w:ind', 'w:firstLine'],
'spaceAfter' => [self::READ_VALUE, 'w:spacing', 'w:after'],
'spaceBefore' => [self::READ_VALUE, 'w:spacing', 'w:before'],
'widowControl' => [self::READ_FALSE, 'w:widowControl'],
Expand Down
3 changes: 1 addition & 2 deletions src/PhpWord/Style/AbstractStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,11 @@ protected function setEnumVal($value = null, $enum = [], $default = null)
* Set object value.
*
* @param mixed $value
* @param string $styleName
* @param mixed &$style
*
* @return mixed
*/
protected function setObjectVal($value, $styleName, &$style)
protected function setObjectVal($value, string $styleName, &$style)
{
$styleClass = substr(static::class, 0, (int) strrpos(static::class, '\\')) . '\\' . $styleName;
if (is_array($value)) {
Expand Down
58 changes: 17 additions & 41 deletions src/PhpWord/Style/Indentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@ class Indentation extends AbstractStyle
/**
* Left indentation (twip).
*
* @var float|int
* @var null|float
*/
private $left = 0;

/**
* Right indentation (twip).
*
* @var float|int
* @var null|float
*/
private $right = 0;

/**
* Additional first line indentation (twip).
*
* @var float|int
* @var null|float
*/
private $firstLine = 0;

/**
* Indentation removed from first line (twip).
*
* @var float|int
* @var null|float
*/
private $hanging;
private $hanging = 0;

/**
* Create a new instance.
Expand All @@ -66,96 +66,72 @@ public function __construct($style = [])

/**
* Get left.
*
* @return float|int
*/
public function getLeft()
public function getLeft(): ?float
{
return $this->left;
}

/**
* Set left.
*
* @param float|int $value
*
* @return self
*/
public function setLeft($value)
public function setLeft(?float $value): self
{
$this->left = $this->setNumericVal($value, $this->left);
$this->left = $this->setNumericVal($value);

return $this;
}

/**
* Get right.
*
* @return float|int
*/
public function getRight()
public function getRight(): ?float
{
return $this->right;
}

/**
* Set right.
*
* @param float|int $value
*
* @return self
*/
public function setRight($value)
public function setRight(?float $value): self
{
$this->right = $this->setNumericVal($value, $this->right);
$this->right = $this->setNumericVal($value);

return $this;
}

/**
* Get first line.
*
* @return float|int
*/
public function getFirstLine()
public function getFirstLine(): ?float
{
return $this->firstLine;
}

/**
* Set first line.
*
* @param float|int $value
*
* @return self
*/
public function setFirstLine($value)
public function setFirstLine(?float $value): self
{
$this->firstLine = $this->setNumericVal($value, $this->firstLine);
$this->firstLine = $this->setNumericVal($value);

return $this;
}

/**
* Get hanging.
*
* @return float|int
*/
public function getHanging()
public function getHanging(): ?float
{
return $this->hanging;
}

/**
* Set hanging.
*
* @param float|int $value
*
* @return self
*/
public function setHanging($value = null)
public function setHanging(?float $value = null): self
{
$this->hanging = $this->setNumericVal($value, $this->hanging);
$this->hanging = $this->setNumericVal($value);

return $this;
}
Expand Down
114 changes: 86 additions & 28 deletions src/PhpWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,74 +322,132 @@ public function setNext($value = null)
return $this;
}

/**
* Get hanging.
*/
public function getHanging(): ?float
{
return $this->getChildStyleValue($this->indentation, 'hanging');
}

/**
* Get indentation.
*
* @return null|Indentation
* @deprecated 1.4.0 Use getIndentLeft
*/
public function getIndentation()
public function getIndent(): ?float
{
return $this->getChildStyleValue($this->indentation, 'left');
}

/**
* Get indentation.
*/
public function getIndentation(): ?Indentation
{
return $this->indentation;
}

/**
* Set shading.
*
* @param mixed $value
*
* @return self
* Get firstLine.
*/
public function setIndentation($value = null)
public function getIndentFirstLine(): ?float
{
$this->setObjectVal($value, 'Indentation', $this->indentation);
return $this->getChildStyleValue($this->indentation, 'firstLine');
}

return $this;
/**
* Get left indentation.
*/
public function getIndentLeft(): ?float
{
return $this->getChildStyleValue($this->indentation, 'left');
}

/**
* Get indentation.
* Get right indentation.
*/
public function getIndentRight(): ?float
{
return $this->getChildStyleValue($this->indentation, 'right');
}

/**
* Set hanging.
*
* @return int
* @deprecated 1.4.0 Use setIndentHanging
*/
public function getIndent()
public function setHanging(?float $value = null): self
{
return $this->getChildStyleValue($this->indentation, 'left');
return $this->setIndentation(['hanging' => $value]);
}

/**
* Set indentation.
*
* @param int $value
*
* @return self
* @deprecated 1.4.0 Use setIndentLeft
*/
public function setIndent($value = null)
public function setIndent(?float $value = null): self
{
return $this->setIndentation(['left' => $value]);
}

/**
* Get hanging.
* Set indentation.
*
* @return int
* @param array{
* left?:null|float|int|numeric-string,
* right?:null|float|int|numeric-string,
* hanging?:null|float|int|numeric-string,
* firstLine?:null|float|int|numeric-string
* } $value
*/
public function getHanging()
public function setIndentation(array $value = []): self
{
return $this->getChildStyleValue($this->indentation, 'hanging');
$value = array_map(function ($indent) {
if (is_string($indent) || is_numeric($indent)) {
$indent = $this->setFloatVal($indent);
}

return $indent;
}, $value);
$this->setObjectVal($value, 'Indentation', $this->indentation);

return $this;
}

/**
* Set hanging.
*
* @param int $value
*
* @return self
* Set hanging indentation.
*/
public function setHanging($value = null)
public function setIndentHanging(?float $value = null): self
{
return $this->setIndentation(['hanging' => $value]);
}

/**
* Set firstline indentation.
*/
public function setIndentFirstLine(?float $value = null): self
{
return $this->setIndentation(['firstLine' => $value]);
}

/**
* Set left indentation.
*/
public function setIndentLeft(?float $value = null): self
{
return $this->setIndentation(['left' => $value]);
}

/**
* Set right indentation.
*/
public function setIndentRight(?float $value = null): self
{
return $this->setIndentation(['right' => $value]);
}

/**
* Get spacing.
*
Expand Down
Loading

0 comments on commit 136f549

Please sign in to comment.