@@ -37,6 +37,16 @@ class Font extends AbstractStyle
37
37
*/
38
38
private $ colorIndex = 0 ;
39
39
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
+
40
50
/**
41
51
* Write style.
42
52
*
@@ -50,19 +60,63 @@ public function write()
50
60
}
51
61
52
62
$ content = '' ;
53
- $ content .= $ this ->getValueIf ($ style ->isRTL (), '\rtlch ' );
54
- $ content .= '\cf ' . $ this ->colorIndex ;
55
- $ content .= '\f ' . $ this ->nameIndex ;
56
63
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 );
59
71
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
60
94
$ content .= $ this ->getValueIf ($ style ->isBold (), '\b ' );
61
95
$ 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
+ }
63
99
$ content .= $ this ->getValueIf ($ style ->isStrikethrough (), '\strike ' );
100
+ $ content .= $ this ->getValueIf ($ style ->isDoubleStrikethrough (), '\striked1 ' );
64
101
$ content .= $ this ->getValueIf ($ style ->isSuperScript (), '\super ' );
65
102
$ 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 );
66
120
67
121
return $ content . ' ' ;
68
122
}
@@ -86,4 +140,24 @@ public function setColorIndex($value = 0): void
86
140
{
87
141
$ this ->colorIndex = $ value ;
88
142
}
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
+ }
89
163
}
0 commit comments