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