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.
This commit is contained in:
troosan 2018-12-30 00:45:45 +01:00
parent 1c35c4871a
commit 54e7c6dd5a
6 changed files with 107 additions and 20 deletions

View File

@ -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

View File

@ -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
*

View File

@ -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();

View File

@ -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 = '<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());
}
}

View File

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