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 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) { diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index f343c0502f..b2bbff2716 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,67 @@ 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); + + if (empty($content)) { + return $content; + } return $content . ' '; } @@ -86,4 +144,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; + } }