Skip to content

Writer HTML: Support Default font color #2731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading