@@ -34,40 +34,48 @@ class Font extends AbstractStyle
3434 */
3535 public function write ()
3636 {
37- if (!( $ this ->style instanceof \PhpOffice \PhpWord \Style \Font) ) {
37+ if (!$ this ->style instanceof \PhpOffice \PhpWord \Style \Font) {
3838 return ;
3939 }
4040
41+ $ font = $ this ->style ->getName ();
42+ $ size = $ this ->style ->getSize ();
43+ $ color = $ this ->style ->getColor ();
44+ $ fgColor = $ this ->style ->getFgColor ();
45+ $ underline = $ this ->style ->getUnderline () != FontStyle::UNDERLINE_NONE ;
46+ $ lineThrough = $ this ->style ->isStrikethrough () || $ this ->style ->isDoubleStrikethrough ();
47+
4148 $ css = array ();
42- if (PhpWord::DEFAULT_FONT_NAME != $ this ->style ->getName ()) {
43- $ css ['font-family ' ] = "' " . $ this ->style ->getName () . "' " ;
44- }
45- if (PhpWord::DEFAULT_FONT_SIZE != $ this ->style ->getSize ()) {
46- $ css ['font-size ' ] = $ this ->style ->getSize () . 'pt ' ;
47- }
48- if (PhpWord::DEFAULT_FONT_COLOR != $ this ->style ->getColor ()) {
49- $ css ['color ' ] = '# ' . $ this ->style ->getColor ();
50- }
51- $ css ['background ' ] = $ this ->style ->getFgColor ();
52- if ($ this ->style ->isBold ()) {
53- $ css ['font-weight ' ] = 'bold ' ;
54- }
55- if ($ this ->style ->isItalic ()) {
56- $ css ['font-style ' ] = 'italic ' ;
57- }
49+
50+ $ css ['font-family ' ] = $ this ->getValueIf ($ font != PhpWord::DEFAULT_FONT_NAME , "' {$ font }' " );
51+ $ css ['font-size ' ] = $ this ->getValueIf ($ size != PhpWord::DEFAULT_FONT_SIZE , "{$ size }pt " );
52+ $ css ['color ' ] = $ this ->getValueIf ($ color != PhpWord::DEFAULT_FONT_COLOR , "# {$ color }" );
53+ $ css ['background ' ] = $ this ->getValueIf ($ fgColor != '' , $ fgColor );
54+ $ css ['font-weight ' ] = $ this ->getValueIf ($ this ->style ->isBold (), 'bold ' );
55+ $ css ['font-style ' ] = $ this ->getValueIf ($ this ->style ->isItalic (), 'italic ' );
56+
57+ $ css ['text-decoration ' ] = '' ;
58+ $ css ['text-decoration ' ] .= $ this ->getValueIf ($ underline , 'underline ' );
59+ $ css ['text-decoration ' ] .= $ this ->getValueIf ($ lineThrough , 'line-through ' );
60+
5861 if ($ this ->style ->isSuperScript ()) {
5962 $ css ['vertical-align ' ] = 'super ' ;
6063 } elseif ($ this ->style ->isSubScript ()) {
6164 $ css ['vertical-align ' ] = 'sub ' ;
6265 }
63- $ css ['text-decoration ' ] = '' ;
64- if ($ this ->style ->getUnderline () != FontStyle::UNDERLINE_NONE ) {
65- $ css ['text-decoration ' ] .= 'underline ' ;
66- }
67- if ($ this ->style ->isStrikethrough ()) {
68- $ css ['text-decoration ' ] .= 'line-through ' ;
69- }
7066
7167 return $ this ->assembleCss ($ css );
7268 }
69+
70+ /**
71+ * Get value if ...
72+ *
73+ * @param bool $condition
74+ * @param string $value
75+ * @return string
76+ */
77+ private function getValueIf ($ condition , $ value )
78+ {
79+ return $ condition ? $ value : '' ;
80+ }
7381}
0 commit comments