Add style class checker and rename $fStyle/$pStyle variables

This commit is contained in:
Ivan Lanin 2014-05-01 14:37:58 +07:00
parent 4e5bbb98b1
commit 9b1f2f210c
30 changed files with 223 additions and 213 deletions

View File

@ -76,10 +76,10 @@ This release marked heavy refactorings on internal code structure with the creat
- General: Rename `Footnote` to `Footnotes` to reflect the nature of collection - @ivanlanin
- General: Add some unit tests for Shared & Element (100%!) - @Progi1984
- Test: Add some samples and tests for image wrapping style - @brunocasado GH-59
- Refactor: Remove Style\Tabs
- Refactor: Apply composite pattern for writers
- Refactor: Split `AbstractContainer` from `AbstractElement`
- Refactor: Apply composite pattern for Word2007 reader
- Refactor: Remove Style\Tabs - @ivanlanin
- Refactor: Apply composite pattern for writers - @ivanlanin
- Refactor: Split `AbstractContainer` from `AbstractElement` - @ivanlanin
- Refactor: Apply composite pattern for Word2007 reader - @ivanlanin
## 0.9.1 - 27 Mar 2014

View File

@ -165,7 +165,7 @@ class DocumentProperties
*/
public function setLastModifiedBy($value = '')
{
$this->lastModifiedBy = $this->setValue($value, '');
$this->lastModifiedBy = $this->setValue($value, $this->creator);
return $this;
}

View File

@ -43,14 +43,14 @@ class PreserveText extends AbstractElement
* Create a new Preserve Text Element
*
* @param string $text
* @param mixed $styleFont
* @param mixed $styleParagraph
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return $this
*/
public function __construct($text = null, $styleFont = null, $styleParagraph = null)
public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
{
$this->fontStyle = $this->setStyle(new Font('text'), $styleFont);
$this->paragraphStyle = $this->setStyle(new Paragraph(), $styleParagraph);
$this->fontStyle = $this->setStyle(new Font('text'), $fontStyle);
$this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle);
$matches = preg_split('/({.*?})/', $text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
if (isset($matches[0])) {

View File

@ -93,15 +93,15 @@ class Section extends AbstractContainer
/**
* Add a Table-of-Contents Element
*
* @param mixed $styleFont
* @param mixed $styleTOC
* @param mixed $fontStyle
* @param mixed $tocStyle
* @param integer $minDepth
* @param integer $maxDepth
* @return \PhpOffice\PhpWord\Element\TOC
*/
public function addTOC($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
public function addTOC($fontStyle = null, $tocStyle = null, $minDepth = 1, $maxDepth = 9)
{
$toc = new TOC($styleFont, $styleTOC, $minDepth, $maxDepth);
$toc = new TOC($fontStyle, $tocStyle, $minDepth, $maxDepth);
$this->elements[] = $toc;
return $toc;
}

View File

@ -50,29 +50,29 @@ class TOC extends AbstractElement
/**
* Create a new Table-of-Contents Element
*
* @param mixed $styleFont
* @param array $styleTOC
* @param mixed $fontStyle
* @param array $tocStyle
* @param integer $minDepth
* @param integer $maxDepth
*/
public function __construct($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
public function __construct($fontStyle = null, $tocStyle = null, $minDepth = 1, $maxDepth = 9)
{
$this->TOCStyle = new TOCStyle();
if (!is_null($styleTOC) && is_array($styleTOC)) {
foreach ($styleTOC as $key => $value) {
if (!is_null($tocStyle) && is_array($tocStyle)) {
foreach ($tocStyle as $key => $value) {
$this->TOCStyle->setStyleValue($key, $value);
}
}
if (!is_null($styleFont)) {
if (is_array($styleFont)) {
if (!is_null($fontStyle)) {
if (is_array($fontStyle)) {
$this->fontStyle = new Font();
foreach ($styleFont as $key => $value) {
foreach ($fontStyle as $key => $value) {
$this->fontStyle->setStyleValue($key, $value);
}
} else {
$this->fontStyle = $styleFont;
$this->fontStyle = $fontStyle;
}
}

View File

@ -171,12 +171,12 @@ class PhpWord
* Adds a font style definition to styles.xml
*
* @param string $styleName
* @param mixed $styleFont
* @param mixed $styleParagraph
* @param mixed $fontStyle
* @param mixed $paragraphStyle
*/
public function addFontStyle($styleName, $styleFont, $styleParagraph = null)
public function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
{
Style::addFontStyle($styleName, $styleFont, $styleParagraph);
Style::addFontStyle($styleName, $fontStyle, $paragraphStyle);
}
/**
@ -195,12 +195,12 @@ class PhpWord
* Adds a heading style definition to styles.xml
*
* @param int $titleCount
* @param mixed $styleFont
* @param mixed $styleParagraph
* @param mixed $fontStyle
* @param mixed $paragraphStyle
*/
public function addTitleStyle($titleCount, $styleFont, $styleParagraph = null)
public function addTitleStyle($titleCount, $fontStyle, $paragraphStyle = null)
{
Style::addTitleStyle($titleCount, $styleFont, $styleParagraph);
Style::addTitleStyle($titleCount, $fontStyle, $paragraphStyle);
}
/**

View File

@ -21,9 +21,9 @@ abstract class AbstractPart extends \PhpOffice\PhpWord\Reader\Word2007\AbstractP
*
* @param mixed $parent
* @param string $docPart
* @param mixed $pStyle
* @param mixed $paragraphStyle
*/
protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart, $pStyle = null)
protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart, $paragraphStyle = null)
{
}

View File

@ -72,16 +72,16 @@ abstract class AbstractPart
* @param \DOMElement $domNode
* @param mixed $parent
* @param string $docPart
* @param mixed $pStyle
* @param mixed $paragraphStyle
*
* @todo Footnote paragraph style
*/
protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart, $pStyle = null)
protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart, $paragraphStyle = null)
{
if (!in_array($domNode->nodeName, array('w:r', 'w:hyperlink'))) {
return;
}
$fStyle = $this->readFontStyle($xmlReader, $domNode);
$fontStyle = $this->readFontStyle($xmlReader, $domNode);
// Link
if ($domNode->nodeName == 'w:hyperlink') {
@ -89,7 +89,7 @@ abstract class AbstractPart
$textContent = $xmlReader->getValue('w:r/w:t', $domNode);
$target = $this->getMediaTarget($docPart, $rId);
if (!is_null($target)) {
$parent->addLink($target, $textContent, $fStyle, $pStyle);
$parent->addLink($target, $textContent, $fontStyle, $paragraphStyle);
}
} else {
// Footnote
@ -116,13 +116,13 @@ abstract class AbstractPart
$target = $this->getMediaTarget($docPart, $rId);
if (!is_null($target)) {
$textContent = "<Object: {$target}>";
$parent->addText($textContent, $fStyle, $pStyle);
$parent->addText($textContent, $fontStyle, $paragraphStyle);
}
// TextRun
} else {
$textContent = $xmlReader->getValue('w:t', $domNode);
$parent->addText($textContent, $fStyle, $pStyle);
$parent->addText($textContent, $fontStyle, $paragraphStyle);
}
}
}

View File

@ -121,12 +121,12 @@ class Document extends AbstractPart
private function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart)
{
// Paragraph style
$pStyle = null;
$paragraphStyle = null;
$headingMatches = array();
if ($xmlReader->elementExists('w:pPr', $domNode)) {
$pStyle = $this->readParagraphStyle($xmlReader, $domNode);
if (is_string($pStyle)) {
preg_match('/Heading(\d)/', $pStyle, $headingMatches);
$paragraphStyle = $this->readParagraphStyle($xmlReader, $domNode);
if (is_string($paragraphStyle)) {
preg_match('/Heading(\d)/', $paragraphStyle, $headingMatches);
}
}
@ -134,7 +134,7 @@ class Document extends AbstractPart
if ($xmlReader->elementExists('w:r/w:instrText', $domNode)) {
$ignoreText = false;
$textContent = '';
$fStyle = $this->readFontStyle($xmlReader, $domNode);
$fontStyle = $this->readFontStyle($xmlReader, $domNode);
$nodes = $xmlReader->getElements('w:r', $domNode);
foreach ($nodes as $node) {
$instrText = $xmlReader->getValue('w:instrText', $node);
@ -154,7 +154,7 @@ class Document extends AbstractPart
}
}
}
$parent->addPreserveText($textContent, $fStyle, $pStyle);
$parent->addPreserveText($textContent, $fontStyle, $paragraphStyle);
// List item
} elseif ($xmlReader->elementExists('w:pPr/w:numPr', $domNode)) {
@ -165,7 +165,7 @@ class Document extends AbstractPart
foreach ($nodes as $node) {
$textContent .= $xmlReader->getValue('w:t', $node);
}
$parent->addListItem($textContent, $levelId, null, "PHPWordList{$numId}", $pStyle);
$parent->addListItem($textContent, $levelId, null, "PHPWordList{$numId}", $paragraphStyle);
// Heading
} elseif (!empty($headingMatches)) {
@ -182,17 +182,17 @@ class Document extends AbstractPart
$linkCount = $xmlReader->countElements('w:hyperlink', $domNode);
$runLinkCount = $runCount + $linkCount;
if ($runLinkCount == 0) {
$parent->addTextBreak(null, $pStyle);
$parent->addTextBreak(null, $paragraphStyle);
} else {
if ($runLinkCount > 1) {
$textrun = $parent->addTextRun($pStyle);
$textrun = $parent->addTextRun($paragraphStyle);
$textParent = &$textrun;
} else {
$textParent = &$parent;
}
$nodes = $xmlReader->getElements('*', $domNode);
foreach ($nodes as $node) {
$this->readRun($xmlReader, $node, $textParent, $docPart, $pStyle);
$this->readRun($xmlReader, $node, $textParent, $docPart, $paragraphStyle);
}
}
}

View File

@ -40,25 +40,25 @@ class Styles extends AbstractPart
switch ($type) {
case 'paragraph':
$pStyle = $this->readParagraphStyle($xmlReader, $node);
$fStyle = $this->readFontStyle($xmlReader, $node);
$paragraphStyle = $this->readParagraphStyle($xmlReader, $node);
$fontStyle = $this->readFontStyle($xmlReader, $node);
if (!empty($headingMatches)) {
$phpWord->addTitleStyle($headingMatches[1], $fStyle, $pStyle);
$phpWord->addTitleStyle($headingMatches[1], $fontStyle, $paragraphStyle);
} else {
if (empty($fStyle)) {
if (is_array($pStyle)) {
$phpWord->addParagraphStyle($name, $pStyle);
if (empty($fontStyle)) {
if (is_array($paragraphStyle)) {
$phpWord->addParagraphStyle($name, $paragraphStyle);
}
} else {
$phpWord->addFontStyle($name, $fStyle, $pStyle);
$phpWord->addFontStyle($name, $fontStyle, $paragraphStyle);
}
}
break;
case 'character':
$fStyle = $this->readFontStyle($xmlReader, $node);
if (!empty($fStyle)) {
$phpWord->addFontStyle($name, $fStyle);
$fontStyle = $this->readFontStyle($xmlReader, $node);
if (!empty($fontStyle)) {
$phpWord->addFontStyle($name, $fontStyle);
}
break;

View File

@ -41,12 +41,12 @@ class Style
* Add font style
*
* @param string $styleName
* @param array $styleFont
* @param array $styleParagraph
* @param array $fontStyle
* @param array $paragraphStyle
*/
public static function addFontStyle($styleName, $styleFont, $styleParagraph = null)
public static function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
{
self::setStyleValues($styleName, new Font('text', $styleParagraph), $styleFont);
self::setStyleValues($styleName, new Font('text', $paragraphStyle), $fontStyle);
}
/**
@ -76,12 +76,12 @@ class Style
* Add title style
*
* @param int $titleCount
* @param array $styleFont
* @param array $styleParagraph
* @param array $fontStyle
* @param array $paragraphStyle
*/
public static function addTitleStyle($titleCount, $styleFont, $styleParagraph = null)
public static function addTitleStyle($titleCount, $fontStyle, $paragraphStyle = null)
{
self::setStyleValues("Heading_{$titleCount}", new Font('title', $styleParagraph), $styleFont);
self::setStyleValues("Heading_{$titleCount}", new Font('title', $paragraphStyle), $fontStyle);
}
/**

View File

@ -30,33 +30,33 @@ class Text extends Element
{
$html = '';
// Paragraph style
$pStyle = $this->element->getParagraphStyle();
$pStyleIsObject = ($pStyle instanceof Paragraph);
if ($pStyleIsObject) {
$styleWriter = new ParagraphStyleWriter($pStyle);
$pStyle = $styleWriter->write();
$paragraphStyle = $this->element->getParagraphStyle();
$paragraphStyleIsObject = ($paragraphStyle instanceof Paragraph);
if ($paragraphStyleIsObject) {
$styleWriter = new ParagraphStyleWriter($paragraphStyle);
$paragraphStyle = $styleWriter->write();
}
// Font style
$fStyle = $this->element->getFontStyle();
$fStyleIsObject = ($fStyle instanceof Font);
if ($fStyleIsObject) {
$styleWriter = new FontStyleWriter($fStyle);
$fStyle = $styleWriter->write();
$fontStyle = $this->element->getFontStyle();
$fontStyleIsObject = ($fontStyle instanceof Font);
if ($fontStyleIsObject) {
$styleWriter = new FontStyleWriter($fontStyle);
$fontStyle = $styleWriter->write();
}
if ($pStyle && !$this->withoutP) {
$attribute = $pStyleIsObject ? 'style' : 'class';
$html .= "<p {$attribute}=\"{$pStyle}\">";
if ($paragraphStyle && !$this->withoutP) {
$attribute = $paragraphStyleIsObject ? 'style' : 'class';
$html .= "<p {$attribute}=\"{$paragraphStyle}\">";
}
if ($fStyle) {
$attribute = $fStyleIsObject ? 'style' : 'class';
$html .= "<span {$attribute}=\"{$fStyle}\">";
if ($fontStyle) {
$attribute = $fontStyleIsObject ? 'style' : 'class';
$html .= "<span {$attribute}=\"{$fontStyle}\">";
}
$html .= htmlspecialchars($this->element->getText());
if ($fStyle) {
if ($fontStyle) {
$html .= '</span>';
}
if ($pStyle && !$this->withoutP) {
if ($paragraphStyle && !$this->withoutP) {
$html .= '</p>' . PHP_EOL;
}

View File

@ -30,15 +30,15 @@ class TextRun extends Element
$elements = $this->element->getElements();
if (count($elements) > 0) {
// Paragraph style
$pStyle = $this->element->getParagraphStyle();
$pStyleIsObject = ($pStyle instanceof Paragraph);
if ($pStyleIsObject) {
$styleWriter = new ParagraphStyleWriter($pStyle);
$pStyle = $styleWriter->write();
$paragraphStyle = $this->element->getParagraphStyle();
$paragraphStyleIsObject = ($paragraphStyle instanceof Paragraph);
if ($paragraphStyleIsObject) {
$styleWriter = new ParagraphStyleWriter($paragraphStyle);
$paragraphStyle = $styleWriter->write();
}
$tag = $this->withoutP ? 'span' : 'p';
$attribute = $pStyleIsObject ? 'style' : 'class';
$html .= "<{$tag} {$attribute}=\"{$pStyle}\">";
$attribute = $paragraphStyleIsObject ? 'style' : 'class';
$html .= "<{$tag} {$attribute}=\"{$paragraphStyle}\">";
foreach ($elements as $element) {
$elementWriter = new Element($this->parentWriter, $element, true);
$html .= $elementWriter->write();

View File

@ -26,6 +26,10 @@ class Font extends AbstractStyle
*/
public function write()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Font)) {
return;
}
$css = array();
if (PhpWord::DEFAULT_FONT_NAME != $this->style->getName()) {
$css['font-family'] = "'" . $this->style->getName() . "'";

View File

@ -23,6 +23,10 @@ class Image extends AbstractStyle
*/
public function write()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Image)) {
return;
}
$css = array();
if ($this->style->getWidth()) {
$css['width'] = $this->style->getWidth() . 'px';

View File

@ -23,6 +23,10 @@ class Paragraph extends AbstractStyle
*/
public function write()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Paragraph)) {
return;
}
$css = array();
if ($this->style->getAlign()) {
$css['text-align'] = $this->style->getAlign();

View File

@ -21,11 +21,11 @@ class Text extends Element
*/
public function write()
{
$styleFont = $this->element->getFontStyle();
$styleParagraph = $this->element->getParagraphStyle();
$fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle();
// @todo Commented for TextRun. Should really checkout this value
// $SfIsObject = ($styleFont instanceof Font) ? true : false;
// $SfIsObject = ($fontStyle instanceof Font) ? true : false;
$SfIsObject = false;
if ($SfIsObject) {
@ -35,23 +35,23 @@ class Text extends Element
if (!$this->withoutP) {
$this->xmlWriter->startElement('text:p'); // text:p
}
if (empty($styleFont)) {
if (empty($styleParagraph)) {
if (empty($fontStyle)) {
if (empty($paragraphStyle)) {
$this->xmlWriter->writeAttribute('text:style-name', 'P1');
} elseif (is_string($styleParagraph)) {
$this->xmlWriter->writeAttribute('text:style-name', $styleParagraph);
} elseif (is_string($paragraphStyle)) {
$this->xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
}
$this->xmlWriter->writeRaw($this->element->getText());
} else {
if (empty($styleParagraph)) {
if (empty($paragraphStyle)) {
$this->xmlWriter->writeAttribute('text:style-name', 'Standard');
} elseif (is_string($styleParagraph)) {
$this->xmlWriter->writeAttribute('text:style-name', $styleParagraph);
} elseif (is_string($paragraphStyle)) {
$this->xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
}
// text:span
$this->xmlWriter->startElement('text:span');
if (is_string($styleFont)) {
$this->xmlWriter->writeAttribute('text:style-name', $styleFont);
if (is_string($fontStyle)) {
$this->xmlWriter->writeAttribute('text:style-name', $fontStyle);
}
$this->xmlWriter->writeRaw($this->element->getText());
$this->xmlWriter->endElement();

View File

@ -55,23 +55,14 @@ class Content extends AbstractPart
$xmlWriter->startElement('office:text');
// text:sequence-decls
$sequences = array('Illustration', 'Table', 'Text', 'Drawing');
$xmlWriter->startElement('text:sequence-decls');
$xmlWriter->startElement('text:sequence-decl');
$xmlWriter->writeAttribute('text:display-outline-level', 0);
$xmlWriter->writeAttribute('text:name', 'Illustration');
$xmlWriter->endElement();
$xmlWriter->startElement('text:sequence-decl');
$xmlWriter->writeAttribute('text:display-outline-level', 0);
$xmlWriter->writeAttribute('text:name', 'Table');
$xmlWriter->endElement();
$xmlWriter->startElement('text:sequence-decl');
$xmlWriter->writeAttribute('text:display-outline-level', 0);
$xmlWriter->writeAttribute('text:name', 'Text');
$xmlWriter->endElement();
$xmlWriter->startElement('text:sequence-decl');
$xmlWriter->writeAttribute('text:display-outline-level', 0);
$xmlWriter->writeAttribute('text:name', 'Drawing');
$xmlWriter->endElement();
foreach ($sequences as $sequence) {
$xmlWriter->startElement('text:sequence-decl');
$xmlWriter->writeAttribute('text:display-outline-level', 0);
$xmlWriter->writeAttribute('text:name', $sequence);
$xmlWriter->endElement();
}
$xmlWriter->endElement(); // text:sequence-decl
$sections = $phpWord->getSections();
@ -79,7 +70,6 @@ class Content extends AbstractPart
if ($sectionCount > 0) {
foreach ($sections as $section) {
$elements = $section->getElements();
// $xmlWriter->startElement('text:section');
foreach ($elements as $element) {
$elementWriter = new ElementWriter($xmlWriter, $this, $element, false);
@ -104,7 +94,7 @@ class Content extends AbstractPart
// Font and paragraph
$styles = Style::getStyles();
$pStyleCount = 0;
$paragraphStyleCount = 0;
if (count($styles) > 0) {
foreach ($styles as $styleName => $style) {
if (preg_match('#^T[0-9]+$#', $styleName) != 0
@ -117,11 +107,11 @@ class Content extends AbstractPart
$styleWriter->write();
}
if ($style instanceof Paragraph) {
$pStyleCount++;
$paragraphStyleCount++;
}
}
}
if ($pStyleCount == 0) {
if ($paragraphStyleCount == 0) {
$style = new Paragraph();
$style->setStyleName('P1');
$styleWriter = new \PhpOffice\PhpWord\Writer\ODText\Style\Paragraph($xmlWriter, $style);
@ -181,31 +171,31 @@ class Content extends AbstractPart
$sections = $phpWord->getSections();
$sectionCount = count($sections);
if ($sectionCount > 0) {
$pStyleCount = 0;
$fStyleCount = 0;
$paragraphStyleCount = 0;
$fontStyleCount = 0;
foreach ($sections as $section) {
$elements = $section->getElements();
foreach ($elements as $element) {
if ($element instanceof Text) {
$fStyle = $element->getFontStyle();
$pStyle = $element->getParagraphStyle();
$fontStyle = $element->getFontStyle();
$paragraphStyle = $element->getParagraphStyle();
// Font
if ($fStyle instanceof Font) {
$fStyleCount++;
if ($fontStyle instanceof Font) {
$fontStyleCount++;
$arrStyle = array(
'color' => $fStyle->getColor(),
'name' => $fStyle->getName()
'color' => $fontStyle->getColor(),
'name' => $fontStyle->getName()
);
$phpWord->addFontStyle('T' . $fStyleCount, $arrStyle);
$element->setFontStyle('T' . $fStyleCount);
$phpWord->addFontStyle('T' . $fontStyleCount, $arrStyle);
$element->setFontStyle('T' . $fontStyleCount);
// Paragraph
} elseif ($pStyle instanceof Paragraph) {
$pStyleCount++;
} elseif ($paragraphStyle instanceof Paragraph) {
$paragraphStyleCount++;
$phpWord->addParagraphStyle('P' . $pStyleCount, array());
$element->setParagraphStyle('P' . $pStyleCount);
$phpWord->addParagraphStyle('P' . $paragraphStyleCount, array());
$element->setParagraphStyle('P' . $paragraphStyleCount);
}
}
}

View File

@ -28,6 +28,10 @@ class Font extends AbstractStyle
*/
public function write()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Font)) {
return;
}
$this->xmlWriter->startElement('style:style');
$this->xmlWriter->writeAttribute('style:name', $this->style->getStyleName());
$this->xmlWriter->writeAttribute('style:family', 'text');

View File

@ -28,6 +28,10 @@ class Paragraph extends AbstractStyle
*/
public function write()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Paragraph)) {
return;
}
$marginTop = is_null($this->style->getSpaceBefore()) ? '0' : round(17.6 / $this->style->getSpaceBefore(), 2);
$marginBottom = is_null($this->style->getSpaceAfter()) ? '0' : round(17.6 / $this->style->getSpaceAfter(), 2);

View File

@ -229,11 +229,11 @@ class RTF extends AbstractWriter implements WriterInterface
foreach ($elements as $element) {
if ($element instanceof Text) {
$fStyle = $element->getFontStyle();
$fontStyle = $element->getFontStyle();
if ($fStyle instanceof Font) {
if (in_array($fStyle->getName(), $arrFonts) == false) {
$arrFonts[] = $fStyle->getName();
if ($fontStyle instanceof Font) {
if (in_array($fontStyle->getName(), $arrFonts) == false) {
$arrFonts[] = $fontStyle->getName();
}
}
}
@ -286,14 +286,14 @@ class RTF extends AbstractWriter implements WriterInterface
foreach ($elements as $element) {
if ($element instanceof Text) {
$fStyle = $element->getFontStyle();
$fontStyle = $element->getFontStyle();
if ($fStyle instanceof Font) {
if (in_array($fStyle->getColor(), $arrColors) == false) {
$arrColors[] = $fStyle->getColor();
if ($fontStyle instanceof Font) {
if (in_array($fontStyle->getColor(), $arrColors) == false) {
$arrColors[] = $fontStyle->getColor();
}
if (in_array($fStyle->getFgColor(), $arrColors) == false) {
$arrColors[] = $fStyle->getFgColor();
if (in_array($fontStyle->getFgColor(), $arrColors) == false) {
$arrColors[] = $fontStyle->getFgColor();
}
}
}

View File

@ -27,24 +27,24 @@ class Text extends Element
{
$rtfText = '';
$styleFont = $this->element->getFontStyle();
if (is_string($styleFont)) {
$styleFont = Style::getStyle($styleFont);
$fontStyle = $this->element->getFontStyle();
if (is_string($fontStyle)) {
$fontStyle = Style::getStyle($fontStyle);
}
$styleParagraph = $this->element->getParagraphStyle();
if (is_string($styleParagraph)) {
$styleParagraph = Style::getStyle($styleParagraph);
$paragraphStyle = $this->element->getParagraphStyle();
if (is_string($paragraphStyle)) {
$paragraphStyle = Style::getStyle($paragraphStyle);
}
if ($styleParagraph && !$this->withoutP) {
if ($paragraphStyle && !$this->withoutP) {
if ($this->parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) {
$rtfText .= '\pard\nowidctlpar';
if ($styleParagraph->getSpaceAfter() != null) {
$rtfText .= '\sa' . $styleParagraph->getSpaceAfter();
if ($paragraphStyle->getSpaceAfter() != null) {
$rtfText .= '\sa' . $paragraphStyle->getSpaceAfter();
}
if ($styleParagraph->getAlign() != null) {
if ($styleParagraph->getAlign() == 'center') {
if ($paragraphStyle->getAlign() != null) {
if ($paragraphStyle->getAlign() == 'center') {
$rtfText .= '\qc';
}
}
@ -56,49 +56,49 @@ class Text extends Element
$this->parentWriter->setLastParagraphStyle();
}
if ($styleFont instanceof Font) {
if ($styleFont->getColor() != null) {
$idxColor = array_search($styleFont->getColor(), $this->parentWriter->getColorTable());
if ($fontStyle instanceof Font) {
if ($fontStyle->getColor() != null) {
$idxColor = array_search($fontStyle->getColor(), $this->parentWriter->getColorTable());
if ($idxColor !== false) {
$rtfText .= '\cf' . ($idxColor + 1);
}
} else {
$rtfText .= '\cf0';
}
if ($styleFont->getName() != null) {
$idxFont = array_search($styleFont->getName(), $this->parentWriter->getFontTable());
if ($fontStyle->getName() != null) {
$idxFont = array_search($fontStyle->getName(), $this->parentWriter->getFontTable());
if ($idxFont !== false) {
$rtfText .= '\f' . $idxFont;
}
} else {
$rtfText .= '\f0';
}
if ($styleFont->getBold()) {
if ($fontStyle->getBold()) {
$rtfText .= '\b';
}
if ($styleFont->getItalic()) {
if ($fontStyle->getItalic()) {
$rtfText .= '\i';
}
if ($styleFont->getSize()) {
$rtfText .= '\fs' . ($styleFont->getSize() * 2);
if ($fontStyle->getSize()) {
$rtfText .= '\fs' . ($fontStyle->getSize() * 2);
}
}
if ($this->parentWriter->getLastParagraphStyle() != '' || $styleFont) {
if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) {
$rtfText .= ' ';
}
$rtfText .= $this->element->getText();
if ($styleFont instanceof Font) {
if ($fontStyle instanceof Font) {
$rtfText .= '\cf0';
$rtfText .= '\f0';
if ($styleFont->getBold()) {
if ($fontStyle->getBold()) {
$rtfText .= '\b0';
}
if ($styleFont->getItalic()) {
if ($fontStyle->getItalic()) {
$rtfText .= '\i0';
}
if ($styleFont->getSize()) {
if ($fontStyle->getSize()) {
$rtfText .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2);
}
}

View File

@ -29,11 +29,11 @@ class CheckBox extends Element
$name = String::controlCharacterPHP2OOXML($name);
$text = htmlspecialchars($this->element->getText());
$text = String::controlCharacterPHP2OOXML($text);
$fStyle = $this->element->getFontStyle();
$pStyle = $this->element->getParagraphStyle();
$fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle();
if (!$this->withoutP) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
@ -78,7 +78,7 @@ class CheckBox extends Element
$this->xmlWriter->endElement();// w:fldChar
$this->xmlWriter->endElement(); // w:r
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r');

View File

@ -25,18 +25,18 @@ class Link extends Element
public function write()
{
$rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0);
$fStyle = $this->element->getFontStyle();
$pStyle = $this->element->getParagraphStyle();
$fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle();
if (!$this->withoutP) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$styleWriter->write();
}
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:hyperlink');

View File

@ -27,8 +27,8 @@ class ListItem extends Element
$textObject = $this->element->getTextObject();
$depth = $this->element->getDepth();
$numId = $this->element->getStyle()->getNumId();
$pStyle = $textObject->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$paragraphStyle = $textObject->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setWithoutPPR(true);
$styleWriter->setIsInline(true);

View File

@ -25,14 +25,14 @@ class PreserveText extends Element
*/
public function write()
{
$fStyle = $this->element->getFontStyle();
$pStyle = $this->element->getParagraphStyle();
$fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle();
$texts = $this->element->getText();
if (!is_array($texts)) {
$texts = array($texts);
}
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
@ -41,7 +41,7 @@ class PreserveText extends Element
foreach ($texts as $text) {
if (substr($text, 0, 1) == '{') {
$text = substr($text, 1, -1);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r');
@ -72,7 +72,7 @@ class PreserveText extends Element
} else {
$text = htmlspecialchars($text);
$text = String::controlCharacterPHP2OOXML($text);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r');

View File

@ -26,17 +26,17 @@ class TOC extends Element
public function write()
{
$titles = $this->element->getTitles();
$styleFont = $this->element->getStyleFont();
$fontStyle = $this->element->getStyleFont();
$styleTOC = $this->element->getStyleTOC();
$fIndent = $styleTOC->getIndent();
$tabLeader = $styleTOC->getTabLeader();
$tabPos = $styleTOC->getTabPos();
$tocStyle = $this->element->getStyleTOC();
$fIndent = $tocStyle->getIndent();
$tabLeader = $tocStyle->getTabLeader();
$tabPos = $tocStyle->getTabPos();
$maxDepth = $this->element->getMaxDepth();
$minDepth = $this->element->getMinDepth();
$isObject = ($styleFont instanceof Font) ? true : false;
$isObject = ($fontStyle instanceof Font) ? true : false;
for ($i = 0; $i < count($titles); $i++) {
$title = $titles[$i];
@ -46,8 +46,8 @@ class TOC extends Element
$this->xmlWriter->startElement('w:pPr');
if ($isObject && !is_null($styleFont->getParagraphStyle())) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $styleFont->getParagraphStyle());
if ($isObject && !is_null($fontStyle->getParagraphStyle())) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $fontStyle->getParagraphStyle());
$styleWriter->write();
}
@ -57,10 +57,10 @@ class TOC extends Element
$this->xmlWriter->endElement();
}
if (!empty($styleFont) && !$isObject) {
if (!empty($fontStyle) && !$isObject) {
$this->xmlWriter->startElement('w:pPr');
$this->xmlWriter->startElement('w:pStyle');
$this->xmlWriter->writeAttribute('w:val', $styleFont);
$this->xmlWriter->writeAttribute('w:val', $fontStyle);
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
}
@ -106,7 +106,7 @@ class TOC extends Element
$this->xmlWriter->startElement('w:r');
if ($isObject) {
$styleWriter = new FontStyleWriter($this->xmlWriter, $styleFont);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->write();
}

View File

@ -25,19 +25,19 @@ class Text extends Element
*/
public function write()
{
$fStyle = $this->element->getFontStyle();
$pStyle = $this->element->getParagraphStyle();
$fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle();
$text = htmlspecialchars($this->element->getText());
$text = String::controlCharacterPHP2OOXML($text);
if (!$this->withoutP) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$styleWriter->write();
}
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r');

View File

@ -26,21 +26,21 @@ class TextBreak extends Element
{
if (!$this->withoutP) {
$hasStyle = false;
$fStyle = null;
$pStyle = null;
$fontStyle = null;
$paragraphStyle = null;
if (!is_null($this->element)) {
$fStyle = $this->element->getFontStyle();
$pStyle = $this->element->getParagraphStyle();
$hasStyle = !is_null($fStyle) || !is_null($pStyle);
$fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle();
$hasStyle = !is_null($fontStyle) || !is_null($paragraphStyle);
}
if ($hasStyle) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$styleWriter->write();
if (!is_null($fStyle)) {
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
if (!is_null($fontStyle)) {
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:pPr');

View File

@ -23,8 +23,8 @@ class TextRun extends Element
*/
public function write()
{
$pStyle = $this->element->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$paragraphStyle = $this->element->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');