#196: RTF link styling
This commit is contained in:
parent
7ae8c3cb81
commit
1ee43da4de
|
|
@ -26,27 +26,26 @@ use PhpOffice\PhpWord\Style\ListItem as ListItemStyle;
|
|||
class ListItem extends AbstractElement
|
||||
{
|
||||
/**
|
||||
* ListItem Style
|
||||
* Element style
|
||||
*
|
||||
* @var \PhpOffice\PhpWord\Style\ListItem
|
||||
*/
|
||||
private $style;
|
||||
|
||||
/**
|
||||
* Textrun
|
||||
* Text object
|
||||
*
|
||||
* @var Text
|
||||
* @var \PhpOffice\PhpWord\Element\Text
|
||||
*/
|
||||
private $textObject;
|
||||
|
||||
/**
|
||||
* ListItem Depth
|
||||
* Depth
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $depth;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new ListItem
|
||||
*
|
||||
|
|
@ -70,7 +69,9 @@ class ListItem extends AbstractElement
|
|||
}
|
||||
|
||||
/**
|
||||
* Get ListItem style
|
||||
* Get style
|
||||
*
|
||||
* @return \PhpOffice\PhpWord\Style\ListItem
|
||||
*/
|
||||
public function getStyle()
|
||||
{
|
||||
|
|
@ -78,7 +79,9 @@ class ListItem extends AbstractElement
|
|||
}
|
||||
|
||||
/**
|
||||
* Get ListItem TextRun
|
||||
* Get Text object
|
||||
*
|
||||
* @return \PhpOffice\PhpWord\Element\Text
|
||||
*/
|
||||
public function getTextObject()
|
||||
{
|
||||
|
|
@ -86,10 +89,23 @@ class ListItem extends AbstractElement
|
|||
}
|
||||
|
||||
/**
|
||||
* Get ListItem depth
|
||||
* Get depth
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getDepth()
|
||||
{
|
||||
return $this->depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get text
|
||||
*
|
||||
* @return string
|
||||
* @since 0.11.0
|
||||
*/
|
||||
public function getText()
|
||||
{
|
||||
return $this->textObject->getText();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -226,7 +226,7 @@ class RTF extends AbstractWriter implements WriterInterface
|
|||
$elements = $section->getElements();
|
||||
|
||||
foreach ($elements as $element) {
|
||||
if ($element instanceof Text) {
|
||||
if (method_exists($element, 'getFontStyle')) {
|
||||
$fontStyle = $element->getFontStyle();
|
||||
|
||||
if ($fontStyle instanceof Font) {
|
||||
|
|
@ -284,7 +284,7 @@ class RTF extends AbstractWriter implements WriterInterface
|
|||
$elements = $section->getElements();
|
||||
|
||||
foreach ($elements as $element) {
|
||||
if ($element instanceof Text) {
|
||||
if (method_exists($element, 'getFontStyle')) {
|
||||
$fontStyle = $element->getFontStyle();
|
||||
|
||||
if ($fontStyle instanceof Font) {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,13 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
||||
|
||||
use PhpOffice\PhpWord\Shared\String;
|
||||
use PhpOffice\PhpWord\Style;
|
||||
use PhpOffice\PhpWord\Style\Font as FontStyle;
|
||||
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
|
||||
use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter;
|
||||
use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter;
|
||||
|
||||
/**
|
||||
* Abstract RTF element writer
|
||||
*
|
||||
|
|
@ -24,4 +31,129 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
|||
*/
|
||||
class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractElement
|
||||
{
|
||||
/**
|
||||
* Font style
|
||||
*
|
||||
* @var \PhpWord\PhpOffice\Style\Font
|
||||
*/
|
||||
private $fontStyle;
|
||||
|
||||
/**
|
||||
* Paragraph style
|
||||
*
|
||||
* @var \PhpWord\PhpOffice\Style\Paragraph
|
||||
*/
|
||||
private $paragraphStyle;
|
||||
|
||||
/**
|
||||
* Get font and paragraph styles
|
||||
*/
|
||||
protected function getStyles()
|
||||
{
|
||||
/** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */
|
||||
$parentWriter = $this->parentWriter;
|
||||
|
||||
// Font style
|
||||
if (method_exists($this->element, 'getFontStyle')) {
|
||||
$this->fontStyle = $this->element->getFontStyle();
|
||||
if (is_string($this->fontStyle)) {
|
||||
$this->fontStyle = Style::getStyle($this->fontStyle);
|
||||
}
|
||||
}
|
||||
|
||||
// Paragraph style
|
||||
if (method_exists($this->element, 'getParagraphStyle')) {
|
||||
$this->paragraphStyle = $this->element->getParagraphStyle();
|
||||
if (is_string($this->paragraphStyle)) {
|
||||
$this->paragraphStyle = Style::getStyle($this->paragraphStyle);
|
||||
}
|
||||
|
||||
if ($this->paragraphStyle !== null && !$this->withoutP) {
|
||||
if ($parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) {
|
||||
$parentWriter->setLastParagraphStyle($this->element->getParagraphStyle());
|
||||
} else {
|
||||
$parentWriter->setLastParagraphStyle();
|
||||
$this->paragraphStyle = null;
|
||||
}
|
||||
} else {
|
||||
$parentWriter->setLastParagraphStyle();
|
||||
$this->paragraphStyle = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write opening
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function writeOpening()
|
||||
{
|
||||
if ($this->withoutP || !$this->paragraphStyle instanceof ParagraphStyle) {
|
||||
return;
|
||||
}
|
||||
|
||||
$styleWriter = new ParagraphStyleWriter($this->paragraphStyle);
|
||||
return $styleWriter->write();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write text
|
||||
*
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
protected function writeText($text)
|
||||
{
|
||||
return String::toUnicode($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write closing
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function writeClosing()
|
||||
{
|
||||
if ($this->withoutP) {
|
||||
return;
|
||||
}
|
||||
|
||||
return '\par' . PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write font style
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function writeFontStyle()
|
||||
{
|
||||
if (!$this->fontStyle instanceof FontStyle) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */
|
||||
$parentWriter = $this->parentWriter;
|
||||
|
||||
// Create style writer and set color/name index
|
||||
$styleWriter = new FontStyleWriter($this->fontStyle);
|
||||
if ($this->fontStyle->getColor() != null) {
|
||||
$colorIndex = array_search($this->fontStyle->getColor(), $parentWriter->getColorTable());
|
||||
if ($colorIndex !== false) {
|
||||
$styleWriter->setColorIndex($colorIndex + 1);
|
||||
}
|
||||
}
|
||||
if ($this->fontStyle->getName() != null) {
|
||||
$fontIndex = array_search($this->fontStyle->getName(), $parentWriter->getFontTable());
|
||||
if ($fontIndex !== false) {
|
||||
$styleWriter->setNameIndex($fontIndex + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Write style
|
||||
$content = $styleWriter->write();
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,8 +17,6 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
||||
|
||||
use PhpOffice\PhpWord\Shared\String;
|
||||
|
||||
/**
|
||||
* Link element RTF writer
|
||||
*
|
||||
|
|
@ -33,21 +31,19 @@ class Link extends AbstractElement
|
|||
*/
|
||||
public function write()
|
||||
{
|
||||
$element = $this->element;
|
||||
if (!$element instanceof \PhpOffice\PhpWord\Element\Link) {
|
||||
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->getStyles();
|
||||
|
||||
$content = '';
|
||||
if (!$this->withoutP) {
|
||||
$content .= '{';
|
||||
}
|
||||
$content .= '{\field {\*\fldinst {HYPERLINK "' . $element->getTarget() . '"}}{\\fldrslt {';
|
||||
$content .= String::toUnicode($element->getText());
|
||||
$content .= $this->writeOpening();
|
||||
$content .= '{\field {\*\fldinst {HYPERLINK "' . $this->element->getTarget() . '"}}{\\fldrslt {';
|
||||
$content .= $this->writeFontStyle();
|
||||
$content .= $this->writeText($this->element->getText());
|
||||
$content .= '}}}';
|
||||
if (!$this->withoutP) {
|
||||
$content .= '}';
|
||||
}
|
||||
$content .= $this->writeClosing();
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
<?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.
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2010-2014 PHPWord contributors
|
||||
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
||||
|
||||
use PhpOffice\PhpWord\Shared\String;
|
||||
|
||||
/**
|
||||
* ListItem element RTF writer; extends from text
|
||||
*
|
||||
* @since 0.11.0
|
||||
*/
|
||||
class ListItem extends Text
|
||||
{
|
||||
}
|
||||
|
|
@ -20,9 +20,6 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
|||
use PhpOffice\PhpWord\Element\Text as TextElement;
|
||||
use PhpOffice\PhpWord\Shared\String;
|
||||
use PhpOffice\PhpWord\Style;
|
||||
use PhpOffice\PhpWord\Style\Font as FontStyle;
|
||||
use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter;
|
||||
use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter;
|
||||
|
||||
/**
|
||||
* Text element RTF writer
|
||||
|
|
@ -33,117 +30,26 @@ class Text extends AbstractElement
|
|||
{
|
||||
/**
|
||||
* Write element
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
|
||||
$elementClass = str_replace('\\Writer\\RTF', '', get_class($this));
|
||||
if (!$this->element instanceof $elementClass) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var \PhpOffice\PhpWord\Style\Font $fontStyle Scrutinizer type hint */
|
||||
$fontStyle = $this->getFontStyle($this->element);
|
||||
/** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */
|
||||
$parentWriter = $this->parentWriter;
|
||||
$this->getStyles();
|
||||
|
||||
$content = '';
|
||||
$content .= $this->writeParagraphStyle($this->element);
|
||||
$content .= $this->writeOpening();
|
||||
$content .= '{';
|
||||
$content .= $this->writeFontStyle($fontStyle);
|
||||
if ($fontStyle || $parentWriter->getLastParagraphStyle() != '') {
|
||||
$content .= ' ';
|
||||
}
|
||||
$content .= String::toUnicode($this->element->getText());
|
||||
$content .= $this->writeFontStyle();
|
||||
$content .= $this->writeText($this->element->getText());
|
||||
$content .= '}';
|
||||
|
||||
if (!$this->withoutP) {
|
||||
$content .= '\par' . PHP_EOL;
|
||||
}
|
||||
$content .= $this->writeClosing();
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write paragraph style
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function writeParagraphStyle(TextElement $element)
|
||||
{
|
||||
/** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */
|
||||
$parentWriter = $this->parentWriter;
|
||||
$content = '';
|
||||
|
||||
// Get paragraph style
|
||||
$paragraphStyle = $element->getParagraphStyle();
|
||||
if (is_string($paragraphStyle)) {
|
||||
$paragraphStyle = Style::getStyle($paragraphStyle);
|
||||
}
|
||||
|
||||
// Write style when applicable
|
||||
if ($paragraphStyle && !$this->withoutP) {
|
||||
if ($parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) {
|
||||
$parentWriter->setLastParagraphStyle($element->getParagraphStyle());
|
||||
|
||||
$styleWriter = new ParagraphStyleWriter($paragraphStyle);
|
||||
$content = $styleWriter->write();
|
||||
} else {
|
||||
$parentWriter->setLastParagraphStyle();
|
||||
}
|
||||
} else {
|
||||
$parentWriter->setLastParagraphStyle();
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write font style beginning
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Style\Font $style
|
||||
* @return string
|
||||
*/
|
||||
private function writeFontStyle($style)
|
||||
{
|
||||
if (!$style instanceof FontStyle) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */
|
||||
$parentWriter = $this->parentWriter;
|
||||
|
||||
// Create style writer and set color/name index
|
||||
$styleWriter = new FontStyleWriter($style);
|
||||
if ($style->getColor() != null) {
|
||||
$colorIndex = array_search($style->getColor(), $parentWriter->getColorTable());
|
||||
if ($colorIndex !== false) {
|
||||
$styleWriter->setColorIndex($colorIndex + 1);
|
||||
}
|
||||
}
|
||||
if ($style->getName() != null) {
|
||||
$fontIndex = array_search($style->getName(), $parentWriter->getFontTable());
|
||||
if ($fontIndex !== false) {
|
||||
$styleWriter->setNameIndex($fontIndex + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Write style
|
||||
$content = $styleWriter->write();
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get font style
|
||||
*
|
||||
* @return \PhpOffice\PhpWord\Style\Font
|
||||
*/
|
||||
private function getFontStyle(TextElement $element)
|
||||
{
|
||||
$fontStyle = $element->getFontStyle();
|
||||
if (is_string($fontStyle)) {
|
||||
$fontStyle = Style::getStyle($fontStyle);
|
||||
}
|
||||
|
||||
return $fontStyle;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,12 +33,14 @@ class TextRun extends AbstractElement
|
|||
*/
|
||||
public function write()
|
||||
{
|
||||
$content = '';
|
||||
|
||||
$content .= '{\pard\nowidctlpar';
|
||||
$writer = new Container($this->parentWriter, $this->element);
|
||||
|
||||
$content = '';
|
||||
$content .= $this->writeOpening();
|
||||
$content .= '{';
|
||||
$content .= $writer->write();
|
||||
$content .= '\par}' . PHP_EOL;
|
||||
$content .= '}';
|
||||
$content .= $this->writeClosing();
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,29 +20,10 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
|||
use PhpOffice\PhpWord\Shared\String;
|
||||
|
||||
/**
|
||||
* TextBreak element RTF writer
|
||||
* Title element RTF writer; extends from text
|
||||
*
|
||||
* @since 0.10.0
|
||||
* @since 0.11.0
|
||||
*/
|
||||
class Title extends AbstractElement
|
||||
class Title extends Text
|
||||
{
|
||||
/**
|
||||
* Write element
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = '';
|
||||
|
||||
$content .= '\pard\nowidctlpar ';
|
||||
$content .= String::toUnicode($this->element->getText());
|
||||
$content .= '\par' . PHP_EOL;
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ class Font extends AbstractStyle
|
|||
$content .= $this->getValueIf($style->isSuperScript(), '\super');
|
||||
$content .= $this->getValueIf($style->isSubScript(), '\sub');
|
||||
|
||||
return $content;
|
||||
return $content . ' ';
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ use PhpOffice\PhpWord\Element\Table as TableElement;
|
|||
use PhpOffice\PhpWord\Shared\XMLWriter;
|
||||
use PhpOffice\PhpWord\Style\Cell as CellStyle;
|
||||
use PhpOffice\PhpWord\Style\Row as RowStyle;
|
||||
use PhpOffice\PhpWord\Style\Table as TableStyle;
|
||||
use PhpOffice\PhpWord\Writer\Word2007\Style\Cell as CellStyleWriter;
|
||||
use PhpOffice\PhpWord\Writer\Word2007\Style\Row as RowStyleWriter;
|
||||
use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
|
||||
|
|
|
|||
|
|
@ -147,9 +147,6 @@ class Table extends AbstractStyle
|
|||
|
||||
/**
|
||||
* Write shading
|
||||
*
|
||||
* @param int $width
|
||||
* @param string $unit
|
||||
*/
|
||||
private function writeShading(XMLWriter $xmlWriter, TableStyle $style)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue