Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.83% covered (success)
97.83%
45 / 46
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Styles
97.83% covered (success)
97.83%
45 / 46
0.00% covered (danger)
0.00%
0 / 1
19
0.00% covered (danger)
0.00%
0 / 1
 read
97.83% covered (success)
97.83%
45 / 46
0.00% covered (danger)
0.00%
0 / 1
19
1<?php
2/**
3 * This file is part of PHPWord - A pure PHP library for reading and writing
4 * word processing documents.
5 *
6 * PHPWord is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12 *
13 * @see         https://github.com/PHPOffice/PHPWord
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18namespace PhpOffice\PhpWord\Reader\Word2007;
19
20use PhpOffice\PhpWord\PhpWord;
21use PhpOffice\PhpWord\Shared\XMLReader;
22use PhpOffice\PhpWord\Style\Language;
23
24/**
25 * Styles reader.
26 *
27 * @since 0.10.0
28 */
29class Styles extends AbstractPart
30{
31    /**
32     * Read styles.xml.
33     */
34    public function read(PhpWord $phpWord): void
35    {
36        $xmlReader = new XMLReader();
37        $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
38
39        $fontDefaults = $xmlReader->getElement('w:docDefaults/w:rPrDefault');
40        if ($fontDefaults !== null) {
41            $fontDefaultStyle = $this->readFontStyle($xmlReader, $fontDefaults);
42            if ($fontDefaultStyle) {
43                if (array_key_exists('name', $fontDefaultStyle)) {
44                    $phpWord->setDefaultFontName($fontDefaultStyle['name']);
45                }
46                if (array_key_exists('size', $fontDefaultStyle)) {
47                    $phpWord->setDefaultFontSize($fontDefaultStyle['size']);
48                }
49                if (array_key_exists('lang', $fontDefaultStyle)) {
50                    $phpWord->getSettings()->setThemeFontLang(new Language($fontDefaultStyle['lang']));
51                }
52            }
53        }
54
55        $paragraphDefaults = $xmlReader->getElement('w:docDefaults/w:pPrDefault');
56        if ($paragraphDefaults !== null) {
57            $paragraphDefaultStyle = $this->readParagraphStyle($xmlReader, $paragraphDefaults);
58            if ($paragraphDefaultStyle != null) {
59                $phpWord->setDefaultParagraphStyle($paragraphDefaultStyle);
60            }
61        }
62
63        $nodes = $xmlReader->getElements('w:style');
64        if ($nodes->length > 0) {
65            foreach ($nodes as $node) {
66                $type = $xmlReader->getAttribute('w:type', $node);
67                $name = $xmlReader->getAttribute('w:val', $node, 'w:name');
68                if (null === $name) {
69                    $name = $xmlReader->getAttribute('w:styleId', $node);
70                }
71                $headingMatches = [];
72                preg_match('/Heading\s*(\d)/i', $name, $headingMatches);
73                // $default = ($xmlReader->getAttribute('w:default', $node) == 1);
74                switch ($type) {
75                    case 'paragraph':
76                        $paragraphStyle = $this->readParagraphStyle($xmlReader, $node);
77                        $fontStyle = $this->readFontStyle($xmlReader, $node);
78                        if (!empty($headingMatches)) {
79                            $phpWord->addTitleStyle($headingMatches[1], $fontStyle, $paragraphStyle);
80                        } else {
81                            if (empty($fontStyle)) {
82                                if (is_array($paragraphStyle)) {
83                                    $phpWord->addParagraphStyle($name, $paragraphStyle);
84                                }
85                            } else {
86                                $phpWord->addFontStyle($name, $fontStyle, $paragraphStyle);
87                            }
88                        }
89
90                        break;
91                    case 'character':
92                        $fontStyle = $this->readFontStyle($xmlReader, $node);
93                        if (!empty($fontStyle)) {
94                            $phpWord->addFontStyle($name, $fontStyle);
95                        }
96
97                        break;
98                    case 'table':
99                        $tStyle = $this->readTableStyle($xmlReader, $node);
100                        if (!empty($tStyle)) {
101                            $phpWord->addTableStyle($name, $tStyle);
102                        }
103
104                        break;
105                }
106            }
107        }
108    }
109}