From 1ee43da4de7895201c8f038c085c6f3a666b437d Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 13 May 2014 23:36:33 +0700 Subject: [PATCH] #196: RTF link styling --- src/PhpWord/Element/ListItem.php | 32 +++-- src/PhpWord/Writer/RTF.php | 4 +- .../Writer/RTF/Element/AbstractElement.php | 132 ++++++++++++++++++ src/PhpWord/Writer/RTF/Element/Link.php | 20 ++- src/PhpWord/Writer/RTF/Element/ListItem.php | 29 ++++ src/PhpWord/Writer/RTF/Element/Text.php | 112 ++------------- src/PhpWord/Writer/RTF/Element/TextRun.php | 10 +- src/PhpWord/Writer/RTF/Element/Title.php | 25 +--- src/PhpWord/Writer/RTF/Style/Font.php | 2 +- src/PhpWord/Writer/Word2007/Element/Table.php | 1 - src/PhpWord/Writer/Word2007/Style/Table.php | 3 - 11 files changed, 214 insertions(+), 156 deletions(-) create mode 100644 src/PhpWord/Writer/RTF/Element/ListItem.php diff --git a/src/PhpWord/Element/ListItem.php b/src/PhpWord/Element/ListItem.php index 1042362c..59bd8391 100644 --- a/src/PhpWord/Element/ListItem.php +++ b/src/PhpWord/Element/ListItem.php @@ -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(); + } } diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 434ffd67..27329db2 100644 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -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) { diff --git a/src/PhpWord/Writer/RTF/Element/AbstractElement.php b/src/PhpWord/Writer/RTF/Element/AbstractElement.php index fbf8114e..e6e42c18 100644 --- a/src/PhpWord/Writer/RTF/Element/AbstractElement.php +++ b/src/PhpWord/Writer/RTF/Element/AbstractElement.php @@ -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; + } } diff --git a/src/PhpWord/Writer/RTF/Element/Link.php b/src/PhpWord/Writer/RTF/Element/Link.php index bb87e291..684f6e65 100644 --- a/src/PhpWord/Writer/RTF/Element/Link.php +++ b/src/PhpWord/Writer/RTF/Element/Link.php @@ -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; } diff --git a/src/PhpWord/Writer/RTF/Element/ListItem.php b/src/PhpWord/Writer/RTF/Element/ListItem.php new file mode 100644 index 00000000..96375a4a --- /dev/null +++ b/src/PhpWord/Writer/RTF/Element/ListItem.php @@ -0,0 +1,29 @@ +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; - } } diff --git a/src/PhpWord/Writer/RTF/Element/TextRun.php b/src/PhpWord/Writer/RTF/Element/TextRun.php index 8d7324a3..8d70366c 100644 --- a/src/PhpWord/Writer/RTF/Element/TextRun.php +++ b/src/PhpWord/Writer/RTF/Element/TextRun.php @@ -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; } diff --git a/src/PhpWord/Writer/RTF/Element/Title.php b/src/PhpWord/Writer/RTF/Element/Title.php index 78bb3c2e..998bcedc 100644 --- a/src/PhpWord/Writer/RTF/Element/Title.php +++ b/src/PhpWord/Writer/RTF/Element/Title.php @@ -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; - } } diff --git a/src/PhpWord/Writer/RTF/Style/Font.php b/src/PhpWord/Writer/RTF/Style/Font.php index d14ee9c5..b70c089f 100644 --- a/src/PhpWord/Writer/RTF/Style/Font.php +++ b/src/PhpWord/Writer/RTF/Style/Font.php @@ -63,7 +63,7 @@ class Font extends AbstractStyle $content .= $this->getValueIf($style->isSuperScript(), '\super'); $content .= $this->getValueIf($style->isSubScript(), '\sub'); - return $content; + return $content . ' '; } /** diff --git a/src/PhpWord/Writer/Word2007/Element/Table.php b/src/PhpWord/Writer/Word2007/Element/Table.php index 6349172a..b936d697 100644 --- a/src/PhpWord/Writer/Word2007/Element/Table.php +++ b/src/PhpWord/Writer/Word2007/Element/Table.php @@ -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; diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index d996df00..590e819b 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -147,9 +147,6 @@ class Table extends AbstractStyle /** * Write shading - * - * @param int $width - * @param string $unit */ private function writeShading(XMLWriter $xmlWriter, TableStyle $style) {