Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.06% covered (success)
93.06%
67 / 72
83.33% covered (warning)
83.33%
10 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Header
93.06% covered (success)
93.06%
67 / 72
83.33% covered (warning)
83.33%
10 / 12
25.21
0.00% covered (danger)
0.00%
0 / 1
 getFontTable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getColorTable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 write
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 writeCharset
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 writeDefaults
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 writeFontTable
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 writeColorTable
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 writeGenerator
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 registerFont
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 registerBorderColor
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 registerFontItems
66.67% covered (warning)
66.67%
8 / 12
0.00% covered (danger)
0.00%
0 / 1
3.33
 registerTableItem
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
4
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\Writer\RTF\Part;
20
21use PhpOffice\PhpWord\Settings;
22use PhpOffice\PhpWord\Shared\Converter;
23use PhpOffice\PhpWord\Style;
24use PhpOffice\PhpWord\Style\Font;
25use 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 */
40class 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;
64    }
65
66    /**
67     * Get color table.
68     *
69     * @return array
70     */
71    public function getColorTable()
72    {
73        return $this->colorTable;
74    }
75
76    /**
77     * Write part.
78     *
79     * @return string
80     */
81    public function write()
82    {
83        $this->registerFont();
84
85        $content = '';
86
87        $content .= $this->writeCharset();
88        $content .= $this->writeDefaults();
89        $content .= $this->writeFontTable();
90        $content .= $this->writeColorTable();
91        $content .= $this->writeGenerator();
92        $content .= PHP_EOL;
93
94        return $content;
95    }
96
97    /**
98     * Write character set.
99     *
100     * @return string
101     */
102    private function writeCharset()
103    {
104        $content = '';
105
106        $content .= '\ansi';
107        $content .= '\ansicpg1252';
108        $content .= PHP_EOL;
109
110        return $content;
111    }
112
113    /**
114     * Write header defaults.
115     *
116     * @return string
117     */
118    private function writeDefaults()
119    {
120        $content = '';
121
122        $content .= '\deff0';
123        $content .= PHP_EOL;
124
125        return $content;
126    }
127
128    /**
129     * Write font table.
130     *
131     * @return string
132     */
133    private function writeFontTable()
134    {
135        $content = '';
136
137        $content .= '{';
138        $content .= '\fonttbl';
139        foreach ($this->fontTable as $index => $font) {
140            $content .= "{\\f{$index}\\fnil\\fcharset0 {$font};}";
141        }
142        $content .= '}';
143        $content .= PHP_EOL;
144
145        return $content;
146    }
147
148    /**
149     * Write color table.
150     *
151     * @return string
152     */
153    private function writeColorTable()
154    {
155        $content = '';
156
157        $content .= '{';
158        $content .= '\colortbl;';
159        foreach ($this->colorTable as $color) {
160            [$red, $green, $blue] = Converter::htmlToRgb($color);
161            $content .= "\\red{$red}\\green{$green}\\blue{$blue};";
162        }
163        $content .= '}';
164        $content .= PHP_EOL;
165
166        return $content;
167    }
168
169    /**
170     * Write.
171     *
172     * @return string
173     */
174    private function writeGenerator()
175    {
176        $content = '';
177
178        $content .= '{\*\generator PHPWord;}'; // Set the generator
179        $content .= PHP_EOL;
180
181        return $content;
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();
190        $this->fontTable[] = Settings::getDefaultFontName();
191
192        // Search named styles
193        $styles = Style::getStyles();
194        foreach ($styles as $style) {
195            $this->registerFontItems($style);
196        }
197
198        // Search inline styles
199        $sections = $phpWord->getSections();
200        foreach ($sections as $section) {
201            $elements = $section->getElements();
202            $this->registerBorderColor($section->getStyle());
203            foreach ($elements as $element) {
204                if (method_exists($element, 'getFontStyle')) {
205                    $style = $element->getFontStyle();
206                    $this->registerFontItems($style);
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();
220        foreach ($colors as $color) {
221            if ($color !== null) {
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();
235        $defaultColor = Settings::DEFAULT_FONT_COLOR;
236
237        if ($style instanceof Font) {
238            $this->registerTableItem($this->fontTable, $style->getName(), $defaultFont);
239            $this->registerTableItem($this->colorTable, $style->getColor(), $defaultColor);
240            $this->registerTableItem($this->colorTable, $style->getFgColor(), $defaultColor);
241
242            return;
243        }
244        if ($style instanceof Table) {
245            $this->registerTableItem($this->colorTable, $style->getBorderTopColor(), $defaultColor);
246            $this->registerTableItem($this->colorTable, $style->getBorderRightColor(), $defaultColor);
247            $this->registerTableItem($this->colorTable, $style->getBorderLeftColor(), $defaultColor);
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) {
262            $table[] = $value;
263        }
264    }
265}