This commit is contained in:
wangjinlei
2022-11-23 14:48:15 +08:00
parent 0a92e69b83
commit ec59e99a8b
636 changed files with 59164 additions and 46329 deletions

View File

@@ -0,0 +1,112 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader;
use PhpOffice\PhpWord\Exception\Exception;
/**
* Reader abstract class.
*
* @since 0.8.0
*
* @codeCoverageIgnore Abstract class
*/
abstract class AbstractReader implements ReaderInterface
{
/**
* Read data only?
*
* @var bool
*/
protected $readDataOnly = true;
/**
* File pointer.
*
* @var bool|resource
*/
protected $fileHandle;
/**
* Read data only?
*
* @return bool
*/
public function isReadDataOnly()
{
// return $this->readDataOnly;
return true;
}
/**
* Set read data only.
*
* @param bool $value
*
* @return self
*/
public function setReadDataOnly($value = true)
{
$this->readDataOnly = $value;
return $this;
}
/**
* Open file for reading.
*
* @param string $filename
*
* @return resource
*/
protected function openFile($filename)
{
// Check if file exists
if (!file_exists($filename) || !is_readable($filename)) {
throw new Exception("Could not open $filename for reading! File does not exist.");
}
// Open file
$this->fileHandle = fopen($filename, 'rb');
if ($this->fileHandle === false) {
throw new Exception("Could not open file $filename for reading.");
}
}
/**
* Can the current ReaderInterface read the file?
*
* @param string $filename
*
* @return bool
*/
public function canRead($filename)
{
// Check if file exists
try {
$this->openFile($filename);
} catch (Exception $e) {
return false;
}
if (is_resource($this->fileHandle)) {
fclose($this->fileHandle);
}
return true;
}
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader;
use Exception;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html as HTMLParser;
/**
* HTML Reader class.
*
* @since 0.11.0
*/
class HTML extends AbstractReader implements ReaderInterface
{
/**
* Loads PhpWord from file.
*
* @param string $docFile
*
* @return \PhpOffice\PhpWord\PhpWord
*/
public function load($docFile)
{
$phpWord = new PhpWord();
if ($this->canRead($docFile)) {
$section = $phpWord->addSection();
HTMLParser::addHtml($section, file_get_contents($docFile), true);
} else {
throw new Exception("Cannot read {$docFile}.");
}
return $phpWord;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,95 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Reader for ODText.
*
* @since 0.10.0
*/
class ODText extends AbstractReader implements ReaderInterface
{
/**
* Loads PhpWord from file.
*
* @param string $docFile
*
* @return \PhpOffice\PhpWord\PhpWord
*/
public function load($docFile)
{
$phpWord = new PhpWord();
$relationships = $this->readRelationships($docFile);
$readerParts = [
'content.xml' => 'Content',
'meta.xml' => 'Meta',
];
foreach ($readerParts as $xmlFile => $partName) {
$this->readPart($phpWord, $relationships, $partName, $docFile, $xmlFile);
}
return $phpWord;
}
/**
* Read document part.
*
* @param array $relationships
* @param string $partName
* @param string $docFile
* @param string $xmlFile
*/
private function readPart(PhpWord $phpWord, $relationships, $partName, $docFile, $xmlFile): void
{
$partClass = "PhpOffice\\PhpWord\\Reader\\ODText\\{$partName}";
if (class_exists($partClass)) {
/** @var \PhpOffice\PhpWord\Reader\ODText\AbstractPart $part Type hint */
$part = new $partClass($docFile, $xmlFile);
$part->setRels($relationships);
$part->read($phpWord);
}
}
/**
* Read all relationship files.
*
* @param string $docFile
*
* @return array
*/
private function readRelationships($docFile)
{
$rels = [];
$xmlFile = 'META-INF/manifest.xml';
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($docFile, $xmlFile);
$nodes = $xmlReader->getElements('manifest:file-entry');
foreach ($nodes as $node) {
$type = $xmlReader->getAttribute('manifest:media-type', $node);
$target = $xmlReader->getAttribute('manifest:full-path', $node);
$rels[] = ['type' => $type, 'target' => $target];
}
return $rels;
}
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\ODText;
use PhpOffice\PhpWord\Reader\Word2007\AbstractPart as Word2007AbstractPart;
/**
* Abstract part reader.
*
* @since 0.10.0
*
* @codeCoverageIgnore
*/
abstract class AbstractPart extends Word2007AbstractPart
{
}

View File

@@ -0,0 +1,118 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\ODText;
use DateTime;
use PhpOffice\PhpWord\Element\TrackChange;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Content reader.
*
* @since 0.10.0
*/
class Content extends AbstractPart
{
/**
* Read content.xml.
*/
public function read(PhpWord $phpWord): void
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$trackedChanges = [];
$nodes = $xmlReader->getElements('office:body/office:text/*');
if ($nodes->length > 0) {
$section = $phpWord->addSection();
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);
break;
case 'text:p': // Paragraph
$children = $node->childNodes;
foreach ($children as $child) {
switch ($child->nodeName) {
case 'text:change-start':
$changeId = $child->getAttribute('text:change-id');
if (isset($trackedChanges[$changeId])) {
$changed = $trackedChanges[$changeId];
}
break;
case 'text:change-end':
unset($changed);
break;
case 'text:change':
$changeId = $child->getAttribute('text:change-id');
if (isset($trackedChanges[$changeId])) {
$changed = $trackedChanges[$changeId];
}
break;
}
}
$element = $section->addText($node->nodeValue);
if (isset($changed) && is_array($changed)) {
$element->setTrackChange($changed['changed']);
if (isset($changed['textNodes'])) {
foreach ($changed['textNodes'] as $changedNode) {
$element = $section->addText($changedNode->nodeValue);
$element->setTrackChange($changed['changed']);
}
}
}
break;
case 'text:list': // List
$listItems = $xmlReader->getElements('text:list-item/text:p', $node);
foreach ($listItems as $listItem) {
// $listStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
$section->addListItem($listItem->nodeValue, 0);
}
break;
case 'text:tracked-changes':
$changedRegions = $xmlReader->getElements('text:changed-region', $node);
foreach ($changedRegions as $changedRegion) {
$type = ($changedRegion->firstChild->nodeName == 'text:insertion') ? TrackChange::INSERTED : TrackChange::DELETED;
$creatorNode = $xmlReader->getElements('office:change-info/dc:creator', $changedRegion->firstChild);
$author = $creatorNode[0]->nodeValue;
$dateNode = $xmlReader->getElements('office:change-info/dc:date', $changedRegion->firstChild);
$date = $dateNode[0]->nodeValue;
$date = preg_replace('/\.\d+$/', '', $date);
$date = DateTime::createFromFormat('Y-m-d\TH:i:s', $date);
$changed = new TrackChange($type, $author, $date);
$textNodes = $xmlReader->getElements('text:deletion/text:p', $changedRegion);
$trackedChanges[$changedRegion->getAttribute('text:id')] = ['changed' => $changed, 'textNodes' => $textNodes];
}
break;
}
}
}
}
}

View File

