Skip to content

Implement various missing features for the ODT writer #1796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/PhpWord/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\Style\Font;

/**
* Field element
*
Expand Down Expand Up @@ -115,24 +117,58 @@ class Field extends AbstractElement
/**
* Font style
*
* @var \PhpOffice\PhpWord\Style\Font
* @var string|\PhpOffice\PhpWord\Style\Font
*/
protected $fontStyle;

/**
* Set Font style
*
* @param string|array|\PhpOffice\PhpWord\Style\Font $style
* @return string|\PhpOffice\PhpWord\Style\Font
*/
public function setFontStyle($style = null)
{
if ($style instanceof Font) {
$this->fontStyle = $style;
} elseif (is_array($style)) {
$this->fontStyle = new Font('text');
$this->fontStyle->setStyleByArray($style);
} elseif (null === $style) {
$this->fontStyle = null;
} else {
$this->fontStyle = $style;
}

return $this->fontStyle;
}

/**
* Get Font style
*
* @return string|\PhpOffice\PhpWord\Style\Font
*/
public function getFontStyle()
{
return $this->fontStyle;
}

/**
* Create a new Field Element
*
* @param string $type
* @param array $properties
* @param array $options
* @param TextRun|string|null $text
* @param string|array|\PhpOffice\PhpWord\Style\Font $fontStyle
*/
public function __construct($type = null, $properties = array(), $options = array(), $text = null)
public function __construct($type = null, $properties = array(), $options = array(), $text = null, $fontStyle = null)
{
$this->setType($type);
$this->setProperties($properties);
$this->setOptions($options);
$this->setText($text);
$this->setFontStyle($fontStyle);
}

/**
Expand Down
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
46 changes: 46 additions & 0 deletions src/PhpWord/Shared/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,50 @@ public static function angleToDegree($angle = 1)
return round($angle / self::DEGREE_TO_ANGLE);
}

/**
* Convert colorname as string to RGB
*
* @param string $value color name
* @return string color as hex RGB string, or original value if unknown
*/
public static function stringToRgb($value)
{
switch ($value) {
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW:
return 'FFFF00';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_LIGHTGREEN:
return '90EE90';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_CYAN:
return '00FFFF';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_MAGENTA:
return 'FF00FF';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_BLUE:
return '0000FF';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_RED:
return 'FF0000';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKBLUE:
return '00008B';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKCYAN:
return '008B8B';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKGREEN:
return '006400';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKMAGENTA:
return '8B008B';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKRED:
return '8B0000';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKYELLOW:
return '8B8B00';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKGRAY:
return 'A9A9A9';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_LIGHTGRAY:
return 'D3D3D3';
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_BLACK:
return '000000';
}

return $value;
}

/**
* Convert HTML hexadecimal to RGB
*
Expand All @@ -282,6 +326,8 @@ public static function htmlToRgb($value)
{
if ($value[0] == '#') {
$value = substr($value, 1);
} else {
$value = self::stringToRgb($value);
}

if (strlen($value) == 6) {
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
Loading