From 7f78322d043b3e26c2ae6b8ecdb269ba2f886e9f Mon Sep 17 00:00:00 2001 From: joelgo Date: Wed, 17 Mar 2021 13:09:13 -0500 Subject: [PATCH 1/3] Update html.php For acept text in line-height how "line-height:normal", etc. --- src/PhpWord/Shared/Html.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index eabd102d61..d5a5b65799 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -661,7 +661,21 @@ protected static function parseStyle($attribute, $styles) break; case 'line-height': $matches = array(); - if (preg_match('/([0-9]+\.?[0-9]*[a-z]+)/', $cValue, $matches)) { + if (preg_match('/([a-z]+)/', $cValue, $matches)) { + //$cvalue text and not number + if($cValue == 'normal') { + $cValue = 1.12; + $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO; + //we are subtracting 1 line height because the Spacing writer is adding one line + $spacing = ($cValue * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT; + }else{ + $cValue = 1.13; + $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO; + //we are subtracting 1 line height because the Spacing writer is adding one line + $spacing = ($cValue * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT; + } + } + elseif (preg_match('/([0-9]+\.?[0-9]*[a-z]+)/', $cValue, $matches)) { //matches number with a unit, e.g. 12px, 15pt, 20mm, ... $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::EXACT; $spacing = Converter::cssToTwip($matches[1]); From d731e9284fc5a6b26e750009cb31dece7fea0ba0 Mon Sep 17 00:00:00 2001 From: DE TROOSTEMBERGH Antoine Date: Wed, 14 Apr 2021 22:15:58 +0200 Subject: [PATCH 2/3] fix checkstyle issues --- src/PhpWord/Shared/Html.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index d5a5b65799..fbdc051bf3 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -663,19 +663,15 @@ protected static function parseStyle($attribute, $styles) $matches = array(); if (preg_match('/([a-z]+)/', $cValue, $matches)) { //$cvalue text and not number - if($cValue == 'normal') { + if ($cValue == 'normal') { $cValue = 1.12; - $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO; - //we are subtracting 1 line height because the Spacing writer is adding one line - $spacing = ($cValue * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT; - }else{ + } else { $cValue = 1.13; - $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO; - //we are subtracting 1 line height because the Spacing writer is adding one line - $spacing = ($cValue * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT; } - } - elseif (preg_match('/([0-9]+\.?[0-9]*[a-z]+)/', $cValue, $matches)) { + $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO; + //we are subtracting 1 line height because the Spacing writer is adding one line + $spacing = ($cValue * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT; + } elseif (preg_match('/([0-9]+\.?[0-9]*[a-z]+)/', $cValue, $matches)) { //matches number with a unit, e.g. 12px, 15pt, 20mm, ... $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::EXACT; $spacing = Converter::cssToTwip($matches[1]); From a4055fb7a0ddb30c557dcdefbaa37e378a1fa929 Mon Sep 17 00:00:00 2001 From: DE TROOSTEMBERGH Antoine Date: Thu, 15 Apr 2021 21:04:45 +0200 Subject: [PATCH 3/3] 'normal' line-height means default line height Add unit test --- src/PhpWord/Shared/Html.php | 11 ++--------- tests/PhpWord/Shared/HtmlTest.php | 5 +++++ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index fbdc051bf3..a2ea946524 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -661,16 +661,9 @@ protected static function parseStyle($attribute, $styles) break; case 'line-height': $matches = array(); - if (preg_match('/([a-z]+)/', $cValue, $matches)) { - //$cvalue text and not number - if ($cValue == 'normal') { - $cValue = 1.12; - } else { - $cValue = 1.13; - } + if ($cValue === 'normal') { $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO; - //we are subtracting 1 line height because the Spacing writer is adding one line - $spacing = ($cValue * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT; + $spacing = 0; } elseif (preg_match('/([0-9]+\.?[0-9]*[a-z]+)/', $cValue, $matches)) { //matches number with a unit, e.g. 12px, 15pt, 20mm, ... $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::EXACT; diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index c6061632e2..80f96d5773 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -158,6 +158,7 @@ public function testParseLineHeight() Html::addHtml($section, '

test

'); Html::addHtml($section, '

test

'); Html::addHtml($section, '

test

'); + Html::addHtml($section, '

test

'); $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); $this->assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr/w:spacing')); @@ -175,6 +176,10 @@ public function testParseLineHeight() $this->assertTrue($doc->elementExists('/w:document/w:body/w:p[4]/w:pPr/w:spacing')); $this->assertEquals(244.8, $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:spacing', 'w:line')); $this->assertEquals(LineSpacingRule::EXACT, $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:spacing', 'w:lineRule')); + + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p[5]/w:pPr/w:spacing')); + $this->assertEquals(Paragraph::LINE_HEIGHT, $doc->getElementAttribute('/w:document/w:body/w:p[5]/w:pPr/w:spacing', 'w:line')); + $this->assertEquals(LineSpacingRule::AUTO, $doc->getElementAttribute('/w:document/w:body/w:p[5]/w:pPr/w:spacing', 'w:lineRule')); } /**