From 50313116ff7561cf9c2bd99edc3822a671a34b91 Mon Sep 17 00:00:00 2001 From: Collie-IT <40590185+Collie-IT@users.noreply.github.com> Date: Thu, 21 Nov 2024 19:36:18 +0100 Subject: [PATCH 1/8] Add default font color for Word (.docx) Adds the abillity to add a default font color for generated .docx. --- src/PhpWord/PhpWord.php | 21 +++++++++++++++ src/PhpWord/Settings.php | 29 +++++++++++++++++++++ src/PhpWord/Writer/Word2007/Part/Styles.php | 4 +++ 3 files changed, 54 insertions(+) diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index cf6f16ae02..8d8142ba88 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -255,6 +255,27 @@ public function setDefaultFontName($fontName): void { Settings::setDefaultFontName($fontName); } + + /** + * Set default font color. + * + * @param string $fontColor + */ + public function setDefaultFontColor($fontColor): void + { + Settings::setDefaultFontColor($fontColor); + } + + /** + * Get default font color. + * + * @return string + */ + public function getDefaultFontColor() + { + return Settings::getDefaultFontColor(); + } + /** * Get default font size. diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 984486ccfe..3983d6da06 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -117,6 +117,13 @@ class Settings */ private static $defaultFontName = self::DEFAULT_FONT_NAME; + /** + * Default font color. + * + * @var string + */ + private static $defaultFontColor = self::DEFAULT_FONT_COLOR; + /** * Default font size. * @@ -367,6 +374,28 @@ public static function setDefaultFontName(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 2112fd3ce6..c20c465d5c 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -84,6 +84,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void $phpWord = $this->getParentWriter()->getPhpWord(); $fontName = $phpWord->getDefaultFontName(); $fontSize = $phpWord->getDefaultFontSize(); + $fontColor = $phpWord->getDefaultFontColor(); $language = $phpWord->getSettings()->getThemeFontLang(); $latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin(); @@ -97,6 +98,9 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void $xmlWriter->writeAttribute('w:eastAsia', $fontName); $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 From 61c3375570794eed99eb6cc11cf9801e6084c9c5 Mon Sep 17 00:00:00 2001 From: MichaelFrey Date: Fri, 22 Nov 2024 17:52:41 +0100 Subject: [PATCH 2/8] fix format --- src/PhpWord/PhpWord.php | 9 ++++----- src/PhpWord/Settings.php | 8 ++++---- src/PhpWord/Writer/Word2007/Part/Styles.php | 6 +++--- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 8d8142ba88..c0b88f7cbc 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -255,8 +255,8 @@ public function setDefaultFontName($fontName): void { Settings::setDefaultFontName($fontName); } - - /** + + /** * Set default font color. * * @param string $fontColor @@ -265,8 +265,8 @@ public function setDefaultFontColor($fontColor): void { Settings::setDefaultFontColor($fontColor); } - - /** + + /** * Get default font color. * * @return string @@ -276,7 +276,6 @@ public function getDefaultFontColor() return Settings::getDefaultFontColor(); } - /** * Get default font size. * diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 3983d6da06..d50a08e5b6 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -117,13 +117,13 @@ class Settings */ private static $defaultFontName = self::DEFAULT_FONT_NAME; - /** + /** * Default font color. * * @var string */ private static $defaultFontColor = self::DEFAULT_FONT_COLOR; - + /** * Default font size. * @@ -374,8 +374,8 @@ public static function setDefaultFontName(string $value): bool return false; } - - /** + + /** * Get default font color. */ public static function getDefaultFontColor(): string diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index c20c465d5c..1d75b34c45 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -84,7 +84,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void $phpWord = $this->getParentWriter()->getPhpWord(); $fontName = $phpWord->getDefaultFontName(); $fontSize = $phpWord->getDefaultFontSize(); - $fontColor = $phpWord->getDefaultFontColor(); + $fontColor = $phpWord->getDefaultFontColor(); $language = $phpWord->getSettings()->getThemeFontLang(); $latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin(); @@ -98,9 +98,9 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void $xmlWriter->writeAttribute('w:eastAsia', $fontName); $xmlWriter->writeAttribute('w:cs', $fontName); $xmlWriter->endElement(); // w:rFonts - $xmlWriter->startElement('w:color'); + $xmlWriter->startElement('w:color'); $xmlWriter->writeAttribute('w:val', $fontColor); - $xmlWriter->endElement(); + $xmlWriter->endElement(); $xmlWriter->startElement('w:sz'); $xmlWriter->writeAttribute('w:val', $fontSize * 2); $xmlWriter->endElement(); // w:sz From 0658b4e9eb071a2f2ca6beda0753fb54c629d7f6 Mon Sep 17 00:00:00 2001 From: Collie-IT <40590185+Collie-IT@users.noreply.github.com> Date: Fri, 22 Nov 2024 18:37:07 +0100 Subject: [PATCH 3/8] Update 1.4.0.md Add default font color --- docs/changes/1.x/1.4.0.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 8db845337d..cfff0810b1 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -6,6 +6,7 @@ - Writer ODText: Support for ListItemRun by [@Progi1984](https://github.com/Progi1984) fixing [#2159](https://github.com/PHPOffice/PHPWord/issues/2159), [#2620](https://github.com/PHPOffice/PHPWord/issues/2620) in [#2669](https://github.com/PHPOffice/PHPWord/pull/2669) - Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675) +- 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 From a60896c984dfe679f5cf596dfab10ca07e9278cc Mon Sep 17 00:00:00 2001 From: Collie-IT <40590185+Collie-IT@users.noreply.github.com> Date: Fri, 22 Nov 2024 18:38:55 +0100 Subject: [PATCH 4/8] Update introduction.md Add documentation default font color --- docs/usage/introduction.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/usage/introduction.md b/docs/usage/introduction.md index b3a101ab0d..1a87fe4b93 100644 --- a/docs/usage/introduction.md +++ b/docs/usage/introduction.md @@ -128,13 +128,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 +By default, every text appears in Arial 10 point in the color red (FF0000). You can alter the default font by using the following two functions: ``` php setDefaultFontName('Times New Roman'); +$phpWord->setDefaultFontColor('FF0000'); $phpWord->setDefaultFontSize(12); ``` @@ -381,4 +382,4 @@ To control whether or not words in all capital letters shall be hyphenated use t getSettings()->setDoNotHyphenateCaps(true); -``` \ No newline at end of file +``` From d44bd6fc33b1670db52b343ed4d48cf20dcf1877 Mon Sep 17 00:00:00 2001 From: Collie-IT <40590185+Collie-IT@users.noreply.github.com> Date: Fri, 22 Nov 2024 19:39:34 +0100 Subject: [PATCH 5/8] Update introduction.md Correct default value --- docs/usage/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage/introduction.md b/docs/usage/introduction.md index 1a87fe4b93..8a7e428c56 100644 --- a/docs/usage/introduction.md +++ b/docs/usage/introduction.md @@ -128,7 +128,7 @@ You can alter the default paper by using the following function: ### Default font -By default, every text appears in Arial 10 point in the color red (FF0000). You can alter the +By default, every text appears in Arial 10 point in the color black (000000). You can alter the default font by using the following two functions: ``` php From 6c5f33e4eb1ea2c85bde5f4e7367bbedb9e533e9 Mon Sep 17 00:00:00 2001 From: MichaelFrey Date: Fri, 22 Nov 2024 20:33:38 +0100 Subject: [PATCH 6/8] add tests for SetGetDefaultFontColor --- src/PhpWord/Reader/Word2007/Styles.php | 3 +++ tests/PhpWordTests/PhpWordTest.php | 12 ++++++++++++ tests/PhpWordTests/SettingsTest.php | 16 ++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/PhpWord/Reader/Word2007/Styles.php b/src/PhpWord/Reader/Word2007/Styles.php index 760adf9493..f43199ab33 100644 --- a/src/PhpWord/Reader/Word2007/Styles.php +++ b/src/PhpWord/Reader/Word2007/Styles.php @@ -46,6 +46,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/tests/PhpWordTests/PhpWordTest.php b/tests/PhpWordTests/PhpWordTest.php index 33118a11e8..a3c74993b1 100644 --- a/tests/PhpWordTests/PhpWordTest.php +++ b/tests/PhpWordTests/PhpWordTest.php @@ -82,6 +82,18 @@ public function testSetGetDefaultFontSize(): void self::assertEquals($fontSize, $phpWord->getDefaultFontSize()); } + /** + * 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 46c72eab28..10dde7b55a 100644 --- a/tests/PhpWordTests/SettingsTest.php +++ b/tests/PhpWordTests/SettingsTest.php @@ -59,6 +59,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(); @@ -75,6 +76,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); @@ -236,6 +238,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. */ From b398d4d4b99be9b96c701d3d6a019f9c625e5736 Mon Sep 17 00:00:00 2001 From: MichaelFrey Date: Fri, 22 Nov 2024 22:36:46 +0100 Subject: [PATCH 7/8] debug test --- phpword.ini.dist | 1 + tests/PhpWordTests/SettingsTest.php | 4 ++++ 2 files changed, 5 insertions(+) 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/tests/PhpWordTests/SettingsTest.php b/tests/PhpWordTests/SettingsTest.php index 10dde7b55a..843ade7159 100644 --- a/tests/PhpWordTests/SettingsTest.php +++ b/tests/PhpWordTests/SettingsTest.php @@ -30,6 +30,9 @@ class SettingsTest extends TestCase { private $compatibility; + /** @var string */ + private $defaultFontColor; + private $defaultFontSize; private $defaultFontName; @@ -287,6 +290,7 @@ public function testLoadConfig(): void 'pdfRendererPath' => '', 'defaultFontName' => 'Arial', 'defaultFontSize' => 10, + 'defaultFontColor' => '000000', 'outputEscapingEnabled' => false, 'defaultPaper' => 'A4', ]; From 8aaa00f2e75444f4d1f0bcc7b5e3707a13e84e4f Mon Sep 17 00:00:00 2001 From: MichaelFrey Date: Fri, 22 Nov 2024 22:40:47 +0100 Subject: [PATCH 8/8] add defaultFontColor to FontTest --- tests/PhpWordTests/Writer/HTML/FontTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/PhpWordTests/Writer/HTML/FontTest.php b/tests/PhpWordTests/Writer/HTML/FontTest.php index 442c2639c9..87b58a516b 100644 --- a/tests/PhpWordTests/Writer/HTML/FontTest.php +++ b/tests/PhpWordTests/Writer/HTML/FontTest.php @@ -33,6 +33,9 @@ class FontTest extends \PHPUnit\Framework\TestCase /** @var float|int */ private $defaultFontSize; + /** @var string */ + private $defaultFontColor; + /** * Executed before each method of the class. */ @@ -40,6 +43,7 @@ protected function setUp(): void { $this->defaultFontName = Settings::getDefaultFontName(); $this->defaultFontSize = Settings::getDefaultFontSize(); + $this->defaultFontColor = Settings::getDefaultFontColor(); } /** @@ -49,6 +53,7 @@ protected function tearDown(): void { Settings::setDefaultFontName($this->defaultFontName); Settings::setDefaultFontSize($this->defaultFontSize); + Settings::setDefaultFontColor($this->defaultFontColor); } /**