Skip to content

Commit

Permalink
don't default styles to false
Browse files Browse the repository at this point in the history
this allows us to for instance make part of a Heading not bold, which
would otherwise be the default.
  • Loading branch information
troosan committed Dec 29, 2018
1 parent 1c35c48 commit 54e7c6d
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 20 deletions.
Binary file modified samples/resources/Sample_11_ReadWord2007.docx
Binary file not shown.
18 changes: 9 additions & 9 deletions src/PhpWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ class Font extends AbstractStyle
*
* @var bool
*/
private $bold = false;
private $bold;

/**
* Italic
*
* @var bool
*/
private $italic = false;
private $italic;

/**
* Undeline
Expand Down Expand Up @@ -157,30 +157,30 @@ class Font extends AbstractStyle
*
* @var bool
*/
private $strikethrough = false;
private $strikethrough;

/**
* Double strikethrough
*
* @var bool
*/
private $doubleStrikethrough = false;
private $doubleStrikethrough;

/**
* Small caps
*
* @var bool
* @see http://www.schemacentral.com/sc/ooxml/e-w_smallCaps-1.html
*/
private $smallCaps = false;
private $smallCaps;

/**
* All caps
*
* @var bool
* @see http://www.schemacentral.com/sc/ooxml/e-w_caps-1.html
*/
private $allCaps = false;
private $allCaps;

/**
* Foreground/highlight
Expand Down Expand Up @@ -235,15 +235,15 @@ class Font extends AbstractStyle
*
* @var bool
*/
private $rtl = false;
private $rtl;

/**
* noProof (disables AutoCorrect)
*
* @var bool
* http://www.datypic.com/sc/ooxml/e-w_noProof-1.html
*/
private $noProof = false;
private $noProof;

/**
* Languages
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/PhpWord/Writer/Word2007/Style/AbstractStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ protected function writeChildStyle(XMLWriter $xmlWriter, $name, $value)
}
}

/**
* 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
*
Expand Down
20 changes: 10 additions & 10 deletions src/PhpWord/Writer/Word2007/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,21 @@ private function writeStyle()
$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());
Expand All @@ -139,7 +139,7 @@ private function writeStyle()
$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();
Expand Down
72 changes: 72 additions & 0 deletions tests/PhpWord/Reader/Word2007/PartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,76 @@ public function testReadFootnote()
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $endnote->getElement(0));
$this->assertEquals('This is an endnote', $endnote->getElement(0)->getText());
}

public function testReadHeadingWithOverriddenStyle()
{
$documentXml = '<w:p>
<w:pPr>
<w:pStyle w:val="Heading1"/>
</w:pPr>
<w:r>
<w:t>This is a bold </w:t>
</w:r>
<w:r w:rsidRPr="00377798">
<w:rPr>
<w:b w:val="0"/>
</w:rPr>
<w:t>heading</w:t>
</w:r>
<w:r>
<w:t xml:space="preserve"> but with parts not in bold</w:t>
</w:r>
</w:p>';

$stylesXml = '<w:style w:type="paragraph" w:default="1" w:styleId="Normal">
<w:name w:val="Normal"/>
<w:qFormat/>
</w:style>
<w:style w:type="paragraph" w:styleId="Heading1">
<w:name w:val="heading 1"/>
<w:basedOn w:val="Normal"/>
<w:next w:val="Normal"/>
<w:link w:val="Heading1Char"/>
<w:uiPriority w:val="9"/>
<w:qFormat/>
<w:rsid w:val="00377798"/>
<w:pPr>
<w:keepNext/>
<w:keepLines/>
<w:spacing w:before="240"/>
<w:outlineLvl w:val="0"/>
</w:pPr>
<w:rPr>
<w:rFonts w:asciiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia" w:hAnsiTheme="majorHAnsi" w:cstheme="majorBidi"/>
<w:b/>
<w:color w:val="2F5496" w:themeColor="accent1" w:themeShade="BF"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:style>';

$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());
}
}
2 changes: 1 addition & 1 deletion tests/PhpWord/Reader/Word2007Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testLoad()
$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'));
}

/**
Expand Down

0 comments on commit 54e7c6d

Please sign in to comment.