Merge pull request #1387 from SailorMax/html_writer_auto_invert_text_color
Html writer auto invert text color
This commit is contained in:
commit
2d2fe52b7a
|
|
@ -51,6 +51,14 @@ class Table extends AbstractElement
|
|||
$rowCellCount = count($rowCells);
|
||||
for ($j = 0; $j < $rowCellCount; $j++) {
|
||||
$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();
|
||||
$cellRowSpan = 1;
|
||||
$cellVMerge = $cellStyle->getVMerge();
|
||||
|
|
@ -74,7 +82,9 @@ class Table extends AbstractElement
|
|||
$cellTag = $tblHeader ? 'th' : 'td';
|
||||
$cellColSpanAttr = (is_numeric($cellColSpan) && ($cellColSpan > 1) ? " colspan=\"{$cellColSpan}\"" : '');
|
||||
$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]);
|
||||
$content .= $writer->write();
|
||||
if ($cellRowSpan > 1) {
|
||||
|
|
|
|||
|
|
@ -86,10 +86,10 @@ class ElementTest extends \PHPUnit\Framework\TestCase
|
|||
$section = $phpWord->addSection();
|
||||
$table = $section->addTable();
|
||||
$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');
|
||||
$row2 = $table->addRow();
|
||||
$cell21 = $row2->addCell(500);
|
||||
$cell21 = $row2->addCell(500, array('bgColor' => 'ffffff'));
|
||||
$cell21->addText('first cell');
|
||||
$cell22 = $row2->addCell(500);
|
||||
$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('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('#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'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue