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

PHPOffice / PHPWord / 17396170466

02 Sep 2025 07:14AM UTC coverage: 96.925% (+0.2%) from 96.757%
17396170466

Pull #2567

github

web-flow
Merge 3223dba6c into 0ab0b4940
Pull Request #2567: WIP Do Not Install

308 of 313 new or added lines in 28 files covered. (98.4%)

4 existing lines in 1 file now uncovered.

12704 of 13107 relevant lines covered (96.93%)

37.38 hits per line

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

93.06
/src/PhpWord/Writer/RTF/Part/Header.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\Writer\RTF\Part;
20

21
use PhpOffice\PhpWord\Settings;
22
use PhpOffice\PhpWord\Shared\Converter;
23
use PhpOffice\PhpWord\Style;
24
use PhpOffice\PhpWord\Style\Font;
25
use PhpOffice\PhpWord\Style\Table;
26

27
/**
28
 * RTF header part writer.
29
 *
30
 * - Character set
31
 * - Font table
32
 * - File table (not supported yet)
33
 * - Color table
34
 * - Style sheet (not supported yet)
35
 * - List table (not supported yet)
36
 *
37
 * @since 0.11.0
38
 * @see  http://www.biblioscape.com/rtf15_spec.htm#Heading6
39
 */
40
class Header extends AbstractPart
41
{
42
    /**
43
     * Font table.
44
     *
45
     * @var array
46
     */
47
    private $fontTable = [];
48

49
    /**
50
     * Color table.
51
     *
52
     * @var array
53
     */
54
    private $colorTable = [];
55

56
    /**
57
     * Get font table.
58
     *
59
     * @return array
60
     */
61
    public function getFontTable()
62
    {
63
        return $this->fontTable;
1✔
64
    }
65

66
    /**
67
     * Get color table.
68
     *
69
     * @return array
70
     */
71
    public function getColorTable()
72
    {
73
        return $this->colorTable;
4✔
74
    }
75

76
    /**
77
     * Write part.
78
     *
79
     * @return string
80
     */
81
    public function write()
82
    {
83
        $this->registerFont();
3✔
84

85
        $content = '';
3✔
86

87
        $content .= $this->writeCharset();
3✔
88
        $content .= $this->writeDefaults();
3✔
89
        $content .= $this->writeFontTable();
3✔
90
        $content .= $this->writeColorTable();
3✔
91
        $content .= $this->writeGenerator();
3✔
92
        $content .= PHP_EOL;
3✔
93

94
        return $content;
3✔
95
    }
96

97
    /**
98
     * Write character set.
99
     *
100
     * @return string
101
     */
102
    private function writeCharset()
103
    {
104
        $content = '';
3✔
105

106
        $content .= '\ansi';
3✔
107
        $content .= '\ansicpg1252';
3✔
108
        $content .= PHP_EOL;
3✔
109

110
        return $content;
3✔
111
    }
112

113
    /**
114
     * Write header defaults.
115
     *
116
     * @return string
117
     */
118
    private function writeDefaults()
119
    {
120
        $content = '';
3✔
121

122
        $content .= '\deff0';
3✔
123
        $content .= PHP_EOL;
3✔
124

125
        return $content;
3✔
126
    }
127

128
    /**
129
     * Write font table.
130
     *
131
     * @return string
132
     */
133
    private function writeFontTable()
134
    {
135
        $content = '';
3✔
136

137
        $content .= '{';
3✔
138
        $content .= '\fonttbl';
3✔
139
        foreach ($this->fontTable as $index => $font) {
3✔
140
            $content .= "{\\f{$index}\\fnil\\fcharset0 {$font};}";
3✔
141
        }
142
        $content .= '}';
3✔
143
        $content .= PHP_EOL;
3✔
144

145
        return $content;
3✔
146
    }
147

148
    /**
149
     * Write color table.
150
     *
151
     * @return string
152
     */
153
    private function writeColorTable()
154
    {
155
        $content = '';
3✔
156

157
        $content .= '{';
3✔
158
        $content .= '\colortbl;';
3✔
159
        foreach ($this->colorTable as $color) {
3✔
160
            [$red, $green, $blue] = Converter::htmlToRgb($color);
1✔
161
            $content .= "\\red{$red}\\green{$green}\\blue{$blue};";
1✔
162
        }
163
        $content .= '}';
3✔
164
        $content .= PHP_EOL;
3✔
165

166
        return $content;
3✔
167
    }
168

169
    /**
170
     * Write.
171
     *
172
     * @return string
173
     */
174
    private function writeGenerator()
175
    {
176
        $content = '';
3✔
177

178
        $content .= '{\*\generator PHPWord;}'; // Set the generator
3✔
179
        $content .= PHP_EOL;
3✔
180

181
        return $content;
3✔
182
    }
183

184
    /**
185
     * Register all fonts and colors in both named and inline styles to appropriate header table.
186
     */
187
    private function registerFont(): void
188
    {
189
        $phpWord = $this->getParentWriter()->getPhpWord();
3✔
190
        $this->fontTable[] = Settings::getDefaultFontName();
3✔
191

192
        // Search named styles
193
        $styles = Style::getStyles();
3✔
194
        foreach ($styles as $style) {
3✔
195
            $this->registerFontItems($style);
1✔
196
        }
197

198
        // Search inline styles
199
        $sections = $phpWord->getSections();
3✔
200
        foreach ($sections as $section) {
3✔
201
            $elements = $section->getElements();
3✔
202
            $this->registerBorderColor($section->getStyle());
3✔
203
            foreach ($elements as $element) {
3✔
204
                if (method_exists($element, 'getFontStyle')) {
3✔
205
                    $style = $element->getFontStyle();
2✔
206
                    $this->registerFontItems($style);
2✔
207
                }
208
            }
209
        }
210
    }
211

212
    /**
213
     * Register border colors.
214
     *
215
     * @param Style\Border $style
216
     */
217
    private function registerBorderColor($style): void
218
    {
219
        $colors = $style->getBorderColor();
3✔
220
        foreach ($colors as $color) {
3✔
221
            if ($color !== null) {
3✔
222
                $this->registerTableItem($this->colorTable, $color);
×
223
            }
224
        }
225
    }
226

227
    /**
228
     * Register fonts and colors.
229
     *
230
     * @param Style\AbstractStyle $style
231
     */
232
    private function registerFontItems($style): void
233
    {
234
        $defaultFont = Settings::getDefaultFontName();
2✔
235
        $defaultColor = Settings::DEFAULT_FONT_COLOR;
2✔
236

237
        if ($style instanceof Font) {
2✔
238
            $this->registerTableItem($this->fontTable, $style->getName(), $defaultFont);
2✔
239
            $this->registerTableItem($this->colorTable, $style->getColor(), $defaultColor);
2✔
240
            $this->registerTableItem($this->colorTable, $style->getFgColor(), $defaultColor);
2✔
241

242
            return;
2✔
243
        }
244
        if ($style instanceof Table) {
1✔
UNCOV
245
            $this->registerTableItem($this->colorTable, $style->getBorderTopColor(), $defaultColor);
×
UNCOV
246
            $this->registerTableItem($this->colorTable, $style->getBorderRightColor(), $defaultColor);
×
UNCOV
247
            $this->registerTableItem($this->colorTable, $style->getBorderLeftColor(), $defaultColor);
×
UNCOV
248
            $this->registerTableItem($this->colorTable, $style->getBorderBottomColor(), $defaultColor);
×
249
        }
250
    }
251

252
    /**
253
     * Register individual font and color.
254
     *
255
     * @param array &$table
256
     * @param string $value
257
     * @param string $default
258
     */
259
    private function registerTableItem(&$table, $value, $default = null): void
260
    {
261
        if (in_array($value, $table) === false && $value !== null && $value != $default) {
2✔
262
            $table[] = $value;
1✔
263
        }
264
    }
265
}
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

© 2025 Coveralls, Inc