Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.06% |
67 / 72 |
|
83.33% |
10 / 12 |
CRAP | |
0.00% |
0 / 1 |
Header | |
93.06% |
67 / 72 |
|
83.33% |
10 / 12 |
25.21 | |
0.00% |
0 / 1 |
getFontTable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getColorTable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
write | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
writeCharset | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
writeDefaults | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
writeFontTable | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
writeColorTable | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
writeGenerator | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
registerFont | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
registerBorderColor | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
registerFontItems | |
66.67% |
8 / 12 |
|
0.00% |
0 / 1 |
3.33 | |||
registerTableItem | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPWord - A pure PHP library for reading and writing |
4 | * word processing documents. |
5 | * |
6 | * PHPWord is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPWord |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord\Writer\RTF\Part; |
19 | |
20 | use PhpOffice\PhpWord\Settings; |
21 | use PhpOffice\PhpWord\Shared\Converter; |
22 | use PhpOffice\PhpWord\Style; |
23 | use PhpOffice\PhpWord\Style\Font; |
24 | use PhpOffice\PhpWord\Style\Table; |
25 | |
26 | /** |
27 | * RTF header part writer. |
28 | * |
29 | * - Character set |
30 | * - Font table |
31 | * - File table (not supported yet) |
32 | * - Color table |
33 | * - Style sheet (not supported yet) |
34 | * - List table (not supported yet) |
35 | * |
36 | * @since 0.11.0 |
37 | * @see http://www.biblioscape.com/rtf15_spec.htm#Heading6 |
38 | */ |
39 | class Header extends AbstractPart |
40 | { |
41 | /** |
42 | * Font table. |
43 | * |
44 | * @var array |
45 | */ |
46 | private $fontTable = []; |
47 | |
48 | /** |
49 | * Color table. |
50 | * |
51 | * @var array |
52 | */ |
53 | private $colorTable = []; |
54 | |
55 | /** |
56 | * Get font table. |
57 | * |
58 | * @return array |
59 | */ |
60 | public function getFontTable() |
61 | { |
62 | return $this->fontTable; |
63 | } |
64 | |
65 | /** |
66 | * Get color table. |
67 | * |
68 | * @return array |
69 | */ |
70 | public function getColorTable() |
71 | { |
72 | return $this->colorTable; |
73 | } |
74 | |
75 | /** |
76 | * Write part. |
77 | * |
78 | * @return string |
79 | */ |
80 | public function write() |
81 | { |
82 | $this->registerFont(); |
83 | |
84 | $content = ''; |
85 | |
86 | $content .= $this->writeCharset(); |
87 | $content .= $this->writeDefaults(); |
88 | $content .= $this->writeFontTable(); |
89 | $content .= $this->writeColorTable(); |
90 | $content .= $this->writeGenerator(); |
91 | $content .= PHP_EOL; |
92 | |
93 | return $content; |
94 | } |
95 | |
96 | /** |
97 | * Write character set. |
98 | * |
99 | * @return string |
100 | */ |
101 | private function writeCharset() |
102 | { |
103 | $content = ''; |
104 | |
105 | $content .= '\ansi'; |
106 | $content .= '\ansicpg1252'; |
107 | $content .= PHP_EOL; |
108 | |
109 | return $content; |
110 | } |
111 | |
112 | /** |
113 | * Write header defaults. |
114 | * |
115 | * @return string |
116 | */ |
117 | private function writeDefaults() |
118 | { |
119 | $content = ''; |
120 | |
121 | $content .= '\deff0'; |
122 | $content .= PHP_EOL; |
123 | |
124 | return $content; |
125 | } |
126 | |
127 | /** |
128 | * Write font table. |
129 | * |
130 | * @return string |
131 | */ |
132 | private function writeFontTable() |
133 | { |
134 | $content = ''; |
135 | |
136 | $content .= '{'; |
137 | $content .= '\fonttbl'; |
138 | foreach ($this->fontTable as $index => $font) { |
139 | $content .= "{\\f{$index}\\fnil\\fcharset0 {$font};}"; |
140 | } |
141 | $content .= '}'; |
142 | $content .= PHP_EOL; |
143 | |
144 | return $content; |
145 | } |
146 | |
147 | /** |
148 | * Write color table. |
149 | * |
150 | * @return string |
151 | */ |
152 | private function writeColorTable() |
153 | { |
154 | $content = ''; |
155 | |
156 | $content .= '{'; |
157 | $content .= '\colortbl;'; |
158 | foreach ($this->colorTable as $color) { |
159 | [$red, $green, $blue] = Converter::htmlToRgb($color); |
160 | $content .= "\\red{$red}\\green{$green}\\blue{$blue};"; |
161 | } |
162 | $content .= '}'; |
163 | $content .= PHP_EOL; |
164 | |
165 | return $content; |
166 | } |
167 | |
168 | /** |
169 | * Write. |
170 | * |
171 | * @return string |
172 | */ |
173 | private function writeGenerator() |
174 | { |
175 | $content = ''; |
176 | |
177 | $content .= '{\*\generator PHPWord;}'; // Set the generator |
178 | $content .= PHP_EOL; |
179 | |
180 | return $content; |
181 | } |
182 | |
183 | /** |
184 | * Register all fonts and colors in both named and inline styles to appropriate header table. |
185 | */ |
186 | private function registerFont(): void |
187 | { |
188 | $phpWord = $this->getParentWriter()->getPhpWord(); |
189 | $this->fontTable[] = Settings::getDefaultFontName(); |
190 | |
191 | // Search named styles |
192 | $styles = Style::getStyles(); |
193 | foreach ($styles as $style) { |
194 | $this->registerFontItems($style); |
195 | } |
196 | |
197 | // Search inline styles |
198 | $sections = $phpWord->getSections(); |
199 | foreach ($sections as $section) { |
200 | $elements = $section->getElements(); |
201 | $this->registerBorderColor($section->getStyle()); |
202 | foreach ($elements as $element) { |
203 | if (method_exists($element, 'getFontStyle')) { |
204 | $style = $element->getFontStyle(); |
205 | $this->registerFontItems($style); |
206 | } |
207 | } |
208 | } |
209 | } |
210 | |
211 | /** |
212 | * Register border colors. |
213 | * |
214 | * @param \PhpOffice\PhpWord\Style\Border $style |
215 | */ |
216 | private function registerBorderColor($style): void |
217 | { |
218 | $colors = $style->getBorderColor(); |
219 | foreach ($colors as $color) { |
220 | if ($color !== null) { |
221 | $this->registerTableItem($this->colorTable, $color); |
222 | } |
223 | } |
224 | } |
225 | |
226 | /** |
227 | * Register fonts and colors. |
228 | * |
229 | * @param \PhpOffice\PhpWord\Style\AbstractStyle $style |
230 | */ |
231 | private function registerFontItems($style): void |
232 | { |
233 | $defaultFont = Settings::getDefaultFontName(); |
234 | $defaultColor = Settings::DEFAULT_FONT_COLOR; |
235 | |
236 | if ($style instanceof Font) { |
237 | $this->registerTableItem($this->fontTable, $style->getName(), $defaultFont); |
238 | $this->registerTableItem($this->colorTable, $style->getColor(), $defaultColor); |
239 | $this->registerTableItem($this->colorTable, $style->getFgColor(), $defaultColor); |
240 | |
241 | return; |
242 | } |
243 | if ($style instanceof Table) { |
244 | $this->registerTableItem($this->colorTable, $style->getBorderTopColor(), $defaultColor); |
245 | $this->registerTableItem($this->colorTable, $style->getBorderRightColor(), $defaultColor); |
246 | $this->registerTableItem($this->colorTable, $style->getBorderLeftColor(), $defaultColor); |
247 | $this->registerTableItem($this->colorTable, $style->getBorderBottomColor(), $defaultColor); |
248 | } |
249 | } |
250 | |
251 | /** |
252 | * Register individual font and color. |
253 | * |
254 | * @param array &$table |
255 | * @param string $value |
256 | * @param string $default |
257 | */ |
258 | private function registerTableItem(&$table, $value, $default = null): void |
259 | { |
260 | if (in_array($value, $table) === false && $value !== null && $value != $default) { |
261 | $table[] = $value; |
262 | } |
263 | } |
264 | } |