Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
41 / 41 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Font | |
100.00% |
41 / 41 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
write | |
100.00% |
35 / 35 |
|
100.00% |
1 / 1 |
6 | |||
getFontFamily | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPWord - A pure PHP library for reading and writing |
4 | * word processing documents. |
5 | * |
6 | * PHPWord is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPWord |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord\Writer\HTML\Style; |
19 | |
20 | use PhpOffice\PhpWord\Style\Font as FontStyle; |
21 | |
22 | /** |
23 | * Font style HTML writer. |
24 | * |
25 | * @since 0.10.0 |
26 | */ |
27 | class Font extends AbstractStyle |
28 | { |
29 | /** |
30 | * Write style. |
31 | * |
32 | * @return string |
33 | */ |
34 | public function write() |
35 | { |
36 | $style = $this->getStyle(); |
37 | if (!$style instanceof FontStyle) { |
38 | return ''; |
39 | } |
40 | $css = []; |
41 | |
42 | $font = $this->getFontFamily($style->getName(), $style->getFallbackFont()); |
43 | $size = $style->getSize(); |
44 | $color = $style->getColor(); |
45 | $fgColor = $style->getFgColor(); |
46 | $underline = $style->getUnderline() != FontStyle::UNDERLINE_NONE; |
47 | $lineThrough = $style->isStrikethrough() || $style->isDoubleStrikethrough(); |
48 | |
49 | $css['font-family'] = $this->getValueIf(!empty($font), $font); |
50 | $css['font-size'] = $this->getValueIf($size !== null, "{$size}pt"); |
51 | $css['color'] = $this->getValueIf($color !== null, "#{$color}"); |
52 | $css['background'] = $this->getValueIf($fgColor != '', $fgColor); |
53 | $css['font-weight'] = $this->getValueIf($style->isBold(), 'bold'); |
54 | $css['font-style'] = $this->getValueIf($style->isItalic(), 'italic'); |
55 | $css['vertical-align'] = ''; |
56 | $css['vertical-align'] .= $this->getValueIf($style->isSuperScript(), 'super'); |
57 | $css['vertical-align'] .= $this->getValueIf($style->isSubScript(), 'sub'); |
58 | $css['text-decoration'] = ''; |
59 | $css['text-decoration'] .= $this->getValueIf($underline, 'underline '); |
60 | $css['text-decoration'] .= $this->getValueIf($lineThrough, 'line-through '); |
61 | $css['text-transform'] = $this->getValueIf($style->isAllCaps(), 'uppercase'); |
62 | $css['font-variant'] = $this->getValueIf($style->isSmallCaps(), 'small-caps'); |
63 | $css['display'] = $this->getValueIf($style->isHidden(), 'none'); |
64 | $whitespace = $style->getWhiteSpace(); |
65 | if ($whitespace) { |
66 | $css['white-space'] = $whitespace; |
67 | } |
68 | |
69 | $spacing = $style->getSpacing(); |
70 | $css['letter-spacing'] = $this->getValueIf(null !== $spacing, ($spacing / 20) . 'pt'); |
71 | if ($style->isRTL()) { |
72 | $css['direction'] = 'rtl'; |
73 | } elseif ($style->isRTL() === false) { |
74 | $css['direction'] = 'ltr'; |
75 | } |
76 | |
77 | return $this->assembleCss($css); |
78 | } |
79 | |
80 | /** |
81 | * Set font and alternates for css font-family. |
82 | */ |
83 | private function getFontFamily(?string $font, string $genericFont): string |
84 | { |
85 | if (empty($font)) { |
86 | return ''; |
87 | } |
88 | $fontfamily = "'" . htmlspecialchars($font, ENT_QUOTES, 'UTF-8') . "'"; |
89 | if (!empty($genericFont)) { |
90 | $fontfamily .= ", $genericFont"; |
91 | } |
92 | |
93 | return $fontfamily; |
94 | } |
95 | } |