Skip to content

Commit

Permalink
Add default font color for Word (.docx) (#2700)
Browse files Browse the repository at this point in the history
* Add default font color for Word (.docx)

Adds the abillity to add a default font color for generated .docx.

* fix format

* Update 1.4.0.md

Add default font color

* Update introduction.md

Add documentation default font color

* Update introduction.md

Correct default value

* add tests for SetGetDefaultFontColor

* debug test

* add defaultFontColor to FontTest

* Update src/PhpWord/PhpWord.php

As suggested

Co-authored-by: Progi1984 <[email protected]>

* Update src/PhpWord/PhpWord.php

As suggested

Co-authored-by: Progi1984 <[email protected]>

* Update 1.4.0.md

Add default font color

* Update introduction.md

Add documentation default font color

* Update introduction.md

Correct default value

* add tests for SetGetDefaultFontColor

* debug test

* add defaultFontColor to FontTest

* Update src/PhpWord/PhpWord.php

As suggested

Co-authored-by: Progi1984 <[email protected]>

* Update src/PhpWord/PhpWord.php

As suggested

Co-authored-by: Progi1984 <[email protected]>

* fix format

* add test for default color

* clean up

* cs fixer

---------

Co-authored-by: MichaelFrey <[email protected]>
Co-authored-by: Progi1984 <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2025
1 parent 10ae499 commit f1470d0
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 4 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 @@ -11,6 +11,7 @@
- Writer Word2007: Support for padding in Table Cell by [@Azamat8405](https://github.com/Azamat8405) in [#2697](https://github.com/PHPOffice/PHPWord/pull/2697)
- 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)

### Bug fixes

Expand Down
5 changes: 3 additions & 2 deletions docs/usage/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ You can alter the default paper by using the following function:

### Default font

By default, every text appears in Arial 10 point. You can alter the
default font by using the following two functions:
By default, every text appears in Arial 10 point in the color black (000000).
You can alter the default font by using the following functions:

``` php
<?php

$phpWord->setDefaultFontName('Times New Roman');
$phpWord->setDefaultFontColor('FF0000');
$phpWord->setDefaultFontSize(12);
```

Expand Down
1 change: 1 addition & 0 deletions phpword.ini.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ outputEscapingEnabled = false

defaultFontName = Arial
defaultFontSize = 10
defaultFontColor = 000000

[Paper]

Expand Down
18 changes: 17 additions & 1 deletion src/PhpWord/PhpWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function getDefaultAsianFontName(): string
}

/**
* Set default font name.
* Set default asian font name.
*
* @param string $fontName
*/
Expand All @@ -275,6 +275,22 @@ public function setDefaultAsianFontName($fontName): void
Settings::setDefaultAsianFontName($fontName);
}

/**
* Set default font color.
*/
public function setDefaultFontColor(string $fontColor): void
{
Settings::setDefaultFontColor($fontColor);
}

/**
* Get default font color.
*/
public function getDefaultFontColor(): string
{
return Settings::getDefaultFontColor();
}

/**
* Get default font size.
*
Expand Down
3 changes: 3 additions & 0 deletions src/PhpWord/Reader/Word2007/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function read(PhpWord $phpWord): void
if (array_key_exists('size', $fontDefaultStyle)) {
$phpWord->setDefaultFontSize($fontDefaultStyle['size']);
}
if (array_key_exists('color', $fontDefaultStyle)) {
$phpWord->setDefaultFontColor($fontDefaultStyle['color']);
}
if (array_key_exists('lang', $fontDefaultStyle)) {
$phpWord->getSettings()->setThemeFontLang(new Language($fontDefaultStyle['lang']));
}
Expand Down
31 changes: 30 additions & 1 deletion src/PhpWord/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,19 @@ class Settings
private static $defaultFontName = self::DEFAULT_FONT_NAME;

/**
* Default font name.
* Default asian font name.
*
* @var string
*/
private static $defaultAsianFontName = self::DEFAULT_FONT_NAME;

/**
* Default font color.
*
* @var string
*/
private static $defaultFontColor = self::DEFAULT_FONT_COLOR;

/**
* Default font size.
*
Expand Down Expand Up @@ -395,6 +402,28 @@ public static function setDefaultAsianFontName(string $value): bool
return false;
}

/**
* Get default font color.
*/
public static function getDefaultFontColor(): string
{
return self::$defaultFontColor;
}

/**
* Set default font color.
*/
public static function setDefaultFontColor(string $value): bool
{
if (trim($value) !== '') {
self::$defaultFontColor = $value;

return true;
}

return false;
}