@@ -0,0 +1,77 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\ODText;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Meta reader.
*
* @since 0.11.0
*/
class Meta extends AbstractPart
{
/**
* Read meta.xml.
*
* @todo Process property type
*/
public function read(PhpWord $phpWord): void
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$docProps = $phpWord->getDocInfo();
$metaNode = $xmlReader->getElement('office:meta');
// Standard properties
$properties = [
'title' => 'dc:title',
'subject' => 'dc:subject',
'description' => 'dc:description',
'keywords' => 'meta:keyword',
'creator' => 'meta:initial-creator',
'lastModifiedBy' => 'dc:creator',
// 'created' => 'meta:creation-date',
// 'modified' => 'dc:date',
];
foreach ($properties as $property => $path) {
$method = "set{$property}";
$propertyNode = $xmlReader->getElement($path, $metaNode);
if ($propertyNode !== null && method_exists($docProps, $method)) {
$docProps->$method($propertyNode->nodeValue);
}
}
// Custom properties
$propertyNodes = $xmlReader->getElements('meta:user-defined', $metaNode);
foreach ($propertyNodes as $propertyNode) {
$property = $xmlReader->getAttribute('meta:name', $propertyNode);
// Set category, company, and manager property
if (in_array($property, ['Category', 'Company', 'Manager'])) {
$method = "set{$property}";
$docProps->$method($propertyNode->nodeValue);
} else {
// Set other custom properties
$docProps->setCustomProperty($property, $propertyNode->nodeValue);
}
}
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader;
use Exception;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Reader\RTF\Document;
/**
* RTF Reader class.
*
* @since 0.11.0
*/
class RTF extends AbstractReader implements ReaderInterface
{
/**
* Loads PhpWord from file.
*
* @param string $docFile
*
* @return \PhpOffice\PhpWord\PhpWord
*/
public function load($docFile)
{
$phpWord = new PhpWord();
if ($this->canRead($docFile)) {
$doc = new Document();
$doc->rtf = file_get_contents($docFile);
$doc->read($phpWord);
} else {
throw new Exception("Cannot read {$docFile}.");
}
return $phpWord;
}
}

View File

@@ -0,0 +1,394 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\RTF;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\SimpleType\Jc;
/**
* RTF document reader.
*
* References:
* - How to Write an RTF Reader http://latex2rtf.sourceforge.net/rtfspec_45.html
* - PHP rtfclass by Markus Fischer https://github.com/mfn/rtfclass
* - JavaScript RTF-parser by LazyGyu https://github.com/lazygyu/RTF-parser
*
* @since 0.11.0
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
class Document
{
/** @const int */
const PARA = 'readParagraph';
const STYL = 'readStyle';
const SKIP = 'readSkip';
/**
* PhpWord object.
*
* @var \PhpOffice\PhpWord\PhpWord
*/
private $phpWord;
/**
* Section object.
*
* @var \PhpOffice\PhpWord\Element\Section
*/
private $section;
/**
* Textrun object.
*
* @var \PhpOffice\PhpWord\Element\TextRun
*/
private $textrun;
/**
* RTF content.
*
* @var string
*/
public $rtf;
/**
* Content length.
*
* @var int
*/
private $length = 0;
/**
* Character index.
*
* @var int
*/
private $offset = 0;
/**
* Current control word.
*
* @var string
*/
private $control = '';
/**
* Text content.
*
* @var string
*/
private $text = '';
/**
* Parsing a control word flag.
*
* @var bool
*/
private $isControl = false;
/**
* First character flag: watch out for control symbols.
*
* @var bool
*/
private $isFirst = false;
/**
* Group groups.
*
* @var array
*/
private $groups = [];
/**
* Parser flags; not used.
*
* @var array
*/
private $flags = [];
/**
* Parse RTF content.
*
* - Marks controlling characters `{`, `}`, and `\`
* - Removes line endings
* - Builds control words and control symbols
* - Pushes every other character into the text queue
*
* @todo Use `fread` stream for scalability
*/
public function read(PhpWord $phpWord): void
{
$markers = [
123 => 'markOpening', // {
125 => 'markClosing', // }
92 => 'markBackslash', // \
10 => 'markNewline', // LF
13 => 'markNewline', // CR
];
$this->phpWord = $phpWord;
$this->section = $phpWord->addSection();
$this->textrun = $this->section->addTextRun();
$this->length = strlen($this->rtf);
$this->flags['paragraph'] = true; // Set paragraph flag from the beginning
// Walk each characters
while ($this->offset < $this->length) {
$char = $this->rtf[$this->offset];
$ascii = ord($char);
if (isset($markers[$ascii])) { // Marker found: {, }, \, LF, or CR
$markerFunction = $markers[$ascii];
$this->$markerFunction();
} else {
if (false === $this->isControl) { // Non control word: Push character
$this->pushText($char);
} else {
if (preg_match('/^[a-zA-Z0-9-]?$/', $char)) { // No delimiter: Buffer control
$this->control .= $char;
$this->isFirst = false;
} else { // Delimiter found: Parse buffered control
if ($this->isFirst) {
$this->isFirst = false;
} else {
if (' ' == $char) { // Discard space as a control word delimiter
$this->flushControl(true);
}
}
}
}
}
++$this->offset;
}
$this->flushText();
}
/**
* Mark opening braket `{` character.
*/
private function markOpening(): void
{
$this->flush(true);
array_push($this->groups, $this->flags);
}
/**
* Mark closing braket `}` character.
*/
private function markClosing(): void
{
$this->flush(true);
$this->flags = array_pop($this->groups);
}
/**
* Mark backslash `\` character.
*/
private function markBackslash(): void
{
if ($this->isFirst) {
$this->setControl(false);
$this->text .= '\\';
} else {
$this->flush();
$this->setControl(true);
$this->control = '';
}
}
/**
* Mark newline character: Flush control word because it's not possible to span multiline.
*/
private function markNewline(): void
{
if ($this->isControl) {
$this->flushControl(true);
}
}
/**
* Flush control word or text.
*
* @param bool $isControl
*/
private function flush($isControl = false): void
{
if ($this->isControl) {
$this->flushControl($isControl);
} else {
$this->flushText();
}
}
/**
* Flush control word.
*
* @param bool $isControl
*/
private function flushControl($isControl = false): void
{
if (1 === preg_match('/^([A-Za-z]+)(-?[0-9]*) ?$/', $this->control, $match)) {
[, $control, $parameter] = $match;
$this->parseControl($control, $parameter);
}
if (true === $isControl) {
$this->setControl(false);
}
}
/**
* Flush text in queue.
*/
private function flushText(): void
{
if ($this->text != '') {
if (isset($this->flags['property'])) { // Set property
$this->flags['value'] = $this->text;
} else { // Set text
if (true === $this->flags['paragraph']) {
$this->flags['paragraph'] = false;
$this->flags['text'] = $this->text;
}
}
// Add text if it's not flagged as skipped
if (!isset($this->flags['skipped'])) {
$this->readText();
}
$this->text = '';
}
}
/**
* Reset control word and first char state.
*
* @param bool $value
*/
private function setControl($value): void
{
$this->isControl = $value;
$this->isFirst = $value;
}
/**
* Push text into queue.
*
* @param string $char
*/
private function pushText($char): void
{
if ('<' == $char) {
$this->text .= '&lt;';
} elseif ('>' == $char) {
$this->text .= '&gt;';
} else {
$this->text .= $char;
}
}
/**
* Parse control.
*
* @param string $control
* @param string $parameter
*/
private function parseControl($control, $parameter): void
{
$controls = [
'par' => [self::PARA, 'paragraph', true],
'b' => [self::STYL, 'font', 'bold', true],
'i' => [self::STYL, 'font', 'italic', true],
'u' => [self::STYL, 'font', 'underline', true],
'strike' => [self::STYL, 'font', 'strikethrough', true],
'fs' => [self::STYL, 'font', 'size', $parameter],
'qc' => [self::STYL, 'paragraph', 'alignment', Jc::CENTER],
'sa' => [self::STYL, 'paragraph', 'spaceAfter', $parameter],
'fonttbl' => [self::SKIP, 'fonttbl', null],
'colortbl' => [self::SKIP, 'colortbl', null],
'info' => [self::SKIP, 'info', null],
'generator' => [self::SKIP, 'generator', null],
'title' => [self::SKIP, 'title', null],
'subject' => [self::SKIP, 'subject', null],
'category' => [self::SKIP, 'category', null],
'keywords' => [self::SKIP, 'keywords', null],
'comment' => [self::SKIP, 'comment', null],
'shppict' => [self::SKIP, 'pic', null],
'fldinst' => [self::SKIP, 'link', null],
];
if (isset($controls[$control])) {
[$function] = $controls[$control];
if (method_exists($this, $function)) {
$directives = $controls[$control];
array_shift($directives); // remove the function variable; we won't need it
$this->$function($directives);
}
}
}
/**
* Read paragraph.
*
* @param array $directives
*/
private function readParagraph($directives): void
{
[$property, $value] = $directives;
$this->textrun = $this->section->addTextRun();
$this->flags[$property] = $value;
}
/**
* Read style.
*
* @param array $directives
*/
private function readStyle($directives): void
{
[$style, $property, $value] = $directives;
$this->flags['styles'][$style][$property] = $value;
}
/**
* Read skip.
*
* @param array $directives
*/
private function readSkip($directives): void
{
[$property] = $directives;
$this->flags['property'] = $property;
$this->flags['skipped'] = true;
}
/**
* Read text.
*/
private function readText(): void
{
$text = $this->textrun->addText($this->text);
if (isset($this->flags['styles']['font'])) {
$text->getFontStyle()->setStyleByArray($this->flags['styles']['font']);
}
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader;
/**
* Reader interface.
*
* @since 0.8.0
*/
interface ReaderInterface
{
/**
* Can the current ReaderInterface read the file?
*
* @param string $filename
*
* @return bool
*/
public function canRead($filename);
/**
* Loads PhpWord from file.
*
* @param string $filename
*/
public function load($filename);
}

View File

@@ -0,0 +1,175 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
use PhpOffice\PhpWord\Shared\ZipArchive;
/**
* Reader for Word2007.
*
* @since 0.8.0
*
* @todo watermark, checkbox, toc
* @todo Partly done: image, object
*/
class Word2007 extends AbstractReader implements ReaderInterface
{
/**
* Loads PhpWord from file.
*
* @param string $docFile
*
* @return \PhpOffice\PhpWord\PhpWord
*/
public function load($docFile)
{
$phpWord = new PhpWord();
$relationships = $this->readRelationships($docFile);
$steps = [
['stepPart' => 'document', 'stepItems' => [
'styles' => 'Styles',
'numbering' => 'Numbering',
]],
['stepPart' => 'main', 'stepItems' => [
'officeDocument' => 'Document',
'core-properties' => 'DocPropsCore',
'extended-properties' => 'DocPropsApp',
'custom-properties' => 'DocPropsCustom',
]],
['stepPart' => 'document', 'stepItems' => [
'endnotes' => 'Endnotes',
'footnotes' => 'Footnotes',
'settings' => 'Settings',
]],
];
foreach ($steps as $step) {
$stepPart = $step['stepPart'];
$stepItems = $step['stepItems'];
if (!isset($relationships[$stepPart])) {
continue;
}
foreach ($relationships[$stepPart] as $relItem) {
$relType = $relItem['type'];
if (isset($stepItems[$relType])) {
$partName = $stepItems[$relType];
$xmlFile = $relItem['target'];
$this->readPart($phpWord, $relationships, $partName, $docFile, $xmlFile);
}
}
}
return $phpWord;
}
/**
* Read document part.
*
* @param array $relationships
* @param string $partName
* @param string $docFile
* @param string $xmlFile
*/
private function readPart(PhpWord $phpWord, $relationships, $partName, $docFile, $xmlFile): void
{
$partClass = "PhpOffice\\PhpWord\\Reader\\Word2007\\{$partName}";
if (class_exists($partClass)) {
/** @var \PhpOffice\PhpWord\Reader\Word2007\AbstractPart $part Type hint */
$part = new $partClass($docFile, $xmlFile);
$part->setRels($relationships);
$part->read($phpWord);
}
}
/**
* Read all relationship files.
*
* @param string $docFile
*
* @return array
*/
private function readRelationships($docFile)
{
$relationships = [];
// _rels/.rels
$relationships['main'] = $this->getRels($docFile, '_rels/.rels');
// word/_rels/*.xml.rels
$wordRelsPath = 'word/_rels/';
$zip = new ZipArchive();
if ($zip->open($docFile) === true) {
for ($i = 0; $i < $zip->numFiles; ++$i) {
$xmlFile = $zip->getNameIndex($i);
if ((substr($xmlFile, 0, strlen($wordRelsPath))) == $wordRelsPath && (substr($xmlFile, -1)) != '/') {
$docPart = str_replace('.xml.rels', '', str_replace($wordRelsPath, '', $xmlFile));
$relationships[$docPart] = $this->getRels($docFile, $xmlFile, 'word/');
}
}
$zip->close();
}
return $relationships;
}
/**
* Get relationship array.
*
* @param string $docFile
* @param string $xmlFile
* @param string $targetPrefix
*
* @return array
*/
private function getRels($docFile, $xmlFile, $targetPrefix = '')
{
$metaPrefix = 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/';
$officePrefix = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/';
$rels = [];
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($docFile, $xmlFile);
$nodes = $xmlReader->getElements('*');
foreach ($nodes as $node) {
$rId = $xmlReader->getAttribute('Id', $node);
$type = $xmlReader->getAttribute('Type', $node);
$target = $xmlReader->getAttribute('Target', $node);
$mode = $xmlReader->getAttribute('TargetMode', $node);
// Remove URL prefixes from $type to make it easier to read
$type = str_replace($metaPrefix, '', $type);
$type = str_replace($officePrefix, '', $type);
$docPart = str_replace('.xml', '', $target);
// Do not add prefix to link source
if ($type != 'hyperlink' && $mode != 'External') {
$target = $targetPrefix . $target;
}
// Push to return array
$rels[$rId] = ['type' => $type, 'target' => $target, 'docPart' => $docPart, 'targetMode' => $mode];
}
ksort($rels);
return $rels;
}
}

View File

@@ -0,0 +1,735 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
use DateTime;
use DOMElement;
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
use PhpOffice\PhpWord\Element\AbstractContainer;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\TrackChange;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Abstract part reader.
*
* This class is inherited by ODText reader
*
* @since 0.10.0
*/
abstract class AbstractPart
{
/**
* Conversion method.
*
* @const int
*/
const READ_VALUE = 'attributeValue'; // Read attribute value
const READ_EQUAL = 'attributeEquals'; // Read `true` when attribute value equals specified value
const READ_TRUE = 'attributeTrue'; // Read `true` when element exists
const READ_FALSE = 'attributeFalse'; // Read `false` when element exists
const READ_SIZE = 'attributeMultiplyByTwo'; // Read special attribute value for Font::$size
/**
* Document file.
*
* @var string
*/
protected $docFile;
/**
* XML file.
*
* @var string
*/
protected $xmlFile;
/**
* Part relationships.
*
* @var array
*/
protected $rels = [];
/**
* Read part.
*/
abstract public function read(PhpWord $phpWord);
/**
* Create new instance.
*
* @param string $docFile
* @param string $xmlFile
*/
public function __construct($docFile, $xmlFile)
{
$this->docFile = $docFile;
$this->xmlFile = $xmlFile;
}
/**
* Set relationships.
*
* @param array $value
*/
public function setRels($value): void
{
$this->rels = $value;
}
/**
* Read w:p.
*
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
* @param string $docPart
*
* @todo Get font style for preserve text
*/
protected function readParagraph(XMLReader $xmlReader, DOMElement $domNode, $parent, $docPart = 'document'): void
{
// Paragraph style
$paragraphStyle = null;
$headingDepth = null;
if ($xmlReader->elementExists('w:pPr', $domNode)) {
$paragraphStyle = $this->readParagraphStyle($xmlReader, $domNode);
$headingDepth = $this->getHeadingDepth($paragraphStyle);
}
// PreserveText
if ($xmlReader->elementExists('w:r/w:instrText', $domNode)) {
$ignoreText = false;
$textContent = '';
$fontStyle = $this->readFontStyle($xmlReader, $domNode);
$nodes = $xmlReader->getElements('w:r', $domNode);
foreach ($nodes as $node) {
$instrText = $xmlReader->getValue('w:instrText', $node);
if ($xmlReader->elementExists('w:fldChar', $node)) {
$fldCharType = $xmlReader->getAttribute('w:fldCharType', $node, 'w:fldChar');
if ('begin' == $fldCharType) {
$ignoreText = true;
} elseif ('end' == $fldCharType) {
$ignoreText = false;
}
}
if (null !== $instrText) {
$textContent .= '{' . $instrText . '}';
} else {
if (false === $ignoreText) {
$textContent .= $xmlReader->getValue('w:t', $node);
}
}
}
$parent->addPreserveText(htmlspecialchars($textContent, ENT_QUOTES, 'UTF-8'), $fontStyle, $paragraphStyle);
} elseif ($xmlReader->elementExists('w:pPr/w:numPr', $domNode)) {
// List item
$numId = $xmlReader->getAttribute('w:val', $domNode, 'w:pPr/w:numPr/w:numId');
$levelId = $xmlReader->getAttribute('w:val', $domNode, 'w:pPr/w:numPr/w:ilvl');
$nodes = $xmlReader->getElements('*', $domNode);
$listItemRun = $parent->addListItemRun($levelId, "PHPWordList{$numId}", $paragraphStyle);
foreach ($nodes as $node) {
$this->readRun($xmlReader, $node, $listItemRun, $docPart, $paragraphStyle);
}
} elseif ($headingDepth !== null) {
// Heading or Title
$textContent = null;
$nodes = $xmlReader->getElements('w:r', $domNode);
if ($nodes->length === 1) {
$textContent = htmlspecialchars($xmlReader->getValue('w:t', $nodes->item(0)), ENT_QUOTES, 'UTF-8');
} else {
$textContent = new TextRun($paragraphStyle);
foreach ($nodes as $node) {
$this->readRun($xmlReader, $node, $textContent, $docPart, $paragraphStyle);
}
}
$parent->addTitle($textContent, $headingDepth);
} else {
// Text and TextRun
$textRunContainers = $xmlReader->countElements('w:r|w:ins|w:del|w:hyperlink|w:smartTag', $domNode);
if (0 === $textRunContainers) {
$parent->addTextBreak(null, $paragraphStyle);
} else {
$nodes = $xmlReader->getElements('*', $domNode);
$paragraph = $parent->addTextRun($paragraphStyle);
foreach ($nodes as $node) {
$this->readRun($xmlReader, $node, $paragraph, $docPart, $paragraphStyle);
}
}
}
}
/**
* Returns the depth of the Heading, returns 0 for a Title.
*
* @param array $paragraphStyle
*
* @return null|number
*/
private function getHeadingDepth(?array $paragraphStyle = null)
{
if (is_array($paragraphStyle) && isset($paragraphStyle['styleName'])) {
if ('Title' === $paragraphStyle['styleName']) {
return 0;
}
$headingMatches = [];
preg_match('/Heading(\d)/', $paragraphStyle['styleName'], $headingMatches);
if (!empty($headingMatches)) {
return $headingMatches[1];
}
}
return null;
}
/**
* Read w:r.
*
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
* @param string $docPart
* @param mixed $paragraphStyle
*
* @todo Footnote paragraph style
*/
protected function readRun(XMLReader $xmlReader, DOMElement $domNode, $parent, $docPart, $paragraphStyle = null): void
{
if (in_array($domNode->nodeName, ['w:ins', 'w:del', 'w:smartTag', 'w:hyperlink'])) {
$nodes = $xmlReader->getElements('*', $domNode);
foreach ($nodes as $node) {
$this->readRun($xmlReader, $node, $parent, $docPart, $paragraphStyle);
}
} elseif ($domNode->nodeName == 'w:r') {
$fontStyle = $this->readFontStyle($xmlReader, $domNode);
$nodes = $xmlReader->getElements('*', $domNode);
foreach ($nodes as $node) {
$this->readRunChild($xmlReader, $node, $parent, $docPart, $paragraphStyle, $fontStyle);
}
}
}
/**
* Parses nodes under w:r.
*
* @param string $docPart
* @param mixed $paragraphStyle
* @param mixed $fontStyle
*/
protected function readRunChild(XMLReader $xmlReader, DOMElement $node, AbstractContainer $parent, $docPart, $paragraphStyle = null, $fontStyle = null): void
{
$runParent = $node->parentNode->parentNode;
if ($node->nodeName == 'w:footnoteReference') {
// Footnote
$wId = $xmlReader->getAttribute('w:id', $node);
$footnote = $parent->addFootnote();
$footnote->setRelationId($wId);
} elseif ($node->nodeName == 'w:endnoteReference') {
// Endnote
$wId = $xmlReader->getAttribute('w:id', $node);
$endnote = $parent->addEndnote();
$endnote->setRelationId($wId);
} elseif ($node->nodeName == 'w:pict') {
// Image
$rId = $xmlReader->getAttribute('r:id', $node, 'v:shape/v:imagedata');
$target = $this->getMediaTarget($docPart, $rId);
if (null !== $target) {
if ('External' == $this->getTargetMode($docPart, $rId)) {
$imageSource = $target;
} else {
$imageSource = "zip://{$this->docFile}#{$target}";
}
$parent->addImage($imageSource);
}
} elseif ($node->nodeName == 'w:drawing') {
// Office 2011 Image
$xmlReader->registerNamespace('wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing');
$xmlReader->registerNamespace('r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
$xmlReader->registerNamespace('pic', 'http://schemas.openxmlformats.org/drawingml/2006/picture');
$xmlReader->registerNamespace('a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
$name = $xmlReader->getAttribute('name', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:nvPicPr/pic:cNvPr');
$embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip');
if ($name === null && $embedId === null) { // some Converters puts images on a different path
$name = $xmlReader->getAttribute('name', $node, 'wp:anchor/a:graphic/a:graphicData/pic:pic/pic:nvPicPr/pic:cNvPr');
$embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:anchor/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip');
}
$target = $this->getMediaTarget($docPart, $embedId);
if (null !== $target) {
$imageSource = "zip://{$this->docFile}#{$target}";
$parent->addImage($imageSource, null, false, $name);
}
} elseif ($node->nodeName == 'w:object') {
// Object
$rId = $xmlReader->getAttribute('r:id', $node, 'o:OLEObject');
// $rIdIcon = $xmlReader->getAttribute('r:id', $domNode, 'w:object/v:shape/v:imagedata');
$target = $this->getMediaTarget($docPart, $rId);
if (null !== $target) {
$textContent = "&lt;Object: {$target}>";
$parent->addText($textContent, $fontStyle, $paragraphStyle);
}
} elseif ($node->nodeName == 'w:br') {
$parent->addTextBreak();
} elseif ($node->nodeName == 'w:tab') {
$parent->addText("\t");
} elseif ($node->nodeName == 'mc:AlternateContent') {
if ($node->hasChildNodes()) {
// Get fallback instead of mc:Choice to make sure it is compatible
$fallbackElements = $node->getElementsByTagName('Fallback');
if ($fallbackElements->length) {
$fallback = $fallbackElements->item(0);
// TextRun
$textContent = htmlspecialchars($fallback->nodeValue, ENT_QUOTES, 'UTF-8');
$parent->addText($textContent, $fontStyle, $paragraphStyle);
}
}
} elseif ($node->nodeName == 'w:t' || $node->nodeName == 'w:delText') {
// TextRun
$textContent = htmlspecialchars($xmlReader->getValue('.', $node), ENT_QUOTES, 'UTF-8');
if ($runParent->nodeName == 'w:hyperlink') {
$rId = $xmlReader->getAttribute('r:id', $runParent);
$target = $this->getMediaTarget($docPart, $rId);
if (null !== $target) {
$parent->addLink($target, $textContent, $fontStyle, $paragraphStyle);
} else {
$parent->addText($textContent, $fontStyle, $paragraphStyle);
}
} else {
/** @var AbstractElement $element */
$element = $parent->addText($textContent, $fontStyle, $paragraphStyle);
if (in_array($runParent->nodeName, ['w:ins', 'w:del'])) {
$type = ($runParent->nodeName == 'w:del') ? TrackChange::DELETED : TrackChange::INSERTED;
$author = $runParent->getAttribute('w:author');
$date = DateTime::createFromFormat('Y-m-d\TH:i:s\Z', $runParent->getAttribute('w:date'));
$element->setChangeInfo($type, $author, $date);
}
}
}
}
/**
* Read w:tbl.
*
* @param mixed $parent
* @param string $docPart
*/
protected function readTable(XMLReader $xmlReader, DOMElement $domNode, $parent, $docPart = 'document'): void
{
// Table style
$tblStyle = null;
if ($xmlReader->elementExists('w:tblPr', $domNode)) {
$tblStyle = $this->readTableStyle($xmlReader, $domNode);
}
/** @var \PhpOffice\PhpWord\Element\Table $table Type hint */
$table = $parent->addTable($tblStyle);
$tblNodes = $xmlReader->getElements('*', $domNode);
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');
$rowHRule = $rowHRule == 'exact';
$rowStyle = [
'tblHeader' => $xmlReader->elementExists('w:trPr/w:tblHeader', $tblNode),
'cantSplit' => $xmlReader->elementExists('w:trPr/w:cantSplit', $tblNode),
'exactHeight' => $rowHRule,
];
$row = $table->addRow($rowHeight, $rowStyle);
$rowNodes = $xmlReader->getElements('*', $tblNode);
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;
$cellStyleNode = $xmlReader->getElement('w:tcPr', $rowNode);
if (null !== $cellStyleNode) {
$cellStyle = $this->readCellStyle($xmlReader, $cellStyleNode);
}
$cell = $row->addCell($cellWidth, $cellStyle);
$cellNodes = $xmlReader->getElements('*', $rowNode);
foreach ($cellNodes as $cellNode) {
if ('w:p' == $cellNode->nodeName) { // Paragraph
$this->readParagraph($xmlReader, $cellNode, $cell, $docPart);
}
}
}
}
}
}
}
/**
* Read w:pPr.
*
* @return null|array
*/
protected function readParagraphStyle(XMLReader $xmlReader, DOMElement $domNode)
{
if (!$xmlReader->elementExists('w:pPr', $domNode)) {
return null;
}
$styleNode = $xmlReader->getElement('w:pPr', $domNode);
$styleDefs = [
'styleName' => [self::READ_VALUE, ['w:pStyle', 'w:name']],
'alignment' => [self::READ_VALUE, 'w:jc'],
'basedOn' => [self::READ_VALUE, 'w:basedOn'],
'next' => [self::READ_VALUE, 'w:next'],
'indent' => [self::READ_VALUE, 'w:ind', 'w:left'],
'hanging' => [self::READ_VALUE, 'w:ind', 'w:hanging'],
'spaceAfter' => [self::READ_VALUE, 'w:spacing', 'w:after'],
'spaceBefore' => [self::READ_VALUE, 'w:spacing', 'w:before'],
'widowControl' => [self::READ_FALSE, 'w:widowControl'],
'keepNext' => [self::READ_TRUE, 'w:keepNext'],
'keepLines' => [self::READ_TRUE, 'w:keepLines'],
'pageBreakBefore' => [self::READ_TRUE, 'w:pageBreakBefore'],
'contextualSpacing' => [self::READ_TRUE, 'w:contextualSpacing'],
'bidi' => [self::READ_TRUE, 'w:bidi'],
'suppressAutoHyphens' => [self::READ_TRUE, 'w:suppressAutoHyphens'],
];
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
}
/**
* Read w:rPr.
*
* @return null|array
*/
protected function readFontStyle(XMLReader $xmlReader, DOMElement $domNode)
{
if (null === $domNode) {
return null;
}
// Hyperlink has an extra w:r child
if ('w:hyperlink' == $domNode->nodeName) {
$domNode = $xmlReader->getElement('w:r', $domNode);
}
if (!$xmlReader->elementExists('w:rPr', $domNode)) {
return null;
}
$styleNode = $xmlReader->getElement('w:rPr', $domNode);
$styleDefs = [
'styleName' => [self::READ_VALUE, 'w:rStyle'],
'name' => [self::READ_VALUE, 'w:rFonts', ['w:ascii', 'w:hAnsi', 'w:eastAsia', 'w:cs']],
'hint' => [self::READ_VALUE, 'w:rFonts', 'w:hint'],
'size' => [self::READ_SIZE, ['w:sz', 'w:szCs']],
'color' => [self::READ_VALUE, 'w:color'],
'underline' => [self::READ_VALUE, 'w:u'],
'bold' => [self::READ_TRUE, 'w:b'],
'italic' => [self::READ_TRUE, 'w:i'],
'strikethrough' => [self::READ_TRUE, 'w:strike'],
'doubleStrikethrough' => [self::READ_TRUE, 'w:dstrike'],
'smallCaps' => [self::READ_TRUE, 'w:smallCaps'],
'allCaps' => [self::READ_TRUE, 'w:caps'],
'superScript' => [self::READ_EQUAL, 'w:vertAlign', 'w:val', 'superscript'],
'subScript' => [self::READ_EQUAL, 'w:vertAlign', 'w:val', 'subscript'],
'fgColor' => [self::READ_VALUE, 'w:highlight'],
'rtl' => [self::READ_TRUE, 'w:rtl'],
'lang' => [self::READ_VALUE, 'w:lang'],
'position' => [self::READ_VALUE, 'w:position'],
'hidden' => [self::READ_TRUE, 'w:vanish'],
];
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
}
/**
* Read w:tblPr.
*
* @return null|array|string
*
* @todo Capture w:tblStylePr w:type="firstRow"
*/
protected function readTableStyle(XMLReader $xmlReader, DOMElement $domNode)
{
$style = null;
$margins = ['top', 'left', 'bottom', 'right'];
$borders = array_merge($margins, ['insideH', 'insideV']);
if ($xmlReader->elementExists('w:tblPr', $domNode)) {
if ($xmlReader->elementExists('w:tblPr/w:tblStyle', $domNode)) {
$style = $xmlReader->getAttribute('w:val', $domNode, 'w:tblPr/w:tblStyle');
} else {
$styleNode = $xmlReader->getElement('w:tblPr', $domNode);
$styleDefs = [];
foreach ($margins as $side) {
$ucfSide = ucfirst($side);
$styleDefs["cellMargin$ucfSide"] = [self::READ_VALUE, "w:tblCellMar/w:$side", 'w:w'];
}
foreach ($borders as $side) {
$ucfSide = ucfirst($side);
$styleDefs["border{$ucfSide}Size"] = [self::READ_VALUE, "w:tblBorders/w:$side", 'w:sz'];
$styleDefs["border{$ucfSide}Color"] = [self::READ_VALUE, "w:tblBorders/w:$side", 'w:color'];
$styleDefs["border{$ucfSide}Style"] = [self::READ_VALUE, "w:tblBorders/w:$side", 'w:val'];
}
$styleDefs['layout'] = [self::READ_VALUE, 'w:tblLayout', 'w:type'];
$styleDefs['bidiVisual'] = [self::READ_TRUE, 'w:bidiVisual'];
$styleDefs['cellSpacing'] = [self::READ_VALUE, 'w:tblCellSpacing', 'w:w'];
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
$tablePositionNode = $xmlReader->getElement('w:tblpPr', $styleNode);
if ($tablePositionNode !== null) {
$style['position'] = $this->readTablePosition($xmlReader, $tablePositionNode);
}
$indentNode = $xmlReader->getElement('w:tblInd', $styleNode);
if ($indentNode !== null) {
$style['indent'] = $this->readTableIndent($xmlReader, $indentNode);
}
}
}
return $style;
}
/**
* Read w:tblpPr.
*
* @return array
*/
private function readTablePosition(XMLReader $xmlReader, DOMElement $domNode)
{
$styleDefs = [
'leftFromText' => [self::READ_VALUE, '.', 'w:leftFromText'],
'rightFromText' => [self::READ_VALUE, '.', 'w:rightFromText'],
'topFromText' => [self::READ_VALUE, '.', 'w:topFromText'],
'bottomFromText' => [self::READ_VALUE, '.', 'w:bottomFromText'],
'vertAnchor' => [self::READ_VALUE, '.', 'w:vertAnchor'],
'horzAnchor' => [self::READ_VALUE, '.', 'w:horzAnchor'],
'tblpXSpec' => [self::READ_VALUE, '.', 'w:tblpXSpec'],
'tblpX' => [self::READ_VALUE, '.', 'w:tblpX'],
'tblpYSpec' => [self::READ_VALUE, '.', 'w:tblpYSpec'],
'tblpY' => [self::READ_VALUE, '.', 'w:tblpY'],
];
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
}
/**
* Read w:tblInd.
*
* @return TblWidthComplexType
*/
private function readTableIndent(XMLReader $xmlReader, DOMElement $domNode)
{
$styleDefs = [
'value' => [self::READ_VALUE, '.', 'w:w'],
'type' => [self::READ_VALUE, '.', 'w:type'],
];
$styleDefs = $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
return new TblWidthComplexType((int) $styleDefs['value'], $styleDefs['type']);
}
/**
* Read w:tcPr.
*
* @return array
*/
private function readCellStyle(XMLReader $xmlReader, DOMElement $domNode)
{
$styleDefs = [
'valign' => [self::READ_VALUE, 'w:vAlign'],
'textDirection' => [self::READ_VALUE, 'w:textDirection'],
'gridSpan' => [self::READ_VALUE, 'w:gridSpan'],
'vMerge' => [self::READ_VALUE, 'w:vMerge'],
'bgColor' => [self::READ_VALUE, 'w:shd', 'w:fill'],
];
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
}
/**
* Returns the first child element found.
*
* @param null|array|string $elements
*
* @return null|string
*/
private function findPossibleElement(XMLReader $xmlReader, ?DOMElement $parentNode = null, $elements = null)
{
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 array|string $attributes
*
* @return null|string
*/
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;
}
}
return null;
}
return $attributes;
}
/**
* Read style definition.
*
* @param DOMElement $parentNode
* @param array $styleDefs
*
* @ignoreScrutinizerPatch
*
* @return array
*/
protected function readStyleDefs(XMLReader $xmlReader, ?DOMElement $parentNode = null, $styleDefs = [])
{
$styles = [];
foreach ($styleDefs as $styleProp => $styleVal) {
[$method, $element, $attribute, $expected] = array_pad($styleVal, 4, null);
$element = $this->findPossibleElement($xmlReader, $parentNode, $element);
if ($element === null) {
continue;
}
if ($xmlReader->elementExists($element, $parentNode)) {
$node = $xmlReader->getElement($element, $parentNode);
$attribute = $this->findPossibleAttribute($xmlReader, $node, $attribute);
// Use w:val as default if no attribute assigned
$attribute = ($attribute === null) ? 'w:val' : $attribute;
$attributeValue = $xmlReader->getAttribute($attribute, $node);
$styleValue = $this->readStyleDef($method, $attributeValue, $expected);
if ($styleValue !== null) {
$styles[$styleProp] = $styleValue;
}
}
}
return $styles;
}
/**
* Return style definition based on conversion method.
*
* @param string $method
*
* @ignoreScrutinizerPatch
*
* @param null|string $attributeValue
* @param mixed $expected
*
* @return mixed
*/
private function readStyleDef($method, $attributeValue, $expected)
{
$style = $attributeValue;
if (self::READ_SIZE == $method) {
$style = $attributeValue / 2;
} elseif (self::READ_TRUE == $method) {
$style = $this->isOn($attributeValue);
} elseif (self::READ_FALSE == $method) {
$style = !$this->isOn($attributeValue);
} elseif (self::READ_EQUAL == $method) {
$style = $attributeValue == $expected;
}
return $style;
}
/**
* Parses the value of the on/off value, null is considered true as it means the w:val attribute was not present.
*
* @see http://www.datypic.com/sc/ooxml/t-w_ST_OnOff.html
*
* @param string $value
*
* @return bool
*/
private function isOn($value = null)
{
return $value === null || $value === '1' || $value === 'true' || $value === 'on';
}
/**
* Returns the target of image, object, or link as stored in ::readMainRels.
*
* @param string $docPart
* @param string $rId
*
* @return null|string
*/
private function getMediaTarget($docPart, $rId)
{
$target = null;
if (isset($this->rels[$docPart], $this->rels[$docPart][$rId])) {
$target = $this->rels[$docPart][$rId]['target'];
}
return $target;
}
/**
* Returns the target mode.
*
* @param string $docPart
* @param string $rId
*
* @return null|string
*/
private function getTargetMode($docPart, $rId)
{
$mode = null;
if (isset($this->rels[$docPart], $this->rels[$docPart][$rId])) {
$mode = $this->rels[$docPart][$rId]['targetMode'];
}
return $mode;
}
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
/**
* Extended properties reader.
*
* @since 0.10.0
*/
class DocPropsApp extends DocPropsCore
{
/**
* Property mapping.
*
* @var array
*/
protected $mapping = ['Company' => 'setCompany', 'Manager' => 'setManager'];
/**
* Callback functions.
*
* @var array
*/
protected $callbacks = [];
}

View File

@@ -0,0 +1,81 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Core properties reader.
*
* @since 0.10.0
*/
class DocPropsCore extends AbstractPart
{
/**
* Property mapping.
*
* @var array
*/
protected $mapping = [
'dc:creator' => 'setCreator',
'dc:title' => 'setTitle',
'dc:description' => 'setDescription',
'dc:subject' => 'setSubject',
'cp:keywords' => 'setKeywords',
'cp:category' => 'setCategory',
'cp:lastModifiedBy' => 'setLastModifiedBy',
'dcterms:created' => 'setCreated',
'dcterms:modified' => 'setModified',
];
/**
* Callback functions.
*
* @var array
*/
protected $callbacks = ['dcterms:created' => 'strtotime', 'dcterms:modified' => 'strtotime'];
/**
* Read core/extended document properties.
*/
public function read(PhpWord $phpWord): void
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$docProps = $phpWord->getDocInfo();
$nodes = $xmlReader->getElements('*');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
if (!isset($this->mapping[$node->nodeName])) {
continue;
}
$method = $this->mapping[$node->nodeName];
$value = $node->nodeValue == '' ? null : $node->nodeValue;
if (isset($this->callbacks[$node->nodeName])) {
$value = $this->callbacks[$node->nodeName]($value);
}
if (method_exists($docProps, $method)) {
$docProps->$method($value);
}
}
}
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\PhpWord\Metadata\DocInfo;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Custom properties reader.
*
* @since 0.11.0
*/
class DocPropsCustom extends AbstractPart
{
/**
* Read custom document properties.
*/
public function read(PhpWord $phpWord): void
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$docProps = $phpWord->getDocInfo();
$nodes = $xmlReader->getElements('*');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$propertyName = $xmlReader->getAttribute('name', $node);
$attributeNode = $xmlReader->getElement('*', $node);
$attributeType = $attributeNode->nodeName;
$attributeValue = $attributeNode->nodeValue;
$attributeValue = DocInfo::convertProperty($attributeValue, $attributeType);
$attributeType = DocInfo::convertPropertyType($attributeType);
$docProps->setCustomProperty($propertyName, $attributeValue, $attributeType);
}
}
}
}

View File

@@ -0,0 +1,173 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
use DOMElement;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Document reader.
*
* @since 0.10.0
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) For readWPNode
*/
class Document extends AbstractPart
{
/**
* PhpWord object.
*
* @var \PhpOffice\PhpWord\PhpWord
*/
private $phpWord;
/**
* Read document.xml.
*/
public function read(PhpWord $phpWord): void
{
$this->phpWord = $phpWord;
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$readMethods = ['w:p' => 'readWPNode', 'w:tbl' => 'readTable', 'w:sectPr' => 'readWSectPrNode'];
$nodes = $xmlReader->getElements('w:body/*');
if ($nodes->length > 0) {
$section = $this->phpWord->addSection();
foreach ($nodes as $node) {
if (isset($readMethods[$node->nodeName])) {
$readMethod = $readMethods[$node->nodeName];
$this->$readMethod($xmlReader, $node, $section);
}
}
}
}
/**
* Read header footer.
*
* @param array $settings
*/
private function readHeaderFooter($settings, Section &$section): void
{
$readMethods = ['w:p' => 'readParagraph', 'w:tbl' => 'readTable'];
if (is_array($settings) && isset($settings['hf'])) {
foreach ($settings['hf'] as $rId => $hfSetting) {
if (isset($this->rels['document'][$rId])) {
[$hfType, $xmlFile, $docPart] = array_values($this->rels['document'][$rId]);
$addMethod = "add{$hfType}";
$hfObject = $section->$addMethod($hfSetting['type']);
// Read header/footer content
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $xmlFile);
$nodes = $xmlReader->getElements('*');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
if (isset($readMethods[$node->nodeName])) {
$readMethod = $readMethods[$node->nodeName];
$this->$readMethod($xmlReader, $node, $hfObject, $docPart);
}
}
}
}
}
}
}
/**
* Read w:sectPr.
*
* @ignoreScrutinizerPatch
*
* @return array
*/
private function readSectionStyle(XMLReader $xmlReader, DOMElement $domNode)
{
$styleDefs = [
'breakType' => [self::READ_VALUE, 'w:type'],
'vAlign' => [self::READ_VALUE, 'w:vAlign'],
'pageSizeW' => [self::READ_VALUE, 'w:pgSz', 'w:w'],
'pageSizeH' => [self::READ_VALUE, 'w:pgSz', 'w:h'],
'orientation' => [self::READ_VALUE, 'w:pgSz', 'w:orient'],
'colsNum' => [self::READ_VALUE, 'w:cols', 'w:num'],
'colsSpace' => [self::READ_VALUE, 'w:cols', 'w:space'],
'marginTop' => [self::READ_VALUE, 'w:pgMar', 'w:top'],
'marginLeft' => [self::READ_VALUE, 'w:pgMar', 'w:left'],
'marginBottom' => [self::READ_VALUE, 'w:pgMar', 'w:bottom'],
'marginRight' => [self::READ_VALUE, 'w:pgMar', 'w:right'],
'headerHeight' => [self::READ_VALUE, 'w:pgMar', 'w:header'],
'footerHeight' => [self::READ_VALUE, 'w:pgMar', 'w:footer'],
'gutter' => [self::READ_VALUE, 'w:pgMar', 'w:gutter'],
];
$styles = $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
// Header and footer
// @todo Cleanup this part
$nodes = $xmlReader->getElements('*', $domNode);
foreach ($nodes as $node) {
if ($node->nodeName == 'w:headerReference' || $node->nodeName == 'w:footerReference') {
$id = $xmlReader->getAttribute('r:id', $node);
$styles['hf'][$id] = [
'method' => str_replace('w:', '', str_replace('Reference', '', $node->nodeName)),
'type' => $xmlReader->getAttribute('w:type', $node),
];
}
}
return $styles;
}
/**
* Read w:p node.
*
* @todo <w:lastRenderedPageBreak>
*/
private function readWPNode(XMLReader $xmlReader, DOMElement $node, Section &$section): void
{
// Page break
if ($xmlReader->getAttribute('w:type', $node, 'w:r/w:br') == 'page') {
$section->addPageBreak(); // PageBreak
}
// Paragraph
$this->readParagraph($xmlReader, $node, $section);
// Section properties
if ($xmlReader->elementExists('w:pPr/w:sectPr', $node)) {
$sectPrNode = $xmlReader->getElement('w:pPr/w:sectPr', $node);
if ($sectPrNode !== null) {
$this->readWSectPrNode($xmlReader, $sectPrNode, $section);
}
$section = $this->phpWord->addSection();
}
}
/**
* Read w:sectPr node.
*/
private function readWSectPrNode(XMLReader $xmlReader, DOMElement $node, Section &$section): void
{
$style = $this->readSectionStyle($xmlReader, $node);
$section->setStyle($style);
$this->readHeaderFooter($style, $section);
}
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
/**
* Endnotes reader.
*
* @since 0.10.0
*/
class Endnotes extends Footnotes
{
/**
* Collection name.
*
* @var string
*/
protected $collection = 'endnotes';
/**
* Element name.
*
* @var string
*/
protected $element = 'endnote';
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Footnotes reader.
*
* @since 0.10.0
*/
class Footnotes extends AbstractPart
{
/**
* Collection name footnotes|endnotes.
*
* @var string
*/
protected $collection = 'footnotes';
/**
* Element name footnote|endnote.
*
* @var string
*/
protected $element = 'footnote';
/**
* Read (footnotes|endnotes).xml.
*/
public function read(PhpWord $phpWord): void
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$nodes = $xmlReader->getElements('*');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$id = $xmlReader->getAttribute('w:id', $node);
$type = $xmlReader->getAttribute('w:type', $node);
// Avoid w:type "separator" and "continuationSeparator"
// Only look for <footnote> or <endnote> without w:type attribute, or with w:type = normal
if ((null === $type || $type === 'normal')) {
$element = $this->getElement($phpWord, $id);
if ($element !== null) {
$pNodes = $xmlReader->getElements('w:p/*', $node);
foreach ($pNodes as $pNode) {
$this->readRun($xmlReader, $pNode, $element, $this->collection);
}
$addMethod = "add{$this->element}";
$phpWord->$addMethod($element);
}
}
}
}
}
/**
* Searches for the element with the given relationId.
*
* @param int $relationId
*
* @return null|\PhpOffice\PhpWord\Element\AbstractContainer
*/
private function getElement(PhpWord $phpWord, $relationId)
{
$getMethod = "get{$this->collection}";
$collection = $phpWord->$getMethod()->getItems();
//not found by key, looping to search by relationId
foreach ($collection as $collectionElement) {
if ($collectionElement->getRelationId() == $relationId) {
return $collectionElement;
}
}
return null;
}
}

View File

@@ -0,0 +1,122 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
use DOMElement;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Numbering reader.
*
* @since 0.10.0
*/
class Numbering extends AbstractPart
{
/**
* Read numbering.xml.
*/
public function read(PhpWord $phpWord): void
{
$abstracts = [];
$numberings = [];
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
// Abstract numbering definition
$nodes = $xmlReader->getElements('w:abstractNum');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$abstractId = $xmlReader->getAttribute('w:abstractNumId', $node);
$abstracts[$abstractId] = ['levels' => []];
$abstract = &$abstracts[$abstractId];
$subnodes = $xmlReader->getElements('*', $node);
foreach ($subnodes as $subnode) {
switch ($subnode->nodeName) {
case 'w:multiLevelType':
$abstract['type'] = $xmlReader->getAttribute('w:val', $subnode);
break;
case 'w:lvl':
$levelId = $xmlReader->getAttribute('w:ilvl', $subnode);
$abstract['levels'][$levelId] = $this->readLevel($xmlReader, $subnode, $levelId);
break;
}
}
}
}
// Numbering instance definition
$nodes = $xmlReader->getElements('w:num');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$numId = $xmlReader->getAttribute('w:numId', $node);
$abstractId = $xmlReader->getAttribute('w:val', $node, 'w:abstractNumId');
$numberings[$numId] = $abstracts[$abstractId];
$numberings[$numId]['numId'] = $numId;
$subnodes = $xmlReader->getElements('w:lvlOverride/w:lvl', $node);
foreach ($subnodes as $subnode) {
$levelId = $xmlReader->getAttribute('w:ilvl', $subnode);
$overrides = $this->readLevel($xmlReader, $subnode, $levelId);
foreach ($overrides as $key => $value) {
$numberings[$numId]['levels'][$levelId][$key] = $value;
}
}
}
}
// Push to Style collection
foreach ($numberings as $numId => $numbering) {
$phpWord->addNumberingStyle("PHPWordList{$numId}", $numbering);
}
}
/**
* Read numbering level definition from w:abstractNum and w:num.
*
* @param int $levelId
*
* @return array
*/
private function readLevel(XMLReader $xmlReader, DOMElement $subnode, $levelId)
{
$level = [];
$level['level'] = $levelId;
$level['start'] = $xmlReader->getAttribute('w:val', $subnode, 'w:start');
$level['format'] = $xmlReader->getAttribute('w:val', $subnode, 'w:numFmt');
$level['restart'] = $xmlReader->getAttribute('w:val', $subnode, 'w:lvlRestart');
$level['suffix'] = $xmlReader->getAttribute('w:val', $subnode, 'w:suff');
$level['text'] = $xmlReader->getAttribute('w:val', $subnode, 'w:lvlText');
$level['alignment'] = $xmlReader->getAttribute('w:val', $subnode, 'w:lvlJc');
$level['tab'] = $xmlReader->getAttribute('w:pos', $subnode, 'w:pPr/w:tabs/w:tab');
$level['left'] = $xmlReader->getAttribute('w:left', $subnode, 'w:pPr/w:ind');
$level['hanging'] = $xmlReader->getAttribute('w:hanging', $subnode, 'w:pPr/w:ind');
$level['font'] = $xmlReader->getAttribute('w:ascii', $subnode, 'w:rPr/w:rFonts');
$level['hint'] = $xmlReader->getAttribute('w:hint', $subnode, 'w:rPr/w:rFonts');
foreach ($level as $key => $value) {
if (null === $value) {
unset($level[$key]);
}
}
return $level;
}
}

