Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
158 / 158 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
Styles | |
100.00% |
158 / 158 |
|
100.00% |
7 / 7 |
31 | |
100.00% |
1 / 1 |
write | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
writeDefault | |
100.00% |
36 / 36 |
|
100.00% |
1 / 1 |
8 | |||
writeNamed | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
cvttwiptostr | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
writePageLayout | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
writePageLayoutIndiv | |
100.00% |
62 / 62 |
|
100.00% |
1 / 1 |
5 | |||
writeMaster | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
8 |
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\ODText\Part; |
20 | |
21 | use PhpOffice\PhpWord\Settings; |
22 | use PhpOffice\PhpWord\Shared\Converter; |
23 | use PhpOffice\PhpWord\Shared\XMLWriter; |
24 | use PhpOffice\PhpWord\Style; |
25 | |
26 | /** |
27 | * ODText styles part writer: styles.xml. |
28 | */ |
29 | class Styles extends AbstractPart |
30 | { |
31 | /** |
32 | * Write part. |
33 | * |
34 | * @return string |
35 | */ |
36 | public function write() |
37 | { |
38 | $xmlWriter = $this->getXmlWriter(); |
39 | |
40 | // XML header |
41 | $xmlWriter->startDocument('1.0', 'UTF-8'); |
42 | $xmlWriter->startElement('office:document-styles'); |
43 | $this->writeCommonRootAttributes($xmlWriter); |
44 | |
45 | // Font declarations |
46 | $this->writeFontFaces($xmlWriter); |
47 | |
48 | // Office styles |
49 | $xmlWriter->startElement('office:styles'); |
50 | $this->writeDefault($xmlWriter); |
51 | $this->writeNamed($xmlWriter); |
52 | $xmlWriter->endElement(); |
53 | |
54 | // Automatic styles |
55 | $xmlWriter->startElement('office:automatic-styles'); |
56 | $this->writePageLayout($xmlWriter); |
57 | $xmlWriter->endElement(); // office:automatic-styles |
58 | |
59 | // Master style |
60 | $this->writeMaster($xmlWriter); |
61 | |
62 | $xmlWriter->endElement(); // office:document-styles |
63 | |
64 | return $xmlWriter->getData(); |
65 | } |
66 | |
67 | /** |
68 | * Write default styles. |
69 | */ |
70 | private function writeDefault(XMLWriter $xmlWriter): void |
71 | { |
72 | $xmlWriter->startElement('style:default-style'); |
73 | $xmlWriter->writeAttribute('style:family', 'paragraph'); |
74 | |
75 | // Paragraph |
76 | $xmlWriter->startElement('style:paragraph-properties'); |
77 | $xmlWriter->writeAttribute('fo:hyphenation-ladder-count', 'no-limit'); |
78 | $xmlWriter->writeAttribute('style:text-autospace', 'ideograph-alpha'); |
79 | $xmlWriter->writeAttribute('style:punctuation-wrap', 'hanging'); |
80 | $xmlWriter->writeAttribute('style:line-break', 'strict'); |
81 | $xmlWriter->writeAttribute('style:tab-stop-distance', '1.249cm'); |
82 | $xmlWriter->writeAttribute('style:writing-mode', 'page'); |
83 | $xmlWriter->endElement(); // style:paragraph-properties |
84 | |
85 | $language = $this->getParentWriter()->getPhpWord()->getSettings()->getThemeFontLang(); |
86 | $latinLang = $language != null && is_string($language->getLatin()) ? explode('-', $language->getLatin()) : ['fr', 'FR']; |
87 | $asianLang = $language != null && is_string($language->getEastAsia()) ? explode('-', $language->getEastAsia()) : ['zh', 'CN']; |
88 | $complexLang = $language != null && is_string($language->getBidirectional()) ? explode('-', $language->getBidirectional()) : ['hi', 'IN']; |
89 | if ($this->getParentWriter()->getPhpWord()->getSettings()->hasHideGrammaticalErrors()) { |
90 | $latinLang = $asianLang = $complexLang = ['zxx', 'none']; |
91 | } |
92 | |
93 | // Font |
94 | $xmlWriter->startElement('style:text-properties'); |
95 | $xmlWriter->writeAttribute('style:use-window-font-color', 'true'); |
96 | $xmlWriter->writeAttribute('style:font-name', Settings::getDefaultFontName()); |
97 | $xmlWriter->writeAttribute('fo:font-size', Settings::getDefaultFontSize() . 'pt'); |
98 | $xmlWriter->writeAttribute('fo:language', $latinLang[0]); |
99 | $xmlWriter->writeAttribute('fo:country', $latinLang[1]); |
100 | $xmlWriter->writeAttribute('style:letter-kerning', 'true'); |
101 | $xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2'); |
102 | $xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt'); |
103 | $xmlWriter->writeAttribute('style:language-asian', $asianLang[0]); |
104 | $xmlWriter->writeAttribute('style:country-asian', $asianLang[1]); |
105 | $xmlWriter->writeAttribute('style:font-name-complex', Settings::getDefaultFontName() . '2'); |
106 | $xmlWriter->writeAttribute('style:font-size-complex', Settings::getDefaultFontSize() . 'pt'); |
107 | $xmlWriter->writeAttribute('style:language-complex', $complexLang[0]); |
108 | $xmlWriter->writeAttribute('style:country-complex', $complexLang[1]); |
109 | $xmlWriter->writeAttribute('fo:hyphenate', 'false'); |
110 | $xmlWriter->writeAttribute('fo:hyphenation-remain-char-count', '2'); |
111 | $xmlWriter->writeAttribute('fo:hyphenation-push-char-count', '2'); |
112 | $xmlWriter->endElement(); // style:text-properties |
113 | |
114 | $xmlWriter->endElement(); // style:default-style |
115 | } |
116 | |
117 | /** |
118 | * Write named styles. |
119 | */ |
120 | private function writeNamed(XMLWriter $xmlWriter): void |
121 | { |
122 | $styles = Style::getStyles(); |
123 | if (count($styles) > 0) { |
124 | foreach ($styles as $style) { |
125 | if ($style->isAuto() === false) { |
126 | $styleClass = str_replace('\\Style\\', '\\Writer\\ODText\\Style\\', get_class($style)); |
127 | if (class_exists($styleClass)) { |
128 | /** @var \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle $styleWriter Type hint */ |
129 | $styleWriter = new $styleClass($xmlWriter, $style); |
130 | $styleWriter->write(); |
131 | } |
132 | } |
133 | } |
134 | } |
135 | } |
136 | |
137 | /** |
138 | * Convert int in twips to inches/cm then to string and append unit. |
139 | * |
140 | * @param float|int $twips |
141 | * @param float $factor |
142 | * return string |
143 | */ |
144 | private static function cvttwiptostr($twips, $factor = 1.0) |
145 | { |
146 | $ins = (string) ($twips * $factor / Converter::INCH_TO_TWIP) . 'in'; |
147 | $cms = (string) ($twips * $factor * Converter::INCH_TO_CM / Converter::INCH_TO_TWIP) . 'cm'; |
148 | |
149 | return (strlen($ins) < strlen($cms)) ? $ins : $cms; |
150 | } |
151 | |
152 | /** |
153 | * call writePageLayoutIndiv to write page layout styles for each page. |
154 | */ |
155 | private function writePageLayout(XMLWriter $xmlWriter): void |
156 | { |
157 | $sections = $this->getParentWriter()->getPhpWord()->getSections(); |
158 | $countsects = count($sections); |
159 | for ($i = 0; $i < $countsects; ++$i) { |
160 | $this->writePageLayoutIndiv($xmlWriter, $sections[$i], $i + 1); |
161 | } |
162 | } |
163 | |
164 | /** |
165 | * Write page layout styles. |
166 | * |
167 | * @param \PhpOffice\PhpWord\Element\Section $section |
168 | * @param int $sectionNbr |
169 | */ |
170 | private function writePageLayoutIndiv(XMLWriter $xmlWriter, $section, $sectionNbr): void |
171 | { |
172 | $sty = $section->getStyle(); |
173 | if (count($section->getHeaders()) > 0) { |
174 | $topfactor = 0.5; |
175 | } else { |
176 | $topfactor = 1.0; |
177 | } |
178 | if (count($section->getFooters()) > 0) { |
179 | $botfactor = 0.5; |
180 | } else { |
181 | $botfactor = 1.0; |
182 | } |
183 | $orient = $sty->getOrientation(); |
184 | $pwidth = self::cvttwiptostr($sty->getPageSizeW()); |
185 | $pheight = self::cvttwiptostr($sty->getPageSizeH()); |
186 | $mtop = self::cvttwiptostr($sty->getMarginTop(), $topfactor); |
187 | $mbottom = self::cvttwiptostr($sty->getMarginBottom(), $botfactor); |
188 | $mleft = self::cvttwiptostr($sty->getMarginRight()); |
189 | $mright = self::cvttwiptostr($sty->getMarginLeft()); |
190 | |
191 | $xmlWriter->startElement('style:page-layout'); |
192 | $xmlWriter->writeAttribute('style:name', "Mpm$sectionNbr"); |
193 | |
194 | $xmlWriter->startElement('style:page-layout-properties'); |
195 | $xmlWriter->writeAttribute('fo:page-width', $pwidth); |
196 | $xmlWriter->writeAttribute('fo:page-height', $pheight); |
197 | $xmlWriter->writeAttribute('style:num-format', '1'); |
198 | $xmlWriter->writeAttribute('style:print-orientation', $orient); |
199 | $xmlWriter->writeAttribute('fo:margin-top', $mtop); |
200 | $xmlWriter->writeAttribute('fo:margin-bottom', $mbottom); |
201 | $xmlWriter->writeAttribute('fo:margin-left', $mleft); |
202 | $xmlWriter->writeAttribute('fo:margin-right', $mright); |
203 | $xmlWriter->writeAttribute('style:writing-mode', 'lr-tb'); |
204 | $xmlWriter->writeAttribute('style:layout-grid-color', '#c0c0c0'); |
205 | $xmlWriter->writeAttribute('style:layout-grid-lines', '25199'); |
206 | $xmlWriter->writeAttribute('style:layout-grid-base-height', '0.423cm'); |
207 | $xmlWriter->writeAttribute('style:layout-grid-ruby-height', '0cm'); |
208 | $xmlWriter->writeAttribute('style:layout-grid-mode', 'none'); |
209 | $xmlWriter->writeAttribute('style:layout-grid-ruby-below', 'false'); |
210 | $xmlWriter->writeAttribute('style:layout-grid-print', 'false'); |
211 | $xmlWriter->writeAttribute('style:layout-grid-display', 'false'); |
212 | $xmlWriter->writeAttribute('style:layout-grid-base-width', '0.37cm'); |
213 | $xmlWriter->writeAttribute('style:layout-grid-snap-to', 'true'); |
214 | $xmlWriter->writeAttribute('style:footnote-max-height', '0cm'); |
215 | |
216 | $xmlWriter->startElement('style:footnote-sep'); |
217 | $xmlWriter->writeAttribute('style:width', '0.018cm'); |
218 | $xmlWriter->writeAttribute('style:line-style', 'solid'); |
219 | $xmlWriter->writeAttribute('style:adjustment', 'left'); |
220 | $xmlWriter->writeAttribute('style:rel-width', '25%'); |
221 | $xmlWriter->writeAttribute('style:color', '#000000'); |
222 | $xmlWriter->endElement(); //style:footnote-sep |
223 | |
224 | $xmlWriter->endElement(); // style:page-layout-properties |
225 | |
226 | $xmlWriter->startElement('style:header-style'); |
227 | if ($topfactor < 1.0) { |
228 | $xmlWriter->startElement('style:header-footer-properties'); |
229 | $xmlWriter->writeAttribute('fo:min-height', $mtop); |
230 | $xmlWriter->writeAttribute('fo:margin-bottom', $mtop); |
231 | $xmlWriter->writeAttribute('style:dynamic-spacing', 'true'); |
232 | $xmlWriter->endElement(); // style:header-footer-properties |
233 | } |
234 | $xmlWriter->endElement(); // style:header-style |
235 | |
236 | $xmlWriter->startElement('style:footer-style'); |
237 | if ($botfactor < 1.0) { |
238 | $xmlWriter->startElement('style:header-footer-properties'); |
239 | $xmlWriter->writeAttribute('fo:min-height', $mbottom); |
240 | $xmlWriter->writeAttribute('fo:margin-top', $mbottom); |
241 | $xmlWriter->writeAttribute('style:dynamic-spacing', 'true'); |
242 | $xmlWriter->endElement(); // style:header-footer-properties |
243 | } |
244 | $xmlWriter->endElement(); // style:footer-style |
245 | |
246 | $xmlWriter->endElement(); // style:page-layout |
247 | } |
248 | |
249 | /** |
250 | * Write master style. |
251 | */ |
252 | private function writeMaster(XMLWriter $xmlWriter): void |
253 | { |
254 | $xmlWriter->startElement('office:master-styles'); |
255 | |
256 | $sections = $this->getParentWriter()->getPhpWord()->getSections(); |
257 | $countsects = count($sections); |
258 | for ($i = 0; $i < $countsects; ++$i) { |
259 | $iplus1 = $i + 1; |
260 | $xmlWriter->startElement('style:master-page'); |
261 | $xmlWriter->writeAttribute('style:name', "Standard$iplus1"); |
262 | $xmlWriter->writeAttribute('style:page-layout-name', "Mpm$iplus1"); |
263 | // Multiple headers and footers probably not supported, |
264 | // and, even if they are, I'm not sure how, |
265 | // so quit after generating one. |
266 | foreach ($sections[$i]->getHeaders() as $hdr) { |
267 | $xmlWriter->startElement('style:header'); |
268 | foreach ($hdr->getElements() as $elem) { |
269 | $cl1 = get_class($elem); |
270 | $cl2 = str_replace('\\Element\\', '\\Writer\\ODText\\Element\\', $cl1); |
271 | if (class_exists($cl2)) { |
272 | $wtr = new $cl2($xmlWriter, $elem); |
273 | $wtr->write(); |
274 | } |
275 | } |
276 | $xmlWriter->endElement(); // style:header |
277 | |
278 | break; |
279 | } |
280 | foreach ($sections[$i]->getFooters() as $hdr) { |
281 | $xmlWriter->startElement('style:footer'); |
282 | foreach ($hdr->getElements() as $elem) { |
283 | $cl1 = get_class($elem); |
284 | $cl2 = str_replace('\\Element\\', '\\Writer\\ODText\\Element\\', $cl1); |
285 | if (class_exists($cl2)) { |
286 | $wtr = new $cl2($xmlWriter, $elem); |
287 | $wtr->write(); |
288 | } |
289 | } |
290 | $xmlWriter->endElement(); // style:footer |
291 | |
292 | break; |
293 | } |
294 | $xmlWriter->endElement(); // style:master-page |
295 | } |
296 | $xmlWriter->endElement(); // office:master-styles |
297 | } |
298 | } |