From 5cc0fc3ddc3ee4b17d818d6fa0fbe068d379a410 Mon Sep 17 00:00:00 2001 From: MichaelFrey Date: Thu, 30 Jan 2025 19:04:24 +0100 Subject: [PATCH 1/4] Writer ODText: Support Default font color --- docs/changes/1.x/1.4.0.md | 9 ++++- src/PhpWord/Writer/ODText/Part/Styles.php | 3 +- .../Writer/ODText/Style/FontTest.php | 39 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 8c2651c8b7..9eb4983aa1 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -12,7 +12,8 @@ - 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) +- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2773](https://github.com/PHPOffice/PHPWord/pull/2773) +- Writer ODText: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) - Add basic ruby text (phonetic guide) support for Word2007 and HTML Reader/Writer, RTF Writer, basic support for ODT writing by [@Deadpikle](https://github.com/Deadpikle) in [#2727](https://github.com/PHPOffice/PHPWord/pull/2727) ### Bug fixes @@ -35,3 +36,9 @@ - Deprecate `PhpOffice\PhpWord\Style\Paragraph::setIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::setIndentLeft()` ### BC Breaks + +### Note +- Writer ODText previously used to set 'style:use-window-font-color' to 'true', now it is set to 'false'. + The effect of this attribute is "implementation dependent" (if implemented at all). + Setting it to false allows setting a default font color and improves interoperabilt, + but may break certain specific use cases. diff --git a/src/PhpWord/Writer/ODText/Part/Styles.php b/src/PhpWord/Writer/ODText/Part/Styles.php index 72b93f5815..f8ec843894 100644 --- a/src/PhpWord/Writer/ODText/Part/Styles.php +++ b/src/PhpWord/Writer/ODText/Part/Styles.php @@ -92,11 +92,12 @@ private function writeDefault(XMLWriter $xmlWriter): void // Font $xmlWriter->startElement('style:text-properties'); - $xmlWriter->writeAttribute('style:use-window-font-color', 'true'); + $xmlWriter->writeAttribute('style:use-window-font-color', 'false'); $xmlWriter->writeAttribute('style:font-name', Settings::getDefaultFontName()); $xmlWriter->writeAttribute('fo:font-size', Settings::getDefaultFontSize() . 'pt'); $xmlWriter->writeAttribute('fo:language', $latinLang[0]); $xmlWriter->writeAttribute('fo:country', $latinLang[1]); + $xmlWriter->writeAttribute('fo:color', '#' . Settings::getDefaultFontColor()); $xmlWriter->writeAttribute('style:letter-kerning', 'true'); $xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2'); $xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt'); diff --git a/tests/PhpWordTests/Writer/ODText/Style/FontTest.php b/tests/PhpWordTests/Writer/ODText/Style/FontTest.php index 5edef8fea6..5fd7677010 100644 --- a/tests/PhpWordTests/Writer/ODText/Style/FontTest.php +++ b/tests/PhpWordTests/Writer/ODText/Style/FontTest.php @@ -34,6 +34,45 @@ protected function tearDown(): void TestHelperDOCX::clear(); } + public function testDefaultDefaults(): void + { + //$doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); + + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + + //$phpWord->setDefaultFontColor($defaultFontColor); + + $doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); + + $file = 'styles.xml'; + + $path = '/office:document-styles/office:styles/style:default-style/style:text-properties'; + self::assertTrue($doc->elementExists($path, $file)); + $element = $doc->getElement($path, $file); + + self::assertEquals('#000000', $element->getAttribute('fo:color')); + } + + public function testSettingDefaults(): void + { + //$doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); + + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + + $defaultFontColor = '00FF00'; + $phpWord->setDefaultFontColor($defaultFontColor); + + $doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); + + $file = 'styles.xml'; + + $path = '/office:document-styles/office:styles/style:default-style/style:text-properties'; + self::assertTrue($doc->elementExists($path, $file)); + $element = $doc->getElement($path, $file); + + self::assertEquals('#' . $defaultFontColor, $element->getAttribute('fo:color')); + } + /** * Test colors. */ From f0357bb916a4d4ad353e37b0ed7a92e81e366960 Mon Sep 17 00:00:00 2001 From: MichaelFrey Date: Thu, 30 Jan 2025 19:08:45 +0100 Subject: [PATCH 2/4] clean up --- tests/PhpWordTests/Writer/ODText/Style/FontTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/PhpWordTests/Writer/ODText/Style/FontTest.php b/tests/PhpWordTests/Writer/ODText/Style/FontTest.php index 5fd7677010..dcc027f1fd 100644 --- a/tests/PhpWordTests/Writer/ODText/Style/FontTest.php +++ b/tests/PhpWordTests/Writer/ODText/Style/FontTest.php @@ -36,12 +36,8 @@ protected function tearDown(): void public function testDefaultDefaults(): void { - //$doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); - $phpWord = new \PhpOffice\PhpWord\PhpWord(); - //$phpWord->setDefaultFontColor($defaultFontColor); - $doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); $file = 'styles.xml'; @@ -55,8 +51,6 @@ public function testDefaultDefaults(): void public function testSettingDefaults(): void { - //$doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); - $phpWord = new \PhpOffice\PhpWord\PhpWord(); $defaultFontColor = '00FF00'; From cf27c6da578265640c8af08d0aed7e6f373e0a0e Mon Sep 17 00:00:00 2001 From: MichaelFrey Date: Thu, 30 Jan 2025 19:25:26 +0100 Subject: [PATCH 3/4] self::assertEquals('false', $element->getAttribute('style:use-window-font-color')); --- tests/PhpWordTests/Writer/ODText/Style/FontTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/PhpWordTests/Writer/ODText/Style/FontTest.php b/tests/PhpWordTests/Writer/ODText/Style/FontTest.php index dcc027f1fd..b377ff4fcd 100644 --- a/tests/PhpWordTests/Writer/ODText/Style/FontTest.php +++ b/tests/PhpWordTests/Writer/ODText/Style/FontTest.php @@ -47,6 +47,7 @@ public function testDefaultDefaults(): void $element = $doc->getElement($path, $file); self::assertEquals('#000000', $element->getAttribute('fo:color')); + self::assertEquals('false', $element->getAttribute('style:use-window-font-color')); //has to be set to false so that fo:color can take effect } public function testSettingDefaults(): void From 4e9f4cf01e06140c2b8e3a9001b173fe9255f953 Mon Sep 17 00:00:00 2001 From: MichaelFrey Date: Fri, 31 Jan 2025 18:15:53 +0100 Subject: [PATCH 4/4] pull request number added --- docs/changes/1.x/1.4.0.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 9eb4983aa1..f12bb05b0d 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -12,8 +12,8 @@ - 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) in [#2773](https://github.com/PHPOffice/PHPWord/pull/2773) -- Writer ODText: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) +- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2731](https://github.com/PHPOffice/PHPWord/pull/2731) +- Writer ODText: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2735](https://github.com/PHPOffice/PHPWord/pull/2735) - Add basic ruby text (phonetic guide) support for Word2007 and HTML Reader/Writer, RTF Writer, basic support for ODT writing by [@Deadpikle](https://github.com/Deadpikle) in [#2727](https://github.com/PHPOffice/PHPWord/pull/2727) ### Bug fixes @@ -37,8 +37,8 @@ ### BC Breaks -### Note -- Writer ODText previously used to set 'style:use-window-font-color' to 'true', now it is set to 'false'. +### Notes +- Writer ODText previously used to set 'style:use-window-font-color' to 'true', now it is set to 'false'. (see [#2735](https://github.com/PHPOffice/PHPWord/pull/2735)) The effect of this attribute is "implementation dependent" (if implemented at all). Setting it to false allows setting a default font color and improves interoperabilt, but may break certain specific use cases.