Skip to content

Commit fd06f96

Browse files
authored
Writer ODText: Support Default font color (#2735)
* Writer ODText: Support Default font color * clean up * self::assertEquals('false', $element->getAttribute('style:use-window-font-color')); * pull request number added
1 parent 269a810 commit fd06f96

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

docs/changes/1.x/1.4.0.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#2660](https://github.com/PHPOffice/PHPWord/pull/2660)
1313
- 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)
1414
- Add Default font color for Word by [@Collie-IT](https://github.com/Collie-IT) in [#2700](https://github.com/PHPOffice/PHPWord/pull/2700)
15-
- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey)
15+
- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2731](https://github.com/PHPOffice/PHPWord/pull/2731)
16+
- Writer ODText: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2735](https://github.com/PHPOffice/PHPWord/pull/2735)
1617
- 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)
1718

1819
### Bug fixes
@@ -35,3 +36,9 @@
3536
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::setIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::setIndentLeft()`
3637

3738
### BC Breaks
39+
40+
### Notes
41+
- 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))
42+
The effect of this attribute is "implementation dependent" (if implemented at all).
43+
Setting it to false allows setting a default font color and improves interoperabilt,
44+
but may break certain specific use cases.

src/PhpWord/Writer/ODText/Part/Styles.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ private function writeDefault(XMLWriter $xmlWriter): void
9292

9393
// Font
9494
$xmlWriter->startElement('style:text-properties');
95-
$xmlWriter->writeAttribute('style:use-window-font-color', 'true');
95+
$xmlWriter->writeAttribute('style:use-window-font-color', 'false');
9696
$xmlWriter->writeAttribute('style:font-name', Settings::getDefaultFontName());
9797
$xmlWriter->writeAttribute('fo:font-size', Settings::getDefaultFontSize() . 'pt');
9898
$xmlWriter->writeAttribute('fo:language', $latinLang[0]);
9999
$xmlWriter->writeAttribute('fo:country', $latinLang[1]);
100+
$xmlWriter->writeAttribute('fo:color', '#' . Settings::getDefaultFontColor());
100101
$xmlWriter->writeAttribute('style:letter-kerning', 'true');
101102
$xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2');
102103
$xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt');

tests/PhpWordTests/Writer/ODText/Style/FontTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,40 @@ protected function tearDown(): void
3434
TestHelperDOCX::clear();
3535
}
3636

37+
public function testDefaultDefaults(): void
38+
{
39+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
40+
41+
$doc = TestHelperDOCX::getDocument($phpWord, 'ODText');
42+
43+
$file = 'styles.xml';
44+
45+
$path = '/office:document-styles/office:styles/style:default-style/style:text-properties';
46+
self::assertTrue($doc->elementExists($path, $file));
47+
$element = $doc->getElement($path, $file);
48+
49+
self::assertEquals('#000000', $element->getAttribute('fo:color'));
50+
self::assertEquals('false', $element->getAttribute('style:use-window-font-color')); //has to be set to false so that fo:color can take effect
51+
}
52+
53+
public function testSettingDefaults(): void
54+
{
55+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
56+
57+
$defaultFontColor = '00FF00';
58+
$phpWord->setDefaultFontColor($defaultFontColor);
59+
60+
$doc = TestHelperDOCX::getDocument($phpWord, 'ODText');
61+
62+
$file = 'styles.xml';
63+
64+
$path = '/office:document-styles/office:styles/style:default-style/style:text-properties';
65+
self::assertTrue($doc->elementExists($path, $file));
66+
$element = $doc->getElement($path, $file);
67+
68+
self::assertEquals('#' . $defaultFontColor, $element->getAttribute('fo:color'));
69+
}
70+
3771
/**
3872
* Test colors.
3973
*/

0 commit comments

Comments
 (0)