Skip to content

Commit 483c4d5

Browse files
committed
don't add 1 line when using EXACT line spacing rule
1 parent e830ebe commit 483c4d5

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PhpOffice\PhpWord\Settings;
2424
use PhpOffice\PhpWord\SimpleType\Jc;
2525
use PhpOffice\PhpWord\SimpleType\NumberFormat;
26+
use PhpOffice\PhpWord\Style\Paragraph;
2627

2728
/**
2829
* Common Html functions
@@ -531,18 +532,27 @@ private static function parseStyle($attribute, $styles)
531532
$styles['bgColor'] = trim($cValue, '#');
532533
break;
533534
case 'line-height':
535+
$matches = array();
534536
if (preg_match('/([0-9]+\.?[0-9]*[a-z]+)/', $cValue, $matches)) {
537+
//matches number with a unit, e.g. 12px, 15pt, 20mm, ...
535538
$spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::EXACT;
536-
$spacing = Converter::cssToTwip($matches[1]) / \PhpOffice\PhpWord\Style\Paragraph::LINE_HEIGHT;
539+
$spacing = Converter::cssToTwip($matches[1]);
537540
} elseif (preg_match('/([0-9]+)%/', $cValue, $matches)) {
541+
//matches percentages
538542
$spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO;
539-
$spacing = ((int) $matches[1]) / 100;
543+
//we are subtracting 1 line height because the Spacing writer is adding one line
544+
$spacing = ((((int) $matches[1]) / 100) * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT;
540545
} else {
546+
//any other, wich is a multiplier. E.g. 1.2
541547
$spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO;
542-
$spacing = $cValue;
548+
//we are subtracting 1 line height because the Spacing writer is adding one line
549+
$spacing = ($cValue * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT;
543550
}
544551
$styles['spacingLineRule'] = $spacingLineRule;
545-
$styles['lineHeight'] = $spacing;
552+
$styles['line-spacing'] = $spacing;
553+
break;
554+
case 'letter-spacing':
555+
$styles['letter-spacing'] = Converter::cssToTwip($cValue);
546556
break;
547557
case 'text-indent':
548558
$styles['indentation']['firstLine'] = Converter::cssToTwip($cValue);

src/PhpWord/Style/Font.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Font extends AbstractStyle
8080
*
8181
* @var array
8282
*/
83-
protected $aliases = array('line-height' => 'lineHeight');
83+
protected $aliases = array('line-height' => 'lineHeight', 'letter-spacing' => 'spacing');
8484

8585
/**
8686
* Font style type

src/PhpWord/Style/Paragraph.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Paragraph extends Border
6161
*
6262
* @var array
6363
*/
64-
protected $aliases = array('line-height' => 'lineHeight');
64+
protected $aliases = array('line-height' => 'lineHeight', 'line-spacing' => 'spacing');
6565

6666
/**
6767
* Parent style
@@ -545,7 +545,8 @@ public function setLineHeight($lineHeight)
545545
}
546546

547547
$this->lineHeight = $lineHeight;
548-
$this->setSpacing($lineHeight * self::LINE_HEIGHT);
548+
$this->setSpacing(($lineHeight -1) * self::LINE_HEIGHT);
549+
$this->setSpacingLineRule(\PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO);
549550

550551
return $this;
551552
}

src/PhpWord/Writer/Word2007/Style/Spacing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function write()
4646
$line = $style->getLine();
4747
//if linerule is auto, the spacing is supposed to include the height of the line itself, which is 240 twips
4848
if (null !== $line && 'auto' === $style->getLineRule()) {
49-
$line += 240;
49+
$line += \PhpOffice\PhpWord\Style\Paragraph::LINE_HEIGHT;
5050
}
5151
$xmlWriter->writeAttributeIf(!is_null($line), 'w:line', $line);
5252

0 commit comments

Comments
 (0)