From 050d393fa52b9d06e11bd572d4effbdb96c34319 Mon Sep 17 00:00:00 2001 From: rasamassen Date: Mon, 1 Sep 2025 10:39:08 -0500 Subject: [PATCH 1/7] Update AbstractElement.php - fgColor and bgColor In combination with next edit... Sets the fgColor and bgColor according to the colorTable. --- src/PhpWord/Writer/RTF/Element/AbstractElement.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/PhpWord/Writer/RTF/Element/AbstractElement.php b/src/PhpWord/Writer/RTF/Element/AbstractElement.php index e007e6aa26..cb5ecba009 100644 --- a/src/PhpWord/Writer/RTF/Element/AbstractElement.php +++ b/src/PhpWord/Writer/RTF/Element/AbstractElement.php @@ -200,6 +200,18 @@ protected function writeFontStyle() $styleWriter->setColorIndex($colorIndex + 1); } } + if ($this->fontStyle->getFgColor() != null) { + $colorIndex = array_search($this->fontStyle->getFgColor(), $parentWriter->getColorTable()); + if ($colorIndex !== false) { + $styleWriter->setFgColorIndex($colorIndex + 1); + } + } + if ($this->fontStyle->getBgColor() != null) { + $colorIndex = array_search($this->fontStyle->getBgColor(), $parentWriter->getColorTable()); + if ($colorIndex !== false) { + $styleWriter->setBgColorIndex($colorIndex + 1); + } + } if ($this->fontStyle->getName() != null) { $fontIndex = array_search($this->fontStyle->getName(), $parentWriter->getFontTable()); if ($fontIndex !== false) { From 5f97167adb0b2e95d99fbf5ed5549207d0182b2d Mon Sep 17 00:00:00 2001 From: rasamassen Date: Mon, 1 Sep 2025 10:48:11 -0500 Subject: [PATCH 2/7] Update Font.php - add many missing features Adds underline styles, doublestrikethrough, smallcaps, allcaps, fgcolor, hidden, scall, spacing, kerning, position, noproof, and bgcolor. --- src/PhpWord/Writer/RTF/Style/Font.php | 86 +++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index f343c0502f..4c4232a37d 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -37,6 +37,16 @@ class Font extends AbstractStyle */ private $colorIndex = 0; + /** + * @var int Font foreground color index + */ + private $fgColorIndex = 0; + + /** + * @var int Font background color index + */ + private $bgColorIndex = 0; + /** * Write style. * @@ -50,19 +60,63 @@ public function write() } $content = ''; - $content .= $this->getValueIf($style->isRTL(), '\rtlch'); - $content .= '\cf' . $this->colorIndex; - $content .= '\f' . $this->nameIndex; - $size = $style->getSize(); - $content .= $this->getValueIf(is_numeric($size), '\fs' . round($size * 2)); + // To make it easy to determine what's missing as new features are added, + // Everything below is in the same order as array found in PhpOffice\PhpWord\Style\Font\getStyleValues + + // Basic + $content .= $this->getValueIf($style->getName() !== null, '\f' . $this->nameIndex); + $content .= $this->getValueIf($style->getSize() !== null, '\fs' . round($style->getSize() * 2)); + $content .= $this->getValueIf($style->getColor() !== null, '\cf' . $this->colorIndex); + // Underline Keywords + $underlines = [ + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASH => '\uldash', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHHEAVY => '\ulth', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHLONG => '\ulldash', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHLONGHEAVY => '\ulthldash', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOUBLE => '\uldb', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDASH => '\uldashd', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDASHHEAVY => '\ulthdashd', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDOTDASH => '\uldashdd', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDOTDASHHEAVY => '\ulthdashdd', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTTED => '\uld', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTTEDHEAVY => '\ulthd', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_HEAVY => '\ulth', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE => '\ul', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVY => '\ulwave', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVYDOUBLE => '\ululdbwave', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVYHEAVY => '\ulhwave', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_WORDS => '\ulw', + ]; + + // Style $content .= $this->getValueIf($style->isBold(), '\b'); $content .= $this->getValueIf($style->isItalic(), '\i'); - $content .= $this->getValueIf($style->getUnderline() != FontStyle::UNDERLINE_NONE, '\ul'); + if (isset($underlines[$style->getUnderline()])) { + $content .= $underlines[$style->getUnderline()]; + } $content .= $this->getValueIf($style->isStrikethrough(), '\strike'); + $content .= $this->getValueIf($style->isDoubleStrikethrough(), '\striked1'); $content .= $this->getValueIf($style->isSuperScript(), '\super'); $content .= $this->getValueIf($style->isSubScript(), '\sub'); + $content .= $this->getValueIf($style->isSmallCaps(), '\scaps'); + $content .= $this->getValueIf($style->isAllCaps(), '\caps'); + $content .= $this->getValueIf($style->getFgColor() !== null, '\highlight' . $this->fgColorIndex); + $content .= $this->getValueIf($style->isHidden(), '\v'); + + // Spacing + $content .= $this->getValueIf($style->getScale() !== null, '\charscalex' . $style->getScale()); + $content .= $this->getValueIf($style->getSpacing() !== null, '\expnd' . $style->getSpacing()); + $content .= $this->getValueIf($style->getKerning() !== null, '\kerning' . $style->getKerning() * 2); + $content .= $this->getValueIf($style->getPosition() !== null, '\up' . $style->getPosition()); + + // General + $content .= $this->getValueIf($style->isRTL(), '\rtlch'); + + // Other (Font settings currently not in included in array) + $content .= $this->getValueIf($style->isNoProof(), '\noproof'); + $content .= $this->getValueIf($style->getBgColor() !== null, '\cb' . $this->bgColorIndex); return $content . ' '; } @@ -86,4 +140,24 @@ public function setColorIndex($value = 0): void { $this->colorIndex = $value; } + + /** + * Set font foreground color index. + * + * @param int $value + */ + public function setFgColorIndex($value = 0): void + { + $this->fgColorIndex = $value; + } + + /** + * Set font background color index. + * + * @param int $value + */ + public function setBgColorIndex($value = 0): void + { + $this->bgColorIndex = $value; + } } From c12ed05f1c5d0f46e181bd78cea89d89473180df Mon Sep 17 00:00:00 2001 From: rasamassen Date: Mon, 1 Sep 2025 19:05:54 -0500 Subject: [PATCH 3/7] Update Font.php - Fix Extra Space Now that Font isn't returning unnecessary values, it could sometimes occur where it returned an extra space in the document if ($content == ''). This is fixed. --- src/PhpWord/Writer/RTF/Style/Font.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index 4c4232a37d..6566fd3e2a 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -117,6 +117,10 @@ public function write() // Other (Font settings currently not in included in array) $content .= $this->getValueIf($style->isNoProof(), '\noproof'); $content .= $this->getValueIf($style->getBgColor() !== null, '\cb' . $this->bgColorIndex); + + if (empty($content)) { + return $content; + } return $content . ' '; } From 9c1618f25232e7e63b60a777fe6e80ab402fed8a Mon Sep 17 00:00:00 2001 From: rasamassen Date: Mon, 1 Sep 2025 22:04:00 -0500 Subject: [PATCH 4/7] Update Font.php - Fix formatting --- src/PhpWord/Writer/RTF/Style/Font.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index 6566fd3e2a..cb57a784c8 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -113,11 +113,11 @@ public function write() // General $content .= $this->getValueIf($style->isRTL(), '\rtlch'); - + // Other (Font settings currently not in included in array) $content .= $this->getValueIf($style->isNoProof(), '\noproof'); $content .= $this->getValueIf($style->getBgColor() !== null, '\cb' . $this->bgColorIndex); - + if (empty($content)) { return $content; } From e8a11015fe48547f8d886066a5ae4b72666b3563 Mon Sep 17 00:00:00 2001 From: rasamassen Date: Mon, 1 Sep 2025 22:05:44 -0500 Subject: [PATCH 5/7] Update 1.5.0.md - Changelog for Pull 2819 --- docs/changes/1.x/1.5.0.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/changes/1.x/1.5.0.md b/docs/changes/1.x/1.5.0.md index b96865bada..a3f1b074fa 100644 --- a/docs/changes/1.x/1.5.0.md +++ b/docs/changes/1.x/1.5.0.md @@ -7,6 +7,7 @@ ### Bug fixes - Set writeAttribute return type by [@radarhere](https://github.com/radarhere) fixing [#2204](https://github.com/PHPOffice/PHPWord/issues/2204) in [#2776](https://github.com/PHPOffice/PHPWord/pull/2776) +- Writer RTF: Support for various underline styles, doublestrikethrough, smallcaps, allcaps, fgcolor, hidden, scale, spacing, kerning, position, noproof, and bgcolor in Font by [@rasamassen](https://github.com/rasamassen) in [#2819](https://github.com/PHPOffice/PHPWord/pull/2819) ### Miscellaneous @@ -16,4 +17,4 @@ ### BC Breaks -### Notes \ No newline at end of file +### Notes From 86a990ce8dd06659c4e2e7a5a46e795875fca71f Mon Sep 17 00:00:00 2001 From: rasamassen Date: Mon, 1 Sep 2025 22:31:53 -0500 Subject: [PATCH 6/7] Update Font.php - Fix formatting --- src/PhpWord/Writer/RTF/Style/Font.php | 36 +++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index cb57a784c8..d2da0927ad 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -63,7 +63,7 @@ public function write() // To make it easy to determine what's missing as new features are added, // Everything below is in the same order as array found in PhpOffice\PhpWord\Style\Font\getStyleValues - + // Basic $content .= $this->getValueIf($style->getName() !== null, '\f' . $this->nameIndex); $content .= $this->getValueIf($style->getSize() !== null, '\fs' . round($style->getSize() * 2)); @@ -71,23 +71,23 @@ public function write() // Underline Keywords $underlines = [ - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASH => '\uldash', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHHEAVY => '\ulth', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHLONG => '\ulldash', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHLONGHEAVY => '\ulthldash', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOUBLE => '\uldb', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDASH => '\uldashd', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDASHHEAVY => '\ulthdashd', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDOTDASH => '\uldashdd', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDOTDASHHEAVY => '\ulthdashdd', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTTED => '\uld', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTTEDHEAVY => '\ulthd', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_HEAVY => '\ulth', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE => '\ul', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVY => '\ulwave', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVYDOUBLE => '\ululdbwave', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVYHEAVY => '\ulhwave', - \PhpOffice\PhpWord\Style\Font::UNDERLINE_WORDS => '\ulw', + Font::UNDERLINE_DASH => '\uldash', + Font::UNDERLINE_DASHHEAVY => '\ulth', + Font::UNDERLINE_DASHLONG => '\ulldash', + Font::UNDERLINE_DASHLONGHEAVY => '\ulthldash', + Font::UNDERLINE_DOUBLE => '\uldb', + Font::UNDERLINE_DOTDASH => '\uldashd', + Font::UNDERLINE_DOTDASHHEAVY => '\ulthdashd', + Font::UNDERLINE_DOTDOTDASH => '\uldashdd', + Font::UNDERLINE_DOTDOTDASHHEAVY => '\ulthdashdd', + Font::UNDERLINE_DOTTED => '\uld', + Font::UNDERLINE_DOTTEDHEAVY => '\ulthd', + Font::UNDERLINE_HEAVY => '\ulth', + Font::UNDERLINE_SINGLE => '\ul', + Font::UNDERLINE_WAVY => '\ulwave', + Font::UNDERLINE_WAVYDOUBLE => '\ululdbwave', + Font::UNDERLINE_WAVYHEAVY => '\ulhwave', + Font::UNDERLINE_WORDS => '\ulw', ]; // Style From fb17d4145278d79881c341c4c5312702e2ad8e8b Mon Sep 17 00:00:00 2001 From: rasamassen Date: Mon, 1 Sep 2025 22:35:05 -0500 Subject: [PATCH 7/7] Update Font.php - Revert previous commit --- src/PhpWord/Writer/RTF/Style/Font.php | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index d2da0927ad..b2bbff2716 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -71,23 +71,23 @@ public function write() // Underline Keywords $underlines = [ - Font::UNDERLINE_DASH => '\uldash', - Font::UNDERLINE_DASHHEAVY => '\ulth', - Font::UNDERLINE_DASHLONG => '\ulldash', - Font::UNDERLINE_DASHLONGHEAVY => '\ulthldash', - Font::UNDERLINE_DOUBLE => '\uldb', - Font::UNDERLINE_DOTDASH => '\uldashd', - Font::UNDERLINE_DOTDASHHEAVY => '\ulthdashd', - Font::UNDERLINE_DOTDOTDASH => '\uldashdd', - Font::UNDERLINE_DOTDOTDASHHEAVY => '\ulthdashdd', - Font::UNDERLINE_DOTTED => '\uld', - Font::UNDERLINE_DOTTEDHEAVY => '\ulthd', - Font::UNDERLINE_HEAVY => '\ulth', - Font::UNDERLINE_SINGLE => '\ul', - Font::UNDERLINE_WAVY => '\ulwave', - Font::UNDERLINE_WAVYDOUBLE => '\ululdbwave', - Font::UNDERLINE_WAVYHEAVY => '\ulhwave', - Font::UNDERLINE_WORDS => '\ulw', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASH => '\uldash', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHHEAVY => '\ulth', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHLONG => '\ulldash', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHLONGHEAVY => '\ulthldash', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOUBLE => '\uldb', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDASH => '\uldashd', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDASHHEAVY => '\ulthdashd', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDOTDASH => '\uldashdd', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDOTDASHHEAVY => '\ulthdashdd', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTTED => '\uld', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTTEDHEAVY => '\ulthd', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_HEAVY => '\ulth', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE => '\ul', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVY => '\ulwave', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVYDOUBLE => '\ululdbwave', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVYHEAVY => '\ulhwave', + \PhpOffice\PhpWord\Style\Font::UNDERLINE_WORDS => '\ulw', ]; // Style