Skip to content

Commit a6894dd

Browse files
committed
Respect indents from readed file
1 parent 8b891bb commit a6894dd

File tree

4 files changed

+129
-5
lines changed

4 files changed

+129
-5
lines changed

docs/changes/2.x/2.0.0.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
- 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)
1212
- 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)
1313
- 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)
14-
1514
- 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)
15+
- Respect paragraph indent units when reading file by [@tugmaks](https://github.com/tugmaks) fixing [#507](https://github.com/PHPOffice/PHPWord/issues/507)
1616

1717
### Miscellaneous
1818

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,10 @@ protected function readParagraphStyle(XMLReader $xmlReader, DOMElement $domNode)
518518
'alignment' => [self::READ_VALUE, 'w:jc'],
519519
'basedOn' => [self::READ_VALUE, 'w:basedOn'],
520520
'next' => [self::READ_VALUE, 'w:next'],
521-
'indent' => [self::READ_VALUE, 'w:ind', 'w:left'],
522-
'hanging' => [self::READ_VALUE, 'w:ind', 'w:hanging'],
521+
'indentLeft' => [self::READ_VALUE, 'w:ind', 'w:left'],
522+
'indentRight' => [self::READ_VALUE, 'w:ind', 'w:right'],
523+
'indentHanging' => [self::READ_VALUE, 'w:ind', 'w:hanging'],
524+
'indentFirstLine' => [self::READ_VALUE, 'w:ind', 'w:firstLine'],
523525
'spaceAfter' => [self::READ_VALUE, 'w:spacing', 'w:after'],
524526
'spaceBefore' => [self::READ_VALUE, 'w:spacing', 'w:before'],
525527
'widowControl' => [self::READ_FALSE, 'w:widowControl'],

src/PhpWord/Style/Paragraph.php

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20+
use function array_map;
21+
use function is_numeric;
22+
use function is_string;
2023
use PhpOffice\PhpWord\Exception\InvalidStyleException;
2124
use PhpOffice\PhpWord\Settings;
2225
use PhpOffice\PhpWord\Shared\Text;
@@ -332,14 +335,26 @@ public function getIndentation()
332335
}
333336

334337
/**
335-
* Set shading.
338+
* Set indentation.
336339
*
337-
* @param mixed $value
340+
* @param array{
341+
* left?:null|float|int|numeric-string,
342+
* right?:null|float|int|numeric-string,
343+
* hanging?:null|float|int|numeric-string,
344+
* firstLine?:null|float|int|numeric-string
345+
* } $value
338346
*
339347
* @return self
340348
*/
341349
public function setIndentation($value = null)
342350
{
351+
$value = array_map(function ($indent) {
352+
if (is_string($indent) && is_numeric($indent)) {
353+
$indent = $this->setFloatVal($indent);
354+
}
355+
356+
return $indent;
357+
}, $value);
343358
$this->setObjectVal($value, 'Indentation', $this->indentation);
344359

345360
return $this;
@@ -367,6 +382,54 @@ public function setIndent($value = null)
367382
return $this->setIndentation(['left' => $value]);
368383
}
369384

385+
/**
386+
* Set left indentation.
387+
*
388+
* @param float|int|numeric-string $value
389+
*
390+
* @return self
391+
*/
392+
public function setIndentLeft($value = null)
393+
{
394+
return $this->setIndentation(['left' => $value]);
395+
}
396+
397+
/**
398+
* Set right indentation.
399+
*
400+
* @param float|int|numeric-string $value
401+
*
402+
* @return self
403+
*/
404+
public function setIndentRight($value = null)
405+
{
406+
return $this->setIndentation(['right' => $value]);
407+
}
408+
409+
/**
410+
* Set right indentation.
411+
*
412+
* @param float|int|numeric-string $value
413+
*
414+
* @return self
415+
*/
416+
public function setIndentHanging($value = null)
417+
{
418+
return $this->setIndentation(['hanging' => $value]);
419+
}
420+
421+
/**
422+
* Set right indentation.
423+
*
424+
* @param float|int|numeric-string $value
425+
*
426+
* @return self
427+
*/
428+
public function setIndentFirstLine($value = null)
429+
{
430+
return $this->setIndentation(['firstLine' => $value]);
431+
}
432+
370433
/**
371434
* Get hanging.
372435
*
@@ -377,6 +440,16 @@ public function getHanging()
377440
return $this->getChildStyleValue($this->indentation, 'hanging');
378441
}
379442

443+
/**
444+
* Get firstLine.
445+
*
446+
* @return int
447+
*/
448+
public function getFirstLine()
449+
{
450+
return $this->getChildStyleValue($this->indentation, 'firstLine');
451+
}
452+
380453
/**
381454
* Set hanging.
382455
*

tests/PhpWordTests/Reader/Word2007/StyleTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWordTests\Reader\Word2007;
1919

20+
use Generator;
21+
use PhpOffice\PhpWord\Element\TextRun;
2022
use PhpOffice\PhpWord\SimpleType\Border;
2123
use PhpOffice\PhpWord\SimpleType\TblWidth;
2224
use PhpOffice\PhpWord\SimpleType\VerticalJc;
@@ -289,4 +291,51 @@ public function testPageVerticalAlign(): void
289291
$sectionStyle = $phpWord->getSection(0)->getStyle();
290292
self::assertEquals(VerticalJc::CENTER, $sectionStyle->getVAlign());
291293
}
294+
295+
/**
296+
* @dataProvider providerIndentation
297+
*
298+
* @param string $indent
299+
* @param float $left
300+
* @param float $right
301+
* @param null|float $hanging
302+
* @param float $firstLine
303+
*/
304+
public function testIndentation($indent, $left, $right, $hanging, $firstLine): void
305+
{
306+
$documentXml = "<w:p>
307+
<w:pPr>
308+
$indent
309+
</w:pPr>
310+
<w:r>
311+
<w:t>1.</w:t>
312+
</w:r>
313+
</w:p>";
314+
315+
$phpWord = $this->getDocumentFromString(['document' => $documentXml]);
316+
317+
$section = $phpWord->getSection(0);
318+
$textRun = $section->getElements()[0];
319+
self::assertInstanceOf(TextRun::class, $textRun);
320+
321+
$paragraphStyle = $textRun->getParagraphStyle();
322+
self::assertInstanceOf(Style\Paragraph::class, $paragraphStyle);
323+
324+
$indentation = $paragraphStyle->getIndentation();
325+
self::assertSame($left, $indentation->getLeft());
326+
self::assertSame($right, $indentation->getRight());
327+
self::assertSame($hanging, $indentation->getHanging());
328+
self::assertSame($firstLine, $indentation->getFirstLine());
329+
}
330+
331+
/**
332+
* @return Generator<array{0:string, 1:float, 2:float, 3:null|float, 4: float}>
333+
*/
334+
public static function providerIndentation()
335+
{
336+
yield ['<w:ind w:left="709" w:right="488" w:hanging="10" w:firstLine="490"/>', 709.00, 488.00, 10.0, 490.00];
337+
yield ['<w:ind w:hanging="10" w:firstLine="490"/>', 0, 0, 10.0, 490.00];
338+
yield ['<w:ind w:left="709"/>', 709.00, 0, null, 0];
339+
yield ['<w:ind w:right="488"/>', 0, 488.00, null, 0];
340+
}
292341
}

0 commit comments

Comments
 (0)