2014-04-25 16:07:13 +07:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* 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.
|
2014-04-25 16:07:13 +07:00
|
|
|
*
|
2017-11-04 22:44:12 +01:00
|
|
|
* @see https://github.com/PHPOffice/PHPWord
|
2018-03-08 23:52:25 +01:00
|
|
|
* @copyright 2010-2018 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-04-25 16:07:13 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpWord\Writer\ODText\Element;
|
|
|
|
|
|
2018-02-09 21:49:11 +01:00
|
|
|
use PhpOffice\PhpWord\Element\TrackChange;
|
2014-05-14 19:41:44 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
|
|
|
|
|
2014-04-25 16:07:13 +07:00
|
|
|
/**
|
|
|
|
|
* Text element writer
|
|
|
|
|
*
|
|
|
|
|
* @since 0.10.0
|
|
|
|
|
*/
|
2014-05-07 09:47:02 +07:00
|
|
|
class Text extends AbstractElement
|
2014-04-25 16:07:13 +07:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Write element
|
|
|
|
|
*/
|
|
|
|
|
public function write()
|
|
|
|
|
{
|
2014-05-07 09:47:02 +07:00
|
|
|
$xmlWriter = $this->getXmlWriter();
|
|
|
|
|
$element = $this->getElement();
|
2014-05-11 19:41:48 +07:00
|
|
|
if (!$element instanceof \PhpOffice\PhpWord\Element\Text) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-05-07 09:47:02 +07:00
|
|
|
$fontStyle = $element->getFontStyle();
|
|
|
|
|
$paragraphStyle = $element->getParagraphStyle();
|
2014-04-25 16:07:13 +07:00
|
|
|
|
|
|
|
|
// @todo Commented for TextRun. Should really checkout this value
|
2014-05-04 15:13:31 +07:00
|
|
|
// $fStyleIsObject = ($fontStyle instanceof Font) ? true : false;
|
|
|
|
|
$fStyleIsObject = false;
|
2014-04-25 16:07:13 +07:00
|
|
|
|
2014-05-04 15:13:31 +07:00
|
|
|
if ($fStyleIsObject) {
|
2014-04-25 16:07:13 +07:00
|
|
|
// Don't never be the case, because I browse all sections for cleaning all styles not declared
|
2014-05-04 15:13:31 +07:00
|
|
|
throw new Exception('PhpWord : $fStyleIsObject wouldn\'t be an object');
|
2017-11-04 22:44:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$this->withoutP) {
|
|
|
|
|
$xmlWriter->startElement('text:p'); // text:p
|
|
|
|
|
}
|
2018-02-09 21:49:11 +01:00
|
|
|
if ($element->getTrackChange() != null && $element->getTrackChange()->getChangeType() == TrackChange::DELETED) {
|
|
|
|
|
$xmlWriter->startElement('text:change');
|
|
|
|
|
$xmlWriter->writeAttribute('text:change-id', $element->getTrackChange()->getElementId());
|
|
|
|
|
$xmlWriter->endElement();
|
2017-11-04 22:44:12 +01:00
|
|
|
} else {
|
2018-02-09 21:49:11 +01:00
|
|
|
if (empty($fontStyle)) {
|
|
|
|
|
if (empty($paragraphStyle)) {
|
|
|
|
|
$xmlWriter->writeAttribute('text:style-name', 'P1');
|
|
|
|
|
} elseif (is_string($paragraphStyle)) {
|
|
|
|
|
$xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
|
|
|
|
|
}
|
|
|
|
|
$this->writeChangeInsertion(true, $element->getTrackChange());
|
|
|
|
|
$this->writeText($element->getText());
|
|
|
|
|
$this->writeChangeInsertion(false, $element->getTrackChange());
|
|
|
|
|
} else {
|
|
|
|
|
if (empty($paragraphStyle)) {
|
|
|
|
|
$xmlWriter->writeAttribute('text:style-name', 'Standard');
|
|
|
|
|
} elseif (is_string($paragraphStyle)) {
|
|
|
|
|
$xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
|
|
|
|
|
}
|
|
|
|
|
// text:span
|
|
|
|
|
$xmlWriter->startElement('text:span');
|
|
|
|
|
if (is_string($fontStyle)) {
|
|
|
|
|
$xmlWriter->writeAttribute('text:style-name', $fontStyle);
|
|
|
|
|
}
|
|
|
|
|
$this->writeChangeInsertion(true, $element->getTrackChange());
|
|
|
|
|
$this->writeText($element->getText());
|
|
|
|
|
$this->writeChangeInsertion(false, $element->getTrackChange());
|
|
|
|
|
$xmlWriter->endElement();
|
2017-11-04 22:44:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!$this->withoutP) {
|
|
|
|
|
$xmlWriter->endElement(); // text:p
|
2014-04-25 16:07:13 +07:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-09 21:49:11 +01:00
|
|
|
|
|
|
|
|
private function writeChangeInsertion($start = true, TrackChange $trackChange = null)
|
|
|
|
|
{
|
|
|
|
|
if ($trackChange == null || $trackChange->getChangeType() != TrackChange::INSERTED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$xmlWriter = $this->getXmlWriter();
|
|
|
|
|
$xmlWriter->startElement('text:change-' . ($start ? 'start' : 'end'));
|
|
|
|
|
$xmlWriter->writeAttribute('text:change-id', $trackChange->getElementId());
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
2014-04-25 16:07:13 +07:00
|
|
|
}
|