make FontStyle basedOn paragraph if the paragraph is set on the font (#926)

* make FontStyle based on paragraph if it set
* replace tab with spaces
* basedOn not correctly set if FontStyle is based on other FontStyle
* Fix warnings
This commit is contained in:
troosan 2017-09-17 21:38:00 +02:00 committed by GitHub
parent 2fc685b4da
commit 8ce1a19ec4
35 changed files with 153 additions and 89 deletions

View File

@ -219,7 +219,6 @@ class Section extends AbstractContainer
} else {
throw new \Exception('Invalid header/footer type.');
}
}
/**

View File

@ -1303,7 +1303,7 @@ class MsDoc extends AbstractReader implements ReaderInterface
print_r('$sprm.ispmd : 0x'.dechex($sprm_IsPmd).PHP_EOL);
print_r('$sprm.f : 0x'.dechex($sprm_F).PHP_EOL);
print_r('$sprm.sgc : 0x'.dechex($sprm_Sgc));
switch(dechex($sprm_Sgc)) {
switch (dechex($sprm_Sgc)) {
case 0x01:
print_r(' (Paragraph property)');
break;
@ -1322,12 +1322,12 @@ class MsDoc extends AbstractReader implements ReaderInterface
}
print_r(PHP_EOL);
print_r('$sprm.spra : 0x'.dechex($sprm_Spra).PHP_EOL);
switch(dechex($sprm_Spra)) {
switch (dechex($sprm_Spra)) {
case 0x0:
$operand = self::getInt1d($this->dataWorkDocument, $offset);
$offset += 1;
$cb -= 1;
switch(dechex($operand)) {
switch (dechex($operand)) {
case 0x00:
$operand = 'OFF';
break;
@ -1376,9 +1376,9 @@ class MsDoc extends AbstractReader implements ReaderInterface
}
//
switch(dechex($sprm_Sgc)) {
switch (dechex($sprm_Sgc)) {
case 0x01: // Sprm is modifying a paragraph property.
switch($sprm_IsPmd) {
switch ($sprm_IsPmd) {
case 0x0A: // sprmPIlvl
print_r('sprmPIlvl : '.$operand.PHP_EOL.PHP_EOL);
break;
@ -1391,28 +1391,28 @@ class MsDoc extends AbstractReader implements ReaderInterface
}
break;
case 0x02: // Sprm is modifying a character property.
switch($sprm_IsPmd) {
switch ($sprm_IsPmd) {
default:
print_r('$sprm_IsPmd(2) : '.$sprm_IsPmd.PHP_EOL.PHP_EOL);
break;
}
break;
case 0x03: // Sprm is modifying a picture property.
switch($sprm_IsPmd) {
switch ($sprm_IsPmd) {
default:
print_r('$sprm_IsPmd(3) : '.$sprm_IsPmd.PHP_EOL.PHP_EOL);
break;
}
break;
case 0x04: // Sprm is modifying a section property.
switch($sprm_IsPmd) {
switch ($sprm_IsPmd) {
default:
print_r('$sprm_IsPmd(4) : '.$sprm_IsPmd.PHP_EOL.PHP_EOL);
break;
}
break;
case 0x05: // Sprm is modifying a table property.
switch($sprm_IsPmd) {
switch ($sprm_IsPmd) {
default:
print_r('$sprm_IsPmd(4) : '.$sprm_IsPmd.PHP_EOL.PHP_EOL);
break;
@ -1514,11 +1514,11 @@ class MsDoc extends AbstractReader implements ReaderInterface
$length = 0;
$operand = null;
switch(dechex($oSprm->spra)) {
switch (dechex($oSprm->spra)) {
case 0x0:
$operand = self::getInt1d($data, $pos);
$length = 1;
switch(dechex($operand)) {
switch (dechex($operand)) {
case 0x00:
$operand = false;
break;
@ -1593,7 +1593,7 @@ class MsDoc extends AbstractReader implements ReaderInterface
$cbNum -= $arrayReturn['length'];
$operand = $arrayReturn['operand'];
switch(dechex($oSprm->sgc)) {
switch (dechex($oSprm->sgc)) {
// Paragraph property
case 0x01:
break;
@ -1602,7 +1602,7 @@ class MsDoc extends AbstractReader implements ReaderInterface
if (!isset($oStylePrl->styleFont)) {
$oStylePrl->styleFont = array();
}
switch($oSprm->isPmd) {
switch ($oSprm->isPmd) {
// sprmCFRMarkIns
case 0x01:
break;
@ -1620,7 +1620,7 @@ class MsDoc extends AbstractReader implements ReaderInterface
// sprmCFItalic
case 0x36:
// By default, text is not italicized.
switch($operand) {
switch ($operand) {
case false:
case true:
$oStylePrl->styleFont['italic'] = $operand;
@ -1640,7 +1640,7 @@ class MsDoc extends AbstractReader implements ReaderInterface
// sprmCFBold
case 0x35:
// By default, text is not bold.
switch($operand) {
switch ($operand) {
case false:
case true:
$oStylePrl->styleFont['bold'] = $operand;
@ -1656,7 +1656,7 @@ class MsDoc extends AbstractReader implements ReaderInterface
// sprmCFStrike
case 0x37:
// By default, text is not struck through.
switch($operand) {
switch ($operand) {
case false:
case true:
$oStylePrl->styleFont['strikethrough'] = $operand;
@ -1671,7 +1671,7 @@ class MsDoc extends AbstractReader implements ReaderInterface
break;
// sprmCKul
case 0x3E:
switch(dechex($operand)) {
switch (dechex($operand)) {
case 0x00:
$oStylePrl->styleFont['underline'] = Style\Font::UNDERLINE_NONE;
break;
@ -1734,7 +1734,7 @@ class MsDoc extends AbstractReader implements ReaderInterface
// sprmCIco
//@link http://msdn.microsoft.com/en-us/library/dd773060%28v=office.12%29.aspx
case 0x42:
switch(dechex($operand)) {
switch (dechex($operand)) {
case 0x00:
case 0x01:
$oStylePrl->styleFont['color'] = '000000';
@ -1873,7 +1873,7 @@ class MsDoc extends AbstractReader implements ReaderInterface
if (!isset($oStylePrl->styleSection)) {
$oStylePrl->styleSection = array();
}
switch($oSprm->isPmd) {
switch ($oSprm->isPmd) {
// sprmSNfcPgn
case 0x0E:
// numbering format used for page numbers
@ -1925,7 +1925,6 @@ class MsDoc extends AbstractReader implements ReaderInterface
default:
// print_r('@todo Section : 0x'.dechex($oSprm->isPmd));
// print_r(PHP_EOL);
}
break;
// Table property
@ -2285,7 +2284,6 @@ class MsDoc extends AbstractReader implements ReaderInterface
}
}
}
}
}

View File

@ -44,7 +44,6 @@ class Content extends AbstractPart
foreach ($nodes as $node) {
// $styleName = $xmlReader->getAttribute('text:style-name', $node);
switch ($node->nodeName) {
case 'text:h': // Heading
$depth = $xmlReader->getAttribute('text:outline-level', $node);
$section->addTitle($node->nodeValue, $depth);

View File

@ -94,7 +94,6 @@ class Word2007 extends AbstractReader implements ReaderInterface
$part->setRels($relationships);
$part->read($phpWord);
}
}
/**

View File

@ -263,7 +263,6 @@ abstract class AbstractPart
foreach ($tblNodes as $tblNode) {
if ('w:tblGrid' == $tblNode->nodeName) { // Column
// @todo Do something with table columns
} elseif ('w:tr' == $tblNode->nodeName) { // Row
$rowHeight = $xmlReader->getAttribute('w:val', $tblNode, 'w:trPr/w:trHeight');
$rowHRule = $xmlReader->getAttribute('w:hRule', $tblNode, 'w:trPr/w:trHeight');
@ -279,7 +278,6 @@ abstract class AbstractPart
foreach ($rowNodes as $rowNode) {
if ('w:trPr' == $rowNode->nodeName) { // Row style
// @todo Do something with row style
} elseif ('w:tc' == $rowNode->nodeName) { // Cell
$cellWidth = $xmlReader->getAttribute('w:w', $rowNode, 'w:tcPr/w:tcW');
$cellStyle = null;

View File

@ -49,7 +49,6 @@ class Styles extends AbstractPart
preg_match('/Heading(\d)/', $name, $headingMatches);
// $default = ($xmlReader->getAttribute('w:default', $node) == 1);
switch ($type) {
case 'paragraph':
$paragraphStyle = $this->readParagraphStyle($xmlReader, $node);
$fontStyle = $this->readFontStyle($xmlReader, $node);

View File

@ -294,7 +294,6 @@ class OLERead
$offset += self::PROPERTY_STORAGE_BLOCK_SIZE;
}
}
/**

View File

@ -725,7 +725,7 @@ class Font extends AbstractStyle
}
/**
* Set shading
* Set Paragraph
*
* @param mixed $value
* @return self

View File

@ -134,7 +134,6 @@ abstract class AbstractElement
}
$this->xmlWriter->writeElementBlock('w:commentRangeStart', array('w:id' => $comment->getElementId()));
}
}

View File

@ -213,7 +213,6 @@ class Chart extends AbstractPart
$xmlWriter->endElement(); // c:ser
$index++;
}
}
/**

View File

@ -219,7 +219,6 @@ class Settings extends AbstractPart
private function setRevisionView(TrackChangesView $trackChangesView = null)
{
if ($trackChangesView != null) {
$revisionView['w:markup'] = $trackChangesView->hasMarkup() ? 'true': 'false';
$revisionView['w:comments'] = $trackChangesView->hasComments() ? 'true': 'false';
$revisionView['w:insDel'] = $trackChangesView->hasInsDel() ? 'true': 'false';

View File

@ -170,6 +170,9 @@ class Styles extends AbstractPart
$xmlWriter->startElement('w:link');
$xmlWriter->writeAttribute('w:val', $styleLink);
$xmlWriter->endElement();
} else if (!is_null($paragraphStyle)) {
// if type is 'paragraph' it should have a styleId
$xmlWriter->writeAttribute('w:styleId', $styleName);
}
// Style name
@ -178,7 +181,13 @@ class Styles extends AbstractPart
$xmlWriter->endElement();
// Parent style
$xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal');
if (!is_null($paragraphStyle)) {
if ($paragraphStyle->getStyleName() != null) {
$xmlWriter->writeElementBlock('w:basedOn', 'w:val', $paragraphStyle->getStyleName());
} elseif ($paragraphStyle->getBasedOn() != null) {
$xmlWriter->writeElementBlock('w:basedOn', 'w:val', $paragraphStyle->getBasedOn());
}
}
// w:pPr
if (!is_null($paragraphStyle)) {

View File

@ -99,7 +99,6 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
$phpWord->$method($styleId, array());
$this->assertInstanceOf("PhpOffice\\PhpWord\\Style\\{$value}", Style::getStyle($styleId));
}
}
/**

View File

@ -18,7 +18,10 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\TestHelperDOCX;
use PhpOffice\PhpWord\Writer\Word2007;
/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Styles
@ -74,4 +77,70 @@ class StylesTest extends \PHPUnit_Framework_TestCase
$element = $doc->getElement($path, $file);
$this->assertEquals('Normal', $element->getAttribute('w:val'));
}
public function testFontStyleBasedOn()
{
$phpWord = new PhpWord();
$baseParagraphStyle = new Paragraph();
$baseParagraphStyle->setAlignment(Jc::CENTER);
$baseParagraphStyle = $phpWord->addParagraphStyle('BaseStyle', $baseParagraphStyle);
$childFont = new Font();
$childFont->setParagraph($baseParagraphStyle);
$childFont->setSize(16);
$childFont = $phpWord->addFontStyle('ChildFontStyle', $childFont);
$otherFont = new Font();
$otherFont->setSize(20);
$otherFont = $phpWord->addFontStyle('OtherFontStyle', $otherFont);
$doc = TestHelperDOCX::getDocument($phpWord);
$file = 'word/styles.xml';
// Normal style generated?
$path = '/w:styles/w:style[@w:styleId="BaseStyle"]/w:name';
$element = $doc->getElement($path, $file);
$this->assertEquals('BaseStyle', $element->getAttribute('w:val'));
// Font style with paragraph should have it's base style set to that paragraphs style name
$path = '/w:styles/w:style[w:name/@w:val="ChildFontStyle"]/w:basedOn';
$element = $doc->getElement($path, $file);
$this->assertEquals('BaseStyle', $element->getAttribute('w:val'));
// Font style without paragraph should not have a base style set
$path = '/w:styles/w:style[w:name/@w:val="OtherFontStyle"]/w:basedOn';
$element = $doc->getElement($path, $file);
$this->assertNull($element);
}
public function testFontStyleBasedOnOtherFontStyle()
{
$phpWord = new PhpWord();
$styleGenerationP = new Paragraph();
$styleGenerationP->setAlignment(Jc::BOTH);
$styleGeneration = new Font();
$styleGeneration->setParagraph($styleGenerationP);
$styleGeneration->setSize(9.5);
$phpWord->addFontStyle('Generation', $styleGeneration);
$styleGenerationEteinteP = new Paragraph();
$styleGenerationEteinteP->setBasedOn('Generation');
$styleGenerationEteinte = new Font();
$styleGenerationEteinte->setParagraph($styleGenerationEteinteP);
$styleGenerationEteinte->setSize(8.5);
$phpWord->addFontStyle('GeneratEteinte', $styleGenerationEteinte);
$doc = TestHelperDOCX::getDocument($phpWord);
$file = 'word/styles.xml';
$path = '/w:styles/w:style[@w:styleId="GeneratEteinte"]/w:basedOn';
$element = $doc->getElement($path, $file);
$this->assertEquals('Generation', $element->getAttribute('w:val'));
}
}

View File

@ -96,7 +96,6 @@ class XmlDocument
if (null === $this->xpath) {
$this->xpath = new \DOMXpath($this->dom);
}
return $this->xpath->query($path);