Skip to content

Commit 7890e1f

Browse files
committed
Improve Underline in ODF and HTML
Implements underline styles in ODF. Implements as much as CSS offers for underline styles.
1 parent 20267fc commit 7890e1f

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ public function write()
4040
}
4141
$css = [];
4242

43+
$underlines = [
44+
FontStyle::UNDERLINE_DASH => 'underline dashed ',
45+
FontStyle::UNDERLINE_DASHHEAVY => 'underline dashed 2px ',
46+
FontStyle::UNDERLINE_DASHLONG => 'underline dashed ',
47+
FontStyle::UNDERLINE_DASHLONGHEAVY => 'underline dashed 2px ',
48+
FontStyle::UNDERLINE_DOUBLE => 'underline double ',
49+
FontStyle::UNDERLINE_DOTDASH => 'underline dotted ',
50+
FontStyle::UNDERLINE_DOTDASHHEAVY => 'underline dotted 2px ',
51+
FontStyle::UNDERLINE_DOTDOTDASH => 'underline dotted ',
52+
FontStyle::UNDERLINE_DOTDOTDASHHEAVY => 'underline dotted 2px ',
53+
FontStyle::UNDERLINE_DOTTED => 'underline dotted ',
54+
FontStyle::UNDERLINE_DOTTEDHEAVY => 'underline dotted 2px ',
55+
FontStyle::UNDERLINE_HEAVY => 'underline solid 2px ',
56+
FontStyle::UNDERLINE_SINGLE => 'underline solid ',
57+
FontStyle::UNDERLINE_WAVY => 'underline wavy ',
58+
FontStyle::UNDERLINE_WAVYDOUBLE => 'underline wavy ',
59+
FontStyle::UNDERLINE_WAVYHEAVY => 'underline wavy 2px ',
60+
FontStyle::UNDERLINE_WORDS => 'underline solid ',
61+
];
62+
4363
$font = $this->getFontFamily($style->getName(), $style->getFallbackFont());
4464
$size = $style->getSize();
4565
$color = $style->getColor();
@@ -57,7 +77,9 @@ public function write()
5777
$css['vertical-align'] .= $this->getValueIf($style->isSuperScript(), 'super');
5878
$css['vertical-align'] .= $this->getValueIf($style->isSubScript(), 'sub');
5979
$css['text-decoration'] = '';
60-
$css['text-decoration'] .= $this->getValueIf($underline, 'underline ');
80+
if (isset($underlines[$style->getUnderline()])) {
81+
$css['text-decoration'] .= $this->getValueIf($underline, $underlines[$style->getUnderline()]);
82+
}
6183
$css['text-decoration'] .= $this->getValueIf($lineThrough, 'line-through ');
6284
$css['text-transform'] = $this->getValueIf($style->isAllCaps(), 'uppercase');
6385
$css['font-variant'] = $this->getValueIf($style->isSmallCaps(), 'small-caps');

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace PhpOffice\PhpWord\Writer\ODText\Style;
2020

21+
use PhpOffice\PhpWord\Style\Font as FontStyle;
22+
2123
/**
2224
* Font style writer.
2325
*
@@ -72,9 +74,34 @@ public function write(): void
7274
$xmlWriter->writeAttributeIf($style->isItalic(), 'style:font-style-complex', 'italic');
7375

7476
// Underline
75-
// @todo Various mode of underline
77+
$underlines = [
78+
FontStyle::UNDERLINE_DASH => 'dash',
79+
FontStyle::UNDERLINE_DASHHEAVY => 'dash',
80+
FontStyle::UNDERLINE_DASHLONG => 'long-dash',
81+
FontStyle::UNDERLINE_DASHLONGHEAVY => 'long-dash',
82+
FontStyle::UNDERLINE_DOUBLE => 'solid',
83+
FontStyle::UNDERLINE_DOTDASH => 'dot-dash',
84+
FontStyle::UNDERLINE_DOTDASHHEAVY => 'dot-dash',
85+
FontStyle::UNDERLINE_DOTDOTDASH => 'dot-dot-dash',
86+
FontStyle::UNDERLINE_DOTDOTDASHHEAVY => 'dot-dot-dash',
87+
FontStyle::UNDERLINE_DOTTED => 'dotted',
88+
FontStyle::UNDERLINE_DOTTEDHEAVY => 'dotted',
89+
FontStyle::UNDERLINE_HEAVY => 'solid',
90+
FontStyle::UNDERLINE_SINGLE => 'solid',
91+
FontStyle::UNDERLINE_WAVY => 'wave',
92+
FontStyle::UNDERLINE_WAVYDOUBLE => 'wave',
93+
FontStyle::UNDERLINE_WAVYHEAVY => 'wave',
94+
FontStyle::UNDERLINE_WORDS => 'solid',
95+
];
96+
7697
$underline = $style->getUnderline();
77-
$xmlWriter->writeAttributeIf($underline != 'none', 'style:text-underline-style', 'solid');
98+
if (isset($underlines[$underline])) {
99+
$xmlWriter->writeAttributeIf($underline != 'none', 'style:text-underline-style', $underlines[$underline]);
100+
$xmlWriter->writeAttributeIf(str_contains(strtolower($underline), 'heavy'), 'style:text-underline-width', 'bold');
101+
$xmlWriter->writeAttributeIf(str_contains(strtolower($underline), 'thick'), 'style:text-underline-width', 'bold');
102+
$xmlWriter->writeAttributeIf(str_contains(strtolower($underline), 'double'), 'style:text-underline-type', 'double');
103+
$xmlWriter->writeAttributeIf(str_contains(strtolower($underline), 'words'), 'style:text-underline-mode', 'skip-white-space');
104+
}
78105

79106
// Strikethrough, double strikethrough
80107
$xmlWriter->writeAttributeIf($style->isStrikethrough(), 'style:text-line-through-type', 'single');

0 commit comments

Comments
 (0)