Skip to content

Commit 5031311

Browse files
committed
Add default font color for Word (.docx)
Adds the abillity to add a default font color for generated .docx.
1 parent feadceb commit 5031311

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/PhpWord/PhpWord.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,27 @@ public function setDefaultFontName($fontName): void
255255
{
256256
Settings::setDefaultFontName($fontName);
257257
}
258+
259+
/**
260+
* Set default font color.
261+
*
262+
* @param string $fontColor
263+
*/
264+
public function setDefaultFontColor($fontColor): void
265+
{
266+
Settings::setDefaultFontColor($fontColor);
267+
}
268+
269+
/**
270+
* Get default font color.
271+
*
272+
* @return string
273+
*/
274+
public function getDefaultFontColor()
275+
{
276+
return Settings::getDefaultFontColor();
277+
}
278+
258279

259280
/**
260281
* Get default font size.

src/PhpWord/Settings.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ class Settings
117117
*/
118118
private static $defaultFontName = self::DEFAULT_FONT_NAME;
119119

120+
/**
121+
* Default font color.
122+
*
123+
* @var string
124+
*/
125+
private static $defaultFontColor = self::DEFAULT_FONT_COLOR;
126+
120127
/**
121128
* Default font size.
122129
*
@@ -367,6 +374,28 @@ public static function setDefaultFontName(string $value): bool
367374

368375
return false;
369376
}
377+
378+
/**
379+
* Get default font color.
380+
*/
381+
public static function getDefaultFontColor(): string
382+
{
383+
return self::$defaultFontColor;
384+
}
385+
386+
/**
387+
* Set default font color.
388+
*/
389+
public static function setDefaultFontColor(string $value): bool
390+
{
391+
if (trim($value) !== '') {
392+
self::$defaultFontColor = $value;
393+
394+
return true;
395+
}
396+
397+
return false;
398+
}
370399

371400
/**
372401
* Get default font size.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void
8484
$phpWord = $this->getParentWriter()->getPhpWord();
8585
$fontName = $phpWord->getDefaultFontName();
8686
$fontSize = $phpWord->getDefaultFontSize();
87+
$fontColor = $phpWord->getDefaultFontColor();
8788
$language = $phpWord->getSettings()->getThemeFontLang();
8889
$latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin();
8990

@@ -97,6 +98,9 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void
9798
$xmlWriter->writeAttribute('w:eastAsia', $fontName);
9899
$xmlWriter->writeAttribute('w:cs', $fontName);
99100
$xmlWriter->endElement(); // w:rFonts
101+
$xmlWriter->startElement('w:color');
102+
$xmlWriter->writeAttribute('w:val', $fontColor);
103+
$xmlWriter->endElement();
100104
$xmlWriter->startElement('w:sz');
101105
$xmlWriter->writeAttribute('w:val', $fontSize * 2);
102106
$xmlWriter->endElement(); // w:sz

0 commit comments

Comments
 (0)