element instanceof \PhpOffice\PhpWord\Element\Text) { return; } /** @var \PhpOffice\PhpWord\Style\Font $fontStyle Scrutinizer type hint */ $fontStyle = $this->getFontStyle($this->element); /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ $parentWriter = $this->parentWriter; $content = ''; $content .= $this->writeParagraphStyle($this->element); $content .= '{'; $content .= $this->writeFontStyle($fontStyle); if ($fontStyle || $parentWriter->getLastParagraphStyle() != '') { $content .= ' '; } $content .= String::toUnicode($this->element->getText()); $content .= '}'; // Remarked to test using closure {} to avoid closing tags // @since 0.11.0 // $content .= $this->writeFontStyleClosing($fontStyle); if (!$this->withoutP) { $content .= '\par' . PHP_EOL; } return $content; } /** * Write paragraph style * * @return string */ private function writeParagraphStyle(TextElement $element) { /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ $parentWriter = $this->parentWriter; $content = ''; // Get paragraph style $paragraphStyle = $element->getParagraphStyle(); if (is_string($paragraphStyle)) { $paragraphStyle = Style::getStyle($paragraphStyle); } // Write style when applicable if ($paragraphStyle && !$this->withoutP) { if ($parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) { $parentWriter->setLastParagraphStyle($element->getParagraphStyle()); $styleWriter = new ParagraphStyleWriter($paragraphStyle); $content = $styleWriter->write(); } else { $parentWriter->setLastParagraphStyle(); } } else { $parentWriter->setLastParagraphStyle(); } return $content; } /** * Write font style beginning * * @param mixed $style * @return string */ private function writeFontStyle($style) { if (!$style instanceof FontStyle) { return ''; } /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Scrutinizer type hint */ $parentWriter = $this->parentWriter; // Create style writer and set color/name index $styleWriter = new FontStyleWriter($style); if ($style->getColor() != null) { $colorIndex = array_search($style->getColor(), $parentWriter->getColorTable()); if ($colorIndex !== false) { $styleWriter->setColorIndex($colorIndex + 1); } } if ($style->getName() != null) { $fontIndex = array_search($style->getName(), $parentWriter->getFontTable()); if ($fontIndex !== false) { $styleWriter->setNameIndex($fontIndex + 1); } } // Write style $content = $styleWriter->write(); return $content; } /** * Write font style ending * * @param \PhpOffice\PhpWord\Style\Font $style * @return string */ private function writeFontStyleClosing($style) { if (!$style instanceof FontStyle) { return ''; } $styleWriter = new FontStyleWriter($style); $content = $styleWriter->writeClosing(); return $content; } /** * Get font style * * @return \PhpOffice\PhpWord\Style\Font */ private function getFontStyle(TextElement $element) { $fontStyle = $element->getFontStyle(); if (is_string($fontStyle)) { $fontStyle = Style::getStyle($fontStyle); } return $fontStyle; } }