17
17
18
18
namespace PhpOffice \PhpWord \Writer \ODText \Part ;
19
19
20
+ use PhpOffice \PhpWord \Element \Image ;
20
21
use PhpOffice \PhpWord \Element \Table ;
21
22
use PhpOffice \PhpWord \Element \Text ;
22
23
use PhpOffice \PhpWord \Element \TextRun ;
23
- use PhpOffice \PhpWord \Media ;
24
24
use PhpOffice \PhpWord \PhpWord ;
25
25
use PhpOffice \PhpWord \Shared \XMLWriter ;
26
26
use PhpOffice \PhpWord \Style ;
27
27
use PhpOffice \PhpWord \Style \Font ;
28
28
use PhpOffice \PhpWord \Style \Paragraph ;
29
+ use PhpOffice \PhpWord \Style \Table as TableStyle ;
29
30
use PhpOffice \PhpWord \Writer \ODText \Element \Container ;
30
31
use PhpOffice \PhpWord \Writer \ODText \Style \Paragraph as ParagraphStyleWriter ;
31
- use PhpOffice \PhpWord \Writer \ODText \Style \Section as SectionStyleWriter ;
32
32
33
33
/**
34
34
* ODText content part writer: content.xml
35
35
*/
36
36
class Content extends AbstractPart
37
37
{
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
+
38
48
/**
39
49
* Write part
40
50
*
@@ -56,15 +66,9 @@ public function write()
56
66
$ xmlWriter ->writeAttribute ('xmlns:field ' , 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0 ' );
57
67
$ xmlWriter ->writeAttribute ('xmlns:formx ' , 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0 ' );
58
68
69
+ // Font declarations and automatic styles
59
70
$ 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
68
72
69
73
// Body
70
74
$ xmlWriter ->startElement ('office:body ' );
@@ -102,20 +106,24 @@ public function write()
102
106
}
103
107
104
108
/**
105
- * Write section automatic styles
109
+ * Write automatic styles other than fonts and paragraphs
106
110
*
107
111
* @since 0.11.0
108
- * @todo Put more section properties/styles
109
112
*/
110
- private function writeSectionStyles (XMLWriter $ xmlWriter, PhpWord $ phpWord )
113
+ private function writeAutoStyles (XMLWriter $ xmlWriter )
111
114
{
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 );
117
123
$ styleWriter ->write ();
118
124
}
125
+
126
+ $ xmlWriter ->endElement (); // office:automatic-styles
119
127
}
120
128
121
129
/**
@@ -148,51 +156,6 @@ private function writeTextStyles(XMLWriter $xmlWriter)
148
156
}
149
157
}
150
158
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
-
196
159
/**
197
160
* Get automatic styles
198
161
*/
@@ -202,16 +165,22 @@ private function getAutoStyles(PhpWord $phpWord)
202
165
$ paragraphStyleCount = 0 ;
203
166
$ fontStyleCount = 0 ;
204
167
foreach ($ sections as $ section ) {
168
+ $ style = $ section ->getSettings ();
169
+ $ style ->setStyleName ("Section {$ section ->getSectionId ()}" );
170
+ $ this ->autoStyles ['Section ' ][] = $ style ;
205
171
$ this ->getContainerStyle ($ section , $ paragraphStyleCount , $ fontStyleCount );
206
172
}
207
173
}
208
174
209
175
/**
210
176
* Get all styles of each elements in container recursively
211
177
*
178
+ * Table style can be null or string of the style name
179
+ *
212
180
* @param \PhpOffice\PhpWord\Element\AbstractContainer $container
213
181
* @param int $paragraphStyleCount
214
182
* @param int $fontStyleCount
183
+ * @todo Simplify the logic
215
184
*/
216
185
private function getContainerStyle ($ container , &$ paragraphStyleCount , &$ fontStyleCount )
217
186
{
@@ -221,6 +190,19 @@ private function getContainerStyle($container, &$paragraphStyleCount, &$fontStyl
221
190
$ this ->getContainerStyle ($ element , $ paragraphStyleCount , $ fontStyleCount );
222
191
} elseif ($ element instanceof Text) {
223
192
$ 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 ;
224
206
}
225
207
}
226
208
}
0 commit comments