Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/changes/1.x/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -16,4 +17,4 @@

### BC Breaks

### Notes
### Notes
12 changes: 12 additions & 0 deletions src/PhpWord/Writer/RTF/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,21 @@
if ($this->fontStyle->getColor() != null) {
$colorIndex = array_search($this->fontStyle->getColor(), $parentWriter->getColorTable());
if ($colorIndex !== false) {
$styleWriter->setColorIndex($colorIndex + 1);

Check failure on line 200 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.2)

Ignored error pattern #^Binary operation "\+" between int\|string and 1 results in an error\.$# in path /home/runner/work/PHPWord/PHPWord/src/PhpWord/Writer/RTF/Element/AbstractElement.php is expected to occur 1 time, but occurred 3 times.

Check failure on line 200 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Ignored error pattern #^Binary operation "\+" between int\|string and 1 results in an error\.$# in path /home/runner/work/PHPWord/PHPWord/src/PhpWord/Writer/RTF/Element/AbstractElement.php is expected to occur 1 time, but occurred 3 times.

Check failure on line 200 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.4)

Ignored error pattern #^Binary operation "\+" between int\|string and 1 results in an error\.$# in path /home/runner/work/PHPWord/PHPWord/src/PhpWord/Writer/RTF/Element/AbstractElement.php is expected to occur 1 time, but occurred 3 times.

Check failure on line 200 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Ignored error pattern #^Binary operation "\+" between int\|string and 1 results in an error\.$# in path /home/runner/work/PHPWord/PHPWord/src/PhpWord/Writer/RTF/Element/AbstractElement.php is expected to occur 1 time, but occurred 3 times.

Check failure on line 200 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.3)

Ignored error pattern #^Binary operation "\+" between int\|string and 1 results in an error\.$# in path /home/runner/work/PHPWord/PHPWord/src/PhpWord/Writer/RTF/Element/AbstractElement.php is expected to occur 1 time, but occurred 3 times.

Check failure on line 200 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Ignored error pattern #^Binary operation "\+" between int\|string and 1 results in an error\.$# in path /home/runner/work/PHPWord/PHPWord/src/PhpWord/Writer/RTF/Element/AbstractElement.php is expected to occur 1 time, but occurred 3 times.

Check failure on line 200 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Ignored error pattern #^Binary operation "\+" between int\|string and 1 results in an error\.$# in path /home/runner/work/PHPWord/PHPWord/src/PhpWord/Writer/RTF/Element/AbstractElement.php is expected to occur 1 time, but occurred 3 times.

Check failure on line 200 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Ignored error pattern #^Binary operation "\+" between int\|string and 1 results in an error\.$# in path /home/runner/work/PHPWord/PHPWord/src/PhpWord/Writer/RTF/Element/AbstractElement.php is expected to occur 1 time, but occurred 3 times.
}
}
if ($this->fontStyle->getFgColor() != null) {
$colorIndex = array_search($this->fontStyle->getFgColor(), $parentWriter->getColorTable());
if ($colorIndex !== false) {
$styleWriter->setFgColorIndex($colorIndex + 1);

Check failure on line 206 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.2)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 206 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 206 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.4)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 206 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 206 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.3)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 206 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 206 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 206 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Binary operation "+" between int|string and 1 results in an error.
}
}
if ($this->fontStyle->getBgColor() != null) {
$colorIndex = array_search($this->fontStyle->getBgColor(), $parentWriter->getColorTable());
if ($colorIndex !== false) {
$styleWriter->setBgColorIndex($colorIndex + 1);

Check failure on line 212 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.2)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 212 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 212 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.4)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 212 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 212 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.3)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 212 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 212 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Binary operation "+" between int|string and 1 results in an error.

Check failure on line 212 in src/PhpWord/Writer/RTF/Element/AbstractElement.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Binary operation "+" between int|string and 1 results in an error.
}
}
if ($this->fontStyle->getName() != null) {
$fontIndex = array_search($this->fontStyle->getName(), $parentWriter->getFontTable());
if ($fontIndex !== false) {
Expand Down
90 changes: 84 additions & 6 deletions src/PhpWord/Writer/RTF/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 . ' ';
}
Expand All @@ -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;
}
}
Loading