Skip to content

Commit

Permalink
Writer ODText: Support Default font color (#2735)
Browse files Browse the repository at this point in the history
* Writer ODText: Support Default font color

* clean up

* self::assertEquals('false', $element->getAttribute('style:use-window-font-color'));

* pull request number added
  • Loading branch information
MichaelPFrey authored Jan 31, 2025
1 parent 269a810 commit fd06f96
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
9 changes: 8 additions & 1 deletion docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [#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
Expand All @@ -35,3 +36,9 @@
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::setIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::setIndentLeft()`

### BC Breaks

### 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.
3 changes: 2 additions & 1 deletion src/PhpWord/Writer/ODText/Part/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
34 changes: 34 additions & 0 deletions tests/PhpWordTests/Writer/ODText/Style/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,40 @@ protected function tearDown(): void
TestHelperDOCX::clear();
}

public function testDefaultDefaults(): void
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();

$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'));
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
{
$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.
*/
Expand Down

0 comments on commit fd06f96

Please sign in to comment.