Skip to content

Commit f1470d0

Browse files
Collie-ITMichaelPFreyProgi1984
authored
Add default font color for Word (.docx) (#2700)
* Add default font color for Word (.docx) Adds the abillity to add a default font color for generated .docx. * fix format * Update 1.4.0.md Add default font color * Update introduction.md Add documentation default font color * Update introduction.md Correct default value * add tests for SetGetDefaultFontColor * debug test * add defaultFontColor to FontTest * Update src/PhpWord/PhpWord.php As suggested Co-authored-by: Progi1984 <progi1984@gmail.com> * Update src/PhpWord/PhpWord.php As suggested Co-authored-by: Progi1984 <progi1984@gmail.com> * Update 1.4.0.md Add default font color * Update introduction.md Add documentation default font color * Update introduction.md Correct default value * add tests for SetGetDefaultFontColor * debug test * add defaultFontColor to FontTest * Update src/PhpWord/PhpWord.php As suggested Co-authored-by: Progi1984 <progi1984@gmail.com> * Update src/PhpWord/PhpWord.php As suggested Co-authored-by: Progi1984 <progi1984@gmail.com> * fix format * add test for default color * clean up * cs fixer --------- Co-authored-by: MichaelFrey <michael.frey@gmx.ch> Co-authored-by: Progi1984 <progi1984@gmail.com>
1 parent 10ae499 commit f1470d0

File tree

11 files changed

+134
-4
lines changed

11 files changed

+134
-4
lines changed

docs/changes/1.x/1.4.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Writer Word2007: Support for padding in Table Cell by [@Azamat8405](https://github.com/Azamat8405) in [#2697](https://github.com/PHPOffice/PHPWord/pull/2697)
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)
14+
- Add Default font color for Word by [@Collie-IT](https://github.com/Collie-IT) in [#2700](https://github.com/PHPOffice/PHPWord/pull/2700)
1415

1516
### Bug fixes
1617

docs/usage/introduction.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,14 @@ You can alter the default paper by using the following function:
127127

128128
### Default font
129129

130-
By default, every text appears in Arial 10 point. You can alter the
131-
default font by using the following two functions:
130+
By default, every text appears in Arial 10 point in the color black (000000).
131+
You can alter the default font by using the following functions:
132132

133133
``` php
134134
<?php
135135

136136
$phpWord->setDefaultFontName('Times New Roman');
137+
$phpWord->setDefaultFontColor('FF0000');
137138
$phpWord->setDefaultFontSize(12);
138139
```
139140

phpword.ini.dist

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ outputEscapingEnabled = false
1414

1515
defaultFontName = Arial
1616
defaultFontSize = 10
17+
defaultFontColor = 000000
1718

1819
[Paper]
1920

src/PhpWord/PhpWord.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public function getDefaultAsianFontName(): string
266266
}
267267

268268
/**
269-
* Set default font name.
269+
* Set default asian font name.
270270
*
271271
* @param string $fontName
272272
*/
@@ -275,6 +275,22 @@ public function setDefaultAsianFontName($fontName): void
275275
Settings::setDefaultAsianFontName($fontName);
276276
}
277277

278+
/**
279+
* Set default font color.
280+
*/
281+
public function setDefaultFontColor(string $fontColor): void
282+
{
283+
Settings::setDefaultFontColor($fontColor);
284+
}
285+
286+
/**
287+
* Get default font color.
288+
*/
289+
public function getDefaultFontColor(): string
290+
{
291+
return Settings::getDefaultFontColor();
292+
}
293+
278294
/**
279295
* Get default font size.
280296
*

src/PhpWord/Reader/Word2007/Styles.php

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function read(PhpWord $phpWord): void
4747
if (array_key_exists('size', $fontDefaultStyle)) {
4848
$phpWord->setDefaultFontSize($fontDefaultStyle['size']);
4949
}
50+
if (array_key_exists('color', $fontDefaultStyle)) {
51+
$phpWord->setDefaultFontColor($fontDefaultStyle['color']);
52+
}
5053
if (array_key_exists('lang', $fontDefaultStyle)) {
5154
$phpWord->getSettings()->setThemeFontLang(new Language($fontDefaultStyle['lang']));
5255
}

src/PhpWord/Settings.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,19 @@ class Settings
119119
private static $defaultFontName = self::DEFAULT_FONT_NAME;
120120

121121
/**
122-
* Default font name.
122+
* Default asian font name.
123123
*
124124
* @var string
125125
*/
126126
private static $defaultAsianFontName = self::DEFAULT_FONT_NAME;
127127

