Skip to content

Commit 54e7c6d

Browse files
committed
don't default styles to false
this allows us to for instance make part of a Heading not bold, which would otherwise be the default.
1 parent 1c35c48 commit 54e7c6d

File tree

6 files changed

+107
-20
lines changed

6 files changed

+107
-20
lines changed
444 Bytes
Binary file not shown.

src/PhpWord/Style/Font.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ class Font extends AbstractStyle
122122
*
123123
* @var bool
124124
*/
125-
private $bold = false;
125+
private $bold;
126126

127127
/**
128128
* Italic
129129
*
130130
* @var bool
131131
*/
132-
private $italic = false;
132+
private $italic;
133133

134134
/**
135135
* Undeline
@@ -157,30 +157,30 @@ class Font extends AbstractStyle
157157
*
158158
* @var bool
159159
*/
160-
private $strikethrough = false;
160+
private $strikethrough;
161161

162162
/**
163163
* Double strikethrough
164164
*
165165
* @var bool
166166
*/
167-
private $doubleStrikethrough = false;
167+
private $doubleStrikethrough;
168168

169169
/**
170170
* Small caps
171171
*
172172
* @var bool
173173
* @see http://www.schemacentral.com/sc/ooxml/e-w_smallCaps-1.html
174174
*/
175-
private $smallCaps = false;
175+
private $smallCaps;
176176

177177
/**
178178
* All caps
179179
*
180180
* @var bool
181181
* @see http://www.schemacentral.com/sc/ooxml/e-w_caps-1.html
182182
*/
183-
private $allCaps = false;
183+
private $allCaps;
184184

185185
/**
186186
* Foreground/highlight
@@ -235,15 +235,15 @@ class Font extends AbstractStyle
235235
*
236236
* @var bool
237237
*/
238-
private $rtl = false;
238+
private $rtl;
239239

240240
/**
241241
* noProof (disables AutoCorrect)
242242
*
243243
* @var bool
244244
* http://www.datypic.com/sc/ooxml/e-w_noProof-1.html
245245
*/
246-
private $noProof = false;
246+
private $noProof;
247247

248248
/**
249249
* Languages
@@ -258,7 +258,7 @@ class Font extends AbstractStyle
258258
* @var bool
259259
* @see http://www.datypic.com/sc/ooxml/e-w_vanish-1.html
260260
*/
261-
private $hidden = false;
261+
private $hidden;
262262

