diff --git a/samples/resources/Sample_11_ReadWord2007.docx b/samples/resources/Sample_11_ReadWord2007.docx index 406cf1e1..f6526360 100644 Binary files a/samples/resources/Sample_11_ReadWord2007.docx and b/samples/resources/Sample_11_ReadWord2007.docx differ diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 018604d3..09e6f1a2 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -122,14 +122,14 @@ class Font extends AbstractStyle * * @var bool */ - private $bold = false; + private $bold; /** * Italic * * @var bool */ - private $italic = false; + private $italic; /** * Undeline @@ -157,14 +157,14 @@ class Font extends AbstractStyle * * @var bool */ - private $strikethrough = false; + private $strikethrough; /** * Double strikethrough * * @var bool */ - private $doubleStrikethrough = false; + private $doubleStrikethrough; /** * Small caps @@ -172,7 +172,7 @@ class Font extends AbstractStyle * @var bool * @see http://www.schemacentral.com/sc/ooxml/e-w_smallCaps-1.html */ - private $smallCaps = false; + private $smallCaps; /** * All caps @@ -180,7 +180,7 @@ class Font extends AbstractStyle * @var bool * @see http://www.schemacentral.com/sc/ooxml/e-w_caps-1.html */ - private $allCaps = false; + private $allCaps; /** * Foreground/highlight @@ -235,7 +235,7 @@ class Font extends AbstractStyle * * @var bool */ - private $rtl = false; + private $rtl; /** * noProof (disables AutoCorrect) @@ -243,7 +243,7 @@ class Font extends AbstractStyle * @var bool * http://www.datypic.com/sc/ooxml/e-w_noProof-1.html */ - private $noProof = false; + private $noProof; /** * Languages @@ -258,7 +258,7 @@ class Font extends AbstractStyle * @var bool * @see http://www.datypic.com/sc/ooxml/e-w_vanish-1.html */ - private $hidden = false; + private $hidden; /** * Vertically Raised or Lowered Text diff --git a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php index 3236cead..fcd4aeb6 100644 --- a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php @@ -121,6 +121,21 @@ abstract class AbstractStyle } } + /** + * Writes boolean as 0 or 1 + * + * @param bool $value + * @return null|string + */ + protected function writeOnOf($value = null) + { + if ($value === null) { + return null; + } + + return $value ? '1' : '0'; + } + /** * Assemble style array into style string * diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index f299d8ef..dd4fac4f 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -107,21 +107,21 @@ class Font extends AbstractStyle $xmlWriter->writeElementIf($size !== null, 'w:szCs', 'w:val', $size * 2); // Bold, italic - $xmlWriter->writeElementIf($style->isBold(), 'w:b'); - $xmlWriter->writeElementIf($style->isBold(), 'w:bCs'); - $xmlWriter->writeElementIf($style->isItalic(), 'w:i'); - $xmlWriter->writeElementIf($style->isItalic(), 'w:iCs'); + $xmlWriter->writeElementIf($style->isBold() !== null, 'w:b', 'w:val', $this->writeOnOf($style->isBold())); + $xmlWriter->writeElementIf($style->isBold() !== null, 'w:bCs', 'w:val', $this->writeOnOf($style->isBold())); + $xmlWriter->writeElementIf($style->isItalic() !== null, 'w:i', 'w:val', $this->writeOnOf($style->isItalic())); + $xmlWriter->writeElementIf($style->isItalic() !== null, 'w:iCs', 'w:val', $this->writeOnOf($style->isItalic())); // Strikethrough, double strikethrough - $xmlWriter->writeElementIf($style->isStrikethrough(), 'w:strike'); - $xmlWriter->writeElementIf($style->isDoubleStrikethrough(), 'w:dstrike'); + $xmlWriter->writeElementIf($style->isStrikethrough() !== null, 'w:strike', 'w:val', $this->writeOnOf($style->isStrikethrough())); + $xmlWriter->writeElementIf($style->isDoubleStrikethrough() !== null, 'w:dstrike', 'w:val', $this->writeOnOf($style->isDoubleStrikethrough())); // Small caps, all caps - $xmlWriter->writeElementIf($style->isSmallCaps(), 'w:smallCaps'); - $xmlWriter->writeElementIf($style->isAllCaps(), 'w:caps'); + $xmlWriter->writeElementIf($style->isSmallCaps() !== null, 'w:smallCaps', 'w:val', $this->writeOnOf($style->isSmallCaps())); + $xmlWriter->writeElementIf($style->isAllCaps() !== null, 'w:caps', 'w:val', $this->writeOnOf($style->isAllCaps())); //Hidden text - $xmlWriter->writeElementIf($style->isHidden(), 'w:vanish'); + $xmlWriter->writeElementIf($style->isHidden(), 'w:vanish', 'w:val', $this->writeOnOf($style->isHidden())); // Underline $xmlWriter->writeElementIf($style->getUnderline() != 'none', 'w:u', 'w:val', $style->getUnderline()); @@ -139,7 +139,7 @@ class Font extends AbstractStyle $xmlWriter->writeElementIf($style->getKerning() !== null, 'w:kern', 'w:val', $style->getKerning() * 2); // noProof - $xmlWriter->writeElementIf($style->isNoProof() !== false, 'w:noProof'); + $xmlWriter->writeElementIf($style->isNoProof() !== null, 'w:noProof', $this->writeOnOf($style->isNoProof())); // Background-Color $shading = $style->getShading(); diff --git a/tests/PhpWord/Reader/Word2007/PartTest.php b/tests/PhpWord/Reader/Word2007/PartTest.php index 31a492b8..6b7d9294 100644 --- a/tests/PhpWord/Reader/Word2007/PartTest.php +++ b/tests/PhpWord/Reader/Word2007/PartTest.php @@ -160,4 +160,76 @@ class PartTest extends AbstractTestReader $this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $endnote->getElement(0)); $this->assertEquals('This is an endnote', $endnote->getElement(0)->getText()); } + + public function testReadHeadingWithOverriddenStyle() + { + $documentXml = ' + + + + + This is a bold + + + + + + heading + + + but with parts not in bold + + '; + + $stylesXml = ' + + + + + + + + + + + + + + + + + + + + + + + + + '; + + $phpWord = $this->getDocumentFromString(array('document' => $documentXml, 'styles' => $stylesXml)); + + $elements = $phpWord->getSection(0)->getElements(); + $this->assertInstanceOf('PhpOffice\PhpWord\Element\Title', $elements[0]); + /** @var \PhpOffice\PhpWord\Element\Title $title */ + $title = $elements[0]; + $this->assertEquals('Heading1', $title->getStyle()); + + /** @var \PhpOffice\PhpWord\Element\Text $text */ + $text = $title->getText()->getElement(0); + $this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text); + $this->assertEquals('This is a bold ', $text->getText()); + + /** @var \PhpOffice\PhpWord\Element\Text $text */ + $text = $title->getText()->getElement(1); + $this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text); + $this->assertEquals('heading', $text->getText()); + $this->assertFalse($text->getFontStyle()->isBold()); + + /** @var \PhpOffice\PhpWord\Element\Text $text */ + $text = $title->getText()->getElement(2); + $this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $text); + $this->assertEquals(' but with parts not in bold', $text->getText()); + } } diff --git a/tests/PhpWord/Reader/Word2007Test.php b/tests/PhpWord/Reader/Word2007Test.php index e4ea62de..6d660ce5 100644 --- a/tests/PhpWord/Reader/Word2007Test.php +++ b/tests/PhpWord/Reader/Word2007Test.php @@ -62,7 +62,7 @@ class Word2007Test extends \PHPUnit\Framework\TestCase $this->assertEquals(100, $phpWord->getSettings()->getZoom()); $doc = TestHelperDOCX::getDocument($phpWord); - $this->assertFalse($doc->elementExists('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b')); + $this->assertEquals('0', $doc->getElementAttribute('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b', 'w:val')); } /**