Skip to content

Commit

Permalink
Merge branch 'master' into wordunimplemented2
Browse files Browse the repository at this point in the history
  • Loading branch information
oleibman authored Feb 6, 2025
2 parents 346ac92 + 1be7a80 commit 56ea8c0
Show file tree
Hide file tree
Showing 46 changed files with 1,796 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ _build
/build
phpunit.xml
composer.phar
composer.lock
vendor
/report
/build
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@
"php": "^7.1|^8.0",
"ext-dom": "*",
"ext-gd": "*",
"ext-xml": "*",
"ext-zip": "*",
"ext-xml": "*",
"phpoffice/math": "^0.2"
},
"require-dev": {
Expand Down
5 changes: 4 additions & 1 deletion docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2731](https://github.com/PHPOffice/PHPWord/pull/2731)
- Writer ODText: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2735](https://github.com/PHPOffice/PHPWord/pull/2735)
- Add basic ruby text (phonetic guide) support for Word2007 and HTML Reader/Writer, RTF Writer, basic support for ODT writing by [@Deadpikle](https://github.com/Deadpikle) in [#2727](https://github.com/PHPOffice/PHPWord/pull/2727)

- Reader HTML: Support font styles for h1/h6 by [@Progi1984](https://github.com/Progi1984) fixing [#2619](https://github.com/PHPOffice/PHPWord/issues/2619) in [#2737](https://github.com/PHPOffice/PHPWord/pull/2737)
- Writer EPub3: Basic support by [@Sambit003](https://github.com/Sambit003) fixing [#55](https://github.com/PHPOffice/PHPWord/issues/55) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724)

### Bug fixes

- Writer ODText: Support for images inside a textRun by [@Progi1984](https://github.com/Progi1984) fixing [#2240](https://github.com/PHPOffice/PHPWord/issues/2240) in [#2668](https://github.com/PHPOffice/PHPWord/pull/2668)
Expand All @@ -24,6 +26,7 @@
- Reader Word2007: Respect paragraph indent units by [@tugmaks](https://github.com/tugmaks) & [@Progi1984](https://github.com/Progi1984) fixing [#507](https://github.com/PHPOffice/PHPWord/issues/507) in [#2726](https://github.com/PHPOffice/PHPWord/pull/2726)
- Reader Word2007: Support Header elements within Title elements by [@SpraxDev](https://github.com/SpraxDev) fixing [#2616](https://github.com/PHPOffice/PHPWord/issues/2616), [#2426](https://github.com/PHPOffice/PHPWord/issues/2426) in [#2674](https://github.com/PHPOffice/PHPWord/pull/2674)
- Reader HTML: Support for inherit value for property line-height by [@Progi1984](https://github.com/Progi1984) fixing [#2683](https://github.com/PHPOffice/PHPWord/issues/2683) in [#2733](https://github.com/PHPOffice/PHPWord/pull/2733)
- Writer HTML: Fixed null string for Text Elements by [@armagedon007](https://github.com/armagedon007) and [@Progi1984](https://github.com/Progi1984) in [#2738](https://github.com/PHPOffice/PHPWord/pull/2738)

### Miscellaneous

Expand Down
2 changes: 1 addition & 1 deletion samples/Sample_Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}

// Set writers
$writers = ['Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html', 'PDF' => 'pdf'];
$writers = ['Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html', 'PDF' => 'pdf', 'EPub3' => 'epub'];

// Set PDF renderer
if (null === Settings::getPdfRendererPath()) {
Expand Down
4 changes: 1 addition & 3 deletions src/PhpWord/Element/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ public function getSource()

/**
* Get link text.
*
* @return string
*/
public function getText()
public function getText(): string
{
return $this->text;
}
Expand Down
4 changes: 1 addition & 3 deletions src/PhpWord/Element/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ public function setText($text)

/**
* Get Text content.
*
* @return ?string
*/
public function getText()
public function getText(): ?string
{
return $this->text;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/IOFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class IOFactory
*/
public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
{
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF'], true)) {
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF', 'EPub3'], true)) {
throw new Exception("\"{$name}\" is not a valid writer.");
}

Expand Down
20 changes: 10 additions & 10 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,16 @@ protected static function parseNode($node, $element, $styles = [], $data = []):

// Node mapping table
$nodes = [
// $method $node $element $styles $data $argument1 $argument2
'p' => ['Paragraph', $node, $element, $styles, null, null, null],
'h1' => ['Heading', $node, $element, $styles, null, 'Heading1', null],
'h2' => ['Heading', $node, $element, $styles, null, 'Heading2', null],
'h3' => ['Heading', $node, $element, $styles, null, 'Heading3', null],
'h4' => ['Heading', $node, $element, $styles, null, 'Heading4', null],
'h5' => ['Heading', $node, $element, $styles, null, 'Heading5', null],
'h6' => ['Heading', $node, $element, $styles, null, 'Heading6', null],
'#text' => ['Text', $node, $element, $styles, null, null, null],
'strong' => ['Property', null, null, $styles, null, 'bold', true],
// $method $node $element $styles $data $argument1 $argument2
'p' => ['Paragraph', $node, $element, $styles, null, null, null],
'h1' => ['Heading', $node, $element, $styles, null, 'Heading1', null],
'h2' => ['Heading', $node, $element, $styles, null, 'Heading2', null],
'h3' => ['Heading', $node, $element, $styles, null, 'Heading3', null],
'h4' => ['Heading', $node, $element, $styles, null, 'Heading4', null],
'h5' => ['Heading', $node, $element, $styles, null, 'Heading5', null],
'h6' => ['Heading', $node, $element, $styles, null, 'Heading6', null],
'#text' => ['Text', $node, $element, $styles, null, null, null],
'strong' => ['Property', null, null, $styles, null, 'bold', true],
'b' => ['Property', null, null, $styles, null, 'bold', true],
'em' => ['Property', null, null, $styles, null, 'italic', true],
'i' => ['Property', null, null, $styles, null, 'italic', true],
Expand Down
11 changes: 11 additions & 0 deletions src/PhpWord/Shared/ZipArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,15 @@ public function pclzipLocateName($filename)

return ($listIndex > -1) ? $listIndex : false;
}

/**
* Add an empty directory to the zip archive (emulate \ZipArchive).
*
* @param string $dirname Directory name to add to the zip archive
*/
public function addEmptyDir(string $dirname): bool
{
// Create a directory entry by adding an empty file with trailing slash
return $this->addFromString(rtrim($dirname, '/') . '/', '');
}
}
6 changes: 2 additions & 4 deletions src/PhpWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Font extends AbstractStyle
/**
* Font color.
*
* @var string
* @var null|string
*/
private $color;

Expand Down Expand Up @@ -426,10 +426,8 @@ public function setSize($value = null)

/**
* Get font color.
*
* @return string
*/
public function getColor()
public function getColor(): ?string
{
return $this->color;
}
Expand Down
91 changes: 91 additions & 0 deletions src/PhpWord/Writer/EPub3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Writer\EPub3\Part\AbstractPart;

/**
* EPub3 writer.
*/
class EPub3 extends AbstractWriter implements WriterInterface
{
/**
* Create new EPub3 writer.
*/
public function __construct(?PhpWord $phpWord = null)
{
// Assign PhpWord
$this->setPhpWord($phpWord);

// Create parts
$this->parts = [
'Mimetype' => 'mimetype',
'Content' => 'content.opf',
'Toc' => 'toc.ncx',
'Styles' => 'styles.css',
'Manifest' => 'META-INF/container.xml',
'Nav' => 'nav.xhtml',
'ContentXhtml' => 'content.xhtml',
];
foreach (array_keys($this->parts) as $partName) {
$partClass = static::class . '\\Part\\' . $partName;
if (class_exists($partClass)) {
/** @var WriterPartInterface $part */
$part = new $partClass($partName === 'Content' || $partName === 'ContentXhtml' ? $phpWord : null);
$part->setParentWriter($this);
$this->writerParts[strtolower($partName)] = $part;
}
}

// Set package paths
$this->mediaPaths = ['image' => 'Images/', 'object' => 'Objects/'];
}

/**
* Save PhpWord to file.
*/
public function save(string $filename): void
{
$filename = $this->getTempFile($filename);
$zip = $this->getZipArchive($filename);

// Add mimetype first without compression
$zip->addFromString('mimetype', 'application/epub+zip');
$zip->addEmptyDir('META-INF');

// Add other files
foreach ($this->parts as $partName => $fileName) {
if ($fileName === '') {
continue;
}
$part = $this->getWriterPart($partName);
if (!$part instanceof AbstractPart) {
continue;
}
$zip->addFromString($fileName, $part->write());
}

// Close zip archive
$zip->close();

// Cleanup temp file
$this->cleanupTempFile();
}
}
45 changes: 45 additions & 0 deletions src/PhpWord/Writer/EPub3/Element/AbstractElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\EPub3\Element;

use PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement as Word2007AbstractElement;

/**
* Abstract element writer.
*
* @since 0.11.0
*/
abstract class AbstractElement extends Word2007AbstractElement
{
/**
* Get class name of writer element based on read element.
*
* @param \PhpOffice\PhpWord\Element\AbstractElement $element
*/
public static function getElementClass($element): string
{
$elementClass = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($element));
$writerClass = 'PhpOffice\\PhpWord\\Writer\\EPub3\\Element\\' . $elementClass;
if (!class_exists($writerClass)) {
throw new \PhpOffice\PhpWord\Exception\Exception("Writer element class {$writerClass} not found.");
}

return $writerClass;
}
}
45 changes: 45 additions & 0 deletions src/PhpWord/Writer/EPub3/Element/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace PhpOffice\PhpWord\Writer\EPub3\Element;

use PhpOffice\PhpWord\Element\Image as ImageElement;

/**
* Image element writer for EPub3.
*/
class Image extends AbstractElement
{
/**
* Write element.
*/
public function write(): void
{
$xmlWriter = $this->getXmlWriter();
$xmlWriter->setIndent(false);
$element = $this->getElement();
if (!$element instanceof ImageElement) {
return;
}
$mediaIndex = $element->getMediaIndex();
$target = 'media/image' . $mediaIndex . '.' . $element->getImageExtension();
if (!$this->withoutP) {
$xmlWriter->startElement('p');
}
$xmlWriter->startElement('img');
$xmlWriter->writeAttribute('src', $target);
$style = '';
if ($element->getStyle()->getWidth() !== null) {
$style .= 'width:' . $element->getStyle()->getWidth() . 'px;';
}
if ($element->getStyle()->getHeight() !== null) {
$style .= 'height:' . $element->getStyle()->getHeight() . 'px;';
}
if ($style !== '') {
$xmlWriter->writeAttribute('style', $style);
}
$xmlWriter->endElement(); // img
if (!$this->withoutP) {
$xmlWriter->endElement(); // p
}
}
}
50 changes: 50 additions & 0 deletions src/PhpWord/Writer/EPub3/Element/Text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace PhpOffice\PhpWord\Writer\EPub3\Element;

/**
* Text element writer for EPub3.
*/
class Text extends AbstractElement
{
/**
* Write element.
*/
public function write(): void
{
$xmlWriter = $this->getXmlWriter();
$xmlWriter->setIndent(true);
$xmlWriter->setIndentString(' ');
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Text) {
return;
}

$fontStyle = $element->getFontStyle();
$paragraphStyle = $element->getParagraphStyle();

if (!$this->withoutP) {
$xmlWriter->startElement('p');
if (is_string($paragraphStyle) && $paragraphStyle !== '') {
$xmlWriter->writeAttribute('class', $paragraphStyle);
}
}

if (!empty($fontStyle)) {
$xmlWriter->startElement('span');
if (is_string($fontStyle)) {
$xmlWriter->writeAttribute('class', $fontStyle);
}
}

$xmlWriter->text($element->getText());

if (!empty($fontStyle)) {
$xmlWriter->endElement(); // span
}

if (!$this->withoutP) {
$xmlWriter->endElement(); // p
}
}
}
Loading

0 comments on commit 56ea8c0

Please sign in to comment.