Skip to content

Commit 4448bda

Browse files
committed
Better normalization for width of borders
1 parent 70ad015 commit 4448bda

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -682,10 +682,14 @@ protected static function parseStyle($attribute, $styles)
682682
} else {
683683
$which = '';
684684
}
685-
// 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
686-
$size = strtolower(trim($matches[1]));
687-
// BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($size)
688-
$size = ($size == '1px') ? 1 : Converter::cssToTwip($size);
685+
// Note - border width normalization:
686+
// Width of border in Word is calculated differently than HTML borders, usually showing up too bold.
687+
// Smallest 1px (or 1pt) appears in Word like 2-3px/pt in HTML once converted to twips.
688+
// Therefore we need to normalize converted twip value to cca 1/2 of value.
689+
// This may be adjusted, if better ratio or formula found.
690+
// BC change: up to ver. 0.17.0 was $size converted to points - Converter::cssToPoint($size)
691+
$size = Converter::cssToTwip($matches[1]);
692+
$size = intval($size / 2);
689693
// valid variants may be e.g. borderSize, borderTopSize, borderLeftColor, etc ..
690694
$styles["border{$which}Size"] = $size; // twips
691695
$styles["border{$which}Color"] = trim($matches[2], '#');
@@ -884,10 +888,10 @@ protected static function mapAlignVertical($alignment)
884888
case 'baseline':
885889
return 'top';
886890
default:
887-
// @discuss - which one should apply:
888-
// - Word uses default vert. alignment: top
889-
// - all browsers use default vert. alignment: middle
890-
// Returning empty string means attribute wont be set so use Word default (top).
891+
// @discuss - which one should apply:
892+
// - Word uses default vert. alignment: top
893+
// - all browsers use default vert. alignment: middle
894+
// Returning empty string means attribute wont be set so use Word default (top).
891895
return '';
892896
}
893897
}

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ public function testParseHorizRule()
774774
$xpath = '/w:document/w:body/w:p[4]/w:pPr/w:pBdr/w:bottom';
775775
$this->assertTrue($doc->elementExists($xpath));
776776
$this->assertEquals('single', $doc->getElement($xpath)->getAttribute('w:val'));
777-
$this->assertEquals(5 * 15, $doc->getElement($xpath)->getAttribute('w:sz'));
777+
$this->assertEquals(intval(5 * 15 / 2), $doc->getElement($xpath)->getAttribute('w:sz'));
778778
$this->assertEquals('lightblue', $doc->getElement($xpath)->getAttribute('w:color'));
779779

780780
$xpath = '/w:document/w:body/w:p[4]/w:pPr/w:spacing';

0 commit comments

Comments
 (0)