Added support for Vertically Raised or Lowered Text (w:position) (#1294)

* Added support for Vertically Raised or Lowered Text (w:position). Note that only docx writing is implemented for now.
* Add tests + changelog
* add reader + tests + doc
This commit is contained in:
troosan 2018-03-06 06:35:43 +01:00 committed by GitHub
parent 30b224b3d0
commit 250fbd49b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 123 additions and 0 deletions

View File

@ -13,6 +13,7 @@ v0.15.0 (?? ??? 2018)
- Add support for fixed Table Layout @aoloe @ekopach @troosan #841 #1276
- Add support for Cell Spacing @dox07 @troosan #1040
- Add parsing of formatting inside lists @atomicalnet @troosan #594
- Added support for Vertically Raised or Lowered Text (w:position) @anrikun @troosan #640
- Add support for MACROBUTTON field @phryneas @troosan #1021
### Fixed

View File

@ -59,6 +59,7 @@ Available Font style options:
See ``\PhpOffice\PhpWord\Style\Font::UNDERLINE_...`` constants for more values
- ``lang``. Language, either a language code like *en-US*, *fr-BE*, etc. or an object (or as an array) if you need to set eastAsian or bidirectional languages
See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes.
- ``position``. The text position, raised or lowered, in half points
.. _paragraph-style:

View File

@ -422,6 +422,7 @@ abstract class AbstractPart
'fgColor' => array(self::READ_VALUE, 'w:highlight'),
'rtl' => array(self::READ_TRUE, 'w:rtl'),
'lang' => array(self::READ_VALUE, 'w:lang'),
'position' => array(self::READ_VALUE, 'w:position'),
);
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);

View File

@ -232,6 +232,7 @@ class Font extends AbstractStyle
/**
* Right to left languages
*
* @var bool
*/
private $rtl = false;
@ -246,10 +247,19 @@ class Font extends AbstractStyle
/**
* Languages
*
* @var \PhpOffice\PhpWord\Style\Language
*/
private $lang;
/**
* Vertically Raised or Lowered Text
*
* @var int Signed Half-Point Measurement
* @see http://www.datypic.com/sc/ooxml/e-w_position-1.html
*/
private $position;
/**
* Create new font style
*
@ -294,6 +304,7 @@ class Font extends AbstractStyle
'scale' => $this->getScale(),
'spacing' => $this->getSpacing(),
'kerning' => $this->getKerning(),
'position' => $this->getPosition(),
),
'paragraph' => $this->getParagraph(),
'rtl' => $this->isRTL(),
@ -926,4 +937,27 @@ class Font extends AbstractStyle
{
return $this->getParagraph();
}
/**
* Get position
*
* @return int
*/
public function getPosition()
{
return $this->position;
}
/**
* Set position
*
* @param int $value
* @return self
*/
public function setPosition($value = null)
{
$this->position = $this->setIntVal($value, null);
return $this;
}
}

View File

@ -171,6 +171,14 @@ class Frame extends AbstractStyle
*/
private $wrap;
/**
* Vertically raised or lowered text
*
* @var int
* @see http://www.datypic.com/sc/ooxml/e-w_position-1.html
*/
private $position;
/**
* Create a new instance
*
@ -538,4 +546,27 @@ class Frame extends AbstractStyle
return $this;
}
/**
* Get position
*
* @return int
*/
public function getPosition()
{
return $this->position;
}
/**
* Set position
*
* @param int $value
* @return self
*/
public function setPosition($value = null)
{
$this->position = $this->setIntVal($value, null);
return $this;
}
}

View File

@ -19,6 +19,9 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\Common\XMLWriter;
use PhpOffice\PhpWord\Element\Image as ImageElement;
use PhpOffice\PhpWord\Style\Font as FontStyle;
use PhpOffice\PhpWord\Style\Frame as FrameStyle;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Image as ImageStyleWriter;
/**
@ -62,6 +65,16 @@ class Image extends AbstractElement
$this->writeCommentRangeStart();
$xmlWriter->startElement('w:r');
// Write position
$position = $style->getPosition();
if ($position && $style->getWrap() == FrameStyle::WRAP_INLINE) {
$fontStyle = new FontStyle('text');
$fontStyle->setPosition($position);
$fontStyleWriter = new FontStyleWriter($xmlWriter, $fontStyle);
$fontStyleWriter->write();
}
$xmlWriter->startElement('w:pict');
$xmlWriter->startElement('v:shape');
$xmlWriter->writeAttribute('type', '#_x0000_t75');

View File

@ -151,6 +151,9 @@ class Font extends AbstractStyle
$xmlWriter->writeElementIf($styleName === null && $style->isRTL(), 'w:rtl');
}
// Position
$xmlWriter->writeElementIf($style->getPosition() !== null, 'w:position', 'w:val', $style->getPosition());
$xmlWriter->endElement();
}

View File

@ -66,4 +66,28 @@ class StyleTest extends AbstractTestReader
$tableStyle = $elements[0]->getStyle();
$this->assertEquals(10.5, $tableStyle->getCellSpacing());
}
/**
* Test reading of position
*/
public function testReadPosition()
{
$documentXml = '<w:p>
<w:r>
<w:rPr>
<w:position w:val="15"/>
</w:rPr>
<w:t xml:space="preserve">This text is lowered</w:t>
</w:r>
</w:p>';
$phpWord = $this->getDocumentFromString($documentXml);
$elements = $this->get($phpWord->getSections(), 0)->getElements();
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $elements[0]);
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Font', $elements[0]->getFontStyle());
/** @var \PhpOffice\PhpWord\Style\Font $fontStyle */
$fontStyle = $elements[0]->getFontStyle();
$this->assertEquals(15, $fontStyle->getPosition());
}
}

View File

@ -65,4 +65,19 @@ class FontTest extends \PHPUnit\Framework\TestCase
$path = '/w:document/w:body/w:p/w:r/w:rPr/w:lang';
$this->assertTrue($doc->elementExists($path, $file));
}
/**
* Test writing position
*/
public function testPosition()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('This text is lowered', array('position' => -20));
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$path = '/w:document/w:body/w:p/w:r/w:rPr/w:position';
$this->assertTrue($doc->elementExists($path));
$this->assertEquals(-20, $doc->getElementAttribute($path, 'w:val'));
}
}