Skip to content

Commit

Permalink
Merge pull request #1796 from oleibman/odtchanges
Browse files Browse the repository at this point in the history
ODT Changes
  • Loading branch information
troosan committed Dec 29, 2020
2 parents 6126080 + ba3d616 commit b2335d2
Show file tree
Hide file tree
Showing 24 changed files with 1,922 additions and 84 deletions.
2 changes: 2 additions & 0 deletions src/PhpWord/Element/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public function getFootnoteProperties()
* @deprecated Use the `getFootnoteProperties` method instead
*
* @return FootnoteProperties
*
* @codeCoverageIgnore
*/
public function getFootnotePropoperties()
{
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Paragraph extends Border
/**
* Indentation
*
* @var \PhpOffice\PhpWord\Style\Indentation
* @var \PhpOffice\PhpWord\Style\Indentation|null
*/
private $indentation;

Expand Down
81 changes: 81 additions & 0 deletions src/PhpWord/Writer/ODText/Element/Field.php
Original file line number Diff line number Diff line change
@@ -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
* @copyright 2010-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
// Not fully implemented
// - supports only PAGE and NUMPAGES
// - supports only default formats and options
// - supports style only if specified by name
// - spaces before and after field may be dropped

namespace PhpOffice\PhpWord\Writer\ODText\Element;

/**
* Field element writer
*
* @since 0.11.0
*/
class Field extends Text
{
/**
* Write field element.
*/
public function write()
{
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Field) {
return;
}

$type = strtolower($element->getType());
switch ($type) {
case 'date':
case 'page':
case 'numpages':
$this->writeDefault($element, $type);
break;
}
}

private function writeDefault(\PhpOffice\PhpWord\Element\Field $element, $type)
{
$xmlWriter = $this->getXmlWriter();

$xmlWriter->startElement('text:span');
if (method_exists($element, 'getFontStyle')) {
$fstyle = $element->getFontStyle();
if (is_string($fstyle)) {
$xmlWriter->writeAttribute('text:style-name', $fstyle);
}
}
switch ($type) {
case 'date':
$xmlWriter->startElement('text:date');
$xmlWriter->writeAttribute('text:fixed', 'false');
$xmlWriter->endElement();
break;
case 'page':
$xmlWriter->startElement('text:page-number');
$xmlWriter->writeAttribute('text:fixed', 'false');
$xmlWriter->endElement();
break;
case 'numpages':
$xmlWriter->startElement('text:page-count');
$xmlWriter->endElement();
break;
}
$xmlWriter->endElement(); // text:span
}
}
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/ODText/Element/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function write()
$height = Converter::pixelToCm($style->getHeight());

$xmlWriter->startElement('text:p');
$xmlWriter->writeAttribute('text:style-name', 'Standard');
$xmlWriter->writeAttribute('text:style-name', 'IM' . $mediaIndex);

$xmlWriter->startElement('draw:frame');
$xmlWriter->writeAttribute('draw:style-name', 'fr' . $mediaIndex);
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/ODText/Element/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function write()

$xmlWriter->startElement('text:a');
$xmlWriter->writeAttribute('xlink:type', 'simple');
$xmlWriter->writeAttribute('xlink:href', $element->getSource());
$xmlWriter->writeAttribute('xlink:href', ($element->isInternal() ? '#' : '') . $element->getSource());
$this->writeText($element->getText());
$xmlWriter->endElement(); // text:a

Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/ODText/Element/PageBreak.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function write()
$xmlWriter = $this->getXmlWriter();

$xmlWriter->startElement('text:p');
$xmlWriter->writeAttribute('text:style-name', 'P1');
$xmlWriter->writeAttribute('text:style-name', 'PB');
$xmlWriter->endElement();
}
}
58 changes: 47 additions & 11 deletions src/PhpWord/Writer/ODText/Element/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public function write()

// @todo Commented for TextRun. Should really checkout this value
// $fStyleIsObject = ($fontStyle instanceof Font) ? true : false;
$fStyleIsObject = false;
//$fStyleIsObject = false;

if ($fStyleIsObject) {
// Don't never be the case, because I browse all sections for cleaning all styles not declared
throw new Exception('PhpWord : $fStyleIsObject wouldn\'t be an object');
}
//if ($fStyleIsObject) {
// Don't never be the case, because I browse all sections for cleaning all styles not declared
// throw new Exception('PhpWord : $fStyleIsObject wouldn\'t be an object');
//}

