diff --git a/docs/elements.rst b/docs/elements.rst index e497491533..3b7dc1d6a5 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -403,7 +403,9 @@ Currently the following fields are supported: .. code-block:: php - $section->addField($fieldType, [$properties], [$options], [$fieldText]) + $section->addField($fieldType, [$properties], [$options], [$fieldText], [$fontStyle]) + +- ``$fontStyle``. See :ref:`font-style`. See ``\PhpOffice\PhpWord\Element\Field`` for list of properties and options available for each field type. Options which are not specifically defined can be added. Those must start with a ``\``. diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 2efc6b0b9f..3d1503fe22 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\Style\Font; + /** * Field element * @@ -115,10 +117,42 @@ class Field extends AbstractElement /** * Font style * - * @var \PhpOffice\PhpWord\Style\Font + * @var string|\PhpOffice\PhpWord\Style\Font */ protected $fontStyle; + /** + * Set Font style + * + * @param string|array|\PhpOffice\PhpWord\Style\Font $style + * @return string|\PhpOffice\PhpWord\Style\Font + */ + public function setFontStyle($style = null) + { + if ($style instanceof Font) { + $this->fontStyle = $style; + } elseif (is_array($style)) { + $this->fontStyle = new Font('text'); + $this->fontStyle->setStyleByArray($style); + } elseif (null === $style) { + $this->fontStyle = null; + } else { + $this->fontStyle = $style; + } + + return $this->fontStyle; + } + + /** + * Get Font style + * + * @return string|\PhpOffice\PhpWord\Style\Font + */ + public function getFontStyle() + { + return $this->fontStyle; + } + /** * Create a new Field Element * @@ -126,13 +160,15 @@ class Field extends AbstractElement * @param array $properties * @param array $options * @param TextRun|string|null $text + * @param string|array|\PhpOffice\PhpWord\Style\Font $fontStyle */ - public function __construct($type = null, $properties = array(), $options = array(), $text = null) + public function __construct($type = null, $properties = array(), $options = array(), $text = null, $fontStyle = null) { $this->setType($type); $this->setProperties($properties); $this->setOptions($options); $this->setText($text); + $this->setFontStyle($fontStyle); } /** diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index e79dd24a90..b800fbddc3 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -65,6 +65,7 @@ private function writeDefault(\PhpOffice\PhpWord\Element\Field $element) $instruction .= $this->buildPropertiesAndOptions($element); } $xmlWriter->startElement('w:r'); + $this->writeFontStyle(); $xmlWriter->startElement('w:instrText'); $xmlWriter->writeAttribute('xml:space', 'preserve'); $xmlWriter->text($instruction); diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index dd4fac4f0d..2f2218fa57 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -44,6 +44,10 @@ public function write() $xmlWriter->startElement('w:rStyle'); $xmlWriter->writeAttribute('w:val', $this->style); $xmlWriter->endElement(); + $style = \PhpOffice\PhpWord\Style::getStyle($this->style); + if ($style instanceof \PhpOffice\PhpWord\Style\Font) { + $xmlWriter->writeElementIf($style->isRTL(), 'w:rtl'); + } $xmlWriter->endElement(); } else { $this->writeStyle(); @@ -139,7 +143,7 @@ private function writeStyle() $xmlWriter->writeElementIf($style->getKerning() !== null, 'w:kern', 'w:val', $style->getKerning() * 2); // noProof - $xmlWriter->writeElementIf($style->isNoProof() !== null, 'w:noProof', $this->writeOnOf($style->isNoProof())); + $xmlWriter->writeElementIf($style->isNoProof() !== null, 'w:noProof', 'w:val', $this->writeOnOf($style->isNoProof())); // Background-Color $shading = $style->getShading(); diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index 6a295965f7..e9c1ea5bd7 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -296,6 +296,41 @@ public function testFieldElement() $this->assertEquals(' INDEX \\c "3" ', $doc->getElement($element)->textContent); } + public function testUnstyledFieldElement() + { + $phpWord = new PhpWord(); + $phpWord->addFontStyle('h1', array('name' => 'Courier New', 'size' => 8)); + $section = $phpWord->addSection(); + + $section->addField('PAGE'); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = '/w:document/w:body/w:p/w:r[2]/w:instrText'; + $this->assertTrue($doc->elementExists($element)); + $this->assertEquals(' PAGE ', $doc->getElement($element)->textContent); + $sty = '/w:document/w:body/w:p/w:r[2]/w:rPr'; + $this->assertFalse($doc->elementExists($sty)); + } + + public function testStyledFieldElement() + { + $phpWord = new PhpWord(); + $stnam = 'h1'; + $phpWord->addFontStyle($stnam, array('name' => 'Courier New', 'size' => 8)); + $section = $phpWord->addSection(); + + $fld = $section->addField('PAGE'); + $fld->setFontStyle($stnam); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = '/w:document/w:body/w:p/w:r[2]/w:instrText'; + $this->assertTrue($doc->elementExists($element)); + $this->assertEquals(' PAGE ', $doc->getElement($element)->textContent); + $sty = '/w:document/w:body/w:p/w:r[2]/w:rPr'; + $this->assertTrue($doc->elementExists($sty)); + $this->assertEquals($stnam, $doc->getElementAttribute($sty . '/w:rStyle', 'w:val')); + } + public function testFieldElementWithComplexText() { $phpWord = new PhpWord(); diff --git a/tests/PhpWord/Writer/Word2007/Style/FontTest.php b/tests/PhpWord/Writer/Word2007/Style/FontTest.php index ccfffbfb01..41e52ab027 100644 --- a/tests/PhpWord/Writer/Word2007/Style/FontTest.php +++ b/tests/PhpWord/Writer/Word2007/Style/FontTest.php @@ -51,6 +51,80 @@ public function testFontRTL() $this->assertTrue($doc->elementExists($path, $file)); } + public function testFontRTLNamed() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $stnam = 'fstyle'; + $phpWord->addFontStyle($stnam, array( + 'rtl' => true, + 'name' => 'Courier New', + 'size' => 8, + )); + $section = $phpWord->addSection(); + $txt = 'היום יום שני'; // Translation = Today is Monday + $section->addText($txt, $stnam); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + + $element = '/w:document/w:body/w:p/w:r'; + $txtelem = $element . '/w:t'; + $styelem = $element . '/w:rPr'; + $this->assertTrue($doc->elementExists($txtelem)); + $this->assertEquals($txt, $doc->getElement($txtelem)->textContent); + $this->assertTrue($doc->elementExists($styelem)); + $this->assertTrue($doc->elementExists($styelem . '/w:rStyle')); + $this->assertEquals($stnam, $doc->getElementAttribute($styelem . '/w:rStyle', 'w:val')); + $this->assertTrue($doc->elementExists($styelem . '/w:rtl')); + } + + public function testFontNotRTLNamed() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $stnam = 'fstyle'; + $phpWord->addFontStyle($stnam, array( + //'rtl' => true, + 'name' => 'Courier New', + 'size' => 8, + )); + $section = $phpWord->addSection(); + $txt = 'היום יום שני'; // Translation = Today is Monday + $section->addText($txt, $stnam); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + + $element = '/w:document/w:body/w:p/w:r'; + $txtelem = $element . '/w:t'; + $styelem = $element . '/w:rPr'; + $this->assertTrue($doc->elementExists($txtelem)); + $this->assertEquals($txt, $doc->getElement($txtelem)->textContent); + $this->assertTrue($doc->elementExists($styelem)); + $this->assertTrue($doc->elementExists($styelem . '/w:rStyle')); + $this->assertEquals($stnam, $doc->getElementAttribute($styelem . '/w:rStyle', 'w:val')); + $this->assertFalse($doc->elementExists($styelem . '/w:rtl')); + } + + public function testNoProof() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $fontStyle = array( + 'noProof' => true, + 'name' => 'Courier New', + 'size' => 8, + ); + $section = $phpWord->addSection(); + $txt = 'spellung error'; + $section->addText($txt, $fontStyle); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + + $element = '/w:document/w:body/w:p/w:r'; + $txtelem = $element . '/w:t'; + $styelem = $element . '/w:rPr'; + $noproofelem = $styelem . '/w:noProof'; + $this->assertTrue($doc->elementExists($txtelem)); + $this->assertEquals($txt, $doc->getElement($txtelem)->textContent); + $this->assertTrue($doc->elementExists($styelem)); + $this->assertTrue($doc->elementExists($noproofelem)); + $this->assertEquals('1', $doc->getElementAttribute($noproofelem, 'w:val')); + } + /** * Test writing font with language */