Skip to content

Commit

Permalink
Add default font color for Word (.docx)
Browse files Browse the repository at this point in the history
Adds the abillity to add a default font color for generated .docx.
  • Loading branch information
Collie-IT committed Nov 21, 2024
1 parent feadceb commit 5031311
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/PhpWord/PhpWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,27 @@ public function setDefaultFontName($fontName): void
{
Settings::setDefaultFontName($fontName);
}

/**
* Set default font color.
*
* @param string $fontColor
*/
public function setDefaultFontColor($fontColor): void
{
Settings::setDefaultFontColor($fontColor);
}

/**
* Get default font color.
*
* @return string
*/
public function getDefaultFontColor()
{
return Settings::getDefaultFontColor();
}


/**
* Get default font size.
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 @@ -367,6 +374,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

0 comments on commit 5031311

Please sign in to comment.