fix converting margin to incorrect unit (points instead of twips)

fix image alignment on float - relative to inner margin instead of page margin
This commit is contained in:
lubosdz 2020-07-11 23:03:51 +02:00
parent 3066d47003
commit 889f4e3381
2 changed files with 13 additions and 10 deletions

View File

@ -633,11 +633,18 @@ class Html
}
$styles['italic'] = $tValue;
break;
case 'margin':
$cValue = Converter::cssToTwip($cValue);
$styles['spaceBefore'] = $cValue;
$styles['spaceAfter'] = $cValue;
break;
case 'margin-top':
$styles['spaceBefore'] = Converter::cssToPoint($cValue);
// BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($cValue)
$styles['spaceBefore'] = Converter::cssToTwip($cValue);
break;
case 'margin-bottom':
$styles['spaceAfter'] = Converter::cssToPoint($cValue);
// BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($cValue)
$styles['spaceAfter'] = Converter::cssToTwip($cValue);
break;
case 'border-color':
self::mapBorderColor($styles, $cValue);
@ -676,7 +683,7 @@ class Html
}
// 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 Converter was incorrectly converting to points - Converter::cssToPoint($matches[1])
// BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($size)
$size = ($size == '1px') ? 1 : Converter::cssToTwip($size);
// valid variants may be e.g. borderSize, borderTopSize, borderLeftColor, etc ..
$styles["border{$which}Size"] = $size; // twips
@ -732,14 +739,14 @@ class Html
case 'float':
if (trim($v) == 'right') {
$style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_RIGHT;
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE;
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_MARGIN; // inner section area
$style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
$style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
$style['overlap'] = true;
}
if (trim($v) == 'left') {
$style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_LEFT;
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE;
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_MARGIN; // inner section area
$style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
$style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
$style['overlap'] = true;

View File

@ -779,7 +779,7 @@ HTML;
$xpath = '/w:document/w:body/w:p[4]/w:pPr/w:spacing';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals(22.5, $doc->getElement($xpath)->getAttribute('w:before'));
$this->assertEquals(450, $doc->getElement($xpath)->getAttribute('w:before'));
$this->assertEquals(0, $doc->getElement($xpath)->getAttribute('w:after'));
$this->assertEquals(240, $doc->getElement($xpath)->getAttribute('w:line'));
}
@ -869,10 +869,6 @@ HTML;
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
// uncomment to see results
file_put_contents('./table_src.html', $html);
file_put_contents('./table_result_'.time().'.docx', file_get_contents( TestHelperDOCX::getFile() ) );
$xpath = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:vAlign';
$this->assertFalse($doc->elementExists($xpath));