Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.00% covered (success)
98.00%
49 / 50
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Styles
98.00% covered (success)
98.00%
49 / 50
0.00% covered (danger)
0.00%
0 / 1
21
0.00% covered (danger)
0.00%
0 / 1
 read
98.00% covered (success)
98.00%
49 / 50
0.00% covered (danger)
0.00%
0 / 1
21
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                        } elseif (strtolower($name) === 'title') {
85                            $phpWord->addTitleStyle(0, $fontStyle, $paragraphStyle);
86                        } else {
87                            if (empty($fontStyle)) {
88                                if (is_array($paragraphStyle)) {
89                                    $phpWord->addParagraphStyle($name, $paragraphStyle);
90                                }
91                            } else {
92                                $phpWord->addFontStyle($name, $fontStyle, $paragraphStyle);
93                            }
94                        }
95
96                        break;
97                    case 'character':
98                        $fontStyle = $this->readFontStyle($xmlReader, $node);
99                        if (!empty($fontStyle)) {
100                            $phpWord->addFontStyle($name, $fontStyle);
101                        }
102
103                        break;
104                    case 'table':
105                        $tStyle = $this->readTableStyle($xmlReader, $node);
106                        if (!empty($tStyle)) {
107                            $phpWord->addTableStyle($name, $tStyle);
108                        }
109
110                        break;
111                }
112            }
113        }
114    }
115}