Skip to content

Commit 250fbd4

Browse files
authored
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
1 parent 30b224b commit 250fbd4

File tree

9 files changed

+123
-0
lines changed

9 files changed

+123
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ v0.15.0 (?? ??? 2018)
1313
- Add support for fixed Table Layout @aoloe @ekopach @troosan #841 #1276
1414
- Add support for Cell Spacing @dox07 @troosan #1040
1515
- Add parsing of formatting inside lists @atomicalnet @troosan #594
16+
- Added support for Vertically Raised or Lowered Text (w:position) @anrikun @troosan #640
1617
- Add support for MACROBUTTON field @phryneas @troosan #1021
1718

1819
### Fixed

docs/styles.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Available Font style options:
5959
See ``\PhpOffice\PhpWord\Style\Font::UNDERLINE_...`` constants for more values
6060
- ``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
6161
See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes.
62+
- ``position``. The text position, raised or lowered, in half points
6263

6364
.. _paragraph-style:
6465

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
422422
'fgColor' => array(self::READ_VALUE, 'w:highlight'),
423423
'rtl' => array(self::READ_TRUE, 'w:rtl'),
424424
'lang' => array(self::READ_VALUE, 'w:lang'),
425+
'position' => array(self::READ_VALUE, 'w:position'),
425426
);
426427

427428
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);

src/PhpWord/Style/Font.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class Font extends AbstractStyle
232232

233233
/**
234234
* Right to left languages
235+
*
235236
* @var bool
236237
*/
237238
private $rtl = false;
@@ -246,10 +247,19 @@ class Font extends AbstractStyle
246247

247248
/**
248249
* Languages
250+
*
249251
* @var \PhpOffice\PhpWord\Style\Language
250252
*/
251253
private $lang;
252254

255+
/**
256+
* Vertically Raised or Lowered Text
257+
*
258+
* @var int Signed Half-Point Measurement
259+
* @see http://www.datypic.com/sc/ooxml/e-w_position-1.html
260+
*/
261+
private $position;
262+
253263
/**
254264
* Create new font style
255265
*
@@ -294,6 +304,7 @@ public function getStyleValues()
294304
'scale' => $this->getScale(),
295305
'spacing' => $this->getSpacing(),
296306
'kerning' => $this->getKerning(),
307+
'position' => $this->getPosition(),
297308
),
298309
'paragraph' => $this->getParagraph(),
299310
'rtl' => $this->isRTL(),
@@ -926,4 +937,27 @@ public function getParagraphStyle()
926937
{
927938
return $this->getParagraph();
928939
}
940+
941+
/**
942+
* Get position
943+
*
944+
* @return int
945+
*/
946+
public function getPosition()
947+
{
948+
return $this->position;
949+
}
950+
951+
/**
952+
* Set position
953+
*
954+
* @param int $value
955+
* @return self
956+
*/
957+
public function setPosition($value = null)
958+
{
959+
$this->position = $this->setIntVal($value, null);
960+
961+
return $this;
962+
}
929963
}

src/PhpWord/Style/Frame.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ class Frame extends AbstractStyle
171171
*/
172172
private $wrap;
173173

174+
/**
175+
* Vertically raised or lowered text
176+
*
177+
* @var int
178+
* @see http://www.datypic.com/sc/ooxml/e-w_position-1.html
179+
*/
180+
private $position;
181+
174182
/**
175183
* Create a new instance
176184
*
@@ -538,4 +546,27 @@ public function setWrap($value)
538546

539547
return $this;
540548
}
549+
550+
/**
551+
* Get position
552+
*
553+
* @return int
554+
*/
555+
public function getPosition()
556+
{
557+
return $this->position;
558+
}
559+
560+
/**
561+
* Set position
562+
*
563+
* @param int $value
564+
* @return self
565+
*/
566+
public function setPosition($value = null)
567+
{
568+
$this->position = $this->setIntVal($value, null);
569+
570+
return $this;
571+
}
541572
}

src/PhpWord/Writer/Word2007/Element/Image.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
use PhpOffice\Common\XMLWriter;
2121
use PhpOffice\PhpWord\Element\Image as ImageElement;
22+
use PhpOffice\PhpWord\Style\Font as FontStyle;
23+
use PhpOffice\PhpWord\Style\Frame as FrameStyle;
24+
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
2225
use PhpOffice\PhpWord\Writer\Word2007\Style\Image as ImageStyleWriter;
2326

2427
/**
@@ -62,6 +65,16 @@ private function writeImage(XMLWriter $xmlWriter, ImageElement $element)
6265
$this->writeCommentRangeStart();
6366

6467
$xmlWriter->startElement('w:r');
68+
69+
// Write position
70+
$position = $style->getPosition();
71+
if ($position && $style->getWrap() == FrameStyle::WRAP_INLINE) {
72+
$fontStyle = new FontStyle('text');
73+
$fontStyle->setPosition($position);
74+
$fontStyleWriter = new FontStyleWriter($xmlWriter, $fontStyle);
75+
$fontStyleWriter->write();
76+
}
77+
6578
$xmlWriter->startElement('w:pict');
6679
$xmlWriter->startElement('v:shape');
6780
$xmlWriter->writeAttribute('type', '#_x0000_t75');

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ private function writeStyle()
151151
$xmlWriter->writeElementIf($styleName === null && $style->isRTL(), 'w:rtl');
152152
}
153153

154+
// Position
155+
$xmlWriter->writeElementIf($style->getPosition() !== null, 'w:position', 'w:val', $style->getPosition());
156+
154157
$xmlWriter->endElement();
155158
}
156159

tests/PhpWord/Reader/Word2007/StyleTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,28 @@ public function testReadCellSpacing()
6666
$tableStyle = $elements[0]->getStyle();
6767
$this->assertEquals(10.5, $tableStyle->getCellSpacing());
6868
}
69+
70+
/**
71+
* Test reading of position
72+
*/
73+
public function testReadPosition()
74+
{
75+
$documentXml = '<w:p>
76+
<w:r>
77+
<w:rPr>
78+
<w:position w:val="15"/>
79+
</w:rPr>
80+
<w:t xml:space="preserve">This text is lowered</w:t>
81+
</w:r>
82+
</w:p>';
83+
84+
$phpWord = $this->getDocumentFromString($documentXml);
85+
86+
$elements = $this->get($phpWord->getSections(), 0)->getElements();
87+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $elements[0]);
88+
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Font', $elements[0]->getFontStyle());
89+
/** @var \PhpOffice\PhpWord\Style\Font $fontStyle */
90+
$fontStyle = $elements[0]->getFontStyle();
91+
$this->assertEquals(15, $fontStyle->getPosition());
92+
}
6993
}

tests/PhpWord/Writer/Word2007/Style/FontTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,19 @@ public function testFontWithLang()
6565
$path = '/w:document/w:body/w:p/w:r/w:rPr/w:lang';
6666
$this->assertTrue($doc->elementExists($path, $file));
6767
}
68+
69+
/**
70+
* Test writing position
71+
*/
72+
public function testPosition()
73+
{
74+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
75+
$section = $phpWord->addSection();
76+
$section->addText('This text is lowered', array('position' => -20));
77+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
78+
79+
$path = '/w:document/w:body/w:p/w:r/w:rPr/w:position';
80+
$this->assertTrue($doc->elementExists($path));
81+
$this->assertEquals(-20, $doc->getElementAttribute($path, 'w:val'));
82+
}
6883
}

0 commit comments

Comments
 (0)