Skip to content

Commit e7f70f3

Browse files
authored
Merge pull request #1774 from oleibman/fieldstyle
Add font style for Field elements
2 parents 733f845 + 8b2e21b commit e7f70f3

File tree

6 files changed

+156
-4
lines changed

6 files changed

+156
-4
lines changed

docs/elements.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ Currently the following fields are supported:
403403

404404
.. code-block:: php
405405
406-
$section->addField($fieldType, [$properties], [$options], [$fieldText])
406+
$section->addField($fieldType, [$properties], [$options], [$fieldText], [$fontStyle])
407+
408+
- ``$fontStyle``. See :ref:`font-style`.
407409

408410
See ``\PhpOffice\PhpWord\Element\Field`` for list of properties and options available for each field type.
409411
Options which are not specifically defined can be added. Those must start with a ``\``.

src/PhpWord/Element/Field.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Element;
1919

20+
use PhpOffice\PhpWord\Style\Font;
21+
2022
/**
2123
* Field element
2224
*
@@ -115,24 +117,58 @@ class Field extends AbstractElement
115117
/**
116118
* Font style
117119
*
118-
* @var \PhpOffice\PhpWord\Style\Font
120+
* @var string|\PhpOffice\PhpWord\Style\Font
119121
*/
120122
protected $fontStyle;
121123

124+
/**
125+
* Set Font style
126+
*
127+
* @param string|array|\PhpOffice\PhpWord\Style\Font $style
128+
* @return string|\PhpOffice\PhpWord\Style\Font
129+
*/
130+
public function setFontStyle($style = null)
131+
{
132+
if ($style instanceof Font) {
133+
$this->fontStyle = $style;
134+
} elseif (is_array($style)) {
135+
$this->fontStyle = new Font('text');
136+
$this->fontStyle->setStyleByArray($style);
137+
} elseif (null === $style) {
138+
$this->fontStyle = null;
139+
} else {
140+
$this->fontStyle = $style;
141+
}
142+
143+
return $this->fontStyle;
144+
}
145+
146+
/**
147+
* Get Font style
148+
*
149+
* @return string|\PhpOffice\PhpWord\Style\Font
150+
*/
151+
public function getFontStyle()
152+
{
153+
return $this->fontStyle;
154+
}
155+
122156
/**
123157
* Create a new Field Element
124158
*
125159
* @param string $type
126160
* @param array $properties
127161
* @param array $options
128162
* @param TextRun|string|null $text
163+
* @param string|array|\PhpOffice\PhpWord\Style\Font $fontStyle
129164
*/
130-
public function __construct($type = null, $properties = array(), $options = array(), $text = null)
165+
public function __construct($type = null, $properties = array(), $options = array(), $text = null, $fontStyle = null)
131166
{
132167
$this->setType($type);
133168
$this->setProperties($properties);
134169
$this->setOptions($options);
135170
$this->setText($text);
171+
$this->setFontStyle($fontStyle);
136172
}
137173

138174
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ private function writeDefault(\PhpOffice\PhpWord\Element\Field $element)
6565
$instruction .= $this->buildPropertiesAndOptions($element);
6666
}
6767
$xmlWriter->startElement('w:r');
68+
$this->writeFontStyle();
6869
$xmlWriter->startElement('w:instrText');
6970
$xmlWriter->writeAttribute('xml:space', 'preserve');
7071
$xmlWriter->text($instruction);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function write()
4444
$xmlWriter->startElement('w:rStyle');
4545
$xmlWriter->writeAttribute('w:val', $this->style);
4646
$xmlWriter->endElement();
47+
$style = \PhpOffice\PhpWord\Style::getStyle($this->style);
48+
if ($style instanceof \PhpOffice\PhpWord\Style\Font) {
49+
$xmlWriter->writeElementIf($style->isRTL(), 'w:rtl');
50+
}
4751
$xmlWriter->endElement();
4852
} else {
4953
$this->writeStyle();
@@ -139,7 +143,7 @@ private function writeStyle()
139143
$xmlWriter->writeElementIf($style->getKerning() !== null, 'w:kern', 'w:val', $style->getKerning() * 2);
140144

141145
// noProof
142-
$xmlWriter->writeElementIf($style->isNoProof() !== null, 'w:noProof', $this->writeOnOf($style->isNoProof()));
146+
$xmlWriter->writeElementIf($style->isNoProof() !== null, 'w:noProof', 'w:val', $this->writeOnOf($style->isNoProof()));
143147

144148
// Background-Color
145149
$shading = $style->getShading();

tests/PhpWord/Writer/Word2007/ElementTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,41 @@ public function testFieldElement()
296296
$this->assertEquals(' INDEX \\c "3" ', $doc->getElement($element)->textContent);
297297
}
298298

