• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PHPWord / 12976151089

26 Jan 2025 03:51PM UTC coverage: 96.916% (-0.005%) from 96.921%
12976151089

push

github

web-flow
Add default font color for Word (.docx) (#2700)

* Add default font color for Word (.docx)

Adds the abillity to add a default font color for generated .docx.

* fix format

* Update 1.4.0.md

Add default font color

* Update introduction.md

Add documentation default font color

* Update introduction.md

Correct default value

* add tests for SetGetDefaultFontColor

* debug test

* add defaultFontColor to FontTest

* Update src/PhpWord/PhpWord.php

As suggested

Co-authored-by: Progi1984 <progi1984@gmail.com>

* Update src/PhpWord/PhpWord.php

As suggested

Co-authored-by: Progi1984 <progi1984@gmail.com>

* Update 1.4.0.md

Add default font color

* Update introduction.md

Add documentation default font color

* Update introduction.md

Correct default value

* add tests for SetGetDefaultFontColor

* debug test

* add defaultFontColor to FontTest

* Update src/PhpWord/PhpWord.php

As suggested

Co-authored-by: Progi1984 <progi1984@gmail.com>

* Update src/PhpWord/PhpWord.php

As suggested

Co-authored-by: Progi1984 <progi1984@gmail.com>

* fix format

* add test for default color

* clean up

* cs fixer

---------

Co-authored-by: MichaelFrey <michael.frey@gmx.ch>
Co-authored-by: Progi1984 <progi1984@gmail.com>

12 of 13 new or added lines in 4 files covered. (92.31%)

12005 of 12387 relevant lines covered (96.92%)

34.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/src/PhpWord/Reader/Word2007/Styles.php
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\Reader\Word2007;
20

21
use PhpOffice\PhpWord\PhpWord;
22
use PhpOffice\PhpWord\Shared\XMLReader;
23
use PhpOffice\PhpWord\Style\Language;
24

25
/**
26
 * Styles reader.
27
 *
28
 * @since 0.10.0
29
 */
30
class Styles extends AbstractPart
31
{
32
    /**
33
     * Read styles.xml.
34
     */
35
    public function read(PhpWord $phpWord): void
36
    {
37
        $xmlReader = new XMLReader();
12✔
38
        $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
12✔
39

40
        $fontDefaults = $xmlReader->getElement('w:docDefaults/w:rPrDefault');
12✔
41
        if ($fontDefaults !== null) {
12✔
42
            $fontDefaultStyle = $this->readFontStyle($xmlReader, $fontDefaults);
12✔
43
            if ($fontDefaultStyle) {
12✔
44
                if (array_key_exists('name', $fontDefaultStyle)) {
12✔
45
                    $phpWord->setDefaultFontName($fontDefaultStyle['name']);
5✔
46
                }
47
                if (array_key_exists('size', $fontDefaultStyle)) {
12✔
48
                    $phpWord->setDefaultFontSize($fontDefaultStyle['size']);
9✔
49
                }
50
                if (array_key_exists('color', $fontDefaultStyle)) {
12✔
NEW
51
                    $phpWord->setDefaultFontColor($fontDefaultStyle['color']);
×
52
                }
53
                if (array_key_exists('lang', $fontDefaultStyle)) {
12✔
54
                    $phpWord->getSettings()->setThemeFontLang(new Language($fontDefaultStyle['lang']));
8✔
55
                }
56
            }
57
        }
58

59
        $paragraphDefaults = $xmlReader->getElement('w:docDefaults/w:pPrDefault');
12✔
60
        if ($paragraphDefaults !== null) {
12✔
61
            $paragraphDefaultStyle = $this->readParagraphStyle($xmlReader, $paragraphDefaults);
8✔
62
            if ($paragraphDefaultStyle != null) {
8✔
63
                $phpWord->setDefaultParagraphStyle($paragraphDefaultStyle);
3✔
64
            }
65
        }
66

67
        $nodes = $xmlReader->getElements('w:style');
12✔
68
        if ($nodes->length > 0) {
12✔
69
            foreach ($nodes as $node) {
12✔
70
                $type = $xmlReader->getAttribute('w:type', $node);
12✔
71
                $name = $xmlReader->getAttribute('w:val', $node, 'w:name');
12✔
72
                if (null === $name) {
12✔
73
                    $name = $xmlReader->getAttribute('w:styleId', $node);
×
74
                }
75
                $headingMatches = [];
12✔
76
                preg_match('/Heading\s*(\d)/i', $name, $headingMatches);
12✔
77
                // $default = ($xmlReader->getAttribute('w:default', $node) == 1);
78
                switch ($type) {
79
                    case 'paragraph':
12✔
80
                        $paragraphStyle = $this->readParagraphStyle($xmlReader, $node);
12✔
81
                        $fontStyle = $this->readFontStyle($xmlReader, $node);
12✔
82
                        if (!empty($headingMatches)) {
12✔
83
                            $phpWord->addTitleStyle($headingMatches[1], $fontStyle, $paragraphStyle);
4✔
84
                        } else {
85
                            if (empty($fontStyle)) {
11✔
86
                                if (is_array($paragraphStyle)) {
9✔
87
                                    $phpWord->addParagraphStyle($name, $paragraphStyle);
9✔
88
                                }
89
                            } else {
90
                                $phpWord->addFontStyle($name, $fontStyle, $paragraphStyle);
10✔
91
                            }
92
                        }
93

94
                        break;
12✔
95
                    case 'character':
7✔
96
                        $fontStyle = $this->readFontStyle($xmlReader, $node);
7✔
97
                        if (!empty($fontStyle)) {
7✔
98
                            $phpWord->addFontStyle($name, $fontStyle);
5✔
99
                        }
100

101
                        break;
7✔
102
                    case 'table':
7✔
103
                        $tStyle = $this->readTableStyle($xmlReader, $node);
7✔
104
                        if (!empty($tStyle)) {
7✔
105
                            $phpWord->addTableStyle($name, $tStyle);
7✔
106
                        }
107

108
                        break;
7✔
109
                }
110
            }
111
        }
112
    }
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc