From 5bf76293e03d018e1fb19e28280098ac4917fed4 Mon Sep 17 00:00:00 2001 From: White <83274201+warmbook@users.noreply.github.com> Date: Fri, 7 Mar 2025 07:52:04 +0000 Subject: [PATCH 1/2] Make styles to work as a instance property of PhpWord, and use Style::$styles as default values --- src/PhpWord/Element/ListItem.php | 10 + src/PhpWord/Element/ListItemRun.php | 10 + src/PhpWord/Element/Title.php | 25 ++- src/PhpWord/PhpWord.php | 196 +++++++++++++++--- src/PhpWord/Style/ListItem.php | 42 +++- src/PhpWord/Writer/HTML/Element/Text.php | 2 +- src/PhpWord/Writer/HTML/Part/Head.php | 2 +- .../Writer/ODText/Part/AbstractPart.php | 2 +- src/PhpWord/Writer/ODText/Part/Content.php | 8 +- src/PhpWord/Writer/ODText/Part/Styles.php | 5 +- src/PhpWord/Writer/ODText/Style/Paragraph.php | 2 +- src/PhpWord/Writer/PDF/TCPDF.php | 2 +- .../Writer/RTF/Element/AbstractElement.php | 4 +- src/PhpWord/Writer/RTF/Element/Table.php | 2 +- src/PhpWord/Writer/RTF/Element/Title.php | 2 +- src/PhpWord/Writer/RTF/Part/Header.php | 2 +- .../Word2007/Element/AbstractElement.php | 2 +- src/PhpWord/Writer/Word2007/Element/Image.php | 2 +- .../Writer/Word2007/Element/ListItem.php | 2 +- .../Writer/Word2007/Element/ListItemRun.php | 2 +- src/PhpWord/Writer/Word2007/Element/TOC.php | 4 +- .../Writer/Word2007/Part/Footnotes.php | 2 +- .../Writer/Word2007/Part/Numbering.php | 2 +- src/PhpWord/Writer/Word2007/Part/Styles.php | 16 +- .../Writer/Word2007/Style/AbstractStyle.php | 29 ++- src/PhpWord/Writer/Word2007/Style/Font.php | 2 +- .../Writer/Word2007/Style/Paragraph.php | 2 +- 27 files changed, 316 insertions(+), 65 deletions(-) diff --git a/src/PhpWord/Element/ListItem.php b/src/PhpWord/Element/ListItem.php index bedfd110b6..052544da2e 100644 --- a/src/PhpWord/Element/ListItem.php +++ b/src/PhpWord/Element/ListItem.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\Text as SharedText; use PhpOffice\PhpWord\Style\ListItem as ListItemStyle; @@ -69,6 +70,15 @@ public function __construct($text, $depth = 0, $fontStyle = null, $listStyle = n } } + /** + * Set PhpWord as reference. + */ + public function setPhpWord(?PhpWord $phpWord = null): void + { + parent::setPhpWord($phpWord); + $this->style->setPhpWord($phpWord); + } + /** * Get style. * diff --git a/src/PhpWord/Element/ListItemRun.php b/src/PhpWord/Element/ListItemRun.php index 01d0947b81..5547cdb3d7 100644 --- a/src/PhpWord/Element/ListItemRun.php +++ b/src/PhpWord/Element/ListItemRun.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style\ListItem as ListItemStyle; /** @@ -64,6 +65,15 @@ public function __construct($depth = 0, $listStyle = null, $paragraphStyle = nul parent::__construct($paragraphStyle); } + /** + * Set PhpWord as reference. + */ + public function setPhpWord(?PhpWord $phpWord = null): void + { + parent::setPhpWord($phpWord); + $this->style->setPhpWord($phpWord); + } + /** * Get ListItem style. * diff --git a/src/PhpWord/Element/Title.php b/src/PhpWord/Element/Title.php index 89f438a5ef..537216754d 100644 --- a/src/PhpWord/Element/Title.php +++ b/src/PhpWord/Element/Title.php @@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Element; use InvalidArgumentException; +use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\Text as SharedText; use PhpOffice\PhpWord\Style; @@ -79,16 +80,32 @@ public function __construct($text, $depth = 1, ?int $pageNumber = null) } $this->depth = $depth; - $styleName = $depth === 0 ? 'Title' : "Heading_{$this->depth}"; - if (array_key_exists($styleName, Style::getStyles())) { - $this->style = str_replace('_', '', $styleName); - } + $this->setStyleByDepth(Style::getStyles()); if ($pageNumber !== null) { $this->pageNumber = $pageNumber; } } + private function setStyleByDepth($styles) + { + $styleName = $this->depth === 0 ? 'Title' : "Heading_{$this->depth}"; + if (array_key_exists($styleName, $styles)) { + $this->style = str_replace('_', '', $styleName); + } + } + + /** + * Set PhpWord as reference. + */ + public function setPhpWord(?PhpWord $phpWord = null): void + { + parent::setPhpWord($phpWord); + if ($phpWord instanceof PhpWord) { + $this->setStyleByDepth($phpWord->getStyles()); + } + } + /** * Get Title Text content. * diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index c3200fe857..85c960e32f 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -21,6 +21,7 @@ use BadMethodCallException; use PhpOffice\PhpWord\Element\Section; use PhpOffice\PhpWord\Exception\Exception; +use PhpOffice\PhpWord\Style\AbstractStyle; /** * PHPWord main class. @@ -67,6 +68,13 @@ class PhpWord * @since 0.12.0 */ private $metadata = []; + + /** + * Style register. + * + * @var array + */ + private $styles = []; /** * Create new instance. @@ -77,7 +85,6 @@ public function __construct() { // Reset Media and styles Media::resetElements(); - Style::resetStyles(); Settings::setDefaultRtl(null); // Collection @@ -111,7 +118,6 @@ public function __call($function, $args) $getCollection = []; $addCollection = []; - $addStyle = []; $collections = ['Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart', 'Comment']; foreach ($collections as $collection) { @@ -119,11 +125,6 @@ public function __call($function, $args) $addCollection[] = strtolower("add{$collection}"); } - $styles = ['Paragraph', 'Font', 'Table', 'Numbering', 'Link', 'Title']; - foreach ($styles as $style) { - $addStyle[] = strtolower("add{$style}Style"); - } - // Run get collection method if (in_array($function, $getCollection)) { $key = ucfirst(str_replace('get', '', $function)); @@ -140,11 +141,6 @@ public function __call($function, $args) return $collectionObject->addItem($args[0] ?? null); } - // Run add style method - if (in_array($function, $addStyle)) { - return forward_static_call_array(['PhpOffice\\PhpWord\\Style', $function], $args); - } - // Exception throw new BadMethodCallException("Method $function is not defined."); } @@ -311,18 +307,6 @@ public function setDefaultFontSize($fontSize): void Settings::setDefaultFontSize($fontSize); } - /** - * Set default paragraph style definition to styles.xml. - * - * @param array $styles Paragraph style definition - * - * @return Style\Paragraph - */ - public function setDefaultParagraphStyle($styles) - { - return Style::setDefaultParagraphStyle($styles); - } - /** * Save to file or download. * @@ -408,4 +392,168 @@ public function setDocumentProperties($documentProperties) return $this; } + + /** + * Add paragraph style. + * + * @param string $styleName + * @param AbstractStyle|array $styles + * + * @return \PhpOffice\PhpWord\Style\Paragraph + */ + public function addParagraphStyle($styleName, $styles) + { + return $this->setStyleValues($styleName, new Style\Paragraph(), $styles); + } + + /** + * Add font style. + * + * @param string $styleName + * @param AbstractStyle|array $fontStyle + * @param AbstractStyle|array $paragraphStyle + * + * @return \PhpOffice\PhpWord\Style\Font + */ + public function addFontStyle($styleName, $fontStyle, $paragraphStyle = null) + { + return $this->setStyleValues($styleName, new Style\Font('text', $paragraphStyle), $fontStyle); + } + + /** + * Add link style. + * + * @param string $styleName + * @param AbstractStyle|array $styles + * + * @return \PhpOffice\PhpWord\Style\Font + */ + public function addLinkStyle($styleName, $styles) + { + return $this->setStyleValues($styleName, new Style\Font('link'), $styles); + } + + /** + * Add numbering style. + * + * @param string $styleName + * @param AbstractStyle|array $styleValues + * + * @return \PhpOffice\PhpWord\Style\Numbering + * + * @since 0.10.0 + */ + public function addNumberingStyle($styleName, $styleValues) + { + return $this->setStyleValues($styleName, new Style\Numbering(), $styleValues); + } + + /** + * Add title style. + * + * @param null|int $depth Provide null to set title font + * @param AbstractStyle|array $fontStyle + * @param AbstractStyle|array $paragraphStyle + * + * @return \PhpOffice\PhpWord\Style\Font + */ + public function addTitleStyle($depth, $fontStyle, $paragraphStyle = null) + { + if (empty($depth)) { + $styleName = 'Title'; + } else { + $styleName = "Heading_{$depth}"; + } + + return $this->setStyleValues($styleName, new Style\Font('title', $paragraphStyle), $fontStyle); + } + + /** + * Add table style. + * + * @param string $styleName + * @param array $styleTable + * @param null|array $styleFirstRow + * + * @return \PhpOffice\PhpWord\Style\Table + */ + public function addTableStyle($styleName, $styleTable, $styleFirstRow = null) + { + return $this->setStyleValues($styleName, new Style\Table($styleTable, $styleFirstRow), null); + } + + /** + * Set default paragraph style. + * + * @param AbstractStyle|array $styles Paragraph style definition + * + * @return \PhpOffice\PhpWord\Style\Paragraph + */ + public function setDefaultParagraphStyle($styles) + { + return $this->addParagraphStyle('Normal', $styles); + } + + /** + * Get all styles. + * + * @return AbstractStyle[] + */ + public function getStyles() + { + $styles = Style::getStyles(); + $index = Style::countStyles() + 1; + foreach ($this->styles AS $name => $style) { + if (isset($styles[$name])) { + $style->setIndex($styles[$name]->getIndex()); + } else { + $style->setIndex($index); + $index ++; + } + $styles[$name] = $style; + } + return $styles; + } + + /** + * Get style by name. + * + * @param string $styleName + * + * @return ?AbstractStyle Paragraph|Font|Table|Numbering + */ + public function getStyle($styleName) + { + return $this->styles[$styleName] ?? Style::getStyle($styleName); + } + + /** + * Set style values and put it to style collection. + * + * The $styleValues could be an array or object + * + * @param string $name + * @param AbstractStyle $style + * @param AbstractStyle|array $value + * + * @return AbstractStyle + */ + private function setStyleValues($name, $style, $value = null) + { + if (!isset($this->styles[$name])) { + if ($value !== null) { + if (is_array($value)) { + $style->setStyleByArray($value); + } elseif ($value instanceof AbstractStyle) { + if (get_class($style) == get_class($value)) { + $style = $value; + } + } + } + $style->setStyleName($name); + $this->styles[$name] = $style; + } + + return $this->styles[$name]; + } } diff --git a/src/PhpWord/Style/ListItem.php b/src/PhpWord/Style/ListItem.php index e34aeb7c7c..13161280b8 100644 --- a/src/PhpWord/Style/ListItem.php +++ b/src/PhpWord/Style/ListItem.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; /** @@ -36,6 +37,13 @@ class ListItem extends AbstractStyle const TYPE_NUMBER_NESTED = 8; const TYPE_ALPHANUM = 9; + /** + * PhpWord object. + * + * @var ?PhpWord + */ + private $phpWord; + /** * Legacy list type. * @@ -74,6 +82,30 @@ public function __construct($numStyle = null) $this->setListType(); } } + + /** + * Set PhpWord as reference. + */ + public function setPhpWord(?PhpWord $phpWord = null): void + { + $this->phpWord = $phpWord; + } + + /** + * Get style by name. + * + * @param string $styleName + * + * @return ?AbstractStyle Paragraph|Font|Table|Numbering + */ + private function getGlobalStyle($styleName) + { + if (isset($this->phpWord)) { + return $this->phpWord->getStyle($styleName); + } + + return Style::getStyle($styleName); + } /** * Get List Type. @@ -125,7 +157,7 @@ public function getNumStyle() public function setNumStyle($value) { $this->numStyle = $value; - $numStyleObject = Style::getStyle($this->numStyle); + $numStyleObject = $this->getGlobalStyle($this->numStyle); if ($numStyleObject instanceof Numbering) { $this->numId = $numStyleObject->getIndex(); $numStyleObject->setNumId($this->numId); @@ -171,7 +203,7 @@ private function getListTypeStyle() $numStyle .= 'NumId' . $this->numId; } - if (Style::getStyle($numStyle) !== null) { + if ($this->getGlobalStyle($numStyle) !== null) { $this->setNumStyle($numStyle); return; @@ -281,7 +313,11 @@ private function getListTypeStyle() } $style['levels'][$key] = $level; } - Style::addNumberingStyle($numStyle, $style); + if (isset($this->phpWord)) { + $this->phpWord->addNumberingStyle($numStyle, $style); + } else { + Style::addNumberingStyle($numStyle, $style); + } $this->setNumStyle($numStyle); } } diff --git a/src/PhpWord/Writer/HTML/Element/Text.php b/src/PhpWord/Writer/HTML/Element/Text.php index 7be95a5c76..f7b90b6e59 100644 --- a/src/PhpWord/Writer/HTML/Element/Text.php +++ b/src/PhpWord/Writer/HTML/Element/Text.php @@ -263,7 +263,7 @@ private function processFontStyle(): void $attributeStyle = ' class="' . $fontStyle . '"'; // Attribute Lang /** @var Font $cssClassStyle */ - $cssClassStyle = Style::getStyle($fontStyle); + $cssClassStyle = $this->parentWriter->getPhpWord()->getStyle($fontStyle); if ($cssClassStyle !== null && method_exists($cssClassStyle, 'getLang')) { $lang = $cssClassStyle->getLang(); } diff --git a/src/PhpWord/Writer/HTML/Part/Head.php b/src/PhpWord/Writer/HTML/Part/Head.php index 79235e1c4a..fbe78f40b8 100644 --- a/src/PhpWord/Writer/HTML/Part/Head.php +++ b/src/PhpWord/Writer/HTML/Part/Head.php @@ -127,7 +127,7 @@ private function writeStyles(): string } // Custom styles - $customStyles = Style::getStyles(); + $customStyles = $this->getParentWriter()->getPhpWord()->getStyles(); if (is_array($customStyles)) { foreach ($customStyles as $name => $style) { $styleParagraph = null; diff --git a/src/PhpWord/Writer/ODText/Part/AbstractPart.php b/src/PhpWord/Writer/ODText/Part/AbstractPart.php index 458831d3ad..b7e425fbcd 100644 --- a/src/PhpWord/Writer/ODText/Part/AbstractPart.php +++ b/src/PhpWord/Writer/ODText/Part/AbstractPart.php @@ -81,7 +81,7 @@ protected function writeFontFaces(XMLWriter $xmlWriter): void { $xmlWriter->startElement('office:font-face-decls'); $fontTable = []; - $styles = Style::getStyles(); + $styles = $this->getParentWriter()->getPhpWord()->getStyles(); $numFonts = 0; if (count($styles) > 0) { foreach ($styles as $style) { diff --git a/src/PhpWord/Writer/ODText/Part/Content.php b/src/PhpWord/Writer/ODText/Part/Content.php index de82ed96d1..65352af887 100644 --- a/src/PhpWord/Writer/ODText/Part/Content.php +++ b/src/PhpWord/Writer/ODText/Part/Content.php @@ -179,7 +179,8 @@ private function writeAutoStyles(XMLWriter $xmlWriter): void */ private function writeTextStyles(XMLWriter $xmlWriter): void { - $styles = Style::getStyles(); + $phpWord = $this->getParentWriter()->getPhpWord(); + $styles = $phpWord->getStyles(); $paragraphStyleCount = 0; $style = new Paragraph(); @@ -188,7 +189,7 @@ private function writeTextStyles(XMLWriter $xmlWriter): void $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); $styleWriter->write(); - $sects = $this->getParentWriter()->getPhpWord()->getSections(); + $sects = $phpWord->getSections(); $countsects = count($sects); for ($i = 0; $i < $countsects; ++$i) { $iplus1 = $i + 1; @@ -265,6 +266,7 @@ private function getAutoStyles(PhpWord $phpWord): void */ private function getContainerStyle($container, &$paragraphStyleCount, &$fontStyleCount): void { + $phpWord = $this->getParentWriter()->getPhpWord(); $elements = $container->getElements(); foreach ($elements as $element) { if ($element instanceof TextRun) { @@ -286,7 +288,7 @@ private function getContainerStyle($container, &$paragraphStyleCount, &$fontStyl } elseif ($element instanceof Table) { $style = $element->getStyle(); if (is_string($style)) { - $style = Style::getStyle($style); + $style = $phpWord->getStyle($style); } if ($style === null) { $style = new TableStyle(); diff --git a/src/PhpWord/Writer/ODText/Part/Styles.php b/src/PhpWord/Writer/ODText/Part/Styles.php index f8ec843894..6f673d8b7f 100644 --- a/src/PhpWord/Writer/ODText/Part/Styles.php +++ b/src/PhpWord/Writer/ODText/Part/Styles.php @@ -120,14 +120,15 @@ private function writeDefault(XMLWriter $xmlWriter): void */ private function writeNamed(XMLWriter $xmlWriter): void { - $styles = Style::getStyles(); + $phpWord = $this->getParentWriter()->getPhpWord(); + $styles = $phpWord->getStyles(); if (count($styles) > 0) { foreach ($styles as $style) { if ($style->isAuto() === false) { $styleClass = str_replace('\\Style\\', '\\Writer\\ODText\\Style\\', get_class($style)); if (class_exists($styleClass)) { /** @var \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle $styleWriter Type hint */ - $styleWriter = new $styleClass($xmlWriter, $style); + $styleWriter = new $styleClass($xmlWriter, $style, $phpWord); $styleWriter->write(); } } diff --git a/src/PhpWord/Writer/ODText/Style/Paragraph.php b/src/PhpWord/Writer/ODText/Style/Paragraph.php index 99963bd4a3..41fbc286f9 100644 --- a/src/PhpWord/Writer/ODText/Style/Paragraph.php +++ b/src/PhpWord/Writer/ODText/Style/Paragraph.php @@ -74,7 +74,7 @@ public function write(): void } elseif (substr($styleName, 0, 2) === 'HD') { $styleAuto = true; $psm = 'Heading_' . substr($styleName, 2); - $stylep = Style::getStyle($psm); + $stylep = $this->getGlobalStyle($psm); if ($stylep instanceof Style\Font) { if (method_exists($stylep, 'getParagraph')) { $stylep = $stylep->getParagraph(); diff --git a/src/PhpWord/Writer/PDF/TCPDF.php b/src/PhpWord/Writer/PDF/TCPDF.php index 93bdd58528..23ecf7a8fa 100644 --- a/src/PhpWord/Writer/PDF/TCPDF.php +++ b/src/PhpWord/Writer/PDF/TCPDF.php @@ -71,7 +71,7 @@ protected function createExternalWriterInstance($orientation, $unit, $paperSize) protected function prepareToWrite(TCPDFBase $pdf): void { $pdf->AddPage(); - $customStyles = Style::getStyles(); + $customStyles = $this->getParentWriter()->getPhpWord()->getStyles(); $normal = $customStyles['Normal'] ?? null; if ($normal instanceof Style\Paragraph) { $before = $normal->getSpaceBefore(); diff --git a/src/PhpWord/Writer/RTF/Element/AbstractElement.php b/src/PhpWord/Writer/RTF/Element/AbstractElement.php index e007e6aa26..33b324a88f 100644 --- a/src/PhpWord/Writer/RTF/Element/AbstractElement.php +++ b/src/PhpWord/Writer/RTF/Element/AbstractElement.php @@ -106,7 +106,7 @@ protected function getStyles(): void if (method_exists($element, 'getFontStyle')) { $this->fontStyle = $element->getFontStyle(); if (is_string($this->fontStyle)) { - $this->fontStyle = Style::getStyle($this->fontStyle); + $this->fontStyle = $parentWriter->getPhpWord()->getStyle($this->fontStyle); } } @@ -114,7 +114,7 @@ protected function getStyles(): void if (method_exists($element, 'getParagraphStyle')) { $this->paragraphStyle = $element->getParagraphStyle(); if (is_string($this->paragraphStyle)) { - $this->paragraphStyle = Style::getStyle($this->paragraphStyle); + $this->paragraphStyle = $parentWriter->getPhpWord()->getStyle($this->paragraphStyle); } if ($this->paragraphStyle !== null && !$this->withoutP) { diff --git a/src/PhpWord/Writer/RTF/Element/Table.php b/src/PhpWord/Writer/RTF/Element/Table.php index 3b08a5db86..ec38f90007 100644 --- a/src/PhpWord/Writer/RTF/Element/Table.php +++ b/src/PhpWord/Writer/RTF/Element/Table.php @@ -88,7 +88,7 @@ private function writeRowDef(RowElement $row) $content = ''; $tableStyle = $this->element->getStyle(); if (is_string($tableStyle)) { - $tableStyle = Style::getStyle($tableStyle); + $tableStyle = $this->element->getPhpWord()->getStyle($tableStyle); if (!($tableStyle instanceof TableStyle)) { $tableStyle = null; } diff --git a/src/PhpWord/Writer/RTF/Element/Title.php b/src/PhpWord/Writer/RTF/Element/Title.php index 06266897b3..396cde49e8 100644 --- a/src/PhpWord/Writer/RTF/Element/Title.php +++ b/src/PhpWord/Writer/RTF/Element/Title.php @@ -31,7 +31,7 @@ protected function getStyles(): void $element = $this->element; $style = $element->getStyle(); $style = str_replace('Heading', 'Heading_', $style ?? ''); - $style = \PhpOffice\PhpWord\Style::getStyle($style); + $style = $element->getPhpWord()->getStyle($style); if ($style instanceof \PhpOffice\PhpWord\Style\Font) { $this->fontStyle = $style; $pstyle = $style->getParagraph(); diff --git a/src/PhpWord/Writer/RTF/Part/Header.php b/src/PhpWord/Writer/RTF/Part/Header.php index 97644fe4ac..894ee4094f 100644 --- a/src/PhpWord/Writer/RTF/Part/Header.php +++ b/src/PhpWord/Writer/RTF/Part/Header.php @@ -190,7 +190,7 @@ private function registerFont(): void $this->fontTable[] = Settings::getDefaultFontName(); // Search named styles - $styles = Style::getStyles(); + $styles = $phpWord->getStyles(); foreach ($styles as $style) { $this->registerFontItems($style); } diff --git a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php index 5743c8c7c7..596a9eeda3 100644 --- a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -185,7 +185,7 @@ private function writeTextStyle($styleType): void $styleObject = $this->element->$method(); /** @var \PhpOffice\PhpWord\Writer\Word2007\Style\AbstractStyle $styleWriter Type Hint */ - $styleWriter = new $class($this->xmlWriter, $styleObject); + $styleWriter = new $class($this->xmlWriter, $styleObject, $this->element->getPhpWord()); if (method_exists($styleWriter, 'setIsInline')) { $styleWriter->setIsInline(true); } diff --git a/src/PhpWord/Writer/Word2007/Element/Image.php b/src/PhpWord/Writer/Word2007/Element/Image.php index 7835f32ad5..ed72c57091 100644 --- a/src/PhpWord/Writer/Word2007/Element/Image.php +++ b/src/PhpWord/Writer/Word2007/Element/Image.php @@ -72,7 +72,7 @@ private function writeImage(XMLWriter $xmlWriter, ImageElement $element): void if ($position && $style->getWrap() == FrameStyle::WRAP_INLINE) { $fontStyle = new FontStyle('text'); $fontStyle->setPosition($position); - $fontStyleWriter = new FontStyleWriter($xmlWriter, $fontStyle); + $fontStyleWriter = new FontStyleWriter($xmlWriter, $fontStyle, $element->getPhpWord()); $fontStyleWriter->write(); } diff --git a/src/PhpWord/Writer/Word2007/Element/ListItem.php b/src/PhpWord/Writer/Word2007/Element/ListItem.php index a91301cdc0..bef165841a 100644 --- a/src/PhpWord/Writer/Word2007/Element/ListItem.php +++ b/src/PhpWord/Writer/Word2007/Element/ListItem.php @@ -40,7 +40,7 @@ public function write(): void $textObject = $element->getTextObject(); - $styleWriter = new ParagraphStyleWriter($xmlWriter, $textObject->getParagraphStyle()); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $textObject->getParagraphStyle(), $element->getPhpWord()); $styleWriter->setWithoutPPR(true); $styleWriter->setIsInline(true); diff --git a/src/PhpWord/Writer/Word2007/Element/ListItemRun.php b/src/PhpWord/Writer/Word2007/Element/ListItemRun.php index a20912a0a8..b6c2df87af 100644 --- a/src/PhpWord/Writer/Word2007/Element/ListItemRun.php +++ b/src/PhpWord/Writer/Word2007/Element/ListItemRun.php @@ -60,7 +60,7 @@ private function writeParagraphProperties(ListItemRunElement $element): void $xmlWriter = $this->getXmlWriter(); $xmlWriter->startElement('w:pPr'); - $styleWriter = new ParagraphStyleWriter($xmlWriter, $element->getParagraphStyle()); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $element->getParagraphStyle(), $element->getPhpWord()); $styleWriter->setIsInline(true); $styleWriter->setWithoutPPR(true); $styleWriter->write(); diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index 44c6ba11fa..a3505b91d3 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -90,7 +90,7 @@ private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, Title $ti // Title text $xmlWriter->startElement('w:r'); if ($isObject) { - $styleWriter = new FontStyleWriter($xmlWriter, $fontStyle); + $styleWriter = new FontStyleWriter($xmlWriter, $fontStyle, $element->getPhpWord()); $styleWriter->write(); } $xmlWriter->startElement('w:t'); @@ -156,7 +156,7 @@ private function writeStyle(XMLWriter $xmlWriter, TOCElement $element, int $inde // Paragraph if ($isObject && null !== $fontStyle->getParagraph()) { - $styleWriter = new ParagraphStyleWriter($xmlWriter, $fontStyle->getParagraph()); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $fontStyle->getParagraph(), $element->getPhpWord()); $styleWriter->write(); } diff --git a/src/PhpWord/Writer/Word2007/Part/Footnotes.php b/src/PhpWord/Writer/Word2007/Part/Footnotes.php index 5e6e92a9db..fb5f65555f 100644 --- a/src/PhpWord/Writer/Word2007/Part/Footnotes.php +++ b/src/PhpWord/Writer/Word2007/Part/Footnotes.php @@ -146,7 +146,7 @@ protected function writeNote(XMLWriter $xmlWriter, $element): void $xmlWriter->startElement('w:p'); // Paragraph style - $styleWriter = new ParagraphStyleWriter($xmlWriter, $element->getParagraphStyle()); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $element->getParagraphStyle(), $element->getPhpWord()); $styleWriter->setIsInline(true); $styleWriter->write(); diff --git a/src/PhpWord/Writer/Word2007/Part/Numbering.php b/src/PhpWord/Writer/Word2007/Part/Numbering.php index ca29cd01d5..fcd28c1d78 100644 --- a/src/PhpWord/Writer/Word2007/Part/Numbering.php +++ b/src/PhpWord/Writer/Word2007/Part/Numbering.php @@ -38,7 +38,7 @@ class Numbering extends AbstractPart public function write() { $xmlWriter = $this->getXmlWriter(); - $styles = Style::getStyles(); + $styles = $this->getParentWriter()->getPhpWord()->getStyles(); $drawingSchema = 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'; $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index edf0314cc2..4d3f2254d3 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -51,7 +51,7 @@ public function write() $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); // Write default styles - $styles = Style::getStyles(); + $styles = $this->getParentWriter()->getPhpWord()->getStyles(); $this->writeDefaultStyles($xmlWriter, $styles); // Write styles @@ -132,15 +132,15 @@ private function writeDefaultStyles(XMLWriter $xmlWriter, $styles): void $normalStyle = $styles['Normal']; // w:pPr if ($normalStyle instanceof FontStyle && $normalStyle->getParagraph() != null) { - $styleWriter = new ParagraphStyleWriter($xmlWriter, $normalStyle->getParagraph()); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $normalStyle->getParagraph(), $phpWord); $styleWriter->write(); } elseif ($normalStyle instanceof ParagraphStyle) { - $styleWriter = new ParagraphStyleWriter($xmlWriter, $normalStyle); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $normalStyle, $phpWord); $styleWriter->write(); } // w:rPr - $styleWriter = new FontStyleWriter($xmlWriter, $normalStyle); + $styleWriter = new FontStyleWriter($xmlWriter, $normalStyle, $phpWord); $styleWriter->write(); } $xmlWriter->endElement(); // w:style @@ -216,15 +216,17 @@ private function writeFontStyle(XMLWriter $xmlWriter, $styleName, FontStyle $sty $xmlWriter->writeElementBlock('w:basedOn', 'w:val', $paragraphStyle->getBasedOn()); } } + + $phpWord = $this->getParentWriter()->getPhpWord(); // w:pPr if (null !== $paragraphStyle) { - $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle, $phpWord); $styleWriter->write(); } // w:rPr - $styleWriter = new FontStyleWriter($xmlWriter, $style); + $styleWriter = new FontStyleWriter($xmlWriter, $style, $phpWord); $styleWriter->write(); $xmlWriter->endElement(); @@ -254,7 +256,7 @@ private function writeParagraphStyle(XMLWriter $xmlWriter, $styleName, Paragraph $xmlWriter->writeElementIf(null !== $next, 'w:next', 'w:val', $next); // w:pPr - $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); + $styleWriter = new ParagraphStyleWriter($xmlWriter, $style, $this->getParentWriter()->getPhpWord()); $styleWriter->write(); $xmlWriter->endElement(); diff --git a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php index 9a30bebf0e..ea667e1945 100644 --- a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Style; +use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Shared\XMLWriter; @@ -42,6 +43,13 @@ abstract class AbstractStyle */ protected $style; + /** + * PhpWord object. + * + * @var ?PhpWord + */ + private $phpWord; + /** * Write style. */ @@ -52,10 +60,11 @@ abstract public function write(); * * @param \PhpOffice\PhpWord\Style\AbstractStyle|string $style */ - public function __construct(XMLWriter $xmlWriter, $style = null) + public function __construct(XMLWriter $xmlWriter, $style = null, ?PhpWord $phpWord = null) { $this->xmlWriter = $xmlWriter; $this->style = $style; + $this->phpWord = $phpWord; } /** @@ -77,6 +86,22 @@ protected function getStyle() { return $this->style; } + + /** + * Get style by name. + * + * @param string $styleName + * + * @return ?AbstractStyle Paragraph|Font|Table|Numbering + */ + protected function getGlobalStyle($styleName) + { + if (isset($this->phpWord)) { + return $this->phpWord->getStyle($styleName); + } + + return \PhpOffice\Style::getStyle($styleName); + } /** * Convert twip value. @@ -116,7 +141,7 @@ protected function writeChildStyle(XMLWriter $xmlWriter, $name, $value): void $class = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Style\\' . $name; /** @var AbstractStyle $writer */ - $writer = new $class($xmlWriter, $value); + $writer = new $class($xmlWriter, $value, $this->phpWord); $writer->write(); } } diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index 623e8d5e72..c155d6be8f 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -45,7 +45,7 @@ public function write(): void $xmlWriter->startElement('w:rStyle'); $xmlWriter->writeAttribute('w:val', $this->style); $xmlWriter->endElement(); - $style = \PhpOffice\PhpWord\Style::getStyle($this->style); + $style = $this->getGlobalStyle($this->style); if ($style instanceof \PhpOffice\PhpWord\Style\Font) { $xmlWriter->writeElementIf($style->isRTL(), 'w:rtl'); } diff --git a/src/PhpWord/Writer/Word2007/Style/Paragraph.php b/src/PhpWord/Writer/Word2007/Style/Paragraph.php index 55f51a54d6..d748658d2a 100644 --- a/src/PhpWord/Writer/Word2007/Style/Paragraph.php +++ b/src/PhpWord/Writer/Word2007/Style/Paragraph.php @@ -173,7 +173,7 @@ private function writeNumbering(XMLWriter $xmlWriter, $numbering): void $numLevel = $numbering['level']; /** @var Style\Numbering $numbering */ - $numbering = Style::getStyle($numStyle); + $numbering = $this->getGlobalStyle($numStyle); if ($numStyle !== null && $numbering !== null) { $xmlWriter->startElement('w:numPr'); $xmlWriter->startElement('w:numId'); From 4470a044eeb64e7454b5c1bf919bf70e2c239557 Mon Sep 17 00:00:00 2001 From: White <83274201+warmbook@users.noreply.github.com> Date: Fri, 7 Mar 2025 08:23:18 +0000 Subject: [PATCH 2/2] wrong namespace --- src/PhpWord/Writer/Word2007/Style/AbstractStyle.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php index ea667e1945..f59dea9fb2 100644 --- a/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/Word2007/Style/AbstractStyle.php @@ -100,7 +100,7 @@ protected function getGlobalStyle($styleName) return $this->phpWord->getStyle($styleName); } - return \PhpOffice\Style::getStyle($styleName); + return \PhpOffice\PhpWord\Style::getStyle($styleName); } /**