Skip to content

Commit

Permalink
Merge 8b2e21b into 733f845
Browse files Browse the repository at this point in the history
  • Loading branch information
oleibman committed Dec 9, 2019
2 parents 733f845 + 8b2e21b commit 2add155
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 4 deletions.
4 changes: 3 additions & 1 deletion docs/elements.rst
Expand Up @@ -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 ``\``.
Expand Down
40 changes: 38 additions & 2 deletions src/PhpWord/Element/Field.php
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\Style\Font;

/**
* Field element
*
Expand Down Expand Up @@ -115,24 +117,58 @@ 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
*
* @param string $type
* @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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/PhpWord/Writer/Word2007/Element/Field.php
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/PhpWord/Writer/Word2007/Style/Font.php
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
35 changes: 35 additions & 0 deletions tests/PhpWord/Writer/Word2007/ElementTest.php
Expand Up @@ -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();
Expand Down
74 changes: 74 additions & 0 deletions tests/PhpWord/Writer/Word2007/Style/FontTest.php
Expand Up @@ -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
*/
Expand Down

0 comments on commit 2add155

Please sign in to comment.