View File

@@ -0,0 +1,170 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
use DOMElement;
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
use PhpOffice\PhpWord\Style\Language;
/**
* Settings reader.
*
* @since 0.14.0
*/
class Settings extends AbstractPart
{
private static $booleanProperties = [
'mirrorMargins',
'hideSpellingErrors',
'hideGrammaticalErrors',
'trackRevisions',
'doNotTrackMoves',
'doNotTrackFormatting',
'evenAndOddHeaders',
'updateFields',
'autoHyphenation',
'doNotHyphenateCaps',
];
/**
* Read settings.xml.
*/
public function read(PhpWord $phpWord): void
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$docSettings = $phpWord->getSettings();
$nodes = $xmlReader->getElements('*');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$name = str_replace('w:', '', $node->nodeName);
$value = $xmlReader->getAttribute('w:val', $node);
$method = 'set' . $name;
if (in_array($name, $this::$booleanProperties)) {
if ($value == 'false') {
$docSettings->$method(false);
} else {
$docSettings->$method(true);
}
} elseif (method_exists($this, $method)) {
$this->$method($xmlReader, $phpWord, $node);
} elseif (method_exists($docSettings, $method)) {
$docSettings->$method($value);
}
}
}
}
/**
* Sets the document Language.
*/
protected function setThemeFontLang(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
{
$val = $xmlReader->getAttribute('w:val', $node);
$eastAsia = $xmlReader->getAttribute('w:eastAsia', $node);
$bidi = $xmlReader->getAttribute('w:bidi', $node);
$themeFontLang = new Language();
$themeFontLang->setLatin($val);
$themeFontLang->setEastAsia($eastAsia);
$themeFontLang->setBidirectional($bidi);
$phpWord->getSettings()->setThemeFontLang($themeFontLang);
}
/**
* Sets the document protection.
*/
protected function setDocumentProtection(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
{
$documentProtection = $phpWord->getSettings()->getDocumentProtection();
$edit = $xmlReader->getAttribute('w:edit', $node);
if ($edit !== null) {
$documentProtection->setEditing($edit);
}
}
/**
* Sets the proof state.
*/
protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
{
$proofState = $phpWord->getSettings()->getProofState();
$spelling = $xmlReader->getAttribute('w:spelling', $node);
$grammar = $xmlReader->getAttribute('w:grammar', $node);
if ($spelling !== null) {
$proofState->setSpelling($spelling);
}
if ($grammar !== null) {
$proofState->setGrammar($grammar);
}
}
/**
* Sets the proof state.
*/
protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
{
$percent = $xmlReader->getAttribute('w:percent', $node);
$val = $xmlReader->getAttribute('w:val', $node);
if ($percent !== null || $val !== null) {
$phpWord->getSettings()->setZoom($percent === null ? $val : $percent);
}
}
/**
* Set the Revision view.
*/
protected function setRevisionView(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
{
$revisionView = new TrackChangesView();
$revisionView->setMarkup(filter_var($xmlReader->getAttribute('w:markup', $node), FILTER_VALIDATE_BOOLEAN));
$revisionView->setComments($xmlReader->getAttribute('w:comments', $node));
$revisionView->setInsDel(filter_var($xmlReader->getAttribute('w:insDel', $node), FILTER_VALIDATE_BOOLEAN));
$revisionView->setFormatting(filter_var($xmlReader->getAttribute('w:formatting', $node), FILTER_VALIDATE_BOOLEAN));
$revisionView->setInkAnnotations(filter_var($xmlReader->getAttribute('w:inkAnnotations', $node), FILTER_VALIDATE_BOOLEAN));
$phpWord->getSettings()->setRevisionView($revisionView);
}
protected function setConsecutiveHyphenLimit(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
{
$value = $xmlReader->getAttribute('w:val', $node);
if ($value !== null) {
$phpWord->getSettings()->setConsecutiveHyphenLimit($value);
}
}
protected function setHyphenationZone(XMLReader $xmlReader, PhpWord $phpWord, DOMElement $node): void
{
$value = $xmlReader->getAttribute('w:val', $node);
if ($value !== null) {
$phpWord->getSettings()->setHyphenationZone($value);
}
}
}

View File

@@ -0,0 +1,107 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
use PhpOffice\PhpWord\Style\Language;
/**
* Styles reader.
*
* @since 0.10.0
*/
class Styles extends AbstractPart
{
/**
* Read styles.xml.
*/
public function read(PhpWord $phpWord): void
{
$xmlReader = new XMLReader();
$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($paragraphDefaultStyle);
}
}
$nodes = $xmlReader->getElements('w:style');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$type = $xmlReader->getAttribute('w:type', $node);
$name = $xmlReader->getAttribute('w:val', $node, 'w:name');
if (null === $name) {
$name = $xmlReader->getAttribute('w:styleId', $node);
}
$headingMatches = [];
preg_match('/Heading\s*(\d)/i', $name, $headingMatches);
// $default = ($xmlReader->getAttribute('w:default', $node) == 1);
switch ($type) {
case 'paragraph':
$paragraphStyle = $this->readParagraphStyle($xmlReader, $node);
$fontStyle = $this->readFontStyle($xmlReader, $node);
if (!empty($headingMatches)) {
$phpWord->addTitleStyle($headingMatches[1], $fontStyle, $paragraphStyle);
} else {
if (empty($fontStyle)) {
if (is_array($paragraphStyle)) {
$phpWord->addParagraphStyle($name, $paragraphStyle);
}
} else {
$phpWord->addFontStyle($name, $fontStyle, $paragraphStyle);
}
}
break;
case 'character':
$fontStyle = $this->readFontStyle($xmlReader, $node);
if (!empty($fontStyle)) {
$phpWord->addFontStyle($name, $fontStyle);
}
break;
case 'table':
$tStyle = $this->readTableStyle($xmlReader, $node);
if (!empty($tStyle)) {
$phpWord->addTableStyle($name, $tStyle);
}
break;
}
}
}
}
}