if (!$this->withoutP) {
$xmlWriter->startElement('text:p'); // text:p
Expand All @@ -59,26 +59,34 @@ public function write()
} else {
if (empty($fontStyle)) {
if (empty($paragraphStyle)) {
$xmlWriter->writeAttribute('text:style-name', 'P1');
if (!$this->withoutP) {
$xmlWriter->writeAttribute('text:style-name', 'Normal');
}
} elseif (is_string($paragraphStyle)) {
$xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
if (!$this->withoutP) {
$xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
}
}
$this->writeChangeInsertion(true, $element->getTrackChange());
$this->writeText($element->getText());
$this->replaceTabs($element->getText(), $xmlWriter);
$this->writeChangeInsertion(false, $element->getTrackChange());
} else {
if (empty($paragraphStyle)) {
$xmlWriter->writeAttribute('text:style-name', 'Standard');
if (!$this->withoutP) {
$xmlWriter->writeAttribute('text:style-name', 'Normal');
}
} elseif (is_string($paragraphStyle)) {
$xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
if (!$this->withoutP) {
$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->replaceTabs($element->getText(), $xmlWriter);
$this->writeChangeInsertion(false, $element->getTrackChange());
$xmlWriter->endElement();
}
Expand All @@ -88,6 +96,34 @@ public function write()
}
}

private function replacetabs($text, $xmlWriter)
{
if (preg_match('/^ +/', $text, $matches)) {
$num = strlen($matches[0]);
$xmlWriter->startElement('text:s');
$xmlWriter->writeAttributeIf($num > 1, 'text:c', "$num");
$xmlWriter->endElement();
$text = preg_replace('/^ +/', '', $text);
}
preg_match_all('/([\\s\\S]*?)(\\t| +| ?$)/', $text, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$this->writeText($match[1]);
if ($match[2] === '') {
break;
} elseif ($match[2] === "\t") {
$xmlWriter->writeElement('text:tab');
} elseif ($match[2] === ' ') {
$xmlWriter->writeElement('text:s');
break;
} else {
$num = strlen($match[2]);
$xmlWriter->startElement('text:s');
$xmlWriter->writeAttributeIf($num > 1, 'text:c', "$num");
$xmlWriter->endElement();
}
}
}

private function writeChangeInsertion($start = true, TrackChange $trackChange = null)
{
if ($trackChange == null || $trackChange->getChangeType() != TrackChange::INSERTED) {
Expand Down
8 changes: 7 additions & 1 deletion src/PhpWord/Writer/ODText/Element/TextRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @since 0.10.0
*/
class TextRun extends AbstractElement
class TextRun extends Text
{
/**
* Write element
Expand All @@ -33,6 +33,12 @@ public function write()
$element = $this->getElement();

$xmlWriter->startElement('text:p');
/** @scrutinizer ignore-call */
$pStyle = $element->getParagraphStyle();
if (!is_string($pStyle)) {
$pStyle = 'Normal';
}
$xmlWriter->writeAttribute('text:style-name', $pStyle);

$containerWriter = new Container($xmlWriter, $element);
$containerWriter->write();
Expand Down
32 changes: 31 additions & 1 deletion src/PhpWord/Writer/ODText/Element/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,44 @@ public function write()
}

$xmlWriter->startElement('text:h');
$xmlWriter->writeAttribute('text:outline-level', $element->getDepth());
$hdname = 'HD';
$sect = $element->getParent();
if ($sect instanceof \PhpOffice\PhpWord\Element\Section) {
if (self::compareToFirstElement($element, $sect->getElements())) {
$hdname = 'HE';
}
}
$depth = $element->getDepth();
$xmlWriter->writeAttribute('text:style-name', "$hdname$depth");
$xmlWriter->writeAttribute('text:outline-level', $depth);
$xmlWriter->startElement('text:span');
if ($depth > 0) {
$xmlWriter->writeAttribute('text:style-name', 'Heading_' . $depth);
} else {
$xmlWriter->writeAttribute('text:style-name', 'Title');
}
$text = $element->getText();
if (is_string($text)) {
$this->writeText($text);
} elseif ($text instanceof \PhpOffice\PhpWord\Element\AbstractContainer) {
$containerWriter = new Container($xmlWriter, $text);
$containerWriter->write();
}
$xmlWriter->endElement(); // text:span
$xmlWriter->endElement(); // text:h
}

/**
* Test if element is same as first element in array
*
* @param \PhpOffice\PhpWord\Element\AbstractElement $elem
*
* @param \PhpOffice\PhpWord\Element\AbstractElement[] $elemarray
*
* @return bool
*/
private static function compareToFirstElement($elem, $elemarray)
{
return $elem === $elemarray[0];
}
}
Loading

0 comments on commit b2335d2

Please sign in to comment.