128+
/**
129+
* Default font color.
130+
*
131+
* @var string
132+
*/
133+
private static $defaultFontColor = self::DEFAULT_FONT_COLOR;
134+
128135
/**
129136
* Default font size.
130137
*
@@ -395,6 +402,28 @@ public static function setDefaultAsianFontName(string $value): bool
395402
return false;
396403
}
397404

405+
/**
406+
* Get default font color.
407+
*/
408+
public static function getDefaultFontColor(): string
409+
{
410+
return self::$defaultFontColor;
411+
}
412+
413+
/**
414+
* Set default font color.
415+
*/
416+
public static function setDefaultFontColor(string $value): bool
417+
{
418+
if (trim($value) !== '') {
419+
self::$defaultFontColor = $value;
420+
421+
return true;
422+
}
423+
424+
return false;
425+
}
426+
398427
/**
399428
* Get default font size.
400429
*

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

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void
8686
$fontName = $phpWord->getDefaultFontName();
8787
$asianFontName = $phpWord->getDefaultAsianFontName();
8888
$fontSize = $phpWord->getDefaultFontSize();
89+
$fontColor = $phpWord->getDefaultFontColor();
8990
$language = $phpWord->getSettings()->getThemeFontLang();
9091
$latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin();
9192

@@ -99,6 +100,9 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void
99100
$xmlWriter->writeAttribute('w:eastAsia', $asianFontName);
100101
$xmlWriter->writeAttribute('w:cs', $fontName);
101102
$xmlWriter->endElement(); // w:rFonts
103+
$xmlWriter->startElement('w:color');
104+
$xmlWriter->writeAttribute('w:val', $fontColor);
105+
$xmlWriter->endElement();
102106
$xmlWriter->startElement('w:sz');
103107
$xmlWriter->writeAttribute('w:val', $fontSize * 2);
104108
$xmlWriter->endElement(); // w:sz

tests/PhpWordTests/PhpWordTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ public function testSetGetDefaultAsianFontName(): void
9595
self::assertEquals($fontName, $phpWord->getDefaultAsianFontName());
9696
}
9797

98+
/**
99+
* Test set/get default font color.
100+
*/
101+
public function testSetGetDefaultFontColor(): void
102+
{
103+
$phpWord = new PhpWord();
104+
$fontColor = 'FF0000';
105+
self::assertEquals(Settings::DEFAULT_FONT_COLOR, $phpWord->getDefaultFontColor());
106+
$phpWord->setDefaultFontColor($fontColor);
107+
self::assertEquals($fontColor, $phpWord->getDefaultFontColor());
108+
}
109+
98110
/**
99111
* Test set default paragraph style.
100112
*/

