33namespace Box \Spout \Writer \ODS \Manager \Style ;
44
55use Box \Spout \Common \Entity \Style \BorderPart ;
6+ use Box \Spout \Common \Entity \Style \CellAlignment ;
67use Box \Spout \Writer \Common \Entity \Worksheet ;
78use Box \Spout \Writer \ODS \Helper \BorderHelper ;
89
@@ -199,6 +200,7 @@ protected function getStyleSectionContent($style)
199200 $ content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce ' . $ styleIndex . '" style:parent-style-name="Default"> ' ;
200201
201202 $ content .= $ this ->getTextPropertiesSectionContent ($ style );
203+ $ content .= $ this ->getParagraphPropertiesSectionContent ($ style );
202204 $ content .= $ this ->getTableCellPropertiesSectionContent ($ style );
203205
204206 $ content .= '</style:style> ' ;
@@ -214,26 +216,26 @@ protected function getStyleSectionContent($style)
214216 */
215217 private function getTextPropertiesSectionContent ($ style )
216218 {
217- $ content = '' ;
218-
219- if ($ style ->shouldApplyFont ()) {
220- $ content .= $ this ->getFontSectionContent ($ style );
219+ if (!$ style ->shouldApplyFont ()) {
220+ return '' ;
221221 }
222222
223- return $ content ;
223+ return '<style:text-properties '
224+ . $ this ->getFontSectionContent ($ style )
225+ . '/> ' ;
224226 }
225227
226228 /**
227- * Returns the contents of the "<style:text-properties>" section, inside "<style:style >" section
229+ * Returns the contents of the fonts definition section, inside "<style:text-properties >" section
228230 *
229231 * @param \Box\Spout\Common\Entity\Style\Style $style
232+ *
230233 * @return string
231234 */
232235 private function getFontSectionContent ($ style )
233236 {
234237 $ defaultStyle = $ this ->getDefaultStyle ();
235-
236- $ content = '<style:text-properties ' ;
238+ $ content = '' ;
237239
238240 $ fontColor = $ style ->getFontColor ();
239241 if ($ fontColor !== $ defaultStyle ->getFontColor ()) {
@@ -263,11 +265,60 @@ private function getFontSectionContent($style)
263265 $ content .= ' style:text-line-through-style="solid" ' ;
264266 }
265267
266- $ content .= '/> ' ;
267-
268268 return $ content ;
269269 }
270270
271+ /**
272+ * Returns the contents of the "<style:paragraph-properties>" section, inside "<style:style>" section
273+ *
274+ * @param \Box\Spout\Common\Entity\Style\Style $style
275+ *
276+ * @return string
277+ */
278+ private function getParagraphPropertiesSectionContent ($ style )
279+ {
280+ if (!$ style ->shouldApplyCellAlignment ()) {
281+ return '' ;
282+ }
283+
284+ return '<style:paragraph-properties '
285+ . $ this ->getCellAlignmentSectionContent ($ style )
286+ . '/> ' ;
287+ }
288+
289+ /**
290+ * Returns the contents of the cell alignment definition for the "<style:paragraph-properties>" section
291+ *
292+ * @param \Box\Spout\Common\Entity\Style\Style $style
293+ *
294+ * @return string
295+ */
296+ private function getCellAlignmentSectionContent ($ style )
297+ {
298+ return sprintf (
299+ ' fo:text-align="%s" ' ,
300+ $ this ->transformCellAlignment ($ style ->getCellAlignment ())
301+ );
302+ }
303+
304+ /**
305+ * Even though "left" and "right" alignments are part of the spec, and interpreted
306+ * respectively as "start" and "end", using the recommended values increase compatibility
307+ * with software that will read the created ODS file.
308+ *
309+ * @param string $cellAlignment
310+ *
311+ * @return string
312+ */
313+ private function transformCellAlignment ($ cellAlignment )
314+ {
315+ switch ($ cellAlignment ) {
316+ case CellAlignment::LEFT : return 'start ' ;
317+ case CellAlignment::RIGHT : return 'end ' ;
318+ default : return $ cellAlignment ;
319+ }
320+ }
321+
271322 /**
272323 * Returns the contents of the "<style:table-cell-properties>" section, inside "<style:style>" section
273324 *
@@ -276,7 +327,7 @@ private function getFontSectionContent($style)
276327 */
277328 private function getTableCellPropertiesSectionContent ($ style )
278329 {
279- $ content = '' ;
330+ $ content = '<style:table-cell-properties ' ;
280331
281332 if ($ style ->shouldWrapText ()) {
282333 $ content .= $ this ->getWrapTextXMLContent ();
@@ -290,6 +341,8 @@ private function getTableCellPropertiesSectionContent($style)
290341 $ content .= $ this ->getBackgroundColorXMLContent ($ style );
291342 }
292343
344+ $ content .= '/> ' ;
345+
293346 return $ content ;
294347 }
295348
@@ -300,7 +353,7 @@ private function getTableCellPropertiesSectionContent($style)
300353 */
301354 private function getWrapTextXMLContent ()
302355 {
303- return '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/> ' ;
356+ return ' fo:wrap-option="wrap" style:vertical-align="automatic" ' ;
304357 }
305358
306359 /**
@@ -311,13 +364,11 @@ private function getWrapTextXMLContent()
311364 */
312365 private function getBorderXMLContent ($ style )
313366 {
314- $ borderProperty = '<style:table-cell-properties %s /> ' ;
315-
316367 $ borders = array_map (function (BorderPart $ borderPart ) {
317368 return BorderHelper::serializeBorderPart ($ borderPart );
318369 }, $ style ->getBorder ()->getParts ());
319370
320- return sprintf ($ borderProperty , implode (' ' , $ borders ));
371+ return sprintf (' %s ' , implode (' ' , $ borders ));
321372 }
322373
323374 /**
@@ -328,9 +379,6 @@ private function getBorderXMLContent($style)
328379 */
329380 private function getBackgroundColorXMLContent ($ style )
330381 {
331- return sprintf (
332- '<style:table-cell-properties fo:background-color="#%s"/> ' ,
333- $ style ->getBackgroundColor ()
334- );
382+ return sprintf (' fo:background-color="#%s" ' , $ style ->getBackgroundColor ());
335383 }
336384}
0 commit comments