Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
71 / 71 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Font | |
100.00% |
71 / 71 |
|
100.00% |
3 / 3 |
20 | |
100.00% |
1 / 1 |
write | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
writeStyle | |
100.00% |
58 / 58 |
|
100.00% |
1 / 1 |
14 | |||
setIsInline | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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\Word2007\Style; |
19 | |
20 | /** |
21 | * Font style writer. |
22 | * |
23 | * @since 0.10.0 |
24 | */ |
25 | class Font extends AbstractStyle |
26 | { |
27 | /** |
28 | * Is inline in element. |
29 | * |
30 | * @var bool |
31 | */ |
32 | private $isInline = false; |
33 | |
34 | /** |
35 | * Write style. |
36 | */ |
37 | public function write(): void |
38 | { |
39 | $xmlWriter = $this->getXmlWriter(); |
40 | |
41 | $isStyleName = $this->isInline && null !== $this->style && is_string($this->style); |
42 | if ($isStyleName) { |
43 | $xmlWriter->startElement('w:rPr'); |
44 | $xmlWriter->startElement('w:rStyle'); |
45 | $xmlWriter->writeAttribute('w:val', $this->style); |
46 | $xmlWriter->endElement(); |
47 | $style = \PhpOffice\PhpWord\Style::getStyle($this->style); |
48 | if ($style instanceof \PhpOffice\PhpWord\Style\Font) { |
49 | $xmlWriter->writeElementIf($style->isRTL(), 'w:rtl'); |
50 | } |
51 | $xmlWriter->endElement(); |
52 | } else { |
53 | $this->writeStyle(); |
54 | } |
55 | } |
56 | |
57 | /** |
58 | * Write full style. |
59 | */ |
60 | private function writeStyle(): void |
61 | { |
62 | $style = $this->getStyle(); |
63 | if (!$style instanceof \PhpOffice\PhpWord\Style\Font) { |
64 | return; |
65 | } |
66 | |
67 | $xmlWriter = $this->getXmlWriter(); |
68 | |
69 | $xmlWriter->startElement('w:rPr'); |
70 | |
71 | // Style name |
72 | if ($this->isInline === true) { |
73 | $styleName = $style->getStyleName(); |
74 | $xmlWriter->writeElementIf($styleName !== null, 'w:rStyle', 'w:val', $styleName); |
75 | } |
76 | |
77 | // Font name/family |
78 | $font = $style->getName(); |
79 | $hint = $style->getHint(); |
80 | if ($font !== null) { |
81 | $xmlWriter->startElement('w:rFonts'); |
82 | $xmlWriter->writeAttribute('w:ascii', $font); |
83 | $xmlWriter->writeAttribute('w:hAnsi', $font); |
84 | $xmlWriter->writeAttribute('w:eastAsia', $font); |
85 | $xmlWriter->writeAttribute('w:cs', $font); |
86 | $xmlWriter->writeAttributeIf($hint !== null, 'w:hint', $hint); |
87 | $xmlWriter->endElement(); |
88 | } |
89 | |
90 | //Language |
91 | $language = $style->getLang(); |
92 | if ($language != null && ($language->getLatin() !== null || $language->getEastAsia() !== null || $language->getBidirectional() !== null)) { |
93 | $xmlWriter->startElement('w:lang'); |
94 | $xmlWriter->writeAttributeIf($language->getLatin() !== null, 'w:val', $language->getLatin()); |
95 | $xmlWriter->writeAttributeIf($language->getEastAsia() !== null, 'w:eastAsia', $language->getEastAsia()); |
96 | $xmlWriter->writeAttributeIf($language->getBidirectional() !== null, 'w:bidi', $language->getBidirectional()); |
97 | //if bidi is not set but we are writing RTL, write the latin language in the bidi tag |
98 | if ($style->isRTL() && $language->getBidirectional() === null && $language->getLatin() !== null) { |
99 | $xmlWriter->writeAttribute('w:bidi', $language->getLatin()); |
100 | } |
101 | $xmlWriter->endElement(); |
102 | } |
103 | |
104 | // Color |
105 | $color = $style->getColor(); |
106 | $xmlWriter->writeElementIf($color !== null, 'w:color', 'w:val', $color); |
107 | |
108 | // Size |
109 | $size = $style->getSize(); |
110 | $xmlWriter->writeElementIf($size !== null, 'w:sz', 'w:val', $size * 2); |
111 | $xmlWriter->writeElementIf($size !== null, 'w:szCs', 'w:val', $size * 2); |
112 | |
113 | // Bold, italic |
114 | $xmlWriter->writeElementIf($style->isBold() !== null, 'w:b', 'w:val', $this->writeOnOf($style->isBold())); |
115 | $xmlWriter->writeElementIf($style->isBold() !== null, 'w:bCs', 'w:val', $this->writeOnOf($style->isBold())); |
116 | $xmlWriter->writeElementIf($style->isItalic() !== null, 'w:i', 'w:val', $this->writeOnOf($style->isItalic())); |
117 | $xmlWriter->writeElementIf($style->isItalic() !== null, 'w:iCs', 'w:val', $this->writeOnOf($style->isItalic())); |
118 | |
119 | // Strikethrough, double strikethrough |
120 | $xmlWriter->writeElementIf($style->isStrikethrough(), 'w:strike', 'w:val', $this->writeOnOf($style->isStrikethrough())); |
121 | $xmlWriter->writeElementIf($style->isDoubleStrikethrough(), 'w:dstrike', 'w:val', $this->writeOnOf($style->isDoubleStrikethrough())); |
122 | |
123 | // Small caps, all caps |
124 | $xmlWriter->writeElementIf($style->isSmallCaps() !== null, 'w:smallCaps', 'w:val', $this->writeOnOf($style->isSmallCaps())); |
125 | $xmlWriter->writeElementIf($style->isAllCaps() !== null, 'w:caps', 'w:val', $this->writeOnOf($style->isAllCaps())); |
126 | |
127 | //Hidden text |
128 | $xmlWriter->writeElementIf($style->isHidden(), 'w:vanish', 'w:val', $this->writeOnOf($style->isHidden())); |
129 | |
130 | // Underline |
131 | $xmlWriter->writeElementIf($style->getUnderline() != 'none', 'w:u', 'w:val', $style->getUnderline()); |
132 | |
133 | // Foreground-Color |
134 | $xmlWriter->writeElementIf($style->getFgColor() !== null, 'w:highlight', 'w:val', $style->getFgColor()); |
135 | |
136 | // Superscript/subscript |
137 | $xmlWriter->writeElementIf($style->isSuperScript(), 'w:vertAlign', 'w:val', 'superscript'); |
138 | $xmlWriter->writeElementIf($style->isSubScript(), 'w:vertAlign', 'w:val', 'subscript'); |
139 | |
140 | // Spacing |
141 | $xmlWriter->writeElementIf($style->getScale() !== null, 'w:w', 'w:val', $style->getScale()); |
142 | $xmlWriter->writeElementIf($style->getSpacing() !== null, 'w:spacing', 'w:val', $style->getSpacing()); |
143 | $xmlWriter->writeElementIf($style->getKerning() !== null, 'w:kern', 'w:val', $style->getKerning() * 2); |
144 | |
145 | // noProof |
146 | $xmlWriter->writeElementIf($style->isNoProof() !== null, 'w:noProof', 'w:val', $this->writeOnOf($style->isNoProof())); |
147 | |
148 | // Background-Color |
149 | $shading = $style->getShading(); |
150 | if (null !== $shading) { |
151 | $styleWriter = new Shading($xmlWriter, $shading); |
152 | $styleWriter->write(); |
153 | } |
154 | |
155 | // RTL |
156 | if ($this->isInline === true) { |
157 | $styleName = $style->getStyleName(); |
158 | $xmlWriter->writeElementIf($styleName === null && $style->isRTL(), 'w:rtl'); |
159 | } |
160 | |
161 | // Position |
162 | $xmlWriter->writeElementIf($style->getPosition() !== null, 'w:position', 'w:val', $style->getPosition()); |
163 | |
164 | $xmlWriter->endElement(); |
165 | } |
166 | |
167 | /** |
168 | * Set is inline. |
169 | * |
170 | * @param bool $value |
171 | */ |
172 | public function setIsInline($value): void |
173 | { |
174 | $this->isInline = $value; |
175 | } |
176 | } |