Skip to content

Formula : Add Element (& Writer/Reader Word2007/ODText) #2477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: CI
on:
- push
- pull_request
pull_request:
push:
branches:
- master
jobs:
test:
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"ext-dom": "*",
"ext-json": "*",
"ext-xml": "*",
"laminas/laminas-escaper": ">=2.6"
"laminas/laminas-escaper": ">=2.6",
"phpoffice/math": "^0.1"
},
"require-dev": {
"ext-zip": "*",
Expand Down
55 changes: 54 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/changes/1.x/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Word2007 Reader : Added support for Comments by [@shaedrich](https://github.com/shaedrich) in [#2161](https://github.com/PHPOffice/PHPWord/pull/2161) & [#2469](https://github.com/PHPOffice/PHPWord/pull/2469)
- Word2007 Reader/Writer: Permit book-fold printing by [@potofcoffee](https://github.com/potofcoffee) in [#2225](https://github.com/PHPOffice/PHPWord/pull/2225) & [#2470](https://github.com/PHPOffice/PHPWord/pull/2470)
- Word2007 Writer : Add PageNumber to TOC by [@jet-desk](https://github.com/jet-desk) in [#1652](https://github.com/PHPOffice/PHPWord/pull/1652) & [#2471](https://github.com/PHPOffice/PHPWord/pull/2471)
- Word2007 Reader/Writer + ODText Reader/Writer : Add Element Formula in by [@Progi1984](https://github.com/Progi1984) in [#2477](https://github.com/PHPOffice/PHPWord/pull/2477)

### Bug fixes

Expand Down
8 changes: 4 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ Below are the supported features for each file formats.
| **Graphs** | 2D basic graphs | :material-check: | | | | |
| | 2D advanced graphs | | | | | |
| | 3D graphs | :material-check: | | | | |
| **Math** | OMML support | | | | | |
| | MathML support | | | | | |
| **Math** | OMML support | :material-check: | | | | |
| | MathML support | | :material-check: | | | |
| **Bonus** | Encryption | | | | | |
| | Protection | | | | | |

Expand Down Expand Up @@ -99,8 +99,8 @@ Below are the supported features for each file formats.
| **Graphs** | 2D basic graphs | | | | | |
| | 2D advanced graphs | | | | | |
| | 3D graphs | | | | | |
| **Math** | OMML support | | | | | |
| | MathML support | | | | | |
| **Math** | OMML support | :material-check: | | | | |
| | MathML support | | :material-check: | | | |
| **Bonus** | Encryption | | | | | |
| | Protection | | | | | |

Expand Down
21 changes: 21 additions & 0 deletions docs/usage/elements/formula.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Formula

Formula can be added using

``` php
<?php

use PhpOffice\Math\Element;
use PhpOffice\Math\Math;

$fraction = new Element\Fraction();
$fraction
->setDenominator(new Element\Numeric(2))
->setNumerator(new Element\Identifier('π'))
;

$math = new Math();
$math->add($fraction);

$formula = $section->addFormula($math);
```
3 changes: 3 additions & 0 deletions src/PhpWord/Element/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Element;

use BadMethodCallException;
use PhpOffice\Math\Math;
use ReflectionClass;

/**
Expand Down Expand Up @@ -47,6 +48,7 @@
* @method Chart addChart(string $type, array $categories, array $values, array $style = null, $seriesName = null)
* @method FormField addFormField(string $type, mixed $fStyle = null, mixed $pStyle = null)
* @method SDT addSDT(string $type)
* @method Formula addFormula(Math $math)
* @method \PhpOffice\PhpWord\Element\OLEObject addObject(string $source, mixed $style = null) deprecated, use addOLEObject instead
*
* @since 0.10.0
Expand Down Expand Up @@ -88,6 +90,7 @@ public function __call($function, $args)
'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field',
'Line', 'Shape', 'Title', 'TOC', 'PageBreak',
'Chart', 'FormField', 'SDT', 'Comment',
'Formula',
];
$functions = [];
foreach ($elements as $element) {
Expand Down
53 changes: 53 additions & 0 deletions src/PhpWord/Element/Formula.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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
*/

declare(strict_types=1);

namespace PhpOffice\PhpWord\Element;

use PhpOffice\Math\Math;

/**
* Formula element.
*/
class Formula extends AbstractElement
{
/**
* @var Math
*/
protected $math;

/**
* Create a new Formula Element.
*/
public function __construct(Math $math)
{
$this->setMath($math);
}

public function setMath(Math $math): self
{
$this->math = $math;

return $this;
}

public function getMath(): Math
{
return $this->math;
}
}
13 changes: 2 additions & 11 deletions src/PhpWord/Reader/ODText.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,8 @@ public function load($docFile)

/**
* Read document part.
*
* @param array $relationships
* @param string $partName
* @param string $docFile
* @param string $xmlFile
*/
private function readPart(PhpWord $phpWord, $relationships, $partName, $docFile, $xmlFile): void
private function readPart(PhpWord $phpWord, array $relationships, string $partName, string $docFile, string $xmlFile): void
{
$partClass = "PhpOffice\\PhpWord\\Reader\\ODText\\{$partName}";
if (class_exists($partClass)) {
Expand All @@ -72,12 +67,8 @@ private function readPart(PhpWord $phpWord, $relationships, $partName, $docFile,

/**
* Read all relationship files.
*
* @param string $docFile
*
* @return array
*/
private function readRelationships($docFile)
private function readRelationships(string $docFile): array
{
$rels = [];
$xmlFile = 'META-INF/manifest.xml';
Expand Down
71 changes: 45 additions & 26 deletions src/PhpWord/Reader/ODText/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Reader\ODText;

use DateTime;
use PhpOffice\Math\Reader\MathML;
use PhpOffice\PhpWord\Element\TrackChange;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
Expand Down Expand Up @@ -51,37 +52,55 @@ public function read(PhpWord $phpWord): void

break;
case 'text:p': // Paragraph
$children = $node->childNodes;
foreach ($children as $child) {
switch ($child->nodeName) {
case 'text:change-start':
$changeId = $child->getAttribute('text:change-id');
if (isset($trackedChanges[$changeId])) {
$changed = $trackedChanges[$changeId];
}
$element = $xmlReader->getElement('draw:frame/draw:object', $node);
if ($element) {
$mathFile = str_replace('./', '', $element->getAttribute('xlink:href')) . '/content.xml';

break;
case 'text:change-end':
unset($changed);
$xmlReaderObject = new XMLReader();
$mathElement = $xmlReaderObject->getDomFromZip($this->docFile, $mathFile);
if ($mathElement) {
$mathXML = $mathElement->saveXML($mathElement);

break;
case 'text:change':
$changeId = $child->getAttribute('text:change-id');
if (isset($trackedChanges[$changeId])) {
$changed = $trackedChanges[$changeId];
}
if (is_string($mathXML)) {
$reader = new MathML();
$math = $reader->read($mathXML);

break;
$section->addFormula($math);
}
}
}
} else {
$children = $node->childNodes;
foreach ($children as $child) {
switch ($child->nodeName) {
case 'text:change-start':
$changeId = $child->getAttribute('text:change-id');
if (isset($trackedChanges[$changeId])) {
$changed = $trackedChanges[$changeId];
}

break;
case 'text:change-end':
unset($changed);

$element = $section->addText($node->nodeValue);
if (isset($changed) && is_array($changed)) {
$element->setTrackChange($changed['changed']);
if (isset($changed['textNodes'])) {
foreach ($changed['textNodes'] as $changedNode) {
$element = $section->addText($changedNode->nodeValue);
$element->setTrackChange($changed['changed']);
break;
case 'text:change':
$changeId = $child->getAttribute('text:change-id');
if (isset($trackedChanges[$changeId])) {
$changed = $trackedChanges[$changeId];
}

break;
}
}

$element = $section->addText($node->nodeValue);
if (isset($changed) && is_array($changed)) {
$element->setTrackChange($changed['changed']);
if (isset($changed['textNodes'])) {
foreach ($changed['textNodes'] as $changedNode) {
$element = $section->addText($changedNode->nodeValue);
$element->setTrackChange($changed['changed']);
}
}
}
}
Expand Down
Loading