263263
/**
264264
* Vertically Raised or Lowered Text

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ protected function writeChildStyle(XMLWriter $xmlWriter, $name, $value)
121121
}
122122
}
123123

124+
/**
125+
* Writes boolean as 0 or 1
126+
*
127+
* @param bool $value
128+
* @return null|string
129+
*/
130+
protected function writeOnOf($value = null)
131+
{
132+
if ($value === null) {
133+
return null;
134+
}
135+
136+
return $value ? '1' : '0';
137+
}
138+
124139
/**
125140
* Assemble style array into style string
126141
*

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,21 @@ private function writeStyle()
107107
$xmlWriter->writeElementIf($size !== null, 'w:szCs', 'w:val', $size * 2);
108108

109109
// Bold, italic
110-
$xmlWriter->writeElementIf($style->isBold(), 'w:b');
111-
$xmlWriter->writeElementIf($style->isBold(), 'w:bCs');
112-
$xmlWriter->writeElementIf($style->isItalic(), 'w:i');
113-
$xmlWriter->writeElementIf($style->isItalic(), 'w:iCs');
110+
$xmlWriter->writeElementIf($style->isBold() !== null, 'w:b', 'w:val', $this->writeOnOf($style->isBold()));
111+
$xmlWriter->writeElementIf($style->isBold() !== null, 'w:bCs', 'w:val', $this->writeOnOf($style->isBold()));
112+
$xmlWriter->writeElementIf($style->isItalic() !== null, 'w:i', 'w:val', $this->writeOnOf($style->isItalic()));
113+
$xmlWriter->writeElementIf($style->isItalic() !== null, 'w:iCs', 'w:val', $this->writeOnOf($style->isItalic()));
114114

115115
// Strikethrough, double strikethrough
116-
$xmlWriter->writeElementIf($style->isStrikethrough(), 'w:strike');
117-
$xmlWriter->writeElementIf($style->isDoubleStrikethrough(), 'w:dstrike');
116+
$xmlWriter->writeElementIf($style->isStrikethrough() !== null, 'w:strike', 'w:val', $this->writeOnOf($style->isStrikethrough()));
117+
$xmlWriter->writeElementIf($style->isDoubleStrikethrough() !== null, 'w:dstrike', 'w:val', $this->writeOnOf($style->isDoubleStrikethrough()));
118118

119119
// Small caps, all caps
120-
$xmlWriter->writeElementIf($style->isSmallCaps(), 'w:smallCaps');
121-
$xmlWriter->writeElementIf($style->isAllCaps(), 'w:caps');
120+
$xmlWriter->writeElementIf($style->isSmallCaps() !== null, 'w:smallCaps', 'w:val', $this->writeOnOf($style->isSmallCaps()));
121+
$xmlWriter->writeElementIf($style->isAllCaps() !== null, 'w:caps', 'w:val', $this->writeOnOf($style->isAllCaps()));
122122

123123
//Hidden text
124-
$xmlWriter->writeElementIf($style->isHidden(), 'w:vanish');
124+
$xmlWriter->writeElementIf($style->isHidden(), 'w:vanish', 'w:val', $this->writeOnOf($style->isHidden()));
125125

126126
// Underline
127127
$xmlWriter->writeElementIf($style->getUnderline() != 'none', 'w:u', 'w:val', $style->getUnderline());
@@ -139,7 +139,7 @@ private function writeStyle()
139139
$xmlWriter->writeElementIf($style->getKerning() !== null, 'w:kern', 'w:val', $style->getKerning() * 2);
140140

141141
// noProof
142-
$xmlWriter->writeElementIf($style->isNoProof() !== false, 'w:noProof');
142+
$xmlWriter->writeElementIf($style->isNoProof() !== null, 'w:noProof', $this->writeOnOf($style->isNoProof()));
143143

144144
// Background-Color
145145
$shading = $style->getShading();

tests/PhpWord/Reader/Word2007/PartTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,76 @@ public function testReadFootnote()
160160
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $endnote->getElement(0));
161161
$this->assertEquals('This is an endnote', $endnote->getElement(0)->getText());
162162
}
163+
164+
public function testReadHeadingWithOverriddenStyle()
165+
{
166+
$documentXml = '<w:p>
167+
<w:pPr>
168+
<w:pStyle w:val="Heading1"/>
169+
</w:pPr>
170+
<w:r>
171+
<w:t>This is a bold </w:t>
172+
</w:r>
173+
<w:r w:rsidRPr="00377798">
174+
<w:rPr>
175+
<w:b w:val="0"/>
176+
</w:rPr>
177+
<w:t>heading</w:t>
178+
</w:r>
179+
<w:r>
180+
<w:t xml:space="preserve"> but with parts not in bold</w:t>
181+
</w:r>
182+
</w:p>';
183+
184+
$stylesXml = '<w:style w:type="paragraph" w:default="1" w:styleId="Normal">
185+
<w:name w:val="Normal"/>
186+
<w:qFormat/>
187+
</w:style>
188+
<w:style w:type="paragraph" w:styleId="Heading1">
189+
<w:name w:val="heading 1"/>
190+
<w:basedOn w:val="Normal"/>
191+
<w:next w:val="Normal"/>
192+
<w:link w:val="Heading1Char"/>
193+
<w:uiPriority w:val="9"/>
194+
<w:qFormat/>
195+
<w:rsid w:val="00377798"/>
196+
<w:pPr>
197+
<w:keepNext/>
198+
<w:keepLines/>
199+
<w:spacing w:before="240"/>
200+
<w:outlineLvl w:val="0"/>
201+
</w:pPr>
202+
<w:rPr>
203+
<w:rFonts w:asciiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia" w:hAnsiTheme="majorHAnsi" w:cstheme="majorBidi"/>
204+
<w:b/>
205+
<w:color w:val="2F5496" w:themeColor="accent1" w:themeShade="BF"/>
206+
<w:sz w:val="32"/>
207+
<w:szCs w:val="32"/>
208+
</w:rPr>
209+
</w:style>';
210+
211+
$phpWord = $this->getDocumentFromString(array('document' => $documentXml, 'styles' => $stylesXml));
212+
213+
$elements = $phpWord->getSection(0)->getElements();
214+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Title', $elements[0]);
215+
/** @var \PhpOffice\PhpWord\Element\Title $title */
216+
$title = $elements[0];
217+
$this->assertEquals('Heading1', $title->getStyle());
218+
219+
/** @var \PhpOffice\PhpWord\Element\Text $text */
220+
$text = $title->getText()->getElement(0);
221+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text);
222+
$this->assertEquals('This is a bold ', $text->getText());
223+
224+
/** @var \PhpOffice\PhpWord\Element\Text $text */
225+
$text = $title->getText()->getElement(1);
226+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text);
227+
$this->assertEquals('heading', $text->getText());
228+
$this->assertFalse($text->getFontStyle()->isBold());
229+
230+
/** @var \PhpOffice\PhpWord\Element\Text $text */
231+
$text = $title->getText()->getElement(2);
232+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text);
233+
$this->assertEquals(' but with parts not in bold', $text->getText());
234+
}
163235
}

tests/PhpWord/Reader/Word2007Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testLoad()
6262
$this->assertEquals(100, $phpWord->getSettings()->getZoom());
6363

6464
$doc = TestHelperDOCX::getDocument($phpWord);
65-
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b'));
65+
$this->assertEquals('0', $doc->getElementAttribute('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b', 'w:val'));
6666
}
6767

6868
/**

0 commit comments

Comments
 (0)