19
19
20
20
use PhpOffice \PhpWord \Exception \Exception ;
21
21
use PhpOffice \PhpWord \PhpWord ;
22
- use PhpOffice \PhpWord \Settings ;
23
- use PhpOffice \PhpWord \Shared \Drawing ;
24
- use PhpOffice \PhpWord \Style ;
25
- use PhpOffice \PhpWord \Style \Font ;
26
- use PhpOffice \PhpWord \Writer \RTF \Element \Container ;
27
22
28
23
/**
29
24
* RTF writer
32
27
*/
33
28
class RTF extends AbstractWriter implements WriterInterface
34
29
{
35
- /**
36
- * Color register
37
- *
38
- * @var array
39
- */
40
- private $ colorTable ;
41
-
42
- /**
43
- * Font register
44
- *
45
- * @var array
46
- */
47
- private $ fontTable ;
48
-
49
30
/**
50
31
* Last paragraph style
51
32
*
@@ -60,6 +41,18 @@ class RTF extends AbstractWriter implements WriterInterface
60
41
public function __construct (PhpWord $ phpWord = null )
61
42
{
62
43
$ this ->setPhpWord ($ phpWord );
44
+
45
+ $ this ->parts = array ('Header ' , 'Document ' );
46
+ foreach ($ this ->parts as $ partName ) {
47
+ $ partClass = get_class ($ this ) . '\\Part \\' . $ partName ;
48
+ if (class_exists ($ partClass )) {
49
+ /** @var \PhpOffice\PhpWord\Writer\RTF\Part\AbstractPart $part Type hint */
50
+ $ part = new $ partClass ();
51
+ $ part ->setParentWriter ($ this );
52
+ $ this ->writerParts [strtolower ($ partName )] = $ part ;
53
+ }
54
+ }
55
+
63
56
}
64
57
65
58
/**
@@ -70,10 +63,17 @@ public function __construct(PhpWord $phpWord = null)
70
63
*/
71
64
public function save ($ filename = null )
72
65
{
66
+ $ content = '' ;
73
67
$ filename = $ this ->getTempFile ($ filename );
74
68
$ hFile = fopen ($ filename , 'w ' );
75
69
if ($ hFile !== false ) {
76
- fwrite ($ hFile , $ this ->writeDocument ());
70
+ $ content .= '{ ' ;
71
+ $ content .= '\rtf1 ' . PHP_EOL ;
72
+ $ content .= $ this ->getWriterPart ('Header ' )->write ();
73
+ $ content .= $ this ->getWriterPart ('Document ' )->write ();
74
+ $ content .= '} ' ;
75
+
76
+ fwrite ($ hFile , $ content );
77
77
fclose ($ hFile );
78
78
} else {
79
79
throw new Exception ("Can't open file " );
@@ -82,19 +82,19 @@ public function save($filename = null)
82
82
}
83
83
84
84
/**
85
- * Get color table
85
+ * Get font table
86
86
*/
87
- public function getColorTable ()
87
+ public function getFontTable ()
88
88
{
89
- return $ this ->colorTable ;
89
+ return $ this ->getWriterPart ( ' Header ' )-> getFontTable () ;
90
90
}
91
91
92
92
/**
93
- * Get font table
93
+ * Get color table
94
94
*/
95
- public function getFontTable ()
95
+ public function getColorTable ()
96
96
{
97
- return $ this ->fontTable ;
97
+ return $ this ->getWriterPart ( ' Header ' )-> getColorTable () ;
98
98
}
99
99
100
100
/**
@@ -114,172 +114,4 @@ public function setLastParagraphStyle($value = '')
114
114
{
115
115
$ this ->lastParagraphStyle = $ value ;
116
116
}
117
-
118
- /**
119
- * Get all data
120
- *
121
- * @return string
122
- */
123
- private function writeDocument ()
124
- {
125
- $ this ->fontTable = $ this ->populateFontTable ();
126
- $ this ->colorTable = $ this ->populateColorTable ();
127
-
128
- // Set the default character set
129
- $ content = '{\rtf1 ' ;
130
- $ content .= '\ansi\ansicpg1252 ' ; // Set the default font (the first one)
131
- $ content .= '\deff0 ' ; // Set the default tab size (720 twips)
132
- $ content .= '\deftab720 ' ;
133
- $ content .= PHP_EOL ;
134
-
135
- // Set the font tbl group
136
- $ content .= '{\fonttbl ' ;
137
- foreach ($ this ->fontTable as $ idx => $ font ) {
138
- $ content .= '{\f ' . $ idx . '\fnil\fcharset0 ' . $ font . ';} ' ;
139
- }
140
- $ content .= '} ' . PHP_EOL ;
141
-
142
- // Set the color tbl group
143
- $ content .= '{\colortbl ' ;
144
- foreach ($ this ->colorTable as $ color ) {
145
- $ arrColor = Drawing::htmlToRGB ($ color );
146
- $ content .= ';\red ' . $ arrColor [0 ] . '\green ' . $ arrColor [1 ] . '\blue ' . $ arrColor [2 ] . '' ;
147
- }
148
- $ content .= ';} ' . PHP_EOL ;
149
-
150
- $ content .= '{\*\generator PhpWord;} ' . PHP_EOL ; // Set the generator
151
- $ content .= '\viewkind4 ' ; // Set the view mode of the document
152
- $ content .= '\uc1 ' ; // Set the numberof bytes that follows a unicode character
153
- $ content .= '\pard ' ; // Resets to default paragraph properties.
154
- $ content .= '\nowidctlpar ' ; // No widow/orphan control
155
- $ content .= '\lang1036 ' ; // Applies a language to a text run (1036 : French (France))
156
- $ content .= '\kerning1 ' ; // Point size (in half-points) above which to kern character pairs
157
- $ content .= '\fs ' . (Settings::getDefaultFontSize () * 2 ); // Set the font size in half-points
158
- $ content .= PHP_EOL ;
159
-
160
- // Body
161
- $ content .= $ this ->writeContent ();
162
-
163
- $ content .= '} ' ;
164
-
165
- return $ content ;
166
- }
167
-
168
- /**
169
- * Get content data
170
- *
171
- * @return string
172
- */
173
- private function writeContent ()
174
- {
175
- $ content = '' ;
176
-
177
- $ sections = $ this ->getPhpWord ()->getSections ();
178
- foreach ($ sections as $ section ) {
179
- $ writer = new Container ($ this , $ section );
180
- $ content .= $ writer ->write ();
181
- }
182
-
183
- return $ content ;
184
- }
185
-
186
- /**
187
- * Get all fonts
188
- *
189
- * @return array
190
- */
191
- private function populateFontTable ()
192
- {
193
- $ phpWord = $ this ->getPhpWord ();
194
- $ fontTable = array ();
195
- $ fontTable [] = Settings::getDefaultFontName ();
196
-
197
- // Browse styles
198
- $ styles = Style::getStyles ();
199
- if (count ($ styles ) > 0 ) {
200
- foreach ($ styles as $ style ) {
201
- // Font
202
- if ($ style instanceof Font) {
203
- if (in_array ($ style ->getName (), $ fontTable ) == false ) {
204
- $ fontTable [] = $ style ->getName ();
205
- }
206
- }
207
- }
208
- }
209
-
210
- // Search all fonts used
211
- $ sections = $ phpWord ->getSections ();
212
- $ countSections = count ($ sections );
213
- if ($ countSections > 0 ) {
214
- foreach ($ sections as $ section ) {
215
- $ elements = $ section ->getElements ();
216
- foreach ($ elements as $ element ) {
217
- if (method_exists ($ element , 'getFontStyle ' )) {
218
- $ fontStyle = $ element ->getFontStyle ();
219
- if ($ fontStyle instanceof Font) {
220
- if (in_array ($ fontStyle ->getName (), $ fontTable ) == false ) {
221
- $ fontTable [] = $ fontStyle ->getName ();
222
- }
223
- }
224
- }
225
- }
226
- }
227
- }
228
-
229
- return $ fontTable ;
230
- }
231
-
232
- /**
233
- * Get all colors
234
- *
235
- * @return array
236
- */
237
- private function populateColorTable ()
238
- {
239
- $ phpWord = $ this ->getPhpWord ();
240
- $ defaultFontColor = Settings::DEFAULT_FONT_COLOR ;
241
- $ colorTable = array ();
242
-
243
- // Browse styles
244
- $ styles = Style::getStyles ();
245
- if (count ($ styles ) > 0 ) {
246
- foreach ($ styles as $ style ) {
247
- // Font
248
- if ($ style instanceof Font) {
249
- $ color = $ style ->getColor ();
250
- $ fgcolor = $ style ->getFgColor ();
251
- if (!in_array ($ color , $ colorTable ) && $ color != $ defaultFontColor && !empty ($ color )) {
252
- $ colorTable [] = $ color ;
253
- }
254
- if (!in_array ($ fgcolor , $ colorTable ) && $ fgcolor != $ defaultFontColor && !empty ($ fgcolor )) {
255
- $ colorTable [] = $ fgcolor ;
256
- }
257
- }
258
- }
259
- }
260
-
261
- // Search all fonts used
262
- $ sections = $ phpWord ->getSections ();
263
- $ countSections = count ($ sections );
264
- if ($ countSections > 0 ) {
265
- foreach ($ sections as $ section ) {
266
- $ elements = $ section ->getElements ();
267
- foreach ($ elements as $ element ) {
268
- if (method_exists ($ element , 'getFontStyle ' )) {
269
- $ fontStyle = $ element ->getFontStyle ();
270
- if ($ fontStyle instanceof Font) {
271
- if (in_array ($ fontStyle ->getColor (), $ colorTable ) == false ) {
272
- $ colorTable [] = $ fontStyle ->getColor ();
273
- }
274
- if (in_array ($ fontStyle ->getFgColor (), $ colorTable ) == false ) {
275
- $ colorTable [] = $ fontStyle ->getFgColor ();
276
- }
277
- }
278
- }
279
- }
280
- }
281
- }
282
-
283
- return $ colorTable ;
284
- }
285
117
}
0 commit comments