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

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