Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.16% |
160 / 163 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Styles | |
98.16% |
160 / 163 |
|
75.00% |
6 / 8 |
34 | |
0.00% |
0 / 1 |
| write | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| writeDefault | |
95.12% |
39 / 41 |
|
0.00% |
0 / 1 |
8 | |||
| writeNamed | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| cvttwiptostr | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| writePageLayout | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| writePageLayoutIndiv | |
100.00% |
62 / 62 |
|
100.00% |
1 / 1 |
5 | |||
| writeMaster | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
| writeHeaderFooter | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
7.01 | |||
| 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\ODText\Part; |
| 20 | |
| 21 | use PhpOffice\PhpWord\Element\Footer; |
| 22 | use PhpOffice\PhpWord\Settings; |
| 23 | use PhpOffice\PhpWord\Shared\Converter; |
| 24 | use PhpOffice\PhpWord\Shared\XMLWriter; |
| 25 | use PhpOffice\PhpWord\Style; |
| 26 | use PhpOffice\PhpWord\Writer\ODText\Style\Font as FontStyleWriter; |
| 27 | |
| 28 | /** |
| 29 | * ODText styles part writer: styles.xml. |
| 30 | */ |
| 31 | class Styles extends AbstractPart |
| 32 | { |
| 33 | /** |
| 34 | * Write part. |
| 35 | * |
| 36 | * @return string |
| 37 | */ |
| 38 | public function write() |
| 39 | { |
| 40 | $xmlWriter = $this->getXmlWriter(); |
| 41 | |
| 42 | // XML header |
| 43 | $xmlWriter->startDocument('1.0', 'UTF-8'); |
| 44 | $xmlWriter->startElement('office:document-styles'); |
| 45 | $this->writeCommonRootAttributes($xmlWriter); |
| 46 | |
| 47 | // Font declarations |
| 48 | $this->writeFontFaces($xmlWriter); |
| 49 | |
| 50 | // Office styles |
| 51 | $xmlWriter->startElement('office:styles'); |
| 52 | $this->writeDefault($xmlWriter); |
| 53 | $this->writeNamed($xmlWriter); |
| 54 | $xmlWriter->endElement(); |
| 55 | |
| 56 | // Automatic styles |
| 57 | $xmlWriter->startElement('office:automatic-styles'); |
| 58 | $this->writePageLayout($xmlWriter); |
| 59 | $xmlWriter->endElement(); // office:automatic-styles |
| 60 | |
| 61 | // Master style |
| 62 | $this->writeMaster($xmlWriter); |
| 63 | |
| 64 | $xmlWriter->endElement(); // office:document-styles |
| 65 | |
| 66 | return $xmlWriter->getData(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Write default styles. |
| 71 | */ |
| 72 | private function writeDefault(XMLWriter $xmlWriter): void |
| 73 | { |
| 74 | $xmlWriter->startElement('style:default-style'); |
| 75 | $xmlWriter->writeAttribute('style:family', 'paragraph'); |
| 76 | |
| 77 | // Paragraph |
| 78 | $xmlWriter->startElement('style:paragraph-properties'); |
| 79 | $xmlWriter->writeAttribute('fo:hyphenation-ladder-count', 'no-limit'); |
| 80 | $xmlWriter->writeAttribute('style:text-autospace', 'ideograph-alpha'); |
| 81 | $xmlWriter->writeAttribute('style:punctuation-wrap', 'hanging'); |
| 82 | $xmlWriter->writeAttribute('style:line-break', 'strict'); |
| 83 | $xmlWriter->writeAttribute('style:tab-stop-distance', '1.249cm'); |
| 84 | $xmlWriter->writeAttribute('style:writing-mode', 'page'); |
| 85 | $xmlWriter->endElement(); // style:paragraph-properties |
| 86 | |
| 87 | $language = $this->getParentWriter()->getPhpWord()->getSettings()->getThemeFontLang(); |
| 88 | $hideErrors = $this->getParentWriter()->getPhpWord()->getSettings()->hasHideGrammaticalErrors(); |
| 89 | |
| 90 | // Font |
| 91 | $xmlWriter->startElement('style:text-properties'); |
| 92 | $xmlWriter->writeAttribute('style:use-window-font-color', 'false'); |
| 93 | $xmlWriter->writeAttribute('style:font-name', Settings::getDefaultFontName()); |
| 94 | $xmlWriter->writeAttribute('fo:font-size', Settings::getDefaultFontSize() . 'pt'); |
| 95 | if ($hideErrors) { |
| 96 | $xmlWriter->writeAttribute('fo:language', 'zxx'); |
| 97 | $xmlWriter->writeAttribute('fo:country', 'none'); |
| 98 | } else { |
| 99 | // The same default language as the Word2007 writer |
| 100 | FontStyleWriter::writeLanguage($xmlWriter, $language !== null && $language->getLatin() !== null ? $language->getLatin() : Style\Language::EN_US); |
| 101 | } |
| 102 | $xmlWriter->writeAttribute('fo:color', '#' . Settings::getDefaultFontColor()); |
| 103 | $xmlWriter->writeAttribute('style:letter-kerning', 'true'); |
| 104 | $xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2'); |
| 105 | $xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt'); |
| 106 | if ($hideErrors) { |
| 107 | $xmlWriter->writeAttribute('style:language-asian', 'zxx'); |
| 108 | $xmlWriter->writeAttribute('style:country-asian', 'none'); |
| 109 | } elseif ($language !== null) { |
| 110 | FontStyleWriter::writeLanguage($xmlWriter, $language->getEastAsia(), 'asian'); |
| 111 | } |
| 112 | $xmlWriter->writeAttribute('style:font-name-complex', Settings::getDefaultFontName() . '2'); |
| 113 | $xmlWriter->writeAttribute('style:font-size-complex', Settings::getDefaultFontSize() . 'pt'); |
| 114 | if ($hideErrors) { |
| 115 | $xmlWriter->writeAttribute('style:language-complex', 'zxx'); |
| 116 | $xmlWriter->writeAttribute('style:country-complex', 'none'); |
| 117 | } elseif ($language !== null) { |
| 118 | FontStyleWriter::writeLanguage($xmlWriter, $language->getBidirectional(), 'complex'); |
| 119 | } |
| 120 | $xmlWriter->writeAttribute('fo:hyphenate', 'false'); |
| 121 | $xmlWriter->writeAttribute('fo:hyphenation-remain-char-count', '2'); |
| 122 | $xmlWriter->writeAttribute('fo:hyphenation-push-char-count', '2'); |
| 123 | $xmlWriter->endElement(); // style:text-properties |
| 124 | |
| 125 | $xmlWriter->endElement(); // style:default-style |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Write named styles. |
| 130 | */ |
| 131 | private function writeNamed(XMLWriter $xmlWriter): void |
| 132 | { |
| 133 | $styles = Style::getStyles(); |
| 134 | if (count($styles) > 0) { |
| 135 | foreach ($styles as $style) { |
| 136 | if ($style->isAuto() === false) { |
| 137 | $styleClass = str_replace('\\Style\\', '\\Writer\\ODText\\Style\\', get_class($style)); |
| 138 | if (class_exists($styleClass)) { |
| 139 | /** @var \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle $styleWriter Type hint */ |
| 140 | $styleWriter = new $styleClass($xmlWriter, $style); |
| 141 | $styleWriter->write(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Convert int in twips to inches/cm then to string and append unit. |
| 150 | * |
| 151 | * @param float|int $twips |
| 152 | * @param float $factor |
| 153 | * return string |
| 154 | */ |
| 155 | private static function cvttwiptostr($twips, $factor = 1.0) |
| 156 | { |
| 157 | $ins = (string) ($twips * $factor / Converter::INCH_TO_TWIP) . 'in'; |
| 158 | $cms = (string) ($twips * $factor * Converter::INCH_TO_CM / Converter::INCH_TO_TWIP) . 'cm'; |
| 159 | |
| 160 | return (strlen($ins) < strlen($cms)) ? $ins : $cms; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * call writePageLayoutIndiv to write page layout styles for each page. |
| 165 | */ |
| 166 | private function writePageLayout(XMLWriter $xmlWriter): void |
| 167 | { |
| 168 | $sections = $this->getParentWriter()->getPhpWord()->getSections(); |
| 169 | $countsects = count($sections); |
| 170 | for ($i = 0; $i < $countsects; ++$i) { |
| 171 | $this->writePageLayoutIndiv($xmlWriter, $sections[$i], $i + 1); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Write page layout styles. |
| 177 | * |
| 178 | * @param \PhpOffice\PhpWord\Element\Section $section |
| 179 | * @param int $sectionNbr |
| 180 | */ |
| 181 | private function writePageLayoutIndiv(XMLWriter $xmlWriter, $section, $sectionNbr): void |
| 182 | { |
| 183 | $sty = $section->getStyle(); |
| 184 | if (count($section->getHeaders()) > 0) { |
| 185 | $topfactor = 0.5; |
| 186 | } else { |
| 187 | $topfactor = 1.0; |
| 188 | } |
| 189 | if (count($section->getFooters()) > 0) { |
| 190 | $botfactor = 0.5; |
| 191 | } else { |
| 192 | $botfactor = 1.0; |
| 193 | } |
| 194 | $orient = $sty->getOrientation(); |
| 195 | $pwidth = self::cvttwiptostr($sty->getPageSizeW()); |
| 196 | $pheight = self::cvttwiptostr($sty->getPageSizeH()); |
| 197 | $mtop = self::cvttwiptostr($sty->getMarginTop(), $topfactor); |
| 198 | $mbottom = self::cvttwiptostr($sty->getMarginBottom(), $botfactor); |
| 199 | $mleft = self::cvttwiptostr($sty->getMarginRight()); |
| 200 | $mright = self::cvttwiptostr($sty->getMarginLeft()); |
| 201 | |
| 202 | $xmlWriter->startElement('style:page-layout'); |
| 203 | $xmlWriter->writeAttribute('style:name', "Mpm$sectionNbr"); |
| 204 | |
| 205 | $xmlWriter->startElement('style:page-layout-properties'); |
| 206 | $xmlWriter->writeAttribute('fo:page-width', $pwidth); |
| 207 | $xmlWriter->writeAttribute('fo:page-height', $pheight); |
| 208 | $xmlWriter->writeAttribute('style:num-format', '1'); |
| 209 | $xmlWriter->writeAttribute('style:print-orientation', $orient); |
| 210 | $xmlWriter->writeAttribute('fo:margin-top', $mtop); |
| 211 | $xmlWriter->writeAttribute('fo:margin-bottom', $mbottom); |
| 212 | $xmlWriter->writeAttribute('fo:margin-left', $mleft); |
| 213 | $xmlWriter->writeAttribute('fo:margin-right', $mright); |
| 214 | $xmlWriter->writeAttribute('style:writing-mode', 'lr-tb'); |
| 215 | $xmlWriter->writeAttribute('style:layout-grid-color', '#c0c0c0'); |
| 216 | $xmlWriter->writeAttribute('style:layout-grid-lines', '25199'); |
| 217 | $xmlWriter->writeAttribute('style:layout-grid-base-height', '0.423cm'); |
| 218 | $xmlWriter->writeAttribute('style:layout-grid-ruby-height', '0cm'); |
| 219 | $xmlWriter->writeAttribute('style:layout-grid-mode', 'none'); |
| 220 | $xmlWriter->writeAttribute('style:layout-grid-ruby-below', 'false'); |
| 221 | $xmlWriter->writeAttribute('style:layout-grid-print', 'false'); |
| 222 | $xmlWriter->writeAttribute('style:layout-grid-display', 'false'); |
| 223 | $xmlWriter->writeAttribute('style:layout-grid-base-width', '0.37cm'); |
| 224 | $xmlWriter->writeAttribute('style:layout-grid-snap-to', 'true'); |
| 225 | $xmlWriter->writeAttribute('style:footnote-max-height', '0cm'); |
| 226 | |
| 227 | $xmlWriter->startElement('style:footnote-sep'); |
| 228 | $xmlWriter->writeAttribute('style:width', '0.018cm'); |
| 229 | $xmlWriter->writeAttribute('style:line-style', 'solid'); |
| 230 | $xmlWriter->writeAttribute('style:adjustment', 'left'); |
| 231 | $xmlWriter->writeAttribute('style:rel-width', '25%'); |
| 232 | $xmlWriter->writeAttribute('style:color', '#000000'); |
| 233 | $xmlWriter->endElement(); //style:footnote-sep |
| 234 | |
| 235 | $xmlWriter->endElement(); // style:page-layout-properties |
| 236 | |
| 237 | $xmlWriter->startElement('style:header-style'); |
| 238 | if ($topfactor < 1.0) { |
| 239 | $xmlWriter->startElement('style:header-footer-properties'); |
| 240 | $xmlWriter->writeAttribute('fo:min-height', $mtop); |
| 241 | $xmlWriter->writeAttribute('fo:margin-bottom', $mtop); |
| 242 | $xmlWriter->writeAttribute('style:dynamic-spacing', 'true'); |
| 243 | $xmlWriter->endElement(); // style:header-footer-properties |
| 244 | } |
| 245 | $xmlWriter->endElement(); // style:header-style |
| 246 | |
| 247 | $xmlWriter->startElement('style:footer-style'); |
| 248 | if ($botfactor < 1.0) { |
| 249 | $xmlWriter->startElement('style:header-footer-properties'); |
| 250 | $xmlWriter->writeAttribute('fo:min-height', $mbottom); |
| 251 | $xmlWriter->writeAttribute('fo:margin-top', $mbottom); |
| 252 | $xmlWriter->writeAttribute('style:dynamic-spacing', 'true'); |
| 253 | $xmlWriter->endElement(); // style:header-footer-properties |
| 254 | } |
| 255 | $xmlWriter->endElement(); // style:footer-style |
| 256 | |
| 257 | $xmlWriter->endElement(); // style:page-layout |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Write master style. |
| 262 | */ |
| 263 | private function writeMaster(XMLWriter $xmlWriter): void |
| 264 | { |
| 265 | $xmlWriter->startElement('office:master-styles'); |
| 266 | |
| 267 | $sections = $this->getParentWriter()->getPhpWord()->getSections(); |
| 268 | $countsects = count($sections); |
| 269 | for ($i = 0; $i < $countsects; ++$i) { |
| 270 | $iplus1 = $i + 1; |
| 271 | $xmlWriter->startElement('style:master-page'); |
| 272 | $xmlWriter->writeAttribute('style:name', "Standard$iplus1"); |
| 273 | $xmlWriter->writeAttribute('style:page-layout-name', "Mpm$iplus1"); |
| 274 | foreach ($sections[$i]->getHeaders() as $hdr) { |
| 275 | $this->writeHeaderFooter($xmlWriter, $hdr, true); |
| 276 | } |
| 277 | foreach ($sections[$i]->getFooters() as $hdr) { |
| 278 | $this->writeHeaderFooter($xmlWriter, $hdr, false); |
| 279 | } |
| 280 | $xmlWriter->endElement(); // style:master-page |
| 281 | } |
| 282 | $xmlWriter->endElement(); // office:master-styles |
| 283 | } |
| 284 | |
| 285 | private function writeHeaderFooter(XMLWriter $xmlWriter, Footer $container, bool $header): void |
| 286 | { |
| 287 | $type = $container->getType(); |
| 288 | $variants = [ |
| 289 | 'default' => $header ? 'style:header' : 'style:footer', |
| 290 | 'first' => $header ? 'style:header-first' : 'style:footer-first', |
| 291 | 'even' => $header ? 'style:header-left' : 'style:footer-left', |
| 292 | ]; |
| 293 | $elementName = $variants[$type] ?? $variants['default']; |
| 294 | |
| 295 | $xmlWriter->startElement($elementName); |
| 296 | foreach ($container->getElements() as $element) { |
| 297 | if (!$element instanceof \PhpOffice\PhpWord\Element\AbstractElement) { |
| 298 | continue; |
| 299 | } |
| 300 | $writerClass = str_replace('\\Element\\', '\\Writer\\ODText\\Element\\', get_class($element)); |
| 301 | if (class_exists($writerClass)) { |
| 302 | $writer = new $writerClass($xmlWriter, $element); |
| 303 | $writer->write(); |
| 304 | } |
| 305 | } |
| 306 | $xmlWriter->endElement(); |
| 307 | } |
| 308 | } |