From 3ef0f41c252aa3a3a4591bfa5660aaae8bedc389 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 8 Apr 2014 03:03:14 +0700 Subject: [PATCH] New `Style\AbstractStyle` and 'ODText\Base` --- CHANGELOG.md | 14 ++-- src/PhpWord/Style/AbstractStyle.php | 43 ++++++++++++ src/PhpWord/Style/Cell.php | 2 +- src/PhpWord/Style/Font.php | 19 +----- src/PhpWord/Style/Image.php | 30 +++------ src/PhpWord/Style/ListItem.php | 16 +---- src/PhpWord/Style/Paragraph.php | 2 +- src/PhpWord/Style/Row.php | 16 +---- src/PhpWord/Style/Section.php | 2 +- src/PhpWord/Style/TOC.php | 2 +- src/PhpWord/Style/Tab.php | 2 +- src/PhpWord/Style/Table.php | 2 +- src/PhpWord/Style/Tabs.php | 2 +- src/PhpWord/Writer/AbstractWriter.php | 52 +++++++++++--- src/PhpWord/Writer/ODText.php | 31 ++------- src/PhpWord/Writer/ODText/Base.php | 93 ++++++++++++++++++++++++++ src/PhpWord/Writer/ODText/Content.php | 62 ++--------------- src/PhpWord/Writer/ODText/Manifest.php | 2 +- src/PhpWord/Writer/ODText/Meta.php | 2 +- src/PhpWord/Writer/ODText/Styles.php | 61 +---------------- src/PhpWord/Writer/Word2007.php | 32 ++------- 21 files changed, 225 insertions(+), 262 deletions(-) create mode 100644 src/PhpWord/Style/AbstractStyle.php create mode 100644 src/PhpWord/Writer/ODText/Base.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e159009..bc43822b 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ## 0.9.2 - Not yet released +This release marked heavy refactorings on internal code structure with the creation of some abstract classes to reduce code duplication. `Element` subnamespace is introduced in this release to replace `Section`. + ### Features - Image: Get image dimensions without EXIF extension - @andrew-kzoo GH-184 @@ -48,14 +50,16 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ### Miscellaneous - Documentation: Simplify page level docblock - @ivanlanin GH-179 -- Writer: Refactor writer classes and make a new AbstractWriter abstract class - @ivanlanin GH-160 -- General: Refactor folders: Element and Exception - @ivanlanin GH-187 -- General: Remove legacy HashTable and ZipStreamWrapper and all related properties/methods - @ivanlanin GH-187 -- Element: Create new AbstractElement abstract class - @ivanlanin GH-187 +- Writer: Refactor writer classes and create a new `Write\AbstractWriter` abstract class - @ivanlanin GH-160 +- General: Refactor folders: `Element` and `Exception` - @ivanlanin GH-187 +- General: Remove legacy `HashTable` and `Shared\ZipStreamWrapper` and all related properties/methods - @ivanlanin GH-187 +- Element: New `AbstractElement` abstract class - @ivanlanin GH-187 - Media: Refactor media class to use one method for all docPart (section, header, footer, footnote) - @ivanlanin GH-187 - General: Remove underscore prefix from all private properties name - @ivanlanin GH-187 -- General: Move Section Settings to Style\Section - @ivanlanin GH-187 +- General: Move Section `Settings` to `Style\Section` - @ivanlanin GH-187 - General: Give `Abstract` prefix and `Interface` suffix for all abstract classes and interfaces as per [PHP-FIG recommendation](https://github.com/php-fig/fig-standards/blob/master/bylaws/002-psr-naming-conventions.md) - @ivanlanin GH-187 +- Style: New `Style\AbstractStyle` abstract class - @ivanlanin GH-187 +- Writer: New 'ODText\Base` class - @ivanlanin GH-187 ## 0.9.1 - 27 Mar 2014 diff --git a/src/PhpWord/Style/AbstractStyle.php b/src/PhpWord/Style/AbstractStyle.php new file mode 100644 index 00000000..c17ea6e2 --- /dev/null +++ b/src/PhpWord/Style/AbstractStyle.php @@ -0,0 +1,43 @@ +$method($value); + } + } +} diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index 1fbed6e7..2e1f9d79 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * Table cell style */ -class Cell +class Cell extends AbstractStyle { const TEXT_DIR_BTLR = 'btLr'; const TEXT_DIR_TBRL = 'tbRl'; diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 999fc3fa..2fef3a66 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -15,7 +15,7 @@ use PhpOffice\PhpWord\Exception\InvalidStyleException; /** * Font style */ -class Font +class Font extends AbstractStyle { const UNDERLINE_NONE = 'none'; const UNDERLINE_DASH = 'dash'; @@ -202,23 +202,6 @@ class Font return $this; } - /** - * Set style value - * - * @param string $key - * @param mixed $value - */ - public function setStyleValue($key, $value) - { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } - $method = 'set' . $key; - if (method_exists($this, $method)) { - $this->$method($value); - } - } - /** * Get font name * diff --git a/src/PhpWord/Style/Image.php b/src/PhpWord/Style/Image.php index e973f1ac..49060607 100644 --- a/src/PhpWord/Style/Image.php +++ b/src/PhpWord/Style/Image.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * Image and memory image style */ -class Image +class Image extends AbstractStyle { const WRAPPING_STYLE_INLINE = 'inline'; const WRAPPING_STYLE_SQUARE = 'square'; @@ -41,13 +41,6 @@ class Image */ private $align; - /** - * Wrapping style - * - * @var string - */ - private $wrappingStyle; - /** * Margin Top * @@ -62,6 +55,13 @@ class Image */ private $marginLeft; + /** + * Wrapping style + * + * @var string + */ + private $wrappingStyle; + /** * Create new image style */ @@ -75,20 +75,6 @@ class Image $this->setWrappingStyle(self::WRAPPING_STYLE_INLINE); } - /** - * Set style value - * - * @param string $key - * @param mixed $value - */ - public function setStyleValue($key, $value) - { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } - $this->$key = $value; - } - /** * Get width */ diff --git a/src/PhpWord/Style/ListItem.php b/src/PhpWord/Style/ListItem.php index 6681be16..ecc4274c 100644 --- a/src/PhpWord/Style/ListItem.php +++ b/src/PhpWord/Style/ListItem.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * List item style */ -class ListItem +class ListItem extends AbstractStyle { const TYPE_NUMBER = 7; const TYPE_NUMBER_NESTED = 8; @@ -34,20 +34,6 @@ class ListItem $this->listType = self::TYPE_BULLET_FILLED; } - /** - * Set style value - * - * @param string $key - * @param string $value - */ - public function setStyleValue($key, $value) - { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } - $this->$key = $value; - } - /** * Set List Type * diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index c55d9db1..8feb7924 100755 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Exception\InvalidStyleException; /** * Paragraph style */ -class Paragraph +class Paragraph extends AbstractStyle { const LINE_HEIGHT = 240; diff --git a/src/PhpWord/Style/Row.php b/src/PhpWord/Style/Row.php index 113baaf2..d4dc642f 100644 --- a/src/PhpWord/Style/Row.php +++ b/src/PhpWord/Style/Row.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * Table row style */ -class Row +class Row extends AbstractStyle { /** * Repeat table row on every new page @@ -42,20 +42,6 @@ class Row { } - /** - * Set style value - * - * @param string $key - * @param mixed $value - */ - public function setStyleValue($key, $value) - { - if (substr($key, 0, 1) == '_') { - $key = substr($key, 1); - } - $this->$key = $value; - } - /** * Set tblHeader * diff --git a/src/PhpWord/Style/Section.php b/src/PhpWord/Style/Section.php index 52352434..005ca1e5 100644 --- a/src/PhpWord/Style/Section.php +++ b/src/PhpWord/Style/Section.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * Section settings */ -class Section +class Section extends AbstractStyle { /** * Default Page Size Width diff --git a/src/PhpWord/Style/TOC.php b/src/PhpWord/Style/TOC.php index f49d9847..f7a752a9 100644 --- a/src/PhpWord/Style/TOC.php +++ b/src/PhpWord/Style/TOC.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * TOC style */ -class TOC +class TOC extends AbstractStyle { const TABLEADER_DOT = 'dot'; const TABLEADER_UNDERSCORE = 'underscore'; diff --git a/src/PhpWord/Style/Tab.php b/src/PhpWord/Style/Tab.php index 59757ea8..c28d8923 100644 --- a/src/PhpWord/Style/Tab.php +++ b/src/PhpWord/Style/Tab.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; /** * Tab style */ -class Tab +class Tab extends AbstractStyle { /** * Tab Stop Type diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index 178c2854..f7c98c36 100755 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -12,7 +12,7 @@ namespace PhpOffice\PhpWord\Style; /** * Table style */ -class Table +class Table extends AbstractStyle { /** * Style for first row diff --git a/src/PhpWord/Style/Tabs.php b/src/PhpWord/Style/Tabs.php index 497e09f1..66137c0e 100755 --- a/src/PhpWord/Style/Tabs.php +++ b/src/PhpWord/Style/Tabs.php @@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Shared\XMLWriter; /** * Tabs style */ -class Tabs +class Tabs extends AbstractStyle { /** * Tabs diff --git a/src/PhpWord/Writer/AbstractWriter.php b/src/PhpWord/Writer/AbstractWriter.php index b4f4b765..faa13ea6 100644 --- a/src/PhpWord/Writer/AbstractWriter.php +++ b/src/PhpWord/Writer/AbstractWriter.php @@ -11,6 +11,7 @@ namespace PhpOffice\PhpWord\Writer; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Settings; /** * Abstract writer class @@ -148,21 +149,21 @@ abstract class AbstractWriter implements WriterInterface /** * Get temporary file name * - * If $pFilename is php://output or php://stdout, make it a temporary file + * If $filename is php://output or php://stdout, make it a temporary file * - * @param string $pFilename + * @param string $filename * @return string */ - protected function getTempFile($pFilename) + protected function getTempFile($filename) { - $this->originalFilename = $pFilename; - if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - $pFilename = @tempnam(sys_get_temp_dir(), 'phpword_'); - if ($pFilename == '') { - $pFilename = $this->originalFilename; + $this->originalFilename = $filename; + if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') { + $filename = @tempnam(sys_get_temp_dir(), 'phpword_'); + if ($filename == '') { + $filename = $this->originalFilename; } } - $this->tempFilename = $pFilename; + $this->tempFilename = $filename; return $this->tempFilename; } @@ -181,4 +182,37 @@ abstract class AbstractWriter implements WriterInterface @unlink($this->tempFilename); } } + + /** + * Get ZipArchive object + * + * @param string $filename + * @return mixed ZipArchive object + */ + protected function getZipArchive($filename) + { + // Create new ZIP file and open it for writing + $zipClass = Settings::getZipClass(); + $objZip = new $zipClass(); + + // Retrieve OVERWRITE and CREATE constants from the instantiated zip class + // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP + $ro = new \ReflectionObject($objZip); + $zipOverWrite = $ro->getConstant('OVERWRITE'); + $zipCreate = $ro->getConstant('CREATE'); + + // Remove any existing file + if (file_exists($filename)) { + unlink($filename); + } + + // Try opening the ZIP file + if ($objZip->open($filename, $zipOverWrite) !== true) { + if ($objZip->open($filename, $zipCreate) !== true) { + throw new Exception("Could not open " . $filename . " for writing."); + } + } + + return $objZip; + } } diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index 7e1da98c..e3b82f4d 100755 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -47,35 +47,14 @@ class ODText extends AbstractWriter implements WriterInterface /** * Save PhpWord to file * - * @param string $pFilename + * @param string $filename * @throws Exception */ - public function save($pFilename = null) + public function save($filename = null) { if (!is_null($this->phpWord)) { - $pFilename = $this->getTempFile($pFilename); - - // Create new ZIP file and open it for writing - $zipClass = Settings::getZipClass(); - $objZip = new $zipClass(); - - // Retrieve OVERWRITE and CREATE constants from the instantiated zip class - // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP - $ro = new \ReflectionObject($objZip); - $zipOverWrite = $ro->getConstant('OVERWRITE'); - $zipCreate = $ro->getConstant('CREATE'); - - // Remove any existing file - if (file_exists($pFilename)) { - unlink($pFilename); - } - - // Try opening the ZIP file - if ($objZip->open($pFilename, $zipOverWrite) !== true) { - if ($objZip->open($pFilename, $zipCreate) !== true) { - throw new Exception("Could not open " . $pFilename . " for writing."); - } - } + $filename = $this->getTempFile($filename); + $objZip = $this->getZipArchive($filename); // Add mimetype to ZIP file //@todo Not in \ZipArchive::CM_STORE mode @@ -95,7 +74,7 @@ class ODText extends AbstractWriter implements WriterInterface // Close file if ($objZip->close() === false) { - throw new Exception("Could not close zip file $pFilename."); + throw new Exception("Could not close zip file $filename."); } $this->cleanupTempFile(); diff --git a/src/PhpWord/Writer/ODText/Base.php b/src/PhpWord/Writer/ODText/Base.php new file mode 100644 index 00000000..a1f0d315 --- /dev/null +++ b/src/PhpWord/Writer/ODText/Base.php @@ -0,0 +1,93 @@ +writeAttribute('office:version', '1.2'); + $xmlWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); + $xmlWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'); + $xmlWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'); + $xmlWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0'); + $xmlWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0'); + $xmlWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'); + $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); + $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); + $xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); + $xmlWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0'); + $xmlWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'); + $xmlWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0'); + $xmlWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0'); + $xmlWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML'); + $xmlWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0'); + $xmlWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0'); + $xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office'); + $xmlWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer'); + $xmlWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc'); + $xmlWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events'); + $xmlWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report'); + $xmlWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2'); + $xmlWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml'); + $xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#'); + $xmlWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table'); + $xmlWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/'); + } + + /** + * Write font faces declaration + */ + protected function writeFontFaces(XMLWriter $xmlWriter) + { + $xmlWriter->startElement('office:font-face-decls'); + $arrFonts = array(); + $styles = Style::getStyles(); + $numFonts = 0; + if (count($styles) > 0) { + foreach ($styles as $styleName => $style) { + // Font + if ($style instanceof Font) { + $numFonts++; + $name = $style->getName(); + if (!in_array($name, $arrFonts)) { + $arrFonts[] = $name; + + // style:font-face + $xmlWriter->startElement('style:font-face'); + $xmlWriter->writeAttribute('style:name', $name); + $xmlWriter->writeAttribute('svg:font-family', $name); + $xmlWriter->endElement(); + } + } + } + } + if (!in_array(PhpWord::DEFAULT_FONT_NAME, $arrFonts)) { + $xmlWriter->startElement('style:font-face'); + $xmlWriter->writeAttribute('style:name', PhpWord::DEFAULT_FONT_NAME); + $xmlWriter->writeAttribute('svg:font-family', PhpWord::DEFAULT_FONT_NAME); + $xmlWriter->endElement(); + } + $xmlWriter->endElement(); + } +} diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index b902ffb3..327aed34 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -29,7 +29,7 @@ use PhpOffice\PhpWord\TOC; /** * ODText content part writer */ -class Content extends AbstractWriterPart +class Content extends Base { /** * Write content file to XML format @@ -51,38 +51,12 @@ class Content extends AbstractWriterPart // office:document-content $xmlWriter->startElement('office:document-content'); - $xmlWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); - $xmlWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'); - $xmlWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'); - $xmlWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0'); - $xmlWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0'); - $xmlWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'); - $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); - $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); - $xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); - $xmlWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0'); - $xmlWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'); - $xmlWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0'); - $xmlWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0'); - $xmlWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML'); - $xmlWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0'); - $xmlWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0'); - $xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office'); - $xmlWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer'); - $xmlWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc'); - $xmlWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events'); + $this->writeCommonRootAttributes($xmlWriter); $xmlWriter->writeAttribute('xmlns:xforms', 'http://www.w3.org/2002/xforms'); $xmlWriter->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema'); $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); - $xmlWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report'); - $xmlWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2'); - $xmlWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml'); - $xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#'); - $xmlWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table'); $xmlWriter->writeAttribute('xmlns:field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0'); $xmlWriter->writeAttribute('xmlns:formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0'); - $xmlWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/'); - $xmlWriter->writeAttribute('office:version', '1.2'); // We firstly search all fonts used $sections = $phpWord->getSections(); @@ -122,37 +96,9 @@ class Content extends AbstractWriterPart } // office:font-face-decls - $xmlWriter->startElement('office:font-face-decls'); - $arrFonts = array(); - - $styles = Style::getStyles(); - $numFonts = 0; - if (count($styles) > 0) { - foreach ($styles as $styleName => $style) { - // Font - if ($style instanceof Font) { - $numFonts++; - $name = $style->getName(); - if (!in_array($name, $arrFonts)) { - $arrFonts[] = $name; - - // style:font-face - $xmlWriter->startElement('style:font-face'); - $xmlWriter->writeAttribute('style:name', $name); - $xmlWriter->writeAttribute('svg:font-family', $name); - $xmlWriter->endElement(); - } - } - } - if (!in_array(PhpWord::DEFAULT_FONT_NAME, $arrFonts)) { - $xmlWriter->startElement('style:font-face'); - $xmlWriter->writeAttribute('style:name', PhpWord::DEFAULT_FONT_NAME); - $xmlWriter->writeAttribute('svg:font-family', PhpWord::DEFAULT_FONT_NAME); - $xmlWriter->endElement(); - } - } - $xmlWriter->endElement(); + $this->writeFontFaces($xmlWriter); + // office:automatic-styles $xmlWriter->startElement('office:automatic-styles'); $styles = Style::getStyles(); $numPStyles = 0; diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index e94a1fd1..106ddfc5 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -33,8 +33,8 @@ class Manifest extends AbstractWriterPart // manifest:manifest $xmlWriter->startElement('manifest:manifest'); - $xmlWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0'); $xmlWriter->writeAttribute('manifest:version', '1.2'); + $xmlWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0'); // manifest:file-entry $xmlWriter->startElement('manifest:file-entry'); diff --git a/src/PhpWord/Writer/ODText/Meta.php b/src/PhpWord/Writer/ODText/Meta.php index 0df77827..8feb0d30 100644 --- a/src/PhpWord/Writer/ODText/Meta.php +++ b/src/PhpWord/Writer/ODText/Meta.php @@ -36,13 +36,13 @@ class Meta extends AbstractWriterPart // office:document-meta $xmlWriter->startElement('office:document-meta'); + $xmlWriter->writeAttribute('office:version', '1.2'); $xmlWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); $xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); $xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office'); $xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#'); - $xmlWriter->writeAttribute('office:version', '1.2'); // office:meta $xmlWriter->startElement('office:meta'); diff --git a/src/PhpWord/Writer/ODText/Styles.php b/src/PhpWord/Writer/ODText/Styles.php index 306ee5fc..53175165 100644 --- a/src/PhpWord/Writer/ODText/Styles.php +++ b/src/PhpWord/Writer/ODText/Styles.php @@ -18,7 +18,7 @@ use PhpOffice\PhpWord\Style\Table; /** * ODText styloes part writer */ -class Styles extends AbstractWriterPart +class Styles extends Base { /** * Write Styles file to XML format @@ -40,65 +40,10 @@ class Styles extends AbstractWriterPart // Styles:Styles $xmlWriter->startElement('office:document-styles'); - $xmlWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); - $xmlWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'); - $xmlWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'); - $xmlWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0'); - $xmlWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0'); - $xmlWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'); - $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); - $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); - $xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); - $xmlWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0'); - $xmlWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'); - $xmlWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0'); - $xmlWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0'); - $xmlWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML'); - $xmlWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0'); - $xmlWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0'); - $xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office'); - $xmlWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer'); - $xmlWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc'); - $xmlWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events'); - $xmlWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report'); - $xmlWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2'); - $xmlWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml'); - $xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#'); - $xmlWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table'); - $xmlWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/'); - $xmlWriter->writeAttribute('office:version', '1.2'); - + $this->writeCommonRootAttributes($xmlWriter); // office:font-face-decls - $xmlWriter->startElement('office:font-face-decls'); - $arrFonts = array(); - $styles = Style::getStyles(); - $numFonts = 0; - if (count($styles) > 0) { - foreach ($styles as $styleName => $style) { - // Font - if ($style instanceof Font) { - $numFonts++; - $name = $style->getName(); - if (!in_array($name, $arrFonts)) { - $arrFonts[] = $name; - - // style:font-face - $xmlWriter->startElement('style:font-face'); - $xmlWriter->writeAttribute('style:name', $name); - $xmlWriter->writeAttribute('svg:font-family', $name); - $xmlWriter->endElement(); - } - } - } - } - if (!in_array(PhpWord::DEFAULT_FONT_NAME, $arrFonts)) { - $xmlWriter->startElement('style:font-face'); - $xmlWriter->writeAttribute('style:name', PhpWord::DEFAULT_FONT_NAME); - $xmlWriter->writeAttribute('svg:font-family', PhpWord::DEFAULT_FONT_NAME); - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); + $this->writeFontFaces($xmlWriter); // office:styles $xmlWriter->startElement('office:styles'); diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index b426d570..5a489542 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -13,7 +13,6 @@ use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Footnote; use PhpOffice\PhpWord\Media; -use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Writer\Word2007\ContentTypes; use PhpOffice\PhpWord\Writer\Word2007\Rels; use PhpOffice\PhpWord\Writer\Word2007\DocProps; @@ -69,34 +68,13 @@ class Word2007 extends AbstractWriter implements WriterInterface /** * Save document by name * - * @param string $pFilename + * @param string $filename */ - public function save($pFilename = null) + public function save($filename = null) { if (!is_null($this->phpWord)) { - $pFilename = $this->getTempFile($pFilename); - - // Create new ZIP file and open it for writing - $zipClass = Settings::getZipClass(); - $objZip = new $zipClass(); - - // Retrieve OVERWRITE and CREATE constants from the instantiated zip class - // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP - $ro = new \ReflectionObject($objZip); - $zipOverWrite = $ro->getConstant('OVERWRITE'); - $zipCreate = $ro->getConstant('CREATE'); - - // Remove any existing file - if (file_exists($pFilename)) { - unlink($pFilename); - } - - // Try opening the ZIP file - if ($objZip->open($pFilename, $zipOverWrite) !== true) { - if ($objZip->open($pFilename, $zipCreate) !== true) { - throw new Exception("Could not open " . $pFilename . " for writing."); - } - } + $filename = $this->getTempFile($filename); + $objZip = $this->getZipArchive($filename); // Content types $this->cTypes['default'] = array( @@ -156,7 +134,7 @@ class Word2007 extends AbstractWriter implements WriterInterface // Close file if ($objZip->close() === false) { - throw new Exception("Could not close zip file $pFilename."); + throw new Exception("Could not close zip file $filename."); } $this->cleanupTempFile();