299+
public function testUnstyledFieldElement()
300+
{
301+
$phpWord = new PhpWord();
302+
$phpWord->addFontStyle('h1', array('name' => 'Courier New', 'size' => 8));
303+
$section = $phpWord->addSection();
304+
305+
$section->addField('PAGE');
306+
$doc = TestHelperDOCX::getDocument($phpWord);
307+
308+
$element = '/w:document/w:body/w:p/w:r[2]/w:instrText';
309+
$this->assertTrue($doc->elementExists($element));
310+
$this->assertEquals(' PAGE ', $doc->getElement($element)->textContent);
311+
$sty = '/w:document/w:body/w:p/w:r[2]/w:rPr';
312+
$this->assertFalse($doc->elementExists($sty));
313+
}
314+
315+
public function testStyledFieldElement()
316+
{
317+
$phpWord = new PhpWord();
318+
$stnam = 'h1';
319+
$phpWord->addFontStyle($stnam, array('name' => 'Courier New', 'size' => 8));
320+
$section = $phpWord->addSection();
321+
322+
$fld = $section->addField('PAGE');
323+
$fld->setFontStyle($stnam);
324+
$doc = TestHelperDOCX::getDocument($phpWord);
325+
326+
$element = '/w:document/w:body/w:p/w:r[2]/w:instrText';
327+
$this->assertTrue($doc->elementExists($element));
328+
$this->assertEquals(' PAGE ', $doc->getElement($element)->textContent);
329+
$sty = '/w:document/w:body/w:p/w:r[2]/w:rPr';
330+
$this->assertTrue($doc->elementExists($sty));
331+
$this->assertEquals($stnam, $doc->getElementAttribute($sty . '/w:rStyle', 'w:val'));
332+
}
333+
299334
public function testFieldElementWithComplexText()
300335
{
301336
$phpWord = new PhpWord();

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,80 @@ public function testFontRTL()
5151
$this->assertTrue($doc->elementExists($path, $file));
5252
}
5353

54+
public function testFontRTLNamed()
55+
{
56+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
57+
$stnam = 'fstyle';
58+
$phpWord->addFontStyle($stnam, array(
59+
'rtl' => true,
60+
'name' => 'Courier New',
61+
'size' => 8,
62+
));
63+
$section = $phpWord->addSection();
64+
$txt = 'היום יום שני'; // Translation = Today is Monday
65+
$section->addText($txt, $stnam);
66+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
67+
68+
$element = '/w:document/w:body/w:p/w:r';
69+
$txtelem = $element . '/w:t';
70+
$styelem = $element . '/w:rPr';
71+
$this->assertTrue($doc->elementExists($txtelem));
72+
$this->assertEquals($txt, $doc->getElement($txtelem)->textContent);
73+
$this->assertTrue($doc->elementExists($styelem));
74+
$this->assertTrue($doc->elementExists($styelem . '/w:rStyle'));
75+
$this->assertEquals($stnam, $doc->getElementAttribute($styelem . '/w:rStyle', 'w:val'));
76+
$this->assertTrue($doc->elementExists($styelem . '/w:rtl'));
77+
}
78+
79+
public function testFontNotRTLNamed()
80+
{
81+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
82+
$stnam = 'fstyle';
83+
$phpWord->addFontStyle($stnam, array(
84+
//'rtl' => true,
85+
'name' => 'Courier New',
86+
'size' => 8,
87+
));
88+
$section = $phpWord->addSection();
89+
$txt = 'היום יום שני'; // Translation = Today is Monday
90+
$section->addText($txt, $stnam);
91+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
92+
93+
$element = '/w:document/w:body/w:p/w:r';
94+
$txtelem = $element . '/w:t';
95+
$styelem = $element . '/w:rPr';
96+
$this->assertTrue($doc->elementExists($txtelem));
97+
$this->assertEquals($txt, $doc->getElement($txtelem)->textContent);
98+
$this->assertTrue($doc->elementExists($styelem));
99+
$this->assertTrue($doc->elementExists($styelem . '/w:rStyle'));
100+
$this->assertEquals($stnam, $doc->getElementAttribute($styelem . '/w:rStyle', 'w:val'));
101+
$this->assertFalse($doc->elementExists($styelem . '/w:rtl'));
102+
}
103+
104+
public function testNoProof()
105+
{
106+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
107+
$fontStyle = array(
108+
'noProof' => true,
109+
'name' => 'Courier New',
110+
'size' => 8,
111+
);
112+
$section = $phpWord->addSection();
113+
$txt = 'spellung error';
114+
$section->addText($txt, $fontStyle);
115+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
116+
117+
$element = '/w:document/w:body/w:p/w:r';
118+
$txtelem = $element . '/w:t';
119+
$styelem = $element . '/w:rPr';
120+
$noproofelem = $styelem . '/w:noProof';
121+
$this->assertTrue($doc->elementExists($txtelem));
122+
$this->assertEquals($txt, $doc->getElement($txtelem)->textContent);
123+
$this->assertTrue($doc->elementExists($styelem));
124+
$this->assertTrue($doc->elementExists($noproofelem));
125+
$this->assertEquals('1', $doc->getElementAttribute($noproofelem, 'w:val'));
126+
}
127+
54128
/**
55129
* Test writing font with language
56130
*/

0 commit comments

Comments
 (0)