Fixed broken paragraph alignment for OpenXml.

This commit is contained in:
Roman Syroeshko 2015-02-21 17:43:46 +04:00
parent 8bb4573534
commit cc305bcb11
4 changed files with 16 additions and 24 deletions

View File

@ -25,6 +25,7 @@ use PhpOffice\PhpWord\Shared\String;
* *
* OOXML: * OOXML:
* - General: alignment, outline level * - General: alignment, outline level
* - Alignment: left, right, center, both
* - Indentation: left, right, firstline, hanging * - Indentation: left, right, firstline, hanging
* - Spacing: before, after, line spacing * - Spacing: before, after, line spacing
* - Pagination: widow control, keep next, keep line, page break before * - Pagination: widow control, keep next, keep line, page break before
@ -159,14 +160,6 @@ class Paragraph extends Border
*/ */
private $shading; private $shading;
/**
* Create new instance
*/
public function __construct()
{
$this->alignment = new Alignment();
}
/** /**
* Set Style value * Set Style value
* *
@ -202,7 +195,7 @@ class Paragraph extends Border
'name' => $this->getStyleName(), 'name' => $this->getStyleName(),
'basedOn' => $this->getBasedOn(), 'basedOn' => $this->getBasedOn(),
'next' => $this->getNext(), 'next' => $this->getNext(),
'alignment' => $this->getAlign(), 'alignment' => $this->getAlignment(),
'indentation' => $this->getIndentation(), 'indentation' => $this->getIndentation(),
'spacing' => $this->getSpace(), 'spacing' => $this->getSpace(),
'pagination' => array( 'pagination' => array(
@ -225,11 +218,11 @@ class Paragraph extends Border
/** /**
* Get alignment * Get alignment
* *
* @return string * @return \PhpOffice\PhpWord\Style\Alignment
*/ */
public function getAlign() public function getAlignment()
{ {
return $this->alignment->getValue(); return $this->alignment;
} }
/** /**
@ -238,9 +231,9 @@ class Paragraph extends Border
* @param string $value * @param string $value
* @return self * @return self
*/ */
public function setAlign($value = null) public function setAlignment($value = null)
{ {
$this->alignment->setValue($value); $this->setObjectVal(array('value' => $value), 'Alignment', $this->alignment);
return $this; return $this;
} }

View File

@ -38,8 +38,11 @@ class Paragraph extends AbstractStyle
$css = array(); $css = array();
// Alignment // Alignment
$align = $style->getAlign(); $alignment = $style->getAlignment();
$css['text-align'] = $this->getValueIf(!is_null($align), $align); if (!is_null($alignment)) {
$alignmentValue = $alignment->getValue();
$css['text-align'] = $this->getValueIf(!is_null($alignmentValue), $alignmentValue);
}
// Spacing // Spacing
$spacing = $style->getSpace(); $spacing = $style->getSpace();

View File

@ -54,7 +54,7 @@ class Paragraph extends AbstractStyle
} else { } else {
$xmlWriter->writeAttribute('fo:margin-top', $marginTop . 'cm'); $xmlWriter->writeAttribute('fo:margin-top', $marginTop . 'cm');
$xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm'); $xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm');
$xmlWriter->writeAttribute('fo:text-align', $style->getAlign()); $xmlWriter->writeAttribute('fo:text-align', $style->getAlignment());
} }
$xmlWriter->endElement(); //style:paragraph-properties $xmlWriter->endElement(); //style:paragraph-properties

View File

@ -18,9 +18,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style; namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
/** /**
* Paragraph style writer * Paragraph style writer
@ -91,17 +90,14 @@ class Paragraph extends AbstractStyle
$xmlWriter->writeElementIf($styles['name'] !== null, 'w:pStyle', 'w:val', $styles['name']); $xmlWriter->writeElementIf($styles['name'] !== null, 'w:pStyle', 'w:val', $styles['name']);
} }
// Alignment
$styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $styles['alignment'])));
$styleWriter->write();
// Pagination // Pagination
$xmlWriter->writeElementIf($styles['pagination']['widowControl'] === false, 'w:widowControl', 'w:val', '0'); $xmlWriter->writeElementIf($styles['pagination']['widowControl'] === false, 'w:widowControl', 'w:val', '0');
$xmlWriter->writeElementIf($styles['pagination']['keepNext'] === true, 'w:keepNext', 'w:val', '1'); $xmlWriter->writeElementIf($styles['pagination']['keepNext'] === true, 'w:keepNext', 'w:val', '1');
$xmlWriter->writeElementIf($styles['pagination']['keepLines'] === true, 'w:keepLines', 'w:val', '1'); $xmlWriter->writeElementIf($styles['pagination']['keepLines'] === true, 'w:keepLines', 'w:val', '1');
$xmlWriter->writeElementIf($styles['pagination']['pageBreak'] === true, 'w:pageBreakBefore', 'w:val', '1'); $xmlWriter->writeElementIf($styles['pagination']['pageBreak'] === true, 'w:pageBreakBefore', 'w:val', '1');
// Child style: indentation, spacing, and shading // Child style: alignment, indentation, spacing, and shading
$this->writeChildStyle($xmlWriter, 'Alignment', $styles['alignment']);
$this->writeChildStyle($xmlWriter, 'Indentation', $styles['indentation']); $this->writeChildStyle($xmlWriter, 'Indentation', $styles['indentation']);
$this->writeChildStyle($xmlWriter, 'Spacing', $styles['spacing']); $this->writeChildStyle($xmlWriter, 'Spacing', $styles['spacing']);
$this->writeChildStyle($xmlWriter, 'Shading', $styles['shading']); $this->writeChildStyle($xmlWriter, 'Shading', $styles['shading']);