tests/PhpWordTests/SettingsTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class SettingsTest extends TestCase
3131
{
3232
private $compatibility;
3333

34+
/** @var string */
35+
private $defaultFontColor;
36+
3437
private $defaultFontSize;
3538

3639
private $defaultFontName;
@@ -60,6 +63,7 @@ class SettingsTest extends TestCase
6063
protected function setUp(): void
6164
{
6265
$this->compatibility = Settings::hasCompatibility();
66+
$this->defaultFontColor = Settings::getDefaultFontColor();
6367
$this->defaultFontSize = Settings::getDefaultFontSize();
6468
$this->defaultFontName = Settings::getDefaultFontName();
6569
$this->defaultPaper = Settings::getDefaultPaper();
@@ -76,6 +80,7 @@ protected function setUp(): void
7680
protected function tearDown(): void
7781
{
7882
Settings::setCompatibility($this->compatibility);
83+
Settings::setDefaultFontColor($this->defaultFontColor);
7984
Settings::setDefaultFontSize($this->defaultFontSize);
8085
Settings::setDefaultFontName($this->defaultFontName);
8186
Settings::setDefaultPaper($this->defaultPaper);
@@ -251,6 +256,20 @@ public function testSetGetDefaultFontSize(): void
251256
self::assertEquals(12.5, Settings::getDefaultFontSize());
252257
}
253258

259+
/**
260+
* Test set/get default font color.
261+
*/
262+
public function testSetGetDefaultFontColor(): void
263+
{
264+
self::assertEquals(Settings::DEFAULT_FONT_COLOR, Settings::getDefaultFontColor());
265+
self::assertFalse(Settings::setDefaultFontColor(' '));
266+
self::assertEquals(Settings::DEFAULT_FONT_COLOR, Settings::getDefaultFontColor());
267+
self::assertTrue(Settings::setDefaultFontColor('FF0000'));
268+
self::assertEquals('FF0000', Settings::getDefaultFontColor());
269+
self::assertFalse(Settings::setDefaultFontColor(' '));
270+
self::assertEquals('FF0000', Settings::getDefaultFontColor());
271+
}
272+
254273
/**
255274
* Test set/get default paper.
256275
*/
@@ -286,6 +305,7 @@ public function testLoadConfig(): void
286305
'pdfRendererPath' => '',
287306
'defaultFontName' => 'Arial',
288307
'defaultFontSize' => 10,
308+
'defaultFontColor' => '000000',
289309
'outputEscapingEnabled' => false,
290310
'defaultPaper' => 'A4',
291311
];

tests/PhpWordTests/Writer/HTML/FontTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ class FontTest extends \PHPUnit\Framework\TestCase
3434
/** @var float|int */
3535
private $defaultFontSize;
3636

37+
/** @var string */
38+
private $defaultFontColor;
39+
3740
/**
3841
* Executed before each method of the class.
3942
*/
4043
protected function setUp(): void
4144
{
4245
$this->defaultFontName = Settings::getDefaultFontName();
4346
$this->defaultFontSize = Settings::getDefaultFontSize();
47+
$this->defaultFontColor = Settings::getDefaultFontColor();
4448
}
4549

4650
/**
@@ -50,6 +54,7 @@ protected function tearDown(): void
5054
{
5155
Settings::setDefaultFontName($this->defaultFontName);
5256
Settings::setDefaultFontSize($this->defaultFontSize);
57+
Settings::setDefaultFontColor($this->defaultFontColor);
5358
}
5459

5560
/**

tests/PhpWordTests/Writer/Word2007/Part/StylesTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,42 @@ public function testFontStyleBasedOnOtherFontStyle(): void
145145
$element = $doc->getElement($path, $file);
146146
self::assertEquals('Generation', $element->getAttribute('w:val'));
147147
}
148+
149+
/**
150+
* Test default font color.
151+
*/
152+
public function testDefaultDefaultFontColor(): void
153+
{
154+
$phpWord = new PhpWord();
155+
156+
$doc = TestHelperDOCX::getDocument($phpWord);
157+
158+
$file = 'word/styles.xml';
159+
160+
$path = '/w:styles/w:docDefaults/w:rPrDefault/w:rPr/w:color';
161+
self::assertTrue($doc->elementExists($path, $file));
162+
$element = $doc->getElement($path, $file);
163+
164+
self::assertEquals('000000', $element->getAttribute('w:val'));
165+
}
166+
167+
/**
168+
* Test default font color.
169+
*/
170+
public function testDefaultFontColor(): void
171+
{
172+
$phpWord = new PhpWord();
173+
$defaultFontColor = '00FF00';
174+
$phpWord->setDefaultFontColor($defaultFontColor);
175+
176+
$doc = TestHelperDOCX::getDocument($phpWord);
177+
178+
$file = 'word/styles.xml';
179+
180+
$path = '/w:styles/w:docDefaults/w:rPrDefault/w:rPr/w:color';
181+
self::assertTrue($doc->elementExists($path, $file));
182+
$element = $doc->getElement($path, $file);
183+
184+
self::assertEquals($defaultFontColor, $element->getAttribute('w:val'));
185+
}
148186
}

0 commit comments

Comments
 (0)