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