/**
* Get default font size.
*
Expand Down
4 changes: 4 additions & 0 deletions src/PhpWord/Writer/Word2007/Part/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void
$fontName = $phpWord->getDefaultFontName();
$asianFontName = $phpWord->getDefaultAsianFontName();
$fontSize = $phpWord->getDefaultFontSize();
$fontColor = $phpWord->getDefaultFontColor();
$language = $phpWord->getSettings()->getThemeFontLang();
$latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin();

Expand All @@ -99,6 +100,9 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void
$xmlWriter->writeAttribute('w:eastAsia', $asianFontName);
$xmlWriter->writeAttribute('w:cs', $fontName);
$xmlWriter->endElement(); // w:rFonts
$xmlWriter->startElement('w:color');
$xmlWriter->writeAttribute('w:val', $fontColor);
$xmlWriter->endElement();
$xmlWriter->startElement('w:sz');
$xmlWriter->writeAttribute('w:val', $fontSize * 2);
$xmlWriter->endElement(); // w:sz
Expand Down
12 changes: 12 additions & 0 deletions tests/PhpWordTests/PhpWordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ public function testSetGetDefaultAsianFontName(): void
self::assertEquals($fontName, $phpWord->getDefaultAsianFontName());
}

/**
* Test set/get default font color.
*/
public function testSetGetDefaultFontColor(): void
{
$phpWord = new PhpWord();
$fontColor = 'FF0000';
self::assertEquals(Settings::DEFAULT_FONT_COLOR, $phpWord->getDefaultFontColor());
$phpWord->setDefaultFontColor($fontColor);
self::assertEquals($fontColor, $phpWord->getDefaultFontColor());
}

/**
* Test set default paragraph style.
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/PhpWordTests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class SettingsTest extends TestCase
{
private $compatibility;

/** @var string */
private $defaultFontColor;

private $defaultFontSize;

private $defaultFontName;
Expand Down Expand Up @@ -60,6 +63,7 @@ class SettingsTest extends TestCase
protected function setUp(): void
{
$this->compatibility = Settings::hasCompatibility();
$this->defaultFontColor = Settings::getDefaultFontColor();
$this->defaultFontSize = Settings::getDefaultFontSize();
$this->defaultFontName = Settings::getDefaultFontName();
$this->defaultPaper = Settings::getDefaultPaper();
Expand All @@ -76,6 +80,7 @@ protected function setUp(): void
protected function tearDown(): void
{
Settings::setCompatibility($this->compatibility);
Settings::setDefaultFontColor($this->defaultFontColor);
Settings::setDefaultFontSize($this->defaultFontSize);
Settings::setDefaultFontName($this->defaultFontName);
Settings::setDefaultPaper($this->defaultPaper);
Expand Down Expand Up @@ -251,6 +256,20 @@ public function testSetGetDefaultFontSize(): void
self::assertEquals(12.5, Settings::getDefaultFontSize());
}

/**
* Test set/get default font color.
*/
public function testSetGetDefaultFontColor(): void
{
self::assertEquals(Settings::DEFAULT_FONT_COLOR, Settings::getDefaultFontColor());
self::assertFalse(Settings::setDefaultFontColor(' '));
self::assertEquals(Settings::DEFAULT_FONT_COLOR, Settings::getDefaultFontColor());
self::assertTrue(Settings::setDefaultFontColor('FF0000'));
self::assertEquals('FF0000', Settings::getDefaultFontColor());
self::assertFalse(Settings::setDefaultFontColor(' '));
self::assertEquals('FF0000', Settings::getDefaultFontColor());
}

/**
* Test set/get default paper.
*/
Expand Down Expand Up @@ -286,6 +305,7 @@ public function testLoadConfig(): void
'pdfRendererPath' => '',
'defaultFontName' => 'Arial',
'defaultFontSize' => 10,
'defaultFontColor' => '000000',
'outputEscapingEnabled' => false,
'defaultPaper' => 'A4',
];
Expand Down
5 changes: 5 additions & 0 deletions tests/PhpWordTests/Writer/HTML/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ class FontTest extends \PHPUnit\Framework\TestCase
/** @var float|int */
private $defaultFontSize;

/** @var string */
private $defaultFontColor;

/**
* Executed before each method of the class.
*/
protected function setUp(): void
{
$this->defaultFontName = Settings::getDefaultFontName();
$this->defaultFontSize = Settings::getDefaultFontSize();
$this->defaultFontColor = Settings::getDefaultFontColor();
}

/**
Expand All @@ -50,6 +54,7 @@ protected function tearDown(): void
{
Settings::setDefaultFontName($this->defaultFontName);
Settings::setDefaultFontSize($this->defaultFontSize);
Settings::setDefaultFontColor($this->defaultFontColor);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/Part/StylesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,42 @@ public function testFontStyleBasedOnOtherFontStyle(): void
$element = $doc->getElement($path, $file);
self::assertEquals('Generation', $element->getAttribute('w:val'));
}

/**
* Test default font color.
*/
public function testDefaultDefaultFontColor(): void
{
$phpWord = new PhpWord();

$doc = TestHelperDOCX::getDocument($phpWord);

$file = 'word/styles.xml';

$path = '/w:styles/w:docDefaults/w:rPrDefault/w:rPr/w:color';
self::assertTrue($doc->elementExists($path, $file));
$element = $doc->getElement($path, $file);

self::assertEquals('000000', $element->getAttribute('w:val'));
}

/**
* Test default font color.
*/
public function testDefaultFontColor(): void
{
$phpWord = new PhpWord();
$defaultFontColor = '00FF00';
$phpWord->setDefaultFontColor($defaultFontColor);

$doc = TestHelperDOCX::getDocument($phpWord);

$file = 'word/styles.xml';

$path = '/w:styles/w:docDefaults/w:rPrDefault/w:rPr/w:color';
self::assertTrue($doc->elementExists($path, $file));
$element = $doc->getElement($path, $file);

self::assertEquals($defaultFontColor, $element->getAttribute('w:val'));
}
}

0 comments on commit f1470d0

Please sign in to comment.