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