Skip to content

Commit 5f97167

Browse files
authored
Update Font.php - add many missing features
Adds underline styles, doublestrikethrough, smallcaps, allcaps, fgcolor, hidden, scall, spacing, kerning, position, noproof, and bgcolor.
1 parent 050d393 commit 5f97167

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

src/PhpWord/Writer/RTF/Style/Font.php

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ class Font extends AbstractStyle
3737
*/
3838
private $colorIndex = 0;
3939

40+
/**
41+
* @var int Font foreground color index
42+
*/
43+
private $fgColorIndex = 0;
44+
45+
/**
46+
* @var int Font background color index
47+
*/
48+
private $bgColorIndex = 0;
49+
4050
/**
4151
* Write style.
4252
*
@@ -50,19 +60,63 @@ public function write()
5060
}
5161

5262
$content = '';
53-
$content .= $this->getValueIf($style->isRTL(), '\rtlch');
54-
$content .= '\cf' . $this->colorIndex;
55-
$content .= '\f' . $this->nameIndex;
5663

57-
$size = $style->getSize();
58-
$content .= $this->getValueIf(is_numeric($size), '\fs' . round($size * 2));
64+
// To make it easy to determine what's missing as new features are added,
65+
// Everything below is in the same order as array found in PhpOffice\PhpWord\Style\Font\getStyleValues
66+
67+
// Basic
68+
$content .= $this->getValueIf($style->getName() !== null, '\f' . $this->nameIndex);
69+
$content .= $this->getValueIf($style->getSize() !== null, '\fs' . round($style->getSize() * 2));
70+
$content .= $this->getValueIf($style->getColor() !== null, '\cf' . $this->colorIndex);
5971

72+
// Underline Keywords
73+
$underlines = [
74+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DASH => '\uldash',
75+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHHEAVY => '\ulth',
76+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHLONG => '\ulldash',
77+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHLONGHEAVY => '\ulthldash',
78+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOUBLE => '\uldb',
79+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDASH => '\uldashd',
80+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDASHHEAVY => '\ulthdashd',
81+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDOTDASH => '\uldashdd',
82+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDOTDASHHEAVY => '\ulthdashdd',
83+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTTED => '\uld',
84+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTTEDHEAVY => '\ulthd',
85+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_HEAVY => '\ulth',
86+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE => '\ul',
87+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVY => '\ulwave',
88+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVYDOUBLE => '\ululdbwave',
89+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVYHEAVY => '\ulhwave',
90+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_WORDS => '\ulw',
91+
];
92+
93+
// Style
6094
$content .= $this->getValueIf($style->isBold(), '\b');
6195
$content .= $this->getValueIf($style->isItalic(), '\i');
62-
$content .= $this->getValueIf($style->getUnderline() != FontStyle::UNDERLINE_NONE, '\ul');
96+
if (isset($underlines[$style->getUnderline()])) {
97+
$content .= $underlines[$style->getUnderline()];
98+
}
6399
$content .= $this->getValueIf($style->isStrikethrough(), '\strike');
100+
$content .= $this->getValueIf($style->isDoubleStrikethrough(), '\striked1');
64101
$content .= $this->getValueIf($style->isSuperScript(), '\super');
65102
$content .= $this->getValueIf($style->isSubScript(), '\sub');
103+
$content .= $this->getValueIf($style->isSmallCaps(), '\scaps');
104+
$content .= $this->getValueIf($style->isAllCaps(), '\caps');
105+
$content .= $this->getValueIf($style->getFgColor() !== null, '\highlight' . $this->fgColorIndex);
106+
$content .= $this->getValueIf($style->isHidden(), '\v');
107+
108+
// Spacing
109+
$content .= $this->getValueIf($style->getScale() !== null, '\charscalex' . $style->getScale());
110+
$content .= $this->getValueIf($style->getSpacing() !== null, '\expnd' . $style->getSpacing());
111+
$content .= $this->getValueIf($style->getKerning() !== null, '\kerning' . $style->getKerning() * 2);
112+
$content .= $this->getValueIf($style->getPosition() !== null, '\up' . $style->getPosition());
113+
114+
// General
115+
$content .= $this->getValueIf($style->isRTL(), '\rtlch');
116+
117+
// Other (Font settings currently not in included in array)
118+
$content .= $this->getValueIf($style->isNoProof(), '\noproof');
119+
$content .= $this->getValueIf($style->getBgColor() !== null, '\cb' . $this->bgColorIndex);
66120

67121
return $content . ' ';
68122
}
@@ -86,4 +140,24 @@ public function setColorIndex($value = 0): void
86140
{
87141
$this->colorIndex = $value;
88142
}
143+
144+
/**
145+
* Set font foreground color index.
146+
*
147+
* @param int $value
148+
*/
149+
public function setFgColorIndex($value = 0): void
150+
{
151+
$this->fgColorIndex = $value;
152+
}
153+
154+
/**
155+
* Set font background color index.
156+
*
157+
* @param int $value
158+
*/
159+
public function setBgColorIndex($value = 0): void
160+
{
161+
$this->bgColorIndex = $value;
162+
}
89163
}

0 commit comments

Comments
 (0)