From 13fc647d01915891abee0ce106f910223772cf9c Mon Sep 17 00:00:00 2001 From: Maxim Bulygin Date: Thu, 24 May 2018 17:03:35 +0300 Subject: [PATCH 1/3] html writes / setup table cell color --- src/PhpWord/Writer/HTML/Element/Table.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php index 844066f49e..866ce1bf0d 100644 --- a/src/PhpWord/Writer/HTML/Element/Table.php +++ b/src/PhpWord/Writer/HTML/Element/Table.php @@ -50,6 +50,14 @@ public function write() $rowCellCount = count($rowCells); for ($j = 0; $j < $rowCellCount; $j++) { $cellStyle = $rowCells[$j]->getStyle(); + $cellBgColor = $cellStyle->getBgColor(); + $cellFgColor = null; + if ($cellBgColor) { + $r = hexdec(substr($cellBgColor, 0, 2)); + $g = hexdec(substr($cellBgColor, 2, 2)); + $b = hexdec(substr($cellBgColor, 4, 2)); + $cellFgColor = (($r * 0.299 + $g * 0.587 + $b * 0.114) > 186) ? null : 'ffffff'; + } $cellColSpan = $cellStyle->getGridSpan(); $cellRowSpan = 1; $cellVMerge = $cellStyle->getVMerge(); @@ -73,7 +81,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) { From e40449e7c8691af4dbfefd43e7937acc46e8ce34 Mon Sep 17 00:00:00 2001 From: Maxim Bulygin Date: Thu, 24 May 2018 17:40:21 +0300 Subject: [PATCH 2/3] fix variable names --- src/PhpWord/Writer/HTML/Element/Table.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php index 866ce1bf0d..2f10df4159 100644 --- a/src/PhpWord/Writer/HTML/Element/Table.php +++ b/src/PhpWord/Writer/HTML/Element/Table.php @@ -53,10 +53,10 @@ public function write() $cellBgColor = $cellStyle->getBgColor(); $cellFgColor = null; if ($cellBgColor) { - $r = hexdec(substr($cellBgColor, 0, 2)); - $g = hexdec(substr($cellBgColor, 2, 2)); - $b = hexdec(substr($cellBgColor, 4, 2)); - $cellFgColor = (($r * 0.299 + $g * 0.587 + $b * 0.114) > 186) ? null : 'ffffff'; + $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; From 57ae7008b2fcd5941f2ddf487a13e9e2e616231d Mon Sep 17 00:00:00 2001 From: Maxim Bulygin Date: Fri, 1 Mar 2019 16:31:47 +0200 Subject: [PATCH 3/3] add test to auto invert text color --- tests/PhpWord/Writer/HTML/ElementTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/PhpWord/Writer/HTML/ElementTest.php b/tests/PhpWord/Writer/HTML/ElementTest.php index 90044b9293..6ef02754a4 100644 --- a/tests/PhpWord/Writer/HTML/ElementTest.php +++ b/tests/PhpWord/Writer/HTML/ElementTest.php @@ -85,10 +85,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'); @@ -99,6 +99,11 @@ public function testWriteColSpan() $this->assertTrue($xpath->query('/html/body/table/tr[1]/td')->length == 1); $this->assertEquals('2', $xpath->query('/html/body/table/tr/td[1]')->item(0)->attributes->getNamedItem('colspan')->textContent); $this->assertTrue($xpath->query('/html/body/table/tr[2]/td')->length == 2); + + $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')); } /**