Skip to content

Commit

Permalink
Writer HTML: Support Default font color (PHPOffice#2731)
Browse files Browse the repository at this point in the history
* Writer HTML: Support Default font color

* the reader should already support default font color
  • Loading branch information
MichaelPFrey authored Jan 28, 2025
1 parent f1470d0 commit a4468f2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#2660](https://github.com/PHPOffice/PHPWord/pull/2660)
- 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)
- Add Default font color for Word by [@Collie-IT](https://github.com/Collie-IT) in [#2700](https://github.com/PHPOffice/PHPWord/pull/2700)
- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey)

### Bug fixes

Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/HTML/Part/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use PhpOffice\PhpWord\Writer\PDF\TCPDF;

/**
* RTF body part writer.
* HTML body part writer.
*
* @since 0.11.0
*/
Expand Down
5 changes: 3 additions & 2 deletions src/PhpWord/Writer/HTML/Part/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use PhpOffice\PhpWord\Writer\HTML\Style\Table as TableStyleWriter;

/**
* RTF head part writer.
* HTML head part writer.
*
* @since 0.11.0
*/
Expand Down Expand Up @@ -85,11 +85,12 @@ public function write()
private function writeStyles(): string
{
$css = '<style>' . PHP_EOL;

$defaultFontColor = Settings::getDefaultFontColor();
// Default styles
$astarray = [
'font-family' => $this->getFontFamily(Settings::getDefaultFontName(), $this->getParentWriter()->getDefaultGenericFont()),
'font-size' => Settings::getDefaultFontSize() . 'pt',
'color' => "#{$defaultFontColor}",
];
// Mpdf sometimes needs separate tag for body; doesn't harm others.
$bodyarray = $astarray;
Expand Down
39 changes: 35 additions & 4 deletions tests/PhpWordTests/Writer/HTML/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ protected function tearDown(): void
Settings::setDefaultFontColor($this->defaultFontColor);
}

public function testDefaultDefaults(): void
{
$phpWord = new PhpWord();

$dom = Helper::getAsHTML($phpWord);
$xpath = new DOMXPath($dom);
$style = Helper::getTextContent($xpath, '/html/head/style[1]');

$prg = preg_match('/body {(.*?)}/', $style, $matches);
self::assertNotEmpty($matches);
self::assertNotFalse($prg);
self::assertEquals('body {font-family: \'Arial\'; font-size: 12pt; color: #000000;}', $matches[0]);
}

public function testSettingDefaultFontColor(): void
{
$phpWord = new PhpWord();

$defaultFontColor = '00FF00';
$phpWord->setDefaultFontColor($defaultFontColor);

$dom = Helper::getAsHTML($phpWord);
$xpath = new DOMXPath($dom);
$style = Helper::getTextContent($xpath, '/html/head/style[1]');

$prg = preg_match('/body {(.*?)}/', $style, $matches);
self::assertNotEmpty($matches);
self::assertNotFalse($prg);
self::assertEquals('body {font-family: \'Arial\'; font-size: 12pt; color: #00FF00;}', $matches[0]);
}

/**
* Tests font names - without generics.
*/
Expand Down Expand Up @@ -93,7 +124,7 @@ public function testFontNames1(): void
$prg = preg_match('/^[*][^\\r\\n]*/m', $style, $matches);
self::assertNotEmpty($matches);
self::assertNotFalse($prg);
self::assertEquals('* {font-family: \'Courier New\'; font-size: 12pt;}', $matches[0]);
self::assertEquals('* {font-family: \'Courier New\'; font-size: 12pt; color: #000000;}', $matches[0]);
$prg = preg_match('/^[.]style1[^\\r\\n]*/m', $style, $matches);
self::assertNotEmpty($matches);
self::assertNotFalse($prg);
Expand Down Expand Up @@ -149,7 +180,7 @@ public function testFontNames2(): void
$prg = preg_match('/^[*][^\\r\\n]*/m', $style, $matches);
self::assertNotEmpty($matches);
self::assertNotFalse($prg);
self::assertEquals('* {font-family: \'Courier New\'; font-size: 12pt;}', $matches[0]);
self::assertEquals('* {font-family: \'Courier New\'; font-size: 12pt; color: #000000;}', $matches[0]);
$prg = preg_match('/^[.]style1[^\\r\\n]*/m', $style, $matches);
self::assertNotEmpty($matches);
self::assertNotFalse($prg);
Expand Down Expand Up @@ -201,7 +232,7 @@ public function testFontNames3(): void
$prg = preg_match('/^[*][^\\r\\n]*/m', $style, $matches);
self::assertNotEmpty($matches);
self::assertNotFalse($prg);
self::assertEquals('* {font-family: \'Courier New\', monospace; font-size: 12pt;}', $matches[0]);
self::assertEquals('* {font-family: \'Courier New\', monospace; font-size: 12pt; color: #000000;}', $matches[0]);
$prg = preg_match('/^[.]style1[^\\r\\n]*/m', $style, $matches);
self::assertNotEmpty($matches);
self::assertNotFalse($prg);
Expand Down Expand Up @@ -244,7 +275,7 @@ public function testWhiteSpace(): void

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

0 comments on commit a4468f2

Please sign in to comment.