diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index f4e09fc9..b2f71943 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -13,26 +13,18 @@ use PhpOffice\PhpWord\Endnotes; use PhpOffice\PhpWord\Footnotes; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; -use PhpOffice\PhpWord\Element\Endnote; -use PhpOffice\PhpWord\Element\Footnote; -use PhpOffice\PhpWord\Element\Image; -use PhpOffice\PhpWord\Element\Link; -use PhpOffice\PhpWord\Element\ListItem; -use PhpOffice\PhpWord\Element\Object; -use PhpOffice\PhpWord\Element\PageBreak; -use PhpOffice\PhpWord\Element\PreserveText; -use PhpOffice\PhpWord\Element\Table; -use PhpOffice\PhpWord\Element\Text; -use PhpOffice\PhpWord\Element\TextBreak; -use PhpOffice\PhpWord\Element\TextRun; -use PhpOffice\PhpWord\Element\Title; +use PhpOffice\PhpWord\Element\AbstractElement; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; +use PhpOffice\PhpWord\Writer\HTML\Element\Element as ElementWriter; +use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter; +use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter; /** * HTML writer * + * Not supported: PreserveText, PageBreak, Object * @since 0.10.0 */ class HTML extends AbstractWriter implements WriterInterface @@ -154,39 +146,15 @@ class HTML extends AbstractWriter implements WriterInterface $sections = $phpWord->getSections(); $countSections = count($sections); - $pSection = 0; if ($countSections > 0) { foreach ($sections as $section) { - $pSection++; - $contents = $section->getElements(); - foreach ($contents as $element) { - if ($element instanceof Text) { - $html .= $this->writeText($element); - } elseif ($element instanceof TextRun) { - $html .= $this->writeTextRun($element); - } elseif ($element instanceof Link) { - $html .= $this->writeLink($element); - } elseif ($element instanceof Title) { - $html .= $this->writeTitle($element); - } elseif ($element instanceof PreserveText) { - $html .= $this->writePreserveText($element); - } elseif ($element instanceof TextBreak) { - $html .= $this->writeTextBreak($element); - } elseif ($element instanceof PageBreak) { - $html .= $this->writePageBreak($element); - } elseif ($element instanceof Table) { - $html .= $this->writeTable($element); - } elseif ($element instanceof ListItem) { - $html .= $this->writeListItem($element); - } elseif ($element instanceof Image) { - $html .= $this->writeImage($element); - } elseif ($element instanceof Object) { - $html .= $this->writeObject($element); - } elseif ($element instanceof Endnote) { - $html .= $this->writeEndnote($element); - } elseif ($element instanceof Footnote) { - $html .= $this->writeFootnote($element); + $elements = $section->getElements(); + foreach ($elements as $element) { + if ($element instanceof AbstractElement) { + $elementWriter = new ElementWriter($element, false); + $elementWriter->setParentWriter($this); + $html .= $elementWriter->write(); } } } @@ -196,7 +164,7 @@ class HTML extends AbstractWriter implements WriterInterface } /** - * Write footnote/endnote contents + * Write footnote/endnote contents as textruns */ private function writeNotes() { @@ -204,7 +172,7 @@ class HTML extends AbstractWriter implements WriterInterface $endnote = Endnotes::getElements(); $html = ''; - if (count($this->notes) > 0) { + if (!empty($this->notes)) { $html .= "
writeParagraphStyle($paragraphStyle) . '"'; - } - $html .= '>'; - } - if ($fontStyle) { - $html .= 'writeFontStyle($fontStyle) . '"'; - } - $html .= '>'; - } - $html .= htmlspecialchars($text->getText()); - if ($fontStyle) { - $html .= ''; - } - if ($paragraphStyle && !$withoutP) { - $html .= '
' . PHP_EOL; - } - - return $html; - } - - /** - * Write text run content - * - * @param mixed $textrun - * @param boolean $withoutP - * @return string - */ - private function writeTextRun($textrun, $withoutP = false) - { - $html = ''; - $elements = $textrun->getElements(); - if (count($elements) > 0) { - $paragraphStyle = $textrun->getParagraphStyle(); - $spIsObject = ($paragraphStyle instanceof Paragraph); - - $html .= $withoutP ? 'writeParagraphStyle($paragraphStyle) . '"'; - } - } - $html .= '>'; - foreach ($elements as $element) { - if ($element instanceof Text) { - $html .= $this->writeText($element, true); - } elseif ($element instanceof Link) { - $html .= $this->writeLink($element, true); - } elseif ($element instanceof TextBreak) { - $html .= $this->writeTextBreak($element, true); - } elseif ($element instanceof Image) { - $html .= $this->writeImage($element, true); - } elseif ($element instanceof Endnote) { - $html .= $this->writeEndnote($element); - } elseif ($element instanceof Footnote) { - $html .= $this->writeFootnote($element); - } - } - $html .= $withoutP ? '' : ''; - $html .= PHP_EOL; - } - - return $html; - } - - /** - * Write link - * - * @param \PhpOffice\PhpWord\Element\Link $element - * @param boolean $withoutP - * @return string - */ - private function writeLink($element, $withoutP = false) - { - $url = $element->getLinkSrc(); - $text = $element->getLinkName(); - if ($text == '') { - $text = $url; - } - $html = ''; - if (!$withoutP) { - $html .= "" . PHP_EOL; - } - $html .= "{$text}" . PHP_EOL; - if (!$withoutP) { - $html .= "
" . PHP_EOL; - } - - return $html; - } - - /** - * Write heading - * - * @param \PhpOffice\PhpWord\Element\Title $element - * @return string - */ - private function writeTitle($element) - { - $tag = 'h' . $element->getDepth(); - $text = htmlspecialchars($element->getText()); - $html = "<{$tag}>{$text}{$tag}>" . PHP_EOL; - - return $html; - } - - /** - * Write preserve text - * - * @param \PhpOffice\PhpWord\Element\PreserveText $element - * @param boolean $withoutP - * @return string - */ - private function writePreserveText($element, $withoutP = false) - { - return $this->writeUnsupportedElement($element, $withoutP); - } - - /** - * Get text break - * - * @param \PhpOffice\PhpWord\Element\TextBreak $element - * @param boolean $withoutP - * @return string - */ - private function writeTextBreak($element, $withoutP = false) - { - if ($withoutP) { - $html = '' . PHP_EOL; - } - - return $html; - } - - /** - * Write page break - * - * @param \PhpOffice\PhpWord\Element\PageBreak $element - * @return string - */ - private function writePageBreak($element) - { - return $this->writeUnsupportedElement($element, false); - } - - /** - * Write list item - * - * @param \PhpOffice\PhpWord\Element\ListItem $element - * @return string - */ - private function writeListItem($element) - { - $text = htmlspecialchars($element->getTextObject()->getText()); - $html = '
' . $text . '{$p}>' . PHP_EOL; - - return $html; - } - - /** - * Write table - * - * @param \PhpOffice\PhpWord\Element\Table $element - * @return string - */ - private function writeTable($element) - { - $html = ''; - $rows = $element->getRows(); - $cRows = count($rows); - if ($cRows > 0) { - $html .= "
{$html}
" . PHP_EOL; - } - } - } - - return $html; - } - - /** - * Write object - * - * @param \PhpOffice\PhpWord\Element\Object $element - * @param boolean $withoutP - * @return string - */ - private function writeObject($element, $withoutP = false) - { - return $this->writeUnsupportedElement($element, $withoutP); - } - - /** - * Write footnote - * - * @param \PhpOffice\PhpWord\Element\Footnote $element - * @return string - */ - private function writeFootnote($element) - { - return $this->writeNote($element); - } - - /** - * Write endnote - * - * @param \PhpOffice\PhpWord\Element\Endnote $element - * @return string - */ - private function writeEndnote($element) - { - return $this->writeNote($element); - } - - /** - * Write footnote/endnote marks - * - * @param mixed $element - * @return string - */ - private function writeNote($element) - { - $index = count($this->notes) + 1; - $prefix = ($element instanceof Endnote) ? 'endnote' : 'footnote'; - $noteMark = $prefix . '-' . $element->getRelationId(); - $noteAnchor = "note-{$index}"; - $this->notes[$index] = $noteMark; - $html = "{$index}"; - - return $html; - } - - /** - * Write unsupported element - * - * @param mixed $element - * @param boolean $withoutP - * @return string - */ - private function writeUnsupportedElement($element, $withoutP = false) - { - $elementClass = get_class($element); - $elementMark = str_replace('PhpOffice\\PhpWord\\Element\\', '', $elementClass); - $elementMark = htmlentities("<{$elementMark}>"); - if ($withoutP) { - $html = "{$elementMark}" . PHP_EOL; - } else { - $html = "{$elementMark}
" . PHP_EOL; - } - - return $html; - } - /** * Get styles * @@ -593,23 +218,26 @@ class HTML extends AbstractWriter implements WriterInterface ), ); foreach ($defaultStyles as $selector => $style) { - $css .= $selector . ' ' . $this->assembleCss($style, true) . PHP_EOL; + $styleWriter = new StyleWriter(); + $css .= $selector . ' '; + $css .= $styleWriter->assembleCss($style, true) . PHP_EOL; } // Custom styles $customStyles = Style::getStyles(); if (is_array($customStyles)) { foreach ($customStyles as $name => $style) { + $styleWriter = new StyleWriter($this, $style, true); if ($style instanceof Font) { if ($style->getStyleType() == 'title') { $name = str_replace('Heading_', 'h', $name); } else { $name = '.' . $name; } - $css .= "{$name} " . $this->writeFontStyle($style, true) . PHP_EOL; + $css .= "{$name} " . $styleWriter->write() . PHP_EOL; } elseif ($style instanceof Paragraph) { $name = '.' . $name; - $css .= "{$name} " . $this->writeParagraphStyle($style, true) . PHP_EOL; + $css .= "{$name} " . $styleWriter->write() . PHP_EOL; } } } @@ -619,140 +247,33 @@ class HTML extends AbstractWriter implements WriterInterface } /** - * Get font style + * Get is PDF * - * @param \PhpOffice\PhpWord\Style\Font $style - * @param boolean $curlyBracket - * @return string + * @return bool */ - private function writeFontStyle($style, $curlyBracket = false) + public function isPdf() { - $css = array(); - if (PHPWord::DEFAULT_FONT_NAME != $style->getName()) { - $css['font-family'] = "'" . $style->getName() . "'"; - } - if (PHPWord::DEFAULT_FONT_SIZE != $style->getSize()) { - $css['font-size'] = $style->getSize() . 'pt'; - } - if (PHPWord::DEFAULT_FONT_COLOR != $style->getColor()) { - $css['color'] = '#' . $style->getColor(); - } - $css['background'] = $style->getFgColor(); - if ($style->getBold()) { - $css['font-weight'] = 'bold'; - } - if ($style->getItalic()) { - $css['font-style'] = 'italic'; - } - if ($style->getSuperScript()) { - $css['vertical-align'] = 'super'; - } elseif ($style->getSubScript()) { - $css['vertical-align'] = 'sub'; - } - $css['text-decoration'] = ''; - if ($style->getUnderline() != Font::UNDERLINE_NONE) { - $css['text-decoration'] .= 'underline '; - } - if ($style->getStrikethrough()) { - $css['text-decoration'] .= 'line-through '; - } - - return $this->assembleCss($css, $curlyBracket); + return $this->isPdf; } /** - * Get paragraph style + * Get notes * - * @param \PhpOffice\PhpWord\Style\Paragraph $style - * @param boolean $curlyBracket - * @return string + * @return array */ - private function writeParagraphStyle($style, $curlyBracket = false) + public function getNotes() { - $css = array(); - if ($style->getAlign()) { - $css['text-align'] = $style->getAlign(); - } - - return $this->assembleCss($css, $curlyBracket); + return $this->notes; } /** - * Takes array where of CSS properties / values and converts to CSS string + * Add note * - * @param array $css - * @param boolean $curlyBracket - * @return string + * @param int $noteId + * @param string $noteMark */ - private function assembleCss($css, $curlyBracket = false) + public function addNote($noteId, $noteMark) { - $pairs = array(); - foreach ($css as $key => $value) { - if ($value != '') { - $pairs[] = $key . ': ' . $value; - } - } - $string = implode('; ', $pairs); - if ($curlyBracket) { - $string = '{ ' . $string . ' }'; - } - - return $string; - } - - /** - * Get Base64 image data - * - * @return string|null - */ - private function getBase64ImageData(Image $element) - { - $source = $element->getSource(); - $imageType = $element->getImageType(); - $imageData = null; - $imageBinary = null; - $actualSource = null; - - // Get actual source from archive image or other source - // Return null if not found - if ($element->getSourceType() == Image::SOURCE_ARCHIVE) { - $source = substr($source, 6); - list($zipFilename, $imageFilename) = explode('#', $source); - - $zipClass = \PhpOffice\PhpWord\Settings::getZipClass(); - $zip = new $zipClass(); - if ($zip->open($zipFilename) !== false) { - if ($zip->locateName($imageFilename)) { - $zip->extractTo($this->getTempDir(), $imageFilename); - $actualSource = $this->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename; - } - } - $zip->close(); - } else { - $actualSource = $source; - } - if (is_null($actualSource)) { - return null; - } - - // Read image binary data and convert into Base64 - if ($element->getSourceType() == Image::SOURCE_GD) { - $imageResource = call_user_func($element->getImageCreateFunction(), $actualSource); - ob_start(); - call_user_func($element->getImageFunction(), $imageResource); - $imageBinary = ob_get_contents(); - ob_end_clean(); - } else { - if ($fileHandle = fopen($actualSource, 'rb', false)) { - $imageBinary = fread($fileHandle, filesize($actualSource)); - fclose($fileHandle); - } - } - if (!is_null($imageBinary)) { - $base64 = chunk_split(base64_encode($imageBinary)); - $imageData = 'data:' . $imageType . ';base64,' . $base64; - } - - return $imageData; + $this->notes[$noteId] = $noteMark; } } diff --git a/src/PhpWord/Writer/HTML/Element/Element.php b/src/PhpWord/Writer/HTML/Element/Element.php new file mode 100644 index 00000000..26a1618c --- /dev/null +++ b/src/PhpWord/Writer/HTML/Element/Element.php @@ -0,0 +1,101 @@ +element = $element; + $this->withoutP = $withoutP; + } + + /** + * Write element + * + * @return string + */ + public function write() + { + $html = ''; + $elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element)); + $elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $elmName; + if (class_exists($elmWriterClass) === true) { + $elmWriter = new $elmWriterClass($this->element, $this->withoutP); + $elmWriter->setParentWriter($this->parentWriter); + $html = $elmWriter->write(); + } + + return $html; + } + + /** + * Set parent writer + * + * @param \PhpOffice\PhpWord\Writer\HTML $pWriter + */ + public function setParentWriter(HTML $writer) + { + $this->parentWriter = $writer; + } + + /** + * Get parent writer + * + * @return \PhpOffice\PhpWord\Writer\HTML + * @throws \PhpOffice\PhpWord\Exception\Exception + */ + public function getParentWriter() + { + if (!is_null($this->parentWriter)) { + return $this->parentWriter; + } else { + throw new Exception("No parent HTML Writer assigned."); + } + } +} diff --git a/src/PhpWord/Writer/HTML/Element/Endnote.php b/src/PhpWord/Writer/HTML/Element/Endnote.php new file mode 100644 index 00000000..66efcf4d --- /dev/null +++ b/src/PhpWord/Writer/HTML/Element/Endnote.php @@ -0,0 +1,19 @@ +getParentWriter()->isPdf()) { + $imageData = $this->getBase64ImageData($this->element); + if (!is_null($imageData)) { + $styleWriter = new StyleWriter(); + $style = $styleWriter->assembleCss(array( + 'width' => $this->element->getStyle()->getWidth() . 'px', + 'height' => $this->element->getStyle()->getHeight() . 'px', + )); + $html = "{$html}
" . PHP_EOL; + } + } + } + + return $html; + } + + /** + * Get Base64 image data + * + * @return string|null + */ + private function getBase64ImageData(ImageElement $element) + { + $source = $element->getSource(); + $imageType = $element->getImageType(); + $imageData = null; + $imageBinary = null; + $actualSource = null; + + // Get actual source from archive image or other source + // Return null if not found + if ($element->getSourceType() == ImageElement::SOURCE_ARCHIVE) { + $source = substr($source, 6); + list($zipFilename, $imageFilename) = explode('#', $source); + + $zipClass = \PhpOffice\PhpWord\Settings::getZipClass(); + $zip = new $zipClass(); + if ($zip->open($zipFilename) !== false) { + if ($zip->locateName($imageFilename)) { + $zip->extractTo($this->getParentWriter()->getTempDir(), $imageFilename); + $actualSource = $this->getParentWriter()->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename; + } + } + $zip->close(); + } else { + $actualSource = $source; + } + if (is_null($actualSource)) { + return null; + } + + // Read image binary data and convert into Base64 + if ($element->getSourceType() == ImageElement::SOURCE_GD) { + $imageResource = call_user_func($element->getImageCreateFunction(), $actualSource); + ob_start(); + call_user_func($element->getImageFunction(), $imageResource); + $imageBinary = ob_get_contents(); + ob_end_clean(); + } else { + if ($fileHandle = fopen($actualSource, 'rb', false)) { + $imageBinary = fread($fileHandle, filesize($actualSource)); + fclose($fileHandle); + } + } + if (!is_null($imageBinary)) { + $base64 = chunk_split(base64_encode($imageBinary)); + $imageData = 'data:' . $imageType . ';base64,' . $base64; + } + + return $imageData; + } +} diff --git a/src/PhpWord/Writer/HTML/Element/Link.php b/src/PhpWord/Writer/HTML/Element/Link.php new file mode 100644 index 00000000..eab49e34 --- /dev/null +++ b/src/PhpWord/Writer/HTML/Element/Link.php @@ -0,0 +1,42 @@ +element->getLinkSrc(); + $text = $this->element->getLinkName(); + if ($text == '') { + $text = $url; + } + $html = ''; + if (!$this->withoutP) { + $html .= "" . PHP_EOL; + } + $html .= "{$text}" . PHP_EOL; + if (!$this->withoutP) { + $html .= "
" . PHP_EOL; + } + + return $html; + } +} diff --git a/src/PhpWord/Writer/HTML/Element/ListItem.php b/src/PhpWord/Writer/HTML/Element/ListItem.php new file mode 100644 index 00000000..58f5159e --- /dev/null +++ b/src/PhpWord/Writer/HTML/Element/ListItem.php @@ -0,0 +1,31 @@ +element->getTextObject()->getText()); + $html = '' . $text . '{$p}>' . PHP_EOL; + + return $html; + } +} diff --git a/src/PhpWord/Writer/HTML/Element/Note.php b/src/PhpWord/Writer/HTML/Element/Note.php new file mode 100644 index 00000000..ae303e1b --- /dev/null +++ b/src/PhpWord/Writer/HTML/Element/Note.php @@ -0,0 +1,37 @@ +getParentWriter()->getNotes()) + 1; + $prefix = ($this->element instanceof Endnote) ? 'endnote' : 'footnote'; + $noteMark = $prefix . '-' . $this->element->getRelationId(); + $this->getParentWriter()->addNote($noteId, $noteMark); + $html = "{$noteId}"; + + return $html; + } +} diff --git a/src/PhpWord/Writer/HTML/Element/PageBreak.php b/src/PhpWord/Writer/HTML/Element/PageBreak.php new file mode 100644 index 00000000..b05e43ca --- /dev/null +++ b/src/PhpWord/Writer/HTML/Element/PageBreak.php @@ -0,0 +1,19 @@ +element->getRows(); + $rowCount = count($rows); + if ($rowCount > 0) { + $html .= '
"; + } + if ($fStyle) { + $attribute = $fStyleIsObject ? 'style' : 'class'; + $html .= ""; + } + $html .= htmlspecialchars($this->element->getText()); + if ($fStyle) { + $html .= ''; + } + if ($pStyle && !$this->withoutP) { + $html .= '
' . PHP_EOL; + } + + return $html; + } +} diff --git a/src/PhpWord/Writer/HTML/Element/TextBreak.php b/src/PhpWord/Writer/HTML/Element/TextBreak.php new file mode 100644 index 00000000..ed8f9f71 --- /dev/null +++ b/src/PhpWord/Writer/HTML/Element/TextBreak.php @@ -0,0 +1,34 @@ +withoutP) { + $html = '' . PHP_EOL; + } + + return $html; + } +} diff --git a/src/PhpWord/Writer/HTML/Element/TextRun.php b/src/PhpWord/Writer/HTML/Element/TextRun.php new file mode 100644 index 00000000..05d40d89 --- /dev/null +++ b/src/PhpWord/Writer/HTML/Element/TextRun.php @@ -0,0 +1,53 @@ +element->getElements(); + if (count($elements) > 0) { + // Paragraph style + $pStyle = $this->element->getParagraphStyle(); + $pStyleIsObject = ($pStyle instanceof Paragraph); + if ($pStyleIsObject) { + $styleWriter = new StyleWriter($this->parentWriter, $pStyle); + $pStyle = $styleWriter->write(); + } + $tag = $this->withoutP ? 'span' : 'p'; + $attribute = $pStyleIsObject ? 'style' : 'class'; + $html .= "<{$tag} {$attribute}=\"{$pStyle}\">"; + foreach ($elements as $element) { + $elementWriter = new Element($element, true); + $elementWriter->setParentWriter($this->parentWriter); + $html .= $elementWriter->write(); + } + $html .= "{$tag}>"; + $html .= PHP_EOL; + } + + return $html; + } +} diff --git a/src/PhpWord/Writer/HTML/Element/Title.php b/src/PhpWord/Writer/HTML/Element/Title.php new file mode 100644 index 00000000..1395afed --- /dev/null +++ b/src/PhpWord/Writer/HTML/Element/Title.php @@ -0,0 +1,32 @@ +element->getDepth(); + $text = htmlspecialchars($this->element->getText()); + $html = "<{$tag}>{$text}{$tag}>" . PHP_EOL; + + return $html; + } +} diff --git a/src/PhpWord/Writer/HTML/Style/Font.php b/src/PhpWord/Writer/HTML/Style/Font.php new file mode 100644 index 00000000..d5dc66dc --- /dev/null +++ b/src/PhpWord/Writer/HTML/Style/Font.php @@ -0,0 +1,60 @@ +style->getName()) { + $css['font-family'] = "'" . $this->style->getName() . "'"; + } + if (PhpWord::DEFAULT_FONT_SIZE != $this->style->getSize()) { + $css['font-size'] = $this->style->getSize() . 'pt'; + } + if (PhpWord::DEFAULT_FONT_COLOR != $this->style->getColor()) { + $css['color'] = '#' . $this->style->getColor(); + } + $css['background'] = $this->style->getFgColor(); + if ($this->style->getBold()) { + $css['font-weight'] = 'bold'; + } + if ($this->style->getItalic()) { + $css['font-style'] = 'italic'; + } + if ($this->style->getSuperScript()) { + $css['vertical-align'] = 'super'; + } elseif ($this->style->getSubScript()) { + $css['vertical-align'] = 'sub'; + } + $css['text-decoration'] = ''; + if ($this->style->getUnderline() != \PhpOffice\PhpWord\Style\Font::UNDERLINE_NONE) { + $css['text-decoration'] .= 'underline '; + } + if ($this->style->getStrikethrough()) { + $css['text-decoration'] .= 'line-through '; + } + + return $this->assembleCss($css, $this->curlyBracket); + } +} diff --git a/src/PhpWord/Writer/HTML/Style/Paragraph.php b/src/PhpWord/Writer/HTML/Style/Paragraph.php new file mode 100644 index 00000000..c3ae83e1 --- /dev/null +++ b/src/PhpWord/Writer/HTML/Style/Paragraph.php @@ -0,0 +1,33 @@ +style->getAlign()) { + $css['text-align'] = $this->style->getAlign(); + } + + return $this->assembleCss($css, $this->curlyBracket); + } +} diff --git a/src/PhpWord/Writer/HTML/Style/Style.php b/src/PhpWord/Writer/HTML/Style/Style.php new file mode 100644 index 00000000..f2e6544c --- /dev/null +++ b/src/PhpWord/Writer/HTML/Style/Style.php @@ -0,0 +1,95 @@ +parentWriter = $parentWriter; + $this->style = $style; + $this->curlyBracket = $curlyBracket; + } + + /** + * Write element + * + * @return string + */ + public function write() + { + $css = ''; + $styleType = str_replace('PhpOffice\\PhpWord\\Style\\', '', get_class($this->style)); + $styleWriterClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Style\\' . $styleType; + if (class_exists($styleWriterClass) === true) { + $styleWriter = new $styleWriterClass($this->parentWriter, $this->style, $this->curlyBracket); + $css = $styleWriter->write(); + } + + return $css; + } + + /** + * Takes array where of CSS properties / values and converts to CSS string + * + * @param array $css + * @param boolean $curlyBracket + * @return string + */ + public function assembleCss($css, $curlyBracket = false) + { + $pairs = array(); + foreach ($css as $key => $value) { + if ($value != '') { + $pairs[] = $key . ': ' . $value; + } + } + $string = implode('; ', $pairs); + if ($curlyBracket) { + $string = '{ ' . $string . ' }'; + } + + return $string; + } +}