Skip to content

Commit 200d846

Browse files
committed
implement paragraph textAlignment
1 parent 92fc549 commit 200d846

File tree

7 files changed

+99
-2
lines changed

7 files changed

+99
-2
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2017 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\SimpleType;
19+
20+
use PhpOffice\PhpWord\Shared\AbstractEnum;
21+
22+
/**
23+
* Magnification Preset Values
24+
*
25+
* @since 0.14.0
26+
*
27+
* @see http://www.datypic.com/sc/ooxml/t-w_ST_TextAlignment.html
28+
*/
29+
final class TextAlignment extends AbstractEnum
30+
{
31+
//Align Text at Top
32+
const TOP = 'top';
33+
34+
//Align Text at Center
35+
const CENTER = 'center';
36+
37+
//Align Text at Baseline
38+
const BASELINE = 'baseline';
39+
40+
//Align Text at Bottom
41+
const BOTTOM = 'bottom';
42+
43+
//Automatically Determine Alignment
44+
const AUTO = 'auto';
45+
}

src/PhpWord/Style/Paragraph.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PhpOffice\Common\Text;
2121
use PhpOffice\PhpWord\Exception\InvalidStyleException;
2222
use PhpOffice\PhpWord\SimpleType\Jc;
23+
use PhpOffice\PhpWord\SimpleType\TextAlignment;
2324

2425
/**
2526
* Paragraph style
@@ -172,6 +173,13 @@ class Paragraph extends Border
172173
*/
173174
private $bidi = false;
174175

176+
/**
177+
* Vertical Character Alignment on Line
178+
*
179+
* @var string
180+
*/
181+
private $textAlignment;
182+
175183
/**
176184
* Set Style value
177185
*
@@ -224,6 +232,7 @@ public function getStyleValues()
224232
'shading' => $this->getShading(),
225233
'contextualSpacing' => $this->hasContextualSpacing(),
226234
'bidi' => $this->isBidi(),
235+
'textAlignment' => $this->getTextAlignment(),
227236
);
228237

229238
return $styles;
@@ -794,4 +803,28 @@ public function setBidi($bidi)
794803

795804
return $this;
796805
}
806+
807+
/**
808+
* Get textAlignment
809+
*
810+
* @return string
811+
*/
812+
public function getTextAlignment()
813+
{
814+
return $this->textAlignment;
815+
}
816+
817+
/**
818+
* Set textAlignment
819+
*
820+
* @param string $textAlignment
821+
* @return self
822+
*/
823+
public function setTextAlignment($textAlignment)
824+
{
825+
TextAlignment::validate($textAlignment);
826+
$this->textAlignment = $textAlignment;
827+
828+
return $this;
829+
}
797830
}

src/PhpWord/Writer/Word2007/Style/Paragraph.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ private function writeStyle()
109109
//Paragraph contextualSpacing
110110
$xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing');
111111

112+
//Paragraph contextualSpacing
113+
$xmlWriter->writeElementIf($styles['textAlignment'] !== null, 'w:textAlignment', 'w:val', $styles['textAlignment']);
114+
112115
// Child style: alignment, indentation, spacing, and shading
113116
$this->writeChildStyle($xmlWriter, 'Indentation', $styles['indentation']);
114117
$this->writeChildStyle($xmlWriter, 'Spacing', $styles['spacing']);

tests/PhpWord/Element/ImageTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function testImages()
8787
$this->assertEquals($createFunction, $image->getImageCreateFunction());
8888
$this->assertEquals($imageFunction, $image->getImageFunction());
8989
$this->assertFalse($image->isMemImage());
90+
$this->assertNotNull($image->getImageStringData());
9091
}
9192
}
9293

tests/PhpWord/Style/FontTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function testSetStyleValueWithNullOrEmpty()
6969
'doubleStrikethrough' => false,
7070
'smallCaps' => false,
7171
'allCaps' => false,
72+
'rtl' => false,
7273
'fgColor' => null,
7374
'bgColor' => null,
7475
'scale' => null,
@@ -113,6 +114,8 @@ public function testSetStyleValueNormal()
113114
'scale' => 150,
114115
'spacing' => 240,
115116
'kerning' => 10,
117+
'rtl' => true,
118+
'lang' => new Language(Language::EN_US),
116119
);
117120
$object->setStyleByArray($attributes);
118121
foreach ($attributes as $key => $value) {
@@ -173,4 +176,15 @@ public function testLineHeightException()
173176
$object = new Font();
174177
$object->setLineHeight('a');
175178
}
179+
180+
/**
181+
* Test setting the language as a string
182+
*/
183+
public function testSetLangAsString()
184+
{
185+
$object = new Font();
186+
$object->setLang(Language::FR_BE);
187+
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Language', $object->getLang());
188+
$this->assertEquals(Language::FR_BE, $object->getLang()->getLatin());
189+
}
176190
}

tests/PhpWord/Style/ParagraphTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function testSetStyleValueNormal()
8080
'keepLines' => true,
8181
'pageBreakBefore' => true,
8282
'contextualSpacing' => true,
83+
'textAlignment' => 'auto',
8384
'bidi' => true,
8485
);
8586
foreach ($attributes as $key => $value) {
@@ -114,7 +115,7 @@ public function testGetNullStyleValue()
114115
{
115116
$object = new Paragraph();
116117

117-
$attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter');
118+
$attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter', 'textAlignment');
118119
foreach ($attributes as $key) {
119120
$get = $this->findGetter($key, null, $object);
120121
$this->assertNull($object->$get());

tests/PhpWord/Writer/Word2007/Part/SettingsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
1919

20+
use PhpOffice\PhpWord\ComplexType\ProofState;
2021
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
2122
use PhpOffice\PhpWord\PhpWord;
2223
use PhpOffice\PhpWord\Settings;
2324
use PhpOffice\PhpWord\SimpleType\Zoom;
2425
use PhpOffice\PhpWord\Style\Language;
2526
use PhpOffice\PhpWord\TestHelperDOCX;
26-
use PhpOffice\PhpWord\ComplexType\ProofState;
2727

2828
/**
2929
* Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Settings

0 commit comments

Comments
 (0)