Skip to content

Commit 7d9bcd4

Browse files
committed
ODT: New Image and Table style writer
1 parent b1e16e4 commit 7d9bcd4

File tree

3 files changed

+143
-63
lines changed

3 files changed

+143
-63
lines changed

src/PhpWord/Writer/ODText/Part/Content.php

Lines changed: 45 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,34 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\ODText\Part;
1919

20+
use PhpOffice\PhpWord\Element\Image;
2021
use PhpOffice\PhpWord\Element\Table;
2122
use PhpOffice\PhpWord\Element\Text;
2223
use PhpOffice\PhpWord\Element\TextRun;
23-
use PhpOffice\PhpWord\Media;
2424
use PhpOffice\PhpWord\PhpWord;
2525
use PhpOffice\PhpWord\Shared\XMLWriter;
2626
use PhpOffice\PhpWord\Style;
2727
use PhpOffice\PhpWord\Style\Font;
2828
use PhpOffice\PhpWord\Style\Paragraph;
29+
use PhpOffice\PhpWord\Style\Table as TableStyle;
2930
use PhpOffice\PhpWord\Writer\ODText\Element\Container;
3031
use PhpOffice\PhpWord\Writer\ODText\Style\Paragraph as ParagraphStyleWriter;
31-
use PhpOffice\PhpWord\Writer\ODText\Style\Section as SectionStyleWriter;
3232

3333
/**
3434
* ODText content part writer: content.xml
3535
*/
3636
class Content extends AbstractPart
3737
{
38+
/**
39+
* Auto style collection
40+
*
41+
* Collect inline style information from section, image, and table elements
42+
*
43+
* @todo Merge font and paragraph styles
44+
* @var array
45+
*/
46+
private $autoStyles = array('Section' => array(), 'Image' => array(), 'Table' => array());
47+
3848
/**
3949
* Write part
4050
*
@@ -56,15 +66,9 @@ public function write()
5666
$xmlWriter->writeAttribute('xmlns:field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0');
5767
$xmlWriter->writeAttribute('xmlns:formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0');
5868

69+
// Font declarations and automatic styles
5970
$this->writeFontFaces($xmlWriter); // office:font-face-decls
60-
61-
// Automatic styles
62-
$xmlWriter->startElement('office:automatic-styles');
63-
$this->writeSectionStyles($xmlWriter, $phpWord);
64-
$this->writeTextStyles($xmlWriter);
65-
$this->writeImageStyles($xmlWriter);
66-
$this->writeTableStyles($xmlWriter, $phpWord);
67-
$xmlWriter->endElement(); // office:automatic-styles
71+
$this->writeAutoStyles($xmlWriter); // office:automatic-styles
6872

6973
// Body
7074
$xmlWriter->startElement('office:body');
@@ -102,20 +106,24 @@ public function write()
102106
}
103107

104108
/**
105-
* Write section automatic styles
109+
* Write automatic styles other than fonts and paragraphs
106110
*
107111
* @since 0.11.0
108-
* @todo Put more section properties/styles
109112
*/
110-
private function writeSectionStyles(XMLWriter $xmlWriter, PhpWord $phpWord)
113+
private function writeAutoStyles(XMLWriter $xmlWriter)
111114
{
112-
$sections = $phpWord->getSections();
113-
foreach ($sections as $section) {
114-
$style = $section->getSettings();
115-
$style->setStyleName("Section{$section->getSectionId()}");
116-
$styleWriter = new SectionStyleWriter($xmlWriter, $style);
115+
$xmlWriter->startElement('office:automatic-styles');
116+
117+
$this->writeTextStyles($xmlWriter);
118+
foreach ($this->autoStyles as $element => $style) {
119+
$writerClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Style\\' . $element;
120+
121+
/** @var \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle $styleWriter Type hint */
122+
$styleWriter = new $writerClass($xmlWriter, $style);
117123
$styleWriter->write();
118124
}
125+
126+
$xmlWriter->endElement(); // office:automatic-styles
119127
}
120128

121129
/**
@@ -148,51 +156,6 @@ private function writeTextStyles(XMLWriter $xmlWriter)
148156
}
149157
}
150158

151-
/**
152-
* Write image automatic styles
153-
*/
154-
private function writeImageStyles(XMLWriter $xmlWriter)
155-
{
156-
$images = Media::getElements('section');
157-
foreach ($images as $image) {
158-
if ($image['type'] == 'image') {
159-
$xmlWriter->startElement('style:style');
160-
$xmlWriter->writeAttribute('style:name', 'fr' . $image['rID']);
161-
$xmlWriter->writeAttribute('style:family', 'graphic');
162-
$xmlWriter->writeAttribute('style:parent-style-name', 'Graphics');
163-
$xmlWriter->startElement('style:graphic-properties');
164-
$xmlWriter->writeAttribute('style:vertical-pos', 'top');
165-
$xmlWriter->writeAttribute('style:vertical-rel', 'baseline');
166-
$xmlWriter->endElement(); // style:graphic-properties
167-
$xmlWriter->endElement(); // style:style
168-
}
169-
}
170-
}
171-
172-
/**
173-
* Write table automatic styles
174-
*/
175-
private function writeTableStyles(XMLWriter $xmlWriter, PhpWord $phpWord)
176-
{
177-
$sections = $phpWord->getSections();
178-
foreach ($sections as $section) {
179-
$elements = $section->getElements();
180-
foreach ($elements as $element) {
181-
if ($elements instanceof Table) {
182-
$xmlWriter->startElement('style:style');
183-
$xmlWriter->writeAttribute('style:name', $element->getElementId());
184-
$xmlWriter->writeAttribute('style:family', 'table');
185-
$xmlWriter->startElement('style:table-properties');
186-
//$xmlWriter->writeAttribute('style:width', 'table');
187-
$xmlWriter->writeAttribute('style:rel-width', 100);
188-
$xmlWriter->writeAttribute('table:align', 'center');
189-
$xmlWriter->endElement();
190-
$xmlWriter->endElement();
191-
}
192-
}
193-
}
194-
}
195-
196159
/**
197160
* Get automatic styles
198161
*/
@@ -202,16 +165,22 @@ private function getAutoStyles(PhpWord $phpWord)
202165
$paragraphStyleCount = 0;
203166
$fontStyleCount = 0;
204167
foreach ($sections as $section) {
168+
$style = $section->getSettings();
169+
$style->setStyleName("Section{$section->getSectionId()}");
170+
$this->autoStyles['Section'][] = $style;
205171
$this->getContainerStyle($section, $paragraphStyleCount, $fontStyleCount);
206172
}
207173
}
208174

209175
/**
210176
* Get all styles of each elements in container recursively
211177
*
178+
* Table style can be null or string of the style name
179+
*
212180
* @param \PhpOffice\PhpWord\Element\AbstractContainer $container
213181
* @param int $paragraphStyleCount
214182
* @param int $fontStyleCount
183+
* @todo Simplify the logic
215184
*/
216185
private function getContainerStyle($container, &$paragraphStyleCount, &$fontStyleCount)
217186
{
@@ -221,6 +190,19 @@ private function getContainerStyle($container, &$paragraphStyleCount, &$fontStyl
221190
$this->getContainerStyle($element, $paragraphStyleCount, $fontStyleCount);
222191
} elseif ($element instanceof Text) {
223192
$this->getElementStyle($element, $paragraphStyleCount, $fontStyleCount);
193+
} elseif ($element instanceof Image) {
194+
$style = $element->getStyle();
195+
$style->setStyleName('fr' . $element->getMediaIndex());
196+
$this->autoStyles['Image'][] = $style;
197+
} elseif ($element instanceof Table) {
198+
$style = $element->getStyle();
199+
if ($style === null) {
200+
$style = new TableStyle();
201+
} elseif (is_string($style)) {
202+
$style = Style::getStyle($style);
203+
}
204+
$style->setStyleName($element->getElementId());
205+
$this->autoStyles['Table'][] = $style;
224206
}
225207
}
226208
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\ODText\Style;
19+
20+
/**
21+
* Image style writer
22+
*
23+
* @since 0.11.0
24+
*/
25+
class Image extends AbstractStyle
26+
{
27+
/**
28+
* Write style
29+
*/
30+
public function write()
31+
{
32+
/** @var \PhpOffice\PhpWord\Style\Image $style Type hint */
33+
$style = $this->getStyle();
34+
if (!$style instanceof \PhpOffice\PhpWord\Style\Image) {
35+
return;
36+
}
37+
$xmlWriter = $this->getXmlWriter();
38+
39+
$xmlWriter->startElement('style:style');
40+
$xmlWriter->writeAttribute('style:name', $style->getStyleName());
41+
$xmlWriter->writeAttribute('style:family', 'graphic');
42+
$xmlWriter->writeAttribute('style:parent-style-name', 'Graphics');
43+
$xmlWriter->startElement('style:graphic-properties');
44+
$xmlWriter->writeAttribute('style:vertical-pos', 'top');
45+
$xmlWriter->writeAttribute('style:vertical-rel', 'baseline');
46+
$xmlWriter->endElement(); // style:graphic-properties
47+
$xmlWriter->endElement(); // style:style
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\ODText\Style;
19+
20+
/**
21+
* Table style writer
22+
*
23+
* @since 0.11.0
24+
*/
25+
class Table extends AbstractStyle
26+
{
27+
/**
28+
* Write style
29+
*/
30+
public function write()
31+
{
32+
/** @var \PhpOffice\PhpWord\Style\Table $style Type hint */
33+
$style = $this->getStyle();
34+
if (!$style instanceof \PhpOffice\PhpWord\Style\Table) {
35+
return;
36+
}
37+
$xmlWriter = $this->getXmlWriter();
38+
39+
$xmlWriter->startElement('style:style');
40+
$xmlWriter->writeAttribute('style:name', $style->getStyleName());
41+
$xmlWriter->writeAttribute('style:family', 'table');
42+
$xmlWriter->startElement('style:table-properties');
43+
//$xmlWriter->writeAttribute('style:width', 'table');
44+
$xmlWriter->writeAttribute('style:rel-width', 100);
45+
$xmlWriter->writeAttribute('table:align', 'center');
46+
$xmlWriter->endElement(); // style:table-properties
47+
$xmlWriter->endElement(); // style:style
48+
}
49+
}

0 commit comments

Comments
 (0)