|
| 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\Element; |
| 20 | + |
| 21 | +use PhpOffice\PhpWord\Style; |
| 22 | +use PhpOffice\PhpWord\Style\Font; |
| 23 | +use PhpOffice\PhpWord\Style\Paragraph; |
| 24 | +use PhpOffice\PhpWord\Writer\HTML; |
| 25 | +use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter; |
| 26 | +use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter; |
| 27 | + |
| 28 | +/** |
| 29 | + * PreserveText element HTML writer. Issue 2188. |
| 30 | + */ |
| 31 | +class PreserveText extends AbstractElement |
| 32 | +{ |
| 33 | + /** |
| 34 | + * Opening tags. |
| 35 | + * |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + private $openingTags = ''; |
| 39 | + |
| 40 | + /** |
| 41 | + * Closing tag. |
| 42 | + * |
| 43 | + * @var string |
| 44 | + */ |
| 45 | + private $closingTags = ''; |
| 46 | + |
| 47 | + /** |
| 48 | + * Write text. |
| 49 | + * |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + public function write() |
| 53 | + { |
| 54 | + $this->processFontStyle(); |
| 55 | + |
| 56 | + /** @var \PhpOffice\PhpWord\Element\PreserveText */ |
| 57 | + $element = $this->element; |
| 58 | + $text = $element->getText(); |
| 59 | + if (is_array($text)) { |
| 60 | + $text = implode('', $text); |
| 61 | + } |
| 62 | + |
| 63 | + $text = $this->parentWriter->escapeHTML($text ?? ''); |
| 64 | + if (!$this->withoutP && !trim($text)) { |
| 65 | + $text = ' '; |
| 66 | + } |
| 67 | + |
| 68 | + $content = ''; |
| 69 | + $content .= $this->writeOpening(); |
| 70 | + $content .= $this->openingTags; |
| 71 | + $content .= $text; |
| 72 | + $content .= $this->closingTags; |
| 73 | + $content .= $this->writeClosing(); |
| 74 | + |
| 75 | + return $content; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Write opening. |
| 80 | + * |
| 81 | + * @return string |
| 82 | + */ |
| 83 | + protected function writeOpening() |
| 84 | + { |
| 85 | + $content = ''; |
| 86 | + if (!$this->withoutP) { |
| 87 | + $style = $this->getParagraphStyle(); |
| 88 | + $content .= "<p{$style}>"; |
| 89 | + } |
| 90 | + |
| 91 | + return $content; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Write ending. |
| 96 | + * |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + protected function writeClosing() |
| 100 | + { |
| 101 | + $content = ''; |
| 102 | + |
| 103 | + if (!$this->withoutP) { |
| 104 | + $content .= '</p>' . PHP_EOL; |
| 105 | + } |
| 106 | + |
| 107 | + return $content; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Write paragraph style. |
| 112 | + * |
| 113 | + * @return string |
| 114 | + */ |
| 115 | + private function getParagraphStyle() |
| 116 | + { |
| 117 | + /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ |
| 118 | + $element = $this->element; |
| 119 | + $style = ''; |
| 120 | + if (!method_exists($element, 'getParagraphStyle')) { |
| 121 | + return $style; |
| 122 | + } |
| 123 | + |
| 124 | + $paragraphStyle = $element->getParagraphStyle(); |
| 125 | + $pStyleIsObject = ($paragraphStyle instanceof Paragraph); |
| 126 | + if ($pStyleIsObject) { |
| 127 | + $styleWriter = new ParagraphStyleWriter($paragraphStyle); |
| 128 | + $styleWriter->setParentWriter($this->parentWriter); |
| 129 | + $style = $styleWriter->write(); |
| 130 | + } elseif (is_string($paragraphStyle)) { |
| 131 | + $style = $paragraphStyle; |
| 132 | + } |
| 133 | + if ($style) { |
| 134 | + $attribute = $pStyleIsObject ? 'style' : 'class'; |
| 135 | + $style = " {$attribute}=\"{$style}\""; |
| 136 | + } |
| 137 | + |
| 138 | + return $style; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Get font style. |
| 143 | + */ |
| 144 | + private function processFontStyle(): void |
| 145 | + { |
| 146 | + /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ |
| 147 | + $element = $this->element; |
| 148 | + |
| 149 | + $attributeStyle = $attributeLang = ''; |
| 150 | + $lang = null; |
| 151 | + |
| 152 | + $fontStyle = $element->getFontStyle(); |
| 153 | + if ($fontStyle instanceof Font) { |
| 154 | + // Attribute style |
| 155 | + $styleWriter = new FontStyleWriter($fontStyle); |
| 156 | + $fontCSS = $styleWriter->write(); |
| 157 | + if ($fontCSS) { |
| 158 | + $attributeStyle = ' style="' . $fontCSS . '"'; |
| 159 | + } else { |
| 160 | + $className = $fontStyle->getStyleName(); |
| 161 | + if ($className) { |
| 162 | + $attributeStyle = ' class="' . $className . '"'; |
| 163 | + } |
| 164 | + } |
| 165 | + // Attribute Lang |
| 166 | + $lang = $fontStyle->getLang(); |
| 167 | + } elseif (!empty($fontStyle)) { |
| 168 | + // Attribute class |
| 169 | + $attributeStyle = ' class="' . $fontStyle . '"'; |
| 170 | + // Attribute Lang |
| 171 | + /** @var Font $cssClassStyle */ |
| 172 | + $cssClassStyle = Style::getStyle($fontStyle); |
| 173 | + if ($cssClassStyle !== null && method_exists($cssClassStyle, 'getLang')) { |
| 174 | + $lang = $cssClassStyle->getLang(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + if ($lang) { |
| 179 | + $attributeLang = $lang->getLatin(); |
| 180 | + if (!$attributeLang) { |
| 181 | + $attributeLang = $lang->getEastAsia(); |
| 182 | + } |
| 183 | + if (!$attributeLang) { |
| 184 | + $attributeLang = $lang->getBidirectional(); |
| 185 | + } |
| 186 | + if ($attributeLang) { |
| 187 | + $attributeLang = " lang='$attributeLang'"; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if ($attributeStyle || $attributeLang) { |
| 192 | + $this->openingTags = "<span$attributeLang$attributeStyle>"; |
| 193 | + $this->closingTags = '</span>'; |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments