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