Skip to content

implement support for section vAlign #1569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ v0.17.0 (?? ??? 2019)
----------------------
### Added
- Add RightToLeft table presentation. @troosan #1550
- Add support for page vertical alignment. @troosan #672 #1569

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Available Section style options:
See ``\PhpOffice\PhpWord\Style\Section::ORIENTATION_...`` class constants for possible values
- ``pageSizeH``. Page height in *twip*. Implicitly defined by ``orientation`` option. Any changes are discouraged.
- ``pageSizeW``. Page width in *twip*. Implicitly defined by ``orientation`` option. Any changes are discouraged.
- ``vAlign``. Vertical Page Alignment
See ``\PhpOffice\PhpWord\SimpleType\VerticalJc`` for possible values

.. _font-style:

Expand Down
8 changes: 8 additions & 0 deletions samples/Sample_03_Sections.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use PhpOffice\PhpWord\SimpleType\VerticalJc;

include_once 'Sample_Header.php';

// New Word Document
Expand All @@ -21,6 +23,12 @@
);
$section->addText('This section uses other margins with folio papersize.');

// The text of this section is vertically centered
$section = $phpWord->addSection(
array('vAlign' => VerticalJc::CENTER)
);
$section->addText('This section is vertically centered.');

// New portrait section with Header & Footer
$section = $phpWord->addSection(
array(
Expand Down
1 change: 1 addition & 0 deletions src/PhpWord/Reader/Word2007/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private function readSectionStyle(XMLReader $xmlReader, \DOMElement $domNode)
{
$styleDefs = array(
'breakType' => array(self::READ_VALUE, 'w:type'),
'vAlign' => array(self::READ_VALUE, 'w:vAlign'),
'pageSizeW' => array(self::READ_VALUE, 'w:pgSz', 'w:w'),
'pageSizeH' => array(self::READ_VALUE, 'w:pgSz', 'w:h'),
'orientation' => array(self::READ_VALUE, 'w:pgSz', 'w:orient'),
Expand Down
36 changes: 36 additions & 0 deletions src/PhpWord/SimpleType/VerticalJc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\SimpleType;

use PhpOffice\PhpWord\Shared\AbstractEnum;

/**
* Vertical Alignment Type.
*
* Introduced in ISO/IEC-29500:2008.
*
* @see http://www.datypic.com/sc/ooxml/t-w_ST_VerticalJc.html
* @since 0.17.0
*/
final class VerticalJc extends AbstractEnum
{
const TOP = 'top';
const CENTER = 'center';
const BOTH = 'both';
const BOTTOM = 'bottom';
}
15 changes: 13 additions & 2 deletions src/PhpWord/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Style;

use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\SimpleType\VerticalJc;

/**
* Table cell style
Expand All @@ -28,10 +29,20 @@ class Cell extends Border
* Vertical alignment constants
*
* @const string
* @deprecated Use \PhpOffice\PhpWord\SimpleType\VerticalJc::TOP instead
*/
const VALIGN_TOP = 'top';
/**
* @deprecated Use \PhpOffice\PhpWord\SimpleType\VerticalJc::CENTER instead
*/
const VALIGN_CENTER = 'center';
/**
* @deprecated Use \PhpOffice\PhpWord\SimpleType\VerticalJc::BOTTOM instead
*/
const VALIGN_BOTTOM = 'bottom';
/**
* @deprecated Use \PhpOffice\PhpWord\SimpleType\VerticalJc::BOTH instead
*/
const VALIGN_BOTH = 'both';

//Text direction constants
Expand Down Expand Up @@ -145,8 +156,8 @@ public function getVAlign()
*/
public function setVAlign($value = null)
{
$enum = array(self::VALIGN_TOP, self::VALIGN_CENTER, self::VALIGN_BOTTOM, self::VALIGN_BOTH);
$this->vAlign = $this->setEnumVal($value, $enum, $this->vAlign);
VerticalJc::validate($value);
$this->vAlign = $this->setEnumVal($value, VerticalJc::values(), $this->vAlign);

return $this;
}
Expand Down
34 changes: 34 additions & 0 deletions src/PhpWord/Style/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord\Style;

use PhpOffice\PhpWord\SimpleType\VerticalJc;

/**
* Section settings
*/
Expand Down Expand Up @@ -166,6 +168,14 @@ class Section extends Border
*/
private $lineNumbering;

/**
* Vertical Text Alignment on Page
* One of \PhpOffice\PhpWord\SimpleType\VerticalJc
*
* @var string
*/
private $vAlign;

/**
* Create new instance
*/
Expand Down Expand Up @@ -599,4 +609,28 @@ public function setLineNumbering($value = null)

return $this;
}

/**
* Get vertical alignment
*
* @return string
*/
public function getVAlign()
{
return $this->vAlign;
}

/**
* Set vertical alignment
*
* @param string $value
* @return self
*/
public function setVAlign($value = null)
{
VerticalJc::validate($value);
$this->vAlign = $value;

return $this;
}
}
4 changes: 4 additions & 0 deletions src/PhpWord/Writer/Word2007/Style/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public function write()
$xmlWriter->writeAttribute('w:h', $style->getPageSizeH());
$xmlWriter->endElement(); // w:pgSz

// Vertical alignment
$vAlign = $style->getVAlign();
$xmlWriter->writeElementIf(!is_null($vAlign), 'w:vAlign', 'w:val', $vAlign);

// Margins
$margins = array(
'w:top' => array('getMarginTop', SectionStyle::DEFAULT_MARGIN),
Expand Down
13 changes: 13 additions & 0 deletions tests/PhpWord/Reader/Word2007/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use PhpOffice\PhpWord\AbstractTestReader;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\SimpleType\VerticalJc;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\Style\TablePosition;
Expand Down Expand Up @@ -213,4 +214,16 @@ public function testReadHeading()
$this->getDocumentFromString(array('styles' => $documentXml));
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', Style::getStyle($name));
}

public function testPageVerticalAlign()
{
$documentXml = '<w:sectPr>
<w:vAlign w:val="center"/>
</w:sectPr>';

$phpWord = $this->getDocumentFromString(array('document' => $documentXml));

$sectionStyle = $phpWord->getSection(0)->getStyle();
$this->assertEquals(VerticalJc::CENTER, $sectionStyle->getVAlign());
}
}
4 changes: 3 additions & 1 deletion tests/PhpWord/Style/CellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord\Style;

use PhpOffice\PhpWord\SimpleType\VerticalJc;

/**
* Test class for PhpOffice\PhpWord\Style\Cell
*
Expand All @@ -33,7 +35,7 @@ public function testSetGetNormal()
$object = new Cell();

$attributes = array(
'valign' => Cell::VALIGN_TOP,
'valign' => VerticalJc::TOP,
'textDirection' => Cell::TEXT_DIR_BTLR,
'bgColor' => 'FFFF00',
'borderTopSize' => 120,
Expand Down
16 changes: 16 additions & 0 deletions tests/PhpWord/Style/SectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord\Style;

use PhpOffice\PhpWord\SimpleType\VerticalJc;

/**
* Test class for PhpOffice\PhpWord\Style\Section
*
Expand Down Expand Up @@ -328,4 +330,18 @@ public function testBreakType()
$oSettings->setBreakType();
$this->assertNull($oSettings->getBreakType());
}

/**
* Vertical page alignment
*/
public function testVerticalAlign()
{
// Section Settings
$oSettings = new Section();

$this->assertNull($oSettings->getVAlign());

$oSettings->setVAlign(VerticalJc::BOTH);
$this->assertEquals('both', $oSettings->getVAlign());
}
}