Merge pull request #1774 from oleibman/fieldstyle

Add font style for Field elements
This commit is contained in:
troosan 2020-07-06 08:34:30 +02:00 committed by GitHub
commit e7f70f3450
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 156 additions and 4 deletions

View File

@ -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 ``\``.

View File

@ -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);
}
/**

View File

@ -65,6 +65,7 @@ class Field extends Text
$instruction .= $this->buildPropertiesAndOptions($element);
}
$xmlWriter->startElement('w:r');
$this->writeFontStyle();
$xmlWriter->startElement('w:instrText');
$xmlWriter->writeAttribute('xml:space', 'preserve');
$xmlWriter->text($instruction);

View File

@ -44,6 +44,10 @@ class Font extends AbstractStyle
$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 @@ class Font extends AbstractStyle
$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();

View File

@ -296,6 +296,41 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$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();

View File

@ -51,6 +51,80 @@ class FontTest extends \PHPUnit\Framework\TestCase
$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
*/