From f1470d005e583aecbe064e8286c1312857792699 Mon Sep 17 00:00:00 2001 From: Collie-IT <40590185+Collie-IT@users.noreply.github.com> Date: Sun, 26 Jan 2025 16:51:52 +0100 Subject: [PATCH] Add default font color for Word (.docx) (#2700) * 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 * Update src/PhpWord/PhpWord.php As suggested Co-authored-by: Progi1984 * 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 * Update src/PhpWord/PhpWord.php As suggested Co-authored-by: Progi1984 * fix format * add test for default color * clean up * cs fixer --------- Co-authored-by: MichaelFrey Co-authored-by: Progi1984 --- docs/changes/1.x/1.4.0.md | 1 + docs/usage/introduction.md | 5 ++- phpword.ini.dist | 1 + src/PhpWord/PhpWord.php | 18 ++++++++- src/PhpWord/Reader/Word2007/Styles.php | 3 ++ src/PhpWord/Settings.php | 31 ++++++++++++++- src/PhpWord/Writer/Word2007/Part/Styles.php | 4 ++ tests/PhpWordTests/PhpWordTest.php | 12 ++++++ tests/PhpWordTests/SettingsTest.php | 20 ++++++++++ tests/PhpWordTests/Writer/HTML/FontTest.php | 5 +++ .../Writer/Word2007/Part/StylesTest.php | 38 +++++++++++++++++++ 11 files changed, 134 insertions(+), 4 deletions(-) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 32f5ba47c9..cb12a3f68a 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -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 diff --git a/docs/usage/introduction.md b/docs/usage/introduction.md index 6aa42aaae0..19d6aff51f 100644 --- a/docs/usage/introduction.md +++ b/docs/usage/introduction.md @@ -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 setDefaultFontName('Times New Roman'); +$phpWord->setDefaultFontColor('FF0000'); $phpWord->setDefaultFontSize(12); ``` diff --git a/phpword.ini.dist b/phpword.ini.dist index f3f66dbe2e..21d3b50609 100644 --- a/phpword.ini.dist +++ b/phpword.ini.dist @@ -14,6 +14,7 @@ outputEscapingEnabled = false defaultFontName = Arial defaultFontSize = 10 +defaultFontColor = 000000 [Paper] diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index aa62a76f0d..c3200fe857 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -266,7 +266,7 @@ public function getDefaultAsianFontName(): string } /** - * Set default font name. + * Set default asian font name. * * @param string $fontName */ @@ -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. * diff --git a/src/PhpWord/Reader/Word2007/Styles.php b/src/PhpWord/Reader/Word2007/Styles.php index b327ff3346..d0777c3026 100644 --- a/src/PhpWord/Reader/Word2007/Styles.php +++ b/src/PhpWord/Reader/Word2007/Styles.php @@ -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'])); } diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 0cbfed7988..16f49166fa 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -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. * @@ -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. * diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index c57c1e58be..edf0314cc2 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -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(); @@ -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 diff --git a/tests/PhpWordTests/PhpWordTest.php b/tests/PhpWordTests/PhpWordTest.php index d194f4895d..76919bbdf3 100644 --- a/tests/PhpWordTests/PhpWordTest.php +++ b/tests/PhpWordTests/PhpWordTest.php @@ -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. */ diff --git a/tests/PhpWordTests/SettingsTest.php b/tests/PhpWordTests/SettingsTest.php index cf8e12b205..13b32e1293 100644 --- a/tests/PhpWordTests/SettingsTest.php +++ b/tests/PhpWordTests/SettingsTest.php @@ -31,6 +31,9 @@ class SettingsTest extends TestCase { private $compatibility; + /** @var string */ + private $defaultFontColor; + private $defaultFontSize; private $defaultFontName; @@ -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(); @@ -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); @@ -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. */ @@ -286,6 +305,7 @@ public function testLoadConfig(): void 'pdfRendererPath' => '', 'defaultFontName' => 'Arial', 'defaultFontSize' => 10, + 'defaultFontColor' => '000000', 'outputEscapingEnabled' => false, 'defaultPaper' => 'A4', ]; diff --git a/tests/PhpWordTests/Writer/HTML/FontTest.php b/tests/PhpWordTests/Writer/HTML/FontTest.php index d2519f962f..17f82a26be 100644 --- a/tests/PhpWordTests/Writer/HTML/FontTest.php +++ b/tests/PhpWordTests/Writer/HTML/FontTest.php @@ -34,6 +34,9 @@ class FontTest extends \PHPUnit\Framework\TestCase /** @var float|int */ private $defaultFontSize; + /** @var string */ + private $defaultFontColor; + /** * Executed before each method of the class. */ @@ -41,6 +44,7 @@ protected function setUp(): void { $this->defaultFontName = Settings::getDefaultFontName(); $this->defaultFontSize = Settings::getDefaultFontSize(); + $this->defaultFontColor = Settings::getDefaultFontColor(); } /** @@ -50,6 +54,7 @@ protected function tearDown(): void { Settings::setDefaultFontName($this->defaultFontName); Settings::setDefaultFontSize($this->defaultFontSize); + Settings::setDefaultFontColor($this->defaultFontColor); } /** diff --git a/tests/PhpWordTests/Writer/Word2007/Part/StylesTest.php b/tests/PhpWordTests/Writer/Word2007/Part/StylesTest.php index 6939c6fddc..09936d6d33 100644 --- a/tests/PhpWordTests/Writer/Word2007/Part/StylesTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Part/StylesTest.php @@ -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')); + } }