From 0b13b22e07af71d9781f04fdaa8f931eea6f5134 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 9 May 2014 14:57:06 +0700 Subject: [PATCH] Refactor HTML & PDF writer with composite pattern --- src/PhpWord/Reader/Word2007/Document.php | 4 +- src/PhpWord/Style/Paragraph.php | 1 - src/PhpWord/Writer/HTML.php | 115 ++++++------ .../Element/AbstractElement.php} | 31 ++-- src/PhpWord/Writer/HTML/Element/Container.php | 50 ++++++ src/PhpWord/Writer/HTML/Element/Element.php | 84 --------- src/PhpWord/Writer/HTML/Element/Footnote.php | 15 +- src/PhpWord/Writer/HTML/Element/Image.php | 17 +- src/PhpWord/Writer/HTML/Element/Link.php | 16 +- src/PhpWord/Writer/HTML/Element/ListItem.php | 10 +- src/PhpWord/Writer/HTML/Element/Table.php | 34 ++-- src/PhpWord/Writer/HTML/Element/Text.php | 157 ++++++++++++++--- src/PhpWord/Writer/HTML/Element/TextBreak.php | 8 +- src/PhpWord/Writer/HTML/Element/TextRun.php | 32 +--- src/PhpWord/Writer/HTML/Element/Title.php | 10 +- .../Writer/HTML/Style/AbstractStyle.php | 30 +++- src/PhpWord/Writer/HTML/Style/Font.php | 45 ++--- src/PhpWord/Writer/HTML/Style/Generic.php | 1 + src/PhpWord/Writer/HTML/Style/Image.php | 14 +- src/PhpWord/Writer/HTML/Style/Paragraph.php | 21 ++- src/PhpWord/Writer/RTF.php | 71 ++++---- .../Writer/RTF/Element/AbstractElement.php | 27 +++ src/PhpWord/Writer/RTF/Element/Container.php | 52 ++++++ src/PhpWord/Writer/RTF/Element/Text.php | 163 +++++++++--------- src/PhpWord/Writer/RTF/Element/TextBreak.php | 2 +- src/PhpWord/Writer/RTF/Element/TextRun.php | 29 +--- src/PhpWord/Writer/RTF/Element/Title.php | 12 +- .../Writer/RTF/Style/AbstractStyle.php | 29 ++++ src/PhpWord/Writer/RTF/Style/Font.php | 102 +++++++++++ src/PhpWord/Writer/RTF/Style/Paragraph.php | 51 ++++++ .../Word2007/Element/AbstractElement.php | 2 +- .../Writer/Word2007/Element/Container.php | 4 +- 32 files changed, 745 insertions(+), 494 deletions(-) rename src/PhpWord/Writer/{RTF/Element/Element.php => HTML/Element/AbstractElement.php} (62%) create mode 100644 src/PhpWord/Writer/HTML/Element/Container.php delete mode 100644 src/PhpWord/Writer/HTML/Element/Element.php create mode 100644 src/PhpWord/Writer/RTF/Element/AbstractElement.php create mode 100644 src/PhpWord/Writer/RTF/Element/Container.php create mode 100644 src/PhpWord/Writer/RTF/Style/AbstractStyle.php create mode 100644 src/PhpWord/Writer/RTF/Style/Font.php create mode 100644 src/PhpWord/Writer/RTF/Style/Paragraph.php diff --git a/src/PhpWord/Reader/Word2007/Document.php b/src/PhpWord/Reader/Word2007/Document.php index 7f5635e3..d38b3fb9 100644 --- a/src/PhpWord/Reader/Word2007/Document.php +++ b/src/PhpWord/Reader/Word2007/Document.php @@ -133,8 +133,8 @@ class Document extends AbstractPart $headingMatches = array(); if ($xmlReader->elementExists('w:pPr', $domNode)) { $paragraphStyle = $this->readParagraphStyle($xmlReader, $domNode); - if (is_string($paragraphStyle)) { - preg_match('/Heading(\d)/', $paragraphStyle, $headingMatches); + if (is_array($paragraphStyle) && array_key_exists('styleName', $paragraphStyle)) { + preg_match('/Heading(\d)/', $paragraphStyle['styleName'], $headingMatches); } } diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index 2765aca4..79a12242 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -148,7 +148,6 @@ class Paragraph extends AbstractStyle public function setAlign($value = null) { if (strtolower($value) == 'justify') { - // justify becames both $value = 'both'; } $this->align = $value; diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index e28d7733..d292d9ab 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -23,7 +23,7 @@ use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style; -use PhpOffice\PhpWord\Writer\HTML\Element\Element as ElementWriter; +use PhpOffice\PhpWord\Writer\HTML\Element\Container; use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter; use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter; use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter; @@ -89,20 +89,20 @@ class HTML extends AbstractWriter implements WriterInterface */ public function writeDocument() { - $html = ''; - $html .= '' . PHP_EOL; - $html .= '' . PHP_EOL; - $html .= '' . PHP_EOL; - $html .= '' . PHP_EOL; - $html .= $this->writeHTMLHead(); - $html .= '' . PHP_EOL; - $html .= '' . PHP_EOL; - $html .= $this->writeHTMLBody(); - $html .= $this->writeNotes(); - $html .= '' . PHP_EOL; - $html .= '' . PHP_EOL; + $content = ''; + $content .= '' . PHP_EOL; + $content .= '' . PHP_EOL; + $content .= '' . PHP_EOL; + $content .= '' . PHP_EOL; + $content .= $this->writeHead(); + $content .= '' . PHP_EOL; + $content .= '' . PHP_EOL; + $content .= $this->writeBody(); + $content .= $this->writeNotes(); + $content .= '' . PHP_EOL; + $content .= '' . PHP_EOL; - return $html; + return $content; } /** @@ -110,7 +110,7 @@ class HTML extends AbstractWriter implements WriterInterface * * @return string */ - private function writeHTMLHead() + private function writeHead() { $properties = $this->getPhpWord()->getDocumentProperties(); $propertiesMapping = array( @@ -126,20 +126,20 @@ class HTML extends AbstractWriter implements WriterInterface $title = $properties->getTitle(); $title = ($title != '') ? $title : 'PHPWord'; - $html = ''; - $html .= '' . PHP_EOL; - $html .= '' . htmlspecialchars($title) . '' . PHP_EOL; + $content = ''; + $content .= '' . PHP_EOL; + $content .= '' . htmlspecialchars($title) . '' . PHP_EOL; foreach ($propertiesMapping as $key => $value) { $value = ($value == '') ? $key : $value; $method = "get" . $key; if ($properties->$method() != '') { - $html .= '' . PHP_EOL; } } - $html .= $this->writeStyles(); + $content .= $this->writeStyles(); - return $html; + return $content; } /** @@ -147,55 +147,22 @@ class HTML extends AbstractWriter implements WriterInterface * * @return string */ - private function writeHTMLBody() + private function writeBody() { $phpWord = $this->getPhpWord(); - $html = ''; + $content = ''; $sections = $phpWord->getSections(); $countSections = count($sections); if ($countSections > 0) { foreach ($sections as $section) { - $elements = $section->getElements(); - foreach ($elements as $element) { - if ($element instanceof AbstractElement) { - $elementWriter = new ElementWriter($this, $element, false); - $html .= $elementWriter->write(); - } - } + $writer = new Container($this, $section); + $content .= $writer->write(); } } - return $html; - } - - /** - * Write footnote/endnote contents as textruns - */ - private function writeNotes() - { - $phpWord = $this->getPhpWord(); - $html = ''; - - if (!empty($this->notes)) { - $html .= "
"; - foreach ($this->notes as $noteId => $noteMark) { - $noteAnchor = "note-{$noteId}"; - list($noteType, $noteTypeId) = explode('-', $noteMark); - $method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes'); - $collection = $phpWord->$method()->getItems(); - if (array_key_exists($noteTypeId, $collection)) { - $element = $collection[$noteTypeId]; - $elmWriter = new TextRunWriter($this, $element, true); - $content = "{$noteId}"; - $content .= $elmWriter->write(); - $html .= "

{$content}

" . PHP_EOL; - } - } - } - - return $html; + return $content; } /** @@ -253,6 +220,36 @@ class HTML extends AbstractWriter implements WriterInterface return $css; } + /** + * Write footnote/endnote contents as textruns + */ + private function writeNotes() + { + $phpWord = $this->getPhpWord(); + $content = PHP_EOL; + + if (!empty($this->notes)) { + $content .= "
" . PHP_EOL; + foreach ($this->notes as $noteId => $noteMark) { + list($noteType, $noteTypeId) = explode('-', $noteMark); + $method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes'); + $collection = $phpWord->$method()->getItems(); + + if (array_key_exists($noteTypeId, $collection)) { + $element = $collection[$noteTypeId]; + $noteAnchor = "
"; + $noteAnchor .= "{$noteId}"; + + $writer = new TextRunWriter($this, $element); + $writer->setOpeningText($noteAnchor); + $content .= $writer->write(); + } + } + } + + return $content; + } + /** * Get is PDF * diff --git a/src/PhpWord/Writer/RTF/Element/Element.php b/src/PhpWord/Writer/HTML/Element/AbstractElement.php similarity index 62% rename from src/PhpWord/Writer/RTF/Element/Element.php rename to src/PhpWord/Writer/HTML/Element/AbstractElement.php index 8464a377..8da09414 100644 --- a/src/PhpWord/Writer/RTF/Element/Element.php +++ b/src/PhpWord/Writer/HTML/Element/AbstractElement.php @@ -15,22 +15,22 @@ * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ -namespace PhpOffice\PhpWord\Writer\RTF\Element; +namespace PhpOffice\PhpWord\Writer\HTML\Element; -use PhpOffice\PhpWord\Element\AbstractElement; -use PhpOffice\PhpWord\Writer\RTF; +use PhpOffice\PhpWord\Element\AbstractElement as Element; +use PhpOffice\PhpWord\Writer\AbstractWriter; /** - * Generic element writer + * Abstract HTML element writer * - * @since 0.10.0 + * @since 0.11.0 */ -class Element +abstract class AbstractElement { /** * Parent writer * - * @var \PhpOffice\PhpWord\Writer\RTF + * @var \PhpOffice\PhpWord\Writer\AbstractWriter */ protected $parentWriter; @@ -53,7 +53,7 @@ class Element * * @param bool $withoutP */ - public function __construct(RTF $parentWriter, AbstractElement $element, $withoutP = false) + public function __construct(AbstractWriter $parentWriter, Element $element, $withoutP = false) { $this->parentWriter = $parentWriter; $this->element = $element; @@ -61,19 +61,12 @@ class Element } /** - * Write element + * Set without paragraph * - * @return string + * @param bool $value */ - public function write() + public function setWithoutP($value) { - $content = ''; - $writerClass = str_replace('\\Element\\', '\\Writer\\RTF\\Element\\', get_class($this->element)); - if (class_exists($writerClass)) { - $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP); - $content = $writer->write(); - } - - return $content; + $this->withoutP = $value; } } diff --git a/src/PhpWord/Writer/HTML/Element/Container.php b/src/PhpWord/Writer/HTML/Element/Container.php new file mode 100644 index 00000000..6249426a --- /dev/null +++ b/src/PhpWord/Writer/HTML/Element/Container.php @@ -0,0 +1,50 @@ +element; + $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); + $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; + $content = ''; + + $elements = $container->getElements(); + foreach ($elements as $element) { + $writerClass = str_replace('\\Element', '\\Writer\\HTML\\Element', get_class($element)); + if (class_exists($writerClass)) { + $writer = new $writerClass($this->parentWriter, $element, $withoutP); + $content .= $writer->write(); + } + } + + return $content; + } +} diff --git a/src/PhpWord/Writer/HTML/Element/Element.php b/src/PhpWord/Writer/HTML/Element/Element.php deleted file mode 100644 index 5098ba2e..00000000 --- a/src/PhpWord/Writer/HTML/Element/Element.php +++ /dev/null @@ -1,84 +0,0 @@ -parentWriter = $parentWriter; - $this->element = $element; - $this->withoutP = $withoutP; - } - - /** - * Write element - * - * @return string - */ - public function write() - { - $content = ''; - $writerClass = str_replace('\\Element\\', '\\Writer\\HTML\\Element\\', get_class($this->element)); - if (class_exists($writerClass)) { - $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP); - $content = $writer->write(); - } - - return $content; - } -} diff --git a/src/PhpWord/Writer/HTML/Element/Footnote.php b/src/PhpWord/Writer/HTML/Element/Footnote.php index f1570ed0..cd1baacb 100644 --- a/src/PhpWord/Writer/HTML/Element/Footnote.php +++ b/src/PhpWord/Writer/HTML/Element/Footnote.php @@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element; * * @since 0.10.0 */ -class Footnote extends Element +class Footnote extends AbstractElement { /** * Note type footnote|endnote @@ -32,21 +32,18 @@ class Footnote extends Element protected $noteType = 'footnote'; /** - * Write footnote/endnote marks + * Write footnote/endnote marks; The actual content is written in parent writer (HTML) * * @return string */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) { - return; - } - $noteId = count($this->parentWriter->getNotes()) + 1; $noteMark = $this->noteType . '-' . $this->element->getRelationId(); - $this->parentWriter->addNote($noteId, $noteMark); - $html = "{$noteId}"; + $content = "{$noteId}"; - return $html; + $this->parentWriter->addNote($noteId, $noteMark); + + return $content; } } diff --git a/src/PhpWord/Writer/HTML/Element/Image.php b/src/PhpWord/Writer/HTML/Element/Image.php index 93c10826..2ae87865 100644 --- a/src/PhpWord/Writer/HTML/Element/Image.php +++ b/src/PhpWord/Writer/HTML/Element/Image.php @@ -25,7 +25,7 @@ use PhpOffice\PhpWord\Writer\HTML\Style\Image as ImageStyleWriter; * * @since 0.10.0 */ -class Image extends Element +class Image extends Text { /** * Write image @@ -34,25 +34,20 @@ class Image extends Element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) { - return; - } - - $html = ''; + $content = ''; if (!$this->parentWriter->isPdf()) { $imageData = $this->getBase64ImageData($this->element); if (!is_null($imageData)) { $styleWriter = new ImageStyleWriter($this->element->getStyle()); $style = $styleWriter->write(); - $html = ""; - if (!$this->withoutP) { - $html = "

{$html}

" . PHP_EOL; - } + $content .= $this->writeOpening(); + $content .= ""; + $content .= $this->writeClosing(); } } - return $html; + return $content; } /** diff --git a/src/PhpWord/Writer/HTML/Element/Link.php b/src/PhpWord/Writer/HTML/Element/Link.php index 2792eb67..6f1977f8 100644 --- a/src/PhpWord/Writer/HTML/Element/Link.php +++ b/src/PhpWord/Writer/HTML/Element/Link.php @@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element; * * @since 0.10.0 */ -class Link extends Element +class Link extends Text { /** * Write link @@ -31,15 +31,11 @@ class Link extends Element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) { - return; - } + $content = ''; + $content .= $this->writeOpening(); + $content .= "element->getTarget()}\">{$this->element->getText()}"; + $content .= $this->writeClosing(); - $html = "element->getTarget()}\">{$this->element->getText()}" . PHP_EOL; - if (!$this->withoutP) { - $html = '

' . $html . '

' . PHP_EOL; - } - - return $html; + return $content; } } diff --git a/src/PhpWord/Writer/HTML/Element/ListItem.php b/src/PhpWord/Writer/HTML/Element/ListItem.php index 89f2ad69..722d920a 100644 --- a/src/PhpWord/Writer/HTML/Element/ListItem.php +++ b/src/PhpWord/Writer/HTML/Element/ListItem.php @@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element; * * @since 0.10.0 */ -class ListItem extends Element +class ListItem extends AbstractElement { /** * Write list item @@ -31,13 +31,9 @@ class ListItem extends Element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\ListItem) { - return; - } - $text = htmlspecialchars($this->element->getTextObject()->getText()); - $html = '

' . $text . '

' . PHP_EOL; + $content = '

' . $text . '

' . PHP_EOL; - return $html; + return $content; } } diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php index b3974554..a92deea6 100644 --- a/src/PhpWord/Writer/HTML/Element/Table.php +++ b/src/PhpWord/Writer/HTML/Element/Table.php @@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element; * * @since 0.10.0 */ -class Table extends Element +class Table extends AbstractElement { /** * Write table @@ -31,40 +31,28 @@ class Table extends Element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Table) { - return; - } - - $html = ''; + $content = ''; $rows = $this->element->getRows(); $rowCount = count($rows); if ($rowCount > 0) { - $html .= '' . PHP_EOL; + $content .= '
' . PHP_EOL; foreach ($rows as $row) { // $height = $row->getHeight(); $rowStyle = $row->getStyle(); $tblHeader = $rowStyle->getTblHeader(); - $html .= '' . PHP_EOL; + $content .= '' . PHP_EOL; foreach ($row->getCells() as $cell) { + $writer = new Container($this->parentWriter, $cell); $cellTag = $tblHeader ? 'th' : 'td'; - $cellContents = $cell->getElements(); - $html .= "<{$cellTag}>" . PHP_EOL; - if (count($cellContents) > 0) { - foreach ($cellContents as $content) { - $writer = new Element($this->parentWriter, $content, false); - $html .= $writer->write(); - } - } else { - $writer = new Element($this->parentWriter, new \PhpOffice\PhpWord\Element\TextBreak(), false); - $html .= $writer->write(); - } - $html .= '' . PHP_EOL; + $content .= "<{$cellTag}>" . PHP_EOL; + $content .= $writer->write(); + $content .= '' . PHP_EOL; } - $html .= '' . PHP_EOL; + $content .= '' . PHP_EOL; } - $html .= '
' . PHP_EOL; + $content .= '' . PHP_EOL; } - return $html; + return $content; } } diff --git a/src/PhpWord/Writer/HTML/Element/Text.php b/src/PhpWord/Writer/HTML/Element/Text.php index 96e0c25e..883a8cd3 100644 --- a/src/PhpWord/Writer/HTML/Element/Text.php +++ b/src/PhpWord/Writer/HTML/Element/Text.php @@ -27,8 +27,36 @@ use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter; * * @since 0.10.0 */ -class Text extends Element +class Text extends AbstractElement { + /** + * Text written after opening + * + * @var string + */ + private $openingText = ''; + + /** + * Text written before closing + * + * @var string + */ + private $closingText = ''; + + /** + * Opening tags + * + * @var string + */ + private $openingTags = ''; + + /** + * Closing tag + * + * @var strings + */ + private $closingTags = ''; + /** * Write text * @@ -36,42 +64,117 @@ class Text extends Element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) { - return; + $this->getFontStyle(); + + $content = ''; + $content .= $this->writeOpening(); + $content .= $this->openingTags; + $content .= htmlspecialchars($this->element->getText()); + $content .= $this->closingTags; + $content .= $this->closingText; + $content .= $this->writeClosing(); + + return $content; + } + + /** + * Set opening text + * + * @param string $value + */ + public function setOpeningText($value) + { + $this->openingText = $value; + } + + /** + * Set closing text + * + * @param string $value + */ + public function setClosingText($value) + { + $this->closingText = $value; + } + + /** + * Write opening + * + * @return string + */ + protected function writeOpening() + { + $content = ''; + if (!$this->withoutP) { + $style = ''; + if (method_exists($this->element, 'getParagraphStyle')) { + $style = $this->getParagraphStyle(); + } + $content .= ""; + $content .= $this->openingText; + } + + return $content; + } + + /** + * Write ending + * + * @return string + */ + protected function writeClosing() + { + $content = ''; + if (!$this->withoutP) { + $content .= $this->closingText; + $content .= "

" . PHP_EOL; + } + + return $content; + } + + /** + * Write paragraph style + * + * @return string + */ + private function getParagraphStyle() + { + $style = ''; + if (method_exists($this->element, 'getParagraphStyle')) { + return $style; } - // Paragraph style $paragraphStyle = $this->element->getParagraphStyle(); $pStyleIsObject = ($paragraphStyle instanceof Paragraph); if ($pStyleIsObject) { $styleWriter = new ParagraphStyleWriter($paragraphStyle); - $paragraphStyle = $styleWriter->write(); + $style = $styleWriter->write(); } - $hasParagraphStyle = $paragraphStyle && !$this->withoutP; - - // Font style - $fontStyle = $this->element->getFontStyle(); - $fontStyleIsObject = ($fontStyle instanceof Font); - if ($fontStyleIsObject) { - $styleWriter = new FontStyleWriter($fontStyle); - $fontStyle = $styleWriter->write(); - } - - $openingTags = ''; - $endingTags = ''; - if ($hasParagraphStyle) { + if ($style) { $attribute = $pStyleIsObject ? 'style' : 'class'; - $openingTags = "

"; - $endingTags = '

' . PHP_EOL; - } - if ($fontStyle) { - $attribute = $fontStyleIsObject ? 'style' : 'class'; - $openingTags = $openingTags . ""; - $endingTags = '' . $endingTags; + $style = " {$attribute}=\"{$style}\""; } - $html = $openingTags . htmlspecialchars($this->element->getText()) . $endingTags; + return $style; + } - return $html; + /** + * Get font style + */ + private function getFontStyle() + { + $style = ''; + $fontStyle = $this->element->getFontStyle(); + $fStyleIsObject = ($fontStyle instanceof Font); + if ($fStyleIsObject) { + $styleWriter = new FontStyleWriter($fontStyle); + $style = $styleWriter->write(); + } + if ($style) { + $attribute = $fStyleIsObject ? 'style' : 'class'; + $this->openingTags = ""; + $this->closingTags = ""; + } } } diff --git a/src/PhpWord/Writer/HTML/Element/TextBreak.php b/src/PhpWord/Writer/HTML/Element/TextBreak.php index 2e2c142a..30560e96 100644 --- a/src/PhpWord/Writer/HTML/Element/TextBreak.php +++ b/src/PhpWord/Writer/HTML/Element/TextBreak.php @@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element; * * @since 0.10.0 */ -class TextBreak extends Element +class TextBreak extends AbstractElement { /** * Write text break @@ -32,11 +32,11 @@ class TextBreak extends Element public function write() { if ($this->withoutP) { - $html = '
' . PHP_EOL; + $content = '
' . PHP_EOL; } else { - $html = '

 

' . PHP_EOL; + $content = '

 

' . PHP_EOL; } - return $html; + return $content; } } diff --git a/src/PhpWord/Writer/HTML/Element/TextRun.php b/src/PhpWord/Writer/HTML/Element/TextRun.php index 60415043..d8ea6d72 100644 --- a/src/PhpWord/Writer/HTML/Element/TextRun.php +++ b/src/PhpWord/Writer/HTML/Element/TextRun.php @@ -27,7 +27,7 @@ use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter; * * @since 0.10.0 */ -class TextRun extends Element +class TextRun extends Text { /** * Write text run @@ -36,31 +36,13 @@ class TextRun extends Element */ public function write() { - if (!($this->element instanceof TextRunElement || $this->element instanceof FootnoteElement)) { - return; - } + $content = ''; - $html = ''; - $elements = $this->element->getElements(); - if (count($elements) > 0) { - // Paragraph style - $paragraphStyle = $this->element->getParagraphStyle(); - $pStyleIsObject = ($paragraphStyle instanceof Paragraph); - if ($pStyleIsObject) { - $styleWriter = new ParagraphStyleWriter($paragraphStyle); - $paragraphStyle = $styleWriter->write(); - } - $tag = $this->withoutP ? 'span' : 'p'; - $attribute = $pStyleIsObject ? 'style' : 'class'; - $html .= "<{$tag} {$attribute}=\"{$paragraphStyle}\">"; - foreach ($elements as $element) { - $elementWriter = new Element($this->parentWriter, $element, true); - $html .= $elementWriter->write(); - } - $html .= ""; - $html .= PHP_EOL; - } + $content .= $this->writeOpening(); + $writer = new Container($this->parentWriter, $this->element); + $content .= $writer->write(); + $content .= $this->writeClosing(); - return $html; + return $content; } } diff --git a/src/PhpWord/Writer/HTML/Element/Title.php b/src/PhpWord/Writer/HTML/Element/Title.php index e2252241..2a33a282 100644 --- a/src/PhpWord/Writer/HTML/Element/Title.php +++ b/src/PhpWord/Writer/HTML/Element/Title.php @@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element; * * @since 0.10.0 */ -class Title extends Element +class Title extends AbstractElement { /** * Write heading @@ -31,14 +31,10 @@ class Title extends Element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) { - return; - } - $tag = 'h' . $this->element->getDepth(); $text = htmlspecialchars($this->element->getText()); - $html = "<{$tag}>{$text}" . PHP_EOL; + $content = "<{$tag}>{$text}" . PHP_EOL; - return $html; + return $content; } } diff --git a/src/PhpWord/Writer/HTML/Style/AbstractStyle.php b/src/PhpWord/Writer/HTML/Style/AbstractStyle.php index a747e0e3..d25e7b6a 100644 --- a/src/PhpWord/Writer/HTML/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/HTML/Style/AbstractStyle.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\HTML\Style; +use PhpOffice\PhpWord\Style\AbstractStyle as Style; + /** * Style writer * @@ -29,7 +31,7 @@ abstract class AbstractStyle * * @var array|\PhpOffice\PhpWord\Style\AbstractStyle */ - protected $style; + private $style; /** * Write style @@ -46,6 +48,20 @@ abstract class AbstractStyle $this->style = $style; } + /** + * Get style + * + * @return array|\PhpOffice\PhpWord\Style\AbstractStyle $style + */ + public function getStyle() + { + if (!$this->style instanceof Style && !is_array($this->style)) { + return; + } + + return $this->style; + } + /** * Takes array where of CSS properties / values and converts to CSS string * @@ -67,4 +83,16 @@ abstract class AbstractStyle return $string; } + + /** + * Get value if ... + * + * @param bool $condition + * @param string $value + * @return string + */ + protected function getValueIf($condition, $value) + { + return $condition ? $value : ''; + } } diff --git a/src/PhpWord/Writer/HTML/Style/Font.php b/src/PhpWord/Writer/HTML/Style/Font.php index 7ab4dc40..0c416e8f 100644 --- a/src/PhpWord/Writer/HTML/Style/Font.php +++ b/src/PhpWord/Writer/HTML/Style/Font.php @@ -34,48 +34,29 @@ class Font extends AbstractStyle */ public function write() { - if (!$this->style instanceof \PhpOffice\PhpWord\Style\Font) { - return; - } - - $font = $this->style->getName(); - $size = $this->style->getSize(); - $color = $this->style->getColor(); - $fgColor = $this->style->getFgColor(); - $underline = $this->style->getUnderline() != FontStyle::UNDERLINE_NONE; - $lineThrough = $this->style->isStrikethrough() || $this->style->isDoubleStrikethrough(); - + $style = $this->getStyle(); $css = array(); + $font = $style->getName(); + $size = $style->getSize(); + $color = $style->getColor(); + $fgColor = $style->getFgColor(); + $underline = $style->getUnderline() != FontStyle::UNDERLINE_NONE; + $lineThrough = $style->isStrikethrough() || $style->isDoubleStrikethrough(); + $css['font-family'] = $this->getValueIf($font != PhpWord::DEFAULT_FONT_NAME, "'{$font}'"); $css['font-size'] = $this->getValueIf($size != PhpWord::DEFAULT_FONT_SIZE, "{$size}pt"); $css['color'] = $this->getValueIf($color != PhpWord::DEFAULT_FONT_COLOR, "#{$color}"); $css['background'] = $this->getValueIf($fgColor != '', $fgColor); - $css['font-weight'] = $this->getValueIf($this->style->isBold(), 'bold'); - $css['font-style'] = $this->getValueIf($this->style->isItalic(), 'italic'); - + $css['font-weight'] = $this->getValueIf($style->isBold(), 'bold'); + $css['font-style'] = $this->getValueIf($style->isItalic(), 'italic'); + $css['vertical-align'] = $this->getValueIf($style->isSuperScript(), 'italic'); + $css['vertical-align'] = $this->getValueIf($style->isSuperScript(), 'super'); + $css['vertical-align'] = $this->getValueIf($style->isSubScript(), 'sub'); $css['text-decoration'] = ''; $css['text-decoration'] .= $this->getValueIf($underline, 'underline '); $css['text-decoration'] .= $this->getValueIf($lineThrough, 'line-through '); - if ($this->style->isSuperScript()) { - $css['vertical-align'] = 'super'; - } elseif ($this->style->isSubScript()) { - $css['vertical-align'] = 'sub'; - } - return $this->assembleCss($css); } - - /** - * Get value if ... - * - * @param bool $condition - * @param string $value - * @return string - */ - private function getValueIf($condition, $value) - { - return $condition ? $value : ''; - } } diff --git a/src/PhpWord/Writer/HTML/Style/Generic.php b/src/PhpWord/Writer/HTML/Style/Generic.php index 1009ce2d..9c9107b6 100644 --- a/src/PhpWord/Writer/HTML/Style/Generic.php +++ b/src/PhpWord/Writer/HTML/Style/Generic.php @@ -31,6 +31,7 @@ class Generic extends AbstractStyle */ public function write() { + $this->style = $this->getStyle(); $css = array(); if (is_array($this->style) && !empty($this->style)) { diff --git a/src/PhpWord/Writer/HTML/Style/Image.php b/src/PhpWord/Writer/HTML/Style/Image.php index ea8b3b3d..7e700aa4 100644 --- a/src/PhpWord/Writer/HTML/Style/Image.php +++ b/src/PhpWord/Writer/HTML/Style/Image.php @@ -31,17 +31,11 @@ class Image extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Image)) { - return; - } - + $this->style = $this->getStyle(); $css = array(); - if ($this->style->getWidth()) { - $css['width'] = $this->style->getWidth() . 'px'; - } - if ($this->style->getWidth()) { - $css['height'] = $this->style->getHeight() . 'px'; - } + + $css['width'] = $this->getValueIf($this->style->getWidth(), $this->style->getWidth() . 'px'); + $css['height'] = $this->getValueIf($this->style->getHeight(), $this->style->getHeight() . 'px'); return $this->assembleCss($css); } diff --git a/src/PhpWord/Writer/HTML/Style/Paragraph.php b/src/PhpWord/Writer/HTML/Style/Paragraph.php index 605533a4..27618061 100644 --- a/src/PhpWord/Writer/HTML/Style/Paragraph.php +++ b/src/PhpWord/Writer/HTML/Style/Paragraph.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\HTML\Style; +use PhpOffice\PhpWord\Settings; + /** * Paragraph style HTML writer * @@ -31,13 +33,20 @@ class Paragraph extends AbstractStyle */ public function write() { - if (!($this->style instanceof \PhpOffice\PhpWord\Style\Paragraph)) { - return; - } - + $style = $this->getStyle(); $css = array(); - if ($this->style->getAlign()) { - $css['text-align'] = $this->style->getAlign(); + + // Alignment + $align = $style->getAlign(); + $css['text-align'] = $this->getValueIf(!is_null($align), $align); + + // Spacing + $spacing = $style->getSpace(); + if (!is_null($spacing)) { + $before = $spacing->getBefore(); + $after = $spacing->getAfter(); + $css['margin-top'] = $this->getValueIf(!is_null($before), ($before / Settings::UNIT_POINT) . 'pt'); + $css['margin-bottom'] = $this->getValueIf(!is_null($after), ($after / Settings::UNIT_POINT) . 'pt'); } return $this->assembleCss($css); diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 829a6d81..434ffd67 100644 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -23,7 +23,7 @@ use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\Drawing; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; -use PhpOffice\PhpWord\Writer\RTF\Element\Element as ElementWriter; +use PhpOffice\PhpWord\Writer\RTF\Element\Container; /** * RTF writer @@ -131,43 +131,43 @@ class RTF extends AbstractWriter implements WriterInterface $this->colorTable = $this->populateColorTable(); // Set the default character set - $sRTFContent = '{\rtf1'; - $sRTFContent .= '\ansi\ansicpg1252'; // Set the default font (the first one) - $sRTFContent .= '\deff0'; // Set the default tab size (720 twips) - $sRTFContent .= '\deftab720'; - $sRTFContent .= PHP_EOL; + $content = '{\rtf1'; + $content .= '\ansi\ansicpg1252'; // Set the default font (the first one) + $content .= '\deff0'; // Set the default tab size (720 twips) + $content .= '\deftab720'; + $content .= PHP_EOL; // Set the font tbl group - $sRTFContent .= '{\fonttbl'; + $content .= '{\fonttbl'; foreach ($this->fontTable as $idx => $font) { - $sRTFContent .= '{\f' . $idx . '\fnil\fcharset0 ' . $font . ';}'; + $content .= '{\f' . $idx . '\fnil\fcharset0 ' . $font . ';}'; } - $sRTFContent .= '}' . PHP_EOL; + $content .= '}' . PHP_EOL; // Set the color tbl group - $sRTFContent .= '{\colortbl '; + $content .= '{\colortbl '; foreach ($this->colorTable as $idx => $color) { $arrColor = Drawing::htmlToRGB($color); - $sRTFContent .= ';\red' . $arrColor[0] . '\green' . $arrColor[1] . '\blue' . $arrColor[2] . ''; + $content .= ';\red' . $arrColor[0] . '\green' . $arrColor[1] . '\blue' . $arrColor[2] . ''; } - $sRTFContent .= ';}' . PHP_EOL; + $content .= ';}' . PHP_EOL; - $sRTFContent .= '{\*\generator PhpWord;}' . PHP_EOL; // Set the generator - $sRTFContent .= '\viewkind4'; // Set the view mode of the document - $sRTFContent .= '\uc1'; // Set the numberof bytes that follows a unicode character - $sRTFContent .= '\pard'; // Resets to default paragraph properties. - $sRTFContent .= '\nowidctlpar'; // No widow/orphan control - $sRTFContent .= '\lang1036'; // Applies a language to a text run (1036 : French (France)) - $sRTFContent .= '\kerning1'; // Point size (in half-points) above which to kern character pairs - $sRTFContent .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2); // Set the font size in half-points - $sRTFContent .= PHP_EOL; + $content .= '{\*\generator PhpWord;}' . PHP_EOL; // Set the generator + $content .= '\viewkind4'; // Set the view mode of the document + $content .= '\uc1'; // Set the numberof bytes that follows a unicode character + $content .= '\pard'; // Resets to default paragraph properties. + $content .= '\nowidctlpar'; // No widow/orphan control + $content .= '\lang1036'; // Applies a language to a text run (1036 : French (France)) + $content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs + $content .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2); // Set the font size in half-points + $content .= PHP_EOL; // Body - $sRTFContent .= $this->writeContent(); + $content .= $this->writeContent(); - $sRTFContent .= '}'; + $content .= '}'; - return $sRTFContent; + return $content; } /** @@ -177,24 +177,15 @@ class RTF extends AbstractWriter implements WriterInterface */ private function writeContent() { - $phpWord = $this->phpWord; - $sRTFBody = ''; + $content = ''; - $sections = $phpWord->getSections(); - $countSections = count($sections); - $pSection = 0; - - if ($countSections > 0) { - foreach ($sections as $section) { - $pSection++; - $elements = $section->getElements(); - foreach ($elements as $element) { - $elementWriter = new ElementWriter($this, $element); - $sRTFBody .= $elementWriter->write(); - } - } + $sections = $this->getPhpWord()->getSections(); + foreach ($sections as $section) { + $writer = new Container($this, $section); + $content .= $writer->write(); } - return $sRTFBody; + + return $content; } /** diff --git a/src/PhpWord/Writer/RTF/Element/AbstractElement.php b/src/PhpWord/Writer/RTF/Element/AbstractElement.php new file mode 100644 index 00000000..fbf8114e --- /dev/null +++ b/src/PhpWord/Writer/RTF/Element/AbstractElement.php @@ -0,0 +1,27 @@ +element; + $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); + $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; + $content = ''; + + $elements = $container->getElements(); + foreach ($elements as $element) { + $writerClass = str_replace('\\Element', '\\Writer\\RTF\\Element', get_class($element)); + if (class_exists($writerClass)) { + $writer = new $writerClass($this->parentWriter, $element, $withoutP); + $content .= '{'; + $content .= $writer->write(); + $content .= '}' . PHP_EOL; + } + } + + return $content; + } +} diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php index f4225ead..ae7466d8 100644 --- a/src/PhpWord/Writer/RTF/Element/Text.php +++ b/src/PhpWord/Writer/RTF/Element/Text.php @@ -17,42 +17,62 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; -use PhpOffice\PhpWord\PhpWord; 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; /** * Text element RTF writer * * @since 0.10.0 */ -class Text extends Element +class Text extends AbstractElement { /** * Write element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) { - return; + $fontStyle = $this->getFontStyle(); + + $content = ''; + $content .= $this->writeParagraphStyle(); + $content .= $this->writeFontStyleBegin($fontStyle); + if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) { + $content .= ' '; + } + $content .= $this->element->getText(); + $content .= $this->writeFontStyleEnd($fontStyle); + + if (!$this->withoutP) { + $content .= '\par' . PHP_EOL; } - $rtfText = ''; + return $content; + } - $fontStyle = $this->element->getFontStyle(); - if (is_string($fontStyle)) { - $fontStyle = Style::getStyle($fontStyle); - } + /** + * Write paragraph style + * + * @return string + */ + private function writeParagraphStyle() + { + $content = ''; + // Get paragraph style $paragraphStyle = $this->element->getParagraphStyle(); if (is_string($paragraphStyle)) { $paragraphStyle = Style::getStyle($paragraphStyle); } + // Write style when applicable if ($paragraphStyle && !$this->withoutP) { if ($this->parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) { - $rtfText .= $this->writeParagraphStyle($paragraphStyle); + $styleWriter = new ParagraphStyleWriter($paragraphStyle); + $content = $styleWriter->write(); $this->parentWriter->setLastParagraphStyle($this->element->getParagraphStyle()); } else { $this->parentWriter->setLastParagraphStyle(); @@ -61,101 +81,72 @@ class Text extends Element $this->parentWriter->setLastParagraphStyle(); } - if ($fontStyle instanceof FontStyle) { - $rtfText .= $this->writeFontStyleBegin($fontStyle); - } - if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) { - $rtfText .= ' '; - } - $rtfText .= $this->element->getText(); - if ($fontStyle instanceof FontStyle) { - $rtfText .= $this->writeFontStyleEnd($fontStyle); - } - if (!$this->withoutP) { - $rtfText .= '\par' . PHP_EOL; - } - - return $rtfText; - } - - /** - * Write paragraph style - * - * @return string - */ - private function writeParagraphStyle(ParagraphStyle $paragraphStyle) - { - $rtfText = '\pard\nowidctlpar'; - if ($paragraphStyle->getSpaceAfter() != null) { - $rtfText .= '\sa' . $paragraphStyle->getSpaceAfter(); - } - if ($paragraphStyle->getAlign() != null) { - if ($paragraphStyle->getAlign() == 'center') { - $rtfText .= '\qc'; - } - } - - return $rtfText; + return $content; } /** * Write font style beginning * + * @param \PhpOffice\PhpWord\Style\Font $style * @return string */ - private function writeFontStyleBegin(FontStyle $style) + private function writeFontStyleBegin($style) { - $rtfText = ''; - if ($style->getColor() != null) { - $idxColor = array_search($style->getColor(), $this->parentWriter->getColorTable()); - if ($idxColor !== false) { - $rtfText .= '\cf' . ($idxColor + 1); - } - } else { - $rtfText .= '\cf0'; - } - if ($style->getName() != null) { - $idxFont = array_search($style->getName(), $this->parentWriter->getFontTable()); - if ($idxFont !== false) { - $rtfText .= '\f' . $idxFont; - } - } else { - $rtfText .= '\f0'; - } - if ($style->isBold()) { - $rtfText .= '\b'; - } - if ($style->isItalic()) { - $rtfText .= '\i'; - } - if ($style->getSize()) { - $rtfText .= '\fs' . ($style->getSize() * 2); + if (!$style instanceof FontStyle) { + return ''; } - return $rtfText; + // Create style writer and set color/name index + $styleWriter = new FontStyleWriter($style); + if ($style->getColor() != null) { + $colorIndex = array_search($style->getColor(), $this->parentWriter->getColorTable()); + if ($colorIndex !== false) { + $styleWriter->setColorIndex($colorIndex + 1); + } + } + if ($style->getName() != null) { + $fontIndex = array_search($style->getName(), $this->parentWriter->getFontTable()); + if ($fontIndex !== false) { + $styleWriter->setNameIndex($fontIndex + 1); + } + } + + // Write style + $content = $styleWriter->write(); + + return $content; } /** * Write font style ending * + * @param \PhpOffice\PhpWord\Style\Font $style * @return string */ - private function writeFontStyleEnd(FontStyle $style) + private function writeFontStyleEnd($style) { - $rtfText = ''; - $rtfText .= '\cf0'; - $rtfText .= '\f0'; - - if ($style->isBold()) { - $rtfText .= '\b0'; - } - if ($style->isItalic()) { - $rtfText .= '\i0'; - } - if ($style->getSize()) { - $rtfText .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2); + if (!$style instanceof FontStyle) { + return ''; } - return $rtfText; + $styleWriter = new FontStyleWriter($style); + $content = $styleWriter->writeEnd(); + + return $content; + } + + /** + * Get font style + * + * @return \PhpOffice\PhpWord\Style\Font + */ + private function getFontStyle() + { + $fontStyle = $this->element->getFontStyle(); + if (is_string($fontStyle)) { + $fontStyle = Style::getStyle($fontStyle); + } + + return $fontStyle; } } diff --git a/src/PhpWord/Writer/RTF/Element/TextBreak.php b/src/PhpWord/Writer/RTF/Element/TextBreak.php index 2762503c..760f34e8 100644 --- a/src/PhpWord/Writer/RTF/Element/TextBreak.php +++ b/src/PhpWord/Writer/RTF/Element/TextBreak.php @@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; * * @since 0.10.0 */ -class TextBreak extends Element +class TextBreak extends AbstractElement { /** * Write element diff --git a/src/PhpWord/Writer/RTF/Element/TextRun.php b/src/PhpWord/Writer/RTF/Element/TextRun.php index f5df5d96..174aebd9 100644 --- a/src/PhpWord/Writer/RTF/Element/TextRun.php +++ b/src/PhpWord/Writer/RTF/Element/TextRun.php @@ -17,14 +17,14 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; -use PhpOffice\PhpWord\Element\Text as TextElement; +use PhpOffice\PhpWord\Writer\RTF\Element\Container; /** * TextRun element RTF writer * * @since 0.10.0 */ -class TextRun extends Element +class TextRun extends AbstractElement { /** * Write element @@ -33,26 +33,13 @@ class TextRun extends Element */ public function write() { - if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) { - return; - } + $content = ''; - $rtfText = ''; + $content .= '\pard\nowidctlpar' . PHP_EOL; + $writer = new Container($this->parentWriter, $this->element); + $content .= $writer->write(); + $content .= '\par' . PHP_EOL; - $elements = $this->element->getElements(); - if (count($elements) > 0) { - $rtfText .= '\pard\nowidctlpar' . PHP_EOL; - foreach ($elements as $element) { - if ($element instanceof TextElement) { - $elementWriter = new Element($this->parentWriter, $element, true); - $rtfText .= '{'; - $rtfText .= $elementWriter->write(); - $rtfText .= '}' . PHP_EOL; - } - } - $rtfText .= '\par' . PHP_EOL; - } - - return $rtfText; + return $content; } } diff --git a/src/PhpWord/Writer/RTF/Element/Title.php b/src/PhpWord/Writer/RTF/Element/Title.php index 5915c42e..d78bb6f4 100644 --- a/src/PhpWord/Writer/RTF/Element/Title.php +++ b/src/PhpWord/Writer/RTF/Element/Title.php @@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; * * @since 0.10.0 */ -class Title extends Element +class Title extends AbstractElement { /** * Write element @@ -35,12 +35,12 @@ class Title extends Element return; } - $rtfText = ''; + $content = ''; - $rtfText .= '\pard\nowidctlpar' . PHP_EOL; - $rtfText .= $this->element->getText(); - $rtfText .= '\par' . PHP_EOL; + $content .= '\pard\nowidctlpar' . PHP_EOL; + $content .= $this->element->getText(); + $content .= '\par' . PHP_EOL; - return $rtfText; + return $content; } } diff --git a/src/PhpWord/Writer/RTF/Style/AbstractStyle.php b/src/PhpWord/Writer/RTF/Style/AbstractStyle.php new file mode 100644 index 00000000..55d6588e --- /dev/null +++ b/src/PhpWord/Writer/RTF/Style/AbstractStyle.php @@ -0,0 +1,29 @@ +getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Font) { + return; + } + + $content = ''; + $content .= '\cf' . $this->colorIndex; + $content .= '\f' . $this->nameIndex; + $content .= $this->getValueIf($style->isBold(), '\b'); + $content .= $this->getValueIf($style->isItalic(), '\i'); + $content .= $this->getValueIf($style->getSize(), '\fs' . ($style->getSize() * 2)); + + return $content; + } + + /** + * Write end style + * + * @return string + */ + public function writeEnd() + { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Font) { + return; + } + + $content = ''; + $content .= '\cf0'; + $content .= '\f0'; + $content .= $this->getValueIf($style->isBold(), '\b0'); + $content .= $this->getValueIf($style->isItalic(), '\i0'); + $content .= $this->getValueIf($style->getSize(), '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2)); + + return $content; + } + + /** + * Set font name index + * + * @param int $value + */ + public function setNameIndex($value = 0) + { + $this->nameIndex = $value; + } + + /** + * Set font color index + * + * @param int $value + */ + public function setColorIndex($value = 0) + { + $this->colorIndex = $value; + } +} diff --git a/src/PhpWord/Writer/RTF/Style/Paragraph.php b/src/PhpWord/Writer/RTF/Style/Paragraph.php new file mode 100644 index 00000000..512e5774 --- /dev/null +++ b/src/PhpWord/Writer/RTF/Style/Paragraph.php @@ -0,0 +1,51 @@ +getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) { + return; + } + + $content = '\pard\nowidctlpar'; + + // Alignment + $align = $style->getAlign(); + $content .= $this->getValueIf(!is_null($align) && $align == 'center', '\qc'); + + // Spacing + $spaceAfter = $style->getSpaceAfter(); + $content .= $this->getValueIf(!is_null($spaceAfter), '\sa' . $spaceAfter); + + return $content; + } +} diff --git a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php index 65b3c400..4b38d01f 100644 --- a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -24,7 +24,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; /** * Abstract element writer * - * @since 0.10.0 + * @since 0.11.0 */ abstract class AbstractElement { diff --git a/src/PhpWord/Writer/Word2007/Element/Container.php b/src/PhpWord/Writer/Word2007/Element/Container.php index 0561509d..384fbd5b 100644 --- a/src/PhpWord/Writer/Word2007/Element/Container.php +++ b/src/PhpWord/Writer/Word2007/Element/Container.php @@ -40,11 +40,11 @@ class Container extends AbstractElement { $xmlWriter = $this->getXmlWriter(); $container = $this->getElement(); + $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); + $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; // Loop through subelements - $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); $subelements = $container->getElements(); - $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; if (count($subelements) > 0) { foreach ($subelements as $subelement) { $writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, get_class($subelement));