Skip to content

Commit a4468f2

Browse files
authored
Writer HTML: Support Default font color (#2731)
* Writer HTML: Support Default font color * the reader should already support default font color
1 parent f1470d0 commit a4468f2

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

docs/changes/1.x/1.4.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#2660](https://github.com/PHPOffice/PHPWord/pull/2660)
1313
- Autoload : Allow to use PHPWord without Composer fixing [#2543](https://github.com/PHPOffice/PHPWord/issues/2543), [#2552](https://github.com/PHPOffice/PHPWord/issues/2552), [#2716](https://github.com/PHPOffice/PHPWord/issues/2716), [#2717](https://github.com/PHPOffice/PHPWord/issues/2717) in [#2722](https://github.com/PHPOffice/PHPWord/pull/2722)
1414
- Add Default font color for Word by [@Collie-IT](https://github.com/Collie-IT) in [#2700](https://github.com/PHPOffice/PHPWord/pull/2700)
15+
- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey)
1516

1617
### Bug fixes
1718

src/PhpWord/Writer/HTML/Part/Body.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use PhpOffice\PhpWord\Writer\PDF\TCPDF;
2424

2525
/**
26-
* RTF body part writer.
26+
* HTML body part writer.
2727
*
2828
* @since 0.11.0
2929
*/

src/PhpWord/Writer/HTML/Part/Head.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
use PhpOffice\PhpWord\Writer\HTML\Style\Table as TableStyleWriter;
3131

3232
/**
33-
* RTF head part writer.
33+
* HTML head part writer.
3434
*
3535
* @since 0.11.0
3636
*/
@@ -85,11 +85,12 @@ public function write()
8585
private function writeStyles(): string
8686
{
8787
$css = '<style>' . PHP_EOL;
88-
88+
$defaultFontColor = Settings::getDefaultFontColor();
8989
// Default styles
9090
$astarray = [
9191
'font-family' => $this->getFontFamily(Settings::getDefaultFontName(), $this->getParentWriter()->getDefaultGenericFont()),
9292
'font-size' => Settings::getDefaultFontSize() . 'pt',
93+
'color' => "#{$defaultFontColor}",
9394
];
9495
// Mpdf sometimes needs separate tag for body; doesn't harm others.
9596
$bodyarray = $astarray;

tests/PhpWordTests/Writer/HTML/FontTest.php

+35-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,37 @@ protected function tearDown(): void
5757
Settings::setDefaultFontColor($this->defaultFontColor);
5858
}
5959

60+
public function testDefaultDefaults(): void
61+
{
62+
$phpWord = new PhpWord();
63+
64+
$dom = Helper::getAsHTML($phpWord);
65+
$xpath = new DOMXPath($dom);
66+
$style = Helper::getTextContent($xpath, '/html/head/style[1]');
67+
68+
$prg = preg_match('/body {(.*?)}/', $style, $matches);
69+
self::assertNotEmpty($matches);
70+
self::assertNotFalse($prg);
71+
self::assertEquals('body {font-family: \'Arial\'; font-size: 12pt; color: #000000;}', $matches[0]);
72+
}
73+
74+
public function testSettingDefaultFontColor(): void
75+
{
76+
$phpWord = new PhpWord();
77+
78+
$defaultFontColor = '00FF00';
79+
$phpWord->setDefaultFontColor($defaultFontColor);
80+
81+
$dom = Helper::getAsHTML($phpWord);
82+
$xpath = new DOMXPath($dom);
83+
$style = Helper::getTextContent($xpath, '/html/head/style[1]');
84+
85+
$prg = preg_match('/body {(.*?)}/', $style, $matches);
86+
self::assertNotEmpty($matches);
87+
self::assertNotFalse($prg);
88+
self::assertEquals('body {font-family: \'Arial\'; font-size: 12pt; color: #00FF00;}', $matches[0]);
89+
}
90+
6091
/**
6192
* Tests font names - without generics.
6293
*/
@@ -93,7 +124,7 @@ public function testFontNames1(): void
93124
$prg = preg_match('/^[*][^\\r\\n]*/m', $style, $matches);
94125
self::assertNotEmpty($matches);
95126
self::assertNotFalse($prg);
96-
self::assertEquals('* {font-family: \'Courier New\'; font-size: 12pt;}', $matches[0]);
127+
self::assertEquals('* {font-family: \'Courier New\'; font-size: 12pt; color: #000000;}', $matches[0]);
97128
$prg = preg_match('/^[.]style1[^\\r\\n]*/m', $style, $matches);
98129
self::assertNotEmpty($matches);
99130
self::assertNotFalse($prg);
@@ -149,7 +180,7 @@ public function testFontNames2(): void
149180
$prg = preg_match('/^[*][^\\r\\n]*/m', $style, $matches);
150181
self::assertNotEmpty($matches);
151182
self::assertNotFalse($prg);
152-
self::assertEquals('* {font-family: \'Courier New\'; font-size: 12pt;}', $matches[0]);
183+
self::assertEquals('* {font-family: \'Courier New\'; font-size: 12pt; color: #000000;}', $matches[0]);
153184
$prg = preg_match('/^[.]style1[^\\r\\n]*/m', $style, $matches);
154185
self::assertNotEmpty($matches);
155186
self::assertNotFalse($prg);
@@ -201,7 +232,7 @@ public function testFontNames3(): void
201232
$prg = preg_match('/^[*][^\\r\\n]*/m', $style, $matches);
202233
self::assertNotEmpty($matches);
203234
self::assertNotFalse($prg);
204-
self::assertEquals('* {font-family: \'Courier New\', monospace; font-size: 12pt;}', $matches[0]);
235+
self::assertEquals('* {font-family: \'Courier New\', monospace; font-size: 12pt; color: #000000;}', $matches[0]);
205236
$prg = preg_match('/^[.]style1[^\\r\\n]*/m', $style, $matches);
206237
self::assertNotEmpty($matches);
207238
self::assertNotFalse($prg);
@@ -244,7 +275,7 @@ public function testWhiteSpace(): void
244275

245276
$style = Helper::getTextContent($xpath, '/html/head/style');
246277
self::assertNotFalse(preg_match('/^[*][^\\r\\n]*/m', $style, $matches));
247-
self::assertEquals('* {font-family: \'Arial\'; font-size: 12pt; white-space: pre-wrap;}', $matches[0]);
278+
self::assertEquals('* {font-family: \'Arial\'; font-size: 12pt; color: #000000; white-space: pre-wrap;}', $matches[0]);
248279
$prg = preg_match('/^[.]style1[^\\r\\n]*/m', $style, $matches);
249280
self::assertNotEmpty($matches);
250281
self::assertNotFalse($prg);

0 commit comments

Comments
 (0)