Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
208 / 208 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
Content | |
100.00% |
208 / 208 |
|
100.00% |
9 / 9 |
49 | |
100.00% |
1 / 1 |
write | |
100.00% |
64 / 64 |
|
100.00% |
1 / 1 |
9 | |||
writeAutoStyles | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
writeTextStyles | |
100.00% |
42 / 42 |
|
100.00% |
1 / 1 |
9 | |||
getAutoStyles | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
getContainerStyle | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
9 | |||
getElementStyle | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
6 | |||
getElementStyleField | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
getElementStyleTextRun | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
4 | |||
collectTrackedChanges | |
100.00% |
6 / 6 |
|
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 | |
19 | namespace PhpOffice\PhpWord\Writer\ODText\Part; |
20 | |
21 | use PhpOffice\PhpWord\Element\AbstractContainer; |
22 | use PhpOffice\PhpWord\Element\Field; |
23 | use PhpOffice\PhpWord\Element\Image; |
24 | use PhpOffice\PhpWord\Element\Table; |
25 | use PhpOffice\PhpWord\Element\Text; |
26 | use PhpOffice\PhpWord\Element\TextRun; |
27 | use PhpOffice\PhpWord\Element\TrackChange; |
28 | use PhpOffice\PhpWord\PhpWord; |
29 | use PhpOffice\PhpWord\Shared\XMLWriter; |
30 | use PhpOffice\PhpWord\Style; |
31 | use PhpOffice\PhpWord\Style\Font; |
32 | use PhpOffice\PhpWord\Style\Paragraph; |
33 | use PhpOffice\PhpWord\Style\Table as TableStyle; |
34 | use PhpOffice\PhpWord\Writer\ODText\Element\Container; |
35 | use PhpOffice\PhpWord\Writer\ODText\Style\Paragraph as ParagraphStyleWriter; |
36 | |
37 | /** |
38 | * ODText content part writer: content.xml. |
39 | */ |
40 | class Content extends AbstractPart |
41 | { |
42 | /** |
43 | * Auto style collection. |
44 | * |
45 | * Collect inline style information from section, image, and table elements |
46 | * |
47 | * @todo Merge font and paragraph styles |
48 | * |
49 | * @var array |
50 | */ |
51 | private $autoStyles = ['Section' => [], 'Image' => [], 'Table' => []]; |
52 | |
53 | private $imageParagraphStyles = []; |
54 | |
55 | /** |
56 | * Write part. |
57 | * |
58 | * @return string |
59 | */ |
60 | public function write() |
61 | { |
62 | $xmlWriter = $this->getXmlWriter(); |
63 | $phpWord = $this->getParentWriter()->getPhpWord(); |
64 | |
65 | $this->getAutoStyles($phpWord); |
66 | |
67 | $xmlWriter->startDocument('1.0', 'UTF-8'); |
68 | $xmlWriter->startElement('office:document-content'); |
69 | $this->writeCommonRootAttributes($xmlWriter); |
70 | $xmlWriter->writeAttribute('xmlns:xforms', 'http://www.w3.org/2002/xforms'); |
71 | $xmlWriter->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema'); |
72 | $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); |
73 | $xmlWriter->writeAttribute('xmlns:field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0'); |
74 | $xmlWriter->writeAttribute('xmlns:formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0'); |
75 | |
76 | // Font declarations and automatic styles |
77 | $this->writeFontFaces($xmlWriter); // office:font-face-decls |
78 | $this->writeAutoStyles($xmlWriter); // office:automatic-styles |
79 | |
80 | // Body |
81 | $xmlWriter->startElement('office:body'); |
82 | $xmlWriter->startElement('office:text'); |
83 | |
84 | // Tracked changes declarations |
85 | $trackedChanges = []; |
86 | $sections = $phpWord->getSections(); |
87 | foreach ($sections as $section) { |
88 | $this->collectTrackedChanges($section, $trackedChanges); |
89 | } |
90 | $xmlWriter->startElement('text:tracked-changes'); |
91 | foreach ($trackedChanges as $trackedElement) { |
92 | $trackedChange = $trackedElement->getTrackChange(); |
93 | $xmlWriter->startElement('text:changed-region'); |
94 | $trackedChange->setElementId(); |
95 | $xmlWriter->writeAttribute('text:id', $trackedChange->getElementId()); |
96 | |
97 | if (($trackedChange->getChangeType() == TrackChange::INSERTED)) { |
98 | $xmlWriter->startElement('text:insertion'); |
99 | } elseif ($trackedChange->getChangeType() == TrackChange::DELETED) { |
100 | $xmlWriter->startElement('text:deletion'); |
101 | } |
102 | |
103 | $xmlWriter->startElement('office:change-info'); |
104 | $xmlWriter->writeElement('dc:creator', $trackedChange->getAuthor()); |
105 | if ($trackedChange->getDate() != null) { |
106 | $xmlWriter->writeElement('dc:date', $trackedChange->getDate()->format('Y-m-d\TH:i:s\Z')); |
107 | } |
108 | $xmlWriter->endElement(); // office:change-info |
109 | if ($trackedChange->getChangeType() == TrackChange::DELETED) { |
110 | $xmlWriter->writeElement('text:p', $trackedElement->getText()); |
111 | } |
112 | |
113 | $xmlWriter->endElement(); // text:insertion|text:deletion |
114 | $xmlWriter->endElement(); // text:changed-region |
115 | } |
116 | $xmlWriter->endElement(); // text:tracked-changes |
117 | |
118 | // Sequence declarations |
119 | $sequences = ['Illustration', 'Table', 'Text', 'Drawing']; |
120 | $xmlWriter->startElement('text:sequence-decls'); |
121 | foreach ($sequences as $sequence) { |
122 | $xmlWriter->startElement('text:sequence-decl'); |
123 | $xmlWriter->writeAttribute('text:display-outline-level', 0); |
124 | $xmlWriter->writeAttribute('text:name', $sequence); |
125 | $xmlWriter->endElement(); |
126 | } |
127 | $xmlWriter->endElement(); // text:sequence-decl |
128 | |
129 | // Sections |
130 | $sections = $phpWord->getSections(); |
131 | foreach ($sections as $section) { |
132 | $name = 'Section' . $section->getSectionId(); |
133 | $xmlWriter->startElement('text:section'); |
134 | $xmlWriter->writeAttribute('text:name', $name); |
135 | $xmlWriter->writeAttribute('text:style-name', $name); |
136 | $xmlWriter->startElement('text:p'); |
137 | $xmlWriter->writeAttribute('text:style-name', 'SB' . $section->getSectionId()); |
138 | $xmlWriter->endElement(); |
139 | |
140 | $containerWriter = new Container($xmlWriter, $section); |
141 | $containerWriter->setPart($this); |
142 | $containerWriter->write(); |
143 | |
144 | $xmlWriter->endElement(); // text:section |
145 | } |
146 | |
147 | $xmlWriter->endElement(); // office:text |
148 | $xmlWriter->endElement(); // office:body |
149 | |
150 | $xmlWriter->endElement(); // office:document-content |
151 | |
152 | return $xmlWriter->getData(); |
153 | } |
154 | |
155 | /** |
156 | * Write automatic styles other than fonts and paragraphs. |
157 | * |
158 | * @since 0.11.0 |
159 | */ |
160 | private function writeAutoStyles(XMLWriter $xmlWriter): void |
161 | { |
162 | $xmlWriter->startElement('office:automatic-styles'); |
163 | |
164 | $this->writeTextStyles($xmlWriter); |
165 | foreach ($this->autoStyles as $element => $styles) { |
166 | $writerClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Style\\' . $element; |
167 | foreach ($styles as $style) { |
168 | /** @var \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle $styleWriter Type hint */ |
169 | $styleWriter = new $writerClass($xmlWriter, $style); |
170 | $styleWriter->write(); |
171 | } |
172 | } |
173 | |
174 | $xmlWriter->endElement(); // office:automatic-styles |
175 | } |
176 | |
177 | /** |
178 | * Write automatic styles. |
179 | */ |
180 | private function writeTextStyles(XMLWriter $xmlWriter): void |
181 | { |
182 | $styles = Style::getStyles(); |
183 | $paragraphStyleCount = 0; |
184 | |
185 | $style = new Paragraph(); |
186 | $style->setStyleName('PB'); |
187 | $style->setAuto(); |
188 | $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); |
189 | $styleWriter->write(); |
190 | |
191 | $sects = $this->getParentWriter()->getPhpWord()->getSections(); |
192 | $countsects = count($sects); |
193 | for ($i = 0; $i < $countsects; ++$i) { |
194 | $iplus1 = $i + 1; |
195 | $style = new Paragraph(); |
196 | $style->setStyleName("SB$iplus1"); |
197 | $style->setAuto(); |
198 | $pnstart = $sects[$i]->getStyle()->getPageNumberingStart(); |
199 | $style->setNumLevel($pnstart); |
200 | $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); |
201 | $styleWriter->write(); |
202 | } |
203 | |
204 | foreach ($styles as $style) { |
205 | $sty = (string) $style->getStyleName(); |
206 | if (substr($sty, 0, 8) === 'Heading_') { |
207 | $style = new Paragraph(); |
208 | $style->setStyleName('HD' . substr($sty, 8)); |
209 | $style->setAuto(); |
210 | $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); |
211 | $styleWriter->write(); |
212 | $style = new Paragraph(); |
213 | $style->setStyleName('HE' . substr($sty, 8)); |
214 | $style->setAuto(); |
215 | $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); |
216 | $styleWriter->write(); |
217 | } |
218 | } |
219 | |
220 | foreach ($styles as $style) { |
221 | if ($style->isAuto() === true) { |
222 | $styleClass = str_replace('\\Style\\', '\\Writer\\ODText\\Style\\', get_class($style)); |
223 | if (class_exists($styleClass)) { |
224 | /** @var \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle $styleWriter Type hint */ |
225 | $styleWriter = new $styleClass($xmlWriter, $style); |
226 | $styleWriter->write(); |
227 | } |
228 | if ($style instanceof Paragraph) { |
229 | ++$paragraphStyleCount; |
230 | } |
231 | } |
232 | } |
233 | foreach ($this->imageParagraphStyles as $style) { |
234 | $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); |
235 | $styleWriter->write(); |
236 | } |
237 | } |
238 | |
239 | /** |
240 | * Get automatic styles. |
241 | */ |
242 | private function getAutoStyles(PhpWord $phpWord): void |
243 | { |
244 | $sections = $phpWord->getSections(); |
245 | $paragraphStyleCount = 0; |
246 | $fontStyleCount = 0; |
247 | foreach ($sections as $section) { |
248 | $style = $section->getStyle(); |
249 | $style->setStyleName("Section{$section->getSectionId()}"); |
250 | $this->autoStyles['Section'][] = $style; |
251 | $this->getContainerStyle($section, $paragraphStyleCount, $fontStyleCount); |
252 | } |
253 | } |
254 | |
255 | /** |
256 | * Get all styles of each elements in container recursively. |
257 | * |
258 | * Table style can be null or string of the style name |
259 | * |
260 | * @param AbstractContainer $container |
261 | * @param int $paragraphStyleCount |
262 | * @param int $fontStyleCount |
263 | * |
264 | * @todo Simplify the logic |
265 | */ |
266 | private function getContainerStyle($container, &$paragraphStyleCount, &$fontStyleCount): void |
267 | { |
268 | $elements = $container->getElements(); |
269 | foreach ($elements as $element) { |
270 | if ($element instanceof TextRun) { |
271 | $this->getElementStyleTextRun($element, $paragraphStyleCount); |
272 | $this->getContainerStyle($element, $paragraphStyleCount, $fontStyleCount); |
273 | } elseif ($element instanceof Text) { |
274 | $this->getElementStyle($element, $paragraphStyleCount, $fontStyleCount); |
275 | } elseif ($element instanceof Field) { |
276 | $this->getElementStyleField($element, $fontStyleCount); |
277 | } elseif ($element instanceof Image) { |
278 | $style = $element->getStyle(); |
279 | $style->setStyleName('fr' . $element->getMediaIndex()); |
280 | $this->autoStyles['Image'][] = $style; |
281 | $sty = new Paragraph(); |
282 | $sty->setStyleName('IM' . $element->getMediaIndex()); |
283 | $sty->setAuto(); |
284 | $sty->setAlignment($style->getAlignment()); |
285 | $this->imageParagraphStyles[] = $sty; |
286 | } elseif ($element instanceof Table) { |
287 | $style = $element->getStyle(); |
288 | if (is_string($style)) { |
289 | $style = Style::getStyle($style); |
290 | } |
291 | if ($style === null) { |
292 | $style = new TableStyle(); |
293 | } |
294 | $style->setStyleName($element->getElementId()); |
295 | $style->setColumnWidths($element->findFirstDefinedCellWidths()); |
296 | $this->autoStyles['Table'][] = $style; |
297 | } |
298 | } |
299 | } |
300 | |
301 | /** |
302 | * Get style of individual element. |
303 | * |
304 | * @param Text $element |
305 | * @param int $paragraphStyleCount |
306 | * @param int $fontStyleCount |
307 | */ |
308 | private function getElementStyle($element, &$paragraphStyleCount, &$fontStyleCount): void |
309 | { |
310 | $fontStyle = $element->getFontStyle(); |
311 | $paragraphStyle = $element->getParagraphStyle(); |
312 | $phpWord = $this->getParentWriter()->getPhpWord(); |
313 | |
314 | if ($fontStyle instanceof Font) { |
315 | // Font |
316 | $name = $fontStyle->getStyleName(); |
317 | if (!$name) { |
318 | ++$fontStyleCount; |
319 | $style = $phpWord->addFontStyle("T{$fontStyleCount}", $fontStyle, null); |
320 | $style->setAuto(); |
321 | $style->setParagraph(null); |
322 | $element->setFontStyle("T{$fontStyleCount}"); |
323 | } else { |
324 | $element->setFontStyle($name); |
325 | } |
326 | } |
327 | if ($paragraphStyle instanceof Paragraph) { |
328 | // Paragraph |
329 | $name = $paragraphStyle->getStyleName(); |
330 | if (!$name) { |
331 | ++$paragraphStyleCount; |
332 | $style = $phpWord->addParagraphStyle("P{$paragraphStyleCount}", $paragraphStyle); |
333 | $style->setAuto(); |
334 | $element->setParagraphStyle("P{$paragraphStyleCount}"); |
335 | } else { |
336 | $element->setParagraphStyle($name); |
337 | } |
338 | } elseif ($paragraphStyle) { |
339 | ++$paragraphStyleCount; |
340 | $parstylename = "P$paragraphStyleCount" . "_$paragraphStyle"; |
341 | $style = $phpWord->addParagraphStyle($parstylename, $paragraphStyle); |
342 | $style->setAuto(); |
343 | $element->setParagraphStyle($parstylename); |
344 | } |
345 | } |
346 | |
347 | /** |
348 | * Get font style of individual field element. |
349 | * |
350 | * @param Field $element |
351 | * @param int $fontStyleCount |
352 | */ |
353 | private function getElementStyleField($element, &$fontStyleCount): void |
354 | { |
355 | $fontStyle = $element->getFontStyle(); |
356 | $phpWord = $this->getParentWriter()->getPhpWord(); |
357 | |
358 | if ($fontStyle instanceof Font) { |
359 | $name = $fontStyle->getStyleName(); |
360 | if (!$name) { |
361 | ++$fontStyleCount; |
362 | $style = $phpWord->addFontStyle("T{$fontStyleCount}", $fontStyle, null); |
363 | $style->setAuto(); |
364 | $style->setParagraph(null); |
365 | $element->setFontStyle("T{$fontStyleCount}"); |
366 | } else { |
367 | $element->setFontStyle($name); |
368 | } |
369 | } |
370 | } |
371 | |
372 | /** |
373 | * Get style of individual element. |
374 | * |
375 | * @param TextRun $element |
376 | * @param int $paragraphStyleCount |
377 | */ |
378 | private function getElementStyleTextRun($element, &$paragraphStyleCount): void |
379 | { |
380 | $paragraphStyle = $element->getParagraphStyle(); |
381 | $phpWord = $this->getParentWriter()->getPhpWord(); |
382 | |
383 | if ($paragraphStyle instanceof Paragraph) { |
384 | // Paragraph |
385 | $name = $paragraphStyle->getStyleName(); |
386 | if (!$name) { |
387 | ++$paragraphStyleCount; |
388 | $style = $phpWord->addParagraphStyle("P{$paragraphStyleCount}", $paragraphStyle); |
389 | $style->setAuto(); |
390 | $element->setParagraphStyle("P{$paragraphStyleCount}"); |
391 | } else { |
392 | $element->setParagraphStyle($name); |
393 | } |
394 | } elseif ($paragraphStyle) { |
395 | ++$paragraphStyleCount; |
396 | $parstylename = "P$paragraphStyleCount" . "_$paragraphStyle"; |
397 | $style = $phpWord->addParagraphStyle($parstylename, $paragraphStyle); |
398 | $style->setAuto(); |
399 | $element->setParagraphStyle($parstylename); |
400 | } |
401 | } |
402 | |
403 | /** |
404 | * Finds all tracked changes. |
405 | * |
406 | * @param \PhpOffice\PhpWord\Element\AbstractElement[] $trackedChanges |
407 | */ |
408 | private function collectTrackedChanges(AbstractContainer $container, &$trackedChanges = []): void |
409 | { |
410 | $elements = $container->getElements(); |
411 | foreach ($elements as $element) { |
412 | if ($element->getTrackChange() != null) { |
413 | $trackedChanges[] = $element; |
414 | } |
415 | if (is_callable([$element, 'getElements'])) { |
416 | $this->collectTrackedChanges($element, $trackedChanges); |
417 | } |
418 | } |
419 | } |
420 | } |