Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.83% covered (success)
95.83%
46 / 48
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Styles
95.83% covered (success)
95.83%
46 / 48
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 1
 read
95.83% covered (success)
95.83%
46 / 48
0.00% covered (danger)
0.00%
0 / 1
20
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\Reader\Word2007;
20
21use PhpOffice\PhpWord\PhpWord;
22use PhpOffice\PhpWord\Shared\XMLReader;
23use PhpOffice\PhpWord\Style\Language;
24
25/**
26 * Styles reader.
27 *
28 * @since 0.10.0
29 */
30class Styles extends AbstractPart
31{
32    /**
33     * Read styles.xml.
34     */
35    public function read(PhpWord $phpWord): void
36    {
37        $xmlReader = new XMLReader();
38        $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
39
40        $fontDefaults = $xmlReader->getElement('w:docDefaults/w:rPrDefault');
41        if ($fontDefaults !== null) {
42            $fontDefaultStyle = $this->readFontStyle($xmlReader, $fontDefaults);
43            if ($fontDefaultStyle) {
44                if (array_key_exists('name', $fontDefaultStyle)) {
45                    $phpWord->setDefaultFontName($fontDefaultStyle['name']);
46                }
47                if (array_key_exists('size', $fontDefaultStyle)) {
48                    $phpWord->setDefaultFontSize($fontDefaultStyle['size']);
49                }
50                if (array_key_exists('color', $fontDefaultStyle)) {
51                    $phpWord->setDefaultFontColor($fontDefaultStyle['color']);
52                }
53                if (array_key_exists('lang', $fontDefaultStyle)) {
54                    $phpWord->getSettings()->setThemeFontLang(new Language($fontDefaultStyle['lang']));
55                }
56            }
57        }
58
59        $paragraphDefaults = $xmlReader->getElement('w:docDefaults/w:pPrDefault');
60        if ($paragraphDefaults !== null) {
61            $paragraphDefaultStyle = $this->readParagraphStyle($xmlReader, $paragraphDefaults);
62            if ($paragraphDefaultStyle != null) {
63                $phpWord->setDefaultParagraphStyle($paragraphDefaultStyle);
64            }
65        }
66
67        $nodes = $xmlReader->getElements('w:style');
68        if ($nodes->length > 0) {
69            foreach ($nodes as $node) {
70                $type = $xmlReader->getAttribute('w:type', $node);
71                $name = $xmlReader->getAttribute('w:val', $node, 'w:name');
72                if (null === $name) {
73                    $name = $xmlReader->getAttribute('w:styleId', $node);
74                }
75                $headingMatches = [];
76                preg_match('/Heading\s*(\d)/i', $name, $headingMatches);
77                // $default = ($xmlReader->getAttribute('w:default', $node) == 1);
78                switch ($type) {
79                    case 'paragraph':
80                        $paragraphStyle = $this->readParagraphStyle($xmlReader, $node);
81                        $fontStyle = $this->readFontStyle($xmlReader, $node);
82                        if (!empty($headingMatches)) {
83                            $phpWord->addTitleStyle($headingMatches[1], $fontStyle, $paragraphStyle);
84                        } else {
85                            if (empty($fontStyle)) {
86                                if (is_array($paragraphStyle)) {
87                                    $phpWord->addParagraphStyle($name, $paragraphStyle);
88                                }
89                            } else {
90                                $phpWord->addFontStyle($name, $fontStyle, $paragraphStyle);
91                            }
92                        }
93
94                        break;
95                    case 'character':
96                        $fontStyle = $this->readFontStyle($xmlReader, $node);
97                        if (!empty($fontStyle)) {
98                            $phpWord->addFontStyle($name, $fontStyle);
99                        }
100
101                        break;
102                    case 'table':
103                        $tStyle = $this->readTableStyle($xmlReader, $node);
104                        if (!empty($tStyle)) {
105                            $phpWord->addTableStyle($name, $tStyle);
106                        }
107
108                        break;
109                }
110            }
111        }
112    }
113}