Better normalization for width of borders

This commit is contained in:
lubosdz 2020-07-14 01:31:16 +02:00
parent 70ad01550b
commit 4448bda721
2 changed files with 13 additions and 9 deletions

View File

@ -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], '#');

View File

@ -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';