Refactor HTML & PDF writer with composite pattern
This commit is contained in:
parent
8c9c0402d2
commit
0b13b22e07
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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 .= '<!DOCTYPE html>' . PHP_EOL;
|
||||
$html .= '<!-- Generated by PHPWord -->' . PHP_EOL;
|
||||
$html .= '<html>' . PHP_EOL;
|
||||
$html .= '<head>' . PHP_EOL;
|
||||
$html .= $this->writeHTMLHead();
|
||||
$html .= '</head>' . PHP_EOL;
|
||||
$html .= '<body>' . PHP_EOL;
|
||||
$html .= $this->writeHTMLBody();
|
||||
$html .= $this->writeNotes();
|
||||
$html .= '</body>' . PHP_EOL;
|
||||
$html .= '</html>' . PHP_EOL;
|
||||
$content = '';
|
||||
$content .= '<!DOCTYPE html>' . PHP_EOL;
|
||||
$content .= '<!-- Generated by PHPWord -->' . PHP_EOL;
|
||||
$content .= '<html>' . PHP_EOL;
|
||||
$content .= '<head>' . PHP_EOL;
|
||||
$content .= $this->writeHead();
|
||||
$content .= '</head>' . PHP_EOL;
|
||||
$content .= '<body>' . PHP_EOL;
|
||||
$content .= $this->writeBody();
|
||||
$content .= $this->writeNotes();
|
||||
$content .= '</body>' . PHP_EOL;
|
||||
$content .= '</html>' . 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 .= '<meta charset="UTF-8" />' . PHP_EOL;
|
||||
$html .= '<title>' . htmlspecialchars($title) . '</title>' . PHP_EOL;
|
||||
$content = '';
|
||||
$content .= '<meta charset="UTF-8" />' . PHP_EOL;
|
||||
$content .= '<title>' . htmlspecialchars($title) . '</title>' . PHP_EOL;
|
||||
foreach ($propertiesMapping as $key => $value) {
|
||||
$value = ($value == '') ? $key : $value;
|
||||
$method = "get" . $key;
|
||||
if ($properties->$method() != '') {
|
||||
$html .= '<meta name="' . $value . '" content="' .
|
||||
$content .= '<meta name="' . $value . '" content="' .
|
||||
htmlspecialchars($properties->$method()) . '" />' . 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 .= "<hr />";
|
||||
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 = "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
|
||||
$content .= $elmWriter->write();
|
||||
$html .= "<p><a name=\"{$noteAnchor}\" />{$content}</p>" . 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 .= "<hr />" . 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 = "<a name=\"note-{$noteId}\" />";
|
||||
$noteAnchor .= "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
|
||||
|
||||
$writer = new TextRunWriter($this, $element);
|
||||
$writer->setOpeningText($noteAnchor);
|
||||
$content .= $writer->write();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get is PDF
|
||||
*
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
50
src/PhpWord/Writer/HTML/Element/Container.php
Normal file
50
src/PhpWord/Writer/HTML/Element/Container.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?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\HTML\Element;
|
||||
|
||||
/**
|
||||
* Container element HTML writer
|
||||
*
|
||||
* @since 0.11.0
|
||||
*/
|
||||
class Container extends AbstractElement
|
||||
{
|
||||
/**
|
||||
* Write container
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$container = $this->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;
|
||||
}
|
||||
}
|
||||
@ -1,84 +0,0 @@
|
||||
<?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\HTML\Element;
|
||||
|
||||
use PhpOffice\PhpWord\Element\AbstractElement;
|
||||
use PhpOffice\PhpWord\Writer\HTML;
|
||||
|
||||
/**
|
||||
* Generic element HTML writer
|
||||
*
|
||||
* Section: Text, TextRun, Link, Title, PreserveText, TextBreak, PageBreak, Table, ListItem, Image,
|
||||
* Object, Endnote, Footnote
|
||||
* Cell: Text, TextRun, Link, PreserveText, TextBreak, ListItem, Image, Object, Endnote, Footnote
|
||||
* TextRun: Text, Link, TextBreak, Image, Endnote, Footnote
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Element
|
||||
{
|
||||
/**
|
||||
* Parent writer
|
||||
*
|
||||
* @var \PhpOffice\PhpWord\Writer\HTML
|
||||
*/
|
||||
protected $parentWriter;
|
||||
|
||||
/**
|
||||
* Element
|
||||
*
|
||||
* @var \PhpOffice\PhpWord\Element\AbstractElement
|
||||
*/
|
||||
protected $element;
|
||||
|
||||
/**
|
||||
* Without paragraph
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $withoutP = false;
|
||||
|
||||
/**
|
||||
* Create new instance
|
||||
*
|
||||
* @param bool $withoutP
|
||||
*/
|
||||
public function __construct(HTML $parentWriter, AbstractElement $element, $withoutP = false)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
@ -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 = "<a name=\"{$noteMark}\"><a href=\"#note-{$noteId}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
|
||||
$content = "<a name=\"{$noteMark}\"><a href=\"#note-{$noteId}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
|
||||
|
||||
return $html;
|
||||
$this->parentWriter->addNote($noteId, $noteMark);
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 = "<img border=\"0\" style=\"{$style}\" src=\"{$imageData}\"/>";
|
||||
if (!$this->withoutP) {
|
||||
$html = "<p>{$html}</p>" . PHP_EOL;
|
||||
}
|
||||
$content .= $this->writeOpening();
|
||||
$content .= "<img border=\"0\" style=\"{$style}\" src=\"{$imageData}\"/>";
|
||||
$content .= $this->writeClosing();
|
||||
}
|
||||
}
|
||||
|
||||
return $html;
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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 .= "<a href=\"{$this->element->getTarget()}\">{$this->element->getText()}</a>";
|
||||
$content .= $this->writeClosing();
|
||||
|
||||
$html = "<a href=\"{$this->element->getTarget()}\">{$this->element->getText()}</a>" . PHP_EOL;
|
||||
if (!$this->withoutP) {
|
||||
$html = '<p>' . $html . '</p>' . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 = '<p>' . $text . '</p>' . PHP_EOL;
|
||||
$content = '<p>' . $text . '</p>' . PHP_EOL;
|
||||
|
||||
return $html;
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 .= '<table>' . PHP_EOL;
|
||||
$content .= '<table>' . PHP_EOL;
|
||||
foreach ($rows as $row) {
|
||||
// $height = $row->getHeight();
|
||||
$rowStyle = $row->getStyle();
|
||||
$tblHeader = $rowStyle->getTblHeader();
|
||||
$html .= '<tr>' . PHP_EOL;
|
||||
$content .= '<tr>' . 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 .= '</td>' . PHP_EOL;
|
||||
$content .= "<{$cellTag}>" . PHP_EOL;
|
||||
$content .= $writer->write();
|
||||
$content .= '</td>' . PHP_EOL;
|
||||
}
|
||||
$html .= '</tr>' . PHP_EOL;
|
||||
$content .= '</tr>' . PHP_EOL;
|
||||
}
|
||||
$html .= '</table>' . PHP_EOL;
|
||||
$content .= '</table>' . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 .= "<p{$style}>";
|
||||
$content .= $this->openingText;
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write ending
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function writeClosing()
|
||||
{
|
||||
$content = '';
|
||||
if (!$this->withoutP) {
|
||||
$content .= $this->closingText;
|
||||
$content .= "</p>" . 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 = "<p {$attribute}=\"{$paragraphStyle}\">";
|
||||
$endingTags = '</p>' . PHP_EOL;
|
||||
}
|
||||
if ($fontStyle) {
|
||||
$attribute = $fontStyleIsObject ? 'style' : 'class';
|
||||
$openingTags = $openingTags . "<span {$attribute}=\"{$fontStyle}\">";
|
||||
$endingTags = '</span>' . $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 = "<span {$attribute}=\"{$style}\">";
|
||||
$this->closingTags = "</span>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 = '<br />' . PHP_EOL;
|
||||
$content = '<br />' . PHP_EOL;
|
||||
} else {
|
||||
$html = '<p> </p>' . PHP_EOL;
|
||||
$content = '<p> </p>' . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 .= "</{$tag}>";
|
||||
$html .= PHP_EOL;
|
||||
}
|
||||
$content .= $this->writeOpening();
|
||||
$writer = new Container($this->parentWriter, $this->element);
|
||||
$content .= $writer->write();
|
||||
$content .= $this->writeClosing();
|
||||
|
||||
return $html;
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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}</{$tag}>" . PHP_EOL;
|
||||
$content = "<{$tag}>{$text}</{$tag}>" . PHP_EOL;
|
||||
|
||||
return $html;
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 : '';
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 : '';
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)) {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
27
src/PhpWord/Writer/RTF/Element/AbstractElement.php
Normal file
27
src/PhpWord/Writer/RTF/Element/AbstractElement.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Abstract RTF element writer
|
||||
*
|
||||
* @since 0.11.0
|
||||
*/
|
||||
class AbstractElement extends \PhpOffice\PhpWord\Writer\HTML\Element\AbstractElement
|
||||
{
|
||||
}
|
||||
52
src/PhpWord/Writer/RTF/Element/Container.php
Normal file
52
src/PhpWord/Writer/RTF/Element/Container.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Container element RTF writer
|
||||
*
|
||||
* @since 0.11.0
|
||||
*/
|
||||
class Container extends \PhpOffice\PhpWord\Writer\HTML\Element\Container
|
||||
{
|
||||
/**
|
||||
* Write container
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$container = $this->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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class TextBreak extends Element
|
||||
class TextBreak extends AbstractElement
|
||||
{
|
||||
/**
|
||||
* Write element
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
29
src/PhpWord/Writer/RTF/Style/AbstractStyle.php
Normal file
29
src/PhpWord/Writer/RTF/Style/AbstractStyle.php
Normal file
@ -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\Style;
|
||||
|
||||
use PhpOffice\PhpWord\Style\AbstractStyle as Style;
|
||||
|
||||
/**
|
||||
* Abstract RTF style writer
|
||||
*
|
||||
* @since 0.11.0
|
||||
*/
|
||||
abstract class AbstractStyle extends \PhpOffice\PhpWord\Writer\HTML\Style\AbstractStyle
|
||||
{
|
||||
}
|
||||
102
src/PhpWord/Writer/RTF/Style/Font.php
Normal file
102
src/PhpWord/Writer/RTF/Style/Font.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?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\Style;
|
||||
|
||||
use PhpOffice\PhpWord\PhpWord;
|
||||
|
||||
/**
|
||||
* RTF font style writer
|
||||
*
|
||||
* @since 0.11.0
|
||||
*/
|
||||
class Font extends AbstractStyle
|
||||
{
|
||||
/**
|
||||
* @var int Font name index
|
||||
*/
|
||||
private $nameIndex = 0;
|
||||
|
||||
/**
|
||||
* @var int Font color index
|
||||
*/
|
||||
private $colorIndex = 0;
|
||||
|
||||
/**
|
||||
* Write style
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$style = $this->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;
|
||||
}
|
||||
}
|
||||
51
src/PhpWord/Writer/RTF/Style/Paragraph.php
Normal file
51
src/PhpWord/Writer/RTF/Style/Paragraph.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?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\Style;
|
||||
|
||||
/**
|
||||
* RTF paragraph style writer
|
||||
*
|
||||
* @since 0.11.0
|
||||
*/
|
||||
class Paragraph extends AbstractStyle
|
||||
{
|
||||
/**
|
||||
* Write style
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$style = $this->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;
|
||||
}
|
||||
}
|
||||
@ -24,7 +24,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter;
|
||||
/**
|
||||
* Abstract element writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
* @since 0.11.0
|
||||
*/
|
||||
abstract class AbstractElement
|
||||
{
|
||||
|
||||
@ -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));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user