Skip to content

Commit

Permalink
Merge pull request #1387 from SailorMax/html_writer_auto_invert_text_…
Browse files Browse the repository at this point in the history
…color

Html writer auto invert text color
  • Loading branch information
troosan authored Mar 5, 2019
2 parents 81af913 + 8931ab1 commit 2d2fe52
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/PhpWord/Writer/HTML/Element/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public function write()
$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();
Expand All @@ -74,7 +82,9 @@ public function write()
$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) {
Expand Down
9 changes: 7 additions & 2 deletions tests/PhpWord/Writer/HTML/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public function testWriteColSpan()
$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');
Expand All @@ -100,6 +100,11 @@ public function testWriteColSpan()
$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'));
}

/**
Expand Down

1 comment on commit 2d2fe52

@shi-yc
Copy link

@shi-yc shi-yc commented on 2d2fe52 Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to use the table's location attribute, I need this function for my current project, please help me to answer, thank you

Please sign in to comment.