Skip to content

Commit 1956745

Browse files
authored
Update Font.php - Implement most features for RTF
Adds a lot of features. Font name, color, lang, and shading still not working.
1 parent 7f02c9c commit 1956745

File tree

1 file changed

+65
-5
lines changed

1 file changed

+65
-5
lines changed

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

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

40+
/**
41+
* @var int Font lang index
42+
*/
43+
private $langIndex = 0;
44+
4045
/**
4146
* Write style.
4247
*
@@ -50,20 +55,66 @@ public function write()
5055
}
5156

5257
$content = '';
53-
$content .= $this->getValueIf($style->isRTL(), '\rtlch');
54-
$content .= '\cf' . $this->colorIndex;
58+
59+
// Font Name
5560
$content .= '\f' . $this->nameIndex;
5661

57-
$size = $style->getSize();
58-
$content .= $this->getValueIf(is_numeric($size), '\fs' . round($size * 2));
62+
// Basic - same order as array found in PhpOffice\PhpWord\Style\Font\getStyleValues
63+
$content .= $this->getValueIf($style->getName() !== null, '\f' . $this->nameIndex); // Doesn't work; fonts not implemented.
64+
$content .= $this->getValueIf($style->getSize() !== null, '\fs' . round($style->getSize() * 2));
65+
$content .= $this->getValueIf($style->getColor() !== null, '\cf' . $this->colorIndex); // Doesn't work; coloring not implemented.
66+
// Hint (font content type) not implemented.
67+
68+
// Underline Keywords
69+
$underlines = [
70+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DASH => '\uldash',
71+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHHEAVY => '\ulth',
72+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHLONG => '\ulldash',
73+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DASHLONGHEAVY => '\ulthldash',
74+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOUBLE => '\uldb',
75+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDASH => '\uldashd',
76+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDASHHEAVY => '\ulthdashd',
77+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDOTDASH => '\uldashdd',
78+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTDOTDASHHEAVY => '\ulthdashdd',
79+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTTED => '\uld',
80+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_DOTTEDHEAVY => '\ulthd',
81+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_HEAVY => '\ulth',
82+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE => '\ul',
83+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVY => '\ulwave',
84+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVYDOUBLE => '\ululdbwave',
85+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_WAVYHEAVY => '\ulhwave',
86+
\PhpOffice\PhpWord\Style\Font::UNDERLINE_WORDS => '\ulw',
87+
];
5988

89+
// Style - same order as array found in PhpOffice\PhpWord\Style\Font\getStyleValues
6090
$content .= $this->getValueIf($style->isBold(), '\b');
6191
$content .= $this->getValueIf($style->isItalic(), '\i');
62-
$content .= $this->getValueIf($style->getUnderline() != FontStyle::UNDERLINE_NONE, '\ul');
92+
if (isset($underlines[$style->getUnderline()])) { $content .= $underlines[$style->getUnderline()]; }
6393
$content .= $this->getValueIf($style->isStrikethrough(), '\strike');
94+
$content .= $this->getValueIf($style->isDoubleStrikethrough(), '\striked1');
6495
$content .= $this->getValueIf($style->isSuperScript(), '\super');
6596
$content .= $this->getValueIf($style->isSubScript(), '\sub');
97+
$content .= $this->getValueIf($style->isSmallCaps(), '\scaps');
98+
$content .= $this->getValueIf($style->isAllCaps(), '\caps');
99+
$content .= $this->getValueIf($style->getFgColor() !== null, '\highlight' . $this->colorIndex); // Doesn't work; coloring not implemented.
100+
$content .= $this->getValueIf($style->isHidden(), '\v');
101+
102+
// Spacing - same order as array found in PhpOffice\PhpWord\Style\Font\getStyleValues
103+
$content .= $this->getValueIf($style->getScale() !== null, '\charscalex' . $style->getScale());
104+
$content .= $this->getValueIf($style->getSpacing() !== null, '\expnd' . $style->getSpacing());
105+
$content .= $this->getValueIf($style->getKerning() !== null, '\kerning' . $style->getKerning() * 2);
106+
$content .= $this->getValueIf($style->getPosition() !== null, '\up' . $style->getPosition());
66107

108+
// General - same order as array found in PhpOffice\PhpWord\Style\Font\getStyleValues
109+
// Paragraph not implemented.
110+
$content .= $this->getValueIf($style->isRTL(), '\rtlch');
111+
$content .= $this->getValueIf($style->getShading() !== null, '\chcfpat' . $this->colorIndex); // Doesn't work; coloring not implemented.
112+
$content .= $this->getValueIf($style->getColor() !== null, '\lnag' . $this->langIndex); // Doesn't work; language not implemented.
113+
// Whitespace and fallbackFont are HTML specific
114+
115+
// Other items not in included in array found in PhpOffice\PhpWord\Style\Font\getStyleValues
116+
$content .= $this->getValueIf($style->isNoProof(), '\noproof');
117+
$content .= $this->getValueIf($style->getBgColor() !== null, '\cb' . $this->colorIndex); // Doesn't work; coloring not implemented.
67118
return $content . ' ';
68119
}
69120

@@ -86,4 +137,13 @@ public function setColorIndex($value = 0): void
86137
{
87138
$this->colorIndex = $value;
88139
}
140+
/**
141+
* Set font lang index.
142+
*
143+
* @param int $value
144+
*/
145+
public function setLangIndex($value = 0): void
146+
{
147+
$this->langIndex = $value;
148+
}
89149
}

0 commit comments

Comments
 (0)