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 diff --git a/docs/usage/introduction.md b/docs/usage/introduction.md index b3a101ab0d..8a7e428c56 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 black (000000). 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 +``` 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 cf6f16ae02..e67dfb7187 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -256,6 +256,22 @@ public function setDefaultFontName($fontName): void Settings::setDefaultFontName($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 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/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 984486ccfe..d50a08e5b6 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. * @@ -368,6 +375,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..1d75b34c45 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 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..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; @@ -59,6 +62,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 +79,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 +241,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. */ @@ -271,6 +290,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 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); } /**