Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default font color for Word (.docx) #2700

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions docs/usage/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

$phpWord->setDefaultFontName('Times New Roman');
$phpWord->setDefaultFontColor('FF0000');
$phpWord->setDefaultFontSize(12);
```

Expand Down Expand Up @@ -381,4 +382,4 @@ To control whether or not words in all capital letters shall be hyphenated use t
<?php

$phpWord->getSettings()->setDoNotHyphenateCaps(true);
```
```
1 change: 1 addition & 0 deletions phpword.ini.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ outputEscapingEnabled = false

defaultFontName = Arial
defaultFontSize = 10
defaultFontColor = 000000

[Paper]

Expand Down
16 changes: 16 additions & 0 deletions src/PhpWord/PhpWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
3 changes: 3 additions & 0 deletions src/PhpWord/Reader/Word2007/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
}
Expand Down
29 changes: 29 additions & 0 deletions src/PhpWord/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
4 changes: 4 additions & 0 deletions src/PhpWord/Writer/Word2007/Part/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/PhpWordTests/PhpWordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/PhpWordTests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class SettingsTest extends TestCase
{
private $compatibility;

/** @var string */
private $defaultFontColor;

private $defaultFontSize;

private $defaultFontName;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -271,6 +290,7 @@ public function testLoadConfig(): void
'pdfRendererPath' => '',
'defaultFontName' => 'Arial',
'defaultFontSize' => 10,
'defaultFontColor' => '000000',
'outputEscapingEnabled' => false,
'defaultPaper' => 'A4',
];
Expand Down
5 changes: 5 additions & 0 deletions tests/PhpWordTests/Writer/HTML/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ class FontTest extends \PHPUnit\Framework\TestCase
/** @var float|int */
private $defaultFontSize;

/** @var string */
private $defaultFontColor;

/**
* Executed before each method of the class.
*/
protected function setUp(): void
{
$this->defaultFontName = Settings::getDefaultFontName();
$this->defaultFontSize = Settings::getDefaultFontSize();
$this->defaultFontColor = Settings::getDefaultFontColor();
}

/**
Expand All @@ -49,6 +53,7 @@ protected function tearDown(): void
{
Settings::setDefaultFontName($this->defaultFontName);
Settings::setDefaultFontSize($this->defaultFontSize);
Settings::setDefaultFontColor($this->defaultFontColor);
}

/**
Expand Down
Loading