Merge pull request #1387 from SailorMax/html_writer_auto_invert_text_color

Html writer auto invert text color
This commit is contained in:
troosan 2019-03-05 23:05:37 +01:00 committed by GitHub
commit 2d2fe52b7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -51,6 +51,14 @@ class Table extends AbstractElement
$rowCellCount = count($rowCells); $rowCellCount = count($rowCells);
for ($j = 0; $j < $rowCellCount; $j++) { for ($j = 0; $j < $rowCellCount; $j++) {
$cellStyle = $rowCells[$j]->getStyle(); $cellStyle = $rowCells[$j]->getStyle();
$cellBgColor = $cellStyle->getBgColor();
$cellFgColor = null;
if ($cellBgColor) {
$red = hexdec(substr($cellBgColor, 0, 2));
$green = hexdec(substr($cellBgColor, 2, 2));
$blue = hexdec(substr($cellBgColor, 4, 2));
$cellFgColor = (($red * 0.299 + $green * 0.587 + $blue * 0.114) > 186) ? null : 'ffffff';
}
$cellColSpan = $cellStyle->getGridSpan(); $cellColSpan = $cellStyle->getGridSpan();
$cellRowSpan = 1; $cellRowSpan = 1;
$cellVMerge = $cellStyle->getVMerge(); $cellVMerge = $cellStyle->getVMerge();
@ -74,7 +82,9 @@ class Table extends AbstractElement
$cellTag = $tblHeader ? 'th' : 'td'; $cellTag = $tblHeader ? 'th' : 'td';
$cellColSpanAttr = (is_numeric($cellColSpan) && ($cellColSpan > 1) ? " colspan=\"{$cellColSpan}\"" : ''); $cellColSpanAttr = (is_numeric($cellColSpan) && ($cellColSpan > 1) ? " colspan=\"{$cellColSpan}\"" : '');
$cellRowSpanAttr = ($cellRowSpan > 1 ? " rowspan=\"{$cellRowSpan}\"" : ''); $cellRowSpanAttr = ($cellRowSpan > 1 ? " rowspan=\"{$cellRowSpan}\"" : '');
$content .= "<{$cellTag}{$cellColSpanAttr}{$cellRowSpanAttr}>" . PHP_EOL; $cellBgColorAttr = (is_null($cellBgColor) ? '' : " bgcolor=\"#{$cellBgColor}\"");
$cellFgColorAttr = (is_null($cellFgColor) ? '' : " color=\"#{$cellFgColor}\"");
$content .= "<{$cellTag}{$cellColSpanAttr}{$cellRowSpanAttr}{$cellBgColorAttr}{$cellFgColorAttr}>" . PHP_EOL;
$writer = new Container($this->parentWriter, $rowCells[$j]); $writer = new Container($this->parentWriter, $rowCells[$j]);
$content .= $writer->write(); $content .= $writer->write();
if ($cellRowSpan > 1) { if ($cellRowSpan > 1) {

View File

@ -86,10 +86,10 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$section = $phpWord->addSection(); $section = $phpWord->addSection();
$table = $section->addTable(); $table = $section->addTable();
$row1 = $table->addRow(); $row1 = $table->addRow();
$cell11 = $row1->addCell(1000, array('gridSpan' => 2)); $cell11 = $row1->addCell(1000, array('gridSpan' => 2, 'bgColor' => '6086B8'));
$cell11->addText('cell spanning 2 bellow'); $cell11->addText('cell spanning 2 bellow');
$row2 = $table->addRow(); $row2 = $table->addRow();
$cell21 = $row2->addCell(500); $cell21 = $row2->addCell(500, array('bgColor' => 'ffffff'));
$cell21->addText('first cell'); $cell21->addText('first cell');
$cell22 = $row2->addCell(500); $cell22 = $row2->addCell(500);
$cell22->addText('second cell'); $cell22->addText('second cell');
@ -100,6 +100,11 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(1, $xpath->query('/html/body/table/tr[1]/td')->length); $this->assertEquals(1, $xpath->query('/html/body/table/tr[1]/td')->length);
$this->assertEquals('2', $xpath->query('/html/body/table/tr/td[1]')->item(0)->attributes->getNamedItem('colspan')->textContent); $this->assertEquals('2', $xpath->query('/html/body/table/tr/td[1]')->item(0)->attributes->getNamedItem('colspan')->textContent);
$this->assertEquals(2, $xpath->query('/html/body/table/tr[2]/td')->length); $this->assertEquals(2, $xpath->query('/html/body/table/tr[2]/td')->length);
$this->assertEquals('#6086B8', $xpath->query('/html/body/table/tr[1]/td')->item(0)->attributes->getNamedItem('bgcolor')->textContent);
$this->assertEquals('#ffffff', $xpath->query('/html/body/table/tr[1]/td')->item(0)->attributes->getNamedItem('color')->textContent);
$this->assertEquals('#ffffff', $xpath->query('/html/body/table/tr[2]/td')->item(0)->attributes->getNamedItem('bgcolor')->textContent);
$this->assertNull($xpath->query('/html/body/table/tr[2]/td')->item(0)->attributes->getNamedItem('color'));
} }
/** /**