fix reading of docx default style (#1238)
This commit is contained in:
parent
4a530d1d97
commit
99b04f0353
@ -9,6 +9,9 @@ v0.15.0 (?? ??? 2018)
|
|||||||
- Parsing of "align" HTML attribute - @troosan #1231
|
- Parsing of "align" HTML attribute - @troosan #1231
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
- fix reading of docx default style - @troosan #1238
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
v0.14.0 (29 Dec 2017)
|
v0.14.0 (29 Dec 2017)
|
||||||
----------------------
|
----------------------
|
||||||
@ -58,6 +61,8 @@ This version brings compatibility with PHP 7.0 & 7.1
|
|||||||
### Deprecated
|
### Deprecated
|
||||||
- PhpWord->getProtection(), get it from the settings instead PhpWord->getSettings()->getDocumentProtection();
|
- PhpWord->getProtection(), get it from the settings instead PhpWord->getSettings()->getDocumentProtection();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
v0.13.0 (31 July 2016)
|
v0.13.0 (31 July 2016)
|
||||||
-------------------
|
-------------------
|
||||||
This release brings several improvements in `TemplateProcessor`, automatic output escaping feature for OOXML, ODF, HTML, and RTF (turned off, by default).
|
This release brings several improvements in `TemplateProcessor`, automatic output escaping feature for OOXML, ODF, HTML, and RTF (turned off, by default).
|
||||||
|
|||||||
@ -353,7 +353,6 @@ The footnote numbering can be controlled by setting the FootnoteProperties on th
|
|||||||
$fp->setNumStart(2);
|
$fp->setNumStart(2);
|
||||||
//when to restart counting (continuous (default), eachSect, eachPage)
|
//when to restart counting (continuous (default), eachSect, eachPage)
|
||||||
$fp->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_PAGE);
|
$fp->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_PAGE);
|
||||||
|
|
||||||
//And finaly, set it on the Section
|
//And finaly, set it on the Section
|
||||||
$section->setFootnoteProperties($properties);
|
$section->setFootnoteProperties($properties);
|
||||||
|
|
||||||
|
|||||||
@ -167,7 +167,6 @@ Use mirror margins to set up facing pages for double-sided documents, such as bo
|
|||||||
|
|
||||||
$phpWord->getSettings()->setMirrorMargins(true);
|
$phpWord->getSettings()->setMirrorMargins(true);
|
||||||
|
|
||||||
|
|
||||||
Spelling and grammatical checks
|
Spelling and grammatical checks
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|||||||
@ -51,7 +51,6 @@ Example:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Using samples
|
Using samples
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
|
|||||||
@ -307,7 +307,7 @@ abstract class AbstractPart
|
|||||||
|
|
||||||
$styleNode = $xmlReader->getElement('w:pPr', $domNode);
|
$styleNode = $xmlReader->getElement('w:pPr', $domNode);
|
||||||
$styleDefs = array(
|
$styleDefs = array(
|
||||||
'styleName' => array(self::READ_VALUE, 'w:pStyle'),
|
'styleName' => array(self::READ_VALUE, array('w:pStyle', 'w:name')),
|
||||||
'alignment' => array(self::READ_VALUE, 'w:jc'),
|
'alignment' => array(self::READ_VALUE, 'w:jc'),
|
||||||
'basedOn' => array(self::READ_VALUE, 'w:basedOn'),
|
'basedOn' => array(self::READ_VALUE, 'w:basedOn'),
|
||||||
'next' => array(self::READ_VALUE, 'w:next'),
|
'next' => array(self::READ_VALUE, 'w:next'),
|
||||||
@ -349,9 +349,9 @@ abstract class AbstractPart
|
|||||||
$styleNode = $xmlReader->getElement('w:rPr', $domNode);
|
$styleNode = $xmlReader->getElement('w:rPr', $domNode);
|
||||||
$styleDefs = array(
|
$styleDefs = array(
|
||||||
'styleName' => array(self::READ_VALUE, 'w:rStyle'),
|
'styleName' => array(self::READ_VALUE, 'w:rStyle'),
|
||||||
'name' => array(self::READ_VALUE, 'w:rFonts', 'w:ascii'),
|
'name' => array(self::READ_VALUE, 'w:rFonts', array('w:ascii', 'w:hAnsi', 'w:eastAsia', 'w:cs')),
|
||||||
'hint' => array(self::READ_VALUE, 'w:rFonts', 'w:hint'),
|
'hint' => array(self::READ_VALUE, 'w:rFonts', 'w:hint'),
|
||||||
'size' => array(self::READ_SIZE, 'w:sz'),
|
'size' => array(self::READ_SIZE, array('w:sz', 'w:szCs')),
|
||||||
'color' => array(self::READ_VALUE, 'w:color'),
|
'color' => array(self::READ_VALUE, 'w:color'),
|
||||||
'underline' => array(self::READ_VALUE, 'w:u'),
|
'underline' => array(self::READ_VALUE, 'w:u'),
|
||||||
'bold' => array(self::READ_TRUE, 'w:b'),
|
'bold' => array(self::READ_TRUE, 'w:b'),
|
||||||
@ -364,9 +364,7 @@ abstract class AbstractPart
|
|||||||
'subScript' => array(self::READ_EQUAL, 'w:vertAlign', 'w:val', 'subscript'),
|
'subScript' => array(self::READ_EQUAL, 'w:vertAlign', 'w:val', 'subscript'),
|
||||||
'fgColor' => array(self::READ_VALUE, 'w:highlight'),
|
'fgColor' => array(self::READ_VALUE, 'w:highlight'),
|
||||||
'rtl' => array(self::READ_TRUE, 'w:rtl'),
|
'rtl' => array(self::READ_TRUE, 'w:rtl'),
|
||||||
'font-latin' => array(self::READ_VALUE, 'w:font', 'w:val'),
|
'lang' => array(self::READ_VALUE, 'w:lang'),
|
||||||
'font-eastAsia' => array(self::READ_VALUE, 'w:font', 'w:eastAsia'),
|
|
||||||
'font-bidi' => array(self::READ_VALUE, 'w:font', 'w:bidi'),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
|
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
|
||||||
@ -400,6 +398,7 @@ abstract class AbstractPart
|
|||||||
$ucfSide = ucfirst($side);
|
$ucfSide = ucfirst($side);
|
||||||
$styleDefs["border{$ucfSide}Size"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:sz');
|
$styleDefs["border{$ucfSide}Size"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:sz');
|
||||||
$styleDefs["border{$ucfSide}Color"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:color');
|
$styleDefs["border{$ucfSide}Color"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:color');
|
||||||
|
$styleDefs["border{$ucfSide}Style"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:val');
|
||||||
}
|
}
|
||||||
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
|
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
|
||||||
}
|
}
|
||||||
@ -428,6 +427,54 @@ abstract class AbstractPart
|
|||||||
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
|
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first child element found
|
||||||
|
*
|
||||||
|
* @param XMLReader $xmlReader
|
||||||
|
* @param \DOMElement $parentNode
|
||||||
|
* @param string|array $elements
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
private function findPossibleElement(XMLReader $xmlReader, \DOMElement $parentNode = null, $elements)
|
||||||
|
{
|
||||||
|
if (is_array($elements)) {
|
||||||
|
//if element is an array, we take the first element that exists in the XML
|
||||||
|
foreach ($elements as $possibleElement) {
|
||||||
|
if ($xmlReader->elementExists($possibleElement, $parentNode)) {
|
||||||
|
return $possibleElement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return $elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first attribute found
|
||||||
|
*
|
||||||
|
* @param XMLReader $xmlReader
|
||||||
|
* @param \DOMElement $node
|
||||||
|
* @param string|array $attributes
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
private function findPossibleAttribute(XMLReader $xmlReader, \DOMElement $node, $attributes)
|
||||||
|
{
|
||||||
|
//if attribute is an array, we take the first attribute that exists in the XML
|
||||||
|
if (is_array($attributes)) {
|
||||||
|
foreach ($attributes as $possibleAttribute) {
|
||||||
|
if ($xmlReader->getAttribute($possibleAttribute, $node)) {
|
||||||
|
return $possibleAttribute;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return $attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read style definition
|
* Read style definition
|
||||||
*
|
*
|
||||||
@ -442,11 +489,18 @@ abstract class AbstractPart
|
|||||||
$styles = array();
|
$styles = array();
|
||||||
|
|
||||||
foreach ($styleDefs as $styleProp => $styleVal) {
|
foreach ($styleDefs as $styleProp => $styleVal) {
|
||||||
@list($method, $element, $attribute, $expected) = $styleVal;
|
list($method, $element, $attribute, $expected) = array_pad($styleVal, 4, null);
|
||||||
|
|
||||||
|
$element = $this->findPossibleElement($xmlReader, $parentNode, $element);
|
||||||
|
if ($element === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ($xmlReader->elementExists($element, $parentNode)) {
|
if ($xmlReader->elementExists($element, $parentNode)) {
|
||||||
$node = $xmlReader->getElement($element, $parentNode);
|
$node = $xmlReader->getElement($element, $parentNode);
|
||||||
|
|
||||||
|
$attribute = $this->findPossibleAttribute($xmlReader, $node, $attribute);
|
||||||
|
|
||||||
// Use w:val as default if no attribute assigned
|
// Use w:val as default if no attribute assigned
|
||||||
$attribute = ($attribute === null) ? 'w:val' : $attribute;
|
$attribute = ($attribute === null) ? 'w:val' : $attribute;
|
||||||
$attributeValue = $xmlReader->getAttribute($attribute, $node);
|
$attributeValue = $xmlReader->getAttribute($attribute, $node);
|
||||||
|
|||||||
@ -80,8 +80,8 @@ class Settings extends AbstractPart
|
|||||||
|
|
||||||
$themeFontLang = new Language();
|
$themeFontLang = new Language();
|
||||||
$themeFontLang->setLatin($val);
|
$themeFontLang->setLatin($val);
|
||||||
$themeFontLang->setLatin($eastAsia);
|
$themeFontLang->setEastAsia($eastAsia);
|
||||||
$themeFontLang->setLatin($bidi);
|
$themeFontLang->setBidirectional($bidi);
|
||||||
|
|
||||||
$phpWord->getSettings()->setThemeFontLang($themeFontLang);
|
$phpWord->getSettings()->setThemeFontLang($themeFontLang);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Reader\Word2007;
|
|||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
use PhpOffice\Common\XMLReader;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Style\Language;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Styles reader
|
* Styles reader
|
||||||
@ -37,6 +38,28 @@ class Styles extends AbstractPart
|
|||||||
$xmlReader = new XMLReader();
|
$xmlReader = new XMLReader();
|
||||||
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
|
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
|
||||||
|
|
||||||
|
$fontDefaults = $xmlReader->getElement('w:docDefaults/w:rPrDefault');
|
||||||
|
if ($fontDefaults !== null) {
|
||||||
|
$fontDefaultStyle = $this->readFontStyle($xmlReader, $fontDefaults);
|
||||||
|
if (array_key_exists('name', $fontDefaultStyle)) {
|
||||||
|
$phpWord->setDefaultFontName($fontDefaultStyle['name']);
|
||||||
|
}
|
||||||
|
if (array_key_exists('size', $fontDefaultStyle)) {
|
||||||
|
$phpWord->setDefaultFontSize($fontDefaultStyle['size']);
|
||||||
|
}
|
||||||
|
if (array_key_exists('lang', $fontDefaultStyle)) {
|
||||||
|
$phpWord->getSettings()->setThemeFontLang(new Language($fontDefaultStyle['lang']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$paragraphDefaults = $xmlReader->getElement('w:docDefaults/w:pPrDefault');
|
||||||
|
if ($paragraphDefaults !== null) {
|
||||||
|
$paragraphDefaultStyle = $this->readParagraphStyle($xmlReader, $paragraphDefaults);
|
||||||
|
if ($paragraphDefaultStyle != null) {
|
||||||
|
$phpWord->setDefaultParagraphStyle();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$nodes = $xmlReader->getElements('w:style');
|
$nodes = $xmlReader->getElements('w:style');
|
||||||
if ($nodes->length > 0) {
|
if ($nodes->length > 0) {
|
||||||
foreach ($nodes as $node) {
|
foreach ($nodes as $node) {
|
||||||
|
|||||||
@ -20,7 +20,6 @@ namespace PhpOffice\PhpWord\Style;
|
|||||||
use PhpOffice\Common\Text;
|
use PhpOffice\Common\Text;
|
||||||
use PhpOffice\PhpWord\Exception\InvalidStyleException;
|
use PhpOffice\PhpWord\Exception\InvalidStyleException;
|
||||||
use PhpOffice\PhpWord\SimpleType\Jc;
|
use PhpOffice\PhpWord\SimpleType\Jc;
|
||||||
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
|
|
||||||
use PhpOffice\PhpWord\SimpleType\TextAlignment;
|
use PhpOffice\PhpWord\SimpleType\TextAlignment;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -154,12 +154,12 @@ class Shape extends AbstractElement
|
|||||||
case 'arc':
|
case 'arc':
|
||||||
case 'line':
|
case 'line':
|
||||||
$points = explode(' ', $value);
|
$points = explode(' ', $value);
|
||||||
@list($start, $end) = $points;
|
list($start, $end) = array_pad($points, 2, null);
|
||||||
$points = array('start' => $start, 'end' => $end);
|
$points = array('start' => $start, 'end' => $end);
|
||||||
break;
|
break;
|
||||||
case 'curve':
|
case 'curve':
|
||||||
$points = explode(' ', $value);
|
$points = explode(' ', $value);
|
||||||
@list($start, $end, $point1, $point2) = $points;
|
list($start, $end, $point1, $point2) = array_pad($points, 4, null);
|
||||||
$points = array('start' => $start, 'end' => $end, 'point1' => $point1, 'point2' => $point2);
|
$points = array('start' => $start, 'end' => $end, 'point1' => $point1, 'point2' => $point2);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,6 @@
|
|||||||
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
|
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLWriter;
|
use PhpOffice\Common\XMLWriter;
|
||||||
use PhpOffice\PhpWord\Settings as PhpWordSettings;
|
|
||||||
use PhpOffice\PhpWord\Style;
|
use PhpOffice\PhpWord\Style;
|
||||||
use PhpOffice\PhpWord\Style\Font as FontStyle;
|
use PhpOffice\PhpWord\Style\Font as FontStyle;
|
||||||
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
|
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
|
||||||
@ -82,9 +81,10 @@ class Styles extends AbstractPart
|
|||||||
*/
|
*/
|
||||||
private function writeDefaultStyles(XMLWriter $xmlWriter, $styles)
|
private function writeDefaultStyles(XMLWriter $xmlWriter, $styles)
|
||||||
{
|
{
|
||||||
$fontName = PhpWordSettings::getDefaultFontName();
|
$phpWord = $this->getParentWriter()->getPhpWord();
|
||||||
$fontSize = PhpWordSettings::getDefaultFontSize();
|
$fontName = $phpWord->getDefaultFontName();
|
||||||
$language = $this->getParentWriter()->getPhpWord()->getSettings()->getThemeFontLang();
|
$fontSize = $phpWord->getDefaultFontSize();
|
||||||
|
$language = $phpWord->getSettings()->getThemeFontLang();
|
||||||
$latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin();
|
$latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin();
|
||||||
|
|
||||||
// Default font
|
// Default font
|
||||||
@ -123,7 +123,18 @@ class Styles extends AbstractPart
|
|||||||
$xmlWriter->writeAttribute('w:val', 'Normal');
|
$xmlWriter->writeAttribute('w:val', 'Normal');
|
||||||
$xmlWriter->endElement(); // w:name
|
$xmlWriter->endElement(); // w:name
|
||||||
if (isset($styles['Normal'])) {
|
if (isset($styles['Normal'])) {
|
||||||
$styleWriter = new ParagraphStyleWriter($xmlWriter, $styles['Normal']);
|
$normalStyle = $styles['Normal'];
|
||||||
|
// w:pPr
|
||||||
|
if ($normalStyle instanceof Fontstyle && $normalStyle->getParagraph() != null) {
|
||||||
|
$styleWriter = new ParagraphStyleWriter($xmlWriter, $normalStyle->getParagraph());
|
||||||
|
$styleWriter->write();
|
||||||
|
} elseif ($normalStyle instanceof ParagraphStyle) {
|
||||||
|
$styleWriter = new ParagraphStyleWriter($xmlWriter, $normalStyle);
|
||||||
|
$styleWriter->write();
|
||||||
|
}
|
||||||
|
|
||||||
|
// w:rPr
|
||||||
|
$styleWriter = new FontStyleWriter($xmlWriter, $normalStyle);
|
||||||
$styleWriter->write();
|
$styleWriter->write();
|
||||||
}
|
}
|
||||||
$xmlWriter->endElement(); // w:style
|
$xmlWriter->endElement(); // w:style
|
||||||
|
|||||||
@ -109,7 +109,7 @@ class Paragraph extends AbstractStyle
|
|||||||
//Paragraph contextualSpacing
|
//Paragraph contextualSpacing
|
||||||
$xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing');
|
$xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing');
|
||||||
|
|
||||||
//Paragraph contextualSpacing
|
//Paragraph textAlignment
|
||||||
$xmlWriter->writeElementIf($styles['textAlignment'] !== null, 'w:textAlignment', 'w:val', $styles['textAlignment']);
|
$xmlWriter->writeElementIf($styles['textAlignment'] !== null, 'w:textAlignment', 'w:val', $styles['textAlignment']);
|
||||||
|
|
||||||
// Child style: alignment, indentation, spacing, and shading
|
// Child style: alignment, indentation, spacing, and shading
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user