implement paragraph textAlignment

This commit is contained in:
troosan 2017-11-05 21:39:10 +01:00
parent 92fc54992a
commit 200d846f61
7 changed files with 99 additions and 2 deletions

View File

@ -0,0 +1,45 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2017 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\SimpleType;
use PhpOffice\PhpWord\Shared\AbstractEnum;
/**
* Magnification Preset Values
*
* @since 0.14.0
*
* @see http://www.datypic.com/sc/ooxml/t-w_ST_TextAlignment.html
*/
final class TextAlignment extends AbstractEnum
{
//Align Text at Top
const TOP = 'top';
//Align Text at Center
const CENTER = 'center';
//Align Text at Baseline
const BASELINE = 'baseline';
//Align Text at Bottom
const BOTTOM = 'bottom';
//Automatically Determine Alignment
const AUTO = 'auto';
}

View File

@ -20,6 +20,7 @@ namespace PhpOffice\PhpWord\Style;
use PhpOffice\Common\Text; use PhpOffice\Common\Text;
use PhpOffice\PhpWord\Exception\InvalidStyleException; use PhpOffice\PhpWord\Exception\InvalidStyleException;
use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\TextAlignment;
/** /**
* Paragraph style * Paragraph style
@ -172,6 +173,13 @@ class Paragraph extends Border
*/ */
private $bidi = false; private $bidi = false;
/**
* Vertical Character Alignment on Line
*
* @var string
*/
private $textAlignment;
/** /**
* Set Style value * Set Style value
* *
@ -224,6 +232,7 @@ class Paragraph extends Border
'shading' => $this->getShading(), 'shading' => $this->getShading(),
'contextualSpacing' => $this->hasContextualSpacing(), 'contextualSpacing' => $this->hasContextualSpacing(),
'bidi' => $this->isBidi(), 'bidi' => $this->isBidi(),
'textAlignment' => $this->getTextAlignment(),
); );
return $styles; return $styles;
@ -794,4 +803,28 @@ class Paragraph extends Border
return $this; return $this;
} }
/**
* Get textAlignment
*
* @return string
*/
public function getTextAlignment()
{
return $this->textAlignment;
}
/**
* Set textAlignment
*
* @param string $textAlignment
* @return self
*/
public function setTextAlignment($textAlignment)
{
TextAlignment::validate($textAlignment);
$this->textAlignment = $textAlignment;
return $this;
}
} }

View File

@ -109,6 +109,9 @@ class Paragraph extends AbstractStyle
//Paragraph contextualSpacing //Paragraph contextualSpacing
$xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing'); $xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing');
//Paragraph contextualSpacing
$xmlWriter->writeElementIf($styles['textAlignment'] !== null, 'w:textAlignment', 'w:val', $styles['textAlignment']);
// Child style: alignment, indentation, spacing, and shading // Child style: alignment, indentation, spacing, and shading
$this->writeChildStyle($xmlWriter, 'Indentation', $styles['indentation']); $this->writeChildStyle($xmlWriter, 'Indentation', $styles['indentation']);
$this->writeChildStyle($xmlWriter, 'Spacing', $styles['spacing']); $this->writeChildStyle($xmlWriter, 'Spacing', $styles['spacing']);

View File

@ -87,6 +87,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($createFunction, $image->getImageCreateFunction()); $this->assertEquals($createFunction, $image->getImageCreateFunction());
$this->assertEquals($imageFunction, $image->getImageFunction()); $this->assertEquals($imageFunction, $image->getImageFunction());
$this->assertFalse($image->isMemImage()); $this->assertFalse($image->isMemImage());
$this->assertNotNull($image->getImageStringData());
} }
} }

View File

@ -69,6 +69,7 @@ class FontTest extends \PHPUnit_Framework_TestCase
'doubleStrikethrough' => false, 'doubleStrikethrough' => false,
'smallCaps' => false, 'smallCaps' => false,
'allCaps' => false, 'allCaps' => false,
'rtl' => false,
'fgColor' => null, 'fgColor' => null,
'bgColor' => null, 'bgColor' => null,
'scale' => null, 'scale' => null,
@ -113,6 +114,8 @@ class FontTest extends \PHPUnit_Framework_TestCase
'scale' => 150, 'scale' => 150,
'spacing' => 240, 'spacing' => 240,
'kerning' => 10, 'kerning' => 10,
'rtl' => true,
'lang' => new Language(Language::EN_US),
); );
$object->setStyleByArray($attributes); $object->setStyleByArray($attributes);
foreach ($attributes as $key => $value) { foreach ($attributes as $key => $value) {
@ -173,4 +176,15 @@ class FontTest extends \PHPUnit_Framework_TestCase
$object = new Font(); $object = new Font();
$object->setLineHeight('a'); $object->setLineHeight('a');
} }
/**
* Test setting the language as a string
*/
public function testSetLangAsString()
{
$object = new Font();
$object->setLang(Language::FR_BE);
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Language', $object->getLang());
$this->assertEquals(Language::FR_BE, $object->getLang()->getLatin());
}
} }

View File

@ -80,6 +80,7 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase
'keepLines' => true, 'keepLines' => true,
'pageBreakBefore' => true, 'pageBreakBefore' => true,
'contextualSpacing' => true, 'contextualSpacing' => true,
'textAlignment' => 'auto',
'bidi' => true, 'bidi' => true,
); );
foreach ($attributes as $key => $value) { foreach ($attributes as $key => $value) {
@ -114,7 +115,7 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase
{ {
$object = new Paragraph(); $object = new Paragraph();
$attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter'); $attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter', 'textAlignment');
foreach ($attributes as $key) { foreach ($attributes as $key) {
$get = $this->findGetter($key, null, $object); $get = $this->findGetter($key, null, $object);
$this->assertNull($object->$get()); $this->assertNull($object->$get());

View File

@ -17,13 +17,13 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Part; namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\ComplexType\ProofState;
use PhpOffice\PhpWord\ComplexType\TrackChangesView; use PhpOffice\PhpWord\ComplexType\TrackChangesView;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Zoom; use PhpOffice\PhpWord\SimpleType\Zoom;
use PhpOffice\PhpWord\Style\Language; use PhpOffice\PhpWord\Style\Language;
use PhpOffice\PhpWord\TestHelperDOCX; use PhpOffice\PhpWord\TestHelperDOCX;
use PhpOffice\PhpWord\ComplexType\ProofState;
/** /**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Settings * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Settings