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

Respect indents from readed file #2570

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
- MsDoc Reader : Correct Font Size Calculation by [@oleibman](https://github.com/oleibman) fixing [#2526](https://github.com/PHPOffice/PHPWord/issues/2526) in [#2531](https://github.com/PHPOffice/PHPWord/pull/2531)
- TemplateProcessor Persist File After Destruct [@oleibman](https://github.com/oleibman) fixing [#2539](https://github.com/PHPOffice/PHPWord/issues/2539) in [#2545](https://github.com/PHPOffice/PHPWord/pull/2545)
- bug: TemplateProcessor fix multiline values [@gimler](https://github.com/gimler) fixing [#268](https://github.com/PHPOffice/PHPWord/issues/268), [#2323](https://github.com/PHPOffice/PHPWord/issues/2323) and [#2486](https://github.com/PHPOffice/PHPWord/issues/2486) in [#2522](https://github.com/PHPOffice/PHPWord/pull/2522)

- 32-bit Problem in PasswordEncoder [@oleibman](https://github.com/oleibman) fixing [#2550](https://github.com/PHPOffice/PHPWord/issues/2550) in [#2551](https://github.com/PHPOffice/PHPWord/pull/2551)
- Respect paragraph indent units when reading file by [@tugmaks](https://github.com/tugmaks) fixing [#507](https://github.com/PHPOffice/PHPWord/issues/507)

### Miscellaneous

Expand Down
6 changes: 4 additions & 2 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,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
77 changes: 75 additions & 2 deletions src/PhpWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

namespace PhpOffice\PhpWord\Style;

use function array_map;
use function is_numeric;
use function is_string;
use PhpOffice\PhpWord\Exception\InvalidStyleException;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\Text;
Expand Down Expand Up @@ -332,14 +335,26 @@ public function getIndentation()
}

/**
* Set shading.
* Set indentation.
*
* @param mixed $value
* @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
*
* @return self
*/
public function setIndentation($value = null)
{
$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;
Expand Down Expand Up @@ -367,6 +382,54 @@ public function setIndent($value = null)
return $this->setIndentation(['left' => $value]);
}

/**
* Set left indentation.
*
* @param float|int|numeric-string $value
*
* @return self
*/
public function setIndentLeft($value = null)
{
return $this->setIndentation(['left' => $value]);
}

/**
* Set right indentation.
*
* @param float|int|numeric-string $value
*
* @return self
*/
public function setIndentRight($value = null)
{
return $this->setIndentation(['right' => $value]);
}

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

/**
* Set right indentation.
*
* @param float|int|numeric-string $value
*
* @return self
*/
public function setIndentFirstLine($value = null)
{
return $this->setIndentation(['firstLine' => $value]);
}

/**
* Get hanging.
*
Expand All @@ -377,6 +440,16 @@ public function getHanging()
return $this->getChildStyleValue($this->indentation, 'hanging');
}

/**
* Get firstLine.
*
* @return int
*/
public function getFirstLine()
{
return $this->getChildStyleValue($this->indentation, 'firstLine');
}

/**
* Set hanging.
*
Expand Down
49 changes: 49 additions & 0 deletions tests/PhpWordTests/Reader/Word2007/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWordTests\Reader\Word2007;

use Generator;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\SimpleType\Border;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\SimpleType\VerticalJc;
Expand Down Expand Up @@ -289,4 +291,51 @@ public function testPageVerticalAlign(): void
$sectionStyle = $phpWord->getSection(0)->getStyle();
self::assertEquals(VerticalJc::CENTER, $sectionStyle->getVAlign());
}

/**
* @dataProvider providerIndentation
*
* @param string $indent
* @param float $left
* @param float $right
* @param null|float $hanging
* @param float $firstLine
*/
public function testIndentation($indent, $left, $right, $hanging, $firstLine): void
{
$documentXml = "<w:p>
<w:pPr>
$indent
</w:pPr>
<w:r>
<w:t>1.</w:t>
</w:r>
</w:p>";

$phpWord = $this->getDocumentFromString(['document' => $documentXml]);

$section = $phpWord->getSection(0);
$textRun = $section->getElements()[0];
self::assertInstanceOf(TextRun::class, $textRun);

$paragraphStyle = $textRun->getParagraphStyle();
self::assertInstanceOf(Style\Paragraph::class, $paragraphStyle);

$indentation = $paragraphStyle->getIndentation();
self::assertSame($left, $indentation->getLeft());
self::assertSame($right, $indentation->getRight());
self::assertSame($hanging, $indentation->getHanging());
self::assertSame($firstLine, $indentation->getFirstLine());
}

/**
* @return Generator<array{0:string, 1:float, 2:float, 3:null|float, 4: float}>
*/
public static function providerIndentation()
{
yield ['<w:ind w:left="709" w:right="488" w:hanging="10" w:firstLine="490"/>', 709.00, 488.00, 10.0, 490.00];
yield ['<w:ind w:hanging="10" w:firstLine="490"/>', 0, 0, 10.0, 490.00];
yield ['<w:ind w:left="709"/>', 709.00, 0, null, 0];
yield ['<w:ind w:right="488"/>', 0, 488.00, null, 0];
}
}