From 4448bda7215c7389bec955988004c4d13a5ac482 Mon Sep 17 00:00:00 2001 From: lubosdz Date: Tue, 14 Jul 2020 01:31:16 +0200 Subject: [PATCH] Better normalization for width of borders --- src/PhpWord/Shared/Html.php | 20 ++++++++++++-------- tests/PhpWord/Shared/HtmlTest.php | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index edd418bf..c8a7fa69 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -682,10 +682,14 @@ class Html } else { $which = ''; } - // normalization: in HTML 1px means tinest possible line width, so we cannot convert 1px -> 15 twips, coz line'd be bold, we use smallest twip instead - $size = strtolower(trim($matches[1])); - // BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($size) - $size = ($size == '1px') ? 1 : Converter::cssToTwip($size); + // Note - border width normalization: + // Width of border in Word is calculated differently than HTML borders, usually showing up too bold. + // Smallest 1px (or 1pt) appears in Word like 2-3px/pt in HTML once converted to twips. + // Therefore we need to normalize converted twip value to cca 1/2 of value. + // This may be adjusted, if better ratio or formula found. + // BC change: up to ver. 0.17.0 was $size converted to points - Converter::cssToPoint($size) + $size = Converter::cssToTwip($matches[1]); + $size = intval($size / 2); // valid variants may be e.g. borderSize, borderTopSize, borderLeftColor, etc .. $styles["border{$which}Size"] = $size; // twips $styles["border{$which}Color"] = trim($matches[2], '#'); @@ -884,10 +888,10 @@ class Html case 'baseline': return 'top'; default: - // @discuss - which one should apply: - // - Word uses default vert. alignment: top - // - all browsers use default vert. alignment: middle - // Returning empty string means attribute wont be set so use Word default (top). + // @discuss - which one should apply: + // - Word uses default vert. alignment: top + // - all browsers use default vert. alignment: middle + // Returning empty string means attribute wont be set so use Word default (top). return ''; } } diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index 010d1918..93df9337 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -774,7 +774,7 @@ HTML; $xpath = '/w:document/w:body/w:p[4]/w:pPr/w:pBdr/w:bottom'; $this->assertTrue($doc->elementExists($xpath)); $this->assertEquals('single', $doc->getElement($xpath)->getAttribute('w:val')); - $this->assertEquals(5 * 15, $doc->getElement($xpath)->getAttribute('w:sz')); + $this->assertEquals(intval(5 * 15 / 2), $doc->getElement($xpath)->getAttribute('w:sz')); $this->assertEquals('lightblue', $doc->getElement($xpath)->getAttribute('w:color')); $xpath = '/w:document/w:body/w:p[4]/w:pPr